Skip to content
Open
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
29 changes: 19 additions & 10 deletions .githooks/pre-push.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,31 @@ if ($editableLocation) {
# Get extension repo paths and join them with spaces
$Extensions = (azdev extension repo list -o tsv) -join ' '

# Fetch upstream/dev branch
Write-Host "Fetching upstream/dev branch..." -ForegroundColor Green
git fetch upstream dev
# Detect the remote pointing to Azure/azure-cli
$remoteLines = git remote -v | Select-String -Pattern "Azure/azure-cli" | Select-String -Pattern "\(fetch\)"
$UpstreamRemote = if ($remoteLines) { ($remoteLines[0] -split "\s+")[0] } else { $null }
Comment on lines +29 to +31
if (-not $UpstreamRemote) {
Write-Host "Error: No remote found pointing to Azure/azure-cli. Please run 'git remote add upstream https://github.com/Azure/azure-cli.git' first." -ForegroundColor Red
exit 1
}
Write-Host "Using remote: $UpstreamRemote" -ForegroundColor Cyan

# Fetch remote dev branch
Write-Host "Fetching $UpstreamRemote/dev branch..." -ForegroundColor Green
git fetch $UpstreamRemote dev
if ($LASTEXITCODE -ne 0) {
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
Write-Host "Error: Failed to fetch $UpstreamRemote/dev branch." -ForegroundColor Red
exit 1
}

# Check if current branch needs rebasing
$mergeBase = git merge-base HEAD upstream/dev
$upstreamHead = git rev-parse upstream/dev
$mergeBase = git merge-base HEAD "$UpstreamRemote/dev"
$upstreamHead = git rev-parse "$UpstreamRemote/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." -ForegroundColor Yellow
Write-Host "Your branch is not up to date with $UpstreamRemote/dev." -ForegroundColor Yellow
Write-Host "Would you like to automatically rebase and setup? [Y/n]" -ForegroundColor Yellow

try {
Expand All @@ -55,14 +64,14 @@ if ($mergeBase -ne $upstreamHead) {
}

if ($input -match '^[Yy]$') {
Write-Host "Rebasing with upstream/dev..." -ForegroundColor Green
git rebase upstream/dev
Write-Host "Rebasing with $UpstreamRemote/dev..." -ForegroundColor Green
git rebase "$UpstreamRemote/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
$mergeBase = git merge-base HEAD "$UpstreamRemote/dev"
Write-Host "Updated mergeBase: $mergeBase" -ForegroundColor Cyan

Write-Host "Running azdev setup..." -ForegroundColor Green
Expand Down
28 changes: 18 additions & 10 deletions .githooks/pre-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,42 @@ fi
# Get extension repo paths and join them with spaces
EXTENSIONS=$(azdev extension repo list -o tsv | tr '\n' ' ' | sed 's/ $//')

# Fetch upstream/dev branch
printf "\033[0;32mFetching upstream/dev branch...\033[0m\n"
git fetch upstream dev
# Detect the remote pointing to Azure/azure-cli
UPSTREAM_REMOTE=$(git remote -v | grep -i "Azure/azure-cli" | grep "(fetch)" | head -1 | awk '{print $1}')
if [ -z "$UPSTREAM_REMOTE" ]; then
printf "\033[0;31mError: No remote found pointing to Azure/azure-cli. Please run 'git remote add upstream https://github.com/Azure/azure-cli.git' first.\033[0m\n"
exit 1
fi
Comment on lines +26 to +31
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's unlikely that these hooks will run in cli extension repo. But I may be wrong.

printf "\033[0;36mUsing remote: %s\033[0m\n" "$UPSTREAM_REMOTE"

# Fetch remote dev branch
printf "\033[0;32mFetching %s/dev branch...\033[0m\n" "$UPSTREAM_REMOTE"
git fetch "$UPSTREAM_REMOTE" dev
if [ $? -ne 0 ]; then
printf "\033[0;31mError: Failed to fetch upstream/dev branch. Please run 'git remote add upstream https://github.com/Azure/azure-cli.git' first.\033[0m\n"
printf "\033[0;31mError: Failed to fetch %s/dev branch.\033[0m\n" "$UPSTREAM_REMOTE"
exit 1
fi

# Check if current branch needs rebasing
MERGE_BASE=$(git merge-base HEAD upstream/dev)
UPSTREAM_HEAD=$(git rev-parse upstream/dev)
MERGE_BASE=$(git merge-base HEAD "$UPSTREAM_REMOTE/dev")
UPSTREAM_HEAD=$(git rev-parse "$UPSTREAM_REMOTE/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.\033[0m\n"
printf "\033[1;33mYour branch is not up to date with %s/dev.\033[0m\n" "$UPSTREAM_REMOTE"
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
printf "\033[0;32mRebasing with %s/dev...\033[0m\n" "$UPSTREAM_REMOTE"
git rebase "$UPSTREAM_REMOTE/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)
MERGE_BASE=$(git merge-base HEAD "$UPSTREAM_REMOTE/dev")
printf "\033[0;36mUpdated mergeBase: %s\033[0m\n" "$MERGE_BASE"

printf "\033[0;32mRunning azdev setup...\033[0m\n"
Expand Down
Loading