-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-commit-msg
31 lines (24 loc) · 1.18 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
#!/bin/sh
#
# An example hook script to prepare the commit log 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. The hook's purpose is to edit the commit
# message file. If the hook fails with a non-zero status,
# the commit is aborted.
#
# Converts branch name 'feature/g503rD57/description' and commit message 'fixed index problem' into a commit message '[g503rD57] fixed index problem'
# Converts branch name 'hotfix/description' and commit message 'fixed index problem' into a commit message '[hotfix] fixed index problem'
#
if [ -z "$BRANCHES_TO_SKIP" ]; then
BRANCHES_TO_SKIP=(master develop test)
fi
BRANCH_NAME=$(git symbolic-ref --short HEAD)
# Seems this one is working :)
# Thanks @mejl for this regex
[[ $BRANCH_NAME =~ ^[^\/]*\/([a-zA-Z0-9]*)\/.*$ ]] && BASH_REMATCH
BRANCH_EXCLUDED=$(printf "%s\n" "${BRANCHES_TO_SKIP[@]}" | grep -c "^$BRANCH_NAME$")
BRANCH_IN_COMMIT=$(grep -c "\[$BRANCH_NAME\]" $1)
if [ -n "$BRANCH_NAME" ] && [ -n "${BASH_REMATCH[1]}" ] && ! [[ $BRANCH_EXCLUDED -eq 1 ]] && ! [[ $BRANCH_IN_COMMIT -ge 1 ]]; then
sed -i.bak -e "1s/^/[${BASH_REMATCH[1]}] /" $1
fi