-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprepare-commit-msg
More file actions
executable file
·58 lines (53 loc) · 1.97 KB
/
prepare-commit-msg
File metadata and controls
executable file
·58 lines (53 loc) · 1.97 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/bash
#
# This hook adds guidance about the commit message format
# on top of the default commit message hints.
#
# Called by "git commit" with the name of the file that has the commit message,
# followed by the description of the commit message's source.
#
# To enable this hook, set the hooksPath in git:
# git config core.hooksPath .dev/githooks
COMMIT_MSG_FILE=$1
COMMIT_SOURCE=$2
SHA1=$3
scopes_file=".dev/scopes.txt"
# Join scopes to regex, skipping empty and commented lines
test -r "$scopes_file" &&
scopes="($(grep -vE '^\s*(#|$)' "$scopes_file" | paste -sd '|' - ))"
beginswith() { case $2 in "$1"*) true;; *) false;; esac; }
# https://mincong.io/2019/07/23/prepare-commit-message-using-git-hook
# Only add custom message when there is no commit source ($COMMIT_SOURCE is empty).
# Otherwise, keep the default message proposed by Git.
# Possible commit sources: message, template, merge, squash or commit.
# See https://git-scm.com/docs/githooks
original=$(cat "$COMMIT_MSG_FILE")
if test -z "$COMMIT_SOURCE"
then
# Find common path prefix of changed files
path=$(while read file
do test -z "$count" && common="$file" && count=$(expr length "$file") ||
while expr substr "$file" $count 1 != substr "$common" $count 1 >/dev/null; do let count--; done
done <<<"$(git -P diff --cached --name-only -r)" &&
expr substr "$common" 1 "$count")
{
# Infer type & scope from changed files
expr "$path" : ".dev" >/dev/null &&
( git config list --local | grep -q kull &&
printf "feat(dev): " ||
printf "build: "; )
# Example for gradle projects
expr "$path" : "gradle" \| "$path" : "build" >/dev/null &&
printf "build(gradle): "
echo "
# Please enter the message in the format:
# <type>(<scope>): <description>
# Types:
# - production code: fix, feat, enhance, refactor, style
# - auxiliaries: docs, test, build
${scopes:+# Allowed scopes: $scopes}
# For details see https://kull.jfischer.org
$original
"
} > "$COMMIT_MSG_FILE"
fi