Skip to content

Commit e64ac60

Browse files
committed
Merge pull request #63 from rtfpessoa/fix-filenames-parsing
Fix filename parsing with special characters and prefixes
2 parents c0f7c7a + e44b2a2 commit e64ac60

File tree

3 files changed

+74
-10
lines changed

3 files changed

+74
-10
lines changed

src/diff-parser.js

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
DiffParser.prototype.LINE_TYPE = LINE_TYPE;
2525

26-
DiffParser.prototype.generateDiffJson = function(diffInput) {
26+
DiffParser.prototype.generateDiffJson = function(diffInput, config) {
2727
var files = [];
2828
var currentFile = null;
2929
var currentBlock = null;
@@ -133,11 +133,11 @@
133133
var deletedFileMode = /^deleted file mode (\d{6})/;
134134
var newFileMode = /^new file mode (\d{6})/;
135135

136-
var copyFrom = /^copy from (.+)/;
137-
var copyTo = /^copy to (.+)/;
136+
var copyFrom = /^copy from "?(.+?)"?/;
137+
var copyTo = /^copy to "?(.+?)"?/;
138138

139-
var renameFrom = /^rename from (.+)/;
140-
var renameTo = /^rename to (.+)/;
139+
var renameFrom = /^rename from "?(.+?)"?/;
140+
var renameTo = /^rename to "?(.+?)"?/;
141141

142142
var similarityIndex = /^similarity index (\d+)%/;
143143
var dissimilarityIndex = /^dissimilarity index (\d+)%/;
@@ -160,10 +160,10 @@
160160
var values = [];
161161
if (utils.startsWith(line, 'diff')) {
162162
startFile();
163-
} else if (currentFile && !currentFile.oldName && (values = /^--- [aiwco]\/(.+)$/.exec(line))) {
163+
} else if (currentFile && !currentFile.oldName && (values = getSrcFilename(line, config))) {
164164
currentFile.oldName = values[1];
165165
currentFile.language = getExtension(currentFile.oldName, currentFile.language);
166-
} else if (currentFile && !currentFile.newName && (values = /^\+\+\+ [biwco]?\/(.+)$/.exec(line))) {
166+
} else if (currentFile && !currentFile.newName && (values = getDstFilename(line, config))) {
167167
currentFile.newName = values[1];
168168
currentFile.language = getExtension(currentFile.newName, currentFile.language);
169169
} else if (currentFile && utils.startsWith(line, '@@')) {
@@ -226,6 +226,27 @@
226226
return language;
227227
}
228228

229+
function getSrcFilename(line, cfg) {
230+
var prefixes = ["a\\/", "i\\/", "w\\/", "c\\/", "o\\/"];
231+
232+
if (cfg.srcPrefix) prefixes.push(cfg.srcPrefix);
233+
234+
return _getFilename('---', line, prefixes);
235+
}
236+
237+
function getDstFilename(line, cfg) {
238+
var prefixes = ["b\\/", "i\\/", "w\\/", "c\\/", "o\\/"];
239+
240+
if (cfg.dstPrefix) prefixes.push(cfg.dstPrefix);
241+
242+
return _getFilename('\\+\\+\\+', line, prefixes);
243+
}
244+
245+
function _getFilename(linePrefix, line, prefixes) {
246+
var prefixesStr = prefixes.join("|");
247+
return new RegExp('^' + linePrefix + ' "?(?:' + prefixesStr + ')(.+?)"?$').exec(line);
248+
}
249+
229250
module.exports.DiffParser = new DiffParser();
230251

231252
})();

src/diff2html.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
/*
2727
* Generates json object from string diff input
2828
*/
29-
Diff2Html.prototype.getJsonFromDiff = function(diffInput) {
30-
return diffParser.generateDiffJson(diffInput);
29+
Diff2Html.prototype.getJsonFromDiff = function(diffInput, config) {
30+
var configOrEmpty = config || {};
31+
return diffParser.generateDiffJson(diffInput, configOrEmpty);
3132
};
3233

3334
/*
@@ -38,7 +39,7 @@
3839

3940
var diffJson = diffInput;
4041
if (!configOrEmpty.inputFormat || configOrEmpty.inputFormat === 'diff') {
41-
diffJson = diffParser.generateDiffJson(diffInput);
42+
diffJson = diffParser.generateDiffJson(diffInput, configOrEmpty);
4243
}
4344

4445
var fileList = '';

test/diff-parser-tests.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,47 @@ describe('DiffParser', function() {
6464
assert.equal(1, file1.blocks.length);
6565
}
6666

67+
it('should parse diff with special characters', function() {
68+
var diff =
69+
'diff --git "a/bla with \ttab.scala" "b/bla with \ttab.scala"\n' +
70+
'index 4c679d7..e9bd385 100644\n' +
71+
'--- "a/bla with \ttab.scala"\n' +
72+
'+++ "b/bla with \ttab.scala"\n' +
73+
'@@ -1 +1,2 @@\n' +
74+
'-cenas\n' +
75+
'+cenas com ananas\n' +
76+
'+bananas';
77+
78+
var result = Diff2Html.getJsonFromDiff(diff);
79+
var file1 = result[0];
80+
assert.equal(1, result.length);
81+
assert.equal(2, file1.addedLines);
82+
assert.equal(1, file1.deletedLines);
83+
assert.equal('bla with \ttab.scala', file1.oldName);
84+
assert.equal('bla with \ttab.scala', file1.newName);
85+
assert.equal(1, file1.blocks.length);
86+
});
87+
88+
it('should parse diff with prefix', function() {
89+
var diff =
90+
'diff --git "\tbla with \ttab.scala" "\tbla with \ttab.scala"\n' +
91+
'index 4c679d7..e9bd385 100644\n' +
92+
'--- "\tbla with \ttab.scala"\n' +
93+
'+++ "\tbla with \ttab.scala"\n' +
94+
'@@ -1 +1,2 @@\n' +
95+
'-cenas\n' +
96+
'+cenas com ananas\n' +
97+
'+bananas';
98+
99+
var result = Diff2Html.getJsonFromDiff(diff, {"srcPrefix": "\t", "dstPrefix": "\t"});
100+
var file1 = result[0];
101+
assert.equal(1, result.length);
102+
assert.equal(2, file1.addedLines);
103+
assert.equal(1, file1.deletedLines);
104+
assert.equal('bla with \ttab.scala', file1.oldName);
105+
assert.equal('bla with \ttab.scala', file1.newName);
106+
assert.equal(1, file1.blocks.length);
107+
});
108+
67109
});
68110
});

0 commit comments

Comments
 (0)