salix/.husky/addReferenceTag.js

35 lines
971 B
JavaScript

const fs = require('fs');
const path = require('path');
function getCurrentBranchName(p = process.cwd()) {
const gitHeadPath = `${p}/.git/HEAD`;
return fs.existsSync(p) ?
fs.existsSync(gitHeadPath) ?
fs.readFileSync(gitHeadPath, 'utf-8').trim().split('/')[2] :
getCurrentBranchName(path.resolve(p, '..')) :
false
}
const branchName = getCurrentBranchName();
if (branchName) {
const msgPath = `.git/COMMIT_EDITMSG`;
const msg = fs.readFileSync(msgPath, "utf-8");
const referenceTag = `refs #${branchName.match(/^\d+/)}`;
console.log('referenceTag: ', referenceTag);
console.log(msg);
if (!msg.includes(referenceTag)) {
const splitedMsg = msg.split(':');
if (splitedMsg.length > 1) {
const finalMsg = splitedMsg[0] + ':' + referenceTag + splitedMsg.slice(1).join(':');
console.log('finalMsg: ', finalMsg);
fs.writeFileSync(msgPath, finalMsg);
}
}
}