Skip to content

Commit a4b5c6c

Browse files
committed
EOFNL fixes for createPatch
1 parent bea416d commit a4b5c6c

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

diff.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ var JsDiff = (function() {
191191
ret.push("+++ " + fileName + "\t" + newHeader);
192192

193193
var diff = LineDiff.diff(oldStr, newStr);
194+
if (!diff[diff.length-1].value) {
195+
diff.pop(); // Remove trailing newline add
196+
}
194197
diff.push({value: "", lines: []}); // Append an empty value to make cleanup easier
195198

196199
function contextLines(lines) {
@@ -217,6 +220,14 @@ var JsDiff = (function() {
217220
}
218221
}
219222
curRange.push.apply(curRange, lines.map(function(entry) { return (current.added?"+":"-") + entry; }));
223+
224+
// Figure out if this is the last line for the given file and missing NL
225+
if (!/\n$/.test(current.value)
226+
&& (i === diff.length-2
227+
|| (i === diff.length-3 && current.added === !diff[diff.length-2].added))) {
228+
curRange.push('\\ No newline at end of file');
229+
}
230+
220231
if (current.added) {
221232
newLine += lines.length;
222233
} else {
@@ -245,11 +256,8 @@ var JsDiff = (function() {
245256
newLine += lines.length;
246257
}
247258
}
248-
if (diff.length > 1 && !/\n$/.test(diff[diff.length-2].value)) {
249-
ret.push("\\ No newline at end of file\n");
250-
}
251-
252-
return ret.join("\n");
259+
260+
return ret.join('\n') + '\n';
253261
},
254262

255263
convertChangesToXML: function(changes){

0 commit comments

Comments
 (0)