Skip to content

Commit 549a733

Browse files
authored
{CI} Refactor git hooks to improve cross-platform compatibility (Azure#30882)
* {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 ffc3e1c commit 549a733

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

.githooks/pre-commit

+22-11
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

+22-11
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

+4-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ if ($mergeBase -ne $upstreamHead) {
4545
Write-Host "Would you like to automatically rebase and setup? [Y/n]" -ForegroundColor Yellow
4646

4747
try {
48-
$reader = [System.IO.StreamReader]::new("CON")
49-
$input = $reader.ReadLine()
48+
$input = Read-Host
49+
if ([string]::IsNullOrEmpty($input)) {
50+
$input = "Y"
51+
}
5052
} catch {
5153
Write-Host "Error reading input. Aborting push..." -ForegroundColor Red
5254
exit 1

0 commit comments

Comments
 (0)