Skip to content

Commit

Permalink
git: fix UpstreamBranch
Browse files Browse the repository at this point in the history
UpstreamBranch is assuming the value returned by `git rev-parse` match
'<remote name>/<branch name>' and split the string on the first / to
retrieve the remote branch name. That only works when the remote name
does not have slashes in its name, otherwise the returned branch name is
invalid and not pointing to a git valid object:

- If the remote is 'origin', the rev-parse returned value would be like
  'origin/branch/name' and UpstreamBranch would return 'branch/name'.

- But if the remote is 'gitlab/origin', the rev-parse returned value
  would be like 'gitlab/origin/branch/name' and UpstreamBranch would
  return 'origin/branch/name'.

`git rev-parse --abbrev-ref @{upstream}` constructs its value by
appending branch.<name>.remote and branch.<name>.merge (and removing
'refs/heads/' from the merge part) according to its man page.

What we really want here is branch.<name>.merge (without 'refs/heads/'),
not @{upstream}. This patch is implementing this.

Signed-off-by: Antoine Tenart <atenart@kernel.org>
  • Loading branch information
atenart committed Jan 22, 2021
1 parent 910234a commit cb7a8c2
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,15 +147,11 @@ func CurrentUpstreamBranch() (string, error) {

// 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()
upstreamBranch, err := gitconfig.Local("branch." + branch + ".merge")
if err != nil {
return "", errors.Errorf("No upstream for branch '%s'", branch)
}
upstreamBranch := strings.SplitN(string(ref), "/", 2)[1]
return strings.TrimSpace(upstreamBranch), nil
return strings.TrimPrefix(upstreamBranch, "refs/heads/"), nil
}

// PathWithNameSpace returns the owner/repository for the current repo
Expand Down

0 comments on commit cb7a8c2

Please sign in to comment.