Skip to content

Commit

Permalink
{CI} Optimize azure-cli-extensions githook (Azure#8367)
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzelin007 authored Dec 13, 2024
1 parent aa86309 commit afcd1f3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 63 deletions.
68 changes: 39 additions & 29 deletions .githooks/pre-push.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,49 @@ if ($AZURE_CLI_FOLDER) {
# Check if current branch needs rebasing
$cliMergeBase = git -C $AZURE_CLI_FOLDER merge-base HEAD upstream/dev
$cliUpstreamHead = git -C $AZURE_CLI_FOLDER rev-parse upstream/dev
Write-Host "Initial CLI_MERGE_BASE: $cliMergeBase" -ForegroundColor Cyan

if ($cliMergeBase -ne $cliUpstreamHead) {
Write-Host ""
Write-Host "Your $AZURE_CLI_FOLDER repo code is not up to date with upstream/dev. Please run the following commands to rebase and setup:" -ForegroundColor Yellow
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
Write-Host "git -C $AZURE_CLI_FOLDER rebase upstream/dev" -ForegroundColor Yellow
if ($Extensions) {
Write-Host "azdev setup -c $AZURE_CLI_FOLDER -r $Extensions" -ForegroundColor Yellow
} else {
Write-Host "azdev setup -c $AZURE_CLI_FOLDER" -ForegroundColor Yellow
Write-Host "Your $AZURE_CLI_FOLDER repo code is not up to date with upstream/dev." -ForegroundColor Yellow
Write-Host "Would you like to automatically rebase and setup? [Y/n]" -ForegroundColor Yellow

try {
$reader = [System.IO.StreamReader]::new("CON")
$input = $reader.ReadLine()
} catch {
Write-Host "Error reading input. Aborting push..." -ForegroundColor Red
exit 1
}
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
Write-Host ""
Write-Host "You have 5 seconds to stop the push (Ctrl+C)..." -ForegroundColor Yellow
for ($i = 5; $i -gt 0; $i--) {
Write-Host "`rTime remaining: $i seconds..." -NoNewline -ForegroundColor Yellow
Start-Sleep -Seconds 1

if ($input -match '^[Yy]$') {
Write-Host "Rebasing with upstream/dev..." -ForegroundColor Green
git -C $AZURE_CLI_FOLDER rebase upstream/dev
if ($LASTEXITCODE -ne 0) {
Write-Host "Rebase failed. Please resolve conflicts and try again." -ForegroundColor Red
exit 1
}
Write-Host "Rebase completed successfully." -ForegroundColor Green
$cliMergeBase = git -C $AZURE_CLI_FOLDER merge-base HEAD upstream/dev
Write-Host "Updated CLI_MERGE_BASE: $cliMergeBase" -ForegroundColor Cyan

Write-Host "Running azdev setup..." -ForegroundColor Green
if ($Extensions) {
azdev setup -c $AZURE_CLI_FOLDER -r $Extensions
} else {
azdev setup -c $AZURE_CLI_FOLDER
}
if ($LASTEXITCODE -ne 0) {
Write-Host "azdev setup failed. Please check your environment." -ForegroundColor Red
exit 1
}
Write-Host "Setup completed successfully." -ForegroundColor Green
} elseif ($input -match '^[Nn]$') {
Write-Host "Skipping rebase and setup. Continue push..." -ForegroundColor Red
} else {
Write-Host "Invalid input. Aborting push..." -ForegroundColor Red
exit 1
}
Write-Host "`rContinuing without rebase..."
}
}

Expand Down Expand Up @@ -142,19 +167,4 @@ if ($LASTEXITCODE -ne 0) {
}

Write-Host "Pre-push hook passed." -ForegroundColor Green

if ($AZURE_CLI_FOLDER) {
if ($cliMergeBase -ne $cliUpstreamHead) {
Write-Host ""
Write-Host "Your $AZURE_CLI_FOLDER repo code is not up to date with upstream/dev. Please run the following commands to rebase and setup:" -ForegroundColor Yellow
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
Write-Host "git -C $AZURE_CLI_FOLDER rebase upstream/dev" -ForegroundColor Yellow
if ($Extensions) {
Write-Host "azdev setup -c $AZURE_CLI_FOLDER -r $Extensions" -ForegroundColor Yellow
} else {
Write-Host "azdev setup -c $AZURE_CLI_FOLDER" -ForegroundColor Yellow
}
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
}
}
exit 0
67 changes: 33 additions & 34 deletions .githooks/pre-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ if [ ! -z "$EDITABLE_LOCATION" ]; then
fi

# Get extension repo paths and join them with spaces
EXTENSIONS=$(azdev extension repo list -o tsv | tr '\n' ' ')
EXTENSIONS=$(azdev extension repo list -o tsv | tr '\n' ' ' | sed 's/ $//')

# Verify if current repo is in extension repo list
CURRENT_REPO=$(pwd)
Expand Down Expand Up @@ -53,28 +53,42 @@ if [ ! -z "$AZURE_CLI_FOLDER" ]; then
# Check if current branch needs rebasing
CLI_MERGE_BASE=$(git -C "$AZURE_CLI_FOLDER" merge-base HEAD upstream/dev)
CLI_UPSTREAM_HEAD=$(git -C "$AZURE_CLI_FOLDER" rev-parse upstream/dev)
printf "\033[0;36mInitial CLI_MERGE_BASE: %s\033[0m\n" "$CLI_MERGE_BASE"

if [ "$CLI_MERGE_BASE" != "$CLI_UPSTREAM_HEAD" ]; then
printf "\n"
printf "\033[0;33mYour %s repo code is not up to date with upstream/dev. Please run the following commands to rebase and setup:\033[0m\n" "$AZURE_CLI_FOLDER"
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
printf "\033[0;33mgit -C %s rebase upstream/dev\033[0m\n" "$AZURE_CLI_FOLDER"
if [ ! -z "$EXTENSIONS" ]; then
printf "\033[0;33mazdev setup -c %s -r %s\033[0m\n" "$AZURE_CLI_FOLDER" "$EXTENSIONS"
printf "\033[1;33mYour branch is not up to date with upstream/dev.\033[0m\n"
printf "\033[1;33mWould you like to automatically rebase and setup? [Y/n]\033[0m\n"

read -r INPUT < /dev/tty
if [ "$INPUT" = "Y" ] || [ "$INPUT" = "y" ]; then
printf "\033[0;32mRebasing with upstream/dev...\033[0m\n"
git -C "$AZURE_CLI_FOLDER" rebase upstream/dev
if [ $? -ne 0 ]; then
printf "\033[0;31mRebase failed. Please resolve conflicts and try again.\033[0m\n"
exit 1
fi
printf "\033[0;32mRebase completed successfully.\033[0m\n"
CLI_MERGE_BASE=$(git -C "$AZURE_CLI_FOLDER" merge-base HEAD upstream/dev)
printf "\033[0;36mUpdated CLI_MERGE_BASE: %s\033[0m\n" "$CLI_MERGE_BASE"

printf "\033[0;32mRunning azdev setup...\033[0m\n"
if [ -n "$EXTENSIONS" ]; then
azdev setup -c "$AZURE_CLI_FOLDER" -r "$EXTENSIONS"
else
azdev setup -c "$AZURE_CLI_FOLDER"
fi
if [ $? -ne 0 ]; then
printf "\033[0;31mazdev setup failed. Please check your environment.\033[0m\n"
exit 1
fi
printf "\033[0;32mSetup completed successfully.\033[0m\n"
elif [ "$INPUT" = "N" ] || [ "$INPUT" = "n" ]; then
printf "\r\033[K\033[1;33mSkipping rebase and setup. Continue push...\033[0m\n"
else
printf "\033[0;33mazdev setup -c %s\033[0m\n" "$AZURE_CLI_FOLDER"
printf "\033[0;31mInvalid input. Aborting push...\033[0m\n"
exit 1
fi
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
printf "\n"
printf "\033[0;33mYou have 5 seconds to stop the push (Ctrl+C)...\033[0m\n"

# Using a C-style for loop instead of seq
i=5
while [ $i -ge 1 ]; do
printf "\r\033[K\033[1;33mTime remaining: %d seconds...\033[0m" $i
sleep 1
i=$((i-1))
done
printf "\rContinuing without rebase...\n"
fi
fi

Expand Down Expand Up @@ -135,19 +149,4 @@ else
fi

printf "\033[0;32mPre-push hook passed.\033[0m\n"

if [ ! -z "$AZURE_CLI_FOLDER" ]; then
if [ "$CLI_MERGE_BASE" != "$CLI_UPSTREAM_HEAD" ]; then
printf "\n"
printf "\033[0;33mYour %s repo code is not up to date with upstream/dev. Please run the following commands to rebase and setup:\033[0m\n" "$AZURE_CLI_FOLDER"
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
printf "\033[0;33mgit -C %s rebase upstream/dev\033[0m\n" "$AZURE_CLI_FOLDER"
if [ ! -z "$EXTENSIONS" ]; then
printf "\033[0;33mazdev setup -c %s -r %s\033[0m\n" "$AZURE_CLI_FOLDER" "$EXTENSIONS"
else
printf "\033[0;33mazdev setup -c %s\033[0m\n" "$AZURE_CLI_FOLDER"
fi
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
fi
fi
exit 0

0 comments on commit afcd1f3

Please sign in to comment.