Skip to content

feat: pre-push git hook to check for lock file relevant changes #741

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

Merged
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
46 changes: 46 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Configuration: Each entry is an array with [pattern, command, description]
# Format: "pattern" "command" "pattern description"
CHECKS=(
'^(package\.json|package-lock\.json)$' 'pnpm install --package-lock --ignore-scripts' 'package.json or package-lock.json – please run npm install to update dependencies'
)

# Check for changes in specified files before pushing and run corresponding commands
## Get the upstream branch
UPSTREAM=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2>/dev/null || echo "")
if [ -z "$UPSTREAM" ]; then
echo "No upstream configured, detecting default branch."
# Try to detect the default branch from origin/HEAD
DEFAULT_BRANCH=$(git symbolic-ref refs/remotes/origin/HEAD 2>/dev/null | sed 's@^refs/remotes/origin/@@')
if [ -z "$DEFAULT_BRANCH" ]; then
echo "Could not detect default branch, falling back to 'main'."
DEFAULT_BRANCH="main"
fi
UPSTREAM="$DEFAULT_BRANCH"
fi

## Get the list of files changed between upstream and HEAD
FILES=$(git diff --name-only $UPSTREAM..HEAD)

## Check each pattern and run corresponding command
for ((i=0; i<${#CHECKS[@]}; i+=3)); do
pattern="${CHECKS[i]}"
command="${CHECKS[i+1]}"
description="${CHECKS[i+2]}"

if echo "$FILES" | grep -qE "$pattern"; then
echo "Detected changes in $description"

## Run the corresponding command
eval "$command"

if [ $? -ne 0 ]; then
echo "Command failed: $command. Aborting push."
exit 1
fi

# Exit after first match to avoid running multiple commands
exit 0
fi
done

echo "No monitored file changes detected. Skipping checks."
3 changes: 3 additions & 0 deletions .husky/pre-push.license
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SPDX-FileCopyrightText: 2025 DB Systel GmbH

SPDX-License-Identifier: Apache-2.0
Loading