Skip to content

Commit 28f81ab

Browse files
fix(server): storage cleanup finds the default branch when the remote has no HEAD ref (pingdotgg#17909)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 780fb61 commit 28f81ab

2 files changed

Lines changed: 102 additions & 1 deletion

File tree

‎apps/server/src/storageCleanup.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,83 @@ describe("storage cleanup reports and local file policies", () => {
15671567
}),
15681568
),
15691569
);
1570+
it.live(
1571+
"discovers trunk without a remote HEAD or tracking ref and removes an integrated worktree",
1572+
() =>
1573+
runCleanupTest(
1574+
Effect.gen(function* () {
1575+
const { service, fs, repo, worktree, command } = yield* cleanupFixture({
1576+
...DEFAULT_SERVER_SETTINGS.storageCleanup,
1577+
worktreeUnchanged: true,
1578+
});
1579+
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
1580+
yield* command(remote, ["init", "--bare", "-b", "trunk"]);
1581+
yield* command(repo, ["remote", "add", "origin", remote]);
1582+
yield* command(repo, ["push", "origin", "main:trunk"]);
1583+
yield* command(repo, ["update-ref", "-d", "refs/remotes/origin/trunk"]);
1584+
expect((yield* command(repo, ["for-each-ref", "refs/remotes/origin"])).stdout).toBe("");
1585+
1586+
const report = yield* service.runNow;
1587+
expect(report.entries[0]).toMatchObject({
1588+
outcome: "removed",
1589+
reason: "No commits beyond the default branch",
1590+
});
1591+
expect(yield* fs.exists(worktree)).toBe(false);
1592+
}),
1593+
),
1594+
);
1595+
it.live(
1596+
"keeps worktree commits present on nondefault main but absent from advertised trunk",
1597+
() =>
1598+
runCleanupTest(
1599+
Effect.gen(function* () {
1600+
const { service, fs, repo, worktree, command } = yield* cleanupFixture({
1601+
...DEFAULT_SERVER_SETTINGS.storageCleanup,
1602+
worktreeUnchanged: true,
1603+
});
1604+
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
1605+
yield* command(remote, ["init", "--bare", "-b", "trunk"]);
1606+
yield* command(repo, ["remote", "add", "origin", remote]);
1607+
yield* command(repo, ["push", "origin", "main:trunk"]);
1608+
yield* fs.writeFileString(`${worktree}/tracked.txt`, "unmerged work\n");
1609+
yield* command(worktree, ["commit", "-am", "unmerged work"]);
1610+
yield* command(repo, ["push", "origin", "feature:main"]);
1611+
expect(
1612+
(yield* command(repo, ["symbolic-ref", "refs/remotes/origin/HEAD"]).pipe(Effect.exit))
1613+
._tag,
1614+
).toBe("Failure");
1615+
1616+
const report = yield* service.runNow;
1617+
expect(report.entries[0]).toMatchObject({
1618+
outcome: "kept",
1619+
reason: "Has commits beyond the default branch",
1620+
});
1621+
expect(yield* fs.readFileString(`${worktree}/tracked.txt`)).toBe("unmerged work\n");
1622+
}),
1623+
),
1624+
);
1625+
it.live("keeps the worktree when the remote default is unavailable even if main exists", () =>
1626+
runCleanupTest(
1627+
Effect.gen(function* () {
1628+
const { service, fs, repo, worktree, command } = yield* cleanupFixture({
1629+
...DEFAULT_SERVER_SETTINGS.storageCleanup,
1630+
worktreeUnchanged: true,
1631+
});
1632+
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
1633+
yield* command(remote, ["init", "--bare", "-b", "trunk"]);
1634+
yield* command(repo, ["remote", "add", "origin", remote]);
1635+
// The remote HEAD names an unborn trunk; main exists but is not its default.
1636+
yield* command(repo, ["push", "origin", "main"]);
1637+
1638+
const report = yield* service.runNow;
1639+
expect(report.entries[0]).toMatchObject({
1640+
outcome: "kept",
1641+
reason: "Default branch is unavailable",
1642+
});
1643+
expect(yield* fs.exists(worktree)).toBe(true);
1644+
}),
1645+
),
1646+
);
15701647
it.live("preserves tracked edits written after the final status check", () =>
15711648
runCleanupTest(
15721649
Effect.gen(function* () {

‎apps/server/src/storageCleanup.ts‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ const make = Effect.gen(function* () {
592592
)).filter((thread) => thread.worktreePath !== null && thread.branch !== null);
593593
const snapshot = yield* readThreads();
594594
const refreshedDefaultRefs = new Map<string, Set<string>>();
595+
const defaultBranches = new Map<string, string | null>();
595596
const groups = Map.groupBy(
596597
snapshot.threads.filter((thread) => thread.worktreePath !== null),
597598
(thread) => path.resolve(thread.worktreePath!),
@@ -684,7 +685,30 @@ const make = Effect.gen(function* () {
684685
if (!eligible && (settings.worktreeUnchanged || settings.worktreeOnMerge)) {
685686
const repositoryCwd = path.resolve(project.workspaceRoot);
686687
const remote = yield* git.resolvePrimaryRemoteName(repositoryCwd);
687-
const branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
688+
const defaultBranchKey = `${repositoryCwd}\0${remote}`;
689+
let branch = defaultBranches.get(defaultBranchKey);
690+
if (branch === undefined) {
691+
branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
692+
// Without a local HEAD ref, only the remote can identify its default;
693+
// a conventional name may be a feature branch.
694+
if (branch === null) {
695+
branch = yield* git
696+
.execute({
697+
operation: "StorageCleanup.resolveDefaultBranch",
698+
cwd: repositoryCwd,
699+
args: ["ls-remote", "--symref", "--", remote, "HEAD"],
700+
timeoutMs: 20_000,
701+
})
702+
.pipe(
703+
Effect.map(
704+
(result) =>
705+
/^ref: refs\/heads\/(.+)\tHEAD\r?$/m.exec(result.stdout)?.[1] ?? null,
706+
),
707+
Effect.orElseSucceed(() => null),
708+
);
709+
}
710+
defaultBranches.set(defaultBranchKey, branch);
711+
}
688712
if (branch === null) return keep("default branch is unavailable");
689713
const defaultRef = `refs/remotes/${remote}/${branch}`;
690714
const refreshed = refreshedDefaultRefs.get(repositoryCwd) ?? new Set<string>();

0 commit comments

Comments
 (0)