Skip to content

Commit b6f8a8d

Browse files
committed
git-commit-push-scriptsh updated
1 parent fe33d0e commit b6f8a8d

1 file changed

Lines changed: 73 additions & 40 deletions

File tree

git-commit-push-script.sh

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -173,55 +173,88 @@ if [ -n "$SQUISH_BIN" ]; then
173173
print_info "squish model: ${GRAY}auto-detect${NC}"
174174
fi
175175
print_info "squish port: ${CYAN}${SQUISH_PORT:-8000}${NC}"
176-
print_info "squish flags: ${GRAY}${SQUISH_FLAGS:-<none>}${NC}"
177176

178177
# Check if a server is already listening on the port
179178
_port="${SQUISH_PORT:-8000}"
180179
if nc -z 127.0.0.1 "$_port" 2>/dev/null; then
181180
print_info "squish server: ${GREEN}already running on :$_port${NC}"
182181
else
183-
print_info "squish server: ${YELLOW}not running — squish will auto-start (first call ~20–90s)${NC}"
184-
fi
185-
echo ""
186-
187-
# Prompt — send full diff, truncated only at MAX_DIFF_CHARS for token sanity
188-
PROMPT="Git commit message (max 50 chars, no quotes/formatting):
189-
$diff"
190-
191-
# Run squish with timeout and spinner
192-
print_step "Asking AI for commit message (Squish local LLM)..."
193-
# shellcheck disable=SC2086 # SQUISH_FLAGS intentionally word-splits for multi-flag support
194-
echo "$PROMPT" | timeout $TIMEOUT_SECONDS $SQUISH_BIN run $SQUISH_FLAGS --max-tokens 60 --temperature 0.2 2>/tmp/squish_stderr.txt | head -1 > /tmp/commit_msg.txt &
195-
LLM_PID=$!
196-
spinner $LLM_PID
197-
wait $LLM_PID
198-
exit_code=$?
199-
200-
# ── Debug: result diagnostics ─────────────────────────────────────────
201-
commit_message=$(cat /tmp/commit_msg.txt 2>/dev/null)
202-
squish_stderr=$(cat /tmp/squish_stderr.txt 2>/dev/null)
203-
rm -f /tmp/commit_msg.txt /tmp/squish_stderr.txt
204-
205-
print_info "squish exit code: ${CYAN}$exit_code${NC}"
206-
if [ -n "$commit_message" ]; then
207-
print_info "squish raw response: ${GREEN}\"$commit_message\"${NC}"
208-
else
209-
print_info "squish raw response: ${RED}<empty>${NC}"
210-
fi
211-
if [ -n "$squish_stderr" ]; then
212-
print_info "squish stderr: ${YELLOW}$(echo "$squish_stderr" | head -3)${NC}"
182+
print_info "squish server: ${YELLOW}not running — starting it now…${NC}"
183+
# Start the server in the background and wait for it
184+
$SQUISH_BIN serve ${SQUISH_MODEL:+--model $SQUISH_MODEL} --port "$_port" > /tmp/squish_serve.log 2>&1 &
185+
_serve_pid=$!
186+
_waited=0
187+
while [ $_waited -lt 90 ] && ! nc -z 127.0.0.1 "$_port" 2>/dev/null; do
188+
sleep 1
189+
_waited=$((_waited + 1))
190+
done
191+
if ! nc -z 127.0.0.1 "$_port" 2>/dev/null; then
192+
print_warning "Server failed to start. Using fallback message."
193+
commit_message="$fallback_message"
194+
else
195+
print_success "Server ready (${_waited}s)"
196+
fi
213197
fi
214198
echo ""
215199

216-
# Check if timeout occurred or empty response
217-
if [ $exit_code -eq 124 ]; then
218-
print_warning "squish timed out after ${TIMEOUT_SECONDS}s. Using fallback message."
219-
commit_message="$fallback_message"
220-
elif [ -z "$commit_message" ]; then
221-
print_warning "squish returned empty response. Using fallback message."
222-
commit_message="$fallback_message"
223-
else
224-
print_success "squish responded successfully"
200+
if [ -z "$commit_message" ]; then
201+
# Build a focused prompt — only the stat summary + truncated diff,
202+
# no surrounding debug text that could confuse the model.
203+
stat_summary=$(git diff --cached --stat | tail -1)
204+
changed_names=$(git diff --cached --name-only | head -10 | tr '\n' ' ')
205+
206+
PAYLOAD=$(printf '{"model":"squish","messages":[{"role":"system","content":"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."},{"role":"user","content":"Files changed: %s\nSummary: %s\n\nDiff:\n%s\n\nCommit message:"}],"max_tokens":60,"temperature":0.2,"stream":false}' \
207+
"$changed_names" "$stat_summary" "$diff")
208+
209+
# Run squish with timeout and spinner
210+
print_step "Asking AI for commit message (Squish local LLM)..."
211+
_port="${SQUISH_PORT:-8000}"
212+
curl -s --max-time $TIMEOUT_SECONDS \
213+
-X POST "http://127.0.0.1:${_port}/v1/chat/completions" \
214+
-H "Content-Type: application/json" \
215+
-d "$PAYLOAD" 2>/tmp/squish_stderr.txt \
216+
> /tmp/squish_response.txt &
217+
LLM_PID=$!
218+
spinner $LLM_PID
219+
wait $LLM_PID
220+
exit_code=$?
221+
222+
# ── Debug: result diagnostics ─────────────────────────────────────────
223+
raw_response=$(cat /tmp/squish_response.txt 2>/dev/null)
224+
squish_stderr=$(cat /tmp/squish_stderr.txt 2>/dev/null)
225+
rm -f /tmp/squish_response.txt /tmp/squish_stderr.txt
226+
227+
# Extract the message content from the JSON response
228+
commit_message=$(echo "$raw_response" | python3 -c "
229+
import sys, json
230+
try:
231+
data = json.load(sys.stdin)
232+
print(data['choices'][0]['message']['content'].strip())
233+
except Exception:
234+
pass
235+
" 2>/dev/null)
236+
237+
print_info "squish exit code: ${CYAN}$exit_code${NC}"
238+
if [ -n "$commit_message" ]; then
239+
print_info "squish raw response: ${GREEN}\"$commit_message\"${NC}"
240+
else
241+
print_info "squish raw response: ${RED}<empty>${NC}"
242+
fi
243+
if [ -n "$squish_stderr" ]; then
244+
print_info "squish stderr: ${YELLOW}$(echo "$squish_stderr" | head -3)${NC}"
245+
fi
246+
echo ""
247+
248+
# Check if timeout occurred or empty response
249+
if [ $exit_code -eq 124 ]; then
250+
print_warning "squish timed out after ${TIMEOUT_SECONDS}s. Using fallback message."
251+
commit_message="$fallback_message"
252+
elif [ -z "$commit_message" ]; then
253+
print_warning "squish returned empty response. Using fallback message."
254+
commit_message="$fallback_message"
255+
else
256+
print_success "squish responded successfully"
257+
fi
225258
fi
226259
fi
227260

0 commit comments

Comments
 (0)