Files
@ 42155469350e
Branch filter:
Location: majic-scripts/hooks/hg_commit_message.sh
42155469350e
8.0 KiB
text/x-sh
Noticket: Added a small script for creating quick backups of files before making changes to them.
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | #!/bin/bash
#
# hg_commit_message.sh
#
# Copyright (C) 2013, Branko Majic <branko@majic.rs>
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
program="hg_commit_message.sh"
version="0.1-dev"
function usage() {
cat <<EOF
$program $version, a Mercurial hook that can be used for validating commit
message format.
Usage: $program [OPTIONS] (regex|hgrc)
$program is a Mercurial hook that can be used for validating commit message
format. The hook allows validation of commit messages against a regular
expression.
The hook can be configured either through command-line arguments, or via hgrc
"commit_message" section.
The hook accepts a single positional argument, that should either be the regular
expression that will be used to validate the commit messages, or constant
"hgrc", which indicates that the hook should read its configurtion from the
Mercurial configuration files. If set to "hgrc", set the option "regex" in
Mercurial configuration files instead.
The validation will try to match _any_ part of the message with the specified
regular expression. Therefore, if it's necesasry to match the _whole_ message,
using '^' and '$' at beginning/end of the regex string is necessary.
The hook can be used as one of the following hook types:
- pretxncommit (on downstream side, to run the commit message validation before
the content is committed locally)
- pretxnchangegroup (on upstream side, to run the commit message validation
before the pushed content is accepted)
Downstream example, using command-line arguments for configuration:
[hooks]
pretxncommit.message = /path/to/$program -e "^(TST-[[:digit:]]+|Noticket): "
Downstream example, using Mercurial configuration files:
[hooks]
pretxncommit.message = /path/to/$program hgrc
[commit_message]
extended_regex = True
regex = ^(TST-[[:digit:]]+|Noticket):[[:space:]]
Upstream example, using command-line arguments for configuration:
[hooks]
pretxnchangegroup.message = /path/to/$program -e "^(TST-[[:digit:]]+|Noticket): "
Upstream example, using Mercurial configuration files:
[hooks]
pretxnchangegroup.message = /path/to/$program hgrc
[commit_message]
extended_regex = True
regex = ^(TST-[[:digit:]]+|Noticket):[[:space:]]
$program accepts the following options:
-e Treat the provided regular expression as extended. Default is to
treat the expression as plain regular expression. If configuring
inside of hgrc use option "extended_regex" (set to True or
False).
-m msg An alternative error message that should be output to the
user. Default is to output the regular expression used for
validation. If configuring inside of hgrc use option
"error_message". If custom message contains string "%s", it will
be replaced by the actual regular expression.
-v show script version and licensing information
-h show usage help
Please report bugs and send feature requests to <branko@majic.rs>.
EOF
}
function version() {
cat <<EOF
$program, version $version
+-----------------------------------------------------------------------+
| Copyright (C) 2013, Branko Majic <branko@majic.rs> |
| |
| This program 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. |
| |
| This program 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 this program. If not, see <http://www.gnu.org/licenses/>. |
+-----------------------------------------------------------------------+
EOF
}
# Set-up colours for message printing if we're not piping and terminal is
# capable of outputting the colors.
_color_terminal=$(tput colors 2>&1)
if [[ -t 1 ]] && (( ${_color_terminal} > 0 )); then
_text_bold=$(tput bold)
_text_white=$(tput setaf 7)
_text_blue=$(tput setaf 6)
_text_green=$(tput setaf 2)
_text_yellow=$(tput setaf 3)
_text_red=$(tput setaf 1)
_text_reset=$(tput sgr0)
else
_text_bold=""
_text_white=""
_text_blue=""
_text_green=""
_text_yellow=""
_text_red=""
_text_reset=""
fi
# Set-up functions for printing coloured messages.
function debug() {
echo "${_text_bold}${_text_blue}[DEBUG]${_text_reset}" "$@"
}
function info() {
echo "${_text_bold}${_text_white}[INFO] ${_text_reset}" "$@"
}
function success() {
echo "${_text_bold}${_text_green}[OK] ${_text_reset}" "$@"
}
function warning() {
echo "${_text_bold}${_text_yellow}[WARN] ${_text_reset}" "$@"
}
function error() {
echo "${_text_bold}${_text_red}[ERROR]${_text_reset}" "$@" >&2
}
# Set-up the error codes.
ERR_INVALID_ARGUMENT=1
ERR_INVALID_FORMAT=2
ERR_INVALID_CALL=3
# If no arguments were given, just show usage help.
if [[ -z $1 ]]; then
usage
exit 0
fi
# Set-up some defaults.
grep_options="-qe"
error_message="The following commit message did not validate against the regular expression '%s':"
config_section="commit_message"
# Parse the arguments
while getopts "em:vh" opt; do
case "$opt" in
e) grep_options="-qEe";;
m) error_message="$OPTARG";;
v) version
exit 0;;
h) usage
exit 0;;
*) usage
exit $ERR_INVALID_ARGUMENT;;
esac
done
i=$OPTIND
shift $(($i-1))
# Make sure that only one positional argument was provided.
if [[ ${#@} != 1 ]]; then
error "Incorrect number of positional arguments was provided (${#@} instead of 1)."
exit $ERR_INVALID_ARGUMENT
fi
# Read the positional arguments.
regex="$1"
# Read options from the Mercurial configuration files.
if [[ $regex == "hgrc" ]]; then
regex=$(hg showconfig "${config_section}.regex")
if [[ $(hg showconfig "${config_section}.extended_regex") == "True" ]]; then
grep_options="-qEe"
fi
hgrc_error_message=$(hg showconfig "${config_section}.error_message")
if [[ -n "$hgrc_error_message" ]]; then
error_message="$hgrc_error_message"
fi
fi
# Verify the arguments.
if [[ -z $regex ]]; then
error "Regular expression may not be empty."
exit $ERR_INVALID_ARGUMENT
fi
if [[ -z $error_message ]]; then
error "Error message may not be empty."
exit $ERR_INVALID_ARGUMENT
fi
if [[ -z $HG_NODE ]]; then
error "Improper use of hook. No HG_NODE variable was set prior to hook execution."
exit $ERR_INVALID_CALL
fi
# Validate all commit messages, starting from the first new node, all the way up
# to the tip.
for node in $(hg log -r "${HG_NODE}:tip" --template '{node}\n'); do
message=$(hg log -r "$node" --template '{desc}')
if ! echo "$message" | grep "$grep_options" "$regex"; then
error "$(printf "$error_message" "$regex")"
error "Changeset: $node"
error "^${message}$"
exit $ERR_INVALID_FORMAT
fi
done
success "All commit messages have passed the format validation."
exit 0
|