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} Refactor git hooks to improve cross-platform compatibility #30882

Merged
merged 2 commits into from
Feb 25, 2025
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
33 changes: 22 additions & 11 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#!/usr/bin/env sh
":" //; if command -v pwsh >/dev/null 2>&1; then pwsh -ExecutionPolicy Bypass -File .githooks/pre-commit.ps1; else sh .githooks/pre-commit.sh; fi; exit $? # Try PowerShell Core first, then sh on Unix
":" //; exit # Skip rest on Unix

@echo off
powershell -NoProfile -Command "if (Get-Command powershell -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
if %errorlevel% equ 0 (
powershell -ExecutionPolicy Bypass -File .githooks\pre-commit.ps1
) else (
echo Error: PowerShell is not available. Please install PowerShell.
exit /b 1
)
exit /b %errorlevel%
# Check if running in Windows
if [ -n "$COMSPEC" ]; then
Copy link
Member Author

Choose a reason for hiding this comment

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

it "specifies" the command interpreter, which by default is cmd.exe in Windows

# Windows section - Execute directly with PowerShell
powershell -NoProfile -Command "
if (Get-Command powershell -ErrorAction SilentlyContinue) {
Write-Host 'PowerShell found, executing pre-commit.ps1...'
powershell -ExecutionPolicy Bypass -File '.githooks\pre-commit.ps1'
exit $LASTEXITCODE
} else {
Write-Host 'Error: PowerShell is not available. Please install PowerShell.'
exit 1
}
"
echo "Exiting with status $?"
exit $?
else
# Unix-like system section
echo "Unix-like system found, executing pre-commit.sh..."
sh .githooks/pre-commit.sh
echo "Exiting with status $?"
exit $?
fi
33 changes: 22 additions & 11 deletions .githooks/pre-push
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
#!/usr/bin/env sh
":" //; if command -v pwsh >/dev/null 2>&1; then pwsh -ExecutionPolicy Bypass -File .githooks/pre-push.ps1; else sh .githooks/pre-push.sh; fi; exit $? # Try PowerShell Core first, then sh on Unix
":" //; exit # Skip rest on Unix

@echo off
powershell -NoProfile -Command "if (Get-Command powershell -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
if %errorlevel% equ 0 (
powershell -ExecutionPolicy Bypass -File .githooks\pre-push.ps1
) else (
echo Error: PowerShell is not available. Please install PowerShell.
exit /b 1
)
exit /b %errorlevel%
# Check if running in Windows
if [ -n "$COMSPEC" ]; then
# Windows section - Execute directly with PowerShell
powershell -NoProfile -Command "
if (Get-Command powershell -ErrorAction SilentlyContinue) {
Write-Host 'PowerShell found, executing pre-push.ps1...'
powershell -ExecutionPolicy Bypass -File '.githooks\pre-push.ps1'
exit $LASTEXITCODE
} else {
Write-Host 'Error: PowerShell is not available. Please install PowerShell.'
exit 1
}
"
echo "Exiting with status $?"
exit $?
else
# Unix-like system section
echo "Unix-like system found, executing pre-push.sh..."
sh .githooks/pre-push.sh
echo "Exiting with status $?"
exit $?
fi
6 changes: 4 additions & 2 deletions .githooks/pre-push.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ if ($mergeBase -ne $upstreamHead) {
Write-Host "Would you like to automatically rebase and setup? [Y/n]" -ForegroundColor Yellow

try {
$reader = [System.IO.StreamReader]::new("CON")
Copy link
Member Author

Choose a reason for hiding this comment

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

CON relies on the .NET framework, so replace it with the more generic Read-Host

$input = $reader.ReadLine()
$input = Read-Host
if ([string]::IsNullOrEmpty($input)) {
$input = "Y"
}
} catch {
Write-Host "Error reading input. Aborting push..." -ForegroundColor Red
exit 1
Expand Down