From fa238809ae9c4cc8344f9cb4d7b9a1a4e5a3d509 Mon Sep 17 00:00:00 2001 From: Chris McDonnell Date: Tue, 27 May 2025 23:00:34 -0400 Subject: [PATCH] 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. --- pkg/commands/git_commands/remote_loader.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/commands/git_commands/remote_loader.go b/pkg/commands/git_commands/remote_loader.go index dcf62de97..1b2e33682 100644 --- a/pkg/commands/git_commands/remote_loader.go +++ b/pkg/commands/git_commands/remote_loader.go @@ -96,19 +96,23 @@ func (self *RemoteLoader) getRemoteBranchesByRemoteName() (map[string][]*models. cmdArgs := NewGitCmd("for-each-ref"). Arg(fmt.Sprintf("--sort=%s", sortOrder)). - Arg("--format=%(refname:short)"). + Arg("--format=%(refname)"). Arg("refs/remotes"). ToArgv() err := self.cmd.New(cmdArgs).DontLog().RunAndProcessLines(func(line string) (bool, error) { line = strings.TrimSpace(line) - split := strings.SplitN(line, "/", 2) - if len(split) != 2 { + split := strings.SplitN(line, "/", 4) + if len(split) != 4 { + return false, nil + } + remoteName := split[2] + name := split[3] + + if name == "HEAD" { return false, nil } - remoteName := split[0] - name := split[1] _, ok := remoteBranchesByRemoteName[remoteName] if !ok {