| ... | @@ -51,6 +51,25 @@ async function initRepoWithCommit(cwd: string): Promise<void> { | ... | @@ -51,6 +51,25 @@ async function initRepoWithCommit(cwd: string): Promise<void> { |
| 51 | await git(cwd, "commit -m 'initial commit'"); | 51 | await git(cwd, "commit -m 'initial commit'"); |
| 52 | } | 52 | } |
| 53 | | 53 | |
| | 54 | async function commitWithDate( |
| | 55 | cwd: string, |
| | 56 | fileName: string, |
| | 57 | fileContents: string, |
| | 58 | dateIsoString: string, |
| | 59 | message: string, |
| | 60 | ): Promise<void> { |
| | 61 | await writeFile(path.join(cwd, fileName), fileContents); |
| | 62 | await git(cwd, `add ${fileName}`); |
| | 63 | const commitResult = await runTerminalCommand({ |
| | 64 | command: `GIT_AUTHOR_DATE="${dateIsoString}" GIT_COMMITTER_DATE="${dateIsoString}" git commit -m "${message}"`, |
| | 65 | cwd, |
| | 66 | timeoutMs: 10_000, |
| | 67 | }); |
| | 68 | if (commitResult.code !== 0) { |
| | 69 | throw new Error(`git dated commit failed: ${commitResult.stderr}`); |
| | 70 | } |
| | 71 | } |
| | 72 | |
| 54 | // ── Tests ── | 73 | // ── Tests ── |
| 55 | | 74 | |
| 56 | describe("git integration", () => { | 75 | describe("git integration", () => { |
| ... | @@ -106,14 +125,39 @@ describe("git integration", () => { | ... | @@ -106,14 +125,39 @@ describe("git integration", () => { |
| 106 | expect(current!.current).toBe(true); | 125 | expect(current!.current).toBe(true); |
| 107 | }); | 126 | }); |
| 108 | | 127 | |
| 109 | it("sorts current branch first", async () => { | 128 | it("sorts branches by most recent commit first", async () => { |
| 110 | await using tmp = await makeTmpDir(); | 129 | await using tmp = await makeTmpDir(); |
| 111 | await initRepoWithCommit(tmp.path); | 130 | await initRepoWithCommit(tmp.path); |
| 112 | await createGitBranch({ cwd: tmp.path, branch: "aaa-first-alpha" }); | 131 | const initialBranch = (await listGitBranches({ cwd: tmp.path })).branches.find( |
| 113 | await createGitBranch({ cwd: tmp.path, branch: "zzz-last" }); | 132 | (branch) => branch.current, |
| | 133 | )!.name; |
| | 134 | |
| | 135 | await createGitBranch({ cwd: tmp.path, branch: "older-branch" }); |
| | 136 | await checkoutGitBranch({ cwd: tmp.path, branch: "older-branch" }); |
| | 137 | await commitWithDate( |
| | 138 | tmp.path, |
| | 139 | "older.txt", |
| | 140 | "older branch change\n", |
| | 141 | "Thu, 1 Jan 2037 00:00:00 +0000", |
| | 142 | "older branch change", |
| | 143 | ); |
| | 144 | |
| | 145 | await checkoutGitBranch({ cwd: tmp.path, branch: initialBranch }); |
| | 146 | await createGitBranch({ cwd: tmp.path, branch: "newer-branch" }); |
| | 147 | await checkoutGitBranch({ cwd: tmp.path, branch: "newer-branch" }); |
| | 148 | await commitWithDate( |
| | 149 | tmp.path, |
| | 150 | "newer.txt", |
| | 151 | "newer branch change\n", |
| | 152 | "Fri, 1 Jan 2038 00:00:00 +0000", |
| | 153 | "newer branch change", |
| | 154 | ); |
| | 155 | |
| | 156 | // Switch away to show sort order is recency-based, not "current"-based. |
| | 157 | await checkoutGitBranch({ cwd: tmp.path, branch: "older-branch" }); |
| 114 | | 158 | |
| 115 | const result = await listGitBranches({ cwd: tmp.path }); | 159 | const result = await listGitBranches({ cwd: tmp.path }); |
| 116 | expect(result.branches[0]!.current).toBe(true); | 160 | expect(result.branches[0]!.name).toBe("newer-branch"); |
| 117 | }); | 161 | }); |
| 118 | | 162 | |
| 119 | it("lists multiple branches after creating them", async () => { | 163 | it("lists multiple branches after creating them", async () => { |