1
- #! /bin/sh
1
+ #! /bin/bash
2
2
#
3
- # This hook adds a comment for guidance about the commit message
4
- # format on top of the default commit message.
3
+ # This hook adds guidance about the commit message format
4
+ # on top of the default commit message.
5
5
#
6
- # Called by "git commit" with the name of the file that has the
7
- # commit message, followed by the description of the commit
8
- # message's source. The hook's purpose is to edit the commit
9
- # message file. If the hook fails with a non-zero status,
10
- # the commit is aborted.
6
+ # Called by "git commit" with the name of the file that has the commit message,
7
+ # followed by the description of the commit message's source.
11
8
#
12
9
# To enable this hook, set the hooksPath in git:
13
10
# git config core.hooksPath .dev/githooks
@@ -16,15 +13,37 @@ COMMIT_MSG_FILE=$1
16
13
COMMIT_SOURCE=$2
17
14
SHA1=$3
18
15
16
+ scopes_file=" .dev/scopes.txt"
17
+ test -r $scopes_file &&
18
+ scopes=" $( cat " $scopes_file " | sed ' /^\(#.*\|\)$/d' | sed ' :a;N;s/\n/, /g;ta' ) "
19
+
19
20
beginswith () { case $2 in " $1 " * ) true ;; * ) false ;; esac ; }
20
21
22
+ # https://mincong.io/2019/07/23/prepare-commit-message-using-git-hook
23
+ # Only add custom message when there is no commit source ($COMMIT_SOURCE is empty).
24
+ # Otherwise, keep the default message proposed by Git.
25
+ # Possible commit sources: message, template, merge, squash or commit.
26
+ # See https://git-scm.com/docs/githooks
21
27
original=$( cat " $COMMIT_MSG_FILE " )
22
- if beginswith $' \n #' " $original " ; then
23
- {
24
- printf " \n\n# Please enter the message in the format\n"
25
- echo " # <type>(<scope>): <description>"
26
- echo " # Possible types: fix, feat, docs, style, refactor, test, chore, rework, release"
27
- printf " # For details see https://www.notion.so/softwarechallenge/Git-217333329ea64db5b2cc8bbbaf79db87"
28
- echo " $original "
29
- } > " $COMMIT_MSG_FILE "
28
+ if test -z " $COMMIT_SOURCE "
29
+ then
30
+ # Find common path prefix of changed files
31
+ path=$( while read file
32
+ do test -z " $count " && common=" $file " && count=$( expr length " $file " ) ||
33
+ while expr substr " $file " $count 1 ! = substr " $common " $count 1 > /dev/null; do let count--; done
34
+ done <<< " $(git -P diff --cached --name-only -r)" &&
35
+ expr substr " $common " 1 " $count " )
36
+ {
37
+ # Infer type & scope from changed files
38
+ expr " $path " : " gradle" > /dev/null &&
39
+ printf " chore(gradle): " ||
40
+ printf " fix(%s): " $path
41
+
42
+ printf " \n\n# Please enter the message in the format\n"
43
+ echo " # <type>(<scope>): <description>"
44
+ echo " # Possible types: fix, feat, docs, style, refactor, test, chore, rework, release"
45
+ test -n " $scopes " && echo " # Allowed scopes: $scopes "
46
+ printf " # For details see https://www.notion.so/softwarechallenge/Git-217333329ea64db5b2cc8bbbaf79db87"
47
+ echo " $original "
48
+ } > " $COMMIT_MSG_FILE "
30
49
fi
0 commit comments