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

Commit 2e048fb

Browse files
cmalardGarbee
authored andcommitted
feat(autoFix): fix the commit message case instead of ignoring case
Close #105
1 parent 888617b commit 2e048fb

File tree

5 files changed

+54
-18
lines changed

5 files changed

+54
-18
lines changed

.vcmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
"custom"
1414
],
1515
"warnOnFail": false,
16-
"autoFix": false
16+
"autoFix": true
1717
}

lib/cli.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
'use strict';
1818

1919
var fs = require('fs');
20-
var validateMessage = require('../index');
20+
2121
var getGitFolder = require('./getGitFolder');
22+
var validateMessage = require('../index');
2223

2324
// hacky start if not run by mocha :-D
2425
// istanbul ignore next
@@ -42,6 +43,7 @@ if (process.argv.join('').indexOf('mocha') === -1) {
4243
};
4344

4445
var getCommit = function() {
46+
var file;
4547
var fileContent;
4648
var gitDirectory;
4749

@@ -53,6 +55,7 @@ if (process.argv.join('').indexOf('mocha') === -1) {
5355
// these info might change ahead
5456
message: commitMsgFileOrText,
5557
errorLog: commitErrorLogPath || null,
58+
file: null,
5659
};
5760

5861
// On running the validation over a text instead of git files such as COMMIT_EDITMSG and GITGUI_EDITMSG
@@ -62,16 +65,19 @@ if (process.argv.join('').indexOf('mocha') === -1) {
6265

6366
// Try to load commit from a path passed as argument
6467
if (commitMsgFileOrText) {
65-
fileContent = getFileContent(gitDirectory + '/' + commitMsgFileOrText);
68+
file = gitDirectory + '/' + commitMsgFileOrText;
69+
fileContent = getFileContent(file);
6670
}
6771

6872
// If no file or message is available then try to load it from the default commit file
6973
if (!fileContent && !commitMsgFileOrText) {
70-
fileContent = getFileContent(gitDirectory + '/COMMIT_EDITMSG');
74+
file = gitDirectory + '/COMMIT_EDITMSG';
75+
fileContent = getFileContent(file);
7176
}
7277

7378
// Could resolve the content from a file
7479
if (fileContent) {
80+
commit.file = file;
7581
commit.message = fileContent;
7682
}
7783

@@ -85,7 +91,7 @@ if (process.argv.join('').indexOf('mocha') === -1) {
8591
};
8692

8793
var validate = function (commit) {
88-
if (!validateMessage(commit.message) && commit.errorLog) {
94+
if (!validateMessage(commit.message, commit.file) && commit.errorLog) {
8995
fs.appendFileSync(commit.errorLog, commit.message + '\n');
9096
process.exit(1);
9197
} else {

lib/validateMessage.js

+17-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

3-
var util = require('util');
3+
var fs = require('fs');
44
var semverRegex = require('semver-regex');
5+
var util = require('util');
6+
57
var getConfig = require('./config').getConfig;
68

79
var config = getConfig();
@@ -21,8 +23,9 @@ var error = function() {
2123

2224
exports.config = config;
2325

24-
exports.validateMessage = function validateMessage(raw) {
26+
exports.validateMessage = function validateMessage(raw, sourceFile) {
2527
var types = config.types = config.types || 'conventional-commit-types';
28+
var AUTO_FIX = config.autoFix && sourceFile;
2629

2730
// resolve types from a module
2831
if (typeof types === 'string' && types !== '*') {
@@ -72,8 +75,7 @@ exports.validateMessage = function validateMessage(raw) {
7275
isValid = false;
7376
}
7477

75-
// If should auto fix type then do it here
76-
if (config.autoFix) {
78+
if (AUTO_FIX) {
7779
type = lowercase(type);
7880
}
7981

@@ -84,7 +86,7 @@ exports.validateMessage = function validateMessage(raw) {
8486

8587
isValid = validateScope(isValid, scope);
8688

87-
if (config.autoFix) {
89+
if (AUTO_FIX) {
8890
subject = lowercaseFirstLetter(subject);
8991
}
9092

@@ -104,6 +106,16 @@ exports.validateMessage = function validateMessage(raw) {
104106
isValid = isValid || config.warnOnFail;
105107

106108
if (isValid) { // exit early and skip messaging logics
109+
if (AUTO_FIX && !squashing) {
110+
var scopeFixed = scope ? '(' + scope + ')' : '';
111+
var firstLineFixed = type + scopeFixed + ': ' + subject;
112+
113+
if (firstLine !== firstLineFixed) {
114+
var rawFixed = raw.replace(firstLine, firstLineFixed);
115+
fs.writeFileSync(sourceFile, rawFixed);
116+
}
117+
}
118+
107119
return true;
108120
}
109121

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"custom"
7272
],
7373
"warnOnFail": false,
74-
"autoFix": false
74+
"autoFix": true
7575
}
7676
},
7777
"dependencies": {

test/validateMessage.test.js

+25-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('validate-commit-msg.js', function() {
1010
var originalLog, originalError;
1111
var errors = [];
1212
var logs = [];
13+
var writeFileSyncStub;
1314

1415
var VALID = true;
1516
var INVALID = false;
@@ -27,6 +28,7 @@ describe('validate-commit-msg.js', function() {
2728

2829
sinon.spy(console, 'error');
2930
sinon.spy(console, 'log');
31+
writeFileSyncStub = sinon.stub(fs, 'writeFileSync');
3032

3133
function fakeError() {
3234
var msg = format.apply(null, arguments);
@@ -40,6 +42,9 @@ describe('validate-commit-msg.js', function() {
4042
});
4143

4244
afterEach(function() {
45+
console.log = originalLog;
46+
console.error = originalError;
47+
fs.writeFileSync.restore();
4348
m.config.autoFix = false;
4449
});
4550

@@ -292,33 +297,46 @@ describe('validate-commit-msg.js', function() {
292297
});
293298

294299
it('should validate subject against subjectPattern if provided', function() {
295-
var msg = 'chore(build): A something Z';
300+
var subjectPatternBackup = m.config.subjectPattern;
296301
m.config.subjectPattern = /^A.*Z$/;
302+
303+
var msg = 'chore(build): A something Z';
297304
expect(m.validateMessage(msg)).to.equal(VALID);
298305

299306
msg = 'chore(build): something';
300307
expect(m.validateMessage(msg)).to.equal(INVALID);
301308

302309
expect(errors).to.deep.equal(['INVALID COMMIT MSG: subject does not match subject pattern!']);
303310
expect(logs).to.deep.equal([msg]);
311+
312+
m.config.subjectPattern = subjectPatternBackup;
304313
});
305314

306315
it('should lowercase type when autoFix is true and make it valid', function() {
307316
m.config.autoFix = true;
308-
m.config.subjectPattern = /^a.*Z$/;
309317
var msg = 'Chore(build): A something Z';
310-
expect(m.validateMessage(msg)).to.equal(VALID);
318+
expect(m.validateMessage(msg, 'file')).to.equal(VALID);
311319
});
312320

313321
it('should show invalid when autoFix is false and type starts with capital letter', function() {
314322
var msg = 'Chore(build): A something Z';
315323
expect(m.validateMessage(msg)).to.equal(INVALID);
316324
});
317-
});
318325

319-
afterEach(function() {
320-
console.log = originalLog;
321-
console.error = originalError;
326+
it('should update the file provided when autoFix is true and there are changes', function() {
327+
m.config.autoFix = true;
328+
var msg = 'Chore(build): A something Z';
329+
var msgValid = 'chore(build): a something Z';
330+
m.validateMessage(msg, 'file');
331+
expect(writeFileSyncStub.calledWith('file', msgValid)).to.equal(VALID);
332+
});
333+
334+
it('should not update the file provided when autoFix is true and there are no changes', function() {
335+
m.config.autoFix = true;
336+
var msg = 'chore(build): a something Z';
337+
m.validateMessage(msg, 'file');
338+
expect(writeFileSyncStub.called).to.equal(INVALID);
339+
});
322340
});
323341

324342
});

0 commit comments

Comments
 (0)