diff --git a/docs/Config.md b/docs/Config.md index 2f1f57ecc23..38bc2cde01b 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -116,6 +116,7 @@ git: fetchAll: true # Pass --all flag when running git fetch. Set to false to fetch only origin (or the current branch's upstream remote if there is one) branchLogCmd: 'git log --graph --color=always --abbrev-commit --decorate --date=relative --pretty=medium {{branchName}} --' allBranchesLogCmd: 'git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium' + remoteBranchOrder: 'alphabetical' # one of: 'alphabetical' | 'lastCommit' overrideGpg: false # prevents lazygit from spawning a separate process when using GPG disableForcePushing: false parseEmoji: false diff --git a/pkg/commands/git_commands/remote_loader.go b/pkg/commands/git_commands/remote_loader.go index 6551ecb25cf..8a06aa7e61f 100644 --- a/pkg/commands/git_commands/remote_loader.go +++ b/pkg/commands/git_commands/remote_loader.go @@ -1,6 +1,7 @@ package git_commands import ( + "fmt" "strings" "sync" @@ -83,7 +84,20 @@ func (self *RemoteLoader) GetRemotes() ([]*models.Remote, error) { func (self *RemoteLoader) getRemoteBranchesByRemoteName() (map[string][]*models.RemoteBranch, error) { remoteBranchesByRemoteName := make(map[string][]*models.RemoteBranch) - cmdArgs := NewGitCmd("branch").Arg("-r").ToArgv() + sortOrder := "refname" + switch self.UserConfig.Git.RemoteBranchOrder { + case "alphabetical": + sortOrder = "refname" + case "lastCommit": + sortOrder = "-authordate" + } + + cmdArgs := NewGitCmd("for-each-ref"). + Arg(fmt.Sprintf("--sort=%s", sortOrder)). + Arg("--format=%(refname:short)"). + Arg("refs/remotes"). + ToArgv() + err := self.cmd.New(cmdArgs).DontLog().RunAndProcessLines(func(line string) (bool, error) { // excluding lines like 'origin/HEAD -> origin/master' (there will be a separate // line for 'origin/master') diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 70b81accad7..86deb4e7b6c 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -192,6 +192,8 @@ type GitConfig struct { BranchLogCmd string `yaml:"branchLogCmd"` // Command used to display git log of all branches in the main window AllBranchesLogCmd string `yaml:"allBranchesLogCmd"` + // One of: 'alphabetical' (default) | 'lastCommit' + RemoteBranchOrder string `yaml:"remoteBranchOrder" jsonschema:"enum=alphabetical,enum=lastCommit"` // If true, do not spawn a separate process when using GPG OverrideGpg bool `yaml:"overrideGpg"` // If true, do not allow force pushes @@ -663,6 +665,7 @@ func GetDefaultConfig() *UserConfig { FetchAll: true, BranchLogCmd: "git log --graph --color=always --abbrev-commit --decorate --date=relative --pretty=medium {{branchName}} --", AllBranchesLogCmd: "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium", + RemoteBranchOrder: "alphabetical", DisableForcePushing: false, CommitPrefixes: map[string]CommitPrefixConfig(nil), ParseEmoji: false, diff --git a/schema/config.json b/schema/config.json index 51b7439cb62..fd3777d8810 100644 --- a/schema/config.json +++ b/schema/config.json @@ -473,6 +473,15 @@ "description": "Command used to display git log of all branches in the main window", "default": "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium" }, + "remoteBranchOrder": { + "type": "string", + "enum": [ + "alphabetical", + "lastCommit" + ], + "description": "One of: 'alphabetical' (default) | 'lastCommit'", + "default": "alphabetical" + }, "overrideGpg": { "type": "boolean", "description": "If true, do not spawn a separate process when using GPG"