Skip to content

Commit 11ba35e

Browse files
committed
fix(server): skip nested repositories with no commits before indexing the review
The earlier fix relied on `git add --intent-to-add --ignore-errors` exiting 1 on a nested repository with no commits. That only holds up to Git 2.52. Since Git 2.53 (6fe288bfbcbb, read-cache: drop submodule check from add_to_cache()) the add succeeds and records a gitlink, then `git diff --numstat` dies with "cannot hash scaffold" and exit 128, so the review still failed on CI's Git 2.55.0. Accepting exit 1 was also too wide. --ignore-errors skips any path Git cannot index, so an untracked file named git~1 was left out of the diff with no sign that the result was partial. Ask each nested repository for its HEAD first and leave out the ones with no commits, which gives the same result on every Git version. --ignore-errors and the exit 1 handling are gone, so any other add failure is reported again.
1 parent bb68209 commit 11ba35e

2 files changed

Lines changed: 78 additions & 32 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,9 +2177,56 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
21772177

21782178
assert.include(diff, "+# tracked change");
21792179
assert.include(diff, "+untracked contents");
2180+
assert.notInclude(diff, "scaffold");
2181+
2182+
const status = yield* driver.statusDetailsLocal(cwd, { includeBranchChanges: true });
2183+
assert.deepStrictEqual(status.branchChanges, {
2184+
baseRef: null,
2185+
insertions: 2,
2186+
deletions: 1,
2187+
});
21802188
}),
21812189
);
21822190

2191+
it.effect("keeps a nested repository with commits in the review", () =>
2192+
Effect.gen(function* () {
2193+
const cwd = yield* makeTmpDir();
2194+
yield* initRepoWithCommit(cwd);
2195+
const driver = yield* GitVcsDriver.GitVcsDriver;
2196+
const nested = `${cwd}/nested`;
2197+
yield* (yield* FileSystem.FileSystem).makeDirectory(nested);
2198+
yield* initRepoWithCommit(nested);
2199+
2200+
const preview = yield* driver.getReviewDiffPreview({ cwd, ignoreWhitespace: false });
2201+
const diff = preview.sources.find((source) => source.kind === "working-tree")?.diff ?? "";
2202+
2203+
assert.include(diff, "diff --git a/nested b/nested");
2204+
}),
2205+
);
2206+
2207+
// Git rejects this name everywhere (core.protectNTFS). On Windows it is also the short name of .git.
2208+
it.effect.skipIf(HostProcess.Platform.defaultValue() === "win32")(
2209+
"fails the review instead of leaving out an untracked file Git cannot index",
2210+
() =>
2211+
Effect.gen(function* () {
2212+
const cwd = yield* makeTmpDir();
2213+
yield* initRepoWithCommit(cwd);
2214+
const driver = yield* GitVcsDriver.GitVcsDriver;
2215+
yield* writeTextFile(cwd, "git~1", "cannot be indexed\n");
2216+
yield* writeTextFile(cwd, "untracked.txt", "untracked contents\n");
2217+
2218+
const error = yield* driver
2219+
.getReviewDiffPreview({ cwd, ignoreWhitespace: false })
2220+
.pipe(Effect.flip);
2221+
2222+
assert.strictEqual(error._tag, "GitCommandError");
2223+
assert.strictEqual(
2224+
(error as GitCommandError).operation,
2225+
"GitVcsDriver.readUnifiedWorkingTreeReviewDiff.addUntracked",
2226+
);
2227+
}),
2228+
);
2229+
21832230
it.effect("keeps untracked filenames with pathspec magic in the review", () =>
21842231
Effect.gen(function* () {
21852232
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2980,7 +2980,21 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
29802980
{ concurrency: 2 },
29812981
);
29822982
const stagedDeletions = new Set(stagedDeletionsStdout.split("\0").filter(Boolean));
2983-
const pathsToAdd = untrackedPaths.filter((relativePath) => !stagedDeletions.has(relativePath));
2983+
// Only a nested repository is listed with a trailing slash. With no commits it cannot be indexed:
2984+
// Git before 2.53 fails the add, and later versions accept it and then fail every diff.
2985+
const pathsToAdd = yield* Effect.filter(
2986+
untrackedPaths.filter((relativePath) => !stagedDeletions.has(relativePath)),
2987+
(relativePath) =>
2988+
relativePath.endsWith("/")
2989+
? executeGit(
2990+
"GitVcsDriver.readUnifiedWorkingTreeReviewDiff.nestedRepositoryHead",
2991+
path.resolve(cwd, relativePath),
2992+
["rev-parse", "--verify", "--quiet", "HEAD"],
2993+
{ allowNonZeroExit: true },
2994+
).pipe(Effect.map((result) => result.exitCode !== 1))
2995+
: Effect.succeed(true),
2996+
{ concurrency: 16 },
2997+
);
29842998
if (pathsToAdd.length === 0) return undefined;
29852999

29863000
const indexPath = path.isAbsolute(indexValue.trim())
@@ -3015,37 +3029,22 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
30153029
[...tempIndexConfig, "update-index", "--no-split-index"],
30163030
{ env },
30173031
);
3018-
const addUntrackedOperation = "GitVcsDriver.readUnifiedWorkingTreeReviewDiff.addUntracked";
3019-
const addUntrackedArgs = [
3020-
...tempIndexConfig,
3021-
// Avoid redundant tracked-entry pathspec matching in this disposable index.
3022-
"-c",
3023-
"core.preloadIndex=false",
3024-
"--literal-pathspecs",
3025-
"add",
3026-
"--intent-to-add",
3027-
"--ignore-errors",
3028-
"--pathspec-from-file=-",
3029-
"--pathspec-file-nul",
3030-
];
3031-
const addedUntracked = yield* executeGit(addUntrackedOperation, cwd, addUntrackedArgs, {
3032-
env,
3033-
stdin: `${pathsToAdd.join("\0")}\0`,
3034-
allowNonZeroExit: true,
3035-
});
3036-
// --ignore-errors exits 1 after skipping paths it cannot index, such as a nested repository
3037-
// with no commits. Anything else is a fatal failure.
3038-
if (addedUntracked.exitCode !== 0 && addedUntracked.exitCode !== 1) {
3039-
const reason = classifyGitFailure(addedUntracked.stderr);
3040-
return yield* new GitCommandError({
3041-
...gitCommandContext({ operation: addUntrackedOperation, cwd, args: addUntrackedArgs }),
3042-
...(reason === null ? {} : { reason }),
3043-
detail: gitFailureDetail(reason),
3044-
exitCode: addedUntracked.exitCode,
3045-
stdoutLength: addedUntracked.stdout.length,
3046-
stderrLength: addedUntracked.stderr.length,
3047-
});
3048-
}
3032+
yield* executeGit(
3033+
"GitVcsDriver.readUnifiedWorkingTreeReviewDiff.addUntracked",
3034+
cwd,
3035+
[
3036+
...tempIndexConfig,
3037+
// Avoid redundant tracked-entry pathspec matching in this disposable index.
3038+
"-c",
3039+
"core.preloadIndex=false",
3040+
"--literal-pathspecs",
3041+
"add",
3042+
"--intent-to-add",
3043+
"--pathspec-from-file=-",
3044+
"--pathspec-file-nul",
3045+
],
3046+
{ env, stdin: `${pathsToAdd.join("\0")}\0` },
3047+
);
30493048
return env;
30503049
});
30513050

0 commit comments

Comments
 (0)