-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconventional-merge-commit
More file actions
executable file
·58 lines (49 loc) · 2.12 KB
/
conventional-merge-commit
File metadata and controls
executable file
·58 lines (49 loc) · 2.12 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
#!/bin/sh
# prepare-commit-msg:
# rewrite auto-generated merge/squash messages to conventional commit format
# shellcheck source=lib/commit-msg.sh disable=SC1091
. "$(dirname "$0")/lib/commit-msg.sh"
commit_msg_file="$1"
commit_source=${2-}
# Short-circuit: if git explicitly tells us it's not a merge/squash/revert, exit early
# This avoids reading/parsing the message for regular commits
case "$commit_source" in
merge | squash | revert | message | "") ;;
*) exit 0 ;;
esac
# Read and parse commit message
read_commit_msg "$commit_msg_file"
# Strip leading whitespace (POSIX parameter expansion)
stripped="${COMMIT_FIRST_LINE#"${COMMIT_FIRST_LINE%%[![:space:]]*}"}"
# Detect message type from content (single source of truth for trigger patterns)
# Word boundary: keyword alone or followed by non-word character
# Ordered by frequency: most common patterns first (Git defaults: "Merge", "Revert", "Squash")
# ^\s*([Mm]erge[ds]?|[Ss]quash(e[ds])?|[Rr]evert|[Rr]eapply)\b.*$
msg_type=""
case "$stripped" in
[Mm]erge | [Mm]erge[ds] | [Mm]erge[![:alnum:]_]* | [Mm]erge[ds][![:alnum:]_]*) msg_type="merge" ;;
[Rr]evert | [Rr]evert[![:alnum:]_]* | [Rr]eapply | [Rr]eapply[![:alnum:]_]*) msg_type="revert" ;;
[Ss]quash | [Ss]quashe[ds] | [Ss]quash[![:alnum:]_]* | [Ss]quashe[ds][![:alnum:]_]*) msg_type="squash" ;;
esac
# Only process if we detected a merge/squash/revert pattern
if [ -z "$msg_type" ]; then
exit 0
fi
# Transform based on message type
if [ "$msg_type" = "revert" ]; then
# For revert/reapply: "revert: revert <rest>" or "revert: reapply <rest>"
# This reuses the same logic as for merge/squash: lowercase the first letter.
# e.g. "Revert" -> "r" + "evert", "Reapply" -> "r" + "eapply"
rest="${stripped#?}"
new_first_line="revert: r$rest"
else
# For merge/squash: "chore: " + lowercase first char + rest
# Note: Detection ensures first char is M/m (merge) or S/s (squash)
first_char="${stripped%"${stripped#?}"}"
rest="${stripped#?}"
case "$first_char" in
[Mm]) new_first_line="chore: m$rest" ;;
*) new_first_line="chore: s$rest" ;; # Default: S/s (squash)
esac
fi
write_commit_msg "$commit_msg_file" "$new_first_line" "$COMMIT_REST"