34 lines
965 B
JavaScript
34 lines
965 B
JavaScript
|
const fs = require('fs');
|
||
|
const path = require('path');
|
||
|
|
||
|
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();
|
||
|
|
||
|
if (branchName) {
|
||
|
const msgPath = `.git/COMMIT_EDITMSG`;
|
||
|
const msg = fs.readFileSync(msgPath, 'utf-8');
|
||
|
const reference = branchName.match(/^\d+/);
|
||
|
|
||
|
const referenceTag = `refs #${reference}`;
|
||
|
if (!msg.includes(referenceTag) && reference) {
|
||
|
const splitedMsg = msg.split(':');
|
||
|
|
||
|
if (splitedMsg.length > 1) {
|
||
|
const finalMsg = splitedMsg[0] + ': ' + referenceTag + splitedMsg.slice(1).join(':');
|
||
|
fs.writeFileSync(msgPath, finalMsg);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|