-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_commit_msg.py
More file actions
executable file
·151 lines (128 loc) · 4.7 KB
/
generate_commit_msg.py
File metadata and controls
executable file
·151 lines (128 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python3
"""
Generate detailed commit messages by analyzing git diff
"""
import subprocess
import sys
from datetime import datetime
def get_git_diff():
"""Get the staged changes"""
try:
# Get the diff of staged changes
result = subprocess.run(['git', 'diff', '--cached'],
capture_output=True, text=True, check=True)
return result.stdout
except subprocess.CalledProcessError:
return ""
def get_changed_files():
"""Get list of changed files"""
try:
result = subprocess.run(['git', 'diff', '--cached', '--name-only'],
capture_output=True, text=True, check=True)
return result.stdout.strip().split('\n') if result.stdout.strip() else []
except subprocess.CalledProcessError:
return []
def analyze_changes(diff_text, files):
"""Analyze the diff to create a meaningful summary"""
if not diff_text or not files:
return "No changes detected", []
# Categorize files
categories = {
'config': [],
'source': [],
'docker': [],
'docs': [],
'other': []
}
for f in files:
if 'config' in f or f.endswith('.json'):
categories['config'].append(f)
elif f.endswith('.py'):
categories['source'].append(f)
elif 'docker' in f.lower() or f == 'Dockerfile':
categories['docker'].append(f)
elif f.endswith('.md'):
categories['docs'].append(f)
else:
categories['other'].append(f)
# Analyze specific changes
changes = []
# Look for common patterns in the diff
if 'requirements.txt' in files:
changes.append("Updated Python dependencies")
if 'Dockerfile' in files or 'docker-compose.yml' in files:
if 'CMD [' in diff_text:
changes.append("Modified Docker container entry point")
if 'RUN ' in diff_text:
changes.append("Updated Docker build steps")
if 'services:' in diff_text:
changes.append("Modified Docker Compose services")
if categories['config']:
if 'stt_config.json' in categories['config']:
changes.append("Updated STT configuration parameters")
else:
changes.append(f"Modified configuration files: {', '.join(categories['config'])}")
if categories['source']:
# Look for specific code patterns
if '+ class ' in diff_text:
changes.append("Added new class definitions")
if '- class ' in diff_text:
changes.append("Removed class definitions")
if 'def ' in diff_text and '+' in diff_text:
changes.append("Added or modified functions")
if 'import ' in diff_text:
changes.append("Updated imports")
if 'logging' in diff_text:
changes.append("Modified logging configuration")
# Generate title based on most significant change
if len(files) == 1:
file = files[0]
if file == 'Dockerfile':
title = "Update Docker configuration"
elif file.endswith('.py'):
title = f"Update {file.replace('src/', '').replace('.py', '')} module"
else:
title = f"Update {file}"
else:
if categories['source'] and len(categories['source']) > 2:
title = "Refactor codebase"
elif categories['config']:
title = "Update configuration"
elif categories['docker']:
title = "Update Docker setup"
else:
title = "Update project files"
return title, changes
def generate_commit_message():
"""Generate a detailed commit message"""
diff = get_git_diff()
files = get_changed_files()
if not files:
print("No changes to commit")
sys.exit(1)
title, changes = analyze_changes(diff, files)
# Build commit message
timestamp = datetime.now().strftime("%a %b %d %I:%M:%S %p %Z %Y")
message_parts = [
f"{title} at {timestamp}",
""
]
if changes:
message_parts.append("Summary of changes:")
for change in changes:
message_parts.append(f"- {change}")
message_parts.append("")
message_parts.append("Files modified:")
for f in files[:10]: # Limit to 10 files
message_parts.append(f"- {f}")
if len(files) > 10:
message_parts.append(f"... and {len(files) - 10} more files")
message_parts.extend([
"",
"🤖 Generated with Claude Code",
"",
"Co-Authored-By: Claude <noreply@anthropic.com>"
])
return '\n'.join(message_parts)
if __name__ == "__main__":
print(generate_commit_message())