-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare-commit-msg-hook
More file actions
executable file
·77 lines (61 loc) · 2.36 KB
/
prepare-commit-msg-hook
File metadata and controls
executable file
·77 lines (61 loc) · 2.36 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
#!/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.
Create a concise and well-formatted commit message.
Follows the existing convention from the historical commit messages.
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.
Do NOT generate the statistic summary.
Do NOT generate the GitHub PR/Issue reference.
Here's historical commit messages:
###
$(git log --stat --author='sluongng' -5)
###
Here's the existing verbose commit message:
###
$COMMIT_MESSAGE_CONTENT
###
New commit message:
###
"
# Use llm to generate the commit message.
GENERATED_MESSAGE=$(llm -m gemini-f "$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