Repository navigation
Description
Prerequisites
- I have searched for duplicate or closed issues
- I have read the contributing guidelines
Describe the issue
parsePatchFiles (and processFile / parseDiffFromFile) runs realignChangeContentBySimilarity on every parsed file. When a change block's deletion and addition counts differ, it re-splits the block so the most similar lines pair up. That helps a split view, but the unified view renders each block's deletions then its additions, so after the re-split the deleted lines sit in the middle of the added ones.
A common case is wrapping a function body in a loop or try: the re-indented body lines are the best match for the deleted originals, so the unified view shows the new loop header, then the old body, then the new body.
Expected: a unified view shows a change the way the patch states it, all deletions then all additions, as git, GitHub, jsdiff's structuredPatch and @pierre/diffs 1.2.12 do.
Actual (1.3.0 through 1.6.1): the change block -4 +12 comes back as -0 +3, -4 +4, -0 +5, and the unified view reads + + + - - - - + + + + + + + + +. iterateOverDiff with diffStyle: 'unified' emits the rows in that order, so this is what FileDiff draws.
There is no option to turn this off. #1142 (fixing #1139) narrowed the blank-line slide but left similarity pairing unchanged, and #781 asks for the split-view counterpart (no pairing at all in split). We now rejoin the pieces after parsing in our own viewer, which undoes work the parser just did.
Ask: an option to keep change blocks as the patch states them (for example on the parse options), or applying the realignment only when rendering a split view.
Reproduction
Pure parser behavior, no DOM, so a Node script is the full reduction. The patch is git diff output.
// npm i @pierre/[email protected] && node repro.mjs (compare with @1.2.12)
import { parsePatchFiles } from '@pierre/diffs';
const patch = `diff --git a/load.ts b/load.ts
--- a/load.ts
+++ b/load.ts
@@ -1,6 +1,14 @@
export function load(path: string) {
- const text = readFileSync(path, 'utf8');
- const data = JSON.parse(text);
- validate(data);
- return data;
+ let lastError: unknown;
+ for (let attempt = 0; attempt < 3; attempt++) {
+ try {
+ const text = readFileSync(path, 'utf8');
+ const data = JSON.parse(text);
+ validate(data);
+ return data;
+ } catch (err) {
+ lastError = err;
+ }
+ }
+ throw lastError;
}
`;
const [file] = parsePatchFiles(patch)[0].files;
const { hunkContent } = file.hunks[0];
console.log(
'blocks:',
hunkContent.map((b) => (b.type === 'context' ? `context ${b.lines}` : `-${b.deletions} +${b.additions}`)).join(', '),
);
// A unified view shows each change block's deletions, then its additions.
for (const b of hunkContent) {
if (b.type === 'context') {
for (let i = 0; i < b.lines; i++) console.log(' ' + file.additionLines[b.additionLineIndex + i].trimEnd());
continue;
}
for (let i = 0; i < b.deletions; i++) console.log('-' + file.deletionLines[b.deletionLineIndex + i].trimEnd());
for (let i = 0; i < b.additions; i++) console.log('+' + file.additionLines[b.additionLineIndex + i].trimEnd());
}1.2.12 (matches the patch):
blocks: context 1, -4 +12, context 1
export function load(path: string) {
- const text = readFileSync(path, 'utf8');
- const data = JSON.parse(text);
- validate(data);
- return data;
+ let lastError: unknown;
+ for (let attempt = 0; attempt < 3; attempt++) {
+ try {
+ const text = readFileSync(path, 'utf8');
+ const data = JSON.parse(text);
+ validate(data);
+ return data;
+ } catch (err) {
+ lastError = err;
+ }
+ }
+ throw lastError;
}1.6.1 (1.3.0 and 1.5.2 print the same):
blocks: context 1, -0 +3, -4 +4, -0 +5, context 1
export function load(path: string) {
+ let lastError: unknown;
+ for (let attempt = 0; attempt < 3; attempt++) {
+ try {
- const text = readFileSync(path, 'utf8');
- const data = JSON.parse(text);
- validate(data);
- return data;
+ const text = readFileSync(path, 'utf8');
+ const data = JSON.parse(text);
+ validate(data);
+ return data;
+ } catch (err) {
+ lastError = err;
+ }
+ }
+ throw lastError;
}What browser(s) are you seeing the problem on?
Other
What version of @pierre/diffs are you using?
1.6.1 (present from 1.3.0; 1.2.12 keeps the patch's order)
This was written by Claude Code on behalf of max-sixty
Activity
Read a rewrite's deleted lines above its added ones in a diff (#3951)