From 1d7b9dbf07ecd692b82b1a75add227db8c3ca955 Mon Sep 17 00:00:00 2001 From: Zaq? Wiedmann Date: Fri, 10 Nov 2017 02:09:39 -0800 Subject: [PATCH 1/2] [#20] (mrCreate) default to forked-from branch and check for branch pushed --- README.md | 4 ++-- cmd/mrCreate.go | 28 +++++++++++++++++++++++----- cmd/version.go | 2 +- internal/gitlab/gitlab.go | 13 +++++++++++++ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6120e1ff..6d0c0dfb 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ lab will look for hub and uses that as your git binary when available so you don $ lab version git version 2.11.0 hub version 2.3.0-pre9 -lab version 0.5.1 +lab version 0.5.2 ``` # Inspiration @@ -43,7 +43,7 @@ $ go get -u github.com/zaquestion/lab $ lab version git version 2.11.0 -lab version 0.5.1 +lab version 0.5.2 ``` # Configuration diff --git a/cmd/mrCreate.go b/cmd/mrCreate.go index a8ed012c..73a4b5a8 100644 --- a/cmd/mrCreate.go +++ b/cmd/mrCreate.go @@ -24,7 +24,7 @@ const ( ) var ( - // Will be updated to upstream in init() if user if remote exists + // Will be updated to upstream in init() if upstream remote exists targetRemote = "origin" ) @@ -51,15 +51,16 @@ func runMRCreate(cmd *cobra.Command, args []string) { log.Fatal(err) } - sourceRemote, err := gitconfig.Local("branch." + branch + ".remote") - if err != nil { - sourceRemote = "origin" - } + sourceRemote := determineSourceRemote(branch) sourceProjectName, err := git.PathWithNameSpace(sourceRemote) if err != nil { log.Fatal(err) } + if !lab.BranchPushed(sourceProjectName, branch) { + log.Fatal("aborting MR, branch not present on remote: ", sourceRemote) + } + targetProjectName, err := git.PathWithNameSpace(targetRemote) if err != nil { log.Fatal(err) @@ -97,6 +98,23 @@ func runMRCreate(cmd *cobra.Command, args []string) { fmt.Println(mrURL + "/diffs") } +func determineSourceRemote(branch string) string { + // Check if the branch is being tracked + r, err := gitconfig.Local("branch." + branch + ".remote") + if err == nil { + return r + } + + // If not, check if the fork is named after the user + _, err = gitconfig.Local("remote." + lab.User + ".url") + if err == nil { + return lab.User + } + + // If not, default to origin + return "origin" +} + func mrMsg(base, head, sourceRemote, targetRemote string) (string, error) { lastCommitMsg, err := git.LastCommitMessage() if err != nil { diff --git a/cmd/version.go b/cmd/version.go index 00248640..0d906692 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -17,7 +17,7 @@ var versionCmd = &cobra.Command{ git.Stdout = nil git.Stderr = nil version, _ := git.Output() - fmt.Printf("%s%s\n", string(version), "lab version 0.5.1") + fmt.Printf("%s%s\n", string(version), "lab version 0.5.2") }, } diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index 00ce9434..7facc0b7 100644 --- a/internal/gitlab/gitlab.go +++ b/internal/gitlab/gitlab.go @@ -289,3 +289,16 @@ func IssueList(project string, opts *gitlab.ListProjectIssuesOptions) ([]*gitlab } return list, nil } + +func BranchPushed(project, branch string) bool { + p, err := FindProject(project) + if err != nil { + return false + } + + b, _, err := lab.Branches.GetBranch(p.ID, branch) + if err != nil { + return false + } + return b != nil +} From bfc1e0521ad6311eb0795c4830d88d6f7b746d0f Mon Sep 17 00:00:00 2001 From: Zaq? Wiedmann Date: Fri, 10 Nov 2017 02:14:36 -0800 Subject: [PATCH 2/2] (internal/gitlab) comment BranchPushed --- internal/gitlab/gitlab.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/gitlab/gitlab.go b/internal/gitlab/gitlab.go index 7facc0b7..1025f940 100644 --- a/internal/gitlab/gitlab.go +++ b/internal/gitlab/gitlab.go @@ -290,6 +290,7 @@ func IssueList(project string, opts *gitlab.ListProjectIssuesOptions) ([]*gitlab return list, nil } +// BranchPushed checks if a branch exists on a specified GitLab Project func BranchPushed(project, branch string) bool { p, err := FindProject(project) if err != nil {