fix(server): keep submodules out of parent Git rescans

Use Git admin path containment when a watched Git directory is missing. An intact parent and its submodules keep separate common directories even though their administrative files share an ancestor.
This commit is contained in:
Mohamed Boudra
2026-11-26 02:32:05 +01:00
parent f84cfbc362
commit d97d0ba1c0
2 changed files with 60 additions and 5 deletions
@@ -726,6 +726,56 @@ describe("observed workspace placement", () => {
}
});
test("skips a submodule and its linked checkout when the parent Git directory is intact", async () => {
const observed = new ObservedPlacements(
[{ id: "outer", root: "outer", workspaces: [{ id: "outer-ws", cwd: "outer" }] }],
true,
);
try {
await observed.start();
await observed.add({ id: "source", root: "source" });
observed.git("source", "init", "-b", "main");
observed.git(
"source",
"-c",
"user.name=Test",
"-c",
"user.email=test@example.com",
"-c",
"commit.gpgsign=false",
"commit",
"--allow-empty",
"-m",
"initial",
);
observed.git("outer", "init", "-b", "main");
observed.git(
"outer",
"-c",
"protocol.file.allow=always",
"submodule",
"add",
"../source",
"sub",
);
observed.git("outer/sub", "worktree", "add", "-b", "feature", "../../sub-linked");
await observed.add({ id: "sub", root: "outer/sub" });
await observed.add({ id: "sub-linked", root: "sub-linked" });
observed.change("outer", ".git");
await observed.advanceBy(DEBOUNCE_MS);
expect(observed.takeCheckoutReads()).toEqual(["outer"]);
observed.rename("outer/.git", "outer/.git-backup");
observed.change("outer", ".git");
await observed.advanceBy(DEBOUNCE_MS);
expect(observed.takeCheckoutReads()).toEqual(["outer", "outer/sub", "sub-linked"]);
} finally {
await observed.disposeGit();
}
});
test("turns Git changes under several roots into one read per changed root", async () => {
const observed = new ObservedPlacements(threeProjects(1));
await observed.start();
@@ -789,10 +789,14 @@ async function findAffectedCheckoutDirectories(
const commonDirs = dirty.flatMap((entry) =>
typeof entry !== "string" && entry.repository ? [entry.repository.commonDir] : [],
);
// If a main checkout loses .git, surviving linked checkouts still point at
// their missing admin directories inside it.
const gitEntries = createPathContainmentMatcher({
roots: roots.map((root) => join(root, ".git")),
// A checkout can still point into a missing .git directory. For intact
// repositories, match common directories exactly so submodules stay independent.
const missingGitDirectories = createPathContainmentMatcher({
roots: dirty.flatMap((entry) =>
typeof entry !== "string" && entry.repository?.root !== entry.path
? [join(entry.path, ".git")]
: [],
),
compareAsWindows: process.platform === "win32",
});
const repositories = createPathContainmentMatcher({
@@ -806,7 +810,8 @@ async function findAffectedCheckoutDirectories(
const repository = typeof entry === "string" ? null : entry.repository;
const sharesMetadata =
repository &&
(repositories.matchesExact(repository.commonDir) || gitEntries.matches(repository.commonDir));
(repositories.matchesExact(repository.commonDir) ||
missingGitDirectories.matches(repository.commonDir));
// A surviving nested repository provides its own metadata even if its
// parent gains or loses a .git entry.
const hasIndependentNestedRepository =