Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

{CI} Optimize azure-cli githook #30513

Merged
merged 10 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 39 additions & 27 deletions .githooks/pre-push.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,49 @@ if ($LASTEXITCODE -ne 0) {
# Check if current branch needs rebasing
$mergeBase = git merge-base HEAD upstream/dev
$upstreamHead = git rev-parse upstream/dev
Write-Host "Initial mergeBase: $mergeBase" -ForegroundColor Cyan

if ($mergeBase -ne $upstreamHead) {
Write-Host ""
Write-Host "Your branch 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 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 branch 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 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
$mergeBase = git merge-base HEAD upstream/dev
Write-Host "Updated mergeBase: $mergeBase" -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..."
}

# get the current branch name
Expand Down Expand Up @@ -93,17 +118,4 @@ if ($LASTEXITCODE -ne 0) {
}

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

if ($mergeBase -ne $upstreamHead) {
Write-Host ""
Write-Host "Your branch is not up to date with upstream/dev. Please run the following commands to rebase code and setup:" -ForegroundColor Yellow
Write-Host "+++++++++++++++++++++++++++++++++++++++++++++++++++++++" -ForegroundColor Yellow
Write-Host "git 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
74 changes: 34 additions & 40 deletions .githooks/pre-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ else
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/ $//')
Copy link
Member Author

@wangzelin007 wangzelin007 Dec 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
image
image


# Fetch upstream/dev branch
printf "\033[0;32mFetching upstream/dev branch...\033[0m\n"
Expand All @@ -34,35 +34,45 @@ fi
# Check if current branch needs rebasing
MERGE_BASE=$(git merge-base HEAD upstream/dev)
UPSTREAM_HEAD=$(git rev-parse upstream/dev)
printf "\033[0;36mInitial mergeBase: %s\033[0m\n" "$MERGE_BASE"

if [ "$MERGE_BASE" != "$UPSTREAM_HEAD" ]; then
printf "\n"
printf "\033[1;33mYour branch is not up to date with upstream/dev. Please run the following commands to rebase and setup:\033[0m\n"
printf "\033[1;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
printf "\033[1;33mgit rebase upstream/dev\033[0m\n"

# Get extension repo paths
EXTENSIONS=$(azdev extension repo list -o tsv | tr '\n' ' ')
if [ -n "$EXTENSIONS" ]; then
printf "\033[1;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 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"
MERGE_BASE=$(git merge-base HEAD upstream/dev)
printf "\033[0;36mUpdated mergeBase: %s\033[0m\n" "$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[1;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[1;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
printf "\n"
printf "\033[1;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 "\r\033[K\033[1;33mContinuing without rebase...\033[0m\n"
fi

# get the current branch name
# Get the current branch name
CURRENT_BRANCH=$(git branch --show-current)

# Run command azdev lint
Expand Down Expand Up @@ -93,26 +103,10 @@ if [ $? -ne 0 ]; then
printf "\033[0;31mError: azdev test check failed.\033[0m\n"
exit 1
else
# remove the test_results.xml file
# Remove the test_results.xml file
rm -f test_results.xml
fi

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

if [ "$MERGE_BASE" != "$UPSTREAM_HEAD" ]; then
printf "\n"
printf "\033[1;33mYour branch is not up to date with upstream/dev. Please run the following commands to rebase and setup:\033[0m\n"
printf "\033[1;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
printf "\033[1;33mgit rebase upstream/dev\033[0m\n"

# Get extension repo paths
EXTENSIONS=$(azdev extension repo list -o tsv | tr '\n' ' ')
if [ -n "$EXTENSIONS" ]; then
printf "\033[1;33mazdev setup -c %s -r %s\033[0m\n" "$AZURE_CLI_FOLDER" "$EXTENSIONS"
else
printf "\033[1;33mazdev setup -c %s\033[0m\n" "$AZURE_CLI_FOLDER"
fi
printf "\033[1;33m+++++++++++++++++++++++++++++++++++++++++++++++++++++++\033[0m\n"
fi
exit 0