Created
May 31, 2021 10:10
-
-
Save cakeinpanic/2b959e36ae3427ac7dc49bb3737de9fd to your computer and use it in GitHub Desktop.
prepare-commit-msg git hook
This file contains 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 | |
let exec = require('child_process').exec, | |
fs = require('fs') | |
const messagePath = process.cwd() + '/.git/COMMIT_EDITMSG'; | |
// look for current branch name | |
exec("git branch | grep '*'", | |
function (err, output) { | |
if (err) { | |
process.exit(0); | |
} | |
const originalCommitMsg = fs.readFileSync(messagePath); | |
const branchName = output.replace('* ', '').replace('\n', ''); | |
// '(no branch)' indicates we are in a rebase or other non-HEAD scenario | |
if (branchName === '(no branch)') { | |
process.exit(0); | |
} | |
const ticketNumber = (/((CLICKUP|DEV)-?\d+)/i.exec(branchName) || [''])[0] | |
const ticketNumberInPreviousMessage = (/\[((CLICKUP|DEV)-?\d+)\]/.exec(originalCommitMsg) || [''])[1]; | |
let ticketNumberString = ''; | |
if (ticketNumberInPreviousMessage !== ticketNumber) { | |
ticketNumberString = ticketNumber ? `[${ticketNumber}]: ` : ''; | |
} | |
console.log(`branch name: ${branchName}, extracted ticket name: ${ticketNumber}`); | |
const newCommitMsg = `${ticketNumberString}${originalCommitMsg}`; | |
fs.writeFileSync(messagePath, newCommitMsg); | |
process.exit(0); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment