Skip to content

Commit 5365842

Browse files
committed
style(hooks): standardize tool name casing in messages
- add COMMIT_MSG_TYPE check to skip non-interactive commits - update comments for improved clarity Signed-off-by: mingcheng <mingcheng@apache.org>
1 parent 66db14e commit 5365842

1 file changed

Lines changed: 34 additions & 10 deletions

File tree

hooks/prepare-commit-msg

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@ if ! type aigitcommit >/dev/null 2>&1; then
2121
exit 0
2222
fi
2323

24-
# Only run for interactive commits without a pre-populated message
24+
# Only run for interactive commits or explicit empty messages
25+
RUN_EDITOR=0
2526
if [ -n "$COMMIT_MSG_TYPE" ]; then
26-
exit 0
27-
fi
27+
if [ "$COMMIT_MSG_TYPE" != "message" ]; then
28+
exit 0
29+
fi
2830

29-
# Skip if the commit message already contains non-comment content
30-
if grep -Eq '^[[:space:]]*[^#[:space:]]' "$COMMIT_MSG_FILE"; then
31-
exit 0
31+
# For `git commit -m ""` we still want to generate a message and open the editor
32+
if grep -Eq '^[[:space:]]*[^#[:space:]]' "$COMMIT_MSG_FILE"; then
33+
exit 0
34+
fi
35+
RUN_EDITOR=1
36+
else
37+
# Plain `git commit`: always run AIGitCommit regardless of template content
38+
:
3239
fi
3340

3441
# Get only the diff of what has already been staged
@@ -44,13 +51,30 @@ fi
4451
TEMP_FILE=$(mktemp)
4552

4653
# Execute aigitcommit to generate the commit message and append existing content
47-
echo "Generating commit message by using AIGitCommit..."
54+
echo "🚀 Generating commit message by using aigitcommit."
4855
echo "This may take a few seconds..."
49-
aigitcommit $REPO_ROOT --save $TEMP_FILE >/dev/null 2>&1
56+
aigitcommit "$REPO_ROOT" --save "$TEMP_FILE" >/dev/null 2>&1
5057
if [ $? -ne 0 ]; then
5158
echo "Error: aigitcommit failed to generate commit message."
52-
rm -f $TEMP_FILE
59+
rm -f "$TEMP_FILE"
5360
exit 1
5461
fi
5562

56-
cat $COMMIT_MSG_FILE >>$TEMP_FILE && mv -f $TEMP_FILE $COMMIT_MSG_FILE
63+
cat "$COMMIT_MSG_FILE" >>"$TEMP_FILE" && mv -f "$TEMP_FILE" "$COMMIT_MSG_FILE"
64+
65+
# When the commit was started with `git commit -m ""`, invoke the editor manually
66+
if [ "$RUN_EDITOR" -eq 1 ]; then
67+
EDITOR_CMD=$(git var GIT_EDITOR 2>/dev/null)
68+
if [ -z "$EDITOR_CMD" ]; then
69+
if [ -n "$VISUAL" ]; then
70+
EDITOR_CMD="$VISUAL"
71+
elif [ -n "$EDITOR" ]; then
72+
EDITOR_CMD="$EDITOR"
73+
else
74+
EDITOR_CMD="vi"
75+
fi
76+
fi
77+
78+
# shellcheck disable=SC2086 -- intentional word splitting to respect editor arguments
79+
$EDITOR_CMD "$COMMIT_MSG_FILE"
80+
fi

0 commit comments

Comments
 (0)