-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-ai-commit
More file actions
executable file
·102 lines (87 loc) · 2.53 KB
/
Copy pathgit-ai-commit
File metadata and controls
executable file
·102 lines (87 loc) · 2.53 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
#!/usr/bin/env zsh
# commit 用のオプションを格納(ただし -m/--message は除外)
typeset -a PASSTHROUGH_OPTS
VERBOSE=false
while [[ $# -gt 0 ]]; do
case "$1" in
-m|--message)
echo "❌ -m / --message オプションは使用できません。AI が生成します。"
exit 1
;;
-v|--verbose)
VERBOSE=true
shift
;;
*)
PASSTHROUGH_OPTS+=("$1")
shift
;;
esac
done
# 差分取得(ステージ済みのみ)
DIFF=$(git diff --cached)
if [[ -z "$DIFF" ]]; then
echo "⚠️ ステージされた変更がありません。"
exit 1
fi
# プロンプト
PROMPT="次の git diff の内容に基づいて、簡潔で意味のあるコミットメッセージを英語で一文で書いてください。
$DIFF"
# APIキー確認
if [[ -z "$OPENAI_API_KEY" ]]; then
echo "❌ OPENAI_API_KEY が設定されていません。"
exit 1
fi
HTTP_RESPONSE=$(curl -s -w "HTTPSTATUS:%{http_code}" https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d @- <<EOF
{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "あなたは優秀なGitコミットメッセージの作成者です。"},
{"role": "user", "content": $(print -r -- "$PROMPT" | jq -Rs .)}
],
"temperature": 0.3
}
EOF
)
HTTP_STATUS=$(echo "$HTTP_RESPONSE" | sed -n 's/.*HTTPSTATUS:\([0-9]*\).*/\1/p')
RESPONSE=$(echo "$HTTP_RESPONSE" | sed 's/HTTPSTATUS:[0-9]*$//')
if [[ "$VERBOSE" == true ]]; then
echo "📡 HTTP Status: $HTTP_STATUS"
echo "📡 API Response:"
echo "$RESPONSE"
echo
fi
# HTTPステータスコードチェック
if [[ "$HTTP_STATUS" != "200" ]]; then
echo "❌ API エラーが発生しました (HTTP $HTTP_STATUS):"
echo "$RESPONSE"
exit 1
fi
# エラーチェック
if echo "$RESPONSE" | jq -e '.error' >/dev/null 2>&1; then
echo "❌ API エラーが発生しました:"
echo "$RESPONSE"
exit 1
fi
# OpenAI API 呼び出し
COMMIT_MSG=$(echo $RESPONSE | jq -r '.choices[0].message.content' 2>/dev/null)
if [[ -z "$COMMIT_MSG" || "$COMMIT_MSG" == "null" ]]; then
echo "❌ レスポンスの解析に失敗しました:"
echo "$RESPONSE"
exit 1
fi
# 結果確認
echo "✅ 生成されたコミットメッセージ:"
echo
echo "$COMMIT_MSG"
echo
read -q "REPLY?このメッセージでコミットしますか? [y/N] "
echo
if [[ "$REPLY" == [yY] ]]; then
git commit -m "$COMMIT_MSG" "${PASSTHROUGH_OPTS[@]}"
else
echo "🚫 コミットを中止しました。"
fi