-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit-msg.bash
executable file
·95 lines (89 loc) · 4.89 KB
/
commit-msg.bash
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
#!/usr/bin/env bash
#
# Copyright (c) 2019-2020 Alessandro Sciarra <[email protected]>
#
# This file is part of GitHooks.
#
# GitHooks is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GitHooks is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GitHooks. If not, see <https://www.gnu.org/licenses/>.
#
#-----------------------------------------------------------------------
#
# Hook script to check the commit message.
#
# ENFORCED POLICY:
# 1) Trailing spaces at beginning of first three lines are removed
# 2) Trailing spaces at the end of all lines are removed
# 3) An endline at the end of the message is added if missing
# 4) A small letter at the beginning of the first line is capitalised
# 5) Any character among ".?!" at the end of the first line is removed
# in a repetitive way (e.g. "commit..!!!" -> "commit")
# 6) Optionally:
# a) The first line of the commit must be at maximum 60 chars.
# This is the hard limit for GitHub at which a commit message is hidden.
# See https://chris.beams.io/posts/git-commit/ for more infortmation.
# b) The first line of the commit must have at least 8 chars.
# c) The second line must be empty
# d) Any following line after the second must be 72 chars at maximum
# e) The first line must begin with a character
#
# This script is called by "git commit" with one argument, the name of
# the file that has the commit message. The hook should exit with non-zero
# status after issuing an appropriate message if it wants to stop the
# commit. The hook is allowed to edit the commit message file.
#
#-----------------------------------------------------------------------
# NOTE: Some global variables defined here are used in function
# and therefore do not be misled and tempted to remove them
# if their name appears only on their definition line!
#
# Preliminary setup and source of bash functions
printf "\n"; trap '[[ $? -ne 0 ]] && printf "\n" || PrintInfo "Hook $(basename "${BASH_SOURCE[0]}") successfully finished!" ""' EXIT
readonly repositoryTopLevelPath="$(git rev-parse --show-toplevel)"
readonly auxiliaryBashCodeTopLevelPath="${repositoryTopLevelPath}/$(dirname "${BASH_SOURCE[0]}")" # do not use readlink here!
readonly hookImplementationFolderName='BashImplementation'
source "${auxiliaryBashCodeTopLevelPath}/${hookImplementationFolderName}/auxiliaryFunctions.bash"
# Global variable for this hook are sourced in the file just sourced!
# Abort immediately on empty commit
if IsCommitMessageEmpty "$1"; then
AbortCommit "Committed aborted due to empty message!"
fi
# Automatic improve commit message
RemoveTrailingSpacesAtBeginOfFirstThreeLines "$1"
RemoveTrailingSpacesAtEndOfEachLine "$1"
AddEndOfLineAtEndOfFileIfMissing "$1"
CapitalizeFirstLetterFirstLine "$1"
RemovePointAtTheEndFirstLine "$1"
# Store commit message in a file to allow user to resume and edit commit
readonly commitMessageFile="${repositoryTopLevelPath}/.git/COMMIT_MSG"
# It is nicer to store a clean message in case the user has to resume editing after a
# hook failure due to a violation of the rules (storing just the file would lead to
# having the bunch of commented lines in the resumed commit more than once)
# NOTE: quit at "diff" in case of "git commit -v".
sed -n -e '/^#/d' -e '/^diff --git/q' -e 'p;d' "$1" > "${commitMessageFile}"
# Check format
if [[ ${doCommitMessageFormatCheck} = 'TRUE' ]]; then
if IsFirstLineNotStartingWithLetter "${commitMessageFile}"; then
AbortCommit "The first line must start with a letter!" GiveAdviceToResumeCommit
elif IsFirstLineShorterThan "${commitHeadlineMinimumLength}" "${commitMessageFile}"; then
AbortCommit "The first line of your commit must be at least ${commitHeadlineMinimumLength} chars long!" GiveAdviceToResumeCommit
elif IsFirstLineLongerThan "${commitHeadlineMaximumLength}" "${commitMessageFile}"; then
AbortCommit "The first line of your commit exceeds the \"${commitHeadlineMaximumLength}\"-char limit!" GiveAdviceToResumeCommit
elif IsSecondLineNotEmpty "${commitMessageFile}"; then
AbortCommit "The second line of your commit must be empty!" GiveAdviceToResumeCommit
elif IsAnyOfTheLinesAfterTheSecondLongerThan "${commitBodyLineMaximumLength}" "${commitMessageFile}"; then
AbortCommit "All the lines of your commit after the second must be shorter than ${commitBodyLineMaximumLength} chars!" GiveAdviceToResumeCommit
fi
fi
# Remove commit file if not needed
rm -f "${commitMessageFile}" || exit 1