Skip to content

Commit 2017024

Browse files
Raúl Gómez Acuñasatya164
Raúl Gómez Acuña
authored andcommitted
chore: add Git Hook to validate commit messages (callstack#118)
1 parent 79cf42a commit 2017024

File tree

4 files changed

+317
-15
lines changed

4 files changed

+317
-15
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
22
docs/dist/
3+
scripts/

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
"react-native-vector-icons": "*"
2323
},
2424
"devDependencies": {
25+
"babel-cli": "^6.24.1",
2526
"babel-eslint": "^7.2.3",
27+
"babel-preset-flow": "^6.23.0",
2628
"eslint": "^4.1.1",
2729
"eslint-config-prettier": "^2.3.0",
2830
"eslint-plugin-babel": "^4.1.1",
@@ -44,6 +46,7 @@
4446
"react-native-drawer": "^2.3.0"
4547
},
4648
"scripts": {
49+
"commitmsg": "babel-node ./scripts/validateCommitMessage.js $GIT_PARAMS --presets flow",
4750
"precommit": "npm run lint && npm run flow",
4851
"flow": "flow",
4952
"lint": "eslint .",

scripts/validateCommitMessage.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* @flow */
2+
3+
const fs = require('fs');
4+
const util = require('util');
5+
6+
const MAX_LENGTH = 100;
7+
const PATTERN = /^(?:fixup!\s*)?(\w*)(\(([\w\$\.\*/-]*)\))?\: (.*)$/;
8+
const TYPES = {
9+
feat: true,
10+
fix: true,
11+
docs: true,
12+
style: true,
13+
refactor: true,
14+
perf: true,
15+
test: true,
16+
chore: true,
17+
revert: true,
18+
breaking: true,
19+
};
20+
21+
function printError() {
22+
console.error('INVALID COMMIT MSG: ' + util.format.apply(null, arguments));
23+
}
24+
25+
function validateMessage(message /*: string */) {
26+
if (message.length > MAX_LENGTH) {
27+
printError('is longer than %d characters !', MAX_LENGTH);
28+
return false;
29+
}
30+
31+
const match = PATTERN.exec(message);
32+
33+
if (!match) {
34+
printError('does not match "<type>(<scope>): <subject>" ! was: ' + message);
35+
return false;
36+
}
37+
const type = match[1];
38+
39+
if (!TYPES.hasOwnProperty(type)) {
40+
printError('"%s" is not allowed type !', type);
41+
return false;
42+
}
43+
44+
return true;
45+
}
46+
47+
function firstLineFromBuffer(buffer /*: Buffer */) {
48+
return buffer.toString().split('\n').shift();
49+
}
50+
51+
const commitMsgFile = process.argv[2];
52+
53+
try {
54+
const buffer = fs.readFileSync(commitMsgFile);
55+
const msg = firstLineFromBuffer(buffer);
56+
if (!validateMessage(msg)) {
57+
process.exit(1);
58+
} else {
59+
process.exit(0);
60+
}
61+
} catch (error) {
62+
process.exit(1);
63+
}

0 commit comments

Comments
 (0)