-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitcommit.py
More file actions
99 lines (83 loc) · 2.96 KB
/
gitcommit.py
File metadata and controls
99 lines (83 loc) · 2.96 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
import os
import openai
import json
import subprocess
def load_config():
"""Load the configuration file."""
config_path = "~/.gitcommit.json"
with open(os.path.expanduser(config_path), "r") as f:
return json.load(f)
def get_git_changes():
"""Stage all changes (equivalent to 'git add -A')"""
try:
subprocess.run(["git", "add", "--all"], check=True)
except subprocess.CalledProcessError as e:
print(f"Error staging changes: {e.stderr.strip()}")
exit(1)
"""Get a summary of changes added to the staging area."""
try:
result = subprocess.run(
["git", "diff", "--cached"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True
)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Error: {e.stderr.strip()}")
exit(1)
def generate_commit_message(changes_summary):
"""Generate a Git commit message using the OpenAI ChatCompletion API."""
config = load_config()
openai.api_key = config.get("api_key")
response = openai.chat.completions.create(
model=config.get("model", "gpt-4o"),
messages=[
{"role": "system", "content": "You are an expert Git commit message generator."},
{"role": "user", "content": f"Generate a concise, properly formatted Git commit message based on the following changes:\n{changes_summary}"}
],
max_tokens=config.get("max_tokens", 100),
temperature=0.7,
)
commit_message = response.choices[0].message.content
commit_message = commit_message.strip("```").strip()
return commit_message
def create_commit(dry_run=False):
"""Create a Git commit with a generated message."""
changes_summary = get_git_changes()
if not changes_summary:
print("No changes staged for commit.")
exit(0)
# print ("\nGit changes:\n")
# print (changes_summary)
# print ("\n")
commit_message = generate_commit_message(changes_summary)
print("\nGenerated Commit Message:\n")
print(commit_message)
print("\n")
if dry_run:
print("Dry run mode: not committing.")
return
# Prompt the user for confirmation
confirm = input("Commit this message? (y/N): ").strip().lower()
if confirm != "y":
print("Commit canceled.")
return
try:
subprocess.run(
["git", "commit", "-m", commit_message],
check=True
)
except subprocess.CalledProcessError as e:
print(f"Error: {e.stderr.strip()}")
exit(1)
def main():
"""Main entry point."""
import argparse
parser = argparse.ArgumentParser(description="Generate Git commit messages with OpenAI.")
parser.add_argument("--test", action="store_true", help="Preview the commit message without committing.")
args = parser.parse_args()
create_commit(dry_run=args.test)
if __name__ == "__main__":
main()