Improve rename paths

* Identify smallest different sub path of the changed files paths and
highlight the changed part
This commit is contained in:
Rodrigo Fernandes 2016-05-20 23:17:03 +01:00
parent 5cac9fd99f
commit 285c3fefc5
No known key found for this signature in database
GPG key ID: 08E3C5F38969078E
3 changed files with 95 additions and 11 deletions

View file

@ -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">
@ -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' +

View file

@ -11,6 +11,8 @@
var utils = require('./utils.js').Utils;
var Rematch = require('./rematch.js').Rematch;
var separator = '/';
function PrinterUtils() {
}
@ -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 = i + 1;
} else {
break;
}
}
while (j > i && k > i) {
if (oldFilenameParts[j] === newFilenameParts[k]) {
suffixPaths.unshift(newFilenameParts[k]);
j = j - 1;
k = 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;
} else {
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) {
@ -128,6 +177,14 @@
};
};
function unifyPath(path) {
if (path) {
return path.replace('\\', '/');
} else {
return path;
}
}
function isDevNullName(name) {
return name.indexOf('dev/null') !== -1;
}

View file

@ -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({
@ -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);
});
});