refactor(pre-receive.sh): simplify commit validation logic
This commit is contained in:
parent
25dd494af2
commit
028b8787c7
1 changed files with 7 additions and 116 deletions
|
@ -3,7 +3,7 @@
|
|||
# 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
|
||||
# Pattern for merge commits from pull requests
|
||||
PR_MERGE_PATTERN='^Merge pull request'
|
||||
|
||||
# Pattern for standard Git merge commits
|
||||
|
@ -14,8 +14,6 @@ RED='\033[0;31m'
|
|||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[0;33m'
|
||||
BLUE='\033[0;34m'
|
||||
MAGENTA='\033[0;35m'
|
||||
CYAN='\033[0;36m'
|
||||
BOLD='\033[1m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
|
@ -25,42 +23,7 @@ VALID_COMMITS=0
|
|||
MERGE_COMMITS=0
|
||||
TOTAL_COMMITS=0
|
||||
|
||||
# Track commit types for summary
|
||||
declare -A COMMIT_TYPES=(
|
||||
["feat"]=0 ["fix"]=0 ["docs"]=0 ["style"]=0 ["refactor"]=0
|
||||
["test"]=0 ["perf"]=0 ["build"]=0 ["ci"]=0 ["chore"]=0 ["revert"]=0
|
||||
)
|
||||
|
||||
# Track authors
|
||||
declare -A AUTHORS=()
|
||||
|
||||
# Track files changed
|
||||
TOTAL_FILES_CHANGED=0
|
||||
TOTAL_INSERTIONS=0
|
||||
TOTAL_DELETIONS=0
|
||||
|
||||
# Function to draw a horizontal bar
|
||||
draw_bar() {
|
||||
local value=$1
|
||||
local max=$2
|
||||
local width=30
|
||||
local bar_width=$((value * width / max))
|
||||
|
||||
# Ensure minimum width of 1 if value > 0
|
||||
if [ "$value" -gt 0 ] && [ "$bar_width" -eq 0 ]; then
|
||||
bar_width=1
|
||||
fi
|
||||
|
||||
printf "[%${bar_width}s%$((width - bar_width))s] %d\n" | tr ' ' '#' | tr ' ' '.'
|
||||
}
|
||||
|
||||
# Function to extract commit type
|
||||
get_commit_type() {
|
||||
local subject="$1"
|
||||
echo "$subject" | grep -oP '^(feat|fix|docs|style|refactor|test|perf|build|ci|chore|revert)' || echo "unknown"
|
||||
}
|
||||
|
||||
# ASCII box drawing for commit stats
|
||||
# Draw box for statistics
|
||||
draw_box() {
|
||||
local title="$1"
|
||||
local width=60
|
||||
|
@ -83,8 +46,6 @@ while read -r OLD_REV NEW_REV REF_NAME; do
|
|||
OLD_REV=$(git rev-list --max-parents=0 "$NEW_REV")
|
||||
fi
|
||||
|
||||
echo -e "${YELLOW}Validating commits in $REF_NAME${NC}"
|
||||
|
||||
# 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")
|
||||
|
@ -92,57 +53,28 @@ while read -r OLD_REV NEW_REV REF_NAME; do
|
|||
# Get all commit hashes between old and new revision
|
||||
COMMITS=$(git rev-list "$OLD_REV".."$NEW_REV")
|
||||
|
||||
# Process each commit message, but with minimal output
|
||||
# Process each commit message silently
|
||||
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")
|
||||
|
||||
# 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))
|
||||
TOTAL_INSERTIONS=$((TOTAL_INSERTIONS + INSERTIONS))
|
||||
TOTAL_DELETIONS=$((TOTAL_DELETIONS + DELETIONS))
|
||||
|
||||
# Update author stats
|
||||
if [[ -n "${AUTHORS[$AUTHOR]}" ]]; then
|
||||
AUTHORS[$AUTHOR]=$((AUTHORS[$AUTHOR] + 1))
|
||||
else
|
||||
AUTHORS[$AUTHOR]=1
|
||||
fi
|
||||
|
||||
# Process merge commits or regular commits (minimal output)
|
||||
if [[ $PARENT_COUNT -gt 1 ]] && ([[ "$COMMIT_SUBJECT" =~ $PR_MERGE_PATTERN ]] || [[ "$COMMIT_SUBJECT" =~ $GIT_MERGE_PATTERN ]]); then
|
||||
# Process merge commits or regular commits
|
||||
if [[ $PARENT_COUNT -gt 1 ]] && (echo "$COMMIT_SUBJECT" | grep -E "$PR_MERGE_PATTERN" >/dev/null || echo "$COMMIT_SUBJECT" | grep -E "$GIT_MERGE_PATTERN" >/dev/null); then
|
||||
MERGE_COMMITS=$((MERGE_COMMITS + 1))
|
||||
continue
|
||||
fi
|
||||
|
||||
# Validate commit message (no detailed output)
|
||||
# Validate commit message silently
|
||||
if ! [[ "$COMMIT_SUBJECT" =~ $PATTERN ]]; then
|
||||
INVALID_COMMITS=$((INVALID_COMMITS + 1))
|
||||
else
|
||||
VALID_COMMITS=$((VALID_COMMITS + 1))
|
||||
|
||||
# Count commit type
|
||||
COMMIT_TYPE=$(get_commit_type "$COMMIT_SUBJECT")
|
||||
if [[ -n "${COMMIT_TYPES[$COMMIT_TYPE]}" ]]; then
|
||||
COMMIT_TYPES[$COMMIT_TYPE]=$((COMMIT_TYPES[$COMMIT_TYPE] + 1))
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# Show processing finished
|
||||
echo -e "${GREEN}✓${NC} Processed $TOTAL_COMMITS commits"
|
||||
echo
|
||||
|
||||
# Add push summary
|
||||
# Display only the commit statistics box
|
||||
draw_box "COMMIT STATISTICS"
|
||||
echo -e "${BOLD}Branch information:${NC} $BRANCH_COMMITS total commits (created $BRANCH_AGE)"
|
||||
echo -e "${BOLD}Commits in this push:${NC} $TOTAL_COMMITS"
|
||||
|
@ -166,43 +98,6 @@ while read -r OLD_REV NEW_REV REF_NAME; do
|
|||
printf "] $VALID_PERCENT%%\n"
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# Commit type distribution if we have valid commits
|
||||
if [ $VALID_COMMITS -gt 0 ]; then
|
||||
draw_box "COMMIT TYPE DISTRIBUTION"
|
||||
for type in "${!COMMIT_TYPES[@]}"; do
|
||||
count=${COMMIT_TYPES[$type]}
|
||||
if [ $count -gt 0 ]; then
|
||||
# Calculate percentage
|
||||
percent=$((count * 100 / VALID_COMMITS))
|
||||
printf "%-10s " "$type:"
|
||||
draw_bar $count $VALID_COMMITS
|
||||
echo " $count ($percent%)"
|
||||
fi
|
||||
done
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# Author distribution
|
||||
if [ ${#AUTHORS[@]} -gt 0 ]; then
|
||||
draw_box "AUTHOR DISTRIBUTION"
|
||||
for author in "${!AUTHORS[@]}"; do
|
||||
count=${AUTHORS[$author]}
|
||||
percent=$((count * 100 / TOTAL_COMMITS))
|
||||
printf "%-20s " "$author:"
|
||||
draw_bar $count $TOTAL_COMMITS
|
||||
echo " $count ($percent%)"
|
||||
done
|
||||
echo ""
|
||||
fi
|
||||
|
||||
# File stats
|
||||
draw_box "CHANGE STATISTICS"
|
||||
echo -e "Files changed: $TOTAL_FILES_CHANGED"
|
||||
echo -e "Lines added: ${GREEN}+$TOTAL_INSERTIONS${NC}"
|
||||
echo -e "Lines removed: ${RED}-$TOTAL_DELETIONS${NC}"
|
||||
echo -e "Net change: $((TOTAL_INSERTIONS - TOTAL_DELETIONS)) lines"
|
||||
echo ""
|
||||
done
|
||||
|
||||
# If any commits were invalid, reject the push
|
||||
|
@ -210,10 +105,6 @@ if [[ $INVALID_COMMITS -gt 0 ]]; then
|
|||
echo -e "${RED}Push rejected: $INVALID_COMMITS commit(s) have invalid messages.${NC}"
|
||||
echo -e "${YELLOW}Please rewrite your commit messages to follow the semantic commit format:${NC}"
|
||||
echo "type(scope): message"
|
||||
echo -e "\nExamples:"
|
||||
echo " feat(auth): add login with Google"
|
||||
echo " fix: correct calculation in billing module"
|
||||
echo " style(release.yml): format permissions section in YAML"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue