Skip to content

Instantly share code, notes, and snippets.

@plumpNation
Last active June 6, 2025 11:15
Show Gist options
  • Save plumpNation/ff5b2025d4fdaa043d2471af630b667c to your computer and use it in GitHub Desktop.
Save plumpNation/ff5b2025d4fdaa043d2471af630b667c to your computer and use it in GitHub Desktop.
#!/usr/bin/env -S node
// this can be used as a prepare-commit-msg
// Add it to your project source code in githooks/prepare-commit-msg
// `git config --local core.hooksPath githooks/`
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const { argv, exit } = require('process');
const { currentGitActivity } = require('./helpers');
let branchName;
// Get the root directory of the git repository
const projectRootDir = execSync('git rev-parse --show-toplevel').toString().trim();
// Setup regex patterns for jira issue numbers specific to this project
const branchPattern = /^JEFF-\d+-[a-zA-Z-_]+$/;
const commitPattern = /^JEFF-\d+/;
const messageFilePath = argv[2];
const context = argv[3];
// Check if the hook is running in a message context
if (context !== 'message') {
console.info(
`Skipping prepare-commit-msg hook because hook running in non message (${context}) context`,
);
exit(0);
}
try {
// Try to get branch name
branchName = execSync('git symbolic-ref --short HEAD 2> /dev/null').toString().trim();
if (!branchName) {
console.info('Skipping prepare-commit-msg hook because branch name could not be determined');
exit(0);
}
} catch (err) {
const activity = currentGitActivity();
console.warn('Warning: Could not determine branch name');
console.info(`Current git activity: ${activity}`);
exit(0);
}
// Get path to git commit message file
const commitMessagePath = path.join(projectRootDir, messageFilePath);
let commitMessage = fs.readFileSync(commitMessagePath, 'utf8');
// Check if branch name is correctly formatted
if (!branchPattern.test(branchName)) {
console.warn('Warning: Branch name is not correctly formatted. Please follow the pattern JEFF-\\d+-[a-zA-Z-_]+');
console.warn('Leaving commit message as is. This should be corrected before pushing to remote.')
} else {
const branchIssuePrefix = branchName.match(commitPattern)[0];
const commitIssuePrefix = commitMessage.match(commitPattern) && commitMessage.match(commitPattern)[0];
// Check if the commit message is correctly formatted
if (!commitPattern.test(commitMessage)) {
console.warn('Warning: Commit message missing issue number prefix (e.g., JEFF-\\d+).');
console.info('Prefixing commit message with branch issue number.')
commitMessage = `${branchIssuePrefix} ${commitMessage}`;
} else if (branchIssuePrefix !== commitIssuePrefix) {
console.warn('Warning: Commit message issue number does not match with the branch issue number. Prefixing with branch issue number.');
console.info('Prefixing commit message with branch issue number.')
commitMessage = `${branchIssuePrefix} ${commitMessage}`;
}
}
// Write the (possibly modified) commit message back to the file
fs.writeFileSync(commitMessagePath, commitMessage);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment