-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGit-Flow.ps1
More file actions
277 lines (222 loc) · 7.11 KB
/
Git-Flow.ps1
File metadata and controls
277 lines (222 loc) · 7.11 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
param(
[Parameter()]
[string]$Command,
[Parameter()]
[string]$Action,
[Parameter()]
[string]$Name,
[Parameter()]
[switch]$Major
)
$ErrorActionPreference = "Stop"
$PSNativeCommandUseErrorActionPreference = $true
$DEVELOP = "develop"
$MAIN = "main"
function GetBranches {
param(
[string]$Command
)
git branch --list "$Command/*" | ForEach-Object { $_ -replace "$Command/", "" -replace "\* ", "" }
}
Register-ArgumentCompleter -CommandName Git-Flow -ParameterName Name -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
if("finish" -eq $fakeBoundParameter.Action) {
GetBranches $fakeBoundParameter.Command | Where-Object { $_ -like "${wordToComplete}*" }
}
}
function HasRemote {
$output = git remote
return $null -ne $output | Out-Null
}
function GetLastVersion {
if(HasRemote) { git fetch --tags }
$last = git tag | Sort-Object { $_ -as [version] } | Select-Object -Last 1
$last ??= "0.0.0" -as [version]
return $last
}
function GetNextReleaseVersion {
param(
[switch]$BumpMajor
)
[version]$last = GetLastVersion
$next = $BumpMajor -or $Major ? ([version]::new($last.Major + 1, 0, 0)) : ([version]::new($last.Major, $last.Minor + 1, 0))
return $next.ToString()
}
function GetNextHotfixVersion {
[version]$last = GetLastVersion
$next = [version]::new($last.Major, $last.Minor, $last.Build + 1)
return $next.ToString()
}
function VersionNumberIsValid {
param(
[string]$Command,
[string]$Name
)
$Version = $Name -as [version]
if ($null -eq ($Version)) {
Write-Host "$Name is not a version number" -Fore Red
Write-Host "Version Format: MAJOR.MINOR.PATCH (ex. 3.2.1)" -Fore Cyan
return $false
}
if(-1 -eq $Version.Build) {
Write-Host "$Name is missing a PATCH number" -Fore Red
Write-Host "Version Format: MAJOR.MINOR.PATCH (ex. 3.2.1)" -Fore Cyan
return $false
}
if("release" -eq $Command -and 0 -ne $Version.Build){
Write-Host "Patch number should be 0 for a release" -Fore Red
Write-Host "Version Format: MAJOR.MINOR.PATCH (ex. 3.2.0)" -Fore Cyan
return $false
}
if("hotfix" -eq $Command -and 0 -eq $Version.Build){
Write-Host "Patch number should NOT be 0 for a hotfix" -Fore Red
Write-Host "Version Format: MAJOR.MINOR.PATCH (ex. 3.2.1)" -Fore Cyan
return $false
}
return $true
}
function Git-Flow {
param(
[ArgumentCompletions('feature', 'release', 'hotfix')]
[string]$Command,
[ArgumentCompletions('start', 'finish', 'version')]
[string]$Action,
[string]$Name
)
try {
switch ($Command)
{
"feature" {
switch ($Action) {
"start" {
Feature-Start $Name | Out-Null
}
"finish" {
Feature-Finish $Name | Out-Null
}
default {
Write-Host "Unknown Action:" $Action -Fore Red
Write-Host "Valid actions are: start, finish" $Command -Fore Cyan
}
}
}
"release" {
switch ($Action) {
"start" {
Release-Start $Name | Out-Null
}
"finish" {
Release-Finish $Name | Out-Null
}
"version" {
GetNextReleaseVersion
}
default {
Write-Host "Unknown Action:" $Action -Fore Red
Write-Host "Valid actions are: start, finish, version" $Command -Fore Cyan
}
}
}
"hotfix" {
switch ($Action) {
"start" {
Hotfix-Start $Name | Out-Null
}
"finish" {
Hotfix-Finish $Name | Out-Null
}
"version" {
GetNextHotfixVersion
}
default {
Write-Host "Unknown Action:" $Action -Fore Red
Write-Host "Valid actions are: start, finish, version" $Command -Fore Cyan
}
}
}
default {
Write-Host "Unknown Command:" $Command -Fore Red
Write-Host "Valid commands are: feature, release, hotfix" $Command -Fore Cyan
}
}
}
catch [System.Management.Automation.NativeCommandExitException] {
<#Do this if a terminating exception happens#>
}
}
function Feature-Start {
param([string]$Name)
$Remote = HasRemote
git checkout $DEVELOP
if($Remote) { git pull --rebase }
git checkout -b feature/$Name
}
function Feature-Finish {
param([string]$Name)
$Remote = HasRemote
git checkout feature/$Name
if($Remote) { git pull --rebase }
git checkout $DEVELOP
if($Remote) { git pull --rebase }
git merge --no-ff --no-edit feature/$Name
git branch -d feature/$Name
}
function Release-Start {
param([string]$Name)
$Name = $Name ? $Name : (GetNextReleaseVersion)
if(!(VersionNumberIsValid "release" $Name)) { return }
$Remote = HasRemote
# create release branch from develop
git checkout $DEVELOP
if($Remote) { git pull --rebase }
git checkout -b release/$Name
}
function Release-Finish {
param([string]$Name)
if(!(VersionNumberIsValid "release" $Name)) { return }
$Remote = HasRemote
# merge the release branch into main and tag it
git checkout release/$Name
if($Remote) { git pull --rebase }
git checkout $MAIN
if($Remote) { git pull --rebase }
git merge --no-ff --no-edit release/$Name
git tag -a $Name
# merge the tag into develop
git checkout $DEVELOP
if($Remote) { git pull --rebase }
git merge --no-ff --no-edit $Name
git branch -d release/$Name
}
function Hotfix-Start {
param([string]$Name)
$Name = $Name ? $Name : (GetNextHotfixVersion)
if(!(VersionNumberIsValid "hotfix" $Name)) { return }
$Remote = HasRemote
# create hotfix branch from main
git checkout $MAIN
if($Remote) { git pull --rebase }
git checkout -b hotfix/$Name
}
function Hotfix-Finish {
param([string]$Name)
if(!(VersionNumberIsValid "hotfix" $Name)) { return }
$Remote = HasRemote
# merge the hotfix branch into main and tag it
git checkout hotfix/$Name
if($Remote) { git pull --rebase }
git checkout $MAIN
if($Remote) { git pull --rebase }
git merge --no-ff --no-edit hotfix/$Name
git tag -a $Name
# merge the tag into develop
git checkout $DEVELOP
if($Remote) { git pull --rebase }
git merge --no-ff --no-edit $Name
git branch -d hotfix/$Name
}
If ($MyInvocation.InvocationName -ne ".")
{
Git-Flow $Command $Action $Name
exit $LASTEXITCODE
}