Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
a440fb47
H
hub
项目概览
github
/
hub
大约 1 年 前同步成功
通知
3
Star
22523
Fork
2406
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a440fb47
编写于
7月 18, 2013
作者:
J
Jingwen Owen Ou
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'apply'
上级
2d12099f
ff579946
变更
9
显示空白变更内容
内联
并排
Showing
9 changed file
with
164 addition
and
10 deletion
+164
-10
commands/apply.go
commands/apply.go
+83
-0
commands/apply_test.go
commands/apply_test.go
+44
-0
commands/commands.go
commands/commands.go
+1
-0
commands/fetch.go
commands/fetch.go
+8
-3
commands/pull_request.go
commands/pull_request.go
+6
-3
commands/remote.go
commands/remote.go
+6
-1
commands/remote_test.go
commands/remote_test.go
+8
-0
commands/runner.go
commands/runner.go
+6
-1
commands/utils.go
commands/utils.go
+2
-2
未找到文件。
commands/apply.go
0 → 100644
浏览文件 @
a440fb47
package
commands
import
(
"fmt"
"os"
"path/filepath"
"regexp"
)
var
cmdApply
=
&
Command
{
Run
:
apply
,
GitExtension
:
true
,
Usage
:
"apply GITHUB-URL"
,
Short
:
"Apply a patch to files and/or to the index"
,
Long
:
`Downloads the patch file for the pull request or commit at the URL and
applies that patch from disk with git am or git apply. Similar to
cherry-pick, but doesn't add new remotes. git am creates commits while
preserving authorship info while <code>apply</code> only applies the
patch to the working copy.
`
,
}
/*
$ gh apply https://github.com/jingweno/gh/pull/55
> curl https://github.com/jingweno/gh/pull/55.patch -o /tmp/55.patch
> git apply /tmp/55.patch
$ git apply --ignore-whitespace https://github.com/jingweno/gh/commit/fdb9921
> curl https://github.com/jingweno/gh/commit/fdb9921.patch -o /tmp/fdb9921.patch
> git apply --ignore-whitespace /tmp/fdb9921.patch
$ git apply https://gist.github.com/8da7fb575debd88c54cf
> curl https://gist.github.com/8da7fb575debd88c54cf.txt -o /tmp/gist-8da7fb575debd88c54cf.txt
> git apply /tmp/gist-8da7fb575debd88c54cf.txt
*/
func
apply
(
command
*
Command
,
args
*
Args
)
{
if
!
args
.
IsParamsEmpty
()
{
transformApplyArgs
(
args
)
}
}
func
transformApplyArgs
(
args
*
Args
)
{
urlRegexp
:=
regexp
.
MustCompile
(
"^https?://(gist
\\
.)?github
\\
.com/"
)
for
_
,
url
:=
range
args
.
Params
{
if
urlRegexp
.
MatchString
(
url
)
{
idx
:=
args
.
IndexOfParam
(
url
)
gist
:=
urlRegexp
.
FindStringSubmatch
(
url
)[
1
]
==
"gist."
fragmentRegexp
:=
regexp
.
MustCompile
(
"#.+"
)
url
=
fragmentRegexp
.
ReplaceAllString
(
url
,
""
)
pullRegexp
:=
regexp
.
MustCompile
(
"(/pull/
\\
d+)/
\\
w*$"
)
if
!
gist
{
if
pullRegexp
.
MatchString
(
url
)
{
pull
:=
pullRegexp
.
FindStringSubmatch
(
url
)[
1
]
url
=
pullRegexp
.
ReplaceAllString
(
url
,
pull
)
}
}
var
ext
string
if
gist
{
ext
=
".txt"
}
else
{
ext
=
".patch"
}
if
filepath
.
Ext
(
url
)
!=
ext
{
url
+=
ext
}
var
prefix
string
if
gist
{
prefix
=
"gist-"
}
patchFile
:=
filepath
.
Join
(
os
.
TempDir
(),
prefix
+
filepath
.
Base
(
url
))
args
.
Before
(
"curl"
,
"-#LA"
,
fmt
.
Sprintf
(
"gh %s"
,
Version
),
url
,
"-o"
,
patchFile
)
args
.
Params
[
idx
]
=
patchFile
break
}
}
}
commands/apply_test.go
0 → 100644
浏览文件 @
a440fb47
package
commands
import
(
"fmt"
"github.com/bmizerany/assert"
"regexp"
"strings"
"testing"
)
func
TestTransformApplyArgs
(
t
*
testing
.
T
)
{
args
:=
NewArgs
([]
string
{
"apply"
,
"https://github.com/jingweno/gh/pull/55"
})
transformApplyArgs
(
args
)
cmds
:=
args
.
Commands
()
assert
.
Equal
(
t
,
2
,
len
(
cmds
))
curlString
:=
fmt
.
Sprintf
(
"curl -#LA %s https://github.com/jingweno/gh/pull/55.patch -o .+/55.patch"
,
fmt
.
Sprintf
(
"gh %s"
,
Version
))
curlRegexp
:=
regexp
.
MustCompile
(
curlString
)
applyString
:=
"git apply"
assert
.
T
(
t
,
curlRegexp
.
MatchString
(
cmds
[
0
]
.
String
()))
assert
.
T
(
t
,
strings
.
Contains
(
cmds
[
1
]
.
String
(),
applyString
))
args
=
NewArgs
([]
string
{
"apply"
,
"--ignore-whitespace"
,
"https://github.com/jingweno/gh/commit/fdb9921"
})
transformApplyArgs
(
args
)
cmds
=
args
.
Commands
()
assert
.
Equal
(
t
,
2
,
len
(
cmds
))
curlString
=
fmt
.
Sprintf
(
"curl -#LA %s https://github.com/jingweno/gh/commit/fdb9921.patch -o .+/fdb9921.patch"
,
fmt
.
Sprintf
(
"gh %s"
,
Version
))
curlRegexp
=
regexp
.
MustCompile
(
curlString
)
applyString
=
"git apply --ignore-whitespace"
assert
.
T
(
t
,
curlRegexp
.
MatchString
(
cmds
[
0
]
.
String
()))
assert
.
T
(
t
,
strings
.
Contains
(
cmds
[
1
]
.
String
(),
applyString
))
args
=
NewArgs
([]
string
{
"apply"
,
"https://gist.github.com/8da7fb575debd88c54cf"
})
transformApplyArgs
(
args
)
cmds
=
args
.
Commands
()
assert
.
Equal
(
t
,
2
,
len
(
cmds
))
curlString
=
fmt
.
Sprintf
(
"curl -#LA %s https://gist.github.com/8da7fb575debd88c54cf.txt -o .+8da7fb575debd88c54cf.txt"
,
fmt
.
Sprintf
(
"gh %s"
,
Version
))
curlRegexp
=
regexp
.
MustCompile
(
curlString
)
applyString
=
"git apply"
assert
.
T
(
t
,
curlRegexp
.
MatchString
(
cmds
[
0
]
.
String
()))
assert
.
T
(
t
,
strings
.
Contains
(
cmds
[
1
]
.
String
(),
applyString
))
}
commands/commands.go
浏览文件 @
a440fb47
...
...
@@ -54,6 +54,7 @@ var Basic = []*Command{
var
Branching
=
[]
*
Command
{
cmdCheckout
,
cmdMerge
,
cmdApply
,
}
var
Remote
=
[]
*
Command
{
...
...
commands/fetch.go
浏览文件 @
a440fb47
...
...
@@ -35,13 +35,16 @@ remotes are only added if they correspond to valid forks on GitHub.
*/
func
fetch
(
command
*
Command
,
args
*
Args
)
{
if
!
args
.
IsParamsEmpty
()
{
tranformFetchArgs
(
args
)
err
:=
tranformFetchArgs
(
args
)
utils
.
Check
(
err
)
}
}
func
tranformFetchArgs
(
args
*
Args
)
{
func
tranformFetchArgs
(
args
*
Args
)
error
{
remotes
,
err
:=
git
.
Remotes
()
utils
.
Check
(
err
)
if
err
!=
nil
{
return
err
}
names
:=
parseRemoteNames
(
args
)
gh
:=
github
.
New
()
...
...
@@ -67,6 +70,8 @@ func tranformFetchArgs(args *Args) {
}
args
.
Before
(
"git"
,
"remote"
,
"add"
,
project
.
Owner
,
project
.
GitURL
(
""
,
""
,
isSSH
))
}
return
nil
}
func
parseRemoteNames
(
args
*
Args
)
(
names
[]
string
)
{
...
...
commands/pull_request.go
浏览文件 @
a440fb47
...
...
@@ -181,17 +181,19 @@ func readTitleAndBodyFrom(reader *bufio.Reader) (title, body string, err error)
r
:=
regexp
.
MustCompile
(
"
\\
S"
)
var
titleParts
,
bodyParts
[]
string
line
,
err
:=
read
ln
(
reader
)
line
,
err
:=
read
Line
(
reader
)
for
err
==
nil
{
if
strings
.
HasPrefix
(
line
,
"#"
)
{
break
}
if
len
(
bodyParts
)
==
0
&&
r
.
MatchString
(
line
)
{
titleParts
=
append
(
titleParts
,
line
)
}
else
{
bodyParts
=
append
(
bodyParts
,
line
)
}
line
,
err
=
readln
(
reader
)
line
,
err
=
readLine
(
reader
)
}
title
=
strings
.
Join
(
titleParts
,
" "
)
...
...
@@ -203,12 +205,13 @@ func readTitleAndBodyFrom(reader *bufio.Reader) (title, body string, err error)
return
title
,
body
,
nil
}
func
read
ln
(
r
*
bufio
.
Reader
)
(
string
,
error
)
{
func
read
Line
(
r
*
bufio
.
Reader
)
(
string
,
error
)
{
var
(
isPrefix
=
true
err
error
line
,
ln
[]
byte
)
for
isPrefix
&&
err
==
nil
{
line
,
isPrefix
,
err
=
r
.
ReadLine
()
ln
=
append
(
ln
,
line
...
)
...
...
commands/remote.go
浏览文件 @
a440fb47
...
...
@@ -40,15 +40,20 @@ func transformRemoteArgs(args *Args) {
if
!
match
{
return
}
isPriavte
:=
parseRemotePrivateFlag
(
args
)
var
err
error
if
name
==
""
{
name
,
err
=
utils
.
DirName
()
utils
.
Check
(
err
)
}
isPriavte
:=
parseRemotePrivateFlag
(
args
)
if
owner
==
"origin"
{
owner
=
github
.
CurrentConfig
()
.
FetchUser
()
}
else
if
args
.
ParamsSize
()
>
2
{
// `git remote add jingweno foo/bar`
args
.
RemoveParam
(
args
.
ParamsSize
()
-
1
)
}
project
:=
github
.
Project
{
Owner
:
owner
,
Name
:
name
}
...
...
commands/remote_test.go
浏览文件 @
a440fb47
...
...
@@ -49,4 +49,12 @@ func TestTransformRemoteArgs(t *testing.T) {
assert
.
Equal
(
t
,
"jingweno"
,
args
.
GetParam
(
1
))
assert
.
Equal
(
t
,
"add"
,
args
.
FirstParam
())
assert
.
Equal
(
t
,
"git@github.com:jingweno/gh.git"
,
args
.
GetParam
(
2
))
args
=
NewArgs
([]
string
{
"remote"
,
"add"
,
"-p"
,
"origin"
,
"org/foo"
})
transformRemoteArgs
(
args
)
assert
.
Equal
(
t
,
3
,
args
.
ParamsSize
())
assert
.
Equal
(
t
,
"origin"
,
args
.
GetParam
(
1
))
assert
.
Equal
(
t
,
"add"
,
args
.
FirstParam
())
assert
.
Equal
(
t
,
"git@github.com:org/foo.git"
,
args
.
GetParam
(
2
))
}
commands/runner.go
浏览文件 @
a440fb47
package
commands
import
(
"flag"
"fmt"
"github.com/jingweno/gh/cmd"
"github.com/jingweno/gh/git"
...
...
@@ -26,8 +27,12 @@ func (r *Runner) Execute() error {
cmd
.
PrintUsage
()
}
if
err
:=
cmd
.
Flag
.
Parse
(
args
.
Params
);
err
!=
nil
{
if
err
==
flag
.
ErrHelp
{
return
nil
}
else
{
return
err
}
}
args
.
Params
=
cmd
.
Flag
.
Args
()
}
...
...
commands/utils.go
浏览文件 @
a440fb47
...
...
@@ -60,7 +60,7 @@ func convertToGitURL(pullRequest *octokat.PullRequest) (string, error) {
return
project
.
GitURL
(
""
,
user
,
isSSH
),
nil
}
func
parseRepoNameOwner
(
nameWithOwner
string
)
(
owner
,
repo
string
,
match
bool
)
{
func
parseRepoNameOwner
(
nameWithOwner
string
)
(
owner
,
name
string
,
match
bool
)
{
ownerRe
:=
fmt
.
Sprintf
(
"^(%s)$"
,
OwnerRe
)
ownerRegexp
:=
regexp
.
MustCompile
(
ownerRe
)
if
ownerRegexp
.
MatchString
(
nameWithOwner
)
{
...
...
@@ -74,7 +74,7 @@ func parseRepoNameOwner(nameWithOwner string) (owner, repo string, match bool) {
if
nameWithOwnerRegexp
.
MatchString
(
nameWithOwner
)
{
result
:=
nameWithOwnerRegexp
.
FindStringSubmatch
(
nameWithOwner
)
owner
=
result
[
1
]
repo
=
result
[
2
]
name
=
result
[
2
]
match
=
true
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录