@@ -175,6 +175,10 @@ filter New-GitHubRepositoryBranch
175
175
. PARAMETER TargetBranchName
176
176
Name of the branch to be created.
177
177
178
+ . PARAMETER Sha
179
+ The SHA1 value of the commit that this branch should be based on.
180
+ If not specified, will use the head of BranchName.
181
+
178
182
. PARAMETER AccessToken
179
183
If provided, this will be used as the AccessToken for authentication with the
180
184
REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated.
@@ -212,6 +216,17 @@ filter New-GitHubRepositoryBranch
212
216
$repo | New-GitHubRepositoryBranch -TargetBranchName new-branch
213
217
214
218
You can also pipe in a repo that was returned from a previous command.
219
+
220
+ . EXAMPLE
221
+ $branch = Get-GitHubRepositoryBranch -OwnerName microsoft -RepositoryName PowerShellForGitHub -BranchName main
222
+ $branch | New-GitHubRepositoryBranch -TargetBranchName beta
223
+
224
+ You can also pipe in a branch that was returned from a previous command.
225
+
226
+ . EXAMPLE
227
+ New-GitHubRepositoryBranch -Uri 'https://github.com/microsoft/PowerShellForGitHub' -Sha 1c3b80b754a983f4da20e77cfb9bd7f0e4cb5da6 -TargetBranchName new-branch
228
+
229
+ You can also create a new branch based off of a specific SHA1 commit value.
215
230
#>
216
231
[CmdletBinding (
217
232
SupportsShouldProcess ,
@@ -235,6 +250,7 @@ filter New-GitHubRepositoryBranch
235
250
[Alias (' RepositoryUrl' )]
236
251
[string ] $Uri ,
237
252
253
+ [Parameter (ValueFromPipelineByPropertyName )]
238
254
[string ] $BranchName = ' master' ,
239
255
240
256
[Parameter (
@@ -243,6 +259,9 @@ filter New-GitHubRepositoryBranch
243
259
Position = 2 )]
244
260
[string ] $TargetBranchName ,
245
261
262
+ [Parameter (ValueFromPipelineByPropertyName )]
263
+ [string ] $Sha ,
264
+
246
265
[string ] $AccessToken
247
266
)
248
267
@@ -259,51 +278,55 @@ filter New-GitHubRepositoryBranch
259
278
260
279
$originBranch = $null
261
280
262
- try
263
- {
264
- $getGitHubRepositoryBranchParms = @ {
265
- OwnerName = $OwnerName
266
- RepositoryName = $RepositoryName
267
- BranchName = $BranchName
268
- }
269
- if ($PSBoundParameters.ContainsKey (' AccessToken' ))
270
- {
271
- $getGitHubRepositoryBranchParms [' AccessToken' ] = $AccessToken
272
- }
273
-
274
- Write-Log - Level Verbose " Getting $BranchName branch for sha reference"
275
- $originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms
276
- }
277
- catch
281
+ if (-not $PSBoundParameters.ContainsKey (' Sha' ))
278
282
{
279
- # Temporary code to handle current differences in exception object between PS5 and PS7
280
- $throwObject = $_
281
-
282
- if ($PSVersionTable.PSedition -eq ' Core' )
283
+ try
283
284
{
284
- if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException ] -and
285
- ($_.ErrorDetails.Message | ConvertFrom-Json ).message -eq ' Branch not found' )
285
+ $getGitHubRepositoryBranchParms = @ {
286
+ OwnerName = $OwnerName
287
+ RepositoryName = $RepositoryName
288
+ BranchName = $BranchName
289
+ }
290
+ if ($PSBoundParameters.ContainsKey (' AccessToken' ))
286
291
{
287
- $throwObject = " Origin branch $BranchName not found "
292
+ $getGitHubRepositoryBranchParms [ ' AccessToken ' ] = $AccessToken
288
293
}
294
+
295
+ Write-Log - Level Verbose " Getting $BranchName branch for sha reference"
296
+ $originBranch = Get-GitHubRepositoryBranch @getGitHubRepositoryBranchParms
297
+ $Sha = $originBranch.commit.sha
289
298
}
290
- else
299
+ catch
291
300
{
292
- if ($_.Exception.Message -like ' *Not Found*' )
301
+ # Temporary code to handle current differences in exception object between PS5 and PS7
302
+ $throwObject = $_
303
+
304
+ if ($PSVersionTable.PSedition -eq ' Core' )
293
305
{
294
- $throwObject = " Origin branch $BranchName not found"
306
+ if ($_.Exception -is [Microsoft.PowerShell.Commands.HttpResponseException ] -and
307
+ ($_.ErrorDetails.Message | ConvertFrom-Json ).message -eq ' Branch not found' )
308
+ {
309
+ $throwObject = " Origin branch $BranchName not found"
310
+ }
311
+ }
312
+ else
313
+ {
314
+ if ($_.Exception.Message -like ' *Not Found*' )
315
+ {
316
+ $throwObject = " Origin branch $BranchName not found"
317
+ }
295
318
}
296
- }
297
319
298
- Write-Log - Message $throwObject - Level Error
299
- throw $throwObject
320
+ Write-Log - Message $throwObject - Level Error
321
+ throw $throwObject
322
+ }
300
323
}
301
324
302
325
$uriFragment = " repos/$OwnerName /$RepositoryName /git/refs"
303
326
304
327
$hashBody = @ {
305
328
ref = " refs/heads/$TargetBranchName "
306
- sha = $originBranch .commit.sha
329
+ sha = $Sha
307
330
}
308
331
309
332
if (-not $PSCmdlet.ShouldProcess ($BranchName , ' Create Repository Branch' ))
@@ -1106,6 +1129,15 @@ filter Add-GitHubBranchAdditionalProperties
1106
1129
}
1107
1130
1108
1131
Add-Member - InputObject $item - Name ' BranchName' - Value $branchName - MemberType NoteProperty - Force
1132
+
1133
+ if ($null -ne $item.commit )
1134
+ {
1135
+ Add-Member - InputObject $item - Name ' Sha' - Value $item.commit.sha - MemberType NoteProperty - Force
1136
+ }
1137
+ elseif ($null -ne $item.object )
1138
+ {
1139
+ Add-Member - InputObject $item - Name ' Sha' - Value $item.object.sha - MemberType NoteProperty - Force
1140
+ }
1109
1141
}
1110
1142
1111
1143
Write-Output $item
0 commit comments