-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-msg
More file actions
64 lines (54 loc) · 1.69 KB
/
commit-msg
File metadata and controls
64 lines (54 loc) · 1.69 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
/**
* Git hook to log commit messages to be later used for standup updates
*/
const exec = require('child_process').exec;
const fs = require('fs');
const path = require('path');
const STATUS_UPDATE_DIR = path.join(
require('os').homedir(),
'.statusupdate',
);
// opens .git/COMMIT_EDITMSG
const commitMessage = fs.readFileSync(process.argv[2]).toString().replace(/\n/g, '');
(async function() {
try {
const branchName = await getCurrentBranchName();
const repositoryName = await getRepositoryName();
logStatusUpdate({ repositoryName, commitMessage, branchName });
} catch (e) {
console.log('failed to log status update');
process.exit(0);
}
})();
function getCurrentBranchName() {
return new Promise((res, rej) => {
const command = 'git rev-parse --abbrev-ref HEAD';
exec(command, (err, stdout) => err ? rej(err) : res(stdout));
});
}
function getRepositoryName() {
return new Promise((res, rej) => {
const command = 'basename -s .git `git config --get remote.origin.url`';
exec(command, (err, stdout) => err ? rej(err) : res(stdout));
});
}
function logStatusUpdate(update) {
if (!fs.existsSync(STATUS_UPDATE_DIR)){
fs.mkdirSync(STATUS_UPDATE_DIR);
}
const padStart = (num) => num < 10 ? `0${num}` : num;
const now = new Date();
const [dd, mm, yyyy] = [
now.getDate(),
now.getMonth(),
now.getFullYear(),
].map(padStart);
const filename = `${dd}${mm}${yyyy}`;
const filepath = path.join(STATUS_UPDATE_DIR, filename);
const updates = fs.existsSync(filepath)
? JSON.parse(fs.readFileSync(filepath).toString())
: [];
updates.push(update)
fs.writeFileSync(filepath, JSON.stringify(updates));
}