- Keep the current branch first - Place the default branch second - Sort remaining branches by most recent commit and update tests for this behavior
Unified · Split
| @@ -125,7 +125,7 @@ describe("git integration", () => { · 124 unmodified lines | ||
| 125 | 125 | expect(current!.current).toBe(true); |
| 126 | 126 | }); |
| 127 | 127 | |
| 128 | − it("sorts branches by most recent commit first", async () => { | |
| 128 | + it("keeps current branch first and sorts the remaining branches by recency", async () => { | |
| 129 | 129 | await using tmp = await makeTmpDir(); |
| 130 | 130 | await initRepoWithCommit(tmp.path); |
| 131 | 131 | const initialBranch = (await listGitBranches({ cwd: tmp.path })).branches.find( |
| @@ -153,11 +153,54 @@ describe("git integration", () => { · 21 unmodified lines | ||
| 153 | 153 | "newer branch change", |
| 154 | 154 | ); |
| 155 | 155 | |
| 156 | − // Switch away to show sort order is recency-based, not "current"-based. | |
| 156 | + // Switch away to show current branch is pinned, then remaining branches are recency-sorted. | |
| 157 | 157 | await checkoutGitBranch({ cwd: tmp.path, branch: "older-branch" }); |
| 158 | 158 | |
| 159 | 159 | const result = await listGitBranches({ cwd: tmp.path }); |
| 160 | − expect(result.branches[0]!.name).toBe("newer-branch"); | |
| 160 | + expect(result.branches[0]!.name).toBe("older-branch"); | |
| 161 | + expect(result.branches[1]!.name).toBe("newer-branch"); | |
| 162 | + }); | |
| 163 | + | |
| 164 | + it("keeps default branch right after current branch", async () => { | |
| 165 | + await using tmp = await makeTmpDir(); | |
| 166 | + await using remote = await makeTmpDir(); | |
| 167 | + await initRepoWithCommit(tmp.path); | |
| 168 | + const defaultBranch = (await listGitBranches({ cwd: tmp.path })).branches.find( | |
| 169 | + (branch) => branch.current, | |
| 170 | + )!.name; | |
| 171 | + | |
| 172 | + await git(remote.path, "init --bare"); | |
| 173 | + await git(tmp.path, `remote add origin ${JSON.stringify(remote.path)}`); | |
| 174 | + await git(tmp.path, `push -u origin ${defaultBranch}`); | |
| 175 | + await git(tmp.path, `remote set-head origin ${defaultBranch}`); | |
| 176 | + | |
| 177 | + await createGitBranch({ cwd: tmp.path, branch: "current-branch" }); | |
| 178 | + await checkoutGitBranch({ cwd: tmp.path, branch: "current-branch" }); | |
| 179 | + await commitWithDate( | |
| 180 | + tmp.path, | |
| 181 | + "current.txt", | |
| 182 | + "current change\n", | |
| 183 | + "Thu, 1 Jan 2037 00:00:00 +0000", | |
| 184 | + "current change", | |
| 185 | + ); | |
| 186 | + | |
| 187 | + await checkoutGitBranch({ cwd: tmp.path, branch: defaultBranch }); | |
| 188 | + await createGitBranch({ cwd: tmp.path, branch: "newer-branch" }); | |
| 189 | + await checkoutGitBranch({ cwd: tmp.path, branch: "newer-branch" }); | |
| 190 | + await commitWithDate( | |
| 191 | + tmp.path, | |
| 192 | + "newer.txt", | |
| 193 | + "newer change\n", | |
| 194 | + "Fri, 1 Jan 2038 00:00:00 +0000", | |
| 195 | + "newer change", | |
| 196 | + ); | |
| 197 | + | |
| 198 | + await checkoutGitBranch({ cwd: tmp.path, branch: "current-branch" }); | |
| 199 | + | |
| 200 | + const result = await listGitBranches({ cwd: tmp.path }); | |
| 201 | + expect(result.branches[0]!.name).toBe("current-branch"); | |
| 202 | + expect(result.branches[1]!.name).toBe(defaultBranch); | |
| 203 | + expect(result.branches[2]!.name).toBe("newer-branch"); | |
| 161 | 204 | }); |
| 162 | 205 | |
| 163 | 206 | it("lists multiple branches after creating them", async () => { |
| @@ -393,11 +393,13 @@ export class GitCoreService { · 392 unmodified lines | ||
| 393 | 393 | }; |
| 394 | 394 | }) |
| 395 | 395 | .toSorted((a, b) => { |
| 396 | + const aPriority = a.current ? 0 : a.isDefault ? 1 : 2; | |
| 397 | + const bPriority = b.current ? 0 : b.isDefault ? 1 : 2; | |
| 398 | + if (aPriority !== bPriority) return aPriority - bPriority; | |
| 399 | + | |
| 396 | 400 | const aLastCommit = branchLastCommit.get(a.name) ?? 0; |
| 397 | 401 | const bLastCommit = branchLastCommit.get(b.name) ?? 0; |
| 398 | 402 | if (aLastCommit !== bLastCommit) return bLastCommit - aLastCommit; |
| 399 | − if (a.current !== b.current) return a.current ? -1 : 1; | |
| 400 | − if (a.isDefault !== b.isDefault) return a.isDefault ? -1 : 1; | |
| 401 | 403 | return a.name.localeCompare(b.name); |
| 402 | 404 | }); |
| 403 | 405 | |