From b862a50b043178e720dd97c208db051dcd482df8 Mon Sep 17 00:00:00 2001 From: JoeAllen <2494136784@qq.com> Date: Tue, 29 Sep 2026 14:16:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=8F=91=E5=B8=83=E8=84=9A=E6=9C=AC=20p?= =?UTF-8?q?s1=20=E4=BF=AE=E5=A4=8D=20$Root=20=E5=BC=95=E7=94=A8=E9=A1=BA?= =?UTF-8?q?=E5=BA=8F=E4=B8=8E=E5=87=AD=E8=AF=81=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - URL/token 推断块用到 $Root,但其赋值在块之后(首次运行即崩), 调整到最前 - git credential fill 拒收 PS/cmd 管道的 CRLF 输入(\r 使其报 missing protocol field),改为写 LF 临时文件、由 cmd 内部重定向; stderr 同样留在 cmd 内,避开 PS 5.1 在 EAP=Stop 下对原生命令 2>$null 抛 NativeCommandError 的问题 - remote 推断加 try/catch,无 remote 时走 GITEA_OWNER/REPO 环境变量 --- scripts/publish-gitea.ps1 | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/scripts/publish-gitea.ps1 b/scripts/publish-gitea.ps1 index 619072d..fac4a9e 100644 --- a/scripts/publish-gitea.ps1 +++ b/scripts/publish-gitea.ps1 @@ -47,6 +47,9 @@ try { function Die($msg) { Write-Error "错误: $msg"; exit 1 } +$Root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) +$Pkg = if ($PackageName) { $PackageName } elseif ($env:GITEA_PKG) { $env:GITEA_PKG } else { "mqttx" } + # ── 必填配置 ──────────────────────────────────────────────────────────────── # 地址:优先环境变量,否则从 git remote origin 推断 $GiteaUrl = $env:GITEA_URL @@ -66,29 +69,27 @@ $GiteaUrl = $GiteaUrl.TrimEnd("/") # (即推送用的那条 OAuth 凭证,无需另行创建 API token) $Token = $env:GITEA_TOKEN if (-not $Token) { + # 从 Git Credential Manager 取同一主机的凭证(与 git 推送同源)。 + # 注意:git credential fill 对 CRLF 管道输入敏感(PS 管道送出 \r\n + # 会被拒收),必须写 LF 临时文件、由 cmd 做重定向,stderr 也留在 cmd + # 内部(PS 5.1 在 EAP=Stop 下对原生命令的 2>$null 会抛异常)。 + $credHost = $GiteaUrl -replace "^https?://", "" + $req = Join-Path $env:TEMP ("mqttx-cred-" + [guid]::NewGuid().ToString("N") + ".txt") + [IO.File]::WriteAllText($req, "protocol=https`nhost=$credHost`n`n") try { - $env:GIT_TERMINAL_PROMPT = "0" - $env:GCM_INTERACTIVE = "Never" - $credHost = $GiteaUrl -replace "^https?://", "" - $raw = @("protocol=https", "host=$credHost", "") | git credential fill 2>$null + $raw = cmd /c "git credential fill < ""$req"" 2>nul" $Token = ($raw | Where-Object { $_ -like "password=*" }) -replace "^password=", "" } catch { $Token = $null } - finally { - Remove-Item Env:GIT_TERMINAL_PROMPT -ErrorAction SilentlyContinue - Remove-Item Env:GCM_INTERACTIVE -ErrorAction SilentlyContinue - } + finally { Remove-Item $req -ErrorAction SilentlyContinue } } if (-not $Token) { Die "缺少环境变量 GITEA_TOKEN,且无法从 git credential store 自动获取" } -$Root = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path) -$Pkg = if ($PackageName) { $PackageName } elseif ($env:GITEA_PKG) { $env:GITEA_PKG } else { "mqttx" } - # ── owner/repo:参数 > 环境变量 > git remote origin 推断 ───────────────────── if (-not $Owner) { $Owner = $env:GITEA_OWNER } if (-not $Repo) { $Repo = $env:GITEA_REPO } if (-not $Owner -or -not $Repo) { - $remote = git -C $Root remote get-url origin 2>$null + try { $remote = git -C $Root remote get-url origin 2>$null } catch { $remote = $null } if ($remote) { # 兼容 git@host:owner/repo.git 与 https://host/owner/repo.git if ($remote -match "://") {