-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish_queued_commit.ps1
More file actions
213 lines (174 loc) · 5.8 KB
/
Copy pathpublish_queued_commit.ps1
File metadata and controls
213 lines (174 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
param(
[string]$RepoRoot = $PSScriptRoot,
[string]$MainBranch = "main",
[string]$QueueBranch = "contribution-queue",
[string]$CheckCommand = "",
[switch]$DryRun,
[switch]$NoFetch,
[switch]$NoPull,
[switch]$NoPush,
[switch]$PreserveAuthorDate,
[switch]$AutoStash
)
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
function Invoke-Git {
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Args)
& git @Args
if ($LASTEXITCODE -ne 0) {
throw "git $($Args -join ' ') failed with exit code $LASTEXITCODE"
}
}
function Get-GitOutput {
param([Parameter(ValueFromRemainingArguments = $true)][string[]]$Args)
$output = & git @Args
if ($LASTEXITCODE -ne 0) {
throw "git $($Args -join ' ') failed with exit code $LASTEXITCODE"
}
return $output
}
function Assert-CleanWorkingTree {
$status = @(Get-GitOutput status --porcelain)
if ($status.Count -gt 0) {
throw "Working tree is not clean. Commit, stash, or clean local changes before publishing a queued commit."
}
}
function Save-WorkingTreeForPublish {
$status = @(Get-GitOutput status --porcelain)
if ($status.Count -eq 0) {
return $null
}
if (-not $AutoStash) {
throw "Working tree is not clean. Commit, stash, or clean local changes before publishing a queued commit."
}
$message = "auto-publish temporary stash $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
Write-Host "Working tree is not clean; stashing local changes before publishing."
Invoke-Git stash push --include-untracked -m $message
return "stash@{0}"
}
function Restore-WorkingTreeAfterPublish {
param([string]$StashRef)
if ([string]::IsNullOrWhiteSpace($StashRef)) {
return
}
Write-Host "Restoring stashed local changes after publishing."
& git stash pop $StashRef
if ($LASTEXITCODE -ne 0) {
Write-Warning "Could not restore stashed local changes automatically. The stash was kept as $StashRef."
}
}
function Get-CurrentBranch {
$branch = Get-GitOutput branch --show-current
if ($branch -is [array]) {
return $branch[0]
}
return $branch
}
function Get-NextQueuedCommit {
$commits = @(Get-GitOutput cherry $MainBranch $QueueBranch)
foreach ($commit in $commits) {
if ($commit -match '^\+\s+([0-9a-f]{40})$') {
return $Matches[1]
}
}
return $null
}
function Undo-PendingPublish {
$gitDir = Get-GitOutput rev-parse --git-dir
if ($gitDir -is [array]) {
$gitDir = $gitDir[0]
}
$cherryPickHead = Join-Path $gitDir "CHERRY_PICK_HEAD"
if (Test-Path $cherryPickHead) {
Invoke-Git cherry-pick --abort
return
}
Invoke-Git reset --hard HEAD
}
function Invoke-CheckCommand {
param([string]$Command)
if ([string]::IsNullOrWhiteSpace($Command)) {
Write-Host "No check command configured; skipping checks."
return
}
Write-Host "Running check command: $Command"
& powershell -NoProfile -ExecutionPolicy Bypass -Command $Command
if ($LASTEXITCODE -ne 0) {
throw "Check command failed with exit code $LASTEXITCODE"
}
}
Set-Location $RepoRoot
Invoke-Git rev-parse --show-toplevel | Out-Null
$originalBranch = Get-CurrentBranch
$temporaryStash = $null
try {
$branches = @(Get-GitOutput branch --list $QueueBranch)
if ($branches.Count -eq 0) {
throw "Queue branch '$QueueBranch' was not found. Create it and add one meaningful commit per queued contribution."
}
$mainBranches = @(Get-GitOutput branch --list $MainBranch)
if ($mainBranches.Count -eq 0) {
throw "Main branch '$MainBranch' was not found."
}
if ($DryRun) {
$nextCommit = Get-NextQueuedCommit
if (-not $nextCommit) {
Write-Host "No queued commits left in ${MainBranch}..${QueueBranch}."
exit 0
}
$subject = Get-GitOutput log -1 --pretty=format:%s $nextCommit
Write-Host "Next queued commit: $nextCommit $subject"
if ($PreserveAuthorDate) {
Write-Host "DryRun only: author date would be preserved from the queued commit."
} else {
Write-Host "DryRun only: author date would be reset to the publish time."
}
Write-Host "DryRun only: no fetch, pull, checkout, cherry-pick, check, or push was performed."
exit 0
}
$temporaryStash = Save-WorkingTreeForPublish
if (-not $NoFetch) {
Invoke-Git fetch --all --prune
}
Invoke-Git checkout $MainBranch
if (-not $NoPull) {
Invoke-Git pull --ff-only
}
Assert-CleanWorkingTree
$nextCommit = Get-NextQueuedCommit
if (-not $nextCommit) {
Write-Host "No queued commits left in ${MainBranch}..${QueueBranch}."
exit 0
}
$subject = Get-GitOutput log -1 --pretty=format:%s $nextCommit
Write-Host "Next queued commit: $nextCommit $subject"
try {
Invoke-Git cherry-pick --no-commit $nextCommit
Invoke-CheckCommand -Command $CheckCommand
if ($PreserveAuthorDate) {
Invoke-Git commit -C $nextCommit
} else {
Invoke-Git commit --reset-author -C $nextCommit
}
} catch {
Write-Host "Publish failed; restoring the clean working tree."
Undo-PendingPublish
throw
}
if ($NoPush) {
Write-Host "NoPush enabled. Commit was cherry-picked locally but not pushed."
} else {
Invoke-Git push
}
Write-Host "Published queued commit: $nextCommit"
} catch {
Write-Error $_
exit 1
} finally {
if (-not $DryRun -and $temporaryStash) {
Restore-WorkingTreeAfterPublish -StashRef $temporaryStash
}
if ($DryRun -and $originalBranch -and (Get-CurrentBranch) -ne $originalBranch) {
Invoke-Git checkout $originalBranch
}
}