|
| 1 | +Write-Host "Running pre-push hook in powershell..." -ForegroundColor Green |
| 2 | + |
| 3 | +# run azdev_active script and capture its output |
| 4 | +$scriptPath = Join-Path $PSScriptRoot "azdev_active.ps1" |
| 5 | +. $scriptPath |
| 6 | +if ($LASTEXITCODE -ne 0) { |
| 7 | + exit 1 |
| 8 | +} |
| 9 | + |
| 10 | +# Check if azure-cli is installed in editable mode |
| 11 | +$pipShowOutput = pip show azure-cli 2>&1 |
| 12 | +$editableLocation = if ($pipShowOutput) { |
| 13 | + $match = $pipShowOutput | Select-String "Editable project location: (.+)" |
| 14 | + if ($match) { |
| 15 | + $match.Matches.Groups[1].Value |
| 16 | + } |
| 17 | +} |
| 18 | +if ($editableLocation) { |
| 19 | + # get the parent of parent directory of the editable location |
| 20 | + $AZURE_CLI_FOLDER = Split-Path -Parent (Split-Path -Parent $editableLocation) |
| 21 | +} else { |
| 22 | + Write-Host "Error: azure-cli is not installed in editable mode. Please install it in editable mode using `azdev setup`." -ForegroundColor Red |
| 23 | + exit 1 |
| 24 | +} |
| 25 | + |
| 26 | +# Get extension repo paths and join them with spaces |
| 27 | +$Extensions = (azdev extension repo list -o tsv) -join ' ' |
| 28 | + |
| 29 | +# Fetch upstream/dev branch |
| 30 | +Write-Host "Fetching upstream/dev branch..." -ForegroundColor Green |
| 31 | +git fetch upstream dev |
| 32 | +if ($LASTEXITCODE -ne 0) { |
| 33 | + Write-Host "Error: Failed to fetch upstream/dev branch. Please run 'git remote add upstream https://github.com/Azure/azure-cli.git' first." -ForegroundColor Red |
| 34 | + exit 1 |
| 35 | +} |
| 36 | + |
| 37 | +# Check if current branch needs rebasing |
| 38 | +$mergeBase = git merge-base HEAD upstream/dev |
| 39 | +$upstreamHead = git rev-parse upstream/dev |
| 40 | +if ($mergeBase -ne $upstreamHead) { |
| 41 | + Write-Host "" |
| 42 | + Write-Host "Your branch is not up to date with upstream/dev. Please run the following commands to rebase and setup:" -ForegroundColor Yellow |
| 43 | + Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow |
| 44 | + Write-Host "git rebase upstream/dev" -ForegroundColor Yellow |
| 45 | + if ($Extensions) { |
| 46 | + Write-Host "azdev setup -c $AZURE_CLI_FOLDER -r $Extensions" -ForegroundColor Yellow |
| 47 | + } else { |
| 48 | + Write-Host "azdev setup -c $AZURE_CLI_FOLDER" -ForegroundColor Yellow |
| 49 | + } |
| 50 | + Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow |
| 51 | + Write-Host "" |
| 52 | + Write-Host "You have 5 seconds to stop the push (Ctrl+C)..." -ForegroundColor Yellow |
| 53 | + for ($i = 5; $i -gt 0; $i--) { |
| 54 | + Write-Host "`rTime remaining: $i seconds..." -NoNewline -ForegroundColor Yellow |
| 55 | + Start-Sleep -Seconds 1 |
| 56 | + } |
| 57 | + Write-Host "`rContinuing without rebase..." |
| 58 | +} |
| 59 | + |
| 60 | +# get the current branch name |
| 61 | +$currentBranch = git branch --show-current |
| 62 | + |
| 63 | +# Run command azdev lint |
| 64 | +Write-Host "Running azdev lint..." -ForegroundColor Green |
| 65 | +azdev linter --min-severity medium --repo ./ --src $currentBranch --tgt $mergeBase |
| 66 | +if ($LASTEXITCODE -ne 0) { |
| 67 | + Write-Host "Error: azdev lint check failed." -ForegroundColor Red |
| 68 | + exit 1 |
| 69 | +} |
| 70 | + |
| 71 | +# Run command azdev style |
| 72 | +Write-Host "Running azdev style..." -ForegroundColor Green |
| 73 | +azdev style --repo ./ --src $currentBranch --tgt $mergeBase |
| 74 | +if ($LASTEXITCODE -ne 0) { |
| 75 | + $error_msg = azdev style --repo ./ --src $currentBranch --tgt $mergeBase 2>&1 |
| 76 | + if ($error_msg -like "*No modules*") { |
| 77 | + Write-Host "Pre-push hook passed." -ForegroundColor Green |
| 78 | + exit 0 |
| 79 | + } |
| 80 | + Write-Host "Error: azdev style check failed." -ForegroundColor Red |
| 81 | + exit 1 |
| 82 | +} |
| 83 | + |
| 84 | +# Run command azdev test |
| 85 | +Write-Host "Running azdev test..." -ForegroundColor Green |
| 86 | +azdev test --repo ./ --src $currentBranch --tgt $mergeBase --discover --no-exitfirst --xml-path test_results.xml 2>$null |
| 87 | +if ($LASTEXITCODE -ne 0) { |
| 88 | + Write-Host "Error: azdev test check failed." -ForegroundColor Red |
| 89 | + exit 1 |
| 90 | +} else { |
| 91 | + # remove test_results.xml file |
| 92 | + Remove-Item test_results.xml |
| 93 | +} |
| 94 | + |
| 95 | +Write-Host "Pre-push hook passed." -ForegroundColor Green |
| 96 | + |
| 97 | +if ($mergeBase -ne $upstreamHead) { |
| 98 | + Write-Host "" |
| 99 | + Write-Host "Your branch is not up to date with upstream/dev. Please run the following commands to rebase code and setup:" -ForegroundColor Yellow |
| 100 | + Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow |
| 101 | + Write-Host "git rebase upstream/dev" -ForegroundColor Yellow |
| 102 | + if ($Extensions) { |
| 103 | + Write-Host "azdev setup -c $AZURE_CLI_FOLDER -r $Extensions" -ForegroundColor Yellow |
| 104 | + } else { |
| 105 | + Write-Host "azdev setup -c $AZURE_CLI_FOLDER" -ForegroundColor Yellow |
| 106 | + } |
| 107 | + Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow |
| 108 | +} |
| 109 | +exit 0 |
0 commit comments