-
Notifications
You must be signed in to change notification settings - Fork 1
/
gitgpt.zsh
49 lines (43 loc) · 1.4 KB
/
gitgpt.zsh
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
gitgpt() {
if ! command -v jq >/dev/null; then
echo "Error: jq is not installed. Please install jq and try again."
return 1
fi
if [ -z "$OPENAI_API_KEY" ]; then
echo "Error: OPENAI_API_KEY is not set. Please set your API key and try again."
return 1
fi
QUERY="$@"
PRMPT="Tell me exactly what GIT command I can copy and paste to run in my terminal that will achieve ${QUERY}. Answer a valid GIT command and nothing else."
RESPONSE=$(curl -s -X POST "https://api.openai.com/v1/chat/completions" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-d "{
\"model\": \"gpt-3.5-turbo\",
\"messages\": [{\"role\": \"user\", \"content\": \"$PRMPT\"}],
\"temperature\": 0.5,
\"max_tokens\": 100
}")
ERROR_MESSAGE=$(echo "$RESPONSE" | jq -r '.error.message // ""')
COMMAND=$(echo "$RESPONSE" | jq -r '.choices[0].message.content // ""')
if [ -n "$ERROR_MESSAGE" ]; then
echo "Error: $ERROR_MESSAGE"
elif [ -n "$COMMAND" ]; then
if [[ "$COMMAND" =~ ^git ]]; then
echo "-> $COMMAND"
read -q "REPLY?Do you want to run this command? (y/n)"
if [[ "$REPLY" =~ ^[Yy]$ ]]; then
echo
eval "$COMMAND"
else
echo
echo "Command not executed."
fi
else
echo "Output does not start with 'git' and won't be executed."
echo "Generated output: $COMMAND"
fi
else
echo "Error: Couldn't generate a command."
fi
}