-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-commit-msg
More file actions
executable file
·170 lines (148 loc) · 4.93 KB
/
prepare-commit-msg
File metadata and controls
executable file
·170 lines (148 loc) · 4.93 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/bin/bash
# AI-powered commit message generator (supports OpenAI and Groq)
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
# Skip if not a regular commit
if [ -n "$COMMIT_SOURCE" ]; then
exit 0
fi
# Skip if a commit message was already provided
if grep -v "^#" "$COMMIT_MSG_FILE" | grep -q "[^[:space:]]"; then
exit 0
fi
# Get API keys
OPENAI_API_KEY=$(git config --get openai.apikey || echo $OPENAI_API_KEY)
OPENAI_MODEL=$(git config --get openai.model || echo $OPENAI_MODEL)
GROQ_API_KEY=$(git config --get groq.apikey || echo $GROQ_API_KEY)
GROQ_MODEL=$(git config --get groq.model || echo $GROQ_MODEL)
if [ -z "$OPENAI_API_KEY" ] && [ -z "$GROQ_API_KEY" ]; then
echo "No API key found for OpenAI or Groq."
exit 1
fi
# Get diff
DIFF_FILE=$(mktemp)
git diff --cached >"$DIFF_FILE"
# Call the appropriate API
echo "Generating commit message..."
RESPONSE_FILE=$(mktemp)
# Unified Python code for both APIs
OPENAI_API_KEY="$OPENAI_API_KEY" GROQ_API_KEY="$GROQ_API_KEY" OPENAI_MODEL="$OPENAI_MODEL" GROQ_MODEL="$GROQ_MODEL" python3 <<EOF >/dev/null
import os
import json
import requests
import sys
# Read the diff
with open("$DIFF_FILE", "r") as f:
diff_content = f.read()
# Determine which API to use
groq_api_key = os.environ.get('GROQ_API_KEY') or None
groq_model = os.environ.get('GROQ_MODEL') or None
openai_api_key = os.environ.get('OPENAI_API_KEY') or None
openai_model = os.environ.get('OPENAI_MODEL') or None
if groq_api_key:
# Use Groq API
api_key = groq_api_key
endpoint = "https://api.groq.com/openai/v1/chat/completions"
model = groq_model
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"messages": [
{
"role": "user",
"content": f"Generate a concise and meaningful commit message following the Conventional Commit specification <type>(optional scope): <description> for the given code changes."
},
{
"role": "user",
"content": f"Generate a commit message for these changes:\n\n{diff_content}"
},
{
"role": "user",
"content": f"Avoid making any other comment. Just provide the final commit message."
}
],
"model": model,
"temperature": 0.10,
"max_completion_tokens": 4096,
"top_p": 0.95,
"stream": False,
"stop": None
}
elif openai_api_key:
# Use OpenAI API
api_key = openai_api_key
endpoint = "https://api.openai.com/v1/chat/completions"
model = openai_model
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": model,
"messages": [
{
"role": "user",
"content": f"Generate a concise and meaningful commit message following the Conventional Commit specification <type>(optional scope): <description> for the given code changes."
},
{
"role": "user",
"content": f"Generate a commit message for these changes:\n\n{diff_content}"
},
{
"role": "user",
"content": f"Avoid making any other comment. Just provide the final commit message."
}
],
}
else:
print("No LLM was configured")
sys.exit()
# Make the API call
try:
response = requests.post(endpoint, headers=headers, json=data)
response.raise_for_status() # Raise exception for 4XX/5XX responses
# Save the response
with open("$RESPONSE_FILE", "w") as f:
f.write(response.text)
except Exception as e:
# Write error message to response file
with open("$RESPONSE_FILE", "w") as f:
f.write(f"Error: API request failed - {str(e)}")
EOF
# Check if there was an error
if grep -q "Error:" "$RESPONSE_FILE"; then
echo "Failed to call API:"
cat "$RESPONSE_FILE"
rm "$DIFF_FILE" "$RESPONSE_FILE"
exit 1
fi
# Extract commit message
COMMIT_MSG=$(python3 -c "
import json
import re
with open('$RESPONSE_FILE', 'r') as f:
try:
response = json.load(f)
if 'choices' in response and len(response['choices']) > 0:
content = response['choices'][0]['message']['content']
# Remove <think>...</think> sections if present
content = re.sub(r'<think>.*?</think>', '', content, flags=re.DOTALL)
# Trim whitespace
content = content.strip()
print(content)
else:
print('Failed to generate commit message')
except Exception as e:
print(f'Error parsing response: {e}')
")
# Use the generated message
if [ -n "$COMMIT_MSG" ] && [ "$COMMIT_MSG" != "Failed to generate commit message" ]; then
echo "$COMMIT_MSG" >"$COMMIT_MSG_FILE"
echo "Commit message generated successfully."
else
echo "Failed to generate commit message."
fi
# Clean up
rm "$DIFF_FILE" "$RESPONSE_FILE"