salix/.husky/addReferenceTag.js

34 lines
965 B
JavaScript
Raw Permalink Normal View History

const fs = require('fs');
2024-04-12 11:21:37 +00:00
const path = require('path');
2024-04-05 12:13:17 +00:00
function getCurrentBranchName(p = process.cwd()) {
if (!fs.existsSync(p)) return false;
const gitHeadPath = path.join(p, '.git', 'HEAD');
if (!fs.existsSync(gitHeadPath))
return getCurrentBranchName(path.resolve(p, '..'));
const headContent = fs.readFileSync(gitHeadPath, 'utf-8');
return headContent.trim().split('/')[2];
}
const branchName = getCurrentBranchName();
2024-04-05 12:13:17 +00:00
if (branchName) {
const msgPath = `.git/COMMIT_EDITMSG`;
2024-04-05 12:57:52 +00:00
const msg = fs.readFileSync(msgPath, 'utf-8');
2024-04-12 10:04:05 +00:00
const reference = branchName.match(/^\d+/);
2024-04-05 12:13:17 +00:00
2024-04-12 10:03:34 +00:00
const referenceTag = `refs #${reference}`;
2024-04-12 11:23:36 +00:00
if (!msg.includes(referenceTag) && reference) {
2024-04-05 12:13:17 +00:00
const splitedMsg = msg.split(':');
if (splitedMsg.length > 1) {
2024-04-05 12:57:52 +00:00
const finalMsg = splitedMsg[0] + ': ' + referenceTag + splitedMsg.slice(1).join(':');
2024-04-05 12:13:17 +00:00
fs.writeFileSync(msgPath, finalMsg);
}
}
}