Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to strip trailing CR #344

Merged
merged 6 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 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).
Expand Down Expand Up @@ -81,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 diffing UNIX text files against Windows text files.

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

Expand Down
5 changes: 5 additions & 0 deletions src/diff/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import {generateOptions} from '../util/params';

export const lineDiff = new Diff();
lineDiff.tokenize = function(value) {
if(this.options.stripTrailingCr) {
// remove one \r before \n to match GNU diff's --strip-trailing-cr behavior
value = value.replace(/\r\n/g, '\n');
}

let retLines = [],
linesAndNewlines = value.split(/(\n|\r\n)/);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably just be value.split("\n") after this change, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well only if stripTrailingCr===true, to not be breaking. But even in that case, I'm not sure I understand the benefit?

Copy link
Collaborator

@ExplodingCabbage ExplodingCabbage Dec 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I was getting confused by #275 (comment). After reading that issue, but not testing anything to confirm, I had wrongly believed that the \n appearing first in the pipe-separated list of alternatives \n|\r\n/ meant that it would always match in preference to \r\n, making the entire regex equivalent to just /(\n)/, and that the logic using that regex had thus always been fundamentally broken. I thus thought that with my comment above I was just proposing cleaning up some misleading code that never really worked.

But some quick experimentation suggests that I'm wrong, at least in Node, Chrome, and Firefox!

> "foo\r\nbar\r\nbaz\r\n".split(/(\n|\r\n)/)
[
  'foo', '\r\n',
  'bar', '\r\n',
  'baz', '\r\n',
  ''
]

I'm not sure, then, why @cctakaso thought, in #275, that reordering the \r\n and \n in the regex would fix anything. Is it possible there is some JavaScript environment out there, that @cctakaso was using, with a regex engine where the order of alternatives really does affect the result in the way that issue suggests? I would've thought this would be standardised and all implementations would follow the standard, but tbh I don't want to spend hours unpicking the meaning of the ECMAScript 3 standard where regexes got added to JavaScript to confirm that the behaviour here has always been unambiguously specified!

My new suggestion, then, is that I'd be in favour of reversing the order as @cctakaso suggested, to /(\r\n|\n)/, just to make absolutely sure there's no ambiguity and avoid any need to research this - even though I'm now like 85% sure the order doesn't matter and this won't actually have any effect!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the problem #275 presents will be fixed by the option, but the solution they suggested wouldn't change much since the array that the regex produces would still contain all new lines as separate elements, and '\n' !== '\r\n'.
I think there's just a misunderstanding and we shouldn't change the code for it.


Expand Down
6 changes: 6 additions & 0 deletions test/diff/line.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}
]);
});
});
39 changes: 39 additions & 0 deletions test/patch/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down