diff --git a/src/diff-parser.js b/src/diff-parser.js index e027558..0a62963 100644 --- a/src/diff-parser.js +++ b/src/diff-parser.js @@ -408,6 +408,11 @@ // Remove prefix if exists filename = filename.slice(matchingPrefixes[0].length); } + + // Cleanup timestamps generated by the unified diff (diff command) as specified in + // https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html + // Ie: 2016-10-25 11:37:14.000000000 +0200 + filename = filename.replace(/\s+\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)? \+\d{4}.*$/, ''); } return filename; diff --git a/test/diff-parser-tests.js b/test/diff-parser-tests.js index f4dba32..335186a 100644 --- a/test/diff-parser-tests.js +++ b/test/diff-parser-tests.js @@ -399,6 +399,30 @@ describe('DiffParser', function() { assert.equal(1, file1.blocks[0].lines[1].newNumber); }); + it('should parse unified non git diff and strip timestamps off the headers', function() { + var diff = + '--- a/sample.js 2016-10-25 11:37:14.000000000 +0200\n' + + '+++ b/sample.js 2016-10-25 11:37:14.000000000 +0200\n' + + '@@ -1 +1,2 @@\n' + + '-test\n' + + '+test1r\n' + + '+test2r\n'; + + var result = DiffParser.generateDiffJson(diff); + var file1 = result[0]; + assert.equal(1, result.length); + assert.equal(2, file1.addedLines); + assert.equal(1, file1.deletedLines); + assert.equal('sample.js', file1.oldName); + assert.equal('sample.js', file1.newName); + assert.equal(1, file1.blocks.length); + + var linesContent = file1.blocks[0].lines.map(function(line) { + return line.content; + }); + assert.deepEqual(linesContent, ['-test', '+test1r', '+test2r']); + }); + it('should parse unified non git diff', function() { var diff = '--- a/sample.js\n' +