Skip to content

Commit 74a933c

Browse files
committed
fix(server): inspect repository markers and legacy submodule stores
1 parent 004eb70 commit 74a933c

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,17 +485,19 @@ const runCleanupTest = <A, E>(
485485
it.live.each([
486486
{ policy: "tracked-changes", kind: "worktree" },
487487
{ policy: "tracked-changes", kind: "repository" },
488+
{ policy: "tracked-changes", kind: "uppercase" },
488489
{ policy: "tracked-changes", kind: "bare" },
489490
{ policy: "uncommitted-changes", kind: "worktree" },
490491
{ policy: "uncommitted-changes", kind: "repository" },
492+
{ policy: "uncommitted-changes", kind: "uppercase" },
491493
{ policy: "uncommitted-changes", kind: "bare" },
492494
] as const)("keeps an ignored nested $policy / $kind", ({ policy, kind }) =>
493495
runCleanupTest(
494496
Effect.gen(function* () {
495497
const { service, fs, repo, worktree, command, setPolicy } = yield* cleanupFixture();
496498
setPolicy(policy);
497499
const nested = `${worktree}/dist/nested`;
498-
if (kind === "worktree") {
500+
if (kind === "worktree" || kind === "uppercase") {
499501
yield* command(repo, ["worktree", "add", "-b", "nested", nested]);
500502
} else {
501503
yield* command(repo, [
@@ -506,6 +508,7 @@ it.live.each([
506508
nested,
507509
]);
508510
}
511+
if (kind === "uppercase") yield* fs.rename(`${nested}/.git`, `${nested}/.GIT`);
509512
yield* fs.writeFileString(`${nested}/uncommitted.txt`, "irreplaceable work\n");
510513
expect((yield* command(worktree, ["status", "--porcelain"])).stdout).toBe("");
511514
const report = yield* service.runNow;
@@ -516,6 +519,12 @@ it.live.each([
516519
expect(yield* fs.readFileString(`${nested}/uncommitted.txt`)).toBe("irreplaceable work\n");
517520
expect(
518521
(yield* command(nested, [
522+
...(kind === "uppercase"
523+
? [
524+
`--git-dir=${(yield* fs.readFileString(`${nested}/.GIT`)).trim().slice(8)}`,
525+
"--work-tree=.",
526+
]
527+
: []),
519528
"-c",
520529
"safe.bareRepository=all",
521530
"rev-parse",
@@ -609,6 +618,33 @@ it.live("removes clean submodules recursively, including deinitialized repositor
609618
yield* command(repo, ["clone", "--no-hardlinks", repo, legacy]);
610619
yield* command(worktree, ["submodule", "add", repo, "legacy"]);
611620
expect((yield* fs.stat(`${legacy}/.git`)).type).toBe("Directory");
621+
yield* command(legacy, [
622+
"-c",
623+
"protocol.file.allow=always",
624+
"submodule",
625+
"add",
626+
repo,
627+
"child",
628+
]);
629+
yield* command(legacy, [
630+
"-c",
631+
"protocol.file.allow=always",
632+
"submodule",
633+
"add",
634+
repo,
635+
"stored",
636+
]);
637+
yield* command(legacy, [
638+
"-c",
639+
"user.name=Cleanup test",
640+
"-c",
641+
642+
"commit",
643+
"-am",
644+
"record legacy children",
645+
]);
646+
yield* command(legacy, ["push", "origin", "HEAD:legacy"]);
647+
yield* command(legacy, ["submodule", "deinit", "stored"]);
612648
yield* recordSubmodules();
613649
yield* command(submodule, ["submodule", "deinit", "stored"]);
614650
const admin = (yield* command(worktree, ["rev-parse", "--absolute-git-dir"])).stdout.trim();

‎apps/server/src/storageCleanup.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ const make = Effect.gen(function* () {
284284
const skipped = yield* sharedPnpmVirtualStore(root);
285285
const incomplete = { bytes: null, reason: "worktree could not be fully inspected" };
286286
const repositories = new Set([pathKey(root)]);
287+
const moduleStores: string[] = [];
287288
if (inspectRepositories && (yield* fs.exists(path.join(root, ".gitmodules")))) {
288289
const submodules = yield* git.execute({
289290
operation: "StorageCleanup.nestedRepositories",
@@ -301,6 +302,7 @@ const make = Effect.gen(function* () {
301302
for (const submodule of submodules.stdout.split("\0").filter(Boolean)) {
302303
repositories.add(pathKey(path.resolve(submodule)));
303304
repositories.add(pathKey(path.resolve(submodule, ".git")));
305+
moduleStores.push(pathKey(path.resolve(submodule, ".git", "modules")));
304306
}
305307
}
306308
const pending = [root];
@@ -334,13 +336,15 @@ const make = Effect.gen(function* () {
334336
});
335337
} else if (stat.type === "Directory") {
336338
const names = yield* fs.readDirectory(target);
339+
const markers = inspectRepositories ? names.map((name) => name.toLowerCase()) : [];
337340
// Git status omits ignored repositories, and worktree remove deletes them.
338341
// Registered submodules have their own recursive safety checks below.
339342
if (
340343
inspectRepositories &&
341344
!repositories.has(pathKey(target)) &&
342-
(names.includes(".git") ||
343-
(names.includes("HEAD") && names.includes("objects") && names.includes("refs")))
345+
!moduleStores.some((store) => pathKey(target) === store || inside(store, target)) &&
346+
(markers.includes(".git") ||
347+
(markers.includes("head") && markers.includes("objects") && markers.includes("refs")))
344348
)
345349
return { bytes: null, reason: "contains a nested Git repository" };
346350
entries += names.length;

0 commit comments

Comments
 (0)