-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathgitin
More file actions
157 lines (125 loc) · 4.57 KB
/
gitin
File metadata and controls
157 lines (125 loc) · 4.57 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env bash
# This script automates git add, commit, and push by generating commit messages
# using the Gemini API from Google Cloud's Generative AI models.
# you will need to create ~/.env file with GEMINI_API_KEY variable set.
# Configuration
# Stop the script if any command fails
set -e
# Check for Dependencies
if ! command -v jq &> /dev/null; then
echo "Error: 'jq' is not installed." >&2
echo "Please install it to parse the API response (e.g., 'brew install jq' or 'apt install jq')." >&2
exit 1
fi
if ! command -v curl &> /dev/null; then
echo "Error: 'curl' is not installed." >&2
echo "Please install it to make the API call (e.g., 'apt install curl')." >&2
exit 1
fi
if ! command -v git &> /dev/null; then
echo "Error: 'git' is not installed." >&2
echo "Please install it to manage git repositories (e.g., 'apt install git')." >&2
exit 1
fi
# Load API Key
ENV_FILE="$HOME/.env"
if [ ! -f "$ENV_FILE" ]; then
echo "Error: API key file not found at $ENV_FILE" >&2
exit 1
fi
# Load the .env file
export $(grep -v '^#' "$ENV_FILE" | xargs)
if [ -z "$GEMINI_API_KEY" ]; then
echo "Error: GEMINI_API_KEY is not set in $ENV_FILE" >&2
exit 1
fi
# Check if there are any changes at all
if [ -z "$(git status --porcelain)" ]; then
echo "No changes to commit. Exiting."
exit 0
fi
# Find Latest "Pro" Model
echo "Fetching latest model to use..."
LIST_MODELS_URL="https://generativelanguage.googleapis.com/v1beta/models?key=${GEMINI_API_KEY}"
# Make the API call to list models
MODEL_RESPONSE=$(curl -s -X GET "$LIST_MODELS_URL")
# Parse the response to find the latest "pro" model that supports generateContent
MODEL_NAME=$(echo "$MODEL_RESPONSE" | jq -r '
.models |
map(
select(
(.name | (contains("pro") and (contains("flash") | not))) and
(.supportedGenerationMethods | index("generateContent"))
)
) |
map(.name) |
sort |
reverse |
.[0]
')
# Check if we successfully found a model
if [ -z "$MODEL_NAME" ] || [ "$MODEL_NAME" == "null" ]; then
echo "Error: Could not find a suitable 'pro' model." >&2
echo "API Response: $MODEL_RESPONSE" >&2
exit 1
fi
# Run Git add
echo "Changes detected. Running 'git add .'..."
git add .
# Get the diff of *staged* files
DIFF_OUTPUT=$(git diff --staged)
# Check if `git add` actually staged anything (it might not, e.g., if all changes are .gitignored)
if [ -z "$DIFF_OUTPUT" ]; then
echo "No staged changes to commit. Exiting."
exit 0
fi
echo "Staged changes detected. Generating commit message..."
# Prepare Gemini API Payload
# Define the prompt as a shell variable
PROMPT_TEXT="As a senior code developer, analyze the following git diff. Create a single, concise commit message in the imperative mood (e.g., 'Fix bug' not 'Fixed bug'). The message must be under 72 characters. Focus on the *what* and *why* of the change. Do not include file names or any prefix like 'Commit:'.
Diff:
---
"
# Use jq --arg to safely pass the prompt and diff variables
JSON_PAYLOAD=$(jq -n \
--arg prompt "$PROMPT_TEXT" \
--arg diff "$DIFF_OUTPUT" \
'{
"contents": [
{
"parts": [
{"text": $prompt},
{"text": $diff}
]
}
],
"generationConfig": {
"temperature": 0.3
}
}')
# Call Gemini API
# Construct the API URL using the dynamically found MODEL_NAME
API_URL="https://generativelanguage.googleapis.com/v1beta/${MODEL_NAME}:generateContent?key=${GEMINI_API_KEY}"
API_RESPONSE=$(curl -s -X POST \
-H "Content-Type: application/json" \
-d "$JSON_PAYLOAD" \
"$API_URL")
# Parse Response and Commit
# Parse the text from the response, remove surrounding quotes, and trim whitespace
COMMIT_MSG=$(echo "$API_RESPONSE" | jq -r '.candidates[0].content.parts[0].text' | sed -e 's/^[ \t"]*//' -e 's/[ \t"]*$//')
# Handle API errors (e.g., if 'candidates' is null or text is empty)
if [ -z "$COMMIT_MSG" ] || [ "$COMMIT_MSG" == "null" ]; then
echo "Error: Failed to generate commit message from Gemini." >&2
# ShellCheck (SC2086) Fix: Quote the variable
echo "API Response: $API_RESPONSE" >&2
exit 1
fi
echo "---"
echo "Generated Commit: $COMMIT_MSG"
echo "---"
# --- 8. Git Commit and Push ---
echo "Committing..."
git commit -m "$COMMIT_MSG"
echo "Pushing to remote..."
git push
echo "✅ Changes committed and pushed successfully."