2
2
3
3
COMMIT_MSG_FILE=" $1 "
4
4
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 .
6
6
if grep -qE ' ^[^[:space:]#]' " $COMMIT_MSG_FILE " ; then
7
7
exit 0
8
8
fi
9
9
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.
11
17
INLINE_MSG=$( cat << 'EOF '
12
18
# 🎉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
14
20
#
15
21
# Seven Rules for a Great Git Commit Message:
16
22
# 1. Separate subject from body with a blank line
@@ -23,14 +29,24 @@ INLINE_MSG=$(cat <<'EOF'
23
29
#
24
30
# You may modify this commit message.
25
31
# To abort this commit, exit the editor without saving.
32
+ #
33
+ # 🔥Changed files:
26
34
EOF
27
35
)
28
36
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 "
32
48
33
- # Prompt the user to optionally abort the commit.
49
+ # Prompt the user about aborting the commit.
34
50
read -rp " Do you want to abort this commit? (y/N): " answer
35
51
if [[ " $answer " =~ ^[Yy]$ ]]; then
36
52
echo " Commit aborted by user." >&2
0 commit comments