@@ -15,14 +15,14 @@ Optional arguments to the push command
1515#>
1616[CmdletBinding (SupportsShouldProcess = $true )]
1717param (
18- [Parameter (Mandatory = $true )]
19- [string ] $PRBranchName ,
18+ [Parameter (Mandatory = $false )]
19+ [string ] $PRBranchName = ' ' ,
2020
21- [Parameter (Mandatory = $true )]
22- [string ] $CommitMsg ,
21+ [Parameter (Mandatory = $false )]
22+ [string ] $CommitMsg = ' ' ,
2323
24- [Parameter (Mandatory = $true )]
25- [string ] $GitUrl ,
24+ [Parameter (Mandatory = $false )]
25+ [string ] $GitUrl = ' ' ,
2626
2727 [Parameter (Mandatory = $false )]
2828 [string ] $PushArgs = " " ,
@@ -46,151 +46,178 @@ $PSNativeCommandArgumentPassing = "Legacy"
4646# would fail the first time git wrote command output.
4747$ErrorActionPreference = " Continue"
4848
49- if ((git remote) -contains $RemoteName )
50- {
51- Write-Host " git remote get-url $RemoteName "
52- $remoteUrl = git remote get-url $RemoteName
53- if ($remoteUrl -ne $GitUrl )
54- {
55- Write-Error " Remote with name $RemoteName already exists with an incompatible url [$remoteUrl ] which should be [$GitUrl ]."
56- exit 1
57- }
58- }
59- else
60- {
61- Write-Host " git remote add $RemoteName $GitUrl "
62- git remote add $RemoteName $GitUrl
63- if ($LASTEXITCODE -ne 0 )
64- {
65- Write-Error " Unable to add remote LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
66- exit $LASTEXITCODE
67- }
68- }
69- # Checkout to $PRBranch, create new one if it does not exist.
70- git show-ref -- verify -- quiet refs/ heads/ $PRBranchName
71- if ($LASTEXITCODE -eq 0 ) {
72- Write-Host " git checkout $PRBranchName ."
73- git checkout $PRBranchName
74- }
75- else {
76- Write-Host " git checkout -b $PRBranchName ."
77- git checkout - b $PRBranchName
78- }
79- if ($LASTEXITCODE -ne 0 )
49+ function Get-ComparableGitUrl ([string ] $url )
8050{
81- Write-Error " Unable to create branch LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
82- exit $LASTEXITCODE
51+ $parsedUri = $null
52+ if ([Uri ]::TryCreate($url , [UriKind ]::Absolute, [ref ]$parsedUri ) -and ($parsedUri.Scheme -in @ (" http" , " https" )))
53+ {
54+ # Ignore credentials in remote URLs while still validating the destination.
55+ return (" {0}://{1}{2}" -f $parsedUri.Scheme.ToLowerInvariant (), $parsedUri.Host.ToLowerInvariant (), $parsedUri.AbsolutePath.TrimEnd (' /' ).ToLowerInvariant())
56+ }
57+
58+ return $url
8359}
8460
85- if (! $SkipCommit ) {
86- if ($AmendCommit ) {
87- $amendOption = " --amend"
61+ function Invoke-GitBranchPush {
62+ if ([string ]::IsNullOrWhiteSpace($PRBranchName ) -or [string ]::IsNullOrWhiteSpace($CommitMsg ) -or [string ]::IsNullOrWhiteSpace($GitUrl ))
63+ {
64+ throw " PRBranchName, CommitMsg, and GitUrl are required when running git-branch-push.ps1."
65+ }
66+
67+ if ((git remote) -contains $RemoteName )
68+ {
69+ Write-Host " git remote get-url $RemoteName "
70+ $remoteUrl = git remote get-url $RemoteName
71+ $comparableRemoteUrl = Get-ComparableGitUrl $remoteUrl
72+ $comparableGitUrl = Get-ComparableGitUrl $GitUrl
73+ if ($comparableRemoteUrl -ne $comparableGitUrl )
74+ {
75+ Write-Error " Remote with name $RemoteName already exists with an incompatible url [$remoteUrl ] which should be [$GitUrl ]."
76+ exit 1
77+ }
78+ }
79+ else
80+ {
81+ Write-Host " git remote add $RemoteName $GitUrl "
82+ git remote add $RemoteName $GitUrl
83+ if ($LASTEXITCODE -ne 0 )
84+ {
85+ Write-Error " Unable to add remote LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
86+ exit $LASTEXITCODE
87+ }
88+ }
89+ # Checkout to $PRBranch, create new one if it does not exist.
90+ git show-ref -- verify -- quiet refs/ heads/ $PRBranchName
91+ if ($LASTEXITCODE -eq 0 ) {
92+ Write-Host " git checkout $PRBranchName ."
93+ git checkout $PRBranchName
8894 }
8995 else {
90- # Explicitly set this to null so that PS command line parser doesn't try to parse pass it as " "
91- $amendOption = $null
96+ Write-Host " git checkout -b $PRBranchName . "
97+ git checkout - b $PRBranchName
9298 }
93- Write-Host " git -c user.name=`" azure-sdk`" -c user.email=`" azuresdk@microsoft.com`" commit $amendOption -am `" $CommitMsg `" "
94- git - c user.name= " azure-sdk" - c user.email= " azuresdk@microsoft.com" commit $amendOption - am " $CommitMsg "
9599 if ($LASTEXITCODE -ne 0 )
96100 {
97- Write-Error " Unable to add files and create commit LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
101+ Write-Error " Unable to create branch LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
98102 exit $LASTEXITCODE
99103 }
100- }
101- else {
102- Write-Host " Skipped applying commit"
103- }
104104
105- # The number of retries can be increased if necessary. In theory, the number of retries
106- # should be the max number of libraries in the largest pipeline -1 as everything except
107- # the first commit could hit issues and need to rebase. The reason this isn't set to that
108- # is because the largest pipeline is cognitive services which has 18 libraries in its
109- # pipeline and that just seemed a bit too large and 10 seemed like a good starting value.
110- $numberOfRetries = 10
111- $needsRetry = $false
112- $tryNumber = 0
113- do
114- {
115- $needsRetry = $false
116- Write-Host " git push $RemoteName $PRBranchName $PushArgs "
117- git push $RemoteName $PRBranchName $PushArgs
118- $tryNumber ++
119- if ($LASTEXITCODE -ne 0 )
120- {
121- $needsRetry = $true
122- Write-Host " Git push failed with LASTEXITCODE=$ ( $LASTEXITCODE ) Need to fetch and rebase: attempt number=$ ( $tryNumber ) "
123-
124- Write-Host " git fetch $RemoteName $PRBranchName "
125- # Full fetch will fail when the repo is in a sparse-checkout state, and single branch fetch is faster anyway.
126- git fetch $RemoteName $PRBranchName
105+ if (! $SkipCommit ) {
106+ if ($AmendCommit ) {
107+ $amendOption = " --amend"
108+ }
109+ else {
110+ # Explicitly set this to null so that PS command line parser doesn't try to parse pass it as ""
111+ $amendOption = $null
112+ }
113+ Write-Host " git -c user.name=`" azure-sdk`" -c user.email=`" azuresdk@microsoft.com`" commit $amendOption -am `" $CommitMsg `" "
114+ git - c user.name= " azure-sdk" - c user.email= " azuresdk@microsoft.com" commit $amendOption - am " $CommitMsg "
127115 if ($LASTEXITCODE -ne 0 )
128116 {
129- Write-Error " Unable to fetch remote LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
117+ Write-Error " Unable to add files and create commit LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
130118 exit $LASTEXITCODE
131119 }
120+ }
121+ else {
122+ Write-Host " Skipped applying commit"
123+ }
132124
133- try
125+ # The number of retries can be increased if necessary. In theory, the number of retries
126+ # should be the max number of libraries in the largest pipeline -1 as everything except
127+ # the first commit could hit issues and need to rebase. The reason this isn't set to that
128+ # is because the largest pipeline is cognitive services which has 18 libraries in its
129+ # pipeline and that just seemed a bit too large and 10 seemed like a good starting value.
130+ $numberOfRetries = 10
131+ $needsRetry = $false
132+ $tryNumber = 0
133+ do
134+ {
135+ $needsRetry = $false
136+ Write-Host " git push $RemoteName $PRBranchName $PushArgs "
137+ git push $RemoteName $PRBranchName $PushArgs
138+ $tryNumber ++
139+ if ($LASTEXITCODE -ne 0 )
134140 {
135- $TempPatchFile = New-TemporaryFile
136- Write-Host " git diff ${PRBranchName} ~ ${PRBranchName} --output $TempPatchFile "
137- git diff ${PRBranchName} ~ ${PRBranchName} -- output $TempPatchFile
138- if ($LASTEXITCODE -ne 0 )
139- {
140- Write-Error " Unable to create diff file LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
141- continue
142- }
143-
144- Write-Host " git reset --hard $RemoteName /${PRBranchName} "
145- git reset -- hard $RemoteName / ${PRBranchName}
146- if ($LASTEXITCODE -ne 0 )
147- {
148- Write-Error " Unable to hard reset branch LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
149- continue
150- }
141+ $needsRetry = $true
142+ Write-Host " Git push failed with LASTEXITCODE=$ ( $LASTEXITCODE ) Need to fetch and rebase: attempt number=$ ( $tryNumber ) "
151143
152- # -C0 means to use no extra before or after lines of context to enable us to avoid adjacent line merge conflicts
153- Write-Host " git apply -C0 $TempPatchFile "
154- git apply - C0 $TempPatchFile
144+ Write-Host " git fetch $RemoteName $PRBranchName "
145+ # Full fetch will fail when the repo is in a sparse-checkout state, and single branch fetch is faster anyway.
146+ git fetch $RemoteName $PRBranchName
155147 if ($LASTEXITCODE -ne 0 )
156148 {
157- Write-Error " Unable to apply diff file LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
149+ Write-Error " Unable to fetch remote LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
158150 exit $LASTEXITCODE
159151 }
160152
161-
162- Write-Host " git add -A"
163- git add - A
164- if ($LASTEXITCODE -ne 0 )
153+ try
165154 {
166- Write-Error " Unable to git add LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
167- continue
155+ $TempPatchFile = New-TemporaryFile
156+ Write-Host " git diff ${PRBranchName} ~ ${PRBranchName} --output $TempPatchFile "
157+ git diff ${PRBranchName} ~ ${PRBranchName} -- output $TempPatchFile
158+ if ($LASTEXITCODE -ne 0 )
159+ {
160+ Write-Error " Unable to create diff file LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
161+ continue
162+ }
163+
164+ Write-Host " git reset --hard $RemoteName /${PRBranchName} "
165+ git reset -- hard $RemoteName / ${PRBranchName}
166+ if ($LASTEXITCODE -ne 0 )
167+ {
168+ Write-Error " Unable to hard reset branch LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
169+ continue
170+ }
171+
172+ # -C0 means to use no extra before or after lines of context to enable us to avoid adjacent line merge conflicts
173+ Write-Host " git apply -C0 $TempPatchFile "
174+ git apply - C0 $TempPatchFile
175+ if ($LASTEXITCODE -ne 0 )
176+ {
177+ Write-Error " Unable to apply diff file LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
178+ exit $LASTEXITCODE
179+ }
180+
181+
182+ Write-Host " git add -A"
183+ git add - A
184+ if ($LASTEXITCODE -ne 0 )
185+ {
186+ Write-Error " Unable to git add LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
187+ continue
188+ }
189+
190+ Write-Host " git -c user.name=`" azure-sdk`" -c user.email=`" azuresdk@microsoft.com`" commit -m `" $CommitMsg `" "
191+ git - c user.name= " azure-sdk" - c user.email= " azuresdk@microsoft.com" commit - m " $CommitMsg "
192+ if ($LASTEXITCODE -ne 0 )
193+ {
194+ Write-Error " Unable to commit LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
195+ continue
196+ }
168197 }
169-
170- Write-Host " git -c user.name=`" azure-sdk`" -c user.email=`" azuresdk@microsoft.com`" commit -m `" $CommitMsg `" "
171- git - c user.name= " azure-sdk" - c user.email= " azuresdk@microsoft.com" commit - m " $CommitMsg "
172- if ($LASTEXITCODE -ne 0 )
198+ finally
173199 {
174- Write-Error " Unable to commit LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
175- continue
200+ if ( Test-Path $TempPatchFile )
201+ {
202+ Remove-Item $TempPatchFile
203+ }
176204 }
177205 }
178- finally
206+ } while ($needsRetry -and $tryNumber -le $numberOfRetries )
207+
208+ if ($LASTEXITCODE -ne 0 -or $tryNumber -gt $numberOfRetries )
209+ {
210+ Write-Error " Unable to push commit after $ ( $tryNumber ) retries LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
211+ if (0 -eq $LASTEXITCODE )
179212 {
180- if ( Test-Path $TempPatchFile )
181- {
182- Remove-Item $TempPatchFile
183- }
213+ exit 1
184214 }
215+ exit $LASTEXITCODE
185216 }
186- } while ( $needsRetry -and $tryNumber -le $numberOfRetries )
217+ }
187218
188- if ($LASTEXITCODE -ne 0 -or $tryNumber -gt $numberOfRetries )
189- {
190- Write-Error " Unable to push commit after $ ( $tryNumber ) retries LASTEXITCODE=$ ( $LASTEXITCODE ) , see command output above."
191- if (0 -eq $LASTEXITCODE )
192- {
193- exit 1
194- }
195- exit $LASTEXITCODE
219+ if ($env: PESTER_TEST_RUN -eq ' true' ) {
220+ return
196221}
222+
223+ Invoke-GitBranchPush
0 commit comments