-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.py
More file actions
141 lines (104 loc) · 3.4 KB
/
python.py
File metadata and controls
141 lines (104 loc) · 3.4 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
import subprocess
import requests
import sys
import json
import os
config_path = os.path.join(os.path.dirname(__file__), 'config.json')
with open(config_path, 'r') as f:
config = json.load(f)
MODEL = config.get('model', 'qwen2.5-coder:7b')
def get_diff():
result = subprocess.run(
[
'git', 'diff', '--staged',
'-U5',
'--function-context',
'-w',
'--ignore-blank-lines'
],
capture_output=True,
text=True
)
return result.stdout
def classify_diff(diff):
added = []
removed = []
for line in diff.splitlines():
if line.startswith('+') and not line.startswith('+++'):
added.append(line[1:].strip())
elif line.startswith('-') and not line.startswith('---'):
removed.append(line[1:].strip())
added = [l for l in added if l]
removed = [l for l in removed if l]
total_changes = len(added) + len(removed)
if total_changes <= 2:
return "trivial"
if sorted(added) == sorted(removed):
if total_changes > 10:
return "reorder"
else:
return "trivial"
return "functional"
def clean_message(msg):
msg = msg.strip().lower()
if not msg:
return "update code logic"
if len(msg.split()) > 10:
return "update code logic"
if any(word in msg for word in ["format", "whitespace", "blank"]):
return "minor code tweak"
return msg
def generate_commit_message(diff):
try:
response = requests.post(
'http://localhost:11434/api/generate',
json={
"model": "qwen2.5-coder:7b",
"prompt": f"""Write a git commit message (4-8 words).
ONLY describe meaningful behavioral changes.
Make your responces short but NO LONGER THAN 8 WORDS.
The Commit message could be what then theme of the changes was and them __ improvement or the them of the change with __ added
Use the surrounding function context to understand intent.
IGNORE:
- formatting
- whitespace
- reordering
Diff with context:
{diff}
""",
"stream": False
},
timeout=10
)
data = response.json()
if 'response' not in data:
print("API error:", data)
return "update code logic"
return data['response'].strip()
except Exception as e:
print("Request failed:", e)
return "update code logic"
if __name__ == "__main__":
dry_run = '--dry-run' in sys.argv
verbose = '--verbose' in sys.argv
if verbose:
print(f"Diff being sent:\n{diff}\n")
diff = get_diff()
if not diff.strip():
print("No staged changes — run 'git add' first")
exit(0)
dry_run = '--dry-run' in sys.argv
change_type = classify_diff(diff)
if change_type == "trivial":
message = "minor code tweak"
elif change_type == "reorder":
message = "refactor code structure"
else:
message = generate_commit_message(diff)
message = clean_message(message)
print(f"Change type: {change_type}")
print(f"Committing: {message}")
if dry_run:
print("Dry run — skipping commit")
else:
subprocess.run(['git', 'commit', '-m', message])