-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrebase.ps1
More file actions
176 lines (155 loc) · 4.89 KB
/
rebase.ps1
File metadata and controls
176 lines (155 loc) · 4.89 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
param(
[string]$StartFrom = "",
[string]$BaseBranch = "main"
)
# Store the current branch name
$originalBranch = git rev-parse --abbrev-ref HEAD
Write-Output "Starting from branch: $originalBranch"
# Push the base branch to the remote repository
Write-Output "Pushing '$BaseBranch' branch to the remote repository..."
git push origin $BaseBranch
if ($LASTEXITCODE -ne 0)
{
Write-Output "Failed to push '$BaseBranch' branch. Please check the error and push manually."
exit 1
}
Write-Output "Successfully pushed '$BaseBranch' branch."
# List all branches following the "gsd-*" pattern
git fetch --prune origin | Out-Null
$allBranches = @(git branch -r --list "origin/gsd-*" --format="%(refname:short)" |
ForEach-Object { $_ -replace "^origin/", "" } |
Where-Object { $_ -ne "" } |
Sort-Object { [int]($_ -replace "gsd-", "") })
# Check if any branches are found
if ($allBranches.Count -eq 0)
{
Write-Output "No branches found matching the pattern 'gsd-*'."
exit 1
}
# Apply --StartFrom filter if provided
$branches = $allBranches
if ($StartFrom -ne "")
{
$startIndex = [Array]::IndexOf($allBranches, $StartFrom)
if ($startIndex -eq -1)
{
Write-Output "Branch '$StartFrom' not found in gsd-* branches. Available branches:"
$allBranches | ForEach-Object { Write-Output " $_" }
exit 1
}
$branches = $allBranches[$startIndex..($allBranches.Count - 1)]
Write-Output "Resuming from branch: $StartFrom"
}
# Debug output to verify branches
Write-Output "Processing the following branches:"
$branches | ForEach-Object { Write-Output " $_" }
# Check for unstaged changes and stash them if present
$status = git status --porcelain
$hasChanges = $false
if ($status)
{
Write-Output "Detected uncommitted changes. Stashing them temporarily..."
git stash push -m "Temporary stash for rebase script"
if ($LASTEXITCODE -ne 0)
{
Write-Output "Failed to stash changes. Please resolve any issues before running the script."
exit 1
}
$hasChanges = $true
Write-Output "Changes stashed successfully."
}
try
{
for ($i = 0; $i -lt $branches.Count; $i++) {
$currentBranch = $branches[$i]
# Determine parent: if resuming mid-chain, gsd-N's parent is the previous branch in the full list
if ($i -eq 0)
{
if ($StartFrom -ne "")
{
$fullIndex = [Array]::IndexOf($allBranches, $currentBranch)
$parentBranch = if ($fullIndex -eq 0)
{
$BaseBranch
}
else
{
$allBranches[$fullIndex - 1]
}
}
else
{
$parentBranch = $BaseBranch
}
}
else
{
$parentBranch = $branches[$i - 1]
}
Write-Output ""
Write-Output "[$( $i + 1 )/$( $branches.Count )] Rebasing '$currentBranch' onto '$parentBranch'..."
git checkout $currentBranch
if ($LASTEXITCODE -ne 0)
{
throw "Failed to checkout branch '$currentBranch'."
}
git rebase $parentBranch
if ($LASTEXITCODE -ne 0)
{
throw @"
Rebase failed for branch '$currentBranch'.
Resolve the conflicts, then run:
git rebase --continue
Once resolved, resume this script from where it left off with:
.\rebase-chain.ps1 -StartFrom $currentBranch
Or to abort the rebase entirely:
git rebase --abort
"@
}
Write-Output "Successfully rebased '$currentBranch' onto '$parentBranch'."
git push --force-with-lease origin $currentBranch
if ($LASTEXITCODE -ne 0)
{
throw "Push failed for branch '$currentBranch'. Please check the error and push manually."
}
Write-Output "Successfully pushed '$currentBranch'."
}
Write-Output ""
Write-Output "All branches processed successfully."
Write-Output "Switching back to original branch: $originalBranch"
git checkout $originalBranch
if ($LASTEXITCODE -ne 0)
{
throw "Failed to switch back to original branch '$originalBranch'."
}
Write-Output "Done."
}
catch
{
Write-Output ""
Write-Output "ERROR: $_"
Write-Output "Attempting to return to original branch '$originalBranch'..."
git checkout $originalBranch
if ($LASTEXITCODE -ne 0)
{
Write-Output "Warning: Failed to return to original branch '$originalBranch'."
}
exit 1
}
finally
{
if ($hasChanges)
{
Write-Output "Restoring your uncommitted changes..."
git stash pop
if ($LASTEXITCODE -ne 0)
{
Write-Output "Warning: Failed to restore stashed changes."
Write-Output "Restore manually with: git stash pop"
}
else
{
Write-Output "Successfully restored your uncommitted changes."
}
}
}