-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path02_git_commit_summarizer.py
More file actions
64 lines (53 loc) · 1.76 KB
/
Copy path02_git_commit_summarizer.py
File metadata and controls
64 lines (53 loc) · 1.76 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
import os
import subprocess
from google import genai
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
def get_git_diff():
"""
Retrieves the current staged changes (git diff --cached).
"""
try:
result = subprocess.run(
["git", "diff", "--cached"],
capture_output=True,
text=True,
check=True
)
return result.stdout
except subprocess.CalledProcessError as e:
return f"Error retrieving git diff: {e}"
def generate_commit_message(diff_text):
"""
Uses Gemini to generate a conventional commit message based on the diff.
"""
if not diff_text.strip():
return "No staged changes found to summarize."
client = genai.Client(api_key=os.getenv("GEMINI_API_KEY"))
prompt = (
"Analyze the following git diff and generate a perfectly formatted conventional commit message. "
"Use the format: <type>(<scope>): <subject>\n\n<body>\n\n<footer>\n\n"
"Types: feat, fix, docs, style, refactor, test, chore.\n\n"
f"Diff:\n{diff_text}"
)
try:
response = client.models.generate_content(
model="gemini-2.0-flash",
contents=prompt
)
return response.text
except Exception as e:
return f"Error generating commit message: {str(e)}"
def main():
print("Fetching staged changes...")
diff = get_git_diff()
if not diff.strip():
print("No staged changes found. Use 'git add' to stage some changes.")
return
print("Generating conventional commit message...")
commit_message = generate_commit_message(diff)
print("\n--- SUGGESTED COMMIT MESSAGE ---\n")
print(commit_message)
if __name__ == "__main__":
main()