forked from BitGo/BitGoJS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-commit-msg.js
More file actions
executable file
·32 lines (28 loc) · 897 Bytes
/
prepare-commit-msg.js
File metadata and controls
executable file
·32 lines (28 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env node
const { promisify } = require('util');
const childProcess = require('child_process');
const fs = require('fs');
const exec = promisify(childProcess.exec);
// ex WP-1234
const branchRegex = /([A-Z]+-)(\d+)/;
// ex TICKET: WP-1234
const commitRegex = /ticket:\s(\S+)/gim;
async function main() {
const commitMsgFilepath = process.argv[2];
try {
const branch = (await exec(`git branch --show-current`)).stdout.trim();
const found = branch.match(branchRegex);
// Do not append message if branch name does not match regex
if (!found.length) {
return;
}
const ticket = found[0];
const data = fs.readFileSync(commitMsgFilepath, 'utf8');
// Exit if ticket is already in commit footer
if (data.match(commitRegex)) {
return;
}
fs.writeFileSync(commitMsgFilepath, `${data}\nTICKET: ${ticket}\n`);
} catch (e) {}
}
main();