-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-push.sh
More file actions
executable file
·73 lines (63 loc) · 2.15 KB
/
pre-push.sh
File metadata and controls
executable file
·73 lines (63 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env bash
set -euo pipefail
# ---- Input validation -------------------------------------------------
if [[ $# -ne 1 ]]; then
echo "Usage: $0 <root-folder>"
echo "Example: $0 infra-ecs"
exit 1
fi
ROOT_DIR="$1"
PATTERN="^${ROOT_DIR}/(deployment|modules|tests)/.*\.(tf|tfvars|tftest\.hcl)$"
echo "Pre-push check for changes under '${ROOT_DIR}'..."
# ---- Detect changes using stdin (native git hook) or local/remote comparison (pre-commit framework) ----
# Check if stdin has data (native git pre-push hook provides this)
if read -t 0; then
# Read from stdin - native git pre-push hook
while read -r local_ref local_sha remote_ref remote_sha; do
# First push or new branch
if [[ "$remote_sha" == "0000000000000000000000000000000000000000" ]]; then
RANGE="$local_sha"
else
RANGE="$remote_sha..$local_sha"
fi
if git diff --name-only "$RANGE" | grep -E -q "$PATTERN"; then
echo "Relevant changes detected. Running tests..."
(
cd "$ROOT_DIR"
./run-tests.sh
)
exit 0
fi
done
else
# No stdin data - running via pre-commit framework
# Compare current branch with its remote tracking branch
current_branch=$(git rev-parse --abbrev-ref HEAD)
remote_branch=$(git rev-parse --abbrev-ref --symbolic-full-name "@{upstream}" 2>/dev/null || echo "")
if [[ -n "$remote_branch" ]]; then
# Remote tracking branch exists - check for changes since remote
if git diff --name-only "$remote_branch"..HEAD | grep -E -q "$PATTERN"; then
echo "Relevant changes detected. Running tests..."
(
cd "$ROOT_DIR"
./run-tests.sh
)
exit 0
fi
else
# No remote tracking branch - check uncommitted + committed changes
# This handles first push scenarios
if git diff --name-only HEAD | grep -E -q "$PATTERN" || \
git diff --name-only --cached | grep -E -q "$PATTERN" || \
git ls-files "$ROOT_DIR" | grep -E -q "$PATTERN"; then
echo "Relevant changes detected (new branch). Running tests..."
(
cd "$ROOT_DIR"
./run-tests.sh
)
exit 0
fi
fi
fi
echo "No relevant changes detected. Skipping tests."
exit 0