Skip to content

Commit ab975c4

Browse files
committed
Sort branches by date
Use the newly added author date to implement alternative sortings. TODO: Add sort settings page and update groups() in branch-list.tsx to use the selected sort instead of hardcoding BranchSortOrder.LastModified.
1 parent da0a09e commit ab975c4

4 files changed

Lines changed: 32 additions & 7 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export enum BranchSortOrder {
2+
Alphabetical = 'Alphabetical',
3+
LastModified = 'LastModified',
4+
}
5+
6+
export const DEFAULT_BRANCH_SORT_ORDER = BranchSortOrder.LastModified

app/src/ui/branches/branch-list.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as octicons from '../octicons/octicons.generated'
2626
import memoizeOne from 'memoize-one'
2727
import { Repository } from '../../models/repository'
2828
import { formatDate } from '../../lib/format-date'
29+
import { BranchSortOrder } from '../../models/branch-sort-order'
2930

3031
const RowHeight = 30
3132

@@ -175,10 +176,10 @@ export class BranchList extends React.Component<IBranchListProps> {
175176
private get groups() {
176177
return this.getGroups(
177178
this.props.defaultBranch,
178-
this.props.currentBranch,
179179
this.props.allBranches,
180180
this.props.recentBranches,
181-
this.props.allWorktrees
181+
this.props.allWorktrees,
182+
BranchSortOrder.LastModified
182183
)
183184
}
184185

app/src/ui/branches/group-branches.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Branch } from '../../models/branch'
2+
import { BranchSortOrder } from '../../models/branch-sort-order'
23
import { WorktreeEntry } from '../../models/worktree'
34
import { IFilterListGroup, IFilterListItem } from '../lib/filter-list'
45

@@ -35,10 +36,10 @@ function findWorktreeForBranch(
3536

3637
export function groupBranches(
3738
defaultBranch: Branch | null,
38-
currentBranch: Branch | null,
3939
allBranches: ReadonlyArray<Branch>,
4040
recentBranches: ReadonlyArray<Branch>,
41-
allWorktrees: ReadonlyArray<WorktreeEntry>
41+
allWorktrees: ReadonlyArray<WorktreeEntry>,
42+
sortOrder: BranchSortOrder
4243
): ReadonlyArray<IFilterListGroup<IBranchListItem>> {
4344
const groups = new Array<IFilterListGroup<IBranchListItem>>()
4445

@@ -91,8 +92,13 @@ export function groupBranches(
9192
!recentBranchNames.has(b.name) &&
9293
!b.isDesktopForkRemoteBranch
9394
)
95+
const branchComparer = getBranchComparer(sortOrder)
96+
const sortedRemainingBranches = remainingBranches.sort((a, b) =>
97+
// Local branches are always sorted above remote branches, regardless of the sort order
98+
a.type === b.type ? branchComparer(a, b) : a.type - b.type
99+
)
94100

95-
const remainingItems = remainingBranches.map(b => {
101+
const remainingItems = sortedRemainingBranches.map(b => {
96102
const worktreeInUse = findWorktreeForBranch(b.name, allWorktrees)
97103
return {
98104
text: [b.name],
@@ -108,3 +114,14 @@ export function groupBranches(
108114

109115
return groups
110116
}
117+
118+
function getBranchComparer(
119+
sortOrder: BranchSortOrder
120+
): (a: Branch, b: Branch) => number {
121+
switch (sortOrder) {
122+
case BranchSortOrder.Alphabetical:
123+
return (a, b) => a.name.localeCompare(b.name)
124+
case BranchSortOrder.LastModified:
125+
return (a, b) => b.tip.author.date.getTime() - a.tip.author.date.getTime()
126+
}
127+
}

app/test/unit/group-branches-test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import assert from 'node:assert'
33
import { groupBranches } from '../../src/ui/branches'
44
import { Branch, BranchType } from '../../src/models/branch'
55
import { CommitIdentity } from '../../src/models/commit-identity'
6+
import { BranchSortOrder } from '../../src/models/branch-sort-order'
67

78
describe('Branches grouping', () => {
89
const author = new CommitIdentity('Hubot', 'hubot@github.com', new Date())
@@ -52,10 +53,10 @@ describe('Branches grouping', () => {
5253
it('should group branches', () => {
5354
const groups = groupBranches(
5455
defaultBranch,
55-
currentBranch,
5656
allBranches,
5757
recentBranches,
58-
[]
58+
[],
59+
BranchSortOrder.Alphabetical
5960
)
6061
assert.equal(groups.length, 3)
6162

0 commit comments

Comments
 (0)