Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
github
hub
提交
04e2d43b
H
hub
项目概览
github
/
hub
12 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
04e2d43b
编写于
12月 16, 2013
作者:
J
Jingwen Owen Ou
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request jingweno/gh:116 from jingweno/remote_compat
Remote is compatible with Hub
上级
4b352e58
4316c40c
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
61 addition
and
58 deletion
+61
-58
commands/remote.go
commands/remote.go
+48
-12
commands/remote_test.go
commands/remote_test.go
+12
-2
commands/utils.go
commands/utils.go
+0
-23
commands/utils_test.go
commands/utils_test.go
+0
-20
github/project.go
github/project.go
+1
-1
未找到文件。
commands/remote.go
浏览文件 @
04e2d43b
package
commands
import
(
"fmt"
"github.com/jingweno/gh/github"
"github.com/jingweno/gh/utils"
"regexp"
)
var
cmdRemote
=
&
Command
{
...
...
@@ -29,36 +31,51 @@ then uses your GitHub login.
> git remote add origin git://github.com/YOUR_LOGIN/THIS_REPO.git
*/
func
remote
(
command
*
Command
,
args
*
Args
)
{
if
args
.
ParamsSize
()
>=
2
&&
(
args
.
FirstParam
()
==
"add"
||
args
.
FirstParam
()
==
"set-url"
)
{
if
!
args
.
IsParamsEmpty
()
&&
(
args
.
FirstParam
()
==
"add"
||
args
.
FirstParam
()
==
"set-url"
)
{
transformRemoteArgs
(
args
)
}
}
func
transformRemoteArgs
(
args
*
Args
)
{
ownerWithName
:=
args
.
LastParam
()
owner
,
name
,
match
:=
parseRepoNameOwner
(
ownerWithName
)
if
!
match
{
owner
,
name
:=
parseRepoNameOwner
(
ownerWithName
)
if
owner
==
""
{
return
}
var
err
error
localRepo
:=
github
.
LocalRepo
()
var
repoName
string
if
name
==
""
{
name
,
err
=
utils
.
DirName
()
utils
.
Check
(
err
)
project
,
err
:=
localRepo
.
MainProject
()
if
err
==
nil
{
repoName
=
project
.
Name
}
else
{
repoName
,
err
=
utils
.
DirName
()
utils
.
Check
(
err
)
}
name
=
repoName
}
words
:=
args
.
Words
()
isPriavte
:=
parseRemotePrivateFlag
(
args
)
if
owner
==
"origin"
{
owner
=
github
.
CurrentConfig
()
.
FetchUser
()
}
else
if
args
.
ParamsSize
()
>
2
{
// `git remote add jingweno foo/bar`
if
len
(
words
)
==
2
&&
words
[
1
]
==
"origin"
{
// gh add origin
credentials
:=
github
.
CurrentConfigs
()
.
DefaultCredentials
()
owner
=
credentials
.
User
name
=
repoName
}
else
if
len
(
words
)
==
2
{
// gh remote add jingweno foo/bar
if
idx
:=
args
.
IndexOfParam
(
words
[
1
]);
idx
!=
-
1
{
args
.
ReplaceParam
(
idx
,
owner
)
}
}
else
{
args
.
RemoveParam
(
args
.
ParamsSize
()
-
1
)
}
project
:=
github
.
NewProject
(
owner
,
name
,
""
)
// for GitHub Enterprise
isPriavte
=
isPriavte
||
project
.
Host
!=
github
.
GitHubHost
url
:=
project
.
GitURL
(
name
,
owner
,
isPriavte
)
args
.
AppendParams
(
url
)
}
...
...
@@ -70,3 +87,22 @@ func parseRemotePrivateFlag(args *Args) bool {
return
false
}
func
parseRepoNameOwner
(
nameWithOwner
string
)
(
owner
,
name
string
)
{
ownerRe
:=
fmt
.
Sprintf
(
"^(%s)$"
,
OwnerRe
)
ownerRegexp
:=
regexp
.
MustCompile
(
ownerRe
)
if
ownerRegexp
.
MatchString
(
nameWithOwner
)
{
owner
=
ownerRegexp
.
FindStringSubmatch
(
nameWithOwner
)[
1
]
return
}
nameWithOwnerRe
:=
fmt
.
Sprintf
(
"^(%s)
\\
/(%s)$"
,
OwnerRe
,
NameRe
)
nameWithOwnerRegexp
:=
regexp
.
MustCompile
(
nameWithOwnerRe
)
if
nameWithOwnerRegexp
.
MatchString
(
nameWithOwner
)
{
result
:=
nameWithOwnerRegexp
.
FindStringSubmatch
(
nameWithOwner
)
owner
=
result
[
1
]
name
=
result
[
2
]
}
return
}
commands/remote_test.go
浏览文件 @
04e2d43b
...
...
@@ -8,8 +8,20 @@ import (
"testing"
)
func
TestParseRepoNameOwner
(
t
*
testing
.
T
)
{
owner
,
repo
:=
parseRepoNameOwner
(
"jingweno"
)
assert
.
Equal
(
t
,
"jingweno"
,
owner
)
assert
.
Equal
(
t
,
""
,
repo
)
owner
,
repo
=
parseRepoNameOwner
(
"jingweno/gh"
)
assert
.
Equal
(
t
,
"jingweno"
,
owner
)
assert
.
Equal
(
t
,
"gh"
,
repo
)
}
func
TestTransformRemoteArgs
(
t
*
testing
.
T
)
{
os
.
Setenv
(
"GH_PROTOCOL"
,
"git"
)
github
.
CreateTestConfigs
(
"jingweno"
,
"123"
)
args
:=
NewArgs
([]
string
{
"remote"
,
"add"
,
"jingweno"
})
transformRemoteArgs
(
args
)
...
...
@@ -28,8 +40,6 @@ func TestTransformRemoteArgs(t *testing.T) {
reg
=
regexp
.
MustCompile
(
"^git@github.com:jingweno/.+
\\
.git$"
)
assert
.
T
(
t
,
reg
.
MatchString
(
args
.
GetParam
(
2
)))
github
.
CreateTestConfig
(
"jingweno"
,
"123"
)
args
=
NewArgs
([]
string
{
"remote"
,
"add"
,
"origin"
})
transformRemoteArgs
(
args
)
...
...
commands/utils.go
浏览文件 @
04e2d43b
package
commands
import
(
"fmt"
"github.com/jingweno/gh/git"
"github.com/jingweno/gh/utils"
"github.com/jingweno/go-octokit/octokit"
"os"
"regexp"
"strings"
)
...
...
@@ -37,27 +35,6 @@ func parseUserBranchFromPR(pullRequest *octokit.PullRequest) (user string, branc
return
}
func
parseRepoNameOwner
(
nameWithOwner
string
)
(
owner
,
name
string
,
match
bool
)
{
ownerRe
:=
fmt
.
Sprintf
(
"^(%s)$"
,
OwnerRe
)
ownerRegexp
:=
regexp
.
MustCompile
(
ownerRe
)
if
ownerRegexp
.
MatchString
(
nameWithOwner
)
{
owner
=
ownerRegexp
.
FindStringSubmatch
(
nameWithOwner
)[
1
]
match
=
true
return
}
nameWithOwnerRe
:=
fmt
.
Sprintf
(
"^(%s)
\\
/(%s)$"
,
OwnerRe
,
NameRe
)
nameWithOwnerRegexp
:=
regexp
.
MustCompile
(
nameWithOwnerRe
)
if
nameWithOwnerRegexp
.
MatchString
(
nameWithOwner
)
{
result
:=
nameWithOwnerRegexp
.
FindStringSubmatch
(
nameWithOwner
)
owner
=
result
[
1
]
name
=
result
[
2
]
match
=
true
}
return
}
func
hasGitRemote
(
name
string
)
bool
{
remotes
,
err
:=
git
.
Remotes
()
utils
.
Check
(
err
)
...
...
commands/utils_test.go
已删除
100644 → 0
浏览文件 @
4b352e58
package
commands
import
(
"github.com/bmizerany/assert"
"testing"
)
func
TestParseRepoNameOwner
(
t
*
testing
.
T
)
{
owner
,
repo
,
match
:=
parseRepoNameOwner
(
"jingweno"
)
assert
.
T
(
t
,
match
)
assert
.
Equal
(
t
,
"jingweno"
,
owner
)
assert
.
Equal
(
t
,
""
,
repo
)
owner
,
repo
,
match
=
parseRepoNameOwner
(
"jingweno/gh"
)
assert
.
T
(
t
,
match
)
assert
.
Equal
(
t
,
"jingweno"
,
owner
)
assert
.
Equal
(
t
,
"gh"
,
repo
)
}
github/project.go
浏览文件 @
04e2d43b
...
...
@@ -161,7 +161,7 @@ func NewProject(owner, name, host string) *Project {
}
if
host
==
""
{
host
=
GitHubHost
host
=
defaultHost
()
}
if
owner
==
""
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录