-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·76 lines (59 loc) · 1.98 KB
/
pre-commit
File metadata and controls
executable file
·76 lines (59 loc) · 1.98 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
#!/bin/bash
# Load environment variables from .env file (if it exists)
if [ -f .env ]; then
export $(grep -v '^#' .env | xargs)
fi
# Get the diff of staged changes
DIFF=$(git diff --staged)
if [ -z "$DIFF" ]; then
echo "No staged changes. Skipping commit message generation."
exit 0
fi
# Base64 encode the diff
BASE64_DIFF=$(echo -n "$DIFF" | base64)
# Prepare the prompt for Gemini API
PROMPT="Write a concise commit message (30-50 words) based on the following git diff (Base64 encoded):\n\n$BASE64_DIFF"
# Call Gemini API
GEMINI_API_KEY="$GEMINI_API_KEY" # Read API key from environment variable
# Check if the API key is set
if [ -z "$GEMINI_API_KEY" ]; then
echo "Error: GEMINI_API_KEY environment variable is not set."
exit 1
fi
GEMINI_API_ENDPOINT="https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=$GEMINI_API_KEY"
API_RESPONSE=$(curl -s -X POST \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"parts": [{"text": "'"$PROMPT"'"}]
}],
"generationConfig": {
"temperature": 0.3,
"maxOutputTokens": 150
}
}' \
"$GEMINI_API_ENDPOINT")
# ALWAYS print the raw API response for debugging
# echo "Raw API Response:"
# echo "$API_RESPONSE"
# Check for curl errors
if [ $? -ne 0 ]; then
echo "Error: curl command failed. Check your network connection and API endpoint."
exit 1
fi
# Extract the commit message
COMMIT_MESSAGE=$(echo "$API_RESPONSE" | jq -r '.candidates[0].content.parts[0].text')
# Handle API errors
if [ -z "$COMMIT_MESSAGE" ]; then
echo "Error: Failed to generate commit message from API response."
exit 1
fi
# Clean up the commit message
COMMIT_MESSAGE=$(echo "$COMMIT_MESSAGE" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
# Display the generated commit message
echo "Generated commit message:"
echo "$COMMIT_MESSAGE"
echo ""
# Use the generated commit message in the commit, bypassing the hook
git commit --no-verify -m "$COMMIT_MESSAGE"
echo "Commit complete."