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
|
#!/usr/bin/env pwsh -c
<#
.DESCRIPTION
Create local branch of the given repo and attempt to push changes. The push may fail if
there has been other changes pushed to the same branch, if so, fetch, rebase and try again.
.PARAMETER PRBranchName
The name of the github branch the changes are being put into
.PARAMETER CommitMsg
The message for this particular commit
.PARAMETER GitUrl
The GitHub repository URL
.PARAMETER PushArgs
Optional arguments to the push command
#>
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[string] $PRBranchName,
[Parameter(Mandatory = $true)]
[string] $CommitMsg,
[Parameter(Mandatory = $true)]
[string] $GitUrl,
[Parameter(Mandatory = $false)]
[string] $PushArgs = "",
[Parameter(Mandatory = $false)]
[string] $RemoteName = "azure-sdk-fork",
[Parameter(Mandatory = $false)]
[boolean] $SkipCommit = $false,
[Parameter(Mandatory = $false)]
[boolean] $AmendCommit = $false
)
# This is necessary because of the git command output writing to stderr.
# Without explicitly setting the ErrorActionPreference to continue the script
# would fail the first time git wrote command output.
$ErrorActionPreference = "Continue"
if ((git remote) -contains $RemoteName)
{
Write-Host "git remote get-url $RemoteName"
$remoteUrl = git remote get-url $RemoteName
if ($remoteUrl -ne $GitUrl)
{
Write-Error "Remote with name $RemoteName already exists with an incompatible url [$remoteUrl] which should be [$GitUrl]."
exit 1
}
}
else
{
Write-Host "git remote add $RemoteName $GitUrl"
git remote add $RemoteName $GitUrl
if ($LASTEXITCODE -ne 0)
{
Write-Error "Unable to add remote LASTEXITCODE=$($LASTEXITCODE), see command output above."
exit $LASTEXITCODE
}
}
# Checkout to $PRBranch, create new one if not exists.
git show-ref --verify --quiet refs/heads/$PRBranchName
if ($LASTEXITCODE -eq 0) {
Write-Host "git checkout $PRBranchName."
git checkout $PRBranchName
}
else {
Write-Host "git checkout -b $PRBranchName."
git checkout -b $PRBranchName
}
if ($LASTEXITCODE -ne 0)
{
Write-Error "Unable to create branch LASTEXITCODE=$($LASTEXITCODE), see command output above."
exit $LASTEXITCODE
}
if (!$SkipCommit) {
if ($AmendCommit) {
$amendOption = "--amend"
}
else {
$amendOption = ""
}
Write-Host "git -c user.name=`"azure-sdk`" -c user.email=`"azuresdk@microsoft.com`" commit $amendOption -am `"$($CommitMsg)`""
git -c user.name="azure-sdk" -c user.email="azuresdk@microsoft.com" commit $amendOption -am "$($CommitMsg)"
if ($LASTEXITCODE -ne 0)
{
Write-Error "Unable to add files and create commit LASTEXITCODE=$($LASTEXITCODE), see command output above."
exit $LASTEXITCODE
}
}
else {
Write-Host "Skipped applying commit"
}
# The number of retries can be increased if necessary. In theory, the number of retries
# should be the max number of libraries in the largest pipeline -1 as everything except
# the first commit could hit issues and need to rebase. The reason this isn't set to that
# is because the largest pipeline is cognitive services which has 18 libraries in its
# pipeline and that just seemed a bit too large and 10 seemed like a good starting value.
$numberOfRetries = 10
$needsRetry = $false
$tryNumber = 0
do
{
$needsRetry = $false
Write-Host "git push $RemoteName $PRBranchName $PushArgs"
git push $RemoteName $PRBranchName $PushArgs
$tryNumber++
if ($LASTEXITCODE -ne 0)
{
$needsRetry = $true
Write-Host "Git push failed with LASTEXITCODE=$($LASTEXITCODE) Need to fetch and rebase: attempt number=$($tryNumber)"
Write-Host "git fetch $RemoteName $PRBranchName"
# Full fetch will fail when the repo is in a sparse-checkout state, and single branch fetch is faster anyway.
git fetch $RemoteName $PRBranchName
if ($LASTEXITCODE -ne 0)
{
Write-Error "Unable to fetch remote LASTEXITCODE=$($LASTEXITCODE), see command output above."
exit $LASTEXITCODE
}
try
{
$TempPatchFile = New-TemporaryFile
Write-Host "git diff ${PRBranchName}~ ${PRBranchName} --output $TempPatchFile"
git diff ${PRBranchName}~ ${PRBranchName} --output $TempPatchFile
if ($LASTEXITCODE -ne 0)
{
Write-Error "Unable to create diff file LASTEXITCODE=$($LASTEXITCODE), see command output above."
continue
}
Write-Host "git reset --hard $RemoteName/${PRBranchName}"
git reset --hard $RemoteName/${PRBranchName}
if ($LASTEXITCODE -ne 0)
{
Write-Error "Unable to hard reset branch LASTEXITCODE=$($LASTEXITCODE), see command output above."
continue
}
# -C0 means to use no extra before or after lines of context to enable us to avoid adjacent line merge conflicts
Write-Host "git apply -C0 $TempPatchFile"
git apply -C0 $TempPatchFile
if ($LASTEXITCODE -ne 0)
{
Write-Error "Unable to apply diff file LASTEXITCODE=$($LASTEXITCODE), see command output above."
exit $LASTEXITCODE
}
Write-Host "git add -A"
git add -A
if ($LASTEXITCODE -ne 0)
{
Write-Error "Unable to git add LASTEXITCODE=$($LASTEXITCODE), see command output above."
continue
}
Write-Host "git -c user.name=`"azure-sdk`" -c user.email=`"azuresdk@microsoft.com`" commit -m `"$($CommitMsg)`""
git -c user.name="azure-sdk" -c user.email="azuresdk@microsoft.com" commit -m "$($CommitMsg)"
if ($LASTEXITCODE -ne 0)
{
Write-Error "Unable to commit LASTEXITCODE=$($LASTEXITCODE), see command output above."
continue
}
}
finally
{
if ( Test-Path $TempPatchFile )
{
Remove-Item $TempPatchFile
}
}
}
} while($needsRetry -and $tryNumber -le $numberOfRetries)
if ($LASTEXITCODE -ne 0 -or $tryNumber -gt $numberOfRetries)
{
Write-Error "Unable to push commit after $($tryNumber) retries LASTEXITCODE=$($LASTEXITCODE), see command output above."
if (0 -eq $LASTEXITCODE)
{
exit 1
}
exit $LASTEXITCODE
}
|