-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepare-commit-msg
executable file
·51 lines (46 loc) · 1.87 KB
/
prepare-commit-msg
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
#!/bin/bash
#
# This hook adds guidance about the commit message format
# on top of the default commit message.
#
# 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"
test -r $scopes_file &&
scopes="$(cat "$scopes_file" | sed '/^\(#.*\|\)$/d' | sed ':a;N;s/\n/, /g;ta')"
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 &&
printf "feat(dev): "
# Example for gradle projects
expr "$path" : "gradle" \| "$path" : "build" >/dev/null &&
printf "chore(gradle): "
printf "\n\n# Please enter the message in the format\n"
echo "# <type>(<scope>): <description>"
echo "# Possible types: fix, feat, docs, style, refactor, test, chore"
test -n "$scopes" && echo "# Allowed scopes: $scopes"
echo "# For details see https://kull.jfischer.org"
echo "$original"
} > "$COMMIT_MSG_FILE"
fi