Skip to content

Commit 3a61347

Browse files
committed
feat: Update TFO-Python-SDK git hooks
1 parent 0aae540 commit 3a61347

3 files changed

Lines changed: 714 additions & 184 deletions

File tree

docs/githooks/commit-msg

Lines changed: 140 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,170 @@
11
#!/bin/bash
2-
3-
# Commit-msg hook for TelemetryFlow projects
4-
# Enforces Conventional Commits format
2+
# ==============================================================================
3+
# TelemetryFlow SDK (Python) - Commit Message Hook
4+
# ==============================================================================
5+
# Validates commit messages follow Conventional Commits format
56
# https://www.conventionalcommits.org/
7+
#
8+
# Format: <type>(<scope>): <subject>
9+
#
10+
# Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
11+
# Scope: optional, indicates the module/area affected
12+
# Subject: short description of the change
13+
# ==============================================================================
614

715
set -e
816

9-
COMMIT_MSG_FILE=$1
10-
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
11-
1217
# Colors for output
1318
RED='\033[0;31m'
1419
GREEN='\033[0;32m'
1520
YELLOW='\033[1;33m'
21+
BLUE='\033[0;34m'
22+
CYAN='\033[0;36m'
1623
NC='\033[0m' # No Color
1724

18-
# Skip merge commits
19-
if echo "$COMMIT_MSG" | grep -qE "^Merge"; then
25+
# Helper functions
26+
info() { echo -e "${BLUE}ℹ️ $1${NC}"; }
27+
success() { echo -e "${GREEN}$1${NC}"; }
28+
warning() { echo -e "${YELLOW}⚠️ $1${NC}"; }
29+
error() { echo -e "${RED}$1${NC}"; }
30+
31+
# Get the commit message file
32+
COMMIT_MSG_FILE=$1
33+
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
34+
35+
# Skip validation for merge commits
36+
if echo "$COMMIT_MSG" | grep -qE "^Merge "; then
2037
exit 0
2138
fi
2239

23-
# Conventional commit pattern
24-
# Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
25-
# Optional scope in parentheses
26-
# Optional breaking change indicator (!)
27-
# Colon and space, then description
28-
PATTERN="^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?(!)?: .{1,}"
29-
30-
# Check if commit message follows conventional commits format
31-
if ! echo "$COMMIT_MSG" | head -1 | grep -qE "$PATTERN"; then
32-
echo -e "${RED}ERROR: Commit message does not follow Conventional Commits format.${NC}"
40+
# Skip validation for revert commits
41+
if echo "$COMMIT_MSG" | grep -qE "^Revert "; then
42+
exit 0
43+
fi
44+
45+
# Skip validation for fixup/squash commits
46+
if echo "$COMMIT_MSG" | grep -qE "^(fixup|squash)! "; then
47+
exit 0
48+
fi
49+
50+
echo ""
51+
echo -e "${BLUE}╔══════════════════════════════════════════════════════════════╗${NC}"
52+
echo -e "${BLUE}║ TelemetryFlow SDK (Python) - Commit Message Check ║${NC}"
53+
echo -e "${BLUE}╚══════════════════════════════════════════════════════════════╝${NC}"
54+
echo ""
55+
56+
# Get only the first line (subject)
57+
FIRST_LINE=$(echo "$COMMIT_MSG" | head -n1)
58+
59+
# Define valid types
60+
VALID_TYPES="feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert"
61+
62+
# Define valid scopes for TelemetryFlow SDK (Python)
63+
VALID_SCOPES="client|tracer|meter|logger|span|metric|log|context|propagator|exporter|sampler|resource|attribute|baggage|config|http|grpc|otlp|console|batch|retry|tls|example|benchmark|async|decorator|middleware|flask|django|fastapi"
64+
65+
# Regex pattern for conventional commits
66+
# Format: type(scope): subject OR type: subject
67+
PATTERN="^($VALID_TYPES)(\(($VALID_SCOPES)\))?: .{1,100}$"
68+
69+
# Check if message matches the pattern
70+
if ! echo "$FIRST_LINE" | grep -qE "$PATTERN"; then
71+
error "Invalid commit message format!"
3372
echo ""
34-
echo -e "${YELLOW}Your commit message:${NC}"
35-
echo "$COMMIT_MSG" | head -1
73+
echo -e "${YELLOW}Your message:${NC}"
74+
echo " $FIRST_LINE"
3675
echo ""
37-
echo -e "${YELLOW}Expected format:${NC}"
38-
echo " <type>[optional scope][!]: <description>"
76+
echo -e "${CYAN}Expected format:${NC}"
77+
echo " <type>(<scope>): <subject>"
78+
echo " or"
79+
echo " <type>: <subject>"
3980
echo ""
40-
echo -e "${YELLOW}Allowed types:${NC}"
41-
echo " feat: A new feature"
42-
echo " fix: A bug fix"
43-
echo " docs: Documentation only changes"
44-
echo " style: Code style changes (formatting, semicolons, etc)"
45-
echo " refactor: Code change that neither fixes a bug nor adds a feature"
46-
echo " perf: Performance improvement"
47-
echo " test: Adding or updating tests"
48-
echo " build: Build system or external dependency changes"
49-
echo " ci: CI configuration changes"
50-
echo " chore: Other changes that don't modify src or test files"
51-
echo " revert: Reverts a previous commit"
81+
echo -e "${CYAN}Valid types:${NC}"
82+
echo " feat - A new feature"
83+
echo " fix - A bug fix"
84+
echo " docs - Documentation only changes"
85+
echo " style - Code style changes (formatting, etc)"
86+
echo " refactor - Code change that neither fixes a bug nor adds a feature"
87+
echo " perf - Performance improvements"
88+
echo " test - Adding or correcting tests"
89+
echo " build - Build system or external dependencies"
90+
echo " ci - CI configuration files and scripts"
91+
echo " chore - Other changes that don't modify src or test files"
92+
echo " revert - Reverts a previous commit"
5293
echo ""
53-
echo -e "${YELLOW}Examples:${NC}"
54-
echo " feat: add user authentication"
55-
echo " fix(api): resolve null pointer in handler"
56-
echo " docs: update README with installation steps"
57-
echo " feat(auth)!: change token format (breaking change)"
94+
echo -e "${CYAN}Valid scopes (optional):${NC}"
95+
echo " client, tracer, meter, logger, span, metric, log, context,"
96+
echo " propagator, exporter, sampler, resource, attribute, baggage,"
97+
echo " config, http, grpc, otlp, console, batch, retry, tls, example,"
98+
echo " benchmark, async, decorator, middleware, flask, django, fastapi"
99+
echo ""
100+
echo -e "${CYAN}Examples:${NC}"
101+
echo " feat(tracer): add automatic context propagation"
102+
echo " fix(exporter): resolve async export buffer issue"
103+
echo " docs: update SDK installation guide"
104+
echo " refactor(decorator): simplify trace decorator logic"
105+
echo " test(fastapi): add integration tests for FastAPI middleware"
58106
echo ""
59107
exit 1
60108
fi
61109

62-
# Check minimum description length (at least 10 characters after the type)
63-
DESCRIPTION=$(echo "$COMMIT_MSG" | head -1 | sed -E 's/^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?(!)?: //')
64-
if [ ${#DESCRIPTION} -lt 10 ]; then
65-
echo -e "${RED}ERROR: Commit description is too short (minimum 10 characters).${NC}"
66-
echo -e "${YELLOW}Current description:${NC} $DESCRIPTION"
110+
# Check subject length
111+
SUBJECT_LENGTH=${#FIRST_LINE}
112+
if [ "$SUBJECT_LENGTH" -gt 100 ]; then
113+
error "Commit message subject is too long!"
114+
echo ""
115+
echo " Current length: $SUBJECT_LENGTH characters"
116+
echo " Maximum allowed: 100 characters"
117+
echo ""
118+
echo "Please shorten your commit message."
67119
exit 1
68120
fi
69121

70-
# Check that description doesn't start with capital letter (optional but recommended)
71-
if echo "$DESCRIPTION" | grep -qE "^[A-Z]"; then
72-
echo -e "${YELLOW}WARNING: Description should start with lowercase letter.${NC}"
73-
echo -e "${YELLOW}Current:${NC} $DESCRIPTION"
74-
# This is just a warning, not a failure
122+
# Check for period at the end
123+
if echo "$FIRST_LINE" | grep -qE "\.$"; then
124+
warning "Commit message should not end with a period"
125+
echo " Consider removing the trailing period"
75126
fi
76127

77-
# Check commit message line length (first line should be <= 72 characters)
78-
FIRST_LINE_LENGTH=$(echo "$COMMIT_MSG" | head -1 | wc -c | tr -d ' ')
79-
if [ "$FIRST_LINE_LENGTH" -gt 72 ]; then
80-
echo -e "${YELLOW}WARNING: First line exceeds 72 characters (${FIRST_LINE_LENGTH} chars).${NC}"
81-
echo -e "${YELLOW}Consider shortening the commit message.${NC}"
82-
# This is just a warning, not a failure
128+
# Check for uppercase first letter after colon
129+
SUBJECT=$(echo "$FIRST_LINE" | sed 's/^[^:]*: //')
130+
FIRST_CHAR=$(echo "$SUBJECT" | cut -c1)
131+
if echo "$FIRST_CHAR" | grep -qE "[A-Z]"; then
132+
warning "Subject should start with lowercase letter"
133+
echo " Current: $SUBJECT"
134+
echo " Consider: $(echo "$SUBJECT" | sed 's/^./\L&/')"
83135
fi
84136

85-
echo -e "${GREEN}Commit message follows Conventional Commits format.${NC}"
137+
# Extract type for emoji suggestion
138+
TYPE=$(echo "$FIRST_LINE" | sed 's/^\([^(:]*\).*/\1/')
139+
140+
# Success message with emoji based on type
141+
case "$TYPE" in
142+
feat) EMOJI="" ;;
143+
fix) EMOJI="🐛" ;;
144+
docs) EMOJI="📚" ;;
145+
style) EMOJI="💄" ;;
146+
refactor) EMOJI="♻️" ;;
147+
perf) EMOJI="" ;;
148+
test) EMOJI="🧪" ;;
149+
build) EMOJI="🔨" ;;
150+
ci) EMOJI="👷" ;;
151+
chore) EMOJI="🔧" ;;
152+
revert) EMOJI="" ;;
153+
*) EMOJI="📝" ;;
154+
esac
155+
156+
echo ""
157+
success "Commit message is valid! $EMOJI"
158+
echo ""
159+
echo -e " ${CYAN}Type:${NC} $TYPE"
160+
161+
# Extract and display scope if present
162+
if echo "$FIRST_LINE" | grep -qE "^[^(]+\([^)]+\)"; then
163+
SCOPE=$(echo "$FIRST_LINE" | sed 's/^[^(]*(\([^)]*\)).*/\1/')
164+
echo -e " ${CYAN}Scope:${NC} $SCOPE"
165+
fi
166+
167+
echo -e " ${CYAN}Subject:${NC} $SUBJECT"
168+
echo ""
169+
86170
exit 0

0 commit comments

Comments
 (0)