Skip to content

Commit 1bbbf3a

Browse files
authored
feat: pre-push git hook to check for lock file relevant changes (#1203)
1 parent 8703ca8 commit 1bbbf3a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

.husky/pre-push

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Configuration: Each entry is an array with [pattern, command, description]
2+
# Format: "pattern" "command" "pattern description"
3+
CHECKS=(
4+
'^(package\.json|package-lock\.json)$' 'pnpm install --package-lock --ignore-scripts' 'package.json or package-lock.json – please run npm install to update dependencies'
5+
)
6+
7+
# Check for changes in specified files before pushing and run corresponding commands
8+
## Get the upstream branch
9+
UPSTREAM=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null || echo "")
10+
if [ -z "$UPSTREAM" ]; then
11+
echo "No upstream configured, detecting default branch."
12+
# Try to detect the default branch from origin/HEAD
13+
DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
14+
if [ -z "$DEFAULT_BRANCH" ]; then
15+
echo "Could not detect default branch, falling back to 'main'."
16+
DEFAULT_BRANCH="main"
17+
fi
18+
UPSTREAM="$DEFAULT_BRANCH"
19+
fi
20+
21+
## Get the list of files changed between upstream and HEAD
22+
FILES=$(git diff --name-only $UPSTREAM..HEAD)
23+
24+
## Check each pattern and run corresponding command
25+
for ((i=0; i<${#CHECKS[@]}; i+=3)); do
26+
pattern="${CHECKS[i]}"
27+
command="${CHECKS[i+1]}"
28+
description="${CHECKS[i+2]}"
29+
30+
if echo "$FILES" | grep -qE "$pattern"; then
31+
echo "Detected changes in $description"
32+
33+
## Run the corresponding command
34+
eval "$command"
35+
36+
if [ $? -ne 0 ]; then
37+
echo "Command failed: $command. Aborting push."
38+
exit 1
39+
fi
40+
41+
# Exit after first match to avoid running multiple commands
42+
exit 0
43+
fi
44+
done
45+
46+
echo "No monitored file changes detected. Skipping checks."

0 commit comments

Comments
 (0)