-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepare-commit-msg
129 lines (110 loc) · 4.4 KB
/
prepare-commit-msg
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env node
/*
* Automatically adds branch code and branch description to every commit message.
*/
const exec = require('child_process').exec,
util = require('util'),
fs = require('fs'),
COMMAND_TO_GET_BRANCH_NAME = 'git branch | grep "*" | sed "s/* //"';
if (isPathToDefaultCommitMsgBeingPassedAsArg()) {
exec(COMMAND_TO_GET_BRANCH_NAME,
(err, stdout, stderr) => {
if (err) {
// git branch will fail if initial commit has not been done
process.exit(0);
}
const originalDefaultCommitMsg = readContentOfDefaultCommitMsg(),
branchName = trimExtraCharactersFromStartAndEndOfLine(stdout),
tasksCode = getTasksCodeFromBranchName(branchName);
exec(createCommandToGetBranchDescription(branchName),
(err, stdout, stderr) => {
let description = getDescription(stdout);
if (isInABranch(branchName)) {
let newContents = createNewCommitMsg(originalDefaultCommitMsg, tasksCode, description);
writeContentToCommitMsgFile(newContents);
process.exit(0);
} else {
process.exit(0);
}
});
});
}
function isInABranch(branchName) {
// "Not currently on any branch" means you have a detached head, i.e. your HEAD pointer is directly referencing a
// commit instead of symbolically pointing at the name of a branch.
// Read more here: http://stackoverflow.com/a/2498553/3094399
return branchName !== '(no branch)';
}
function isPathToDefaultCommitMsgBeingPassedAsArg() {
// path would be .git/COMMIT_EDITMSG
return /COMMIT_EDITMSG/g.test(process.argv[2]);
}
function getDescription(stdout) {
if (stdout) {
return stdout.replace(/\n/g, '');
}
return '';
}
function createCommandToGetBranchDescription(branchName) {
return 'git config branch.' + branchName + '.description';
}
function trimExtraCharactersFromStartAndEndOfLine(stdout) {
return stdout.replace('* ', '').replace('\n', '');
}
/**
* Return an array containing 1 or 2 tasks code depending on the format of the branch name.
* @param {string} branchName branch name whose format could be:
* - {type_of_branch}/{story_code}/{name_of_story}
* - {type_of_branch}/{story_code}/{subtask_code}/{name_of_subtask}
* - what you want
*/
function getTasksCodeFromBranchName(branchName) {
const branchNameParts = branchName.split('/');
if (branchNameParts.length == 3) {
// Case with only one code (story code)
return [branchNameParts[1]];
}
if (branchNameParts.length > 3) {
// Case with 2 codes (story and subtask)
return [branchNameParts[1], branchNameParts[2]];
}
return [];
}
function readContentOfDefaultCommitMsg() {
// read content from .git/COMMIT_EDITMSG
return fs.readFileSync(process.argv[2]);
}
function writeContentToCommitMsgFile(newContents) {
// write contents back to .git/COMMIT_EDITMSG
fs.writeFileSync(process.argv[2], newContents);
}
/**
* If present, prepend to the commit message the task(s) code(s) written in the branch name.
* @param {string} originalDefaultCommitMsg the orginal commit message
* @param {array} tasksCode an array containing the tasks code written in the branch name
* @param {string} description the description in the commit message
*/
function createNewCommitMsg(originalDefaultCommitMsg, tasksCode, description) {
let newContents;
let tasksCodesToPrepend = '';
if (tasksCode.length >= 1) {
const firstTaskCode = tasksCode[0];
if (originalDefaultCommitMsg.indexOf("[" + firstTaskCode + "]") == -1) {
tasksCodesToPrepend = util.format('[%s] ', firstTaskCode);
}
if (tasksCode.length >= 2) {
const secondTaskCode = tasksCode[1];
if (originalDefaultCommitMsg.indexOf("[" + secondTaskCode + "]") == -1) {
tasksCodesToPrepend = util.format('%s[%s] ', tasksCodesToPrepend, secondTaskCode);
}
}
}
if (tasksCodesToPrepend == '') {
return originalDefaultCommitMsg;
}
newContents = util.format('%s%s', tasksCodesToPrepend, originalDefaultCommitMsg);
if (description) {
newContents = util.format('%s\n\n%s', newContents, description);
}
return newContents;
}