-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommit-msg
More file actions
119 lines (96 loc) · 3.73 KB
/
commit-msg
File metadata and controls
119 lines (96 loc) · 3.73 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
#!/usr/bin/env bash
commit_msg="${1}"
commit_msg_lines=()
error_msg="[COMMIT FAILED]"
allowed_subject_verbs=('Add' 'Create' 'Update' 'Change' 'Fix' 'Refactor' 'Clean up' 'Remove' 'Move' 'Delete' 'Resolve' 'Merge' 'Initial')
# Splits the commit msg into separate lines and adds them to an array
split_commit_msg() {
while IFS= read -r line; do
# Trim trailing spaces from lines
shopt -s extglob
line="${line%%*( )}"
shopt -u extglob
# Ignore comments (lines starting with #)
if [[ ! "${line}" =~ ^# ]]; then
commit_msg_lines+=("${line}")
fi
done < <(cat ${commit_msg})
}
# Validates the commit msg
validate_commit_msg() {
split_commit_msg
# Store the subject
local subject="${commit_msg_lines[0]}"
# Stop validation if the message is empty or no subject is set
if [[ -z "${commit_msg_lines[*]}" ]] || [[ -z "${subject}" ]]; then
exit 0
fi
# Check if the subject has leading whitespace(s)
if [[ "${subject}" =~ ^[[:space:]]+ ]]; then
echo "${error_msg} The subject can not have leading whitespace"
exit 1;
fi
# Check if the subject contains more than 1 word
if [[ $(echo "${subject}" | wc -w) -eq 1 ]]; then
echo "${error_msg} The subject has to contain more than 1 word"
exit 1
fi
# Check if the subject is separated from the body with a blank line
if [[ ${#commit_msg_lines[@]} -gt 1 ]] && [[ -n "${commit_msg_lines[1]}" ]]; then
echo "${error_msg} Separate subject from body with a blank line"
exit 1
fi
# Check if the subject line is limited to 50 characters
if [[ "${#subject}" -gt 50 ]]; then
echo "${error_msg} Limit the subject line to 50 characters (${#commit_msg_lines[0]} characters used)"
exit 1
fi
# Check if the subject line is capitalized
if [[ ! "${subject}" =~ ^[A-Z] ]]; then
echo "${error_msg} Capitalize the subject line"
exit 1
fi
# Check if the subject line does not end with a period
if [[ ! "${subject}" =~ [^\.]$ ]]; then
echo "${error_msg} Do not end the subject line with a period"
exit 1
fi
# Check if the subject starts with one of the allowed verbs
local first_word=$(echo "${subject}" | awk '{print $1;}')
local is_allowed=false
for verb in "${allowed_subject_verbs[@]}"; do
if [[ "${verb}" == "${first_word}" ]]; then
is_allowed=true
fi
done
if [[ "${is_allowed}" == false ]]; then
echo "${error_msg} Use the imperative mood in the subject line"
echo "Your subject has to start with one of the following verbs:"
printf -- ' - %s\n' "${allowed_subject_verbs[@]}"
exit 1
fi
# Check if the body is wrapped at 72 characters (except for url's)
for line in "${commit_msg_lines[@]}"; do
if [[ "${line}" =~ ^[[:space:]]*(https?|ftp|file):\/\/[-A-Za-z0-9\+\&\@\#\/\%\?\=\~\_\|\!\:\,\.\;]*[-A-Za-z0-9\+\&\@\#\/\%\=\~\_\|] ]]; then
continue
elif [[ "${#line}" -gt 72 ]]; then
echo "${error_msg} Wrap the body at 72 characters (${#line} characters used)"
exit 1
fi
done
}
validate_commit_msg
# Remove comment lines from the message
sed -i.bak '/^#/ d' $1
# Remove trailing blank lines
sed -i.bak -e :a -e '/^\n*$/{$d;N;ba' -e '}' $1
# Append the branch name to the commit message
append_branch_name=$(git config --get git-hooks.append-branch-name)
if [[ -n "${append_branch_name}" ]] && [[ "${append_branch_name}" = true ]]; then
branch="$(git symbolic-ref --short HEAD)"
if [[ -z "${branch}" ]]; then
echo "${error_msg} Could not determine the branch name"
exit 1
fi
printf "\n[branch: ${branch}]" >> $1
fi