From db83b54c2de53adcc8ddb86a5f44a9d53ed6a122 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Tue, 28 Jul 2026 14:02:06 +0800 Subject: [PATCH] =?UTF-8?q?perf(gitrepo):=20=E6=94=B9=E7=94=A8=20shallow?= =?UTF-8?q?=20=E6=AA=94=E6=A1=88=E5=88=A4=E6=96=B7=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E9=A1=8D=E5=A4=96=20git=20=E5=AD=90=E8=A1=8C=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lib/gitrepo.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/lib/gitrepo.js b/src/lib/gitrepo.js index 9c49c50..c9f95b0 100644 --- a/src/lib/gitrepo.js +++ b/src/lib/gitrepo.js @@ -1,6 +1,7 @@ 'use strict'; const { execFileSync } = require('child_process'); +const fs = require('fs'); const { assertSafeBranchRef } = require('./gitref'); // git 操作工具:一律以 execFileSync 呼叫 git(不經 shell,避免注入),輸出以 UTF-8 回傳。 @@ -64,6 +65,15 @@ function tryGit(cwd, ...args) { } } +function isShallowRepository(cwd) { + try { + const shallowPath = gitTrim(cwd, 'rev-parse', '--git-path', 'shallow'); + return fs.existsSync(shallowPath); + } catch { + return false; + } +} + /** * 取得目前 HEAD 最新一筆 commit 的訊息標題(commit message 第一行)。 * @@ -142,7 +152,7 @@ function resolveMergeBase(cwd, baseRef) { ['deepen base', 'fetch', '--no-tags', '--deepen=1000', 'origin', `+refs/heads/${baseRef}:refs/remotes/${remoteBase}`], ['deepen PR HEAD', 'fetch', '--no-tags', '--deepen=1000', 'origin', headSha], ]; - if (gitTrim(cwd, 'rev-parse', '--is-shallow-repository') === 'true') { + if (isShallowRepository(cwd)) { strategies.push(['unshallow', 'fetch', '--no-tags', '--unshallow', 'origin']); } @@ -237,7 +247,7 @@ function fileLastUpdatedIso(cwd, file) { * @param {string} [options.headSha] - PR head 的 commit SHA;有提供且與目前 HEAD 不同時會先 detach 到此 commit。可省略(falsy 時不 detach,直接於目前 HEAD 上 commit)。 * @param {string} options.message - commit 訊息。 * @param {string[]} options.files - 要加入 commit 的檔案路徑清單(相對 repo 根目錄);全數無實際變更時不 commit、回傳 false。 - * @param {string} options.token - 具該 repo push 權限的 Gitea access token(建議為能觸發 CI 的 PAT);用於 findings commit 的認證推送。 + * @param {string} options.token - 具該 repo push 權限的 Gitea access token(建議 repo 限定、最小權限且可輪替);用於 findings commit 的認證推送。 * @param {string} options.serverUrl - Gitea 伺服器根網址(例如 https://gitea.example.com),須為合法 URL。 * @param {string} options.repository - repo 完整名稱(owner/repo 格式),與 serverUrl 組成 clone URL。 * @returns {boolean} true=有變更且已 commit 並 push 到來源分支;false=暫存區與 HEAD 無差異,略過 commit/push。