Skip to content

Commit 3637a94

Browse files
steipetePeter Steinberger
andauthored
fix: preserve validated legacy checkout blobs (#978)
* fix: preserve validated legacy checkout blobs * test: skip nonportable pathspec fixture on Windows --------- Co-authored-by: Peter Steinberger <[email protected]>
1 parent 61a02b0 commit 3637a94

2 files changed

Lines changed: 150 additions & 3 deletions

File tree

‎src/repair/target-validation.ts‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3066,7 +3066,7 @@ function buildRawWorktreeTree(
30663066
const indexEnv = { ...objectEnv, GIT_INDEX_FILE: indexFile };
30673067
git.run(["read-tree", "HEAD"], "raw worktree temporary index", { env: indexEnv });
30683068
const updates: string[] = [];
3069-
const canonicalEntries: Array<{ mode: string; relativePath: string }> = [];
3069+
const canonicalEntries: Array<{ mode: string; relativePath: string; sourceOid?: string }> = [];
30703070
const rawEntries: Array<{ mode: string; relativePath: string; sourcePath: string }> = [];
30713071
const worktreeLeafPaths = new Set<string>();
30723072
const zeroOid = "0".repeat(headSha.length);
@@ -3146,7 +3146,11 @@ function buildRawWorktreeTree(
31463146
}
31473147
throw new Error(`unsafe changed target Git ${unsafeAttribute} attribute: ${relativePath}`);
31483148
}
3149-
canonicalEntries.push({ mode, relativePath });
3149+
canonicalEntries.push({
3150+
mode,
3151+
relativePath,
3152+
...(sourceEntry?.mode === mode ? { sourceOid: sourceEntry.oid } : {}),
3153+
});
31503154
continue;
31513155
}
31523156
rawEntries.push({ mode, relativePath, sourcePath });
@@ -3162,8 +3166,29 @@ function buildRawWorktreeTree(
31623166
if (oids.length !== canonicalEntries.length) {
31633167
throw new Error("canonical worktree hash output did not match target paths");
31643168
}
3169+
const noncanonicalEntries = canonicalEntries
3170+
.map((entry, index) => ({ entry, oid: oids[index]! }))
3171+
.filter(({ entry, oid }) => entry.sourceOid && entry.sourceOid !== oid);
3172+
const rawOids =
3173+
noncanonicalEntries.length === 0
3174+
? []
3175+
: hashTargetWorktreeFiles(
3176+
git,
3177+
noncanonicalEntries.map(({ entry }) => entry.relativePath),
3178+
true,
3179+
"hash noncanonical worktree files",
3180+
objectEnv,
3181+
);
3182+
if (rawOids.length !== noncanonicalEntries.length) {
3183+
throw new Error("noncanonical worktree hash output did not match target paths");
3184+
}
3185+
const matchingRawOids = new Map(
3186+
noncanonicalEntries.map(({ entry }, index) => [entry.relativePath, rawOids[index]]),
3187+
);
31653188
for (const [index, entry] of canonicalEntries.entries()) {
3166-
updates.push(`${entry.mode} ${oids[index]}\t${entry.relativePath}\0`);
3189+
const rawOid = matchingRawOids.get(entry.relativePath);
3190+
const oid = rawOid && rawOid === entry.sourceOid ? rawOid : oids[index];
3191+
updates.push(`${entry.mode} ${oid}\t${entry.relativePath}\0`);
31673192
}
31683193
}
31693194
if (rawEntries.length > 0) {

‎test/repair/target-validation.test.ts‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5257,6 +5257,128 @@ test("checkpoint plumbing commits raw modified, added, and deleted worktree cont
52575257
assert.equal(result.tree, git(cwd, "rev-parse", "HEAD^{tree}"));
52585258
});
52595259

