-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaicommit.py
More file actions
86 lines (73 loc) · 3.01 KB
/
Copy pathaicommit.py
File metadata and controls
86 lines (73 loc) · 3.01 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
#!/usr/bin/python3
import os
import re
import openai
import argparse
import subprocess
BASE_URL = "https://api.groq.com/openai/v1"
#MODEL="meta-llama/llama-4-maverick-17b-128e-instruct"
#MODEL="meta-llama/llama-4-scout-17b-16e-instruct"
#MODEL="meta-llama/llama-guard-4-12b"
#MODEL="llama3-70b-8192"
MODEL="llama3-8b-8192"
#MODEL="llama-3.3-70b-versatile"
#MODEL="gemma2-9b-it"
#MODEL="qwen/qwen3-32b"
#MODEL="qwen-qwq-32b"
#MODEL="llama-3.1-8b-instant"
MODEL="deepseek-r1-distill-llama-70b"
def get_git_diff(filename=None):
"""Gets the Git diff of unstaged and staged changes."""
try:
if filename:
diff = subprocess.check_output(["git", "diff", filename], text=True)
else:
diff = subprocess.check_output(["git", "diff"], text=True)
if not diff.strip():
return None # No changes detected
return diff
except subprocess.CalledProcessError as e:
print(f"Error getting Git diff: {e}")
return None
def generate_commit_message(diff_text):
"""Generates a commit message using OpenAI's GPT."""
prompt = f"Summarize the following Git diff as a commit message. Output only the commit message, nothing else.\n\n{diff_text}"
try:
client = openai.OpenAI(base_url=BASE_URL, api_key=os.environ['API_KEY']) # Local LLM
response = client.chat.completions.create(
model=MODEL, # Not using gpt-4o for local
messages=[{"role": "system", "content": "You are a helpful assistant skilled in writing Git commit messages."},
{"role": "user", "content": prompt}],
max_tokens=2000
)
msg = response.choices[0].message.content.strip()
msg = cleaned = re.sub(r'<think>.*?</think>', '', msg, flags=re.DOTALL)
return msg
except Exception as e:
print(f"Error generating commit message: {e}")
return None
def apply_commit(commit_message, filename=None):
"""Stages and commits the changes with the generated message."""
try:
if filename:
subprocess.run(["git", "add", filename], check=True)
subprocess.run(["git", "commit", "-m", commit_message], check=True)
else:
subprocess.run(["git", "add", "."], check=True)
subprocess.run(["git", "commit", "-m", commit_message], check=True)
print(f"Committed successfully: {commit_message}")
except subprocess.CalledProcessError as e:
print(f"Error committing changes: {e}")
def main():
parser = argparse.ArgumentParser(description="Generate Git commit messages using AI.")
parser.add_argument("filename", nargs="?", help="Specific file to commit")
args = parser.parse_args()
if not (diff_text := get_git_diff(args.filename)):
print("No changes detected.")
return
if not (commit_message := generate_commit_message(diff_text)):
print("Failed to generate a commit message.")
return
apply_commit(commit_message, args.filename)
if __name__ == "__main__":
main()