From 71d0acd327b5c0deff823d62d7c47472fb0dca59 Mon Sep 17 00:00:00 2001 From: Maximilian Franzke <787658+mfranzke@users.noreply.github.com> Date: Fri, 1 Aug 2025 21:48:54 +0200 Subject: [PATCH 1/4] feat: pre-push git hook to check for lock file relevant changes --- .husky/pre-push | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 .husky/pre-push diff --git a/.husky/pre-push b/.husky/pre-push new file mode 100644 index 0000000..69ebc65 --- /dev/null +++ b/.husky/pre-push @@ -0,0 +1,44 @@ +# Configuration: Each entry is an array with [pattern, command, description] +# Format: "pattern" "command" "pattern description" +CHECKS=('^(package\.json|package-lock\.json)$' 'npm install --package-lock-only --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." From e6e7b7c58c6af4a352e3668404ad3a14b8a90df7 Mon Sep 17 00:00:00 2001 From: Maximilian Franzke <787658+mfranzke@users.noreply.github.com> Date: Fri, 1 Aug 2025 22:38:46 +0200 Subject: [PATCH 2/4] Update pre-push --- .husky/pre-push | 53 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/.husky/pre-push b/.husky/pre-push index 69ebc65..cca90f3 100644 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -1,6 +1,26 @@ -# Configuration: Each entry is an array with [pattern, command, description] -# Format: "pattern" "command" "pattern description" -CHECKS=('^(package\.json|package-lock\.json)$' 'npm install --package-lock-only --ignore-scripts' 'package.json or package-lock.json – please run npm install to update dependencies') +# Configuration: Define checks as functions for better maintainability +# Each check function should: +# - Define a PATTERN variable for file matching +# - Define a COMMAND variable for the command to run +# - Define a DESCRIPTION variable for user feedback + +check_npm_files() { + PATTERN='^(package\.json|package-lock\.json)$' + COMMAND='npm install --package-lock-only --ignore-scripts' + DESCRIPTION='package.json or package-lock.json – please run npm install to update dependencies' +} + +check_pnpm_files() { + PATTERN='^(package\.json|pnpm-lock\.yaml)$' + COMMAND='pnpm install --lockfile-only --ignore-scripts' + DESCRIPTION='package.json or pnpm-lock.yaml – please run pnpm install to update dependencies' +} + +# List of all check functions +CHECK_FUNCTIONS=( + "check_npm_files" + "check_pnpm_files" +) # Check for changes in specified files before pushing and run corresponding commands ## Get the upstream branch @@ -20,24 +40,29 @@ fi 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" +for check_function in "${CHECK_FUNCTIONS[@]}"; do + # Call the check function to set variables + $check_function + + if echo "$FILES" | grep -qE "$PATTERN"; then + echo "Detected changes in $DESCRIPTION" ## Run the corresponding command - eval "$command" + eval "$COMMAND" if [ $? -ne 0 ]; then - echo "Command failed: $command. Aborting push." + echo "Command failed: $COMMAND. Aborting push." exit 1 fi - # Exit after first match to avoid running multiple commands - exit 0 + # Check for file modifications after running the command + MODIFIED_FILES=$(git diff --name-only) + if [ -n "$MODIFIED_FILES" ]; then + echo "Detected file modifications after running $COMMAND:" + echo "$MODIFIED_FILES" + echo "Please stage the changes before pushing." + exit 1 + fi fi done From 19ba9c1a787e9c4c977c72dd6bbac2979dc97398 Mon Sep 17 00:00:00 2001 From: Maximilian Franzke <787658+mfranzke@users.noreply.github.com> Date: Sat, 2 Aug 2025 03:25:20 +0200 Subject: [PATCH 3/4] Update pre-push --- .husky/pre-push | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/.husky/pre-push b/.husky/pre-push index cca90f3..1787897 100644 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -17,10 +17,19 @@ check_pnpm_files() { } # List of all check functions -CHECK_FUNCTIONS=( - "check_npm_files" - "check_pnpm_files" +# Detect the lock file to determine the package manager +if [ -f "pnpm-lock.yaml" ]; then + CHECK_FUNCTIONS=( + "check_pnpm_files" ) +elif [ -f "package-lock.json" ]; then + CHECK_FUNCTIONS=( + "check_npm_files" + ) +else + echo "No lock file detected for pnpm or npm. Aborting pre-push checks." + exit 1 +fi # Check for changes in specified files before pushing and run corresponding commands ## Get the upstream branch @@ -43,7 +52,7 @@ FILES=$(git diff --name-only $UPSTREAM..HEAD) for check_function in "${CHECK_FUNCTIONS[@]}"; do # Call the check function to set variables $check_function - + if echo "$FILES" | grep -qE "$PATTERN"; then echo "Detected changes in $DESCRIPTION" From 52a11c71e869ad94d3c1d0aa2993e5494c6d3b97 Mon Sep 17 00:00:00 2001 From: Maximilian Franzke <787658+mfranzke@users.noreply.github.com> Date: Sat, 2 Aug 2025 03:47:36 +0200 Subject: [PATCH 4/4] Update pre-push --- .husky/pre-push | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.husky/pre-push b/.husky/pre-push index 1787897..0a7f59c 100644 --- a/.husky/pre-push +++ b/.husky/pre-push @@ -20,7 +20,7 @@ check_pnpm_files() { # Detect the lock file to determine the package manager if [ -f "pnpm-lock.yaml" ]; then CHECK_FUNCTIONS=( - "check_pnpm_files" + "check_pnpm_files" ) elif [ -f "package-lock.json" ]; then CHECK_FUNCTIONS=( @@ -46,7 +46,7 @@ if [ -z "$UPSTREAM" ]; then fi ## Get the list of files changed between upstream and HEAD -FILES=$(git diff --name-only $UPSTREAM..HEAD) +FILES=$(git diff --name-only "$UPSTREAM"..HEAD) ## Check each pattern and run corresponding command for check_function in "${CHECK_FUNCTIONS[@]}"; do @@ -57,7 +57,7 @@ for check_function in "${CHECK_FUNCTIONS[@]}"; do echo "Detected changes in $DESCRIPTION" ## Run the corresponding command - eval "$COMMAND" + $COMMAND if [ $? -ne 0 ]; then echo "Command failed: $COMMAND. Aborting push."