Skip to content

Commit dac4fdf

Browse files
authored
Merge pull request #200 from sysprog21/show-changed-files
Show changed files in prepare-commit-msg hook
2 parents de3b2ca + 0becfc8 commit dac4fdf

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

scripts/prepare-commit-msg.hook

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
COMMIT_MSG_FILE="$1"
44

5-
# Only proceed if the commit message file is empty (ignoring comment or blank lines).
5+
# If the commit message file already contains non-comment lines, do nothing.
66
if grep -qE '^[^[:space:]#]' "$COMMIT_MSG_FILE"; then
77
exit 0
88
fi
99

10-
# Define the inline message with commit guidelines.
10+
# Gather a list of staged (changed) files.
11+
CHANGED_FILES=$(git diff --cached --name-only)
12+
13+
# Prepare a commented list of changed files.
14+
CHANGED_FILES_COMMENTED=$(echo "$CHANGED_FILES" | sed 's/^/# - /')
15+
16+
# Define the inline message with commit guidelines and changed files.
1117
INLINE_MSG=$(cat <<'EOF'
1218
# 🎉Check the rules before writing commit messages.
13-
# https://cbea.ms/git-commit/
19+
# https://github.com/sysprog21/lab0-c/blob/master/CONTRIBUTING.md#git-commit-style
1420
#
1521
# Seven Rules for a Great Git Commit Message:
1622
# 1. Separate subject from body with a blank line
@@ -23,14 +29,24 @@ INLINE_MSG=$(cat <<'EOF'
2329
#
2430
# You may modify this commit message.
2531
# To abort this commit, exit the editor without saving.
32+
#
33+
# 🔥Changed files:
2634
EOF
2735
)
2836

29-
# Write the inline guidelines into the commit message file.
30-
echo > "$COMMIT_MSG_FILE"
31-
echo -e "$INLINE_MSG" >> "$COMMIT_MSG_FILE"
37+
# Write an empty line, the guidelines, and the changed files into the commit message.
38+
{
39+
echo
40+
echo "$INLINE_MSG"
41+
# Append the staged files (as comments).
42+
if [ -n "$CHANGED_FILES" ]; then
43+
echo "$CHANGED_FILES_COMMENTED"
44+
else
45+
echo "# (No staged files detected.)"
46+
fi
47+
} > "$COMMIT_MSG_FILE"
3248

33-
# Prompt the user to optionally abort the commit.
49+
# Prompt the user about aborting the commit.
3450
read -rp "Do you want to abort this commit? (y/N): " answer
3551
if [[ "$answer" =~ ^[Yy]$ ]]; then
3652
echo "Commit aborted by user." >&2

0 commit comments

Comments
 (0)