-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitcommitgpt
More file actions
executable file
·72 lines (60 loc) · 2.5 KB
/
Copy pathgitcommitgpt
File metadata and controls
executable file
·72 lines (60 loc) · 2.5 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
#!/bin/bash
# Your OpenAI API key
API_KEY=""
# Function to send a request to GPT-4o-mini
send_gpt_request() {
# The request text (git diff)
prompt="$1"
files="$2"
# Use jq to safely build JSON
json_payload=$(jq -n --arg content "$prompt" --arg files "$files" '{
model: "gpt-4o-mini",
messages: [
{role: "user", content: ("Here is a git diff:\n\n" + $content + "\n\nPlease write a concise and meaningful commit message in English, and provide a nice list of modified/added/removed files at the end:\n\n" + $files)}
],
max_tokens: 1000
}')
# Send the HTTP POST request to the GPT-4 API with the correct JSON
response=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d "$json_payload")
# Extract the response from JSON
echo "$response" | jq -r '.choices[0].message.content'
}
# Check if we are in a Git repository
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
echo "You are not inside a Git repository."
exit 1
fi
diff=$(git --no-pager diff)
# Check if there are any changes
if [ -z "$diff" ]; then
echo "No changes to send in 'git diff'."
exit 1
fi
# Get the list of modified, added, and removed files
files=$(git status --porcelain | awk '{print $2}' | tr '\n' ', ' | sed 's/, $//')
# Send diff to GPT-4o-mini and get the commit message
commit_message=$(send_gpt_request "$diff" "$files")
# Display the commit message with formatting
echo "Proposed commit message:"
echo "----------------------------------"
echo "$commit_message"
echo "----------------------------------"
# Try to copy the commit message to the clipboard
if command -v wl-copy > /dev/null 2>&1; then
echo "$commit_message" | wl-copy
echo "Commit message has been copied to the clipboard using wl-copy."
elif command -v xclip > /dev/null 2>&1; then
echo "$commit_message" | xclip -selection clipboard
echo "Commit message has been copied to the clipboard using xclip."
elif command -v xsel > /dev/null 2>&1; then
echo "$commit_message" | xsel --clipboard --input
echo "Commit message has been copied to the clipboard using xsel."
elif command -v pbcopy > /dev/null 2>&1; then
echo "$commit_message" | pbcopy
echo "Commit message has been copied to the clipboard using pbcopy (macOS)."
else
echo "No clipboard tool found (wl-copy, xclip, xsel, or pbcopy). Install one of them to automatically copy the commit message."
fi