-
-
Save kraihn/56f487d328c2b975cc54 to your computer and use it in GitHub Desktop.
| #!/usr/bin/env node | |
| var exec = require('child_process').exec, | |
| util = require('util'), | |
| fs = require('fs'), | |
| contents = null, | |
| branch, desc; | |
| // expect .git/COMMIT_EDITMSG | |
| if(/COMMIT_EDITMSG/g.test(process.argv[2])){ | |
| // look for current branch name | |
| branch = exec("git branch | grep '*'", | |
| function (err, stdout, stderr) { | |
| if(err){ | |
| // git branch will fail if initial commit has not been done, | |
| // so we can safely skip this hook | |
| process.exit(0); | |
| } | |
| // opens .git/COMMIT_EDITMSG | |
| contents = fs.readFileSync(process.argv[2]); | |
| // trims extra characters from start/end of line | |
| var name = stdout.replace('* ','').replace('\n',''); | |
| // If the branch has a description, pull that | |
| desc = exec('git config branch.'+ name +'.description', | |
| function(err, stdout, stderr){ | |
| // don't handle errors (because we write out branch anyway) | |
| // we found a description, add to 'name' | |
| if(stdout){ name = util.format('%s (%s)', name, stdout.replace(/\n/g,'')); } | |
| // '(no branch)' indicates we are in a rebase or other non-HEAD scenario | |
| if(name !== '(no branch)'){ | |
| // matches: /ABC-123, /aBc-123, /abc-123, /AbC-123, /abc-123-456 | |
| // Begins with slash because prefix is always: feature, bugfix, chore... | |
| // Project ID is always alpha | |
| // Issue ID is always numeric | |
| var jiraIssue = /\/[a-zA-Z]+([-][0-9]+)+/; | |
| if (jiraIssue.test(name)) { | |
| // Extract issue(s) from branch name | |
| var issueLine = jiraIssue.exec(name)[0].substring(1).toUpperCase().split('-'); | |
| name = [issueLine[0], issueLine[1]].join('-'); | |
| // Append for multiple issues | |
| for(var i = 2; i < issueLine.length; i++) { | |
| name += ',' + [issueLine[0], issueLine[i]].join('-'); | |
| } | |
| } | |
| else { | |
| process.exit(0); | |
| } | |
| // Append name to original contents. | |
| contents = util.format('[%s] %s', name, contents); | |
| // write contents back out to .git/COMMIT_EDITMSG | |
| fs.writeFileSync(process.argv[2], contents); | |
| process.exit(0); | |
| } else { | |
| process.exit(0); | |
| } | |
| }); | |
| }); | |
| } |
To install on Windows add this file as well.
Nice! I wrote a little Powershell gist to help reinitialize all repositories in a given folder. Hope it helps someone: https://gist.github.com/nathanbedford/4e126e81d9ff183cf68e
Also, on the Mac, you may need to create a symlink to the actual node binary after running the above:
sudo ln -s /usr/local/bin/node /usr/bin/node
See this for more details: http://stackoverflow.com/a/20077620
I've seen some cases when referencing multiple jira issues, the second issue's id gets truncated. I believe the issue is in line 43:
var issueLine = jiraIssue.exec(name)[0].substring(1, name.indexOf('-')).toUpperCase().split('-');
It should be changed to:
var issueLine = jiraIssue.exec(name)[0].substring(1).toUpperCase().split('-');
The result of jiraIssue.exec(name)[0] has already gotten rid of the rest of the message (everything after the id's), so there's no need of passing a second parameter to substring.
@bernardop, I had that issue just yesterday. Thank you for posting the fix!
Update gist to remove old hooks first, then re-init (for Windows):
https://gist.github.com/nathanbedford/4e126e81d9ff183cf68e
Based on a blog post at www.ipreferjim.com
GIT PREPARE-COMMIT-MSG WITH NODE.JS
Global Install Hook for
git initTo install in a repository, run
git init. Also works on existing repositories, but doesn't overwrite hooks already in the destination repository.Linux/Mac shell
Windows PowerShell
Individual Repository
To install, run the commands below from within the intended Git repository.
Linux/Mac
Windows PowerShell