-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhance-scope
More file actions
executable file
·62 lines (49 loc) · 1.68 KB
/
enhance-scope
File metadata and controls
executable file
·62 lines (49 loc) · 1.68 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
#!/bin/sh
# commit-msg:
# enhance commit messages touching a single file to include the filename in the summary
# shellcheck source=lib/commit-msg.sh disable=SC1091
. "$(dirname "$0")/lib/commit-msg.sh"
COMMIT_MSG_FILE="$1"
# Read and parse commit message
read_commit_msg "$COMMIT_MSG_FILE"
COMMIT_SUMMARY="$COMMIT_FIRST_LINE"
COMMIT_BODY="$COMMIT_REST"
# Get the list of files touched in the commit
FILES_TOUCHED=$(git diff --cached --name-only)
# Exit if no files staged
if [ -z "$FILES_TOUCHED" ]; then
exit 0
fi
# Count files (handles filenames with spaces correctly)
FILE_COUNT=$(printf '%s\n' "$FILES_TOUCHED" | grep -c .)
# Exit if not exactly one file
if [ "$FILE_COUNT" -ne 1 ]; then
exit 0
fi
FILENAME="${FILES_TOUCHED%%"${NL}"*}"
# Check if commit matches conventional format without scope
ORIGINAL_TYPE=""
case "$COMMIT_SUMMARY" in
feat:\ ?* | fix:\ ?* | chore:\ ?* | docs:\ ?* | refactor:\ ?* | \
test:\ ?* | build:\ ?* | ci:\ ?* | style:\ ?* | perf:\ ?* | revert:\ ?* | \
feat!:\ ?* | fix!:\ ?* | chore!:\ ?* | docs!:\ ?* | refactor!:\ ?* | \
test!:\ ?* | build!:\ ?* | ci!:\ ?* | style!:\ ?* | perf!:\ ?* | revert!:\ ?*)
ORIGINAL_TYPE="${COMMIT_SUMMARY%%:*}"
;;
esac
# Exit if not a conventional commit
if [ -z "$ORIGINAL_TYPE" ]; then
exit 0
fi
# Exit if filename already in summary
case "$COMMIT_SUMMARY" in
*"$FILENAME"*) exit 0 ;;
esac
PREFIX="$ORIGINAL_TYPE: "
SUFFIX="${COMMIT_SUMMARY#"$PREFIX"}"
MODIFIED_SUMMARY="$ORIGINAL_TYPE($FILENAME): $SUFFIX"
# Only modify if new summary is under 51 chars
if [ "${#MODIFIED_SUMMARY}" -lt 51 ]; then
write_commit_msg "$COMMIT_MSG_FILE" "$MODIFIED_SUMMARY" "$COMMIT_BODY"
echo "Modified commit message summary: $MODIFIED_SUMMARY" >&2
fi