Skip to content

Commit cdcfc66

Browse files
committed
Introduce prepare commit message hook
The prepare-commit-msg hook runs after the default message is created but before the editor opens. Change-Id: I7ee59310c4d0eb17daaf2b286587b378e2301eb0
1 parent fe7662b commit cdcfc66

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

scripts/install-git-hooks

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ chmod +x .git/hooks/commit-msg
6868
ln -sf ../../scripts/pre-push.hook .git/hooks/pre-push || exit 1
6969
chmod +x .git/hooks/pre-push
7070

71+
ln -sf ../../scripts/prepare-commit-msg.hook .git/hooks/prepare-commit-msg || exit 1
72+
chmod +x .git/hooks/prepare-commit-msg
73+
7174
touch .git/hooks/applied || exit 1
7275

7376
echo

scripts/prepare-commit-msg.hook

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
COMMIT_MSG_FILE="$1"
4+
5+
# Only proceed if the commit message file is empty (ignoring comment or blank lines).
6+
if grep -qE '^[^[:space:]#]' "$COMMIT_MSG_FILE"; then
7+
exit 0
8+
fi
9+
10+
# Define the inline message with commit guidelines.
11+
INLINE_MSG=$(cat <<'EOF'
12+
# 🎉Check the rules before writing commit messages.
13+
# https://cbea.ms/git-commit/
14+
#
15+
# Seven Rules for a Great Git Commit Message:
16+
# 1. Separate subject from body with a blank line
17+
# 2. Limit the subject line to 50 characters
18+
# 3. Capitalize the subject line
19+
# 4. Do not end the subject line with a period
20+
# 5. Use the imperative mood in the subject line
21+
# 6. Wrap the body at 72 characters
22+
# 7. Use the body to explain what and why vs. how
23+
#
24+
# You may modify this commit message.
25+
# To abort this commit, exit the editor without saving.
26+
EOF
27+
)
28+
29+
# Write the inline guidelines into the commit message file.
30+
echo > "$COMMIT_MSG_FILE"
31+
echo -e "$INLINE_MSG" >> "$COMMIT_MSG_FILE"
32+
33+
# Prompt the user to optionally abort the commit.
34+
read -rp "Do you want to abort this commit? (y/N): " answer
35+
if [[ "$answer" =~ ^[Yy]$ ]]; then
36+
echo "Commit aborted by user." >&2
37+
exit 1
38+
fi
39+
40+
exit 0

0 commit comments

Comments
 (0)