34 lines
991 B
JavaScript
34 lines
991 B
JavaScript
import { existsSync, readFileSync, writeFileSync } from 'fs';
|
|
import { join, resolve } from 'path';
|
|
|
|
function getCurrentBranchName(p = process.cwd()) {
|
|
if (!existsSync(p)) return false;
|
|
|
|
const gitHeadPath = join(p, '.git', 'HEAD');
|
|
|
|
if (!existsSync(gitHeadPath)) {
|
|
return getCurrentBranchName(resolve(p, '..'));
|
|
}
|
|
|
|
const headContent = readFileSync(gitHeadPath, 'utf-8');
|
|
return headContent.trim().split('/')[2];
|
|
}
|
|
|
|
const branchName = getCurrentBranchName();
|
|
|
|
if (branchName) {
|
|
const msgPath = '.git/COMMIT_EDITMSG';
|
|
const msg = 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(':');
|
|
writeFileSync(msgPath, finalMsg);
|
|
}
|
|
}
|
|
}
|