Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
51c0cb5c78 | ||
|
|
7408f04bbc | ||
|
|
6dfb9f1598 |
No files matched your search
+108
-5
@@ -2,7 +2,7 @@ import { existsSync } from "node:fs";
|
|||||||
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||||
import { tmpdir } from "node:os";
|
import { tmpdir } from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
GitCoreService,
|
GitCoreService,
|
||||||
@@ -51,6 +51,25 @@ async function initRepoWithCommit(cwd: string): Promise<void> {
|
|||||||
await git(cwd, "commit -m 'initial commit'");
|
await git(cwd, "commit -m 'initial commit'");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function commitWithDate(
|
||||||
|
cwd: string,
|
||||||
|
fileName: string,
|
||||||
|
fileContents: string,
|
||||||
|
dateIsoString: string,
|
||||||
|
message: string,
|
||||||
|
): Promise<void> {
|
||||||
|
await writeFile(path.join(cwd, fileName), fileContents);
|
||||||
|
await git(cwd, `add ${fileName}`);
|
||||||
|
const commitResult = await runTerminalCommand({
|
||||||
|
command: `GIT_AUTHOR_DATE="${dateIsoString}" GIT_COMMITTER_DATE="${dateIsoString}" git commit -m "${message}"`,
|
||||||
|
cwd,
|
||||||
|
timeoutMs: 10_000,
|
||||||
|
});
|
||||||
|
if (commitResult.code !== 0) {
|
||||||
|
throw new Error(`git dated commit failed: ${commitResult.stderr}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// ── Tests ──
|
// ── Tests ──
|
||||||
|
|
||||||
describe("git integration", () => {
|
describe("git integration", () => {
|
||||||
@@ -106,14 +125,82 @@ describe("git integration", () => {
|
|||||||
expect(current!.current).toBe(true);
|
expect(current!.current).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("sorts current branch first", async () => {
|
it("keeps current branch first and sorts the remaining branches by recency", async () => {
|
||||||
await using tmp = await makeTmpDir();
|
await using tmp = await makeTmpDir();
|
||||||
await initRepoWithCommit(tmp.path);
|
await initRepoWithCommit(tmp.path);
|
||||||
await createGitBranch({ cwd: tmp.path, branch: "aaa-first-alpha" });
|
const initialBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
|
||||||
await createGitBranch({ cwd: tmp.path, branch: "zzz-last" });
|
(branch) => branch.current,
|
||||||
|
)!.name;
|
||||||
|
|
||||||
|
await createGitBranch({ cwd: tmp.path, branch: "older-branch" });
|
||||||
|
await checkoutGitBranch({ cwd: tmp.path, branch: "older-branch" });
|
||||||
|
await commitWithDate(
|
||||||
|
tmp.path,
|
||||||
|
"older.txt",
|
||||||
|
"older branch change\n",
|
||||||
|
"Thu, 1 Jan 2037 00:00:00 +0000",
|
||||||
|
"older branch change",
|
||||||
|
);
|
||||||
|
|
||||||
|
await checkoutGitBranch({ cwd: tmp.path, branch: initialBranch });
|
||||||
|
await createGitBranch({ cwd: tmp.path, branch: "newer-branch" });
|
||||||
|
await checkoutGitBranch({ cwd: tmp.path, branch: "newer-branch" });
|
||||||
|
await commitWithDate(
|
||||||
|
tmp.path,
|
||||||
|
"newer.txt",
|
||||||
|
"newer branch change\n",
|
||||||
|
"Fri, 1 Jan 2038 00:00:00 +0000",
|
||||||
|
"newer branch change",
|
||||||
|
);
|
||||||
|
|
||||||
|
// Switch away to show current branch is pinned, then remaining branches are recency-sorted.
|
||||||
|
await checkoutGitBranch({ cwd: tmp.path, branch: "older-branch" });
|
||||||
|
|
||||||
const result = await listGitBranches({ cwd: tmp.path });
|
const result = await listGitBranches({ cwd: tmp.path });
|
||||||
expect(result.branches[0]!.current).toBe(true);
|
expect(result.branches[0]!.name).toBe("older-branch");
|
||||||
|
expect(result.branches[1]!.name).toBe("newer-branch");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("keeps default branch right after current branch", async () => {
|
||||||
|
await using tmp = await makeTmpDir();
|
||||||
|
await using remote = await makeTmpDir();
|
||||||
|
await initRepoWithCommit(tmp.path);
|
||||||
|
const defaultBranch = (await listGitBranches({ cwd: tmp.path })).branches.find(
|
||||||
|
(branch) => branch.current,
|
||||||
|
)!.name;
|
||||||
|
|
||||||
|
await git(remote.path, "init --bare");
|
||||||
|
await git(tmp.path, `remote add origin ${JSON.stringify(remote.path)}`);
|
||||||
|
await git(tmp.path, `push -u origin ${defaultBranch}`);
|
||||||
|
await git(tmp.path, `remote set-head origin ${defaultBranch}`);
|
||||||
|
|
||||||
|
await createGitBranch({ cwd: tmp.path, branch: "current-branch" });
|
||||||
|
await checkoutGitBranch({ cwd: tmp.path, branch: "current-branch" });
|
||||||
|
await commitWithDate(
|
||||||
|
tmp.path,
|
||||||
|
"current.txt",
|
||||||
|
"current change\n",
|
||||||
|
"Thu, 1 Jan 2037 00:00:00 +0000",
|
||||||
|
"current change",
|
||||||
|
);
|
||||||
|
|
||||||
|
await checkoutGitBranch({ cwd: tmp.path, branch: defaultBranch });
|
||||||
|
await createGitBranch({ cwd: tmp.path, branch: "newer-branch" });
|
||||||
|
await checkoutGitBranch({ cwd: tmp.path, branch: "newer-branch" });
|
||||||
|
await commitWithDate(
|
||||||
|
tmp.path,
|
||||||
|
"newer.txt",
|
||||||
|
"newer change\n",
|
||||||
|
"Fri, 1 Jan 2038 00:00:00 +0000",
|
||||||
|
"newer change",
|
||||||
|
);
|
||||||
|
|
||||||
|
await checkoutGitBranch({ cwd: tmp.path, branch: "current-branch" });
|
||||||
|
|
||||||
|
const result = await listGitBranches({ cwd: tmp.path });
|
||||||
|
expect(result.branches[0]!.name).toBe("current-branch");
|
||||||
|
expect(result.branches[1]!.name).toBe(defaultBranch);
|
||||||
|
expect(result.branches[2]!.name).toBe("newer-branch");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("lists multiple branches after creating them", async () => {
|
it("lists multiple branches after creating them", async () => {
|
||||||
@@ -516,5 +603,21 @@ describe("git integration", () => {
|
|||||||
const skipped = await core.pushCurrentBranch(tmp.path, null);
|
const skipped = await core.pushCurrentBranch(tmp.path, null);
|
||||||
expect(skipped.status).toBe("skipped_up_to_date");
|
expect(skipped.status).toBe("skipped_up_to_date");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("lists branches when recency lookup fails", async () => {
|
||||||
|
await using tmp = await makeTmpDir();
|
||||||
|
await initRepoWithCommit(tmp.path);
|
||||||
|
const core = new GitCoreService();
|
||||||
|
const recencySpy = vi
|
||||||
|
.spyOn(core as any, "readBranchRecency")
|
||||||
|
.mockRejectedValueOnce(new Error("timeout"));
|
||||||
|
|
||||||
|
const result = await core.listBranches({ cwd: tmp.path });
|
||||||
|
|
||||||
|
expect(result.isRepo).toBe(true);
|
||||||
|
expect(result.branches.length).toBeGreaterThan(0);
|
||||||
|
expect(result.branches[0]?.current).toBe(true);
|
||||||
|
recencySpy.mockRestore();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
+42
-3
@@ -312,7 +312,40 @@ export class GitCoreService {
|
|||||||
return value.length > 0 ? value : null;
|
return value.length > 0 ? value : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async readBranchRecency(cwd: string): Promise<Map<string, number>> {
|
||||||
|
const branchRecency = await executeGit(
|
||||||
|
cwd,
|
||||||
|
["for-each-ref", "--format=%(refname:short)%09%(committerdate:unix)", "refs/heads"],
|
||||||
|
{
|
||||||
|
timeoutMs: 15_000,
|
||||||
|
allowNonZeroExit: true,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const branchLastCommit = new Map<string, number>();
|
||||||
|
if (branchRecency.code !== 0) {
|
||||||
|
return branchLastCommit;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const line of branchRecency.stdout.split("\n")) {
|
||||||
|
if (line.length === 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const [name, lastCommitRaw] = line.split("\t");
|
||||||
|
if (!name) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
const lastCommit = Number.parseInt(lastCommitRaw ?? "0", 10);
|
||||||
|
branchLastCommit.set(name, Number.isFinite(lastCommit) ? lastCommit : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return branchLastCommit;
|
||||||
|
}
|
||||||
|
|
||||||
async listBranches(input: GitListBranchesInput): Promise<GitListBranchesResult> {
|
async listBranches(input: GitListBranchesInput): Promise<GitListBranchesResult> {
|
||||||
|
const branchRecencyPromise = this.readBranchRecency(input.cwd).catch(
|
||||||
|
() => new Map<string, number>(),
|
||||||
|
);
|
||||||
const result = await executeGit(input.cwd, ["branch", "--no-color"], {
|
const result = await executeGit(input.cwd, ["branch", "--no-color"], {
|
||||||
timeoutMs: 10_000,
|
timeoutMs: 10_000,
|
||||||
allowNonZeroExit: true,
|
allowNonZeroExit: true,
|
||||||
@@ -326,7 +359,7 @@ export class GitCoreService {
|
|||||||
throw new Error(stderr || "git branch failed");
|
throw new Error(stderr || "git branch failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
const [defaultRef, worktreeList] = await Promise.all([
|
const [defaultRef, worktreeList, branchLastCommit] = await Promise.all([
|
||||||
executeGit(input.cwd, ["symbolic-ref", "refs/remotes/origin/HEAD"], {
|
executeGit(input.cwd, ["symbolic-ref", "refs/remotes/origin/HEAD"], {
|
||||||
timeoutMs: 5_000,
|
timeoutMs: 5_000,
|
||||||
allowNonZeroExit: true,
|
allowNonZeroExit: true,
|
||||||
@@ -335,6 +368,7 @@ export class GitCoreService {
|
|||||||
timeoutMs: 5_000,
|
timeoutMs: 5_000,
|
||||||
allowNonZeroExit: true,
|
allowNonZeroExit: true,
|
||||||
}),
|
}),
|
||||||
|
branchRecencyPromise,
|
||||||
]);
|
]);
|
||||||
const defaultBranch =
|
const defaultBranch =
|
||||||
defaultRef.code === 0
|
defaultRef.code === 0
|
||||||
@@ -370,8 +404,13 @@ export class GitCoreService {
|
|||||||
};
|
};
|
||||||
})
|
})
|
||||||
.toSorted((a, b) => {
|
.toSorted((a, b) => {
|
||||||
if (a.current !== b.current) return a.current ? -1 : 1;
|
const aPriority = a.current ? 0 : a.isDefault ? 1 : 2;
|
||||||
if (a.isDefault !== b.isDefault) return a.isDefault ? -1 : 1;
|
const bPriority = b.current ? 0 : b.isDefault ? 1 : 2;
|
||||||
|
if (aPriority !== bPriority) return aPriority - bPriority;
|
||||||
|
|
||||||
|
const aLastCommit = branchLastCommit.get(a.name) ?? 0;
|
||||||
|
const bLastCommit = branchLastCommit.get(b.name) ?? 0;
|
||||||
|
if (aLastCommit !== bLastCommit) return bLastCommit - aLastCommit;
|
||||||
return a.name.localeCompare(b.name);
|
return a.name.localeCompare(b.name);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user