Skip to content

Commit fe91dac

Browse files
committed
feat(whitespace): handle whitespace-only changes
If there is no difference between the logical structure of the JSON, then try to minimize file thrashing by picking a consistent winner. - If ours and theirs are identical, use that (should be no conflict) - Otherwise, use base (minimize whitespace-only changes to JSON file)
1 parent c5572ac commit fe91dac

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

bin/git-json-merge

100644100755
File mode changed.

lib/git-json-merge.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ function mergeJson (oursJson, baseJson, theirsJson) {
2020
var ours = JSON.parse(oursJson);
2121
var base = JSON.parse(baseJson);
2222
var theirs = JSON.parse(theirsJson);
23-
var newOurs = merge(ours, base, theirs);
24-
var newOursJson = JSON.stringify(newOurs, null, newOursIndent);
25-
26-
return newOursJson;
23+
var diff = xdiff.diff3(ours, base, theirs);
24+
if (diff) {
25+
var newOurs = xdiff.patch(base, diff);
26+
var newOursJson = JSON.stringify(newOurs, null, newOursIndent);
27+
return newOursJson;
28+
} else if (oursJson === theirsJson) {
29+
return oursJson;
30+
} else {
31+
return baseJson;
32+
}
2733
}
2834

2935
function merge (ours, base, theirs) {

test/git-json-merge.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,31 @@ describe('gitJsonMerge', function () {
3232
describeStripBomTest('[{"id":1,\uFEFF"field":"Foo"}]', '[{"id":1,\uFEFF"field":"Foo"}]');
3333
describeStripBomTest('\uFEFF[{"id":1,"field":"Foo"}]\uFEFF', '[{"id":1,"field":"Foo"}]\uFEFF');
3434
});
35+
36+
describe('Keep common string.', function() {
37+
// All are identical
38+
describeStringEquivalence(
39+
'[{"id":1,"field":"Foo"}]',
40+
'[{"id":1,"field":"Foo"}]',
41+
'[{"id":1,"field":"Foo"}]',
42+
'[{"id":1,"field":"Foo"}]'
43+
);
44+
// Ours and theirs have identical whitespace-only changes
45+
describeStringEquivalence(
46+
'[\n {"id": 1,"field":"Foo"\n}\n]',
47+
'[\n {"id":1,"field":"Foo"}\n]',
48+
'[\n {"id": 1,"field":"Foo"\n}\n]',
49+
'[\n {"id": 1,"field":"Foo"\n}\n]'
50+
);
51+
// Ours and theirs have diverging whitespace-only changes
52+
describeStringEquivalence(
53+
'[{"id":1,"field": "Foo"}]',
54+
'[\n {"id":1,"field":"Foo"}\n]',
55+
'[\n {"id": 1,"field":"Foo"\n}\n]',
56+
'[\n {"id":1,"field":"Foo"}\n]'
57+
);
58+
});
59+
3560
});
3661

3762
function toString (object) {
@@ -85,3 +110,13 @@ function describeStripBomTest (str, expected) {
85110
})
86111
});
87112
}
113+
114+
function describeStringEquivalence (ours, base, theirs, expected) {
115+
describe('given arguments of ' + toString(ours) + ' as ours, ' + toString(base) + ' as base and ' + toString(theirs) + ' as theirs', function () {
116+
var actual = gitJsonMerge.mergeJson(ours, base, theirs);
117+
118+
it('should return ' + toString(expected), function () {
119+
expect(actual).to.deep.equal(expected);
120+
})
121+
});
122+
}

0 commit comments

Comments
 (0)