Skip to content

Commit 6547da8

Browse files
vaindclaude
andcommitted
refactor: simplify error handling by changing PSNativeCommandErrorActionPreference
Instead of wrapping every git command with complex error handling, simply set PSNativeCommandErrorActionPreference to false and handle $LASTEXITCODE explicitly. This is much cleaner and more maintainable: - Removes 50+ lines of complex error handling wrappers - Makes the code more readable and easier to understand - Still maintains all the same error handling behavior - All tests continue to pass Changes: - Set PSNativeCommandErrorActionPreference = $false globally - Simplified git clone/fetch/log operations to check $LASTEXITCODE directly - Removed complex try/catch/finally wrappers from Get-ChangelogFromCommits - Simplified Get-ChangelogFromDiff git diff operation - Maintained all progressive fallback and cleanup logic 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent bdb3e8d commit 6547da8

File tree

1 file changed

+24
-72
lines changed

1 file changed

+24
-72
lines changed

updater/scripts/get-changelog.ps1

Lines changed: 24 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ param(
55
)
66

77
Set-StrictMode -Version latest
8-
$PSNativeCommandErrorActionPreference = $true
8+
$PSNativeCommandErrorActionPreference = $false
99
$ErrorActionPreference = 'Stop'
1010

1111
$prefix = 'https?://(www\.)?github.com/'
@@ -53,39 +53,25 @@ function Get-ChangelogFromCommits {
5353
$repoDir = Join-Path $tmpDir 'repo'
5454
Write-Host "Cloning repository to generate changelog from commits..."
5555

56-
# Clone with error handling to prevent script termination
57-
$cloneResult = & {
58-
$oldErrorActionPreference = $ErrorActionPreference
59-
$ErrorActionPreference = 'Continue'
60-
try {
61-
# Try increasing depth if needed
62-
$depth = 200
63-
git clone --depth=$depth --no-single-branch --quiet $repoUrl $repoDir 2>&1
64-
$cloneExitCode = $LASTEXITCODE
65-
66-
# If shallow clone fails due to depth, try with more depth
67-
if ($cloneExitCode -ne 0 -and $depth -eq 200) {
68-
Write-Host "Shallow clone failed, trying with increased depth..."
69-
Remove-Item -Recurse -Force $repoDir -ErrorAction SilentlyContinue
70-
git clone --depth=1000 --no-single-branch --quiet $repoUrl $repoDir 2>&1
71-
$cloneExitCode = $LASTEXITCODE
72-
}
56+
# Try progressive fallback for cloning
57+
$depth = 200
58+
git clone --depth=$depth --no-single-branch --quiet $repoUrl $repoDir 2>&1 | Out-Null
7359

74-
# If still failing, try full clone as last resort
75-
if ($cloneExitCode -ne 0) {
76-
Write-Host "Deep clone failed, trying full clone..."
77-
Remove-Item -Recurse -Force $repoDir -ErrorAction SilentlyContinue
78-
git clone --quiet $repoUrl $repoDir 2>&1
79-
$cloneExitCode = $LASTEXITCODE
80-
}
60+
# If shallow clone fails due to depth, try with more depth
61+
if ($LASTEXITCODE -ne 0) {
62+
Write-Host "Shallow clone failed, trying with increased depth..."
63+
Remove-Item -Recurse -Force $repoDir -ErrorAction SilentlyContinue
64+
git clone --depth=1000 --no-single-branch --quiet $repoUrl $repoDir 2>&1 | Out-Null
65+
}
8166

82-
return $cloneExitCode
83-
} finally {
84-
$ErrorActionPreference = $oldErrorActionPreference
85-
}
67+
# If still failing, try full clone as last resort
68+
if ($LASTEXITCODE -ne 0) {
69+
Write-Host "Deep clone failed, trying full clone..."
70+
Remove-Item -Recurse -Force $repoDir -ErrorAction SilentlyContinue
71+
git clone --quiet $repoUrl $repoDir 2>&1 | Out-Null
8672
}
8773

88-
if ($cloneResult -ne 0) {
74+
if ($LASTEXITCODE -ne 0) {
8975
Write-Warning "Could not clone repository $repoUrl"
9076
return $null
9177
}
@@ -97,47 +83,22 @@ function Get-ChangelogFromCommits {
9783

9884
Push-Location $repoDir
9985
try {
100-
# Ensure we have both tags with error handling
101-
$fetchResult = & {
102-
$oldErrorActionPreference = $ErrorActionPreference
103-
$ErrorActionPreference = 'Continue'
104-
try {
105-
git fetch --tags --quiet 2>&1 | Out-Null
106-
return $LASTEXITCODE
107-
} finally {
108-
$ErrorActionPreference = $oldErrorActionPreference
109-
}
110-
}
111-
112-
if ($fetchResult -ne 0) {
86+
# Ensure we have both tags
87+
git fetch --tags --quiet 2>&1 | Out-Null
88+
if ($LASTEXITCODE -ne 0) {
11389
Write-Warning "Could not fetch tags from repository"
11490
return $null
11591
}
11692

117-
# Get commit messages between tags with error handling
93+
# Get commit messages between tags
11894
Write-Host "Getting commits between $oldTag and $newTag..."
119-
$commitResult = & {
120-
$oldErrorActionPreference = $ErrorActionPreference
121-
$ErrorActionPreference = 'Continue'
122-
try {
123-
$output = git log "$oldTag..$newTag" --pretty=format:'%s' 2>&1
124-
$exitCode = $LASTEXITCODE
125-
return @{
126-
Output = $output
127-
ExitCode = $exitCode
128-
}
129-
} finally {
130-
$ErrorActionPreference = $oldErrorActionPreference
131-
}
132-
}
95+
$commitMessages = git log "$oldTag..$newTag" --pretty=format:'%s' 2>&1
13396

134-
if ($commitResult.ExitCode -ne 0) {
135-
Write-Warning "Could not get commits between $oldTag and $newTag (exit code: $($commitResult.ExitCode))"
97+
if ($LASTEXITCODE -ne 0) {
98+
Write-Warning "Could not get commits between $oldTag and $newTag (exit code: $LASTEXITCODE)"
13699
return $null
137100
}
138101

139-
$commitMessages = $commitResult.Output
140-
141102
if ([string]::IsNullOrEmpty($commitMessages)) {
142103
Write-Host "No commits found between $oldTag and $newTag"
143104
return $null
@@ -192,16 +153,7 @@ function Get-ChangelogFromDiff {
192153

193154
# Generate diff using git diff --no-index
194155
# git diff returns exit code 1 when differences are found, which is expected behavior
195-
# We need to handle this properly when PSNativeCommandErrorActionPreference is enabled
196-
$fullDiff = & {
197-
$oldErrorActionPreference = $ErrorActionPreference
198-
$ErrorActionPreference = 'Continue'
199-
try {
200-
git diff --no-index $oldChangelogPath $newChangelogPath
201-
} finally {
202-
$ErrorActionPreference = $oldErrorActionPreference
203-
}
204-
}
156+
$fullDiff = git diff --no-index $oldChangelogPath $newChangelogPath
205157

206158
# The first lines are diff metadata, skip them
207159
$fullDiff = $fullDiff -split "`n" | Select-Object -Skip 4

0 commit comments

Comments
 (0)