Skip to content

Commit 703256e

Browse files
committed
Move LocalBranchSortOrder and RemoteBranchSortOrder to user config
At the same time, we change the defaults for both of them to "date" (they were "recency" and "alphabetical", respectively, before). This is the reason we need to touch so many integration tests. For some of them I decided to adapt the test assertions to the changed sort order; for others, I added a SetupConfig step to set the order back to "recency" so that I don't have to change what the test does (e.g. how many SelectNextItem() calls are needed to get to a certain branch).
1 parent d792836 commit 703256e

35 files changed

+161
-66
lines changed

docs/Config.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,16 @@ git:
399399
# displays the whole git graph by default in the commits view (equivalent to passing the `--all` argument to `git log`)
400400
showWholeGraph: false
401401

402+
# How branches are sorted in the local branches view.
403+
# One of: 'date' (default) | 'recency' | 'alphabetical'
404+
# Can be changed from within Lazygit with the Sort Order menu (`s`) in the branches panel.
405+
localBranchSortOrder: date
406+
407+
# How branches are sorted in the remote branches view.
408+
# One of: 'date' (default) | 'alphabetical'
409+
# Can be changed from within Lazygit with the Sort Order menu (`s`) in the remote branches panel.
410+
remoteBranchSortOrder: date
411+
402412
# When copying commit hashes to the clipboard, truncate them to this
403413
# length. Set to 40 to disable truncation.
404414
truncateCopiedCommitHashesTo: 12

pkg/commands/git_commands/branch_loader.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (self *BranchLoader) Load(reflogCommits []*models.Commit,
7474
) ([]*models.Branch, error) {
7575
branches := self.obtainBranches()
7676

77-
if self.AppState.LocalBranchSortOrder == "recency" {
77+
if self.UserConfig().Git.LocalBranchSortOrder == "recency" {
7878
reflogBranches := self.obtainReflogBranches(reflogCommits)
7979
// loop through reflog branches. If there is a match, merge them, then remove it from the branches and keep it in the reflog branches
8080
branchesWithRecency := make([]*models.Branch, 0)
@@ -254,7 +254,7 @@ func (self *BranchLoader) obtainBranches() []*models.Branch {
254254
return nil, false
255255
}
256256

257-
storeCommitDateAsRecency := self.AppState.LocalBranchSortOrder != "recency"
257+
storeCommitDateAsRecency := self.UserConfig().Git.LocalBranchSortOrder != "recency"
258258
return obtainBranch(split, storeCommitDateAsRecency), true
259259
})
260260
}
@@ -268,7 +268,7 @@ func (self *BranchLoader) getRawBranches() (string, error) {
268268
)
269269

