Skip to content
This repository has been archived by the owner on Feb 5, 2018. It is now read-only.

Commit

Permalink
fix(robustness): handle undefined message buffer, closes #9
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Nov 20, 2015
1 parent 168fa89 commit f431743
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ if (process.argv.join('').indexOf('mocha') === -1) {
var commitMsgFile = process.argv[2] || './.git/COMMIT_EDITMSG';
var incorrectLogFile = commitMsgFile.replace('COMMIT_EDITMSG', 'logs/incorrect-commit-msgs');

var hasToString = function hasToString(x) {
return x && typeof x.toString === 'function';
};

fs.readFile(commitMsgFile, function(err, buffer) {
var msg = firstLineFromBuffer(buffer);

Expand All @@ -105,7 +109,7 @@ if (process.argv.join('').indexOf('mocha') === -1) {
}

function firstLineFromBuffer(buffer) {
return buffer.toString().split('\n').shift();
return hasToString(buffer) && buffer.toString().split('\n').shift();
}
});
}
5 changes: 5 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ describe('validate-commit-msg.js', function() {
it('should ignore msg prefixed with "WIP: "', function() {
expect(m.validateMessage('WIP: bullshit')).to.equal(VALID);
});


it('should handle undefined message"', function() {
expect(m.validateMessage()).to.equal(INVALID);
});
});

afterEach(function() {
Expand Down

0 comments on commit f431743

Please sign in to comment.