refactor(ho-ok): simplify commit message validation output

This commit is contained in:
Jan K9f 2025-04-07 19:10:53 +02:00
parent e92a559e95
commit cf6a51a7f2
Signed by: jank
GPG key ID: B9F475106B20F144

View file

@ -1,7 +1,6 @@
#!/bin/bash
# Semantic commit pattern - fixed to properly handle hyphens in scope
# Moved the hyphen to the end of the character class to avoid interpretation issues
# Semantic commit pattern
PATTERN='^(feat|fix|docs|style|refactor|test|perf|build|ci|chore|revert)(\([a-zA-Z0-9\._-]+\))?: .{1,}$'
# Pattern for merge commits from pull requests - simplified to catch all formats
@ -84,33 +83,28 @@ while read -r OLD_REV NEW_REV REF_NAME; do
OLD_REV=$(git rev-list --max-parents=0 "$NEW_REV")
fi
echo -e "${YELLOW}Checking commits in $REF_NAME${NC}"
echo -e "${YELLOW}Validating commits in $REF_NAME${NC}"
# Get branch stats from git
BRANCH_COMMITS=$(git rev-list --count "$REF_NAME")
BRANCH_AGE=$(git log -1 --format="%cr" "$REF_NAME")
echo -e "${CYAN}→ Branch has $BRANCH_COMMITS total commits (created $BRANCH_AGE)${NC}"
echo ""
# Get branch stats from git (quietly)
BRANCH_COMMITS=$(git rev-list --count "$REF_NAME" 2>/dev/null || echo "?")
BRANCH_AGE=$(git log -1 --format="%cr" "$REF_NAME" 2>/dev/null || echo "unknown time ago")
# Get all commit hashes between old and new revision
COMMITS=$(git rev-list "$OLD_REV".."$NEW_REV")
# Check each commit message
# Process each commit message, but with minimal output
for COMMIT in $COMMITS; do
TOTAL_COMMITS=$((TOTAL_COMMITS + 1))
COMMIT_MSG=$(git log --format=%B -n 1 "$COMMIT")
COMMIT_SUBJECT=$(echo "$COMMIT_MSG" | head -n 1)
PARENT_COUNT=$(git rev-list --parents -n 1 "$COMMIT" | awk '{print NF - 1}')
AUTHOR=$(git log -1 --format="%an" "$COMMIT")
COMMIT_DATE=$(git log -1 --format="%cd" --date=relative "$COMMIT")
SHORT_HASH=${COMMIT:0:8}
# Get commit stats
STATS=$(git show --stat --format="" "$COMMIT")
FILES_CHANGED=$(echo "$STATS" | grep -c "^[ ]*")
INSERTIONS=$(echo "$STATS" | grep -o '[0-9]* insertion' | cut -d' ' -f1 | awk '{sum+=$1} END {print sum+0}')
DELETIONS=$(echo "$STATS" | grep -o '[0-9]* deletion' | cut -d' ' -f1 | awk '{sum+=$1} END {print sum+0}')
# Get commit stats silently
STATS=$(git show --stat --format="" "$COMMIT" 2>/dev/null)
FILES_CHANGED=$(echo "$STATS" | grep -c "^[ ]*" 2>/dev/null || echo 0)
INSERTIONS=$(echo "$STATS" | grep -o '[0-9]* insertion' | cut -d' ' -f1 | awk '{sum+=$1} END {print sum+0}' 2>/dev/null || echo 0)
DELETIONS=$(echo "$STATS" | grep -o '[0-9]* deletion' | cut -d' ' -f1 | awk '{sum+=$1} END {print sum+0}' 2>/dev/null || echo 0)
# Update stats
TOTAL_FILES_CHANGED=$((TOTAL_FILES_CHANGED + FILES_CHANGED))
@ -124,23 +118,16 @@ while read -r OLD_REV NEW_REV REF_NAME; do
AUTHORS[$AUTHOR]=1
fi
echo -e "${MAGENTA}Commit #$TOTAL_COMMITS:${NC} ${BOLD}$SHORT_HASH${NC} (by $AUTHOR, $COMMIT_DATE)"
echo -e " Changed: ${CYAN}$FILES_CHANGED files${NC} ${GREEN}+$INSERTIONS${NC}/${RED}-$DELETIONS${NC} lines"
echo " $COMMIT_SUBJECT"
# Skip validation for merge commits (both PR merges and standard Git merges)
# Process merge commits or regular commits (minimal output)
if [[ $PARENT_COUNT -gt 1 ]] && ([[ "$COMMIT_SUBJECT" =~ $PR_MERGE_PATTERN ]] || [[ "$COMMIT_SUBJECT" =~ $GIT_MERGE_PATTERN ]]); then
echo -e " ${BLUE}➜ Skipping merge commit validation${NC}"
MERGE_COMMITS=$((MERGE_COMMITS + 1))
continue
fi
# Validate commit message (no detailed output)
if ! [[ "$COMMIT_SUBJECT" =~ $PATTERN ]]; then
echo -e " ${RED}✗ Invalid commit message${NC}"
echo -e " ${YELLOW} Should follow pattern:${NC} type(scope): message"
INVALID_COMMITS=$((INVALID_COMMITS + 1))
else
echo -e " ${GREEN}✓ Valid semantic commit${NC}"
VALID_COMMITS=$((VALID_COMMITS + 1))
# Count commit type
@ -149,13 +136,16 @@ while read -r OLD_REV NEW_REV REF_NAME; do
COMMIT_TYPES[$COMMIT_TYPE]=$((COMMIT_TYPES[$COMMIT_TYPE] + 1))
fi
fi
echo ""
done
# Show processing finished
echo -e "${GREEN}${NC} Processed $TOTAL_COMMITS commits"
echo
# Add push summary
draw_box "COMMIT STATISTICS"
echo -e "${BOLD}Total commits in this push:${NC} $TOTAL_COMMITS"
echo -e "${BOLD}Branch information:${NC} $BRANCH_COMMITS total commits (created $BRANCH_AGE)"
echo -e "${BOLD}Commits in this push:${NC} $TOTAL_COMMITS"
echo -e "${GREEN}Valid semantic commits: ${NC} $VALID_COMMITS"
echo -e "${BLUE}Merge commits: ${NC} $MERGE_COMMITS"
echo -e "${RED}Invalid commits: ${NC} $INVALID_COMMITS"