Skip to content

Commit b1520bc

Browse files
authored
{CI} Refactor git hooks to improve cross-platform compatibility (#8505)
* init * {CI} Refactor git hooks to improve cross-platform compatibility Update pre-commit and pre-push hooks to support both Windows and Unix-like systems. * {CI} Improve input handling in pre-push git hook Update pre-push PowerShell script to use Read-Host for input and provide a default 'Y' response when no input is given
1 parent 4d3f491 commit b1520bc

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

.githooks/pre-commit

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
#!/usr/bin/env sh
2-
":" //; 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
3-
":" //; exit # Skip rest on Unix
42

5-
@echo off
6-
powershell -NoProfile -Command "if (Get-Command powershell -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
7-
if %errorlevel% equ 0 (
8-
powershell -ExecutionPolicy Bypass -File .githooks\pre-commit.ps1
9-
) else (
10-
echo Error: PowerShell is not available. Please install PowerShell.
11-
exit /b 1
12-
)
13-
exit /b %errorlevel%
3+
# Check if running in Windows
4+
if [ -n "$COMSPEC" ]; then
5+
# Windows section - Execute directly with PowerShell
6+
powershell -NoProfile -Command "
7+
if (Get-Command powershell -ErrorAction SilentlyContinue) {
8+
Write-Host 'PowerShell found, executing pre-commit.ps1...'
9+
powershell -ExecutionPolicy Bypass -File '.githooks\pre-commit.ps1'
10+
exit $LASTEXITCODE
11+
} else {
12+
Write-Host 'Error: PowerShell is not available. Please install PowerShell.'
13+
exit 1
14+
}
15+
"
16+
echo "Exiting with status $?"
17+
exit $?
18+
else
19+
# Unix-like system section
20+
echo "Unix-like system found, executing pre-commit.sh..."
21+
sh .githooks/pre-commit.sh
22+
echo "Exiting with status $?"
23+
exit $?
24+
fi

.githooks/pre-push

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
#!/usr/bin/env sh
2-
":" //; 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
3-
":" //; exit # Skip rest on Unix
42

5-
@echo off
6-
powershell -NoProfile -Command "if (Get-Command powershell -ErrorAction SilentlyContinue) { exit 0 } else { exit 1 }"
7-
if %errorlevel% equ 0 (
8-
powershell -ExecutionPolicy Bypass -File .githooks\pre-push.ps1
9-
) else (
10-
echo Error: PowerShell is not available. Please install PowerShell.
11-
exit /b 1
12-
)
13-
exit /b %errorlevel%
3+
# Check if running in Windows
4+
if [ -n "$COMSPEC" ]; then
5+
# Windows section - Execute directly with PowerShell
6+
powershell -NoProfile -Command "
7+
if (Get-Command powershell -ErrorAction SilentlyContinue) {
8+
Write-Host 'PowerShell found, executing pre-push.ps1...'
9+
powershell -ExecutionPolicy Bypass -File '.githooks\pre-push.ps1'
10+
exit $LASTEXITCODE
11+
} else {
12+
Write-Host 'Error: PowerShell is not available. Please install PowerShell.'
13+
exit 1
14+
}
15+
"
16+
echo "Exiting with status $?"
17+
exit $?
18+
else
19+
# Unix-like system section
20+
echo "Unix-like system found, executing pre-push.sh..."
21+
sh .githooks/pre-push.sh
22+
echo "Exiting with status $?"
23+
exit $?
24+
fi

.githooks/pre-push.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ if ($AZURE_CLI_FOLDER) {
6666
Write-Host "Would you like to automatically rebase and setup? [Y/n]" -ForegroundColor Yellow
6767

6868
try {
69-
$reader = [System.IO.StreamReader]::new("CON")
70-
$input = $reader.ReadLine()
69+
$input = Read-Host
70+
if ([string]::IsNullOrEmpty($input)) {
71+
$input = "Y"
72+
}
7173
} catch {
7274
Write-Host "Error reading input. Aborting push..." -ForegroundColor Red
7375
exit 1

0 commit comments

Comments
 (0)