-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-commit-msg
56 lines (47 loc) · 1.75 KB
/
prepare-commit-msg
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
#!/bin/bash
# Only generate commit messages for new commits (skip rebase/amend cases)
if [ "$2" == "message" ] || [ "$2" == "template" ] || [ -n "$3" ]; then
exit 0
fi
# Get the list of staged files
STAGED_FILES=$(git diff --cached --name-only)
# Ensure there are changes to commit
if [ -z "$STAGED_FILES" ]; then
echo "No changes to commit." >&2
exit 1
fi
# Get the diff for all changes
FILE_DIFF=$(git diff --cached)
# Build a prompt
PROMPT="Below is a git diff of the curently staged changes.
################################################################################
$FILE_DIFF
################################################################################
Based on this information, write a short, clear commit message that accurately captures the changes.
Use the imperative present tense and bulleted style.
Do not restate the diff.
Do not wrap your final response in any styling or blocks.
Respond directly with the commit message.
"
# Use jq to safely construct the JSON request body
REQUEST_BODY=$(jq -n \
--arg role "user" \
--arg content "$PROMPT" \
'{
messages: [{role: $role, content: $content}],
temperature: 0.7,
max_tokens: 4096
}')
# Send the request to the mlx-lm server
COMMIT_MESSAGE=$(curl -s localhost:8080/v1/chat/completions \
-H "Content-Type: application/json" \
-d "$REQUEST_BODY" | jq -r '.choices[0].message.content')
# DeepSeek-R1 thinks, lets remove the tag and content of the think.
COMMIT_MESSAGE=$(echo "$COMMIT_MESSAGE" | awk 'BEGIN {skip=0} /<think>/ {skip=1} /<\/think>/ {skip=0; next} !skip {print}')
# Write the final commit message to the commit file
if [ -n "$COMMIT_MESSAGE" ]; then
echo -e "$COMMIT_MESSAGE" > "$1"
else
echo "Failed to generate commit messages." >&2
exit 1
fi