-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitmessageai.py
267 lines (239 loc) · 9.67 KB
/
gitmessageai.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import subprocess
import os
from openai import OpenAI
from dotenv import load_dotenv
import argparse
import requests
import re
import json
load_dotenv()
def call_mistral_api(prompt):
api_key = os.environ.get("MISTRAL_API_KEY")
if not api_key:
print("Error: MISTRAL_API_KEY environment variable not set.")
return None
url = "https://api.mistral.ai/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": "mistral-small-latest",
"messages": [
{
"role": "user",
"content": prompt
}
]
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
response_json = response.json()
if response_json and response_json['choices']:
content = response_json['choices'][0]['message']['content']
return content
else:
print(f"Mistral API Error: Invalid response format: {response_json}")
return None
except requests.exceptions.RequestException as e:
print(f"Mistral API Error: {e}")
if e.response:
print(f"Response status code: {e.response.status_code}")
print(f"Response content: {e.response.text}")
return None
def call_gemini_api(prompt):
gemini_api_key = os.environ.get("GEMINI_API_KEY")
if not gemini_api_key:
print("Error: GEMINI_API_KEY environment variable not set.")
return None
url = "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent"
params = {"key": gemini_api_key}
payload = {"contents": [{"parts": [{"text": prompt}]}]}
try:
response = requests.post(url, json=payload, params=params)
response.raise_for_status() # Raise an exception for bad status codes
response_json = response.json()
if response_json and 'candidates' in response_json and response_json['candidates']:
return response_json['candidates'][0]['content']['parts'][0]['text']
else:
print(f"Gemini API Error: Invalid response format: {response_json}")
return None
except requests.exceptions.RequestException as e:
print(f"Gemini API Error: {e}")
if e.response:
print(f"Response status code: {e.response.status_code}")
print(f"Response content: {e.response.text}")
return None
def call_deepseek_api(prompt):
api_key = os.environ.get("DEEPSEEK_API_KEY")
if not api_key:
print("Error: DEEPSEEK_API_KEY environment variable not set.")
return None
client = OpenAI(api_key=api_key, base_url="https://api.deepseek.com")
try:
response = client.chat.completions.create(
model="deepseek-chat",
messages=[
{"role": "user", "content": prompt}
],
max_tokens=100
)
if response and response.choices:
commit_message = response.choices[0].message.content.strip()
commit_message = commit_message.replace('`', '')
return commit_message
else:
print("Error: No response from the API.")
return None
except Exception as e:
print(f"Error during API call: {e}")
print(e)
return None
def call_grok_api(prompt):
api_key = os.environ.get("GROK_API_KEY")
if not api_key:
print("Error: GROK_API_KEY environment variable not set.")
return None
url = "https://api.x.ai/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": "grok-2-latest",
"messages": [
{
"role": "user",
"content": prompt
}
]
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
json_response = response.json()
if 'choices' in json_response and json_response['choices']:
first_choice = json_response['choices'][0]
if 'message' in first_choice and 'content' in first_choice['message']:
return first_choice['message']['content']
else:
print("Unexpected response format: message or content missing")
return None
else:
print("No choices found in the response")
return None
except requests.exceptions.RequestException as e:
print(f"Error during API request: {e}")
if e.response:
print(f"Response status code: {e.response.status_code}")
print(f"Response content: {e.response.text}")
return None
except json.JSONDecodeError as e:
print(f"Error decoding JSON response: {e}")
return None
def gitmessageai(push=True, only_message=False, api='deepseek', allow_pull_push=False):
# Stage all changes
subprocess.run(["git", "add", "-A"], check=True)
# Get a detailed summary of the changes
diff_process = subprocess.run(["git", "diff", "--staged", "--unified=0"], capture_output=True, text=True, check=True)
diff_output = diff_process.stdout
if not diff_output:
print("No changes to commit.")
return
file_changes = []
current_file = None
lines = diff_output.splitlines()
i = 0
while i < len(lines):
line = lines[i]
if line.startswith("diff --git"):
parts = line.split(" ")
if len(parts) >= 4:
file_a = parts[2][2:]
file_b = parts[3][2:]
if file_a == file_b:
if i + 1 < len(lines) and "deleted file mode" in lines[i+1]:
file_changes.append(f"Deleted file {file_a}")
i += 1
elif i + 1 < len(lines) and "new file mode" in lines[i+1]:
file_changes.append(f"Added file {file_a}")
i += 1
elif i + 1 < len(lines):
file_changes.append(f"Updated file {file_a}")
i +=1
else:
if i + 1 < len(lines) and "similarity index" in lines[i+1]:
file_changes.append(f"Renamed file {file_a} to {file_b}")
i += 1
i += 1
for change in file_changes:
print(change)
if not file_changes:
print("No changes to commit.")
return
# Prepare the prompt for the AI
prompt = f"""
Generate a concise commit message in Conventional Commits format for the following code changes.
Use one of the following types: feat, fix, docs, style, refactor, test, chore, perf, ci, build, or revert.
If applicable, include a scope in parentheses to describe the part of the codebase affected.
The commit message should not exceed 70 characters. Just give the commit message, without any leading or trailing notes.
Changed files:
{', '.join(file_changes[:20])}
"""
if api == 'deepseek':
commit_message = call_deepseek_api(prompt)
if not commit_message:
return
elif api == 'gemini':
commit_message = call_gemini_api(prompt)
if not commit_message:
print("Error: No response from Gemini API.")
return
elif api == 'mistral':
commit_message = call_mistral_api(prompt)
if not commit_message:
print("Error: No response from Mistral API.")
return
elif api == 'grok':
commit_message = call_grok_api(prompt)
if not commit_message:
print("Error: No response from Grok API.")
return
else:
print(f"Error: Invalid API specified: {api}")
return
if commit_message and "```" in commit_message:
commit_message = commit_message.replace("```", "")
# Check if the commit message is empty
if not commit_message:
print("Error: Empty commit message generated. Aborting commit.")
return
if only_message:
print(f"Suggested commit message: {commit_message}")
return
# Commit with the generated message
subprocess.run(["git", "commit", "-m", commit_message], check=True)
# Push the changes
if push:
try:
subprocess.run(["git", "push"], check=True)
except subprocess.CalledProcessError as e:
if allow_pull_push:
print("Push failed, attempting pull and push...")
subprocess.run(["git", "pull", "--rebase"], check=True)
subprocess.run(["git", "push"], check=True)
else:
print("Push failed.")
raise e
else:
print("Changes committed locally, but not pushed.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Generate commit message with AI and commit changes.")
parser.add_argument('--no-push', dest='push', action='store_false', help='Commit changes locally without pushing.')
parser.add_argument('--only-message', dest='only_message', action='store_true', help='Only print the AI generated commit message.')
parser.add_argument('--api', type=str, default='mistral', choices=['deepseek', 'gemini', 'mistral', 'grok'], help='API to use for commit message generation (deepseek, gemini, mistral, grok).')
parser.add_argument('--allow-pull-push', dest='allow_pull_push', action='store_true', help='Allow git pull and push if git push failed.')
args = parser.parse_args()
gitmessageai(push=args.push, only_message=args.only_message, api=args.api, allow_pull_push=args.allow_pull_push)