Skip to content

Commit fa23880

Browse files
Chris McDonnellstefanhaller
authored andcommitted
Use full refname instead of short to prevent disambiguation with tag
In the unlikely scenario that you have a remote branch on `origin` called `foo`, and a local tag called `origin/foo`, git changes the behavior of the previous command such that it produces ``` $ git for-each-ref --sort=refname --format=%(refname:short) refs/remotes origin/branch1 remotes/origin/foo ``` with `remotes/` prepended. Presumably this is to disambiguate it from the local tag `origin/foo`. Unfortunately, this breaks the existing behavior of this function, so the remote branch is never shown. By changing the command, we now get ``` $ git for-each-ref --sort=refname --format=%(refname) refs/remotes refs/remotes/origin/branch1 refs/remotes/origin/foo ``` This allows easy parsing based on the `/`, and none of the code outside this function has to change. ---- We previously were not showing remote HEADs for modern git versions based on how they were formatted from "%(refname:short)". We have decided that this is a feature, not a bug, so we are building that into the code here.
1 parent 737a99b commit fa23880

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

pkg/commands/git_commands/remote_loader.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,23 @@ func (self *RemoteLoader) getRemoteBranchesByRemoteName() (map[string][]*models.
9696

9797
cmdArgs := NewGitCmd("for-each-ref").
9898
Arg(fmt.Sprintf("--sort=%s", sortOrder)).
99-
Arg("--format=%(refname:short)").
99+
Arg("--format=%(refname)").
100100
Arg("refs/remotes").
101101
ToArgv()
102102

103103
err := self.cmd.New(cmdArgs).DontLog().RunAndProcessLines(func(line string) (bool, error) {
104104
line = strings.TrimSpace(line)
105105

106-
split := strings.SplitN(line, "/", 2)
107-
if len(split) != 2 {
106+
split := strings.SplitN(line, "/", 4)
107+
if len(split) != 4 {
108+
return false, nil
109+
}
110+
remoteName := split[2]
111+
name := split[3]
112+
113+
if name == "HEAD" {
108114
return false, nil
109115
}
110-
remoteName := split[0]
111-
name := split[1]
112116

113117
_, ok := remoteBranchesByRemoteName[remoteName]
114118
if !ok {

0 commit comments

Comments
 (0)