提交 c60599b4 编写于 作者: J Jingwen Owen Ou

Merge branch 'remote_fix'

......@@ -17,8 +17,8 @@ var cmdCheckout = &Command{
/**
$ gh checkout https://github.com/jingweno/gh/pull/73
# > git remote add -f -t feature git://github:com/foo/gh.git
# > git checkout --track -B foo-feature foo/feature
> git remote add -f -t feature git://github:com/foo/gh.git
> git checkout --track -B foo-feature foo/feature
$ gh checkout https://github.com/jingweno/gh/pull/73 custom-branch-name
**/
......
......@@ -8,6 +8,12 @@ import (
"strings"
)
var (
NameRe = "[\\w.][\\w.-]*"
OwnerRe = "[a-zA-Z0-9][a-zA-Z0-9-]*"
NameWithOwnerRe = fmt.Sprintf("/^(?:%s|%s\\/%s)$", NameRe, OwnerRe, NameRe)
)
type Command struct {
Run func(cmd *Command, args *Args)
Flag flag.FlagSet
......
......@@ -16,7 +16,7 @@ import (
var cmdPullRequest = &Command{
Run: pullRequest,
Usage: "pull-request [-f] [-i ISSUE] [-b BASE] [-d HEAD] [TITLE]",
Usage: "pull-request [-f] [-i ISSUE] [-b BASE] [-d HEAD] [TITLE]",
Short: "Open a pull request on GitHub",
Long: `Opens a pull request on GitHub for the project that the "origin" remote
points to. The default head of the pull request is the current branch.
......@@ -66,6 +66,9 @@ func pullRequest(cmd *Command, args *Args) {
title, body, err = readTitleAndBody(messageFile)
utils.Check(err)
err = os.Remove(messageFile)
utils.Check(err)
}
if title == "" && flagPullRequestIssue == "" {
......
package commands
import (
"fmt"
"github.com/jingweno/gh/github"
"regexp"
)
var cmdRemote = &Command{
......@@ -11,7 +13,7 @@ var cmdRemote = &Command{
Short: "View and manage a set of remote repositories",
}
/**
/*
$ gh remote add jingweno
> git remote add jingweno git://github.com/jingweno/THIS_REPO.git
......@@ -19,9 +21,8 @@ var cmdRemote = &Command{
> git remote add jingweno git@github.com:jingweno/THIS_REPO.git
$ gh remote add origin
> git remote add origin
git://github.com/YOUR_LOGIN/THIS_REPO.git
**/
> git remote add origin git://github.com/YOUR_LOGIN/THIS_REPO.git
*/
func remote(command *Command, args *Args) {
if args.Size() >= 2 && (args.First() == "add" || args.First() == "set-url") {
transformRemoteArgs(args)
......@@ -29,15 +30,37 @@ func remote(command *Command, args *Args) {
}
func transformRemoteArgs(args *Args) {
ownerWithName := args.Last()
owner, repo, match := parseRepoNameOwner(ownerWithName)
if !match {
return
}
isPriavte := parseRemotePrivateFlag(args)
owner := args.Last()
gh := github.New()
url := gh.ExpandRemoteUrl(owner, isPriavte)
url := gh.ExpandRemoteUrl(owner, repo, isPriavte)
args.Append(url)
}
func parseRepoNameOwner(nameWithOwner string) (string, string, bool) {
ownerRe := fmt.Sprintf("^(%s)$", OwnerRe)
ownerRegexp := regexp.MustCompile(ownerRe)
if ownerRegexp.MatchString(nameWithOwner) {
return ownerRegexp.FindStringSubmatch(nameWithOwner)[1], "", true
}
nameWithOwnerRe := fmt.Sprintf("^(%s)\\/(%s)$", OwnerRe, NameRe)
nameWithOwnerRegexp := regexp.MustCompile(nameWithOwnerRe)
if nameWithOwnerRegexp.MatchString(nameWithOwner) {
match := nameWithOwnerRegexp.FindStringSubmatch(nameWithOwner)
return match[1], match[2], true
}
return "", "", false
}
func parseRemotePrivateFlag(args *Args) bool {
if i := args.IndexOf("-p"); i != -1 {
args.Remove(i)
......
......@@ -5,6 +5,20 @@ import (
"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)
}
func TestTransformRemoteArgs(t *testing.T) {
args := NewArgs([]string{"add", "jingweno"})
transformRemoteArgs(args)
......@@ -21,4 +35,12 @@ func TestTransformRemoteArgs(t *testing.T) {
assert.Equal(t, "add", args.First())
assert.Equal(t, "jingweno", args.Get(1))
assert.Equal(t, "git@github.com:jingweno/gh.git", args.Get(2))
args = NewArgs([]string{"add", "jingweno", "git@github.com:jingweno/gh.git"})
transformRemoteArgs(args)
assert.Equal(t, 3, args.Size())
assert.Equal(t, "add", args.First())
assert.Equal(t, "jingweno", args.Get(1))
assert.Equal(t, "git@github.com:jingweno/gh.git", args.Get(2))
}
......@@ -11,8 +11,9 @@ if declare -F _git > /dev/null; then
__git_list_all_commands() {
cat <<-EOF
alias
pr
pull-request
fork
ci-status
create
browse
compare
......
......@@ -11,8 +11,9 @@ fi
if declare -f _git_commands > /dev/null; then
_hub_commands=(
'alias:show shell instructions for wrapping git'
'pr:open a pull request on GitHub'
'pull-request:open a pull request on GitHub'
'fork:fork origin repo on GitHub'
'ci-status:show CI status of a commit'
'create:create new repo on GitHub for the current project'
'browse:browse the project on GitHub'
'compare:open GitHub compare view'
......
......@@ -83,7 +83,7 @@ func (gh *GitHub) ForkRepository(name, owner string, noRemote bool) (newRemote s
return
}
func (gh *GitHub) ExpandRemoteUrl(owner string, isSSH bool) (url string) {
func (gh *GitHub) ExpandRemoteUrl(owner, name string, isSSH bool) (url string) {
project := gh.Project
if owner == "origin" {
config := gh.config
......@@ -92,7 +92,7 @@ func (gh *GitHub) ExpandRemoteUrl(owner string, isSSH bool) (url string) {
project.Owner = owner
}
return project.GitURL("", owner, isSSH)
return project.GitURL(name, owner, isSSH)
}
func (gh *GitHub) repo() octokat.Repo {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册