From 1d1ddc3ea0097d399b7a927e1972507d183a28bd Mon Sep 17 00:00:00 2001 From: Jan Klattenhoff Date: Mon, 7 Apr 2025 19:24:57 +0200 Subject: [PATCH] refactor(hooks): simplify new branch validation logic --- hooks/pre-recieve.sh | 38 ++++++++------------------------------ 1 file changed, 8 insertions(+), 30 deletions(-) diff --git a/hooks/pre-recieve.sh b/hooks/pre-recieve.sh index a976504..a454c8d 100644 --- a/hooks/pre-recieve.sh +++ b/hooks/pre-recieve.sh @@ -44,38 +44,16 @@ while read -r OLD_REV NEW_REV REF_NAME; do # Handle new branch pushes differently if [[ "$OLD_REV" = "0000000000000000000000000000000000000000" ]]; then echo -e "${YELLOW}New branch detected: $REF_NAME${NC}" - - # Find the common ancestor with other branches - # This helps us identify only the new commits unique to this branch - COMMON_ANCESTOR="" - - # Get list of all other refs excluding the current one and tags - OTHER_REFS=$(git for-each-ref --format='%(refname)' refs/heads/ | grep -v "$REF_NAME") - - if [ -n "$OTHER_REFS" ]; then - # Find the merge-base with each existing branch and use the most recent one - for REF in $OTHER_REFS; do - BASE=$(git merge-base "$NEW_REV" "$REF" 2>/dev/null || echo "") - if [ -n "$BASE" ]; then - if [ -z "$COMMON_ANCESTOR" ] || [ "$(git rev-list -n 1 --date-order "$BASE" "$COMMON_ANCESTOR" | wc -l)" -eq 1 ]; then - COMMON_ANCESTOR="$BASE" - fi - fi - done - fi - - if [ -n "$COMMON_ANCESTOR" ]; then - echo -e "${BLUE}Only validating commits unique to this branch${NC}" - OLD_REV="$COMMON_ANCESTOR" + + # For new branches, only validate the last 10 commits by default + COMMIT_COUNT=$(git rev-list --count "$NEW_REV") + if [ "$COMMIT_COUNT" -gt 10 ]; then + OLD_REV=$(git rev-list -n 1 "$NEW_REV"~10) + echo -e "${BLUE}New branch - validating only the latest 10 commits${NC}" else - # Brand new branch with no common ancestor - validate only the last 10 commits - echo -e "${BLUE}New branch with no common history - validating only the latest 10 commits${NC}" - COMMIT_COUNT=$(git rev-list --count "$NEW_REV") - if [ "$COMMIT_COUNT" -gt 10 ]; then - OLD_REV=$(git rev-list -n 1 "$NEW_REV"~10) - fi + echo -e "${BLUE}New branch - validating all $COMMIT_COUNT commits${NC}" fi - fi + } echo -e "${YELLOW}Checking commits in $REF_NAME${NC}"