2024-04-08 06:51:13 +00:00
|
|
|
const fs = require('fs');
|
2024-04-08 06:08:17 +00:00
|
|
|
const path = require('path');
|
2024-04-05 12:13:17 +00:00
|
|
|
|
2024-04-08 06:51:13 +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];
|
|
|
|
}
|
|
|
|
|
2024-04-05 12:13:17 +00:00
|
|
|
const branchName = getCurrentBranchName();
|
2024-04-08 06:51:13 +00:00
|
|
|
|
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-05 12:13:17 +00:00
|
|
|
|
|
|
|
const referenceTag = `refs #${branchName.match(/^\d+/)}`;
|
|
|
|
if (!msg.includes(referenceTag)) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|