salix-front/.husky/addReferenceTag.js

34 lines
991 B
JavaScript
Raw Normal View History

2025-01-22 09:57:59 +00:00
import { existsSync, readFileSync, writeFileSync } from 'fs';
import { join, resolve } from 'path';
2024-08-08 11:59:45 +00:00
function getCurrentBranchName(p = process.cwd()) {
2025-01-22 09:57:59 +00:00
if (!existsSync(p)) return false;
2024-08-08 11:59:45 +00:00
2025-01-22 09:57:59 +00:00
const gitHeadPath = join(p, '.git', 'HEAD');
2024-08-08 11:59:45 +00:00
2025-01-22 09:57:59 +00:00
if (!existsSync(gitHeadPath)) {
return getCurrentBranchName(resolve(p, '..'));
}
2024-08-08 11:59:45 +00:00
2025-01-22 09:57:59 +00:00
const headContent = readFileSync(gitHeadPath, 'utf-8');
2024-08-08 11:59:45 +00:00
return headContent.trim().split('/')[2];
}
const branchName = getCurrentBranchName();
if (branchName) {
2025-01-22 09:57:59 +00:00
const msgPath = '.git/COMMIT_EDITMSG';
const msg = readFileSync(msgPath, 'utf-8');
2024-08-08 11:59:45 +00:00
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(':');
2025-01-22 09:57:59 +00:00
writeFileSync(msgPath, finalMsg);
2024-08-08 11:59:45 +00:00
}
}
}