Skip to content

Commit

Permalink
fixup! Add a sort order menu for local branches
Browse files Browse the repository at this point in the history
  • Loading branch information
hosaka committed Dec 27, 2023
1 parent c34340a commit bea96a5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
14 changes: 9 additions & 5 deletions pkg/commands/git_commands/branch_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}

Expand Down Expand Up @@ -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{
Expand Down
47 changes: 36 additions & 11 deletions pkg/commands/git_commands/branch_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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: "?",
Expand All @@ -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: "?",
Expand All @@ -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: "?",
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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)
})
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/gui/controllers/helpers/refs_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit bea96a5

Please sign in to comment.