perf(gitrepo): 改用 shallow 檔案判斷避免額外 git 子行程

This commit is contained in:
Jeffery
2026-07-28 14:02:06 +08:00
parent 3c1cd55444
commit db83b54c2d
+12 -2
View File
@@ -1,6 +1,7 @@
'use strict'; 'use strict';
const { execFileSync } = require('child_process'); const { execFileSync } = require('child_process');
const fs = require('fs');
const { assertSafeBranchRef } = require('./gitref'); const { assertSafeBranchRef } = require('./gitref');
// git 操作工具:一律以 execFileSync 呼叫 git(不經 shell,避免注入),輸出以 UTF-8 回傳。 // 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 第一行)。 * 取得目前 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 base', 'fetch', '--no-tags', '--deepen=1000', 'origin', `+refs/heads/${baseRef}:refs/remotes/${remoteBase}`],
['deepen PR HEAD', 'fetch', '--no-tags', '--deepen=1000', 'origin', headSha], ['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']); 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.headSha] - PR head 的 commit SHA;有提供且與目前 HEAD 不同時會先 detach 到此 commit。可省略(falsy 時不 detach,直接於目前 HEAD 上 commit)。
* @param {string} options.message - commit 訊息。 * @param {string} options.message - commit 訊息。
* @param {string[]} options.files - 要加入 commit 的檔案路徑清單(相對 repo 根目錄);全數無實際變更時不 commit、回傳 false。 * @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.serverUrl - Gitea 伺服器根網址(例如 https://gitea.example.com),須為合法 URL。
* @param {string} options.repository - repo 完整名稱(owner/repo 格式),與 serverUrl 組成 clone URL。 * @param {string} options.repository - repo 完整名稱(owner/repo 格式),與 serverUrl 組成 clone URL。
* @returns {boolean} true=有變更且已 commit 並 push 到來源分支;false=暫存區與 HEAD 無差異,略過 commit/push。 * @returns {boolean} true=有變更且已 commit 並 push 到來源分支;false=暫存區與 HEAD 無差異,略過 commit/push。