Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
3652181
Fix assorted trivial capitalisation typos (#410)
ExplodingCabbage Dec 14, 2023
ff14775
Upgrade packages that Dependabot has open PRs about (#415)
ExplodingCabbage Dec 15, 2023
8ca7068
Bump karma from 5.1.1 to 6.3.16 (#357)
dependabot[bot] Dec 15, 2023
0e7a0cb
yarn eslint . --fix (#421)
ExplodingCabbage Dec 15, 2023
bbb2359
Update package.json version to 5.1.0 (#422)
ExplodingCabbage Dec 15, 2023
902e7da
Bump more dependencies to please Dependabot (#425)
ExplodingCabbage Dec 15, 2023
e67c2a0
Bump qs from 6.7.0 to 6.11.0 (#426)
dependabot[bot] Dec 15, 2023
a3759a1
Move demo link to the top of the README (#370)
waldyrious Dec 15, 2023
021c973
chore: update license file (#331)
eddiemonge Dec 16, 2023
87dedb6
readme: add links to section: change objects (#316)
milahu Dec 18, 2023
e0e960a
Document diffJson() options (#332)
cincodenada Dec 18, 2023
976d60d
Fix `exports` field in `package.json` (#351)
fisker Dec 18, 2023
1387be9
Remove index.html from master (#429)
ExplodingCabbage Dec 19, 2023
96f5998
Update comment in index.js to reflect JsDiff->Diff rename in 5.0.0 (#…
ExplodingCabbage Dec 19, 2023
b4d7491
Document in a comment in web_example.html that you need to run a buil…
ExplodingCabbage Dec 19, 2023
f2596ea
Fix a typo (#433)
ExplodingCabbage Dec 20, 2023
6183041
Default value of line delimiters when a patch is applied (#228) (#393)
Cinedin Dec 20, 2023
2e08d01
Fix bug that leads to worse time complexity and cripplingly slow perf…
ExplodingCabbage Dec 23, 2023
5897c8f
Update CONTRIBUTING.md to use yarn (#441)
ExplodingCabbage Dec 23, 2023
a19bded
Fix typo / grammar error in CONTRIBUTING.md (#442)
ExplodingCabbage Dec 23, 2023
14bfcb6
Update release-notes.md with content on npm that never got pushed to …
ExplodingCabbage Dec 23, 2023
8e51326
Write release notes for PRs already merged to master (#444)
ExplodingCabbage Dec 23, 2023
573b7af
Option to strip trailing CR (#344)
oBusk Dec 27, 2023
e35c347
Add release notes for @oBusk's PR #344 (#445)
ExplodingCabbage Dec 27, 2023
a4eac49
Stop treating stuff like vertical tabs as line breaks when dealing wi…
ExplodingCabbage Dec 27, 2023
a2dc5ec
Add test showing patch from bug #177 is handled correctly now (#447)
ExplodingCabbage Dec 27, 2023
bf5ec4a
Flip core algorithm so everything is no longer the mirror image of My…
ExplodingCabbage Dec 27, 2023
97c676d
Merge remote-tracking branch 'origin/master' into 6.0.0-staging
ExplodingCabbage Dec 27, 2023
fe261ae
Prefer to order deletions before insertions when the edit cost is the…
ExplodingCabbage Dec 27, 2023
b1b2035
Speed up algorithm by not considering diagonals that take us off the …
ExplodingCabbage Dec 29, 2023
8bd13d6
Consistently capitalize "jsdiff" in all-lowercase in docs (#449)
ExplodingCabbage Dec 29, 2023
56c6a8a
Expose `formatPatch` on `diff` object and document (#451)
ExplodingCabbage Dec 29, 2023
6a574cc
Merge branch 'master' into 6.0.0-staging
ExplodingCabbage Dec 29, 2023
8365367
Add function to reverse a patch (#450)
ExplodingCabbage Jan 2, 2024
3351c82
Merge remote-tracking branch 'origin/master' into 6.0.0-staging
ExplodingCabbage Jan 2, 2024
3a99253
Always set `added` and `removed` to `true` or `false`, rather than le…
ExplodingCabbage Jan 2, 2024
a98b974
Flesh out the README a bit and fix some errors and omissions (#458)
ExplodingCabbage Jan 4, 2024
c8c5132
Merge branch 'master' into 6.0.0-staging
ExplodingCabbage Jan 4, 2024
c6498e3
Document that applyPatch can return false (#459)
ExplodingCabbage Jan 4, 2024
7eacf2a
Merge branch 'master' into 6.0.0-staging
ExplodingCabbage Jan 5, 2024
b3aab68
Handle case where the user explicitly passes `maxEditLength: 0` the w…
ExplodingCabbage Jan 8, 2024
ea983ba
Fix more gaps in the docs (#466)
ExplodingCabbage Jan 8, 2024
12e092d
Merge remote-tracking branch 'origin/master' into 6.0.0-staging
ExplodingCabbage Jan 8, 2024
e6c45b0
Add a oneChangePerToken option (#460)
ExplodingCabbage Jan 8, 2024
1e79116
Fix order of arguments to .equals and comparator (#467)
ExplodingCabbage Jan 8, 2024
25a14af
Migrate to DABH's fork of colors (#469)
ExplodingCabbage Jan 8, 2024
ca8718c
Bump follow-redirects from 1.14.8 to 1.15.4 (#470)
dependabot[bot] Jan 9, 2024
1c7514c
Fix mistake in README (#471)
ExplodingCabbage Jan 10, 2024
707fccc
Add note to README about setting `context` to Infinity or MAX_SAFE_IN…
ExplodingCabbage Jan 10, 2024
1f1ec96
Replace broken link to Myers's paper in the README with a working one…
ExplodingCabbage Jan 11, 2024
533893d
Add `timeout` option (#478)
ExplodingCabbage Jan 26, 2024
b5d1cfa
Modify node_example.js to support showing added/deleted spaces (#479)
ExplodingCabbage Jan 26, 2024
06a669b
Merge branch 'master' into 6.0.0-staging
ExplodingCabbage Jan 29, 2024
4abb5f3
Support max edit length in patch creation functions (#480)
ExplodingCabbage Jan 29, 2024
dfc6fe4
Add examples to docs of creating and applying patches (importantly in…
ExplodingCabbage Jan 29, 2024
a2f726a
Add myself to the list of maintainers (#482)
ExplodingCabbage Feb 12, 2024
370a9df
5.2.0 release (#483)
ExplodingCabbage Feb 12, 2024
ad635b1
Add a reminder to the releasing docs to update the gh-pages site afte…
ExplodingCabbage Feb 13, 2024
b9f56d3
Merge branch 'master' into 6.0.0-staging
ExplodingCabbage Feb 13, 2024
fc2e36d
Merge pull request #446 from kpdecker/6.0.0-staging
ExplodingCabbage Feb 13, 2024
5f9cd41
Sort out behaviour of newlineIsToken and ignoreWhitespace (#486)
ExplodingCabbage Feb 15, 2024
e83674b
Remove failing test (#487)
ExplodingCabbage Feb 15, 2024
a3e4812
Add some more exhaustive tests based on @Mingun's work (#488)
ExplodingCabbage Feb 15, 2024
13d9749
Fix the weird function signature of async callbacks (#490)
ExplodingCabbage Feb 15, 2024
bf45b03
Fix race conditions involving this.options being overwritten during e…
ExplodingCabbage Feb 15, 2024
a73b771
Add further assertion to test, as suggested by Mingun (#491)
ExplodingCabbage Feb 16, 2024
f38e47d
Support `Object.create(null)` in JSON diffing (#493)
danbeam Feb 19, 2024
3da78c2
Simplify tokenization logic in diffWords (#494)
ExplodingCabbage Feb 19, 2024
7a73dc1
Bump ip from 1.1.5 to 1.1.9 (#495)
dependabot[bot] Feb 21, 2024
f925d4c
Fix trivial typo ("threat"->"treat" in test name) (#498)
ExplodingCabbage Mar 5, 2024
045c346
Add test of how diffWordsWithSpace handles Windows-style newlines (#499)
ExplodingCabbage Mar 6, 2024
490f5ab
Make diffChars diff Unicode code points instead of UTF-16 code units …
ExplodingCabbage Mar 8, 2024
59161e0
Fix diffWords handling of whitespace (#497)
ExplodingCabbage Mar 11, 2024
f4f11df
Bump follow-redirects from 1.15.4 to 1.15.6 (#502)
dependabot[bot] Mar 18, 2024
c9bc8e3
Run: (#503)
ExplodingCabbage Mar 19, 2024
16a060e
Purge inactive/broken Travis and Sauce Labs integrations (#504)
ExplodingCabbage Mar 19, 2024
84b5c9e
Upgrade some more dev dependencies (#505)
ExplodingCabbage Mar 20, 2024
b90a3eb
Bump Mocha one major version (#507)
ExplodingCabbage Mar 20, 2024
64f587c
Always enable "strict mode" in parsePatch (#508)
ExplodingCabbage Mar 20, 2024
53339e2
Remove unused Grunt `version` task; flesh out docs on how to do a rel…
ExplodingCabbage Mar 20, 2024
eb73eb8
Remove style.css from master branch (it's part of the gh-pages site, …
ExplodingCabbage Mar 20, 2024
046b5d3
Bump webpack-dev-middleware from 7.0.0 to 7.1.1 (#511)
dependabot[bot] Mar 22, 2024
4ebc4bf
Bump express from 4.18.3 to 4.19.2 (#512)
dependabot[bot] Apr 28, 2024
c8a9cc5
Remove linedelimiters, improve handling of Windows vs Unix line endin…
ExplodingCabbage Jun 7, 2024
9bb34dc
Support `callback` in patch functions, not just diffFoo functions (#521)
ExplodingCabbage Jun 7, 2024
0126325
Add tests of existing broken parsePatch behaviour, to fix before next…
ExplodingCabbage Jun 14, 2024
5ecab06
Bump braces from 3.0.2 to 3.0.3 (#526)
dependabot[bot] Jun 18, 2024
323e8bb
Fix parse patch bug (#529)
ExplodingCabbage Jun 24, 2024
896c982
Fix release notes typo
ExplodingCabbage Jun 24, 2024
0e7c20c
Add ignoreNewlineAtEof (#530)
ExplodingCabbage Jun 24, 2024
353d117
Rewrite applyPatch (#533)
ExplodingCabbage Jul 26, 2024
939bb45
Fix handling of EOF in createPatch (#535)
ExplodingCabbage Jul 29, 2024
244df82
Fix more logic around newlines at EOF - this time stuff I recently br…
ExplodingCabbage Jul 29, 2024
4f0430a
Add Intl.Segmenter support (#539)
ExplodingCabbage Aug 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix handling of EOF in createPatch (kpdecker#535)
* Add (failing) test case from kpdecker#531

* Fix test I just added (the test itself, not the jsdiff behaviour)

* Fix the new test (but make an old one fail as a side effect)

* Stop allowing newlineIsToken to be passed to patch-generation functions (... and causing them to generate patches that can't be applied)

* Remove impossible case to placate the coverage checker

* Add release notes
  • Loading branch information
ExplodingCabbage authored Jul 29, 2024
commit 939bb457cde09af47d4286527f0b59b67b62748a
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ Broadly, jsdiff's diff functions all take an old text and a new text and perform
- `context` describes how many lines of context should be included. You can set this to `Number.MAX_SAFE_INTEGER` or `Infinity` to include the entire file content in one hunk.
- `ignoreWhitespace`: Same as in `diffLines`. Defaults to `false`.
- `stripTrailingCr`: Same as in `diffLines`. Defaults to `false`.
- `newlineIsToken`: Same as in `diffLines`. Defaults to `false`.

* `Diff.createPatch(fileName, oldStr, newStr[, oldHeader[, newHeader[, options]]])` - creates a unified diff patch.

Expand Down
2 changes: 2 additions & 0 deletions release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
* The `fuzzFactor` now indicates the maximum [*Levenshtein* distance](https://en.wikipedia.org/wiki/Levenshtein_distance) that there can be between the context shown in a hunk and the actual file content at a location where we try to apply the hunk. (Previously, it represented a maximum [*Hamming* distance](https://en.wikipedia.org/wiki/Hamming_distance), meaning that a single insertion or deletion in the source file could stop a hunk from applying even with a high `fuzzFactor`.)
* A hunk containing a deletion can now only be applied in a context where the line to be deleted actually appears verbatim. (Previously, as long as enough context lines in the hunk matched, `applyPatch` would apply the hunk anyway and delete a completely different line.)
* The context line immediately before and immediately after an insertion must match exactly between the hunk and the file for a hunk to apply. (Previously this was not required.)
- [#535](https://github.com/kpdecker/jsdiff/pull/535) **A bug in patch generation functions is now fixed** that would sometimes previously cause `\ No newline at end of file` to appear in the wrong place in the generated patch, resulting in the patch being invalid.
- [#535](https://github.com/kpdecker/jsdiff/pull/535) **Passing `newlineIsToken: true` to *patch*-generation functions is no longer allowed.** (Passing it to `diffLines` is still supported - it's only functions like `createPatch` where passing `newlineIsToken` is now an error.) Allowing it to be passed never really made sense, since in cases where the option had any effect on the output at all, the effect tended to be causing a garbled patch to be created that couldn't actually be applied to the source file.

## v5.2.0

Expand Down
49 changes: 34 additions & 15 deletions src/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHea
if (typeof options.context === 'undefined') {
options.context = 4;
}
if (options.newlineIsToken) {
throw new Error('newlineIsToken may not be used with patch-generation functions, only with diffing functions');
}

if (!options.callback) {
return diffLinesResultToPatch(diffLines(oldStr, newStr, options));
Expand All @@ -29,6 +32,9 @@ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHea
}

function diffLinesResultToPatch(diff) {
// STEP 1: Build up the patch with no "\ No newline at end of file" lines and with the arrays
// of lines containing trailing newline characters. We'll tidy up later...

if(!diff) {
return;
}
Expand All @@ -44,7 +50,7 @@ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHea
oldLine = 1, newLine = 1;
for (let i = 0; i < diff.length; i++) {
const current = diff[i],
lines = current.lines || current.value.replace(/\n$/, '').split('\n');
lines = current.lines || splitLines(current.value);
current.lines = lines;

if (current.added || current.removed) {
Expand Down Expand Up @@ -91,20 +97,6 @@ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHea
newLines: (newLine - newRangeStart + contextSize),
lines: curRange
};
if (i >= diff.length - 2 && lines.length <= options.context) {
// EOF is inside this hunk
let oldEOFNewline = ((/\n$/).test(oldStr));
let newEOFNewline = ((/\n$/).test(newStr));
let noNlBeforeAdds = lines.length == 0 && curRange.length > hunk.oldLines;
if (!oldEOFNewline && noNlBeforeAdds && oldStr.length > 0) {
// special case: old has no eol and no trailing context; no-nl can end up before adds
// however, if the old file is empty, do not output the no-nl line
curRange.splice(hunk.oldLines, 0, '\\ No newline at end of file');
}
if ((!oldEOFNewline && !noNlBeforeAdds) || !newEOFNewline) {
curRange.push('\\ No newline at end of file');
}
}
hunks.push(hunk);

oldRangeStart = 0;
Expand All @@ -117,6 +109,19 @@ export function structuredPatch(oldFileName, newFileName, oldStr, newStr, oldHea
}
}

// Step 2: eliminate the trailing `\n` from each line of each hunk, and, where needed, add
// "\ No newline at end of file".
for (const hunk of hunks) {
for (let i = 0; i < hunk.lines.length; i++) {
if (hunk.lines[i].endsWith('\n')) {
hunk.lines[i] = hunk.lines[i].slice(0, -1);
} else {
hunk.lines.splice(i + 1, 0, '\\ No newline at end of file');
i++; // Skip the line we just added, then continue iterating
}
}
}

return {
oldFileName: oldFileName, newFileName: newFileName,
oldHeader: oldHeader, newHeader: newHeader,
Expand Down Expand Up @@ -197,3 +202,17 @@ export function createTwoFilesPatch(oldFileName, newFileName, oldStr, newStr, ol
export function createPatch(fileName, oldStr, newStr, oldHeader, newHeader, options) {
return createTwoFilesPatch(fileName, fileName, oldStr, newStr, oldHeader, newHeader, options);
}

/**
* Split `text` into an array of lines, including the trailing newline character (where present)
*/
function splitLines(text) {
const hasTrailingNl = text.endsWith('\n');
const result = text.split('\n').map(line => line + '\n');
if (hasTrailingNl) {
result.pop();
} else {
result.push(result.pop().slice(0, -1));
}
return result;
}
76 changes: 38 additions & 38 deletions test/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,39 @@ describe('patch/create', function() {
+ '\\ No newline at end of file\n');
});

it('should get the "No newline" position right in the case from https://github.com/kpdecker/jsdiff/issues/531', function() {
const oldContent = '1st line.\n2nd line.\n3rd line.';
const newContent = 'Z11 thing.\nA New thing.\n2nd line.\nNEW LINE.\n3rd line.\n\nSOMETHING ELSE.';

const diff = createPatch(
'a.txt',
oldContent,
newContent,
undefined,
undefined,
{ context: 3 },
);

expect(diff).to.equal(
'Index: a.txt\n'
+ '===================================================================\n'
+ '--- a.txt\n'
+ '+++ a.txt\n'
+ '@@ -1,3 +1,7 @@\n'
+ '-1st line.\n'
+ '+Z11 thing.\n'
+ '+A New thing.\n'
+ ' 2nd line.\n'
+ '-3rd line.\n'
+ '\\ No newline at end of file\n'
+ '+NEW LINE.\n'
+ '+3rd line.\n'
+ '+\n'
+ '+SOMETHING ELSE.\n'
+ '\\ No newline at end of file\n'
);
});

it('should output "no newline" at end of file message on context missing nl', function() {
expect(createPatch('test', 'line11\nline2\nline3\nline4', 'line1\nline2\nline3\nline4', 'header1', 'header2')).to.equal(
'Index: test\n'
Expand Down Expand Up @@ -669,47 +702,14 @@ describe('patch/create', function() {
});

describe('newlineIsToken', function() {
it('newlineIsToken: false', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n'
+ '@@ -1,2 +1,2 @@\n'

// Diff is shown as entire row, even though text is unchanged
+ '-line\n'
+ '+line\r\n'

+ ' line\n'
+ '\\ No newline at end of file\n';

const diffResult = createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: false});
expect(diffResult).to.equal(expectedResult);
});

it('newlineIsToken: true', function() {
const expectedResult =
'Index: testFileName\n'
+ '===================================================================\n'
+ '--- testFileName\n'
+ '+++ testFileName\n'
+ '@@ -1,3 +1,3 @@\n'
+ ' line\n'

// Newline change is shown as a single diff
+ '-\n'
+ '+\r\n'

+ ' line\n'
+ '\\ No newline at end of file\n';

const diffResult = createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: true});
expect(diffResult).to.equal(expectedResult);
// See https://github.com/kpdecker/jsdiff/pull/345#issuecomment-2255886105
it("isn't allowed any more, since the patches produced were nonsense", function() {
expect(() => {
createPatch('testFileName', 'line\nline', 'line\r\nline', undefined, undefined, {newlineIsToken: true});
}).to['throw']('newlineIsToken may not be used with patch-generation functions, only with diffing functions');
});
});


it('takes an optional callback option', function(done) {
createPatch(
'test',
Expand Down