Skip to content

Commit afcd1f3

Browse files
authored
{CI} Optimize azure-cli-extensions githook (#8367)
1 parent aa86309 commit afcd1f3

File tree

2 files changed

+72
-63
lines changed

2 files changed

+72
-63
lines changed

.githooks/pre-push.ps1

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,49 @@ if ($AZURE_CLI_FOLDER) {
5858
# Check if current branch needs rebasing
5959
$cliMergeBase = git -C $AZURE_CLI_FOLDER merge-base HEAD upstream/dev
6060
$cliUpstreamHead = git -C $AZURE_CLI_FOLDER rev-parse upstream/dev
61+
Write-Host "Initial CLI_MERGE_BASE: $cliMergeBase" -ForegroundColor Cyan
62+
6163
if ($cliMergeBase -ne $cliUpstreamHead) {
6264
Write-Host ""
63-
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
64-
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
65-
Write-Host "git -C $AZURE_CLI_FOLDER rebase upstream/dev" -ForegroundColor Yellow
66-
if ($Extensions) {
67-
Write-Host "azdev setup -c $AZURE_CLI_FOLDER -r $Extensions" -ForegroundColor Yellow
68-
} else {
69-
Write-Host "azdev setup -c $AZURE_CLI_FOLDER" -ForegroundColor Yellow
65+
Write-Host "Your $AZURE_CLI_FOLDER repo code is not up to date with upstream/dev." -ForegroundColor Yellow
66+
Write-Host "Would you like to automatically rebase and setup? [Y/n]" -ForegroundColor Yellow
67+
68+
try {
69+
$reader = [System.IO.StreamReader]::new("CON")
70+
$input = $reader.ReadLine()
71+
} catch {
72+
Write-Host "Error reading input. Aborting push..." -ForegroundColor Red
73+
exit 1
7074
}
71-
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
72-
Write-Host ""
73-
Write-Host "You have 5 seconds to stop the push (Ctrl+C)..." -ForegroundColor Yellow
74-
for ($i = 5; $i -gt 0; $i--) {
75-
Write-Host "`rTime remaining: $i seconds..." -NoNewline -ForegroundColor Yellow
76-
Start-Sleep -Seconds 1
75+
76+
if ($input -match '^[Yy]$') {
77+
Write-Host "Rebasing with upstream/dev..." -ForegroundColor Green
78+
git -C $AZURE_CLI_FOLDER rebase upstream/dev
79+
if ($LASTEXITCODE -ne 0) {
80+
Write-Host "Rebase failed. Please resolve conflicts and try again." -ForegroundColor Red
81+
exit 1
82+
}
83+
Write-Host "Rebase completed successfully." -ForegroundColor Green
84+
$cliMergeBase = git -C $AZURE_CLI_FOLDER merge-base HEAD upstream/dev
85+
Write-Host "Updated CLI_MERGE_BASE: $cliMergeBase" -ForegroundColor Cyan
86+
87+
Write-Host "Running azdev setup..." -ForegroundColor Green
88+
if ($Extensions) {
89+
azdev setup -c $AZURE_CLI_FOLDER -r $Extensions
90+
} else {
91+
azdev setup -c $AZURE_CLI_FOLDER
92+
}
93+
if ($LASTEXITCODE -ne 0) {
94+
Write-Host "azdev setup failed. Please check your environment." -ForegroundColor Red
95+
exit 1
96+
}
97+
Write-Host "Setup completed successfully." -ForegroundColor Green
98+
} elseif ($input -match '^[Nn]$') {
99+
Write-Host "Skipping rebase and setup. Continue push..." -ForegroundColor Red
100+
} else {
101+
Write-Host "Invalid input. Aborting push..." -ForegroundColor Red
102+
exit 1
77103
}
78-
Write-Host "`rContinuing without rebase..."
79104
}
80105
}
81106

@@ -142,19 +167,4 @@ if ($LASTEXITCODE -ne 0) {
142167
}
143168

144169
Write-Host "Pre-push hook passed." -ForegroundColor Green
145-
146-
if ($AZURE_CLI_FOLDER) {
147-
if ($cliMergeBase -ne $cliUpstreamHead) {
148-
Write-Host ""
149-
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
150-
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
151-
Write-Host "git -C $AZURE_CLI_FOLDER rebase upstream/dev" -ForegroundColor Yellow
152-
if ($Extensions) {
153-
Write-Host "azdev setup -c $AZURE_CLI_FOLDER -r $Extensions" -ForegroundColor Yellow
154-
} else {
155-
Write-Host "azdev setup -c $AZURE_CLI_FOLDER" -ForegroundColor Yellow
156-
}
157-
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
158-
}
159-
}
160170
exit 0

.githooks/pre-push.sh

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ if [ ! -z "$EDITABLE_LOCATION" ]; then
1616
fi
1717

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

2121
# Verify if current repo is in extension repo list
2222
CURRENT_REPO=$(pwd)
@@ -53,28 +53,42 @@ if [ ! -z "$AZURE_CLI_FOLDER" ]; then
5353
# Check if current branch needs rebasing
5454
CLI_MERGE_BASE=$(git -C "$AZURE_CLI_FOLDER" merge-base HEAD upstream/dev)
5555
CLI_UPSTREAM_HEAD=$(git -C "$AZURE_CLI_FOLDER" rev-parse upstream/dev)
56+
printf "\033[0;36mInitial CLI_MERGE_BASE: %s\033[0m\n" "$CLI_MERGE_BASE"
57+
5658
if [ "$CLI_MERGE_BASE" != "$CLI_UPSTREAM_HEAD" ]; then
5759
printf "\n"
58-
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"
59-
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
60-
printf "\033[0;33mgit -C %s rebase upstream/dev\033[0m\n" "$AZURE_CLI_FOLDER"
61-
if [ ! -z "$EXTENSIONS" ]; then
62-
printf "\033[0;33mazdev setup -c %s -r %s\033[0m\n" "$AZURE_CLI_FOLDER" "$EXTENSIONS"
60+
printf "\033[1;33mYour branch is not up to date with upstream/dev.\033[0m\n"
61+
printf "\033[1;33mWould you like to automatically rebase and setup? [Y/n]\033[0m\n"
62+
63+
read -r INPUT < /dev/tty
64+
if [ "$INPUT" = "Y" ] || [ "$INPUT" = "y" ]; then
65+
printf "\033[0;32mRebasing with upstream/dev...\033[0m\n"
66+
git -C "$AZURE_CLI_FOLDER" rebase upstream/dev
67+
if [ $? -ne 0 ]; then
68+
printf "\033[0;31mRebase failed. Please resolve conflicts and try again.\033[0m\n"
69+
exit 1
70+
fi
71+
printf "\033[0;32mRebase completed successfully.\033[0m\n"
72+
CLI_MERGE_BASE=$(git -C "$AZURE_CLI_FOLDER" merge-base HEAD upstream/dev)
73+
printf "\033[0;36mUpdated CLI_MERGE_BASE: %s\033[0m\n" "$CLI_MERGE_BASE"
74+
75+
printf "\033[0;32mRunning azdev setup...\033[0m\n"
76+
if [ -n "$EXTENSIONS" ]; then
77+
azdev setup -c "$AZURE_CLI_FOLDER" -r "$EXTENSIONS"
78+
else
79+
azdev setup -c "$AZURE_CLI_FOLDER"
80+
fi
81+
if [ $? -ne 0 ]; then
82+
printf "\033[0;31mazdev setup failed. Please check your environment.\033[0m\n"
83+
exit 1
84+
fi
85+
printf "\033[0;32mSetup completed successfully.\033[0m\n"
86+
elif [ "$INPUT" = "N" ] || [ "$INPUT" = "n" ]; then
87+
printf "\r\033[K\033[1;33mSkipping rebase and setup. Continue push...\033[0m\n"
6388
else
64-
printf "\033[0;33mazdev setup -c %s\033[0m\n" "$AZURE_CLI_FOLDER"
89+
printf "\033[0;31mInvalid input. Aborting push...\033[0m\n"
90+
exit 1
6591
fi
66-
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
67-
printf "\n"
68-
printf "\033[0;33mYou have 5 seconds to stop the push (Ctrl+C)...\033[0m\n"
69-
70-
# Using a C-style for loop instead of seq
71-
i=5
72-
while [ $i -ge 1 ]; do
73-
printf "\r\033[K\033[1;33mTime remaining: %d seconds...\033[0m" $i
74-
sleep 1
75-
i=$((i-1))
76-
done
77-
printf "\rContinuing without rebase...\n"
7892
fi
7993
fi
8094

@@ -135,19 +149,4 @@ else
135149
fi
136150

137151
printf "\033[0;32mPre-push hook passed.\033[0m\n"
138-
139-
if [ ! -z "$AZURE_CLI_FOLDER" ]; then
140-
if [ "$CLI_MERGE_BASE" != "$CLI_UPSTREAM_HEAD" ]; then
141-
printf "\n"
142-
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"
143-
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
144-
printf "\033[0;33mgit -C %s rebase upstream/dev\033[0m\n" "$AZURE_CLI_FOLDER"
145-
if [ ! -z "$EXTENSIONS" ]; then
146-
printf "\033[0;33mazdev setup -c %s -r %s\033[0m\n" "$AZURE_CLI_FOLDER" "$EXTENSIONS"
147-
else
148-
printf "\033[0;33mazdev setup -c %s\033[0m\n" "$AZURE_CLI_FOLDER"
149-
fi
150-
printf "\033[0;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
151-
fi
152-
fi
153152
exit 0

0 commit comments

Comments
 (0)