Skip to content

Commit

Permalink
Merge pull request #737 from Toilal/feat/binary-diff
Browse files Browse the repository at this point in the history
Use a custom diff view for binary files
  • Loading branch information
SBoudrias committed Jan 25, 2015
2 parents 461a181 + 489db88 commit 79e882e
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
44 changes: 44 additions & 0 deletions lib/util/binary-diff.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict';

var fs = require('fs');
var readChunk = require('read-chunk');
var istextorbinary = require('istextorbinary');
var dateFormat = require('dateformat');
var prettyBytes = require('pretty-bytes');
var Table = require('cli-table');

module.exports = {
isBinary: function (existingFilePath, newFileContents) {
var existingHeader = readChunk.sync(existingFilePath, 0, 512);
return istextorbinary.isBinarySync(undefined, existingHeader) || istextorbinary.isBinarySync(undefined, newFileContents);
},

diff: function (existingFilePath, newFileContents) {
var existingStat = fs.statSync(existingFilePath);

var table = new Table({
head: ['', 'Existing', 'Replacement', 'Diff']
});

var sizeDiff;
if (existingStat.size > newFileContents.length) {
sizeDiff = '-';
} else {
sizeDiff = '+';
}
sizeDiff += prettyBytes(Math.abs(existingStat.size - newFileContents.length));

table.push(
['Size',
prettyBytes(existingStat.size),
prettyBytes(newFileContents.length),
sizeDiff],
['Last modified',
dateFormat(existingStat.mtime),
'',
'']
);

return table.toString();
}
};
9 changes: 8 additions & 1 deletion lib/util/conflicter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ var fs = require('fs');
var path = require('path');
var async = require('async');
var detectConflict = require('detect-conflict');
var binaryDiff = require('./binary-diff');
var util = require('util');
var _ = require('lodash');

/**
Expand Down Expand Up @@ -165,7 +167,12 @@ Conflicter.prototype._ask = function (file, cb) {
}

if (result.action === 'diff') {
this.adapter.diff(fs.readFileSync(file.path, 'utf8'), file.contents.toString());
if (binaryDiff.isBinary(file.path, file.contents)) {
this.adapter.log.writeln(binaryDiff.diff(file.path, file.contents));
} else {
var existing = fs.readFileSync(file.path);
this.adapter.diff(existing.toString(), file.contents.toString());
}
return this._ask(file, cb);
}

Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
"chalk": "^0.5.1",
"cheerio": "^0.18.0",
"class-extend": "^0.1.0",
"cli-table": "^0.3.1",
"cross-spawn": "^0.2.3",
"dargs": "^3.0.0",
"dateformat": "^1.0.11",
"debug": "^2.1.0",
"detect-conflict": "^1.0.0",
"diff": "^1.0.4",
Expand All @@ -55,6 +57,8 @@
"mime": "^1.2.9",
"mkdirp": "^0.5.0",
"nopt": "^3.0.0",
"pretty-bytes": "^1.0.2",
"read-chunk": "^1.0.1",
"rimraf": "^2.2.0",
"run-async": "^0.1.0",
"shelljs": "^0.3.0",
Expand Down
58 changes: 58 additions & 0 deletions test/conflicter.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,63 @@ describe('Conflicter', function () {
done();
});
});

it('displays default diff for text files', function (done) {
var testAdapter = new TestAdapter({ action: 'diff' });
var conflicter = new Conflicter(testAdapter);
var _prompt = testAdapter.prompt.bind(testAdapter);

var promptStub = sinon.stub(testAdapter, 'prompt', function (prompts, resultHandler) {
if (promptStub.calledTwice) {
var stubbedResultHandler = function (result) {
result.action = 'write';
return resultHandler(result);
};
return _prompt(prompts, stubbedResultHandler);
} else {
return _prompt(prompts, resultHandler);
}
});

conflicter.collision({
path: path.join(__dirname, 'fixtures/foo.js'),
contents: fs.readFileSync(path.join(__dirname, 'fixtures/foo-template.js'))
}, function () {
sinon.assert.neverCalledWithMatch(testAdapter.log.writeln, /Existing.*Replacement.*Diff/);
sinon.assert.called(testAdapter.diff);

done();
});

});

it('displays custom diff for binary files', function (done) {
var testAdapter = new TestAdapter({ action: 'diff' });
var conflicter = new Conflicter(testAdapter);
var _prompt = testAdapter.prompt.bind(testAdapter);

var promptStub = sinon.stub(testAdapter, 'prompt', function (prompts, resultHandler) {
if (promptStub.calledTwice) {
var stubbedResultHandler = function (result) {
result.action = 'write';
return resultHandler(result);
};
return _prompt(prompts, stubbedResultHandler);
} else {
return _prompt(prompts, resultHandler);
}
});

conflicter.collision({
path: path.join(__dirname, 'fixtures/yeoman-logo.png'),
contents: fs.readFileSync(path.join(__dirname, 'fixtures/testFile.tar.gz'))
}, function () {
sinon.assert.calledWithMatch(testAdapter.log.writeln, /Existing.*Replacement.*Diff/);
sinon.assert.notCalled(testAdapter.diff);

done();
});

});
});
});

0 comments on commit 79e882e

Please sign in to comment.