5260+
test("checkpoint plumbing preserves a review fix restoring a legacy CRLF blob", () => {
5261+
const cwd = gitPackageFixture({ check: 'node -e ""' });
5262+
const wrapper = "apps/android/gradlew.bat";
5263+
const wrapperPath = path.join(cwd, wrapper);
5264+
fs.mkdirSync(path.dirname(wrapperPath), { recursive: true });
5265+
fs.writeFileSync(path.join(cwd, ".gitattributes"), "*.bat text=auto eol=lf\n");
5266+
fs.writeFileSync(wrapperPath, "@echo off\r\nexit /b 0\r\n");
5267+
git(cwd, "add", ".");
5268+
const legacyBlob = git(cwd, "hash-object", "-w", "--no-filters", wrapper);
5269+
git(cwd, "update-index", "--cacheinfo", `100644,${legacyBlob},${wrapper}`);
5270+
git(cwd, "commit", "-m", "legacy CRLF base");
5271+
const base = git(cwd, "rev-parse", "HEAD");
5272+
const identity = {
5273+
name: "clawsweeper",
5274+
email: "274271284+clawsweeper[bot]@users.noreply.github.com",
5275+
};
5276+
5277+
fs.writeFileSync(path.join(cwd, "repair.txt"), "validated repair\n");
5278+
const initial = createTargetCheckpointWithPlumbing({
5279+
cwd,
5280+
messages: ["initial validated repair"],
5281+
identity,
5282+
});
5283+
assert.equal(initial.status, "committed");
5284+
assert.equal(git(cwd, "rev-parse", `HEAD:${wrapper}`), legacyBlob);
5285+
git(cwd, "add", "--renormalize", "--", wrapper);
5286+
git(cwd, "commit", "-m", "simulate previously normalized wrapper");
5287+
assert.notEqual(git(cwd, "rev-parse", `HEAD:${wrapper}`), legacyBlob);
5288+
5289+
git(cwd, "restore", `--source=${base}`, "--staged", "--worktree", "--", wrapper);
5290+
assert.equal(git(cwd, "status", "--porcelain"), `M ${wrapper}`);
5291+
5292+
const corrected = createTargetCheckpointWithPlumbing({
5293+
cwd,
5294+
messages: ["restore unrelated legacy wrapper"],
5295+
identity,
5296+
});
5297+
5298+
assert.equal(corrected.status, "committed");
5299+
assert.equal(git(cwd, "rev-parse", `HEAD:${wrapper}`), legacyBlob);
5300+
assert.equal(git(cwd, "show", "HEAD:repair.txt"), "validated repair");
5301+
assert.equal(git(cwd, "status", "--porcelain"), "");
5302+
5303+
const accepted = captureTargetCheckoutBinding(cwd);
5304+
assert.equal(captureFinalTargetCheckoutBinding(cwd, accepted, corrected.commit).status, "");
5305+
const compacted = compactTargetHistoryWithPlumbing({
5306+
cwd,
5307+
baseRef: base,
5308+
messages: ["compact validated repair"],
5309+
identity,
5310+
});
5311+
assert.equal(git(cwd, "rev-parse", `HEAD:${wrapper}`), legacyBlob);
5312+
assert.equal(captureFinalTargetCheckoutBinding(cwd, accepted, compacted.commit).status, "");
5313+
});
5314+
5315+
test("checkpoint plumbing preserves staged CRLF restorations alongside review edits", () => {
5316+
const cwd = gitPackageFixture({ check: 'node -e ""' });
5317+
const wrapper = "apps/android/gradlew.bat";
5318+
const wrapperPath = path.join(cwd, wrapper);
5319+
fs.mkdirSync(path.dirname(wrapperPath), { recursive: true });
5320+
fs.writeFileSync(path.join(cwd, ".gitattributes"), "*.bat text=auto eol=lf\n");
5321+
fs.writeFileSync(wrapperPath, "@echo off\r\nexit /b 0\r\n");
5322+
git(cwd, "add", ".");
5323+
const legacyBlob = git(cwd, "hash-object", "-w", "--no-filters", wrapper);
5324+
git(cwd, "update-index", "--cacheinfo", `100644,${legacyBlob},${wrapper}`);
5325+
git(cwd, "commit", "-m", "legacy CRLF base");
5326+
const base = git(cwd, "rev-parse", "HEAD");
5327+
const identity = {
5328+
name: "clawsweeper",
5329+
email: "274271284+clawsweeper[bot]@users.noreply.github.com",
5330+
};
5331+
fs.writeFileSync(path.join(cwd, "repair.txt"), "initial repair\n");
5332+
createTargetCheckpointWithPlumbing({ cwd, messages: ["initial repair"], identity });
5333+
git(cwd, "add", "--renormalize", "--", wrapper);
5334+
git(cwd, "commit", "-m", "simulate previously normalized wrapper");
5335+
5336+
git(cwd, "restore", `--source=${base}`, "--staged", "--worktree", "--", wrapper);
5337+
fs.writeFileSync(path.join(cwd, "repair.txt"), "review-corrected repair\n");
5338+
5339+
const corrected = createTargetCheckpointWithPlumbing({
5340+
cwd,
5341+
messages: ["review repair and restore unrelated wrapper"],
5342+
identity,
5343+
});
5344+
5345+
assert.equal(corrected.status, "committed");
5346+
assert.equal(git(cwd, "rev-parse", `HEAD:${wrapper}`), legacyBlob);
5347+
assert.equal(git(cwd, "show", "HEAD:repair.txt"), "review-corrected repair");
5348+
assert.equal(git(cwd, "status", "--porcelain"), "");
5349+
});
5350+
5351+
test("checkpoint plumbing rejects a staged blob that disagrees with unchanged worktree", () => {
5352+
const sources = process.platform === "win32" ? ["source.txt"] : ["source.txt", ":(exclude)*"];
5353+
for (const source of sources) {
5354+
const cwd = gitPackageFixture({ check: 'node -e ""' });
5355+
fs.writeFileSync(path.join(cwd, source), "validated content\n");
5356+
git(cwd, "add", ".");
5357+
git(cwd, "commit", "-m", "validated base");
5358+
const previousHead = git(cwd, "rev-parse", "HEAD");
5359+
const poisonedBlob = execFileSync("git", ["hash-object", "-w", "--stdin"], {
5360+
cwd,
5361+
input: "poisoned staged content\n",
5362+
encoding: "utf8",
5363+
}).trim();
5364+
git(cwd, "update-index", "--cacheinfo", `100644,${poisonedBlob},${source}`);
5365+
5366+
assert.throws(
5367+
() =>
5368+
createTargetCheckpointWithPlumbing({
5369+
cwd,
5370+
messages: ["reject poisoned index"],
5371+
identity: {
5372+
name: "clawsweeper",
5373+
email: "274271284+clawsweeper[bot]@users.noreply.github.com",
5374+
},
5375+
}),
5376+
/target index differs from unchanged worktree content/,
5377+
);
5378+
assert.equal(git(cwd, "rev-parse", "HEAD"), previousHead);
5379+
}
5380+
});
5381+
52605382
test("checkpoint plumbing supports replacing a tracked file with a directory", () => {
52615383
const cwd = gitPackageFixture({ check: 'node -e ""' });
52625384
fs.writeFileSync(path.join(cwd, "shape"), "file\n");

0 commit comments

Comments
 (0)