From bea96a5babac6ae2a941e99a01446d7c23fc2c8e Mon Sep 17 00:00:00 2001 From: Alex March Date: Wed, 27 Dec 2023 18:03:57 +0900 Subject: [PATCH] fixup! Add a sort order menu for local branches --- pkg/commands/git_commands/branch_loader.go | 14 ++++-- .../git_commands/branch_loader_test.go | 47 ++++++++++++++----- pkg/gui/controllers/helpers/refs_helper.go | 8 ++-- 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/pkg/commands/git_commands/branch_loader.go b/pkg/commands/git_commands/branch_loader.go index 85c3b6e60029..1265252b4392 100644 --- a/pkg/commands/git_commands/branch_loader.go +++ b/pkg/commands/git_commands/branch_loader.go @@ -146,7 +146,8 @@ func (self *BranchLoader) obtainBranches() []*models.Branch { return nil, false } - return obtainBranch(split), true + storeCommitDateAsRecency := self.AppState.LocalBranchSortOrder != "recency" + return obtainBranch(split, storeCommitDateAsRecency), true }) } @@ -188,20 +189,23 @@ var branchFields = []string{ } // Obtain branch information from parsed line output of getRawBranches() -func obtainBranch(split []string) *models.Branch { +func obtainBranch(split []string, storeCommitDateAsRecency bool) *models.Branch { headMarker := split[0] fullName := split[1] upstreamName := split[2] track := split[3] subject := split[4] commitHash := split[5] - commitDate := split[6] name := strings.TrimPrefix(fullName, "heads/") pushables, pullables, gone := parseUpstreamInfo(upstreamName, track) + recency := "" - if unixTimestamp, err := strconv.ParseInt(commitDate, 10, 64); err == nil { - recency = utils.UnixToTimeAgo(unixTimestamp) + if storeCommitDateAsRecency { + commitDate := split[6] + if unixTimestamp, err := strconv.ParseInt(commitDate, 10, 64); err == nil { + recency = utils.UnixToTimeAgo(unixTimestamp) + } } return &models.Branch{ diff --git a/pkg/commands/git_commands/branch_loader_test.go b/pkg/commands/git_commands/branch_loader_test.go index 43452ef50a5b..40f25b30f55b 100644 --- a/pkg/commands/git_commands/branch_loader_test.go +++ b/pkg/commands/git_commands/branch_loader_test.go @@ -2,7 +2,9 @@ package git_commands // "*|feat/detect-purge|origin/feat/detect-purge|[ahead 1]" import ( + "strconv" "testing" + "time" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/stretchr/testify/assert" @@ -12,13 +14,18 @@ func TestObtainBranch(t *testing.T) { type scenario struct { testName string input []string + withRecency bool expectedBranch *models.Branch } + now := time.Now().Unix() + twoAndAHalfHoursAgo := strconv.Itoa(int(now - 2.5*60*60)) + scenarios := []scenario{ { - testName: "TrimHeads", - input: []string{"", "heads/a_branch", "", "", "subject", "123", ""}, + testName: "TrimHeads", + input: []string{"", "heads/a_branch", "", "", "subject", "123"}, + withRecency: false, expectedBranch: &models.Branch{ Name: "a_branch", Pushables: "?", @@ -29,8 +36,9 @@ func TestObtainBranch(t *testing.T) { }, }, { - testName: "NoUpstream", - input: []string{"", "a_branch", "", "", "subject", "123", ""}, + testName: "NoUpstream", + input: []string{"", "a_branch", "", "", "subject", "123"}, + withRecency: false, expectedBranch: &models.Branch{ Name: "a_branch", Pushables: "?", @@ -41,8 +49,9 @@ func TestObtainBranch(t *testing.T) { }, }, { - testName: "IsHead", - input: []string{"*", "a_branch", "", "", "subject", "123", ""}, + testName: "IsHead", + input: []string{"*", "a_branch", "", "", "subject", "123"}, + withRecency: false, expectedBranch: &models.Branch{ Name: "a_branch", Pushables: "?", @@ -53,8 +62,9 @@ func TestObtainBranch(t *testing.T) { }, }, { - testName: "IsBehindAndAhead", - input: []string{"", "a_branch", "a_remote/a_branch", "[behind 2, ahead 3]", "subject", "123", ""}, + testName: "IsBehindAndAhead", + input: []string{"", "a_branch", "a_remote/a_branch", "[behind 2, ahead 3]", "subject", "123"}, + withRecency: false, expectedBranch: &models.Branch{ Name: "a_branch", Pushables: "3", @@ -65,8 +75,9 @@ func TestObtainBranch(t *testing.T) { }, }, { - testName: "RemoteBranchIsGone", - input: []string{"", "a_branch", "a_remote/a_branch", "[gone]", "subject", "123", ""}, + testName: "RemoteBranchIsGone", + input: []string{"", "a_branch", "a_remote/a_branch", "[gone]", "subject", "123"}, + withRecency: false, expectedBranch: &models.Branch{ Name: "a_branch", UpstreamGone: true, @@ -77,11 +88,25 @@ func TestObtainBranch(t *testing.T) { CommitHash: "123", }, }, + { + testName: "WithCommitDateAsRecency", + input: []string{"", "a_branch", "", "", "subject", "123", twoAndAHalfHoursAgo}, + withRecency: true, + expectedBranch: &models.Branch{ + Name: "a_branch", + Recency: "2h", + Pushables: "?", + Pullables: "?", + Head: false, + Subject: "subject", + CommitHash: "123", + }, + }, } for _, s := range scenarios { t.Run(s.testName, func(t *testing.T) { - branch := obtainBranch(s.input) + branch := obtainBranch(s.input, s.withRecency) assert.EqualValues(t, s.expectedBranch, branch) }) } diff --git a/pkg/gui/controllers/helpers/refs_helper.go b/pkg/gui/controllers/helpers/refs_helper.go index a112c24d4e9d..6d0d64983270 100644 --- a/pkg/gui/controllers/helpers/refs_helper.go +++ b/pkg/gui/controllers/helpers/refs_helper.go @@ -133,10 +133,12 @@ func (self *RefsHelper) CreateSortOrderMenu(sortOptionsOrder []string, onSelecte } sortOptions := make([]sortMenuOption, 0, len(sortOptionsOrder)) for _, key := range sortOptionsOrder { - if sortOption, ok := availableSortOptions[key]; ok { - sortOption.sortOrder = key - sortOptions = append(sortOptions, sortOption) + sortOption, ok := availableSortOptions[key] + if !ok { + panic(fmt.Sprintf("unexpected sort order: %s", key)) } + sortOption.sortOrder = key + sortOptions = append(sortOptions, sortOption) } menuItems := lo.Map(sortOptions, func(opt sortMenuOption, _ int) *types.MenuItem {