270270
var sortOrder string
271-
switch strings.ToLower(self.AppState.LocalBranchSortOrder) {
271+
switch strings.ToLower(self.UserConfig().Git.LocalBranchSortOrder) {
272272
case "recency", "date":
273273
sortOrder = "-committerdate"
274274
case "alphabetical":

pkg/commands/git_commands/remote_loader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func (self *RemoteLoader) getRemoteBranchesByRemoteName() (map[string][]*models.
8585
remoteBranchesByRemoteName := make(map[string][]*models.RemoteBranch)
8686

8787
var sortOrder string
88-
switch strings.ToLower(self.AppState.RemoteBranchSortOrder) {
88+
switch strings.ToLower(self.UserConfig().Git.RemoteBranchSortOrder) {
8989
case "alphabetical":
9090
sortOrder = "refname"
9191
case "date":

pkg/config/app_config.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,7 @@ type AppState struct {
676676
// For backwards compatibility we keep the old name in yaml files.
677677
ShellCommandsHistory []string `yaml:"customcommandshistory"`
678678

679-
HideCommandLog bool
680-
LocalBranchSortOrder string
681-
RemoteBranchSortOrder string
679+
HideCommandLog bool
682680

683681
// One of: 'date-order' | 'author-date-order' | 'topo-order' | 'default'
684682
// 'topo-order' makes it easier to read the git log graph, but commits may not
@@ -692,14 +690,12 @@ type AppState struct {
692690

693691
func getDefaultAppState() *AppState {
694692
return &AppState{
695-
LastUpdateCheck: 0,
696-
RecentRepos: []string{},
697-
StartupPopupVersion: 0,
698-
LastVersion: "",
699-
LocalBranchSortOrder: "recency",
700-
RemoteBranchSortOrder: "alphabetical",
701-
GitLogOrder: "", // should be "topo-order" eventually
702-
GitLogShowGraph: "", // should be "always" eventually
693+
LastUpdateCheck: 0,
694+
RecentRepos: []string{},
695+
StartupPopupVersion: 0,
696+
LastVersion: "",
697+
GitLogOrder: "", // should be "topo-order" eventually
698+
GitLogShowGraph: "", // should be "always" eventually
703699
}
704700
}
705701

pkg/config/user_config.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,14 @@ type GitConfig struct {
285285
ParseEmoji bool `yaml:"parseEmoji"`
286286
// Config for showing the log in the commits view
287287
Log LogConfig `yaml:"log"`
288+
// How branches are sorted in the local branches view.
289+
// One of: 'date' (default) | 'recency' | 'alphabetical'
290+
// Can be changed from within Lazygit with the Sort Order menu (`s`) in the branches panel.
291+
LocalBranchSortOrder string `yaml:"localBranchSortOrder" jsonschema:"enum=date,enum=recency,enum=alphabetical"`
292+
// How branches are sorted in the remote branches view.
293+
// One of: 'date' (default) | 'alphabetical'
294+
// Can be changed from within Lazygit with the Sort Order menu (`s`) in the remote branches panel.
295+
RemoteBranchSortOrder string `yaml:"remoteBranchSortOrder" jsonschema:"enum=date,enum=alphabetical"`
288296
// When copying commit hashes to the clipboard, truncate them to this
289297
// length. Set to 40 to disable truncation.
290298
TruncateCopiedCommitHashesTo int `yaml:"truncateCopiedCommitHashesTo"`
@@ -805,6 +813,8 @@ func GetDefaultConfig() *UserConfig {
805813
ShowGraph: "always",
806814
ShowWholeGraph: false,
807815
},
816+
LocalBranchSortOrder: "date",
817+
RemoteBranchSortOrder: "date",
808818
SkipHookPrefix: "WIP",
809819
MainBranches: []string{"master", "main"},
810820
AutoFetch: true,

pkg/config/user_config_validation.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ func (config *UserConfig) Validate() error {
2323
[]string{"none", "onlyMainBranches", "allBranches"}); err != nil {
2424
return err
2525
}
26+
if err := validateEnum("git.localBranchSortOrder", config.Git.LocalBranchSortOrder,
27+
[]string{"date", "recency", "alphabetical"}); err != nil {
28+
return err
29+
}
30+
if err := validateEnum("git.remoteBranchSortOrder", config.Git.RemoteBranchSortOrder,
31+
[]string{"date", "alphabetical"}); err != nil {
32+
return err
33+
}
2634
if err := validateKeybindings(config.Keybinding); err != nil {
2735
return err
2836
}

pkg/config/user_config_validation_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,32 @@ func TestUserConfigValidate_enums(t *testing.T) {
5656
{value: "invalid_value", valid: false},
5757
},
5858
},
59+
{
60+
name: "Git.LocalBranchSortOrder",
61+
setup: func(config *UserConfig, value string) {
62+
config.Git.LocalBranchSortOrder = value
63+
},
64+
testCases: []testCase{
65+
{value: "date", valid: true},
66+
{value: "recency", valid: true},
67+
{value: "alphabetical", valid: true},
68+
{value: "", valid: false},
69+
{value: "invalid_value", valid: false},
70+
},
71+
},
72+
{
73+
name: "Git.RemoteBranchSortOrder",
74+
setup: func(config *UserConfig, value string) {
75+
config.Git.RemoteBranchSortOrder = value
76+
},
77+
testCases: []testCase{
78+
{value: "date", valid: true},
79+
{value: "recency", valid: false},
80+
{value: "alphabetical", valid: true},
81+
{value: "", valid: false},
82+
{value: "invalid_value", valid: false},
83+
},
84+
},
5985
{
6086
name: "Keybindings",
6187
setup: func(config *UserConfig, value string) {

pkg/gui/controllers/branches_controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -674,16 +674,15 @@ func (self *BranchesController) createTag(branch *models.Branch) error {
674674

675675
func (self *BranchesController) createSortMenu() error {
676676
return self.c.Helpers().Refs.CreateSortOrderMenu([]string{"recency", "alphabetical", "date"}, func(sortOrder string) error {
677-
if self.c.GetAppState().LocalBranchSortOrder != sortOrder {
678-
self.c.GetAppState().LocalBranchSortOrder = sortOrder
679-
self.c.SaveAppStateAndLogError()
677+
if self.c.UserConfig().Git.LocalBranchSortOrder != sortOrder {
678+
self.c.UserConfig().Git.LocalBranchSortOrder = sortOrder
680679
self.c.Contexts().Branches.SetSelection(0)
681680
self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.BRANCHES}})
682681
return nil
683682
}
684683
return nil
685684
},
686-
self.c.GetAppState().LocalBranchSortOrder)
685+
self.c.UserConfig().Git.LocalBranchSortOrder)
687686
}
688687

689688
func (self *BranchesController) createResetMenu(selectedBranch *models.Branch) error {

pkg/gui/controllers/helpers/refresh_helper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (self *RefreshHelper) Refresh(options types.RefreshOptions) {
125125
refresh("commits and commit files", self.refreshCommitsAndCommitFiles)
126126

127127
includeWorktreesWithBranches = scopeSet.Includes(types.WORKTREES)
128-
if self.c.AppState.LocalBranchSortOrder == "recency" {
128+
if self.c.UserConfig().Git.LocalBranchSortOrder == "recency" {
129129
refresh("reflog and branches", func() { self.refreshReflogAndBranches(includeWorktreesWithBranches, options.KeepBranchSelectionIndex) })
130130
} else {
131131
refresh("branches", func() { self.refreshBranches(includeWorktreesWithBranches, options.KeepBranchSelectionIndex, true) })
@@ -446,7 +446,7 @@ func (self *RefreshHelper) refreshBranches(refreshWorktrees bool, keepBranchSele
446446
defer self.c.Mutexes().RefreshingBranchesMutex.Unlock()
447447

448448
reflogCommits := self.c.Model().FilteredReflogCommits
449-
if self.c.Modes().Filtering.Active() && self.c.AppState.LocalBranchSortOrder == "recency" {
449+
if self.c.Modes().Filtering.Active() && self.c.UserConfig().Git.LocalBranchSortOrder == "recency" {
450450
// in filter mode we filter our reflog commits to just those containing the path
451451
// however we need all the reflog entries to populate the recencies of our branches
452452
// which allows us to order them correctly. So if we're filtering we'll just

pkg/gui/controllers/remote_branches_controller.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,14 @@ func (self *RemoteBranchesController) rebase(selectedBranch *models.RemoteBranch
146146

147147
func (self *RemoteBranchesController) createSortMenu() error {
148148
return self.c.Helpers().Refs.CreateSortOrderMenu([]string{"alphabetical", "date"}, func(sortOrder string) error {
149-
if self.c.GetAppState().RemoteBranchSortOrder != sortOrder {
150-
self.c.GetAppState().RemoteBranchSortOrder = sortOrder
151-
self.c.SaveAppStateAndLogError()
149+
if self.c.UserConfig().Git.RemoteBranchSortOrder != sortOrder {
150+
self.c.UserConfig().Git.RemoteBranchSortOrder = sortOrder
152151
self.c.Contexts().RemoteBranches.SetSelection(0)
153152
self.c.Refresh(types.RefreshOptions{Mode: types.ASYNC, Scope: []types.RefreshableView{types.REMOTES}})
154153
}
155154
return nil
156155
},
157-
self.c.GetAppState().RemoteBranchSortOrder)
156+
self.c.UserConfig().Git.RemoteBranchSortOrder)
158157
}
159158

160159
func (self *RemoteBranchesController) createResetMenu(selectedBranch *models.RemoteBranch) error {

0 commit comments

Comments
 (0)