Skip to content

Instantly share code, notes, and snippets.

@kraihn
Last active February 9, 2018 17:02
Show Gist options
  • Select an option

  • Save kraihn/56f487d328c2b975cc54 to your computer and use it in GitHub Desktop.

Select an option

Save kraihn/56f487d328c2b975cc54 to your computer and use it in GitHub Desktop.
Prefix Commit Message with Jira Issue
#!/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);
}
});
});
}
@kraihn
Copy link
Author

kraihn commented Jun 4, 2015

Based on a blog post at www.ipreferjim.com

GIT PREPARE-COMMIT-MSG WITH NODE.JS


Global Install Hook for git init

To 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

mkdir -p ~/.git-templates/hooks
curl -L https://gist.githubusercontent.com/kraihn/56f487d328c2b975cc54/raw/ > ~/.git-templates/hooks/prepare-commit-msg
chmod u+x ~/.git-templates/hooks/prepare-commit-msg
git config --global init.templatedir "~/.git-templates"

Windows PowerShell

mkdir ~\.git-templates\hooks
(New-Object System.Net.WebClient).DownloadString("https://gist.githubusercontent.com/kraihn/56f487d328c2b975cc54/raw/") | out-file -encoding "Default" -filepath "~\.git-templates\hooks\prepare-commit-msg"
(New-Object System.Net.WebClient).DownloadString("https://gist.githubusercontent.com/kraihn/1f48336016fa689df2d2/raw/") | out-file -encoding "Default" -filepath "~\.git-templates\hooks\prepare-commit-msg.cmd"
git config --global init.templatedir "~/.git-templates"

Individual Repository

To install, run the commands below from within the intended Git repository.

Linux/Mac

curl -L https://gist.githubusercontent.com/kraihn/56f487d328c2b975cc54/raw/ > .git/hooks/prepare-commit-msg
chmod u+x .git/hooks/prepare-commit-msg

Windows PowerShell

(New-Object System.Net.WebClient).DownloadString("https://gist.githubusercontent.com/kraihn/56f487d328c2b975cc54/raw/") | out-file -encoding "Default" -filepath ".git\hooks\prepare-commit-msg"
(New-Object System.Net.WebClient).DownloadString("https://gist.githubusercontent.com/kraihn/1f48336016fa689df2d2/raw/") | out-file -encoding "Default" -filepath ".git\hooks\prepare-commit-msg.cmd"

@kraihn
Copy link
Author

kraihn commented Jun 22, 2015

To install on Windows add this file as well.

https://gist.github.com/kraihn/1f48336016fa689df2d2

@nathanbedford
Copy link

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

@nathanbedford
Copy link

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

@bernardop
Copy link

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.

@kraihn
Copy link
Author

kraihn commented May 20, 2016

@bernardop, I had that issue just yesterday. Thank you for posting the fix!

@nathanbedford
Copy link

Update gist to remove old hooks first, then re-init (for Windows):
https://gist.github.com/nathanbedford/4e126e81d9ff183cf68e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment