-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit_progress.py
More file actions
174 lines (144 loc) · 4.99 KB
/
commit_progress.py
File metadata and controls
174 lines (144 loc) · 4.99 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import subprocess
import json
from pathlib import Path
from datetime import datetime
def get_commit_info(repo_path='.'):
"""
Retrieves comprehensive commit information including metadata and diff.
Args:
repo_path (str): Path to the Git repository. Defaults to current directory.
Returns:
dict: Structured commit data or error information.
"""
try:
# Get commit metadata
metadata_result = subprocess.run(
['git', 'log', '-1', '--pretty=format:%H|%an|%ae|%ad|%s|%b'],
cwd=repo_path,
capture_output=True,
text=True,
check=True
)
# Parse metadata
parts = metadata_result.stdout.split('|', 5)
commit_hash, author, email, date, subject, body = parts if len(parts) == 6 else (*parts, '')
# Get diff with stats
diff_result = subprocess.run(
['git', 'diff', 'HEAD~1', 'HEAD'],
cwd=repo_path,
capture_output=True,
text=True,
check=True
)
# Get file statistics
stats_result = subprocess.run(
['git', 'diff', '--stat', 'HEAD~1', 'HEAD'],
cwd=repo_path,
capture_output=True,
text=True,
check=True
)
# Get list of changed files
files_result = subprocess.run(
['git', 'diff', '--name-status', 'HEAD~1', 'HEAD'],
cwd=repo_path,
capture_output=True,
text=True,
check=True
)
return {
'success': True,
'metadata': {
'commit_hash': commit_hash,
'author': author,
'email': email,
'date': date,
'subject': subject,
'body': body.strip()
},
'stats': stats_result.stdout,
'files_changed': files_result.stdout,
'diff': diff_result.stdout
}
except subprocess.CalledProcessError as e:
return {'success': False, 'error': f"Git command failed: {e.stderr}"}
except FileNotFoundError:
return {'success': False, 'error': "Git not found. Ensure Git is installed."}
except Exception as e:
return {'success': False, 'error': f"Unexpected error: {str(e)}"}
def format_for_llm(commit_data):
"""
Formats commit data into an LLM-friendly text format.
Args:
commit_data (dict): Structured commit data from get_commit_info.
Returns:
str: Formatted text ready for LLM processing.
"""
if not commit_data.get('success'):
return f"ERROR: {commit_data.get('error', 'Unknown error')}"
meta = commit_data['metadata']
output = f"""# Git Commit Analysis Report
Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
## Commit Metadata
- **Commit Hash**: {meta['commit_hash']}
- **Author**: {meta['author']} <{meta['email']}>
- **Date**: {meta['date']}
- **Subject**: {meta['subject']}
## Commit Message
{meta['body'] if meta['body'] else '(No additional commit message)'}
## Files Changed Summary
```
{commit_data['stats']}
```
## Detailed File Changes
```
{commit_data['files_changed']}
```
## Full Diff
```diff
{commit_data['diff']}
```
---
## Instructions for LLM
Please analyze the above commit and create a progress.md report that includes:
1. A summary of what changed in this commit
2. The purpose/goal of these changes
3. Key modifications broken down by file or feature
4. Impact assessment (what functionality is affected)
5. Technical notes (any patterns, refactoring, or architectural changes)
6. Progress made towards project goals
"""
return output
def save_commit_analysis(repo_path='.', output_file='commit_analysis.txt'):
"""
Main function: Analyzes last commit and saves LLM-ready report to file.
Args:
repo_path (str): Path to the Git repository.
output_file (str): Output filename for the analysis.
Returns:
str: Path to the saved file or error message.
"""
commit_data = get_commit_info(repo_path)
formatted_text = format_for_llm(commit_data)
try:
output_path = Path(output_file)
output_path.write_text(formatted_text, encoding='utf-8')
return f"Analysis saved to: {output_path.absolute()}"
except Exception as e:
return f"Failed to save file: {str(e)}"
# Usage examples
if __name__ == "__main__":
# Example 1: Save analysis to default file
result = save_commit_analysis()
print(result)
# Example 2: Custom repo and output file
# result = save_commit_analysis(
# repo_path='/path/to/repo',
# output_file='llm_commit_analysis.txt'
# )
# print(result)
# Example 3: Get data programmatically
# commit_data = get_commit_info()
# if commit_data['success']:
# print("Commit subject:", commit_data['metadata']['subject'])
# print("Files changed:", commit_data['files_changed'])