From 70bdb523718ea3faac209393d74060993608854d Mon Sep 17 00:00:00 2001 From: pablone Date: Fri, 5 Apr 2024 14:57:52 +0200 Subject: [PATCH] fix: refs #6130 code --- .husky/addReferenceTag.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/.husky/addReferenceTag.js b/.husky/addReferenceTag.js index 1361851a6..a51d47683 100644 --- a/.husky/addReferenceTag.js +++ b/.husky/addReferenceTag.js @@ -1,21 +1,10 @@ - - 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 msg = fs.readFileSync(msgPath, 'utf-8'); const referenceTag = `refs #${branchName.match(/^\d+/)}`; console.log('referenceTag: ', referenceTag); @@ -25,10 +14,21 @@ if (branchName) { const splitedMsg = msg.split(':'); if (splitedMsg.length > 1) { - const finalMsg = splitedMsg[0] + ':' + referenceTag + splitedMsg.slice(1).join(':'); + const finalMsg = splitedMsg[0] + ': ' + referenceTag + splitedMsg.slice(1).join(':'); console.log('finalMsg: ', finalMsg); fs.writeFileSync(msgPath, finalMsg); } } } +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]; +}