From 6473f4d6a8167894b802852d458e17b6576b2909 Mon Sep 17 00:00:00 2001 From: Jingwen Owen Ou Date: Fri, 6 Dec 2013 16:51:11 -0800 Subject: [PATCH] Fix more cucumber test failure due to -F --- commands/pull_request.go | 49 +++++++++++++++++++++++++++++++++------- git/remote.go | 5 +++- github/repo.go | 10 ++++++-- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/commands/pull_request.go b/commands/pull_request.go index 33b5bf98..9a9cb198 100644 --- a/commands/pull_request.go +++ b/commands/pull_request.go @@ -16,7 +16,7 @@ import ( var cmdPullRequest = &Command{ Run: pullRequest, - Usage: "pull-request [-f] [-i ISSUE] [-b BASE] [-d HEAD] [-m MESSAGE] [TITLE]", + Usage: "pull-request [-f] [-i ISSUE] [-b BASE] [-h HEAD] [-m MESSAGE] [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. @@ -35,13 +35,14 @@ of title you can paste a full URL to an issue on GitHub. `, } -var flagPullRequestBase, flagPullRequestHead, flagPullRequestIssue, flagPullRequestMessage string +var flagPullRequestBase, flagPullRequestHead, flagPullRequestIssue, flagPullRequestMessage, flagPullRequestFile string func init() { - cmdPullRequest.Flag.StringVar(&flagPullRequestBase, "b", "master", "BASE") - cmdPullRequest.Flag.StringVar(&flagPullRequestHead, "d", "", "HEAD") + cmdPullRequest.Flag.StringVar(&flagPullRequestBase, "b", "", "BASE") + cmdPullRequest.Flag.StringVar(&flagPullRequestHead, "h", "", "HEAD") cmdPullRequest.Flag.StringVar(&flagPullRequestIssue, "i", "", "ISSUE") cmdPullRequest.Flag.StringVar(&flagPullRequestMessage, "m", "", "MESSAGE") + cmdPullRequest.Flag.StringVar(&flagPullRequestFile, "F", "", "FILE") } /* @@ -77,13 +78,31 @@ func pullRequest(cmd *Command, args *Args) { gh := github.NewWithoutProject() gh.Project = baseProject - var base, head string + var ( + base, head string + explicitOwner bool + ) if flagPullRequestBase != "" { - base = flagPullRequestBase + if strings.Contains(flagPullRequestBase, ":") { + split := strings.SplitN(flagPullRequestBase, ":", 2) + base = split[1] + baseProject.Owner = split[0] + } else { + base = flagPullRequestBase + } } + if flagPullRequestHead != "" { - head = flagPullRequestHead + if strings.Contains(flagPullRequestHead, ":") { + split := strings.SplitN(flagPullRequestHead, ":", 2) + head = split[1] + headProject.Owner = split[0] + explicitOwner = true + } else { + head = flagPullRequestHead + } } + if args.ParamsSize() == 1 { arg := args.RemoveParam(0) u, e := github.ParseURL(arg) @@ -124,7 +143,7 @@ func pullRequest(cmd *Command, args *Args) { } // when no tracking, assume remote branch is published under active user's fork - if tberr != nil && gh.Config.User != headProject.Owner { + if tberr != nil && !explicitOwner && gh.Config.User != headProject.Owner { headProject = github.NewProjectFromNameAndOwner(headProject.Name, "") } @@ -133,6 +152,20 @@ func pullRequest(cmd *Command, args *Args) { title, body = readMsg(flagPullRequestMessage) } + if flagPullRequestFile != "" { + var ( + content []byte + err error + ) + if flagPullRequestFile == "-" { + content, err = ioutil.ReadAll(os.Stdin) + } else { + content, err = ioutil.ReadFile(flagPullRequestFile) + } + utils.Check(err) + title, body = readMsg(string(content)) + } + fullBase := fmt.Sprintf("%s:%s", baseProject.Owner, base) fullHead := fmt.Sprintf("%s:%s", headProject.Owner, head) diff --git a/git/remote.go b/git/remote.go index 419bc534..4d3a1deb 100644 --- a/git/remote.go +++ b/git/remote.go @@ -4,6 +4,7 @@ import ( "errors" "net/url" "regexp" + "strings" ) type Remote struct { @@ -24,7 +25,9 @@ func Remotes() (remotes []Remote, err error) { for _, o := range output { if re.MatchString(o) { match := re.FindStringSubmatch(o) - remotesMap[match[1]] = match[2] + k := strings.TrimSpace(match[1]) + v := strings.TrimSpace(match[2]) + remotesMap[k] = v } } diff --git a/github/repo.go b/github/repo.go index 21c95895..106c0d05 100644 --- a/github/repo.go +++ b/github/repo.go @@ -51,7 +51,8 @@ func (r *GitHubRepo) MasterBranch() (branch Branch, err error) { name, err := git.SymbolicFullName(origin.Name) if err != nil { - return + name = "refs/head/master" + err = nil } branch = Branch(name) @@ -66,7 +67,12 @@ func (r *GitHubRepo) MainProject() (project *Project, err error) { return } - return NewProjectFromURL(origin.URL) + project, err = NewProjectFromURL(origin.URL) + if err != nil { + err = fmt.Errorf("Aborted: the origin remote doesn't point to a GitHub repository.") + } + + return } func (r *GitHubRepo) CurrentProject() (project *Project, err error) { -- GitLab