Skip to content

Commit

Permalink
Merge pull request #42 from zaquestion/issue20
Browse files Browse the repository at this point in the history
[#20] (mrCreate) default to forked-from branch and check for branch pushed
  • Loading branch information
zaquestion authored Nov 10, 2017
2 parents 2272d40 + bfc1e05 commit 39a8db2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
28 changes: 23 additions & 5 deletions cmd/mrCreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
},
}

Expand Down
14 changes: 14 additions & 0 deletions internal/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,17 @@ 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 {
return false
}

b, _, err := lab.Branches.GetBranch(p.ID, branch)
if err != nil {
return false
}
return b != nil
}

0 comments on commit 39a8db2

Please sign in to comment.