Skip to content

Commit f8b23b1

Browse files
Chris McDonnellCI
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)", so we continue that practice as a default, but we allow users the ability to show them if they want
1 parent 0d9598b commit f8b23b1

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

docs/Config.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ git:
347347
# If true, pass the --all arg to git fetch
348348
fetchAll: true
349349

350+
# If true, show remote HEADs in the remote branches tab
351+
showRemoteHeads: false
352+
350353
# If true, lazygit will automatically stage files that used to have merge
351354
# conflicts but no longer do; and it will also ask you if you want to
352355
# continue a merge or rebase if you've resolved all conflicts. If false, it

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 !self.UserConfig().Git.ShowRemoteHeads && name == "HEAD" {
108114
return false, nil
109115
}
110-
remoteName := split[0]
111-
name := split[1]
112116

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

pkg/config/user_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ type GitConfig struct {
251251
AutoForwardBranches string `yaml:"autoForwardBranches" jsonschema:"enum=none,enum=onlyMainBranches,enum=allBranches"`
252252
// If true, pass the --all arg to git fetch
253253
FetchAll bool `yaml:"fetchAll"`
254+
// If true, show remote HEADs in the remote branches tab
255+
ShowRemoteHeads bool `yaml:"showRemoteHeads"`
254256
// If true, lazygit will automatically stage files that used to have merge
255257
// conflicts but no longer do; and it will also ask you if you want to
256258
// continue a merge or rebase if you've resolved all conflicts. If false, it
@@ -822,6 +824,7 @@ func GetDefaultConfig() *UserConfig {
822824
AutoRefresh: true,
823825
AutoForwardBranches: "onlyMainBranches",
824826
FetchAll: true,
827+
ShowRemoteHeads: false,
825828
AutoStageResolvedConflicts: true,
826829
BranchLogCmd: "git log --graph --color=always --abbrev-commit --decorate --date=relative --pretty=medium {{branchName}} --",
827830
AllBranchesLogCmds: []string{"git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium"},

schema/config.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@
339339
"description": "If true, pass the --all arg to git fetch",
340340
"default": true
341341
},
342+
"showRemoteHeads": {
343+
"type": "boolean",
344+
"description": "If true, show remote HEADs in the remote branches tab",
345+
"default": false
346+
},
342347
"autoStageResolvedConflicts": {
343348
"type": "boolean",
344349
"description": "If true, lazygit will automatically stage files that used to have merge\nconflicts but no longer do; and it will also ask you if you want to\ncontinue a merge or rebase if you've resolved all conflicts. If false, it\nwon't do either of these things.",

0 commit comments

Comments
 (0)