Skip to content

Instantly share code, notes, and snippets.

@cakeinpanic
Created May 31, 2021 10:10
Show Gist options
  • Save cakeinpanic/2b959e36ae3427ac7dc49bb3737de9fd to your computer and use it in GitHub Desktop.
Save cakeinpanic/2b959e36ae3427ac7dc49bb3737de9fd to your computer and use it in GitHub Desktop.
prepare-commit-msg git hook
#!/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