From 8aba6e98d171eccbaeb87f7162bfe15b03af7954 Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Mon, 2 Jun 2025 14:32:23 +0545 Subject: [PATCH 1/2] fix: stop correctly when MAX_COMMITS_TO_CHECK is reached --- hooks/validate.sh | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/hooks/validate.sh b/hooks/validate.sh index 454d1ab..cfb3941 100755 --- a/hooks/validate.sh +++ b/hooks/validate.sh @@ -311,6 +311,11 @@ validate_commit() { # param1: commit hash # returns: 0 if the validation of the commit and all its ancestors succeeded validate_commit_and_parents() { + NUM_COMMITS_CHECKED=${#PROCESSED_COMMIT[@]} + if [[ ${NUM_COMMITS_CHECKED} -ge ${MAX_COMMITS_TO_CHECK} ]] && [[ ${MAX_COMMITS_TO_CHECK} -ge 1 ]]; then + # enough commits have already been checked, so return early + return 0; + fi local COMMIT_HASH="$1" log "validate_commit_and_parents for $COMMIT_HASH" @@ -321,16 +326,14 @@ validate_commit_and_parents() { # If MAX_COMMITS_TO_CHECK is zero (or a negative number) then that is understood as "infinity". # So perform the next commit check if we have not reached the limit, or if the limit is "infinity". NUM_COMMITS_CHECKED=${#PROCESSED_COMMIT[@]} - if [[ ${NUM_COMMITS_CHECKED} -lt ${MAX_COMMITS_TO_CHECK} ]] || [[ ${MAX_COMMITS_TO_CHECK} -lt 1 ]]; then - local PARENTS=$(git cat-file -p "$COMMIT_HASH" | awk '/^$/{exit} /parent/ {print}' | sed 's/parent //') - #iterate over all parents of commit - if [ ! -z "$PARENTS" ]; then - while read PARENT_HASH; do - if ! validate_commit_and_parents "$PARENT_HASH"; then - ALL_PASSED=false - fi - done <<< $(printf "%s" "$PARENTS") - fi + local PARENTS=$(git cat-file -p "$COMMIT_HASH" | awk '/^$/{exit} /parent/ {print}' | sed 's/parent //') + #iterate over all parents of commit + if [ ! -z "$PARENTS" ]; then + while read PARENT_HASH; do + if ! validate_commit_and_parents "$PARENT_HASH"; then + ALL_PASSED=false + fi + done <<< $(printf "%s" "$PARENTS") fi if [ "$ALL_PASSED" = true ]; then return 0 From f712aa0822915b70a498fd16f524f7b548071cfc Mon Sep 17 00:00:00 2001 From: Phil Davis Date: Mon, 2 Jun 2025 15:32:48 +0545 Subject: [PATCH 2/2] chore: adjust comment about MAX_COMMITS_TO_CHECK --- hooks/validate.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hooks/validate.sh b/hooks/validate.sh index cfb3941..6a41397 100755 --- a/hooks/validate.sh +++ b/hooks/validate.sh @@ -311,6 +311,8 @@ validate_commit() { # param1: commit hash # returns: 0 if the validation of the commit and all its ancestors succeeded validate_commit_and_parents() { + # If MAX_COMMITS_TO_CHECK is zero (or a negative number) then that is understood as "infinity". + # So finish if we have reached the limit, and if the limit is not "infinity". NUM_COMMITS_CHECKED=${#PROCESSED_COMMIT[@]} if [[ ${NUM_COMMITS_CHECKED} -ge ${MAX_COMMITS_TO_CHECK} ]] && [[ ${MAX_COMMITS_TO_CHECK} -ge 1 ]]; then # enough commits have already been checked, so return early @@ -323,8 +325,6 @@ validate_commit_and_parents() { if ! validate_commit "$COMMIT_HASH"; then ALL_PASSED=false fi - # If MAX_COMMITS_TO_CHECK is zero (or a negative number) then that is understood as "infinity". - # So perform the next commit check if we have not reached the limit, or if the limit is "infinity". NUM_COMMITS_CHECKED=${#PROCESSED_COMMIT[@]} local PARENTS=$(git cat-file -p "$COMMIT_HASH" | awk '/^$/{exit} /parent/ {print}' | sed 's/parent //') #iterate over all parents of commit