27
27
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28
28
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
29
# =============================================================================
30
- # Git `commit-msg` hook that looks for patterns the branch name
31
- # and uses the matches as prefix or suffix of every commit title.
32
- #
33
- # This is useful if you have convention to use issue number in the branch,
34
- # and want to add it automatically to the commit title.
35
- # For example, you have branch like `FOO-9999-add-hook`, on adding the commit `Add Hook`,
36
- # the hook may add suffix `Add Hook (FOO-9999)` or prefix `[FOO-9999] Add Hook`.
37
- #
38
- # Note: you can use `commit --no-verify` to skip `commit-msg` hooks.
39
- #
40
- # Example of config:
41
- # ~~~
42
- # [conventional-commit]
43
- # excluded-branch = master
44
- # excluded-branch = development
45
- # prefix-pattern = "FOO-[0-9]+"
46
- # suffix-pattern = "FOO-[0-9]+"
47
- # ~~~
48
30
49
31
FCR=' \033[1;31m' # Red
50
32
FCG=' \033[1;32m' # Green
51
33
FCY=' \033[0;33m' # Yellow
52
34
FCS=' \033[0;37m' # Light gray (silver)
53
35
NC=' \033[0m'
54
36
37
+ CMD=" conventional-commit"
38
+ VERSION=" 1.0.0"
39
+ HOME=" https://github.com/slavcodev/git-hooks-scripts"
40
+ ABOUT=" ${FCG} Conventional commit title${NC} version ${FCY} $VERSION ${NC} "
41
+ HELP=" $ABOUT
42
+
43
+ Git 'commit-msg' hook that looks for patterns the branch name
44
+ and uses the matches as prefix or suffix of every commit title.
45
+
46
+ This is useful if you have convention to use issue number in the branch,
47
+ and want to add it automatically to the commit title.
48
+ For example, you have branch like 'FOO-9999-add-hook', on adding the commit 'Add Hook,
49
+ the hook may add suffix 'Add Hook (FOO-9999)' or prefix '[FOO-9999] Add Hook'.
50
+
51
+ Note: you can use 'commit --no-verify' to skip 'commit-msg' hooks.
52
+
53
+ ${FCY} Example of config:${NC}
54
+ ${FCS} ~~~
55
+ [${CMD} ]
56
+ excluded-branch = master
57
+ excluded-branch = development
58
+ prefix-pattern = " FOO-[0-9]+"
59
+ suffix-pattern = " FOO-[0-9]+"
60
+ ~~~${NC}
61
+
62
+ $HOME
63
+
64
+ > Enjoy coding ❤️"
65
+
55
66
report_start () {
56
67
echo " ${FCG} $@ ${NC} "
57
68
}
@@ -64,63 +75,73 @@ report_done() {
64
75
[[ $# > 0 ]] && echo " ${FCG} ✓ $@ ${NC} " || echo " ${FCG} ✓ Done${NC} "
65
76
}
66
77
67
- prefix_pattern=$( git config --get conventional-commit.prefix-pattern)
68
- suffix_pattern=$( git config --get conventional-commit.suffix-pattern)
78
+ main () {
79
+ prefix_pattern=$( git config --get conventional-commit.prefix-pattern)
80
+ suffix_pattern=$( git config --get conventional-commit.suffix-pattern)
69
81
70
- if [[ -z ${prefix_pattern} && -z ${suffix_pattern} ]]; then
71
- exit
72
- fi
82
+ if [[ -z ${prefix_pattern} && -z ${suffix_pattern} ]]; then
83
+ exit
84
+ fi
85
+
86
+ branch_name=$( git rev-parse --abbrev-ref HEAD)
73
87
74
- branch_name =$( git rev-parse --abbrev-ref HEAD )
88
+ excluded_branches =$( git config --get-all conventional-commit.excluded-branch )
75
89
76
- excluded_branches=$( git config --get-all conventional-commit.excluded-branch)
90
+ if [[ -z ${excluded_branches} ]]; then
91
+ excluded_branches=" master"
92
+ fi
77
93
78
- if [[ -z ${excluded_branches} ]]; then
79
- excluded_branches=" master"
80
- fi
94
+ for excluded_branch in ${excluded_branches} ; do
95
+ if [[ " ${branch_name} " =~ " ${excluded_branch} " ]]; then
96
+ exit
97
+ fi
98
+ done
81
99
82
- for excluded_branch in ${excluded_branches} ; do
83
- if [[ " ${branch_name} " =~ " ${excluded_branch} " ]]; then
100
+ commit_file=$1
101
+ commit_title=$( head -n 1 " ${commit_file} " )
102
+
103
+ if [[ -z " ${commit_title} " ]]; then
84
104
exit
85
105
fi
86
- done
87
-
88
- commit_file=$1
89
- commit_title=$( head -n 1 " ${commit_file} " )
90
106
91
- if [[ -z " ${commit_title} " ]]; then
92
- exit
93
- fi
107
+ prefix=$( echo " ${branch_name} " | grep -Eo " ${prefix_pattern} " )
108
+ suffix=$( echo " ${branch_name} " | grep -Eo " ${suffix_pattern} " )
94
109
95
- prefix=$( echo " ${branch_name} " | grep -Eo " ${prefix_pattern} " )
96
- suffix=$( echo " ${branch_name} " | grep -Eo " ${suffix_pattern} " )
110
+ if [[ -z " ${prefix} " && -z " ${suffix} " ]]; then
111
+ exit
112
+ fi
97
113
98
- if [[ -z " ${prefix} " && -z " ${suffix} " ]]; then
99
- exit
100
- fi
114
+ prefix_exists=$( echo " ${commit_title} " | grep -o " ^\[${prefix} \] " )
115
+ suffix_exists=$( echo " ${commit_title} " | grep -o " (${suffix} )$" )
101
116
102
- prefix_exists= $( echo " $ {commit_title} " | grep -o " ^\[ ${ prefix} \] " )
103
- suffix_exists= $( echo " $ {commit_title} " | grep -o " ( ${ suffix} )$ " )
117
+ commit_title= $ {commit_title#* ]} # remove prefix ending in "."
118
+ commit_title= $ {commit_title% ( * } # remove suffix starting with "."
104
119
105
- commit_title=${commit_title#* ]} # remove prefix ending in "."
106
- commit_title=${commit_title% (* } # remove suffix starting with "."
120
+ if [[ -z " ${prefix_exists} " ]]; then
121
+ echo " Adding prefix '${prefix} ' to the commit title..."
122
+ sed -i -e ' s/' " ${commit_title} " ' /' " [${prefix} ] ${commit_title} " ' /g' " ${commit_file} "
123
+ else
124
+ echo " Prefix '${prefix} ' already exists, skipping..."
125
+ fi
107
126
108
- if [[ -z " ${prefix_exists } " ]]; then
109
- echo " Adding prefix '${prefix } ' to the commit title..."
110
- sed -i -e ' s/' " ${commit_title} " ' /' " [ ${prefix} ] ${commit_title} " ' /g' " ${commit_file} "
111
- else
112
- echo " Prefix '${prefix } ' already exists, skipping..."
113
- fi
127
+ if [[ -z " ${suffix_exists } " ]]; then
128
+ echo " Adding suffix '${suffix } ' to the commit title..."
129
+ sed -i -e ' s/' " ${commit_title} " ' /' " ${commit_title} ( ${suffix} ) " ' /g' " ${commit_file} "
130
+ else
131
+ echo " Suffix '${suffix } ' already exists, skipping..."
132
+ fi
114
133
115
- if [[ -z " ${suffix_exists} " ]]; then
116
- echo " Adding suffix '${suffix} ' to the commit title..."
117
- sed -i -e ' s/' " ${commit_title} " ' /' " ${commit_title} (${suffix} )" ' /g' " ${commit_file} "
118
- else
119
- echo " Suffix '${suffix} ' already exists, skipping..."
120
- fi
134
+ echo " Title was changed to '$( head -n 1 " ${commit_file} " ) '"
121
135
122
- echo " Title was changed to ' $( head -n 1 " ${commit_file} " ) ' "
136
+ report_done
123
137
124
- report_done
138
+ exit $?
139
+ }
125
140
126
- exit $?
141
+ case " $1 " in
142
+ -h|--help|-? )
143
+ echo " ${HELP} "
144
+ ;;
145
+ * )
146
+ main
147
+ esac
0 commit comments