diff options
Diffstat (limited to 'pre-receive')
| -rwxr-xr-x | pre-receive | 81 |
1 files changed, 56 insertions, 25 deletions
diff --git a/pre-receive b/pre-receive index e2533da..0c03e1c 100755 --- a/pre-receive +++ b/pre-receive @@ -64,24 +64,24 @@ # [2] "git" is accepted as a category as well, since it's used for # changes to the CI system. -### options -# Push options are supported, using "git push -o <option>". -# Note that this needs to be run on the server: -# git config receive.advertisePushOptions true -# while cd'ed into the bare git repo. +### End of user documentation, the rest is code. -# enable this with: git push -o debug +### Options: + +# Enable this with: git push -o debug $debugging = 0; -# enable this with: git push -o skip +# 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. +# 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 +# Disable colors with -o mono $color_output = 1; -# default branch is usually 'master'. It is for SBo, anyway. Still, +# Default branch is usually 'master'. It is for SBo, anyway. Still, # query repo for this instead of hardcoding (see set_default_branch()). $default_branch = undef; @@ -96,13 +96,19 @@ sub debug { 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; + my $hash = shift; # not used except to print it + my $result; debug("check_message(\"$full_msg\", \"$hash\")"); @@ -163,14 +169,17 @@ sub check_message { } # Note that this runs *in a bare git repo*, so we can't check -# categories by checking that the directory exists. So we +# 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 { - open my $gitpipe, "git ls-tree -d --name-only $default_branch |" or die $!; + open(my $gitpipe, + "-|", "git", "ls-tree", "-d", "--name-only", $default_branch) + or fatal($!); + while(my $l=<$gitpipe>) { next if $l =~ /^\./; # skip .forgejo and .github dirs chomp $l; @@ -180,6 +189,8 @@ sub check_message { # Allow "git" as a 'category', aclemons uses it in CI. $cats{git}++; + + debug("categories: " . join " ", sort { $a cmp $b } keys %cats); } sub category_ok { @@ -200,6 +211,8 @@ sub print_opt_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/) { @@ -212,10 +225,12 @@ sub process_push_opt { } elsif($opt =~ /help/) { $help++; } else { - die "$SELF: unknown push option '-o $opt', try '-o help'\n"; + 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++) { @@ -226,23 +241,31 @@ sub read_push_options { sub set_default_branch { chomp ($default_branch = `git config get init.defaultBranch`); if(!length($default_branch)) { - debug("got empty result from 'git config get init.defaultBranch', assuming 'master'"); + # This seems to happen with repos that were cloned a long + # time ago, I guess because the init.defaultBranch setting + # didn't exist yet in old versions of git? + debug("got empty result from " . + "'git config get init.defaultBranch', " . + "assuming 'master'"); $default_branch = "master"; } debug("default branch is '$default_branch'"); } 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"; + $red = "\x1b[1;31m"; + $yellow = "\x1b[1;33m"; + $green = "\x1b[1;32m"; $color_off = "\x1b[0m"; } else { - $red = ""; - $yellow = ""; - $green = ""; - $color_off = ""; + $red = $yellow = $green = $color_off = ""; } } @@ -279,12 +302,19 @@ if($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"; @@ -292,10 +322,11 @@ while(<STDIN>) { debug("range $range"); my $cmd = "git log --pretty=format:%h%x09%s $range"; - debug("executing: $cmd"); + my @cmd = ("git", "log", "--pretty=format:%h%x09%s", $range); + debug("executing: " . join " ", @cmd); my $count = 0; - open my $git, "$cmd |" or die $!; + open(my $git, "-|", @cmd) or fatal($!); while(<$git>) { chomp; debug("read line from git: $_\n"); @@ -305,7 +336,7 @@ while(<STDIN>) { } close $git; - debug("checked $count commit messages"); + debug("checked $count commit message" . ($count == 1 ? "" : "s")); } if(@errs) { |
