From 8dfce93271b935765aaad0bfc09e7b0b2feedcd4 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Thu, 16 Jul 2026 11:08:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E8=AE=80=E5=8F=96=E7=89=88=E8=99=9F):=20g?= =?UTF-8?q?it=20tag=20=E8=AE=80=E5=8F=96=E5=A4=B1=E6=95=97=E6=99=82?= =?UTF-8?q?=E8=BC=B8=E5=87=BA=20ERR=20=E4=B8=A6=E6=94=B9=E4=BB=A5=E8=B5=B7?= =?UTF-8?q?=E5=A7=8B=E7=89=88=E8=99=9F=200.0.1=20=E4=BD=9C=E7=82=BA?= =?UTF-8?q?=E9=A0=90=E8=A8=AD=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5 --- src/index.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/src/index.js b/src/index.js index 494e503..f88ae1a 100644 --- a/src/index.js +++ b/src/index.js @@ -18,7 +18,8 @@ process.on('unhandledRejection', (reason) => { * 同步執行 git 指令並回傳標準輸出。 * * 先以 TRC 等級記錄實際執行的指令字串,再透過 execFileSync 同步執行, - * 回傳去除前後空白的 stdout 內容。 + * 回傳去除前後空白的 stdout 內容。stderr 不直通主控台(擷取到 error.stderr), + * 失敗時由呼叫端以統一格式輸出,避免與格式化訊息重複。 * * @param {string[]} args - git 指令的引數陣列(不含 'git' 本身),例如 ['tag', '--list'] * @param {string} stage - 日誌用的流程階段名稱,例如 '讀取版號' @@ -31,7 +32,7 @@ process.on('unhandledRejection', (reason) => { */ function git(args, stage) { logger.trc(`執行指令:git ${args.join(' ')}`, stage); - return execFileSync('git', args, { encoding: 'utf8' }).trim(); + return execFileSync('git', args, { encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim(); } /** @@ -69,22 +70,34 @@ function readIsBeta() { *(容器內以 root 執行而 workspace 屬於其他使用者時,git 會拒絕存取), * 再列出全部 tag、逐一以 parse() 解析並以 compare() 取最大版號; * 不符合版號格式的 tag 以 DBG 記錄後略過。 + * git tag 讀取失敗(例如 workspace 不是 git repository、workflow 未先執行 + * actions/checkout)時不中止程序:輸出 ERR 說明原因與修正方式後回傳 null, + * 讓呼叫端改以起始版號(0.0.1)計算。 * * @returns {?{major: number, minor: number, patch: number, beta: ?number}} - * 最新版號物件;找不到任何符合格式的 tag 時輸出 WRN 並回傳 null(呼叫端從起始版號計算) - * @throws {Error} git 指令執行失敗時,由 git() 拋出 + * 最新版號物件;找不到任何符合格式的 tag、或 git tag 讀取失敗時回傳 null + * (呼叫端從起始版號計算) * * @example * const latest = findLatestVersion(); * // tag 有 1.2.3、1.2.4-beta.3 時 => { major: 1, minor: 2, patch: 4, beta: 3 } + * // workspace 不是 git repo 時 => null,並輸出: + * // [讀取版號][ERR][時間]: 無法讀取 git tag:fatal: not a git repository ... */ function findLatestVersion() { const stage = '讀取版號'; const workspace = process.env.GITHUB_WORKSPACE || process.cwd(); - git(['config', '--global', '--add', 'safe.directory', workspace], stage); - - const output = git(['-C', workspace, 'tag', '--list'], stage); + let output; + try { + git(['config', '--global', '--add', 'safe.directory', workspace], stage); + output = git(['-C', workspace, 'tag', '--list'], stage); + } catch (error) { + const detail = (error.stderr || error.message || String(error)).toString().trim().replace(/\s*\n\s*/g, ';'); + logger.err(`無法讀取 ${workspace} 的 git tag:${detail}`, stage); + logger.wrn('請確認 workflow 已先執行 actions/checkout(fetch-depth: 0);本次改以起始版號計算', stage); + return null; + } const tags = output === '' ? [] : output.split('\n'); logger.inf(`git tag 共 ${tags.length} 筆`, stage);