Skip to content

Instantly share code, notes, and snippets.

@faressoft
Created November 28, 2019 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save faressoft/14757e95d18954cdad573c3100fd13fc to your computer and use it in GitHub Desktop.
Save faressoft/14757e95d18954cdad573c3100fd13fc to your computer and use it in GitHub Desktop.
Commit message hook script to prefix Git commit messages with the Jira ticket Id taken from the current branch name
#!/usr/bin/env node
/**
* Commit message hook script to prefix Git commit messages
* with the Jira ticket Id taken from the current branch name
*
* # Requirements
* - git 2.9+ (which supports global hooks)
*
* # Installation
* - mkdir ~/.githooks
* - git config --global core.hooksPath ~/.githooks
* - Copy the file to ~/.githooks/commit-msg
* - chmod +x ~/.githooks commit-msg
*/
const fs = require('fs')
const commitMessage = fs.readFileSync(process.argv[2], 'utf8')
const gitHead = fs.readFileSync('.git/HEAD', 'utf8')
// Already prefixed with Jira Ticket Id
if (/^\w+\-\d+/.test(commitMessage)) {
return
}
// The HEAD pointer doesn't point to a JIRA ticket branch
if (!/\w+\-\d+/.test(gitHead)) {
return
}
fs.writeFileSync(
process.argv[2],
`${gitHead.match(/\w+\-\d+/)[0]} ${commitMessage}`,
'utf8'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment