Skip to content

Commit

Permalink
Merge pull request #80 from rtfpessoa/improve-paths
Browse files Browse the repository at this point in the history
Improve paths
  • Loading branch information
rtfpessoa committed May 20, 2016
2 parents 146d951 + ce90977 commit ae347be
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 11 deletions.
12 changes: 9 additions & 3 deletions sample/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
Author: rtfpessoa
-->

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/styles/github.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/styles/github.min.css">

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.3.0/languages/scala.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/highlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.4.0/languages/scala.min.js"></script>

<!-- diff2html -->
<link rel="stylesheet" type="text/css" href="../dist/diff2html.css">
Expand All @@ -24,6 +24,12 @@

<script>
var lineDiffExample =
"--- a/src/my/really/big/path/sample.js\n" +
"+++ b/src/my/small/path/sample.js\n" +
"@@ -1 +1,2 @@\n" +
"-test\n" +
"+test1r\n" +
"+test2r\n" +
'diff --git a/src/core/init.java b/src/core/init.java\n' +
'index e49196a..50f310c 100644\n' +
'--- a/src/core/init.java\n' +
Expand Down
65 changes: 61 additions & 4 deletions src/printer-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
var utils = require('./utils.js').Utils;
var Rematch = require('./rematch.js').Rematch;

var separator = '/';

function PrinterUtils() {
}

Expand All @@ -32,18 +34,65 @@
};

PrinterUtils.prototype.getDiffName = function(file) {
var oldFilename = file.oldName;
var newFilename = file.newName;
var oldFilename = unifyPath(file.oldName);
var newFilename = unifyPath(file.newName);

if (oldFilename && newFilename && oldFilename !== newFilename && !isDevNullName(oldFilename) && !isDevNullName(newFilename)) {
return oldFilename + ' -> ' + newFilename;
var prefixPaths = [];
var suffixPaths = [];

var oldFilenameParts = oldFilename.split(separator);
var newFilenameParts = newFilename.split(separator);

var oldFilenamePartsSize = oldFilenameParts.length;
var newFilenamePartsSize = newFilenameParts.length;

var i = 0;
var j = oldFilenamePartsSize - 1;
var k = newFilenamePartsSize - 1;

while (i < j && i < k) {
if (oldFilenameParts[i] === newFilenameParts[i]) {
prefixPaths.push(newFilenameParts[i]);
i += 1;
} else {
break;
}
}

while (j > i && k > i) {
if (oldFilenameParts[j] === newFilenameParts[k]) {
suffixPaths.unshift(newFilenameParts[k]);
j -= 1;
k -= 1;
} else {
break;
}
}

var finalPrefix = prefixPaths.join(separator);
var finalSuffix = suffixPaths.join(separator);

var oldRemainingPath = oldFilenameParts.slice(i, j + 1).join(separator);
var newRemainingPath = newFilenameParts.slice(i, k + 1).join(separator);

if (finalPrefix.length && finalSuffix.length) {
return finalPrefix + separator + '{' + oldRemainingPath + ' → ' + newRemainingPath + '}' + separator + finalSuffix;
} else if (finalPrefix.length) {
return finalPrefix + separator + '{' + oldRemainingPath + ' → ' + newRemainingPath + '}';
} else if (finalSuffix.length) {
return '{' + oldRemainingPath + ' → ' + newRemainingPath + '}' + separator + finalSuffix;
}

return oldFilename + ' → ' + newFilename;

} else if (newFilename && !isDevNullName(newFilename)) {
return newFilename;
} else if (oldFilename) {
return oldFilename;
}

return 'Unknown filename';
return 'unknown/file/path';
};

PrinterUtils.prototype.diffHighlight = function(diffLine1, diffLine2, config) {
Expand Down Expand Up @@ -128,6 +177,14 @@
};
};

function unifyPath(path) {
if (path) {
return path.replace('\\', '/');
}

return path;
}

function isDevNullName(name) {
return name.indexOf('dev/null') !== -1;
}
Expand Down
29 changes: 25 additions & 4 deletions test/printer-utils-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,33 @@ describe('Utils', function() {
});
assert.equal('sample.js', result);
});
it('should generate the file name for a changed file and renamed', function() {
it('should generate the file name for a changed file and full rename', function() {
var result = PrinterUtils.getDiffName({
oldName: 'sample1.js',
newName: 'sample2.js'
});
assert.equal('sample1.js -> sample2.js', result);
assert.equal('sample1.js → sample2.js', result);
});
it('should generate the file name for a changed file and prefix rename', function() {
var result = PrinterUtils.getDiffName({
oldName: 'src/path/sample.js',
newName: 'source/path/sample.js'
});
assert.equal('{src → source}/path/sample.js', result);
});
it('should generate the file name for a changed file and suffix rename', function() {
var result = PrinterUtils.getDiffName({
oldName: 'src/path/sample1.js',
newName: 'src/path/sample2.js'
});
assert.equal('src/path/{sample1.js → sample2.js}', result);
});
it('should generate the file name for a changed file and middle rename', function() {
var result = PrinterUtils.getDiffName({
oldName: 'src/really/big/path/sample.js',
newName: 'src/small/path/sample.js'
});
assert.equal('src/{really/big → small}/path/sample.js', result);
});
it('should generate the file name for a deleted file', function() {
var result = PrinterUtils.getDiffName({
Expand All @@ -49,9 +70,9 @@ describe('Utils', function() {
});
assert.equal('src/my/file.js', result);
});
it('should generate handle undefined filenames', function() {
it('should generate handle undefined filename', function() {
var result = PrinterUtils.getDiffName({});
assert.equal('Unknown filename', result);
assert.equal('unknown/file/path', result);
});
});

Expand Down

0 comments on commit ae347be

Please sign in to comment.