Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#20] (mrCreate) default to forked-from branch and check for branch pushed #42

Merged
merged 2 commits into from
Nov 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}