-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-commit-msg-hook
executable file
·66 lines (53 loc) · 2.26 KB
/
prepare-commit-msg-hook
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
#!/bin/bash
# Get the name of the file containing the commit message
COMMIT_MSG_FILE="$1"
# Read the commit message content only once
COMMIT_MESSAGE_CONTENT=$(cat "$COMMIT_MSG_FILE")
# Check if the commit message is empty (initial commit case) and skip the hook
if [ -z "$COMMIT_MESSAGE_CONTENT" ]; then
exit 0
fi
# Check if we are in a rebase operation, if so skip processing and exit
if git rev-parse --verify HEAD &>/dev/null; then
REBASE_HEAD=$(git rev-parse --git-dir)/rebase-apply/head
if [ -f "$REBASE_HEAD" ]; then
# echo "Rebase in progress, skipping commit message generation."
exit 0
fi
REBASE_MERGE=$(git rev-parse --git-dir)/rebase-merge/head
if [ -f "$REBASE_MERGE" ]; then
# echo "Rebase in progress, skipping commit message generation."
exit 0
fi
fi
# Check if there is a non-comment line in the commit message, if so, don't overwrite.
# Simplified grep to check for non-comment, non-empty lines
if sed '/^# ------------------------ >8 ------------------------$/,$d' <<<"$COMMIT_MESSAGE_CONTENT" |\
grep -vE '^\s*(#|$)' |\
grep . > /dev/null ; then
# echo "Existing commit message found, skipping message update."
exit 0
fi
# Craft the prompt for llm
PROMPT="You are a commit message generator.
Please create a concise and well-formatted commit message, following conventional commits format (e.g., feat: Added a new feature, fix: Resolved a bug, etc.).
The message should be no longer than 50 characters for the first line.
Follow up lines should be wrapped at 72 characters and provide commentary to help reviewer/reader better understand the change.
Here's the existing verbose commit message:
Verbose commit message:
\`\`\`
$COMMIT_MESSAGE_CONTENT
\`\`\`
Commit message:"
# Use llm to generate the commit message.
GENERATED_MESSAGE=$(llm -m gemini-2.0-flash-exp "$PROMPT")
# Check if the generated message is empty or no generation happens and skip replacing
if [ -z "$GENERATED_MESSAGE" ]; then
echo "No commit message generated from llm, skipping message update"
echo "$COMMIT_MESSAGE_CONTENT" >> "$COMMIT_MSG_FILE"
exit 0
fi
# Replace the content of the commit message file with the new message
echo "$GENERATED_MESSAGE" > "$COMMIT_MSG_FILE"
echo "$COMMIT_MESSAGE_CONTENT" >> "$COMMIT_MSG_FILE"
exit 0