-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit-message-converter.sh
51 lines (41 loc) · 1.2 KB
/
commit-message-converter.sh
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
COMMIT_MSG=$(cat "${1:?Missing commit message file}")
# Gets keyword in the commit message.
# {keyword}|{commit message}
OLDIFS=$IFS
IFS='|'
read -r -a ARRAY <<< ${COMMIT_MSG}
KEYWORD=${ARRAY[0]}
IFS=$OLDIFS
CURRENT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_DIR="$(dirname $(dirname ${CURRENT_DIR}))"
# ../../.commit-message-templates
FILE_DIR=${REPO_DIR}/.commit-message-templates
# Reads a file ".commit-message-templates".
[ ! -f ${FILE_DIR} ] && { echo "File not found: ${FILE_DIR}"; exit 99; }
OLDIFS=$IFS
IFS=','
# sed 1d ${FILE_DIR} | while read keyword gitmoji type # This run in a subshell.
while read keyword gitmoji type
do
if [ ${KEYWORD} == ${keyword} ]; then
declare -A object=(
[keyword]=${keyword}
[gitmoji]=${gitmoji}
[type]=${type}
)
break;
fi
done <<< $(sed 1d ${FILE_DIR}) # Run in a mainshell.
IFS=$OLDIFS
# echo keyword, ${object[keyword]}
# echo gitmoji, ${object[gitmoji]}
# echo type, ${object[type]}
# Checks if keyword is matched.
[ -z ${object[keyword]} ] && {
echo "Undefined commit keyword: ${KEYWORD}"
echo "Add it into ${FILE_DIR}"
exit 1
}
# Manipulates commit message.
git commit --amend -m "${object[gitmoji]} ${object[type]}: ${ARRAY[1]}"
exit 0