-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-commit-push-script.sh
More file actions
executable file
Β·404 lines (357 loc) Β· 17.6 KB
/
git-commit-push-script.sh
File metadata and controls
executable file
Β·404 lines (357 loc) Β· 17.6 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/bin/bash
source ~/.bash_profile
# Configuration
MAX_DIFF_CHARS=2000 # Truncate diff to prevent long processing
TIMEOUT_SECONDS=120 # Max time to wait for LLM response (14B model can take 60-90s on first token)
MAX_COMMIT_LENGTH=50 # Max characters for commit message
# Squish model selection β set SQUISH_MODEL to target a specific compressed model.
# Accepts a name hint (e.g. "14b", "7b") or a full path to a model directory.
# Leave empty to let squish auto-detect (uses the first available model in ~/models).
# Examples:
# SQUISH_MODEL="14b" # matches Qwen2.5-14B-*
# SQUISH_MODEL="$HOME/models/Qwen2.5-14B-Instruct-bf16" # explicit path
SQUISH_MODEL="${SQUISH_MODEL:-}"
# Squish server port β override if you run multiple squish servers concurrently.
SQUISH_PORT="${SQUISH_PORT:-8000}"
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
GRAY='\033[0;90m'
BOLD='\033[1m'
DIM='\033[2m'
NC='\033[0m' # No Color
# Animated spinner with colors
spinner() {
local pid=$1
local delay=0.08
local frames=('β£Ύ' 'β£½' 'β£»' 'β’Ώ' 'β‘Ώ' 'β£' 'β£―' 'β£·')
local colors=("$CYAN" "$BLUE" "$PURPLE" "$CYAN" "$BLUE" "$PURPLE" "$CYAN" "$BLUE")
local i=0
local elapsed=0
while ps -p $pid > /dev/null 2>&1; do
printf "\r${colors[$i]}${frames[$i]}${NC} ${WHITE}Generating commit message${NC}${GRAY}...${NC} ${DIM}(${elapsed}s)${NC} "
sleep $delay
i=$(( (i + 1) % ${#frames[@]} ))
elapsed=$(echo "scale=1; $elapsed + $delay" | bc)
if ! ps -p $pid > /dev/null 2>&1; then
break
fi
done
printf "\r${GREEN}β${NC} ${WHITE}Done!${NC} \n"
}
# Status messages
print_header() {
clear
echo ""
echo -e "${PURPLE}βββββ${CYAN}β ${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}β${PURPLE}ββββββββ${CYAN}β ${PURPLE}ββββββ${CYAN}β ${GREEN}ββββββ${YELLOW}β ${GREEN}ββ${YELLOW}β ${GREEN}ββ${YELLOW}β${GREEN}βββββββ${YELLOW}β${GREEN}ββ${YELLOW}β ${GREEN}ββ${YELLOW}β${NC}"
echo -e "${PURPLE}ββ${CYAN}βββ${PURPLE}ββ${CYAN}β${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}ββββ${PURPLE}ββ${CYAN}ββββ${PURPLE}ββ${CYAN}ββββ${PURPLE}ββ${CYAN}β ${GREEN}ββ${YELLOW}βββ${GREEN}ββ${YELLOW}β${GREEN}ββ${YELLOW}β ${GREEN}ββ${YELLOW}β${GREEN}ββ${YELLOW}ββββββ${GREEN}ββ${YELLOW}β ${GREEN}ββ${YELLOW}β${NC}"
echo -e "${PURPLE}βββββββ${CYAN}β${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}β ${GREEN}ββββββ${YELLOW}ββ${GREEN}ββ${YELLOW}β ${GREEN}ββ${YELLOW}β${GREEN}βββββββ${YELLOW}β${GREEN}βββββββ${YELLOW}β${NC}"
echo -e "${PURPLE}ββ${CYAN}βββ${PURPLE}ββ${CYAN}β${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}β ${GREEN}ββ${YELLOW}βββββ ${GREEN}ββ${YELLOW}β ${GREEN}ββ${YELLOW}ββββββ${GREEN}ββ${YELLOW}β${GREEN}ββ${YELLOW}βββ${GREEN}ββ${YELLOW}β${NC}"
echo -e "${PURPLE}ββ${CYAN}β ${PURPLE}ββ${CYAN}ββ${PURPLE}ββββββ${CYAN}ββ ${PURPLE}ββ${CYAN}β β${PURPLE}ββββββ${CYAN}ββ ${GREEN}ββ${YELLOW}β β${GREEN}ββββββ${YELLOW}ββ${GREEN}βββββββ${YELLOW}β${GREEN}ββ${YELLOW}β ${GREEN}ββ${YELLOW}β${NC}"
echo -e "${CYAN}βββ βββ βββββββ βββ βββββββ ${YELLOW}βββ βββββββ βββββββββββ βββ${NC}"
echo ""
echo -e "${DIM}${WHITE} AI-powered git commit messages${NC}"
echo -e "${GRAY}βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo ""
}
print_step() {
echo -e "${CYAN}βΈ${NC} $1"
}
print_success() {
echo -e "${GREEN}β${NC} $1"
}
print_warning() {
echo -e "${YELLOW}β ${NC} $1"
}
print_error() {
echo -e "${RED}β${NC} $1"
}
print_info() {
echo -e "${GRAY} $1${NC}"
}
# Print header
print_header
# Stage all changes
print_step "Staging changes..."
git add -A
print_success "Changes staged"
# Get the branch name
base_branch=$(git rev-parse --abbrev-ref HEAD)
print_info "Branch: ${CYAN}$base_branch${NC}"
# Get default branch or main branch
default_branch=$(git rev-parse --abbrev-ref origin/HEAD | sed 's@^origin/@@')
print_info "Default: ${GRAY}$default_branch${NC}"
# Extract Jira ticket number from current directory
ticket=$(echo $base_branch | grep -o -E '([A-Za-z]+-[0-9]{3,}|[A-Za-z]+-[0-9]{3,})')
if [ -n "$ticket" ]; then
print_info "Ticket: ${YELLOW}$ticket${NC}"
fi
echo ""
# Get changed files for fallback message (staged changes vs last commit)
changed_files=$(git diff --cached --name-only | head -3)
first_file=$(echo "$changed_files" | head -1)
file_count=$(git diff --cached --name-only | wc -l | tr -d ' ')
# Show changed files
print_step "Files changed: ${WHITE}$file_count${NC}"
echo "$changed_files" | while read file; do
if [ -n "$file" ]; then
print_info " ${BLUE}$file${NC}"
fi
done
if [ "$file_count" -gt 3 ]; then
print_info " ${GRAY}...and $((file_count - 3)) more${NC}"
fi
echo ""
# Generate fallback message based on changes
if [ "$file_count" -eq 1 ]; then
fallback_message="${first_file} updated"
elif [ "$file_count" -gt 1 ]; then
fallback_message="${first_file} and $((file_count - 1)) other file(s) updated"
else
fallback_message="Updated ${base_branch} branch"
fi
# Get the git diff (staged changes vs last commit) - truncate for performance
diff=$(git diff --cached | head -c $MAX_DIFF_CHARS)
# Squish local LLM β no API key, no rate limits, no cloud
# Server auto-starts on first use (~20s), then stays alive for near-instant responses
# Build model / port flags from config vars (empty = squish auto-detects)
SQUISH_FLAGS=""
if [ -n "$SQUISH_MODEL" ]; then
SQUISH_FLAGS="--model $SQUISH_MODEL"
fi
if [ -n "$SQUISH_PORT" ]; then
SQUISH_FLAGS="$SQUISH_FLAGS --port $SQUISH_PORT"
fi
# ββ Debug: squish availability βββββββββββββββββββββββββββββββββββββββββββ
# squish may be a zsh alias (not visible to bash scripts) β fall back to
# calling cli.py directly with python3 if the command isn't on PATH.
SQUISH_BIN=$(command -v squish 2>/dev/null)
if [ -z "$SQUISH_BIN" ] && [ -f "/Users/wscholl/squish/cli.py" ]; then
SQUISH_BIN="python3 /Users/wscholl/squish/cli.py"
print_info "squish binary: ${CYAN}$SQUISH_BIN${GRAY} (alias not in bash PATH, using direct path)${NC}"
elif [ -n "$SQUISH_BIN" ]; then
print_info "squish binary: ${CYAN}$SQUISH_BIN${NC}"
else
print_warning "squish not found in PATH β will use fallback message"
commit_message="$fallback_message"
fi
if [ -n "$SQUISH_BIN" ]; then
# Show which model / port will be used
if [ -n "$SQUISH_MODEL" ]; then
print_info "squish model: ${CYAN}$SQUISH_MODEL${NC}"
else
print_info "squish model: ${GRAY}auto-detect${NC}"
fi
print_info "squish port: ${CYAN}${SQUISH_PORT:-8000}${NC}"
# Check if a server is already listening on the port
_port="${SQUISH_PORT:-8000}"
if nc -z 127.0.0.1 "$_port" 2>/dev/null; then
print_info "squish server: ${GREEN}already running on :$_port${NC}"
else
print_info "squish server: ${YELLOW}not running β starting it nowβ¦${NC}"
# Start the server in the background and wait for it
$SQUISH_BIN serve ${SQUISH_MODEL:+--model $SQUISH_MODEL} --port "$_port" > /tmp/squish_serve.log 2>&1 &
_serve_pid=$!
_waited=0
while [ $_waited -lt 90 ] && ! nc -z 127.0.0.1 "$_port" 2>/dev/null; do
sleep 1
_waited=$((_waited + 1))
done
if ! nc -z 127.0.0.1 "$_port" 2>/dev/null; then
print_warning "Server failed to start. Using fallback message."
commit_message="$fallback_message"
else
print_success "Server ready (${_waited}s)"
fi
fi
echo ""
if [ -z "$commit_message" ]; then
# Build a focused prompt β only the stat summary + truncated diff,
# no surrounding debug text that could confuse the model.
stat_summary=$(git diff --cached --stat | tail -1)
changed_names=$(git diff --cached --name-only | head -10 | tr '\n' ' ')
# Write diff to a temp file so Python can read it without any
# shell-interpolation escaping issues (newlines, backslashes, etc.)
echo "$diff" > /tmp/squish_diff.txt
# Use python3 to build the JSON payload β all values go through
# json.dumps() so control characters are properly escaped.
PAYLOAD=$(SQUISH_CHANGED="$changed_names" SQUISH_STAT="$stat_summary" \
python3 - <<'PYEOF'
import json, os
system = (
"You generate concise git commit messages. "
"Reply with ONLY the commit message, nothing else. "
"Max 50 characters. Imperative mood. No period. No quotes. No markdown."
)
diff = open("/tmp/squish_diff.txt").read()
user = (
f"Files changed: {os.environ['SQUISH_CHANGED']}\n"
f"Summary: {os.environ['SQUISH_STAT']}\n\n"
f"Diff:\n{diff}\n\nCommit message:"
)
print(json.dumps({
"model": "squish",
"messages": [
{"role": "system", "content": system},
{"role": "user", "content": user},
],
"max_tokens": 60,
"temperature": 0.2,
"stream": False,
}))
PYEOF
)
rm -f /tmp/squish_diff.txt
# Run squish with timeout and spinner
print_step "Asking AI for commit message (Squish local LLM)..."
_port="${SQUISH_PORT:-8000}"
curl -s --max-time $TIMEOUT_SECONDS \
-X POST "http://127.0.0.1:${_port}/v1/chat/completions" \
-H "Content-Type: application/json" \
-d "$PAYLOAD" 2>/tmp/squish_stderr.txt \
> /tmp/squish_response.txt &
LLM_PID=$!
spinner $LLM_PID
wait $LLM_PID
exit_code=$?
# ββ Debug: result diagnostics βββββββββββββββββββββββββββββββββββββββββ
raw_response=$(cat /tmp/squish_response.txt 2>/dev/null)
squish_stderr=$(cat /tmp/squish_stderr.txt 2>/dev/null)
rm -f /tmp/squish_response.txt /tmp/squish_stderr.txt
# Extract the message content from the JSON response
commit_message=$(echo "$raw_response" | python3 -c "
import sys, json
try:
data = json.load(sys.stdin)
print(data['choices'][0]['message']['content'].strip())
except Exception:
pass
" 2>/dev/null)
print_info "squish exit code: ${CYAN}$exit_code${NC}"
if [ -n "$commit_message" ]; then
print_info "squish parsed message: ${GREEN}\"$commit_message\"${NC}"
else
print_info "squish parsed message: ${RED}<empty>${NC}"
if [ -n "$raw_response" ]; then
print_info "squish raw body: ${YELLOW}$(echo "$raw_response" | head -c 500)${NC}"
else
print_info "squish raw body: ${RED}<no response from server>${NC}"
fi
fi
if [ -n "$squish_stderr" ]; then
print_info "squish stderr: ${YELLOW}$(echo "$squish_stderr" | head -3)${NC}"
fi
echo ""
# Check if timeout occurred or empty response
if [ $exit_code -eq 28 ] || [ $exit_code -eq 124 ]; then
print_warning "squish timed out after ${TIMEOUT_SECONDS}s (exit $exit_code). Using fallback message."
commit_message="$fallback_message"
elif [ -z "$commit_message" ]; then
print_warning "squish returned empty response. Using fallback message."
commit_message="$fallback_message"
else
print_success "squish responded successfully"
fi
fi
fi
# Clean up commit message formatting
commit_message=$(echo "$commit_message" | sed 's/#//g' | sed 's/```//g' | sed 's/Commit message://gi' | sed 's/\.//g' | sed 's/\"//g' | sed "s/'//g" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
# Truncate to max length at word boundary
if [ ${#commit_message} -gt $MAX_COMMIT_LENGTH ]; then
commit_message=$(echo "$commit_message" | cut -c1-$MAX_COMMIT_LENGTH | sed 's/[[:space:]][^[:space:]]*$//')
fi
# Final fallback check
if [ -z "$commit_message" ] || [ "$commit_message" == "null" ]; then
commit_message="$fallback_message"
fi
echo ""
echo -e "${PURPLE}ββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "${BOLD}${WHITE} π Commit Message${NC}"
echo -e "${PURPLE}ββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
if [ -n "$ticket" ]; then
echo -e " ${YELLOW}$ticket${NC} ${WHITE}$commit_message${NC}"
else
echo -e " ${WHITE}$commit_message${NC}"
fi
echo -e "${PURPLE}ββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo ""
# Set the environment variables
export COMMIT_MESSAGE="$commit_message"
export TICKET="$ticket"
# Prepare and execute commit command, remove -S to commit without signing
print_step "Committing changes..."
if [ -z "$ticket" ]; then
expect <<'EOF' > /dev/null 2>&1
spawn git commit -S -m "$env(COMMIT_MESSAGE)"
expect "Enter passphrase for \"/Users/wscholl/.ssh/id_ed25519\":"
send "$env(GIT_SSH_PASSPHRASE)\r"
expect eof
EOF
else
expect <<'EOF' > /dev/null 2>&1
spawn git commit -S -m "$env(TICKET) $env(COMMIT_MESSAGE)"
expect "Enter passphrase for \"/Users/wscholl/.ssh/id_ed25519\":"
send "$env(GIT_SSH_PASSPHRASE)\r"
expect eof
EOF
fi
print_success "Committed successfully"
# Check if the branch exists on the remote
remote_branch=$(git ls-remote --heads origin $base_branch)
# Function: pull_push_after_failed_push - If push fails, attempt to pull and push again
pull_push_after_failed_push() {
print_warning "Push failed. Attempting to pull and push again..."
git fetch origin $base_branch > /dev/null 2>&1
git pull > /dev/null 2>&1
git push --force > /dev/null 2>&1
}
# Check if the branch exists on the remote
if [ -z "$remote_branch" ]; then
# If the branch does not exist on the remote, create it
print_step "Creating remote branch..."
set -e
git push --set-upstream origin $base_branch > /dev/null 2>&1
# Check if the push was successful
if [ $? -ne 0 ]; then
pull_push_after_failed_push
fi
print_success "Branch created and pushed"
else
# Branch exists on the remote, push changes
print_step "Pushing to remote..."
git fetch origin $base_branch > /dev/null 2>&1
git pull > /dev/null 2>&1
git push > /dev/null 2>&1
# Check if the push wasn't successful
if [ $? -ne 0 ]; then
pull_push_after_failed_push
fi
print_success "Pushed successfully"
fi
# Animated success banner
show_pushed_banner() {
echo ""
echo -e "${GRAY}ββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
sleep 0.1
echo -e "${GREEN}ββββββ${WHITE}β ${GREEN}ββ${WHITE}β ${GREEN}ββ${WHITE}β${GREEN}βββββββ${WHITE}β${GREEN}ββ${WHITE}β ${GREEN}ββ${WHITE}β${GREEN}βββββββ${WHITE}β${GREEN}ββββββ${WHITE}β ${GREEN}ββ${WHITE}β${NC}"
sleep 0.05
echo -e "${GREEN}ββ${WHITE}βββ${GREEN}ββ${WHITE}β${GREEN}ββ${WHITE}β ${GREEN}ββ${WHITE}β${GREEN}ββ${WHITE}ββββββ${GREEN}ββ${WHITE}β ${GREEN}ββ${WHITE}β${GREEN}ββ${WHITE}ββββββ${GREEN}ββ${WHITE}βββ${GREEN}ββ${WHITE}β${GREEN}ββ${WHITE}β${NC}"
sleep 0.05
echo -e "${GREEN}ββββββ${WHITE}ββ${GREEN}ββ${WHITE}β ${GREEN}ββ${WHITE}β${GREEN}βββββββ${WHITE}β${GREEN}βββββββ${WHITE}β${GREEN}βββββ${WHITE}β ${GREEN}ββ${WHITE}β ${GREEN}ββ${WHITE}β${GREEN}ββ${WHITE}β${NC}"
sleep 0.05
echo -e "${GREEN}ββ${WHITE}βββββ ${GREEN}ββ${WHITE}β ${GREEN}ββ${WHITE}ββββββ${GREEN}ββ${WHITE}β${GREEN}ββ${WHITE}βββ${GREEN}ββ${WHITE}β${GREEN}ββ${WHITE}ββββ ${GREEN}ββ${WHITE}β ${GREEN}ββ${WHITE}ββββ${NC}"
sleep 0.05
echo -e "${GREEN}ββ${WHITE}β β${GREEN}ββββββ${WHITE}ββ${GREEN}βββββββ${WHITE}β${GREEN}ββ${WHITE}β ${GREEN}ββ${WHITE}β${GREEN}βββββββ${WHITE}β${GREEN}ββββββ${WHITE}ββ${GREEN}ββ${WHITE}β${NC}"
sleep 0.05
echo -e "${WHITE}βββ βββββββ βββββββββββ ββββββββββββββββββ βββ${NC}"
echo -e "${GRAY}ββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo ""
echo -e " ${DIM}π ${WHITE}Changes are now live on ${CYAN}$base_branch${NC}"
echo ""
}
show_pushed_banner