refactor(hooks): simplify new branch validation logic

This commit is contained in:
Jan K9f 2025-04-07 19:24:57 +02:00
parent 411b4dec84
commit 1d1ddc3ea0
Signed by: jank
GPG key ID: B9F475106B20F144

View file

@ -45,37 +45,15 @@ while read -r OLD_REV NEW_REV REF_NAME; do
if [[ "$OLD_REV" = "0000000000000000000000000000000000000000" ]]; then if [[ "$OLD_REV" = "0000000000000000000000000000000000000000" ]]; then
echo -e "${YELLOW}New branch detected: $REF_NAME${NC}" echo -e "${YELLOW}New branch detected: $REF_NAME${NC}"
# Find the common ancestor with other branches # For new branches, only validate the last 10 commits by default
# This helps us identify only the new commits unique to this branch COMMIT_COUNT=$(git rev-list --count "$NEW_REV")
COMMON_ANCESTOR="" if [ "$COMMIT_COUNT" -gt 10 ]; then
OLD_REV=$(git rev-list -n 1 "$NEW_REV"~10)
# Get list of all other refs excluding the current one and tags echo -e "${BLUE}New branch - validating only the latest 10 commits${NC}"
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"
else else
# Brand new branch with no common ancestor - validate only the last 10 commits echo -e "${BLUE}New branch - validating all $COMMIT_COUNT commits${NC}"
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
fi fi
fi }
echo -e "${YELLOW}Checking commits in $REF_NAME${NC}" echo -e "${YELLOW}Checking commits in $REF_NAME${NC}"