Created
November 13, 2014 20:47
-
-
Save techgaun/2569d5d672a580d93ed1 to your computer and use it in GitHub Desktop.
Prepare-commit-msg hook for github + Pivotal Tracker Integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| /* | |
| * Author: Samar Acharya <samar@techgaun.com> | |
| * Place it in .git/hooks and chmod +x .git/hooks/prepare-commit-msg | |
| * Based on http://www.ipreferjim.com/2013/04/git-prepare-commit-msg-with-node-js/ | |
| */ | |
| var exec = require('child_process').exec, | |
| util = require('util'), | |
| fs = require('fs'), | |
| contents = null, | |
| branch; | |
| if(/COMMIT_EDITMSG/g.test(process.argv[2])){ | |
| //get current branch name | |
| branch = exec("git rev-parse --abbrev-ref HEAD", | |
| function (err, stdout, stderr) { | |
| if(err){ | |
| process.exit(0); | |
| } | |
| contents = fs.readFileSync(process.argv[2]); | |
| var name = stdout.replace('* ','').replace('\n',''); | |
| if (name === 'Production') { | |
| process.exit(0); | |
| } | |
| if(name !== '(no branch)'){ | |
| //use regex to identify if commit msg already has PT ID(s) | |
| strContents = contents.toString('utf8'); | |
| if (strContents.search(/\[*#[0-9]{8}(.*#[0-9]{8})*\]/) != -1) { | |
| process.exit(0); | |
| } | |
| 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); | |
| } | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment