|
|
|
|
@@ -35,40 +35,85 @@ function Remove-GitNoise {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Convert-GitOutputLines {
|
|
|
|
|
param($Output)
|
|
|
|
|
return @(
|
|
|
|
|
@($Output) | ForEach-Object {
|
|
|
|
|
if ($_ -is [System.Management.Automation.ErrorRecord]) { $_.ToString() }
|
|
|
|
|
else { "$_" }
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Call git.exe directly. Never wrap with `cmd /c`: on Chinese Windows, cmd
|
|
|
|
|
# re-encodes the command line through the OEM code page.
|
|
|
|
|
# Also never pass Chinese via `git -m`: Windows PowerShell 5.1 encodes native
|
|
|
|
|
# argv using the console code page, so 「正式发布」 becomes 「姝e紡鍙戝竷」
|
|
|
|
|
# in the annotated tag. Commit/tag messages go through a UTF-8 file + `-F`.
|
|
|
|
|
function Invoke-GitRaw {
|
|
|
|
|
param([Parameter(Mandatory = $true)][string[]]$GitArgs)
|
|
|
|
|
$prev = $ErrorActionPreference
|
|
|
|
|
$ErrorActionPreference = "Continue"
|
|
|
|
|
try {
|
|
|
|
|
$output = & git @GitArgs 2>&1
|
|
|
|
|
$code = $LASTEXITCODE
|
|
|
|
|
} finally {
|
|
|
|
|
$ErrorActionPreference = $prev
|
|
|
|
|
}
|
|
|
|
|
return [pscustomobject]@{
|
|
|
|
|
ExitCode = $code
|
|
|
|
|
Lines = @(Remove-GitNoise -Lines @(Convert-GitOutputLines -Output $output))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Invoke-Git {
|
|
|
|
|
param([Parameter(Mandatory = $true)][string]$Args)
|
|
|
|
|
Write-Host ">> git $Args" -ForegroundColor DarkGray
|
|
|
|
|
& cmd /c "git $Args 2>&1" | Out-Null
|
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
|
|
|
throw "git command failed: git $Args"
|
|
|
|
|
param([Parameter(Mandatory = $true)][string[]]$GitArgs)
|
|
|
|
|
Write-Host (">> git " + ($GitArgs -join " ")) -ForegroundColor DarkGray
|
|
|
|
|
$r = Invoke-GitRaw -GitArgs $GitArgs
|
|
|
|
|
if ($r.ExitCode -ne 0) {
|
|
|
|
|
throw "git command failed: git $($GitArgs -join ' ')"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Invoke-GitWithMessageFile {
|
|
|
|
|
param(
|
|
|
|
|
[Parameter(Mandatory = $true)][string[]]$GitArgsPrefix,
|
|
|
|
|
[Parameter(Mandatory = $true)][string]$Message
|
|
|
|
|
)
|
|
|
|
|
$msgFile = Join-Path ([System.IO.Path]::GetTempPath()) ("jiangchang-git-msg-" + [guid]::NewGuid().ToString("N") + ".txt")
|
|
|
|
|
$utf8NoBom = New-Object System.Text.UTF8Encoding $false
|
|
|
|
|
[System.IO.File]::WriteAllText($msgFile, ($Message.TrimEnd() + "`n"), $utf8NoBom)
|
|
|
|
|
try {
|
|
|
|
|
Invoke-Git -GitArgs (@("-c", "i18n.commitEncoding=utf-8") + $GitArgsPrefix + @("-F", $msgFile))
|
|
|
|
|
} finally {
|
|
|
|
|
Remove-Item -LiteralPath $msgFile -Force -ErrorAction SilentlyContinue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Get-GitOutput {
|
|
|
|
|
param([Parameter(Mandatory = $true)][string]$Args)
|
|
|
|
|
$output = & cmd /c "git $Args 2>&1"
|
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
|
|
|
throw "git command failed: git $Args"
|
|
|
|
|
param([Parameter(Mandatory = $true)][string[]]$GitArgs)
|
|
|
|
|
$r = Invoke-GitRaw -GitArgs $GitArgs
|
|
|
|
|
if ($r.ExitCode -ne 0) {
|
|
|
|
|
throw "git command failed: git $($GitArgs -join ' ')"
|
|
|
|
|
}
|
|
|
|
|
return @(Remove-GitNoise -Lines @($output))
|
|
|
|
|
return @($r.Lines)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Test-Repo {
|
|
|
|
|
$output = & cmd /c "git rev-parse --is-inside-work-tree 2>&1"
|
|
|
|
|
if ($LASTEXITCODE -ne 0) {
|
|
|
|
|
$r = Invoke-GitRaw -GitArgs @("rev-parse", "--is-inside-work-tree")
|
|
|
|
|
if ($r.ExitCode -ne 0) {
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
$clean = Remove-GitNoise -Lines @($output)
|
|
|
|
|
return (($clean | Select-Object -First 1) -eq "true")
|
|
|
|
|
return (($r.Lines | Select-Object -First 1) -eq "true")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Get-CurrentBranch {
|
|
|
|
|
$b = (Get-GitOutput "branch --show-current" | Select-Object -First 1).Trim()
|
|
|
|
|
$b = (Get-GitOutput -GitArgs @("branch", "--show-current") | Select-Object -First 1).Trim()
|
|
|
|
|
return $b
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Get-StatusPorcelain {
|
|
|
|
|
$lines = @(Get-GitOutput "status --porcelain")
|
|
|
|
|
$lines = @(Get-GitOutput -GitArgs @("status", "--porcelain"))
|
|
|
|
|
return $lines
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -92,7 +137,7 @@ function Parse-SemVerTag {
|
|
|
|
|
function Get-NextTag {
|
|
|
|
|
param([string]$TagPrefix)
|
|
|
|
|
|
|
|
|
|
$tags = Get-GitOutput "tag --list"
|
|
|
|
|
$tags = Get-GitOutput -GitArgs @("tag", "--list")
|
|
|
|
|
$parsed = @()
|
|
|
|
|
|
|
|
|
|
foreach ($t in $tags) {
|
|
|
|
|
@@ -181,7 +226,7 @@ function Ensure-CleanOrAutoCommit {
|
|
|
|
|
|
|
|
|
|
if ($NeedClean) {
|
|
|
|
|
Write-Host "Working tree is not clean and -RequireClean is enabled." -ForegroundColor Yellow
|
|
|
|
|
Remove-GitNoise -Lines @(& cmd /c "git status --short 2>&1") | ForEach-Object { Write-Host $_ }
|
|
|
|
|
(Invoke-GitRaw -GitArgs @("status", "--short")).Lines | ForEach-Object { Write-Host $_ }
|
|
|
|
|
throw "Abort: dirty working tree."
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -200,8 +245,8 @@ function Ensure-CleanOrAutoCommit {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Invoke-Git "add -A"
|
|
|
|
|
Invoke-Git "commit -m `"$commitMsg`""
|
|
|
|
|
Invoke-Git -GitArgs @("add", "-A")
|
|
|
|
|
Invoke-GitWithMessageFile -GitArgsPrefix @("commit") -Message $commitMsg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
@@ -223,10 +268,10 @@ try {
|
|
|
|
|
if ($DryRun) {
|
|
|
|
|
Write-Host "[DryRun] Would run: git fetch --tags --prune origin" -ForegroundColor Yellow
|
|
|
|
|
} else {
|
|
|
|
|
Invoke-Git "fetch --tags --prune origin"
|
|
|
|
|
Invoke-Git -GitArgs @("fetch", "--tags", "--prune", "origin")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$skillRoot = (Get-GitOutput "rev-parse --show-toplevel" | Select-Object -First 1).Trim()
|
|
|
|
|
$skillRoot = (Get-GitOutput -GitArgs @("rev-parse", "--show-toplevel") | Select-Object -First 1).Trim()
|
|
|
|
|
if (Test-Path -LiteralPath (Join-Path $skillRoot "SKILL.md")) {
|
|
|
|
|
Assert-SkillReleasePackagingSources -SkillRoot $skillRoot
|
|
|
|
|
}
|
|
|
|
|
@@ -235,8 +280,8 @@ try {
|
|
|
|
|
|
|
|
|
|
Ensure-CleanOrAutoCommit -DoAutoCommit:$AutoCommit -NeedClean:$RequireClean -IsDryRun:$DryRun -Msg $CommitMessage
|
|
|
|
|
|
|
|
|
|
$null = Remove-GitNoise -Lines @(& cmd /c 'git rev-parse --abbrev-ref --symbolic-full-name "@{u}" 2>&1')
|
|
|
|
|
$hasUpstream = ($LASTEXITCODE -eq 0)
|
|
|
|
|
$upstream = Invoke-GitRaw -GitArgs @("rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}")
|
|
|
|
|
$hasUpstream = ($upstream.ExitCode -eq 0)
|
|
|
|
|
|
|
|
|
|
if ($DryRun) {
|
|
|
|
|
if ($hasUpstream) {
|
|
|
|
|
@@ -246,29 +291,29 @@ try {
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if ($hasUpstream) {
|
|
|
|
|
Invoke-Git "push"
|
|
|
|
|
Invoke-Git -GitArgs @("push")
|
|
|
|
|
} else {
|
|
|
|
|
Invoke-Git "push -u origin $branch"
|
|
|
|
|
Invoke-Git -GitArgs @("push", "-u", "origin", $branch)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$nextTag = Get-NextTag -TagPrefix $Prefix
|
|
|
|
|
Write-Host "Next tag: $nextTag" -ForegroundColor Green
|
|
|
|
|
|
|
|
|
|
$existing = @(Get-GitOutput "tag --list `"$nextTag`"")
|
|
|
|
|
$existing = @(Get-GitOutput -GitArgs @("tag", "--list", $nextTag))
|
|
|
|
|
if ($existing.Length -gt 0) {
|
|
|
|
|
throw "Tag already exists: $nextTag"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($DryRun) {
|
|
|
|
|
Write-Host "[DryRun] Would run: git tag -a $nextTag -m `"$Message`"" -ForegroundColor Yellow
|
|
|
|
|
Write-Host "[DryRun] Would run: git tag -a $nextTag -F <utf8-msg-file> ($Message)" -ForegroundColor Yellow
|
|
|
|
|
Write-Host "[DryRun] Would run: git push origin $nextTag" -ForegroundColor Yellow
|
|
|
|
|
Write-Host "=== DryRun Complete ===" -ForegroundColor Cyan
|
|
|
|
|
exit 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Invoke-Git "tag -a $nextTag -m `"$Message`""
|
|
|
|
|
Invoke-Git "push origin $nextTag"
|
|
|
|
|
Invoke-GitWithMessageFile -GitArgsPrefix @("tag", "-a", $nextTag) -Message $Message
|
|
|
|
|
Invoke-Git -GitArgs @("push", "origin", $nextTag)
|
|
|
|
|
|
|
|
|
|
Write-Host "Release success: $nextTag" -ForegroundColor Green
|
|
|
|
|
Write-Host "=== Release Script Done ===" -ForegroundColor Cyan
|
|
|
|
|
|