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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
|
#!/usr/bin/perl -w
# git pre-receive hook for SBo.
# checks commit message formats, aborts push if any are bad.
# 20261123 bkw: initial version. may still need work.
# 20261123 bkw: added colors and -o mono, add test env instructions.
# 20261124 bkw: this is in git now, see sbo-maintainer-tools log for changes.
###########################################################################
# How to set up a testing environment for this
#
# 1. Create a bare git repo:
# git clone --bare git://git.slackbuilds.org/slackbuilds.git ~/sbo.bare
#
# 2. Copy this file to the bare repo and make it executable:
# cp /path/to/pre-receive ~/sbo.bare/hooks
# chmod +x ~/sbo.bare/hooks/pre-receive
#
# 3. Enable this config option in the bare repo:
# cd ~/sbo.bare
# git config receive.advertisePushOptions true
#
# 4. Go to your regular git work dir, and create some commits with
# deliberately bad messages. Leave off the category, make them
# longer than the 68-character limit, etc.
#
# 5. Try to push these commits to your bare repo:
# git push ~/sbo.bare
#
# 6. You should see error messages complaining about the bad commits,
# and the push should be rejected.
#
# 7. You can see verbose debug info from this hook by doing this:
# git push -o debug ~/sbo.bare
#
###########################################################################
# This is a "server side" hook. Installing it your regular worktree's
# .git/hooks/ won't do anything.
# This hook looks for "invalid" commit messages.
# A valid message looks like [1]:
# category/build: Message.
# Where:
# - The overall length must be <= 68 characters.
# - Category is one of the top-level dirs in the git repo [2].
# - Build consists only of upper/lowercase letters, numbers,
# underscore, dash, period, or plus characters.
# - A single colon and space are required after the build.
# - The message can be anything.
# - Ideally, there should be a period (dot, full stop, ".") at
# the end of the message, but this script doesn't enforce that,
# since sometimes it has to be left out to squeeze the message in to
# 68 chars.
# [1] A message beginning with "Public www update" is considered valid,
# since this is standard for weekly updates.
# [2] "git" is accepted as a category as well, since it's used for
# changes to the CI system.
### End of user documentation, the rest is code.
### Options:
# Enable this with: git push -o debug
$debugging = 0;
# Enable this with: git push -o skip
# Gives the operator a way to work around this script, in case there's
# a bug in it. Also useful during 'crunch time', aka 'dev cycle', when
# we allow commits to break the "one commit per directory" rule, and
# use "all: <message>" or "python/*: <message>", etc.
$skip = 0;
# Disable colors with -o mono
$color_output = 1;
# Default branch is 'master' for SBo, despite newer releases of git
# chaning the dedault to 'main' for newly-created repos. We aren't
# going to change our existing (for 15+ years) repo.
$default_branch = 'master';
###
@errs = ();
($SELF = $0) =~ s,.*/,,;
sub debug {
return unless $debugging;
warn "$SELF: $_\n" for @_;
}
sub fatal {
die "$SELF: $_[0]\n";
}
sub errmsg {
return " " . yellow($_[0]) . "\n";
}
# Meat and potatoes here. Eat hearty!
sub check_message {
my $full_msg = shift;
my $hash = shift; # not used except to print it
my $result;
debug("check_message(\"$full_msg\", \"$hash\")");
# length check can be combined with the 'serious' errors.
if(length($full_msg) > 68) {
$result = errmsg("Length exceeds 68 characters");
}
for($full_msg) {
# don't check these!
return if /^Public www update:/;
# first 3 errors are serious enough that we don't check
# for the rest (hence the 'last').
if(! /:/) {
$result .= errmsg("No category/build (no colon)");
last;
}
my ($catprg, $msg) = split /:/, $_, 2;
if($catprg !~ m{/}) {
$result .= errmsg("No category");
last;
}
my ($cat, $prg, $junk) = split /\//, $catprg;
if(defined($junk)) {
$result .= errmsg("Invalid category/build (2 or more slashes)");
last;
}
# the rest of these, we can have more than one of.
if($prg =~ /[^-A-Za-z0-9_+.]/) {
$result .= errmsg("Invalid character in build name '$prg'");
}
if(!category_ok($cat)) {
$result .= errmsg("Invalid category '$cat'");
}
if($msg !~ /^ /) {
$result .= errmsg("Missing space after colon");
}
if($msg =~ /^ /) {
$result .= errmsg("Extra space after colon");
}
}
if(defined $result) {
push @errs, (
red("* Malformed commit message for ") . yellow($hash). "\n",
green($full_msg) . "\n",
$result,
"\n" );
}
}
# Note that this runs *in a bare git repo*, so we can't check
# a category by checking that its directory exists. So we
# have to run 'git ls-tree' to get them, which slows this down
# slightly.
{
my %cats;
sub init_cats {
my @cmd = ("git", "ls-tree", "-d", "--name-only", $default_branch);
debug("executing " . join(" ", @cmd));
open(my $gitpipe, "-|", @cmd) or fatal($!);
while(my $l=<$gitpipe>) {
next if $l =~ /^\./; # skip .forgejo and .github dirs
chomp $l;
$cats{$l}++;
}
close $gitpipe;
# Allow "git" as a 'category', aclemons uses it in CI.
$cats{git}++;
debug("categories: " . join " ", sort { $a cmp $b } keys %cats);
}
sub category_ok {
init_cats() unless %cats;
return $cats{$_[0]};
}
}
sub print_opt_help {
print yellow("Options:") . "\n";
print " " . green("-o skip") .
" Accept all commit message as-is; skip error checking.\n";
print " " . green("-o debug") .
" Print debug messages about commit message checking.\n";
print " " . green("-o mono") .
" Don't colorize output.\n";
print " " . green("-o help") .
" Display this help and abort the commit.\n";
}
# Don't be picky about case or whether someone types "debugging"
# when they meant "debug".
sub process_push_opt {
my $opt = lc shift;
if($opt =~ /debug/) {
$debugging++;
} elsif($opt =~ /skip/) {
$skip++;
} elsif($opt =~ /mono/) {
$color_output = 0;
init_colors();
} elsif($opt =~ /help/) {
$help++;
} else {
fatal("$SELF: unknown push option '-o $opt', try '-o help'");
}
}
# man page githooks(5) and git's pre-receive.sample explain
# how this works.
sub read_push_options {
return unless my $count = $ENV{GIT_PUSH_OPTION_COUNT};
for(my $i = 0; $i < $count; $i++) {
process_push_opt($ENV{"GIT_PUSH_OPTION_" . $i});
}
}
sub init_colors {
# Good old ANSI colors. Really should be using 'tput' or something
# to get these, but I don't think anyone's still using anything
# that doesn't either support or at least know how to ignore these
# escape codes.
# Note that the "1;" means we're using bold. It looks nicer to
# me, on my black-background urxvt, but YMMV.
if($color_output) {
$red = "\x1b[1;31m";
$yellow = "\x1b[1;33m";
$green = "\x1b[1;32m";
$color_off = "\x1b[0m";
} else {
$red = $yellow = $green = $color_off = "";
}
}
sub red {
return "$red$_[0]$color_off";
}
sub yellow {
return "$yellow$_[0]$color_off";
}
sub green {
return "$green$_[0]$color_off";
}
### main()
init_colors();
read_push_options();
if($help) {
print_opt_help();
exit 1;
}
if($debugging) {
chomp(my $pwd = `pwd`);
debug("$0 starting up in dir $pwd with -o debug");
}
if($skip) {
debug("skipping commit message checks due to -o skip");
exit 0;
}
# Lines of input look like:
# <start-hash> <end-hash> <ref>
# See git's pre-receive.sample.
# Normally we only get one line of input, but it's unclear to me
# whether this is *always* the case, hence the while loop.
while(<STDIN>) {
chomp;
debug("STDIN line: $_");
my($start, $end, $ref, undef) = split / /;
# $ref currently unused, but maybe it shouldn't be?
if($start eq ('0' x 40)) {
# all-zeroes start hash means, new branch being pushed
$range = "$default_branch..$end";
} else {
$range = "$start..$end";
}
debug("range $range");
my $cmd = "git log --pretty=format:%h%x09%s $range";
my @cmd = ("git", "log", "--pretty=format:%h%x09%s", $range);
debug("executing: " . join " ", @cmd);
my $count = 0;
open(my $git, "-|", @cmd) or fatal($!);
while(<$git>) {
chomp;
debug("read line from git: $_\n");
$count++;
my($hash, $msg) = split /\t/, $_, 2;
check_message($msg, $hash);
}
close $git;
debug("checked $count commit message" . ($count == 1 ? "" : "s"));
}
if(@errs) {
print red("!!! git push aborted; fix the errors below and try again.") .
"\n ...or, if you're " . red("100% certain") .
" everything is correct, re-run the\n" .
" command as: " . yellow("git push -o skip") . "\n" .
" ...and let Urchlay know you found a bug in the pre-receive hook.\n\n";
print for @errs;
}
exit @errs;
|