Skip to content

Commit d58235c

Browse files
committed
Merge pull request Urigo#1129 from ozsay/master
chore(git): Add git commit message validation
2 parents dfd41a3 + 6708dfa commit d58235c

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,23 @@ We would love contributions in:
4444

4545
If you want to contribute and need help or don't know what should you do, you can [contact me directly](https://github.com/urigo)
4646

47+
### Commit message format
48+
49+
This project follows the `angular` project git commit message format.
50+
Please refer to the [official documentation](https://github.com/angular/angular.js/blob/master/CONTRIBUTING.md#-git-commit-guidelines).
51+
4752
## Contributor Developer Setup
4853

54+
### Setup repository
55+
56+
There is a git hook that needed to be installed manually.
57+
58+
```bash
59+
git clone https://github.com/<Your Username>/angular-meteor.git
60+
cd angular-meteor
61+
ln -s ../../validate-commit-msg.js .git/hooks/commit-msg
62+
```
63+
4964
### Run local angular-meteor in your project
5065

5166
Create your Meteor Project

validate-commit-msg.js

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

0 commit comments

Comments
 (0)