Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 5dbd942

Browse files
vojtajinamhevery
authored andcommitted
chore(scripts): add commit-msg hook (validation)
1 parent 84c13d9 commit 5dbd942

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

validate-commit-msg.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Git COMMIT-MSG hook for validating commit message
5+
* See https://docs.google.com/document/d/1rk04jEuGfk9kYzfqCuOlPTSJw3hEDZJTBN5E5f1SALo/edit
6+
*
7+
* Installation:
8+
* >> cd <angular-repo>
9+
* >> ln -s ../../validate-commit-msg.js .git/hooks/commit-msg
10+
*/
11+
var fs = require('fs');
12+
var util = require('util');
13+
14+
15+
var MAX_LENGTH = 70;
16+
var PATTERN = /^(\w*)(\(([\w\$\.]*)\))?\: (.*)$/;
17+
var IGNORED = /^WIP\:/;
18+
var TYPES = {
19+
feat: true,
20+
fix: true,
21+
docs: true,
22+
style: true,
23+
refactor: true,
24+
test: true,
25+
chore: true
26+
};
27+
28+
29+
var error = function() {
30+
// gitx does not display it
31+
// http://gitx.lighthouseapp.com/projects/17830/tickets/294-feature-display-hook-error-message-when-hook-fails
32+
// https://groups.google.com/group/gitx/browse_thread/thread/a03bcab60844b812
33+
console.error('INVALID COMMIT MSG: ' + util.format.apply(null, arguments));
34+
};
35+
36+
37+
var validateMessage = function(message) {
38+
var isValid = true;
39+
40+
if (IGNORED.test(message)) {
41+
console.log('Commit message validation ignored.');
42+
return true;
43+
}
44+
45+
if (message.length > MAX_LENGTH) {
46+
error('is longer than %d characters !', MAX_LENGTH);
47+
isValid = false;
48+
}
49+
50+
var match = PATTERN.exec(message);
51+
52+
if (!match) {
53+
error('does not match "<type>(<scope>): <subject>" !');
54+
return false;
55+
}
56+
57+
var type = match[1];
58+
var scope = match[3];
59+
var subject = match[4];
60+
61+
if (!TYPES.hasOwnProperty(type)) {
62+
error('"%s" is not allowed type !', type);
63+
return false;
64+
}
65+
66+
// Some more ideas, do want anything like this ?
67+
// - allow only specific scopes (eg. fix(docs) should not be allowed ?
68+
// - auto correct the type to lower case ?
69+
// - auto correct first letter of the subject to lower case ?
70+
// - auto add empty line after subject ?
71+
// - auto remove empty () ?
72+
// - auto correct typos in type ?
73+
// - store incorrect messages, so that we can learn
74+
75+
return isValid;
76+
};
77+
78+
79+
var firstLineFromBuffer = function(buffer) {
80+
return buffer.toString().split('\n').shift();
81+
};
82+
83+
84+
85+
// publish for testing
86+
exports.validateMessage = validateMessage;
87+
88+
// hacky start if not run by jasmine :-D
89+
if (process.argv.join('').indexOf('jasmine-node') === -1) {
90+
var commitMsgFile = process.argv[2];
91+
var incorrectLogFile = commitMsgFile.replace('COMMIT_EDITMSG', 'logs/incorrect-commit-msgs');
92+
93+
fs.readFile(commitMsgFile, function(err, buffer) {
94+
var msg = firstLineFromBuffer(buffer);
95+
96+
if (!validateMessage(msg)) {
97+
fs.appendFile(incorrectLogFile, msg + '\n', function() {
98+
process.exit(1);
99+
});
100+
} else {
101+
process.exit(0);
102+
}
103+
});
104+
}

validate-commit-msg.spec.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
describe('validate-commit-msg.js', function() {
2+
var m = require('./validate-commit-msg');
3+
var errors = [];
4+
var logs = [];
5+
6+
var VALID = true;
7+
var INVALID = false;
8+
9+
beforeEach(function() {
10+
errors.length = 0;
11+
logs.length = 0;
12+
13+
spyOn(console, 'error').andCallFake(function(msg) {
14+
errors.push(msg.replace(/\x1B\[\d+m/g, '')); // uncolor
15+
});
16+
17+
spyOn(console, 'log').andCallFake(function(msg) {
18+
logs.push(msg.replace(/\x1B\[\d+m/g, '')); // uncolor
19+
});
20+
});
21+
22+
describe('validateMessage', function() {
23+
24+
it('should be valid', function() {
25+
expect(m.validateMessage('fix($compile): something')).toBe(VALID);
26+
expect(m.validateMessage('feat($location): something')).toBe(VALID);
27+
expect(m.validateMessage('docs($filter): something')).toBe(VALID);
28+
expect(m.validateMessage('style($http): something')).toBe(VALID);
29+
expect(m.validateMessage('refactor($httpBackend): something')).toBe(VALID);
30+
expect(m.validateMessage('test($resource): something')).toBe(VALID);
31+
expect(m.validateMessage('chore($controller): something')).toBe(VALID);
32+
expect(errors).toEqual([]);
33+
});
34+
35+
36+
it('should validate 70 characters length', function() {
37+
var msg = 'fix($compile): something super mega extra giga tera long, maybe even longer... ' +
38+
'way over 80 characters';
39+
40+
expect(m.validateMessage(msg)).toBe(INVALID);
41+
expect(errors).toEqual(['INVALID COMMIT MSG: is longer than 70 characters !']);
42+
});
43+
44+
45+
it('should validate "<type>(<scope>): <subject>" format', function() {
46+
var msg = 'not correct format';
47+
48+
expect(m.validateMessage(msg)).toBe(INVALID);
49+
expect(errors).toEqual(['INVALID COMMIT MSG: does not match "<type>(<scope>): <subject>" !']);
50+
});
51+
52+
53+
it('should validate type', function() {
54+
expect(m.validateMessage('weird($filter): something')).toBe(INVALID);
55+
expect(errors).toEqual(['INVALID COMMIT MSG: "weird" is not allowed type !']);
56+
});
57+
58+
59+
it('should allow empty scope', function() {
60+
expect(m.validateMessage('fix: blablabla')).toBe(VALID);
61+
});
62+
63+
64+
it('should allow dot in scope', function() {
65+
expect(m.validateMessage('chore(mocks.$httpBackend): something')).toBe(VALID);
66+
});
67+
68+
69+
it('should ignore msg prefixed with "WIP: "', function() {
70+
expect(m.validateMessage('WIP: bullshit')).toBe(VALID);
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)