From 28511619b5195a582175207efefda21a27a9ef76 Mon Sep 17 00:00:00 2001 From: Oscar Busk Date: Sat, 5 Feb 2022 03:20:59 +0100 Subject: [PATCH 1/6] Basic option for ignoring CR when doing linediff --- README.md | 2 ++ src/diff/line.js | 5 +++++ test/diff/line.js | 6 ++++++ 3 files changed, 13 insertions(+) diff --git a/README.md b/README.md index 986acf9f5..197d311c9 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ npm install diff --save Options * `ignoreWhitespace`: `true` to ignore leading and trailing whitespace. This is the same as `diffTrimmedLines` + * `stripTrailingCr`: `true` to remove all trailing CR (`\r`) characters before perfoming the diff. + This helps to get a useful diff when comparing files from UNIX/Windows respectively. * `newlineIsToken`: `true` to treat newline characters as separate tokens. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of `diffLines` and `diffLines` is better suited for patches and other computer friendly output. Returns a list of [change objects](#change-objects). diff --git a/src/diff/line.js b/src/diff/line.js index f9b4f4993..087729d1f 100644 --- a/src/diff/line.js +++ b/src/diff/line.js @@ -3,6 +3,11 @@ import {generateOptions} from '../util/params'; export const lineDiff = new Diff(); lineDiff.tokenize = function(value) { + if(this.options.stripTrailingCr) { + // remove all CR (\r) characters from the input string + value = value.replace(/\r+\n/g, '\n'); + } + let retLines = [], linesAndNewlines = value.split(/(\n|\r\n)/); diff --git a/test/diff/line.js b/test/diff/line.js index 6bc1e1832..d38095f42 100644 --- a/test/diff/line.js +++ b/test/diff/line.js @@ -106,4 +106,10 @@ describe('diff/line', function() { {value: '\nhello', count: 2, added: true, removed: undefined} ]); }); + + describe('Strip trailing CR', function() { + expect(diffLines('line\nline', 'line\r\nline', {stripTrailingCr: true})).to.eql([ + {value: 'line\nline', count: 2} + ]); + }); }); From f1b1feb4926c12527dfc9f6ebaa630ec5a42b71b Mon Sep 17 00:00:00 2001 From: Oscar Busk Date: Sat, 5 Feb 2022 03:44:22 +0100 Subject: [PATCH 2/6] Add tests for supporting `stripTrailingCr` in createPatch --- test/patch/create.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/test/patch/create.js b/test/patch/create.js index 8b8c05b59..2df4bb137 100644 --- a/test/patch/create.js +++ b/test/patch/create.js @@ -705,6 +705,45 @@ describe('patch/create', function() { }); }); + describe('stripTrailingCr', function() { + it('stripTrailingCr: false', function() { + const expectedResult = + '===================================================================\n' + + '--- foo\n' + + '+++ bar\n' + + '@@ -1,2 +1,2 @@\n' + + '\-line\n' + + '\+line\r\n' + + '\ line\n' + + '\\ No newline at end of file\n'; + expect(createTwoFilesPatch( + 'foo', + 'bar', + 'line\nline', + 'line\r\nline', + undefined, + undefined, + {stripTrailingCr: false} + )).to.equal(expectedResult); + }); + + it('stripTrailingCr: true', function() { + const expectedResult = + '===================================================================\n' + + '--- foo\n' + + '+++ bar\n'; + expect(createTwoFilesPatch( + 'foo', + 'bar', + 'line\nline', + 'line\r\nline', + undefined, + undefined, + {stripTrailingCr: true} + )).to.equal(expectedResult); + }); + }); + describe('#structuredPatch', function() { it('should handle files with the last line changed', function() { const res = structuredPatch( From 9b1bd6bd5480af19e818b719dcd00c679669223e Mon Sep 17 00:00:00 2001 From: Oscar Busk Date: Sat, 5 Feb 2022 03:46:11 +0100 Subject: [PATCH 3/6] Update documentation for createPatch --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 197d311c9..d781b25b2 100644 --- a/README.md +++ b/README.md @@ -83,6 +83,8 @@ npm install diff --save - `context` describes how many lines of context should be included. - `ignoreWhitespace`: `true` to ignore leading and trailing whitespace. - `newlineIsToken`: `true` to treat newline characters as separate tokens. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of `diffLines` and `diffLines` is better suited for patches and other computer friendly output. + - `stripTrailingCr`: `true` to remove all trailing CR (`\r`) characters before perfoming the diff. + This helps to get a useful diff when comparing files from UNIX/Windows respectively. * `Diff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch. From fbbac24a993ecbf79990c994031e46d7e9c26d04 Mon Sep 17 00:00:00 2001 From: Oscar Busk Date: Fri, 22 Dec 2023 19:09:57 +0100 Subject: [PATCH 4/6] Improve english of README changes --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d781b25b2..67baba9a2 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ npm install diff --save Options * `ignoreWhitespace`: `true` to ignore leading and trailing whitespace. This is the same as `diffTrimmedLines` * `stripTrailingCr`: `true` to remove all trailing CR (`\r`) characters before perfoming the diff. - This helps to get a useful diff when comparing files from UNIX/Windows respectively. + This helps to get a useful diff when diffing UNIX text files against Windows text files. * `newlineIsToken`: `true` to treat newline characters as separate tokens. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of `diffLines` and `diffLines` is better suited for patches and other computer friendly output. Returns a list of [change objects](#change-objects). From 5a7eb9237286e3a182f8db5a2b37400ef1b18d67 Mon Sep 17 00:00:00 2001 From: Oscar Busk Date: Fri, 22 Dec 2023 19:18:43 +0100 Subject: [PATCH 5/6] Only remove one \r to match GNU diff behaviour --- src/diff/line.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/diff/line.js b/src/diff/line.js index 087729d1f..4ab01b2b3 100644 --- a/src/diff/line.js +++ b/src/diff/line.js @@ -4,8 +4,8 @@ import {generateOptions} from '../util/params'; export const lineDiff = new Diff(); lineDiff.tokenize = function(value) { if(this.options.stripTrailingCr) { - // remove all CR (\r) characters from the input string - value = value.replace(/\r+\n/g, '\n'); + // remove one \r before \n to match GNU diff's --strip-trailing-cr behavior + value = value.replace(/\r\n/g, '\n'); } let retLines = [], From 3de1001eb255cd036f657192632e126212b296f0 Mon Sep 17 00:00:00 2001 From: Oscar Busk Date: Fri, 22 Dec 2023 19:23:01 +0100 Subject: [PATCH 6/6] Fix the same bad english in other place in README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 67baba9a2..60eacb188 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ npm install diff --save - `ignoreWhitespace`: `true` to ignore leading and trailing whitespace. - `newlineIsToken`: `true` to treat newline characters as separate tokens. This allows for changes to the newline structure to occur independently of the line content and to be treated as such. In general this is the more human friendly form of `diffLines` and `diffLines` is better suited for patches and other computer friendly output. - `stripTrailingCr`: `true` to remove all trailing CR (`\r`) characters before perfoming the diff. - This helps to get a useful diff when comparing files from UNIX/Windows respectively. + This helps to get a useful diff when diffing UNIX text files against Windows text files. * `Diff.createPatch(fileName, oldStr, newStr, oldHeader, newHeader)` - creates a unified diff patch.