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

Commit fd3bd5b

Browse files
author
Kent C. Dodds
committed
chore(githooks): Add pre-commit hook to validate commit messages
This should help contributors follow our conventions better :-) Closes #446
1 parent d6d1f78 commit fd3bd5b

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
},
7474
"config": {
7575
"ghooks": {
76-
"pre-commit": "npm run code-checks && npm run test && npm run check-coverage"
76+
"commit-msg": "node scripts/pre-commit-git-check.js && npm run code-checks && npm run test && npm run check-coverage"
7777
}
7878
},
7979
"description": "AngularJS directive which takes JSON representing a form and renders to HTML",

scripts/pre-commit-git-check.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var fs = require('fs');
2+
var path = require('path');
3+
// var test = getTests();
4+
var format = /(.+)\(.+\)\: (.+)(\n\n([.|\s|\S]*)(\n\n([.|\s|\S]*))?)?/;
5+
6+
fs.readFile(path.resolve(__dirname, '../.git/COMMIT_EDITMSG'), {encoding: 'UTF-8'}, function(err, contents) {
7+
if (err || !commitFollowsFormat(contents)) {
8+
throw err || new Error('Commit message does not follow conventions! Run `npm run commit` to commit instead of `git commit`');
9+
}
10+
});
11+
12+
13+
function commitFollowsFormat(contents) {
14+
var parts = format.exec(contents);
15+
return !!parts;
16+
}
17+
18+
// TODO add specific tests for parts
19+
/*
20+
function getTests() {
21+
return {
22+
type: testType,
23+
scope: testScope,
24+
subject: testSubject,
25+
body: testBody,
26+
footer: testFooter
27+
};
28+
}
29+
*/

0 commit comments

Comments
 (0)