Skip to content

Commit

Permalink
mr: Default to remote tracking branch
Browse files Browse the repository at this point in the history
We currently assume that the local branch name matches the remote one,
which isn't necessarily the case. Address this by using the remote
tracking name where appropriate.
  • Loading branch information
fmuellner committed Dec 10, 2020
1 parent 7c55513 commit d939764
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
14 changes: 12 additions & 2 deletions cmd/mr_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,25 @@ func runMRCreate(cmd *cobra.Command, args []string) {
log.Fatal(err)
}

remoteBranch, err := git.CurrentUpstreamBranch()
if remoteBranch == "" {
// Fall back to local branch
remoteBranch, err = git.CurrentBranch()
}

if err != nil {
log.Fatal(err)
}

p, err := lab.FindProject(sourceProjectName)
if err != nil {
log.Fatal(err)
}
if _, err := lab.GetCommit(p.ID, branch); err != nil {
if _, err := lab.GetCommit(p.ID, remoteBranch); err != nil {
err = errors.Wrapf(
err,
"aborting MR, source branch %s not present on remote %s. did you forget to push?",
branch, sourceRemote)
remoteBranch, sourceRemote)
log.Fatal(err)
}

Expand Down
9 changes: 7 additions & 2 deletions cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,14 @@ func flagConfig(fs *flag.FlagSet) {
func getCurrentBranchMR(rn string) int {
var num int = 0

currentBranch, err := git.CurrentBranch()
currentBranch, err := git.CurrentUpstreamBranch()
if currentBranch == "" {
// Fall back to local branch
currentBranch, err = git.CurrentBranch()
}

if err != nil {
log.Fatal(err)
return 0
}

mrs, err := lab.MRList(rn, gitlab.ListProjectMergeRequestsOptions{
Expand Down
27 changes: 27 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,33 @@ func CurrentBranch() (string, error) {
return strings.TrimSpace(string(branch)), nil
}

// CurrentUpstreamBranch returns the upstream of the currently checked out branch
func CurrentUpstreamBranch() (string, error) {
localBranch, err := CurrentBranch()
if err != nil {
return "", err
}

branch, err := UpstreamBranch(localBranch)
if err != nil {
return "", err
}
return branch, nil
}

// UpstreamBranch returns the upstream of the specified branch
func UpstreamBranch(branch string) (string, error) {
cmd := New("rev-parse", "--abbrev-ref", branch+"@{upstream}")
cmd.Stdout = nil
cmd.Stderr = nil
ref, err := cmd.Output()
if err != nil {
return "", errors.Errorf("No upstream for branch '%s'", branch)
}
upstreamBranch := strings.SplitN(string(ref), "/", 2)[1]
return strings.TrimSpace(upstreamBranch), nil
}

// PathWithNameSpace returns the owner/repository for the current repo
// Such as zaquestion/lab
// Respects GitLab subgroups (https://docs.gitlab.com/ce/user/group/subgroups/)
Expand Down

0 comments on commit d939764

Please sign in to comment.