From f7e6f1c64e90d302e83e663f054b754039dec6e3 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Fri, 26 Jun 2026 14:09:13 +0800 Subject: [PATCH] =?UTF-8?q?fix(git):=20=E8=A1=9D=E7=AA=81=E5=81=B5?= =?UTF-8?q?=E6=B8=AC=E6=9A=AB=E5=AD=98=E5=88=86=E6=94=AF=E5=8A=A0=20PID=20?= =?UTF-8?q?=E4=B8=A6=E4=BB=A5=20finally=20=E7=A2=BA=E4=BF=9D=E6=B8=85?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/lib/git.js | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/app/lib/git.js b/app/lib/git.js index 28b9bfd..eb6c880 100644 --- a/app/lib/git.js +++ b/app/lib/git.js @@ -97,25 +97,28 @@ export class Git { * @returns {{ hasConflict: boolean, files: string[] }} */ detectConflict(target, source) { - // 建立暫時的本地 target 分支,嘗試以 --no-commit 合併 source - const tmp = `__conflict_check_${target}`; + // 建立暫時的本地 target 分支,嘗試以 --no-commit 合併 source。 + // 名稱帶 PID,避免並行或前次殘留造成命名衝突。 + const tmp = `__conflict_check_${target}_${process.pid}`; this._git(['checkout', '-B', tmp, `refs/remotes/pr/${target}`]); - const merge = this._git(['merge', '--no-commit', '--no-ff', `refs/remotes/pr/${source}`]); - let hasConflict = merge.status !== 0; - let files = []; + try { + const merge = this._git(['merge', '--no-commit', '--no-ff', `refs/remotes/pr/${source}`]); + const hasConflict = merge.status !== 0; + let files = []; - if (hasConflict) { - const unmerged = this._git(['diff', '--name-only', '--diff-filter=U']); - files = unmerged.stdout.split('\n').map((s) => s.trim()).filter(Boolean); + if (hasConflict) { + const unmerged = this._git(['diff', '--name-only', '--diff-filter=U']); + files = unmerged.stdout.split('\n').map((s) => s.trim()).filter(Boolean); + } + + return { hasConflict, files }; + } finally { + // 無論成敗都還原工作區並清除暫存分支 + this._git(['merge', '--abort']); + this._git(['checkout', '--detach']); + this._git(['branch', '-D', tmp]); } - - // 還原工作區 - this._git(['merge', '--abort']); - this._git(['checkout', '--detach']); - this._git(['branch', '-D', tmp]); - - return { hasConflict, files }; } /**