|
| 1 | +#!/bin/sh |
| 2 | +# Commit message validation hook for Dude projects |
| 3 | + |
| 4 | +# Colors for output |
| 5 | +RED='\033[1;31m' |
| 6 | +GREEN='\033[0;32m' |
| 7 | +YELLOW='\033[1;33m' |
| 8 | +BLUE='\033[1;34m' |
| 9 | +PURPLE='\033[0;35m' |
| 10 | +NC='\033[0m' # No Color |
| 11 | + |
| 12 | +# Emojis for better visual feedback |
| 13 | +CHECK="✅" |
| 14 | +CROSS="❌" |
| 15 | +ARROW="➜" |
| 16 | +SEARCH="🔍" |
| 17 | + |
| 18 | +echo "" |
| 19 | +echo "${SEARCH} Checking commit message format..." |
| 20 | + |
| 21 | +# Get the commit message from the file passed as argument |
| 22 | +commit_msg_file="$1" |
| 23 | +if [ -f "$commit_msg_file" ]; then |
| 24 | + commit_msg=$(cat "$commit_msg_file") |
| 25 | + |
| 26 | + printf " ${ARROW} Validating commit message... " |
| 27 | + |
| 28 | + # Check for task ID pattern (e.g., DEV-123, TASK-456, etc.) |
| 29 | + task_id_pattern="[A-Z]+-[0-9]+" |
| 30 | + |
| 31 | + # Check for magic words (closing) |
| 32 | + closing_words="close|closes|closed|closing|fix|fixes|fixed|fixing|resolve|resolves|resolved|resolving|complete|completes|completed|completing" |
| 33 | + |
| 34 | + # Check for magic words (non-closing) |
| 35 | + non_closing_words="ref|refs|references|part of|related to|contributes to|toward|towards" |
| 36 | + |
| 37 | + # Check if commit message contains task ID |
| 38 | + if ! echo "$commit_msg" | grep -iE "$task_id_pattern" >/dev/null 2>&1; then |
| 39 | + echo "${RED}${CROSS} FAILED${NC}" |
| 40 | + echo " ${RED}Commit message missing task ID${NC}" |
| 41 | + echo " ${YELLOW}Your commit message:${NC}" |
| 42 | + echo " ${BLUE}\"$commit_msg\"${BLUE}" |
| 43 | + echo "" |
| 44 | + echo " ${YELLOW}Expected format: [MAGIC_WORD]: [TASK-ID]${NC}" |
| 45 | + echo " ${YELLOW}Correct example: ${BLUE}\"$commit_msg, Ref: DEV-123\"${NC}" |
| 46 | + echo " ${YELLOW}See: ${NC}${BLUE}https://linear.app/docs/github?tabs=206cad22125a#link-through-pull-requests${NC}" |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | + |
| 50 | + # Check if commit message contains magic words |
| 51 | + if ! echo "$commit_msg" | grep -iE "($closing_words|$non_closing_words)" >/dev/null 2>&1; then |
| 52 | + echo "${RED}${CROSS} FAILED${NC}" |
| 53 | + echo " ${RED}Commit message missing magic words${NC}" |
| 54 | + echo " ${YELLOW}Your commit message:${NC}" |
| 55 | + echo " ${PURPLE}\"$commit_msg\"${NC}" |
| 56 | + echo " ${YELLOW}Closing magic words: ${NC}${PURPLE}close, closes, closed, closing, fix, fixes, fixed, fixing, resolve, resolves, resolved, resolving, complete, completes, completed, completing${NC}" |
| 57 | + echo " ${YELLOW}Non-closing magic words: ${NC}${PURPLE}ref, refs, references, part of, related to, contributes to, toward, towards${NC}" |
| 58 | + # Extract task ID from commit message if present for dynamic example |
| 59 | + extracted_task_id=$(echo "$commit_msg" | grep -ioE "$task_id_pattern" | head -1 || echo "DEV-123") |
| 60 | + echo " ${YELLOW}Correct examples:${NC}" |
| 61 | + echo " ${PURPLE}\"$commit_msg, Closes $extracted_task_id\"${NC}" |
| 62 | + echo " ${PURPLE}\"$commit_msg, Ref: $extracted_task_id\"${NC}" |
| 63 | + echo " ${YELLOW}See: ${NC}${PURPLE}https://linear.app/docs/github?tabs=206cad22125a#link-through-pull-requests${NC}" |
| 64 | + exit 1 |
| 65 | + fi |
| 66 | + |
| 67 | + echo "${GREEN}${CHECK} OK${NC}" |
| 68 | + |
| 69 | + # If we reach this point, ALL hooks have passed - show success box |
| 70 | + echo "" |
| 71 | + |
| 72 | + # Create ASCII box with reminders |
| 73 | + print_success_box() { |
| 74 | + local box_width=60 |
| 75 | + local border="${GREEN}╔$(printf '═%.0s' $(seq 1 $((box_width-2))))╗${NC}" |
| 76 | + local bottom="${GREEN}╚$(printf '═%.0s' $(seq 1 $((box_width-2))))╝${NC}" |
| 77 | + |
| 78 | + echo "$border" |
| 79 | + echo "${GREEN}║${NC} ${GREEN}✅ All checks passed! Ready to push!${NC}$(printf '%*s' 21 '')${GREEN}║${NC}" |
| 80 | + echo "${GREEN}║${NC}$(printf '%*s' 58 '')${GREEN}║${NC}" |
| 81 | + echo "${GREEN}║${NC} ${GREEN}💡 Reminders before pushing:${NC}$(printf '%*s' 29 '')${GREEN}║${NC}" |
| 82 | + echo "${GREEN}║${NC}$(printf '%*s' 58 '')${GREEN}║${NC}" |
| 83 | + |
| 84 | + # Check CHANGELOG.md age |
| 85 | + if [ -f "CHANGELOG.md" ]; then |
| 86 | + changelog_age_minutes=$(find CHANGELOG.md -mmin +0 -printf '%Tm\n' 2>/dev/null || echo "0") |
| 87 | + if [ "$changelog_age_minutes" -gt 10 ]; then |
| 88 | + echo "${GREEN}║${NC} ${GREEN}• Update CHANGELOG.md with recent changes${NC}$(printf '%*s' 20 '')${GREEN}║${NC}" |
| 89 | + fi |
| 90 | + |
| 91 | + # Check if CHANGELOG.md has today's date |
| 92 | + changelog_first_line=$(head -n 1 CHANGELOG.md) |
| 93 | + current_date=$(date +%Y-%m-%d) |
| 94 | + if ! echo "$changelog_first_line" | grep -q "$current_date"; then |
| 95 | + echo "${GREEN}║${NC} ${GREEN}• Consider updating changelog date to $current_date${NC}$(printf '%*s' 10 '')${GREEN}║${NC}" |
| 96 | + fi |
| 97 | + fi |
| 98 | + |
| 99 | + # Check for uncommitted files |
| 100 | + uncommitted_files=$(git status --porcelain | wc -l | tr -d ' ') |
| 101 | + if [ "$uncommitted_files" -gt 0 ]; then |
| 102 | + if [ "$uncommitted_files" -ge 10 ]; then |
| 103 | + echo "${GREEN}║${NC} ${GREEN}• You have $uncommitted_files uncommitted files${NC}$(printf '%*s' 27 '')${GREEN}║${NC}" |
| 104 | + else |
| 105 | + echo "${GREEN}║${NC} ${GREEN}• You have $uncommitted_files uncommitted files${NC}$(printf '%*s' 27 '')${GREEN}║${NC}" |
| 106 | + fi |
| 107 | + fi |
| 108 | + |
| 109 | + # Check current branch |
| 110 | + current_branch=$(git branch --show-current 2>/dev/null || echo "unknown") |
| 111 | + if [ "$current_branch" = "master" ] || [ "$current_branch" = "main" ]; then |
| 112 | + echo "${GREEN}║${NC} ${GREEN}• You're on '$current_branch' - use task ID branches, if possible ${NC}$(printf '%*s' 0 '')${GREEN}║${NC}" |
| 113 | + fi |
| 114 | + |
| 115 | + # General reminders |
| 116 | + echo "${GREEN}║${NC} ${GREEN}• Test changes locally before pushing${NC}$(printf '%*s' 20 '')${GREEN}║${NC}" |
| 117 | + echo "${GREEN}║${NC} ${GREEN}• When in doubt, huddle calls and docs are your friends${NC}$(printf '%*s' 2 '')${GREEN}║${NC}" |
| 118 | + |
| 119 | + # Check if this is Friday afternoon |
| 120 | + day_of_week=$(date +%u) # 1=Monday, 7=Sunday |
| 121 | + hour_of_day=$(date +%H) |
| 122 | + if [ "$day_of_week" -eq 5 ] && [ "$hour_of_day" -ge 15 ]; then |
| 123 | + echo "${GREEN}║${NC} ${GREEN}⚠️ Friday afternoon - defer non-critical deploys${NC}$(printf '%*s' 8 '')${GREEN}║${NC}" |
| 124 | + fi |
| 125 | + |
| 126 | + echo "$bottom" |
| 127 | + } |
| 128 | + |
| 129 | + print_success_box |
| 130 | +else |
| 131 | + echo "${YELLOW}⚠️ SKIPPED (no commit message file)${NC}" |
| 132 | +fi |
0 commit comments