Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
git-diff: Add support for ignoreChangeWhitespace & ignoreWhitespace (#…
Browse files Browse the repository at this point in the history
…100)

* git-diff: Add support for ignoreChangeWhitespace & ignoreWhitespace
  • Loading branch information
utkarshgupta137 authored Nov 3, 2020
1 parent eb2e292 commit d2cb646
Show file tree
Hide file tree
Showing 13 changed files with 172 additions and 26 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,12 @@ text.

`options` - An optional object with the following keys:

* `ignoreEolWhitespace` - `true` to ignore any whitespace diffs at the end of
lines.
* `ignoreEolWhitespace` - `true` to ignore changes in whitespace at the end of lines.
* `ignoreWhitespaceChange` - `true` to ignore changes in amount of whitespace.
This ignores whitespace at line end, and considers all other sequences of
one or more whitespace characters to be equivalent.
* `ignoreWhitespace` - `true` to ignore whitespace when comparing lines.
This ignores differences even if one line has whitespace where the other line has none.
* `useIndex` - `true` to compare against the index version instead of the HEAD
version.

Expand Down
4 changes: 2 additions & 2 deletions spec/fixtures/file.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
first
second
third
second
third
2 changes: 1 addition & 1 deletion spec/fixtures/whitespace.git/config
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = false
precomposeunicode = true
Binary file modified spec/fixtures/whitespace.git/index
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion spec/fixtures/whitespace.git/refs/heads/master
Original file line number Diff line number Diff line change
@@ -1 +1 @@
de14a8e29deb59e93d6daa82b1c55413581017a1
5f6a1c992cfafadd97053fae380aef21a964621a
144 changes: 138 additions & 6 deletions spec/git-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,14 +738,80 @@ describe('git', () => {
expect(diffs).toBeNull()
})

describe('ignoreEolWhitespace option', () => {
describe('ignoreSpaceAtEOL option', () => {
it('ignores eol of line whitespace changes', () => {
repo = git.open(path.join(__dirname, 'fixtures/whitespace.git'))

let diffs = repo.getLineDiffs('file.txt', 'first\r\nsecond\r\nthird\r\n', {ignoreEolWhitespace: false})
let diffs = repo.getLineDiffs('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreEolWhitespace: false})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', 'first\r\nsecond\r\nthird\r\n', {ignoreEolWhitespace: true})
diffs = repo.getLineDiffs('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreEolWhitespace: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffs('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreSpaceAtEOL: false})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreSpaceAtEOL: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffs('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceAtEOL: false})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceAtEOL: true})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceAtEOL: false})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceAtEOL: true})
expect(diffs.length).toBe(1)
})
})

describe('ignoreSpaceChange option', () => {
it('ignores whitespace changes', () => {
repo = git.open(path.join(__dirname, 'fixtures/whitespace.git'))

let diffs = repo.getLineDiffs('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreSpaceChange: false})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreSpaceChange: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffs('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceChange: false})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceChange: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffs('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceChange: false})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceChange: true})
expect(diffs.length).toBe(1)
})
})

describe('ignoreAllSpace option', () => {
it('ignores whitespace', () => {
repo = git.open(path.join(__dirname, 'fixtures/whitespace.git'))

let diffs = repo.getLineDiffs('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreAllSpace: false})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreAllSpace: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffs('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreAllSpace: false})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreAllSpace: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffs('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreAllSpace: false})
expect(diffs.length).toBe(1)

diffs = repo.getLineDiffs('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreAllSpace: true})
expect(diffs.length).toBe(0)
})
})
Expand Down Expand Up @@ -811,14 +877,80 @@ describe('git', () => {
expect(diffs).toBeNull()
})

describe('ignoreEolWhitespace option', () => {
describe('ignoreSpaceAtEOL option', () => {
it('ignores eol of line whitespace changes', () => {
repo = git.open(path.join(__dirname, 'fixtures/whitespace.git'))

let diffs = repo.getLineDiffDetails('file.txt', 'first\r\nsecond\r\nthird\r\n', {ignoreEolWhitespace: false})
let diffs = repo.getLineDiffDetails('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreEolWhitespace: false})
expect(diffs.length).toBe(6)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreEolWhitespace: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreSpaceAtEOL: false})
expect(diffs.length).toBe(6)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreSpaceAtEOL: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceAtEOL: false})
expect(diffs.length).toBe(6)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceAtEOL: true})
expect(diffs.length).toBe(4)

diffs = repo.getLineDiffDetails('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceAtEOL: false})
expect(diffs.length).toBe(6)

diffs = repo.getLineDiffDetails('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceAtEOL: true})
expect(diffs.length).toBe(6)
})
})

describe('ignoreSpaceChange option', () => {
it('ignores whitespace changes', () => {
repo = git.open(path.join(__dirname, 'fixtures/whitespace.git'))

let diffs = repo.getLineDiffDetails('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreSpaceChange: false})
expect(diffs.length).toBe(6)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreSpaceChange: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceChange: false})
expect(diffs.length).toBe(6)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceChange: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffDetails('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceChange: false})
expect(diffs.length).toBe(6)

diffs = repo.getLineDiffDetails('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreSpaceChange: true})
expect(diffs.length).toBe(2)
})
})

describe('ignoreAllSpace option', () => {
it('ignores whitespace', () => {
repo = git.open(path.join(__dirname, 'fixtures/whitespace.git'))

let diffs = repo.getLineDiffDetails('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreAllSpace: false})
expect(diffs.length).toBe(6)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n second\r\n\tthird\r\n', {ignoreAllSpace: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreAllSpace: false})
expect(diffs.length).toBe(6)

diffs = repo.getLineDiffDetails('file.txt', 'first\r\n \tsecond\r\n \tthird\r\n', {ignoreAllSpace: true})
expect(diffs.length).toBe(0)

diffs = repo.getLineDiffDetails('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreAllSpace: false})
expect(diffs.length).toBe(6)

diffs = repo.getLineDiffs('file.txt', 'first\r\nsecond\r\nthird\r\n', {ignoreEolWhitespace: true})
diffs = repo.getLineDiffDetails('file.txt', ' \tfirst\r\n \tsecond\r\n \tthird\r\n', {ignoreAllSpace: true})
expect(diffs.length).toBe(0)
})
})
Expand Down
38 changes: 24 additions & 14 deletions src/repository.cc
Original file line number Diff line number Diff line change
Expand Up @@ -791,16 +791,21 @@ NAN_METHOD(Repository::GetLineDiffs) {
std::vector<git_diff_hunk> ranges;
git_diff_options options = CreateDefaultGitDiffOptions();

// Set GIT_DIFF_IGNORE_WHITESPACE_EOL when ignoreEolWhitespace: true
if (info.Length() >= 3) {
Local<Object> optionsArg(Local<Object>::Cast(info[2]));
Local<Value> ignoreEolWhitespace;
ignoreEolWhitespace = Nan::Get(
optionsArg,
Nan::New<String>("ignoreEolWhitespace").ToLocalChecked()).ToLocalChecked();

if (Nan::To<bool>(ignoreEolWhitespace).FromJust())
// Set GIT_DIFF_IGNORE_WHITESPACE when ignoreWhitespace: true
if (Nan::To<bool>(Nan::Get(optionsArg, Nan::New<String>("ignoreAllSpace").ToLocalChecked()).ToLocalChecked()).FromJust())
options.flags = GIT_DIFF_IGNORE_WHITESPACE;
// Set GIT_DIFF_IGNORE_WHITESPACE_CHANGE when ignoreWhitespaceChange: true
else if (Nan::To<bool>(Nan::Get(optionsArg, Nan::New<String>("ignoreSpaceChange").ToLocalChecked()).ToLocalChecked()).FromJust())
options.flags = GIT_DIFF_IGNORE_WHITESPACE_CHANGE;
// Set GIT_DIFF_IGNORE_WHITESPACE_EOL when ignoreEolWhitespace: true
else if (Nan::To<bool>(Nan::Get(optionsArg, Nan::New<String>("ignoreSpaceAtEOL").ToLocalChecked()).ToLocalChecked()).FromJust()
|| Nan::To<bool>(Nan::Get(optionsArg, Nan::New<String>("ignoreEolWhitespace").ToLocalChecked()).ToLocalChecked()).FromJust())
options.flags = GIT_DIFF_IGNORE_WHITESPACE_EOL;
// Set GIT_DIFF_NORMAL when none of the above are defined
else
options.flags = GIT_DIFF_NORMAL;
}

options.context_lines = 0;
Expand Down Expand Up @@ -869,16 +874,21 @@ NAN_METHOD(Repository::GetLineDiffDetails) {
std::vector<LineDiff> lineDiffs;
git_diff_options options = CreateDefaultGitDiffOptions();

// Set GIT_DIFF_IGNORE_WHITESPACE_EOL when ignoreEolWhitespace: true
if (info.Length() >= 3) {
Local<Object> optionsArg(Local<Object>::Cast(info[2]));
Local<Value> ignoreEolWhitespace;
ignoreEolWhitespace = Nan::Get(
optionsArg,
Nan::New<String>("ignoreEolWhitespace").ToLocalChecked()).ToLocalChecked();

if (Nan::To<bool>(ignoreEolWhitespace).FromJust())
// Set GIT_DIFF_IGNORE_WHITESPACE when ignoreWhitespace: true
if (Nan::To<bool>(Nan::Get(optionsArg, Nan::New<String>("ignoreAllSpace").ToLocalChecked()).ToLocalChecked()).FromJust())
options.flags = GIT_DIFF_IGNORE_WHITESPACE;
// Set GIT_DIFF_IGNORE_WHITESPACE_CHANGE when ignoreWhitespaceChange: true
else if (Nan::To<bool>(Nan::Get(optionsArg, Nan::New<String>("ignoreSpaceChange").ToLocalChecked()).ToLocalChecked()).FromJust())
options.flags = GIT_DIFF_IGNORE_WHITESPACE_CHANGE;
// Set GIT_DIFF_IGNORE_WHITESPACE_EOL when ignoreEolWhitespace: true
else if (Nan::To<bool>(Nan::Get(optionsArg, Nan::New<String>("ignoreSpaceAtEOL").ToLocalChecked()).ToLocalChecked()).FromJust()
|| Nan::To<bool>(Nan::Get(optionsArg, Nan::New<String>("ignoreEolWhitespace").ToLocalChecked()).ToLocalChecked()).FromJust())
options.flags = GIT_DIFF_IGNORE_WHITESPACE_EOL;
// Set GIT_DIFF_NORMAL when none of the above are defined
else
options.flags = GIT_DIFF_NORMAL;
}

options.context_lines = 0;
Expand Down

0 comments on commit d2cb646

Please sign in to comment.