-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre-commit
More file actions
executable file
·170 lines (145 loc) · 4.67 KB
/
pre-commit
File metadata and controls
executable file
·170 lines (145 loc) · 4.67 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/bin/bash
# Hook script to enforce branch naming policy.
#
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# Author : Jacques Raphanel
# Version: 1.9
#
# File containing the commit message
BRANCHNAME=$(git rev-parse --abbrev-ref HEAD)
# Predefined list of supported commit types
TYPELIST="chore|ci|fix|bugfix|hotfix|doc(s)?|experimental|build|feat(ure)?|refactor|revert|style|perf|release"
# Project root directory
ROOTDIR=$PWD
COLOR_RESET='\033[0m'
COLOR_ERROR='\033[31m'
# Print an error message through stderr
echo_err() {
printf "${COLOR_ERROR}%s${COLOR_RESET}\n" "$@" >&2
}
# Report invalid commit type error
report_invalid_type() {
echo_err "Invalid commit type detected from branch name !"
echo_err ""
echo_err "The list of supported commit types is:"
echo_err " => $(echo $TYPELIST | sed -e 's:|: :g')"
exit 1
}
# Report invalid JIRA ref error
report_invalid_jira_ref() {
echo_err "Invalid JIRA reference detected from branch name !"
echo_err ""
echo_err "Expected format: <type>/<subject>/<jira_ref>"
echo_err ""
echo_err "Examples:"
echo_err "no-ref"
echo_err "REW-374"
echo_err "DS-241"
exit 1
}
# Report invalid branch name format
report_invalid_format() {
echo_err "Invalid branch name!"
echo_err "Please rename your branch and try to commit again."
echo_err ""
echo_err "Format: <type>/<subject>/<jira_ref>"
echo_err ""
echo_err "where jira_ref can be no-ref if not associated to a jira ticket"
echo_err "or omitted in case of experimental or release branch type"
exit 1
}
# Check that the branch name is following company's conventions
check_branch_name() {
# Build the regular expression to validate the branch name
local COMMITRE="^(${TYPELIST})" # Branch type
COMMITRE="${COMMITRE}/[a-z].*" # Description/Subject
COMMITRE="${COMMITRE}/(no-ref|[A-Z]{2,6}-[0-9]+)\$" # JIRA reference
if echo $BRANCHNAME | grep -Eq "$COMMITRE" >& /dev/null; then
return
fi
# Apply specific logic in case of specific branches:
case "$BRANCHNAME" in
HEAD)
# New project or rebase from specific Git Hash
return
;;
main|master|develop)
# main branch from github project
return
;;
preprod|staging)
# specific branch for deployment purpose
return
;;
release/*)
# specific release branch
if echo $BRANCHNAME | grep -Eq "release/[0-9.]+x\$" >& /dev/null; then
return
fi
echo_err "release branch name must be formatted using 'release/MAJOR.MINOR.x' format"
exit 1
esac
if [ `echo $BRANCHNAME | grep -c '/'` -eq 0 ]; then
report_invalid_format
fi
# Decompose the branch name into simple fields
local BRANCHTYPE=$(echo $BRANCHNAME | cut -d/ -f1)
local SUBJECT=$(echo $BRANCHNAME | cut -d/ -f2)
local JIRAISSUE=$(echo $BRANCHNAME | cut -d/ -f3)
# check if it is related to an unsupported type
COMMITRE="^(${TYPELIST})(/.*)?$"
if ! echo $BRANCHTYPE | grep -Eq "$COMMITRE"; then
report_invalid_type
fi
# ensure that subject is defined
if [ -z "${SUBJECT// /}" ]; then
report_invalid_format
fi
# double-check JIRA issue definition
if [ -z "${JIRAISSUE// /}" ]; then
if [ "$BRANCHTYPE" = "experimental" ]; then
# JIRA issue not required in case of experimental branch
return
fi
else
if ! echo $JIRAISSUE | grep -Eq "^(no-ref|[A-Z]{2,6}-[0-9]+)$"; then
report_invalid_jira_ref
fi
fi
}
# Execute php-cs-fixer with fix mode
run_pre_commit() {
make pre-commit
if [ $? -ne 0 ]; then
exit $?
fi
files=$(git diff --cached --name-only --diff-filter=ACM)
if [ "$files" != "" ]; then
git add $files
fi
}
# Validate the branch name
main() {
# Check if the branch message follow our conventions or not
check_branch_name
if [ -r Makefile ]; then
if [ `grep -Ec '^pre-commit:' Makefile` -gt 0 ]; then
run_pre_commit
fi
fi
}
# Check if commit types are locally customized
if [ -r "$ROOTDIR/.github/conventions/commit_types.conf" ]; then
TYPELIST=$(sed -z 's:\n:|:g' "$ROOTDIR/.github/conventions/commit_types.conf")
if [ -z "$TYPELIST" ]; then
echo_err "Invalid commit_types.conf file detected"
exit 1
fi
fi
# Perform all required checks
main
exit 0
# vim:set tabstop=4 shiftwidth=4 expandtab autoindent: