Skip to content

Commit 9f67823

Browse files
committed
fix(server): storage cleanup finds the default branch when the remote has no HEAD ref
A remote added with `git remote add` has no refs/remotes/<remote>/HEAD, so cleanup kept every worktree in the project with "Default branch is unavailable" and the unchanged and merged rules never ran. Probe main then master on the primary remote before giving up, as the other callers of resolveDefaultBranchName already do. Fixes #17807
1 parent 2a4ee33 commit 9f67823

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ const cleanupFixture = Effect.gen(function* () {
288288
editBeforeRemoval = true;
289289
},
290290
fs,
291+
repo,
291292
worktree,
292293
config,
293294
command,
@@ -304,6 +305,9 @@ const cleanupFixture = Effect.gen(function* () {
304305
setPolicy: (worktreeKeepWhen: WorktreeKeepWhen) => {
305306
settings = { ...settings, storageCleanup: { ...settings.storageCleanup, worktreeKeepWhen } };
306307
},
308+
setRules: (rules: Partial<typeof settings.storageCleanup>) => {
309+
settings = { ...settings, storageCleanup: { ...settings.storageCleanup, ...rules } };
310+
},
307311
disable: () => {
308312
settings = {
309313
...settings,
@@ -478,6 +482,33 @@ describe("storage cleanup reports and local file policies", () => {
478482
}),
479483
),
480484
);
485+
it.live.each([
486+
["main", "removed", "No commits beyond the default branch"],
487+
["master", "removed", "No commits beyond the default branch"],
488+
["trunk", "kept", "Default branch is unavailable"],
489+
] as const)(
490+
"resolves the default branch %j when the remote has no HEAD ref",
491+
([branch, outcome, reason]) =>
492+
runCleanupTest(
493+
Effect.gen(function* () {
494+
const { service, fs, repo, worktree, command, setRules } = yield* cleanupFixture;
495+
// `git remote add` followed by a push, unlike a clone, leaves
496+
// refs/remotes/origin/HEAD unset.
497+
const remote = yield* fs.makeTempDirectoryScoped({ prefix: "cleanup-remote-" });
498+
yield* command(remote, ["init", "--bare", "-b", branch]);
499+
yield* command(repo, ["remote", "add", "origin", remote]);
500+
yield* command(repo, ["push", "origin", `main:${branch}`]);
501+
const head = yield* command(repo, ["symbolic-ref", "refs/remotes/origin/HEAD"]).pipe(
502+
Effect.exit,
503+
);
504+
expect(head._tag).toBe("Failure");
505+
setRules({ worktreeAfterDays: null, worktreeUnchanged: true });
506+
const report = yield* service.runNow;
507+
expect(report.entries[0]).toMatchObject({ outcome, reason });
508+
expect(yield* fs.exists(worktree)).toBe(outcome === "kept");
509+
}),
510+
),
511+
);
481512
it.live("preserves tracked edits written after the final status check", () =>
482513
runCleanupTest(
483514
Effect.gen(function* () {

‎apps/server/src/storageCleanup.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,14 @@ const make = Effect.gen(function* () {
432432
if (!eligible && (settings.worktreeUnchanged || settings.worktreeOnMerge)) {
433433
const repositoryCwd = path.resolve(project.workspaceRoot);
434434
const remote = yield* git.resolvePrimaryRemoteName(repositoryCwd);
435-
const branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
435+
let branch = yield* git.resolveDefaultBranchName(repositoryCwd, remote);
436+
// A remote added with `git remote add` has no HEAD ref, so probe the
437+
// conventional names before giving up.
438+
for (const refName of ["main", "master"]) {
439+
if (branch !== null) break;
440+
if (yield* git.remoteBranchExists({ cwd: repositoryCwd, remoteName: remote, refName }))
441+
branch = refName;
442+
}
436443
if (branch === null) return keep("default branch is unavailable");
437444
const defaultRef = `refs/remotes/${remote}/${branch}`;
438445
const refreshed = refreshedDefaultRefs.get(repositoryCwd) ?? new Set<string>();

0 commit comments

Comments
 (0)