-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommitmeant.py
More file actions
71 lines (53 loc) · 2.27 KB
/
commitmeant.py
File metadata and controls
71 lines (53 loc) · 2.27 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
import os
import subprocess
from src.openaiinteractions import OpenAIInteraction
class Commitmeant(OpenAIInteraction):
def __init__(self):
super().__init__()
def check_git_repository(self):
try:
subprocess.run(["git", "rev-parse", "--is-inside-work-tree"], check=True, capture_output=True)
except subprocess.CalledProcessError:
print("This script must be run inside a git repository.")
exit(1)
def stage_changes(self):
subprocess.run(["git", "add", "."])
def no_changes_detected(self):
return not subprocess.run(["git", "diff-index", "--quiet", "HEAD", "--"]).returncode
def get_git_diff(self):
git_diff = subprocess.run(["git", "diff", "--cached", "--no-color"], capture_output=True, text=True).stdout
messages = [
{"role": "system", "content": "Generate a short and concise commit message based on the following git diff:"},
{"role": "user", "content": git_diff},
]
# token_length = self.num_tokens_from_messages(messages)
# if token_length > 4097:
# print("Token length of the git diff is too large (> 4097). Cancelling commit.")
# exit(1)
return git_diff
def commit_changes(self, commit_message):
subprocess.run(["git", "commit", "-m", commit_message])
print(f"Committed changes with message: {commit_message}")
def main():
git_helper = Commitmeant()
git_helper.check_git_repository()
git_helper.stage_changes()
if git_helper.no_changes_detected():
print("No changes detected. Exiting.")
exit(0)
git_diff = git_helper.get_git_diff()
print("Raw diff: ", git_diff)
commit_message = ""
user_accepts = False
while not user_accepts:
commit_message = git_helper.generate_response(
system_prompt="Generate a short and concise commit message based on the following git diff:",
user_content=git_diff,
)
print(f"Suggested commit message: {commit_message}")
user_input = input("Is this commit message acceptable? (yes/no): ").lower()
if user_input in ["yes", "y"]:
user_accepts = True
git_helper.commit_changes(commit_message)
if __name__ == "__main__":
main()