Skip to content

Commit 202e696

Browse files
committed
chore: add Git hook for linting commit messages
1 parent 8592704 commit 202e696

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

tools/git/hooks/commit-msg

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env bash
2+
#
3+
# @license Apache-2.0
4+
#
5+
# Copyright (c) 2023 The Stdlib Authors.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
19+
# A Git hook called after a developer writes a commit message. If this scripts exits with a non-zero status, the commit will be aborted.
20+
#
21+
# This hook is called with the following arguments:
22+
#
23+
# - `$1`: path to temporary file containing the commit message written by the developer
24+
25+
# shellcheck disable=SC2181
26+
27+
28+
# VARIABLES #
29+
30+
# Resolve environment variables:
31+
skip_lint="${SKIP_LINT_COMMIT}"
32+
33+
# Get the path to a file containing the commit message:
34+
commit_message="$1"
35+
36+
37+
# FUNCTIONS #
38+
39+
# Defines an error handler.
40+
#
41+
# $1 - error status
42+
on_error() {
43+
cleanup
44+
exit "$1"
45+
}
46+
47+
# Runs clean-up tasks.
48+
cleanup() {
49+
echo '' >&2
50+
}
51+
52+
# Runs initialization tasks.
53+
init() {
54+
return 0
55+
}
56+
57+
# Lints a commit message.
58+
lint() {
59+
if [[ -z "${skip_lint}" ]]; then
60+
make FILES="${commit_message}" lint-commit-files > /dev/null >&2
61+
if [[ "$?" -ne 0 ]]; then
62+
echo '' >&2
63+
echo 'Git commit message lint errors.' >&2
64+
return 1
65+
fi
66+
fi
67+
68+
return 0
69+
}
70+
71+
# Main execution sequence.
72+
main() {
73+
init
74+
if [[ "$?" -ne 0 ]]; then
75+
on_error 1
76+
fi
77+
lint
78+
if [[ "$?" -ne 0 ]]; then
79+
on_error 1
80+
fi
81+
cleanup
82+
exit 0
83+
}
84+
85+
# Run main:
86+
main

0 commit comments

Comments
 (0)