-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaicommit
127 lines (111 loc) · 3.27 KB
/
aicommit
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
#!/bin/bash
# -------------------------
# Checks & Configuration
# -------------------------
if [ -z "$GEMINI_API_KEY" ]; then
echo "ERROR: Please set your GEMINI_API_KEY environment variable before running this script."
exit 1
fi
if ! command -v jq >/dev/null 2>&1 ; then
echo "ERROR: 'jq' utility not found. Please install 'jq' to parse and build JSON."
exit 1
fi
# -------------------------
# Function: Generate commit message
# -------------------------
generate_commit_message() {
# Build the JSON payload safely with jq, escaping special characters
REQUEST_BODY=$(
jq -n \
--arg diff "$DIFF_CONTENT" \
'{
contents: [
{
parts: [
{
text: (
"Analyze the following Git diff and produce a concise, well-written commit message describing the changes:\n\n" + $diff
)
}
]
}
]
}'
)
# Call Gemini API
local response
response=$(curl -s -X POST "$API_URL" \
-H "Content-Type: application/json" \
-d "$REQUEST_BODY")
# Extract commit message
local message
message=$(echo "$response" | jq -r '.candidates[0].content.parts[0].text // empty')
# Remove triple backticks and surrounding whitespace
message=$(echo "$message" | sed -e 's/```//g' -e 's/^[[:space:]]*//;s/[[:space:]]*$//')
if [ -z "$message" ]; then
echo "Failed to generate a commit message. Response from API was:"
echo "$response"
exit 1
fi
echo "$message"
}
# -------------------------
# Git operations
# -------------------------
git add .
DIFF_CONTENT=$(git diff --cached)
if [ -z "$DIFF_CONTENT" ]; then
echo "No changes detected in the staging area. Exiting..."
exit 0
fi
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
# -------------------------
# Gemini API Endpoint
# -------------------------
API_URL="https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-thinking-exp:generateContent?key=$GEMINI_API_KEY"
# -------------------------
# Generate initial message
# -------------------------
COMMIT_MESSAGE=$(generate_commit_message)
# -------------------------
# Prompt user for action in a loop
# -------------------------
while true; do
echo "Generated Commit Message:"
echo "----------------------------------------"
echo "$COMMIT_MESSAGE"
echo "----------------------------------------"
echo "What would you like to do?"
echo " [E] Edit in Vim"
echo " [R] Regenerate Commit Message"
echo " [A] Accept commit message (or press Enter)"
read -r -p "Choice (E/R/A): " choice
case "$choice" in
[Ee])
# Create a temp file and open in Vim for editing
TEMP_FILE="$(mktemp)"
echo "$COMMIT_MESSAGE" > "$TEMP_FILE"
vim "$TEMP_FILE"
COMMIT_MESSAGE="$(cat "$TEMP_FILE")"
rm "$TEMP_FILE"
;;
[Rr])
# Regenerate the commit message
echo "Regenerating commit message..."
COMMIT_MESSAGE=$(generate_commit_message)
;;
""|[Aa])
# Accept and break
break
;;
*)
# Invalid input, assume user wants to accept
break
;;
esac
done
# -------------------------
# Create and push commit
# -------------------------
git commit -S -m "$COMMIT_MESSAGE"
git push -u origin "$CURRENT_BRANCH"