Repository navigation
Expand file tree
/
Copy pathgpt4-git_commit.py
More file actions
61 lines (49 loc) · 2.08 KB
/
Copy pathgpt4-git_commit.py
File metadata and controls
61 lines (49 loc) · 2.08 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
import subprocess
import sys
import openai
import os
# Set your OpenAI API key as an environment variable for security
openai.api_key = os.environ.get("OPENAI_API_KEY")
def generate_commit_message(diff):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that generates concise, clear git commit messages."},
{"role": "user", "content": f"Generate a commit message for this diff:\n\n{diff}"}
],
max_tokens=50
)
return response['choices'][0]['message']['content'].strip()
def git_add_commit_and_push():
# Get list of modified and new files
status_output = subprocess.run(["git", "status", "--porcelain"], capture_output=True, text=True).stdout
files_to_add = []
for line in status_output.split('\n'):
if line.strip() and not line.startswith('??') and not '__pycache__' in line:
files_to_add.append(line.split()[-1])
elif line.startswith('??') and not '__pycache__' in line:
files_to_add.append(line.split()[-1])
if not files_to_add:
print("No changes to commit.")
return
# Add files
for file in files_to_add:
subprocess.run(["git", "add", file])
# Get the diff of staged changes
diff = subprocess.run(["git", "diff", "--cached"], capture_output=True, text=True).stdout
# Generate commit message
commit_message = generate_commit_message(diff)
# Commit changes
commit_result = subprocess.run(["git", "commit", "-m", commit_message], capture_output=True, text=True)
# Print the commit output
print(commit_result.stdout)
if commit_result.stderr:
print("Commit Errors:", commit_result.stderr, file=sys.stderr)
# Push changes
push_result = subprocess.run(["git", "push"], capture_output=True, text=True)
# Print the push output
print(push_result.stdout)
if push_result.stderr:
print("Push Errors:", push_result.stderr, file=sys.stderr)
if __name__ == "__main__":
git_add_commit_and_push()