From 68c8b13abb6b52b1745feca3031decdb4d93bacb Mon Sep 17 00:00:00 2001 From: "B. Watson" Date: Mon, 30 Nov 2026 05:21:27 -0500 Subject: Move hooks to hooks/. --- HOOKS.txt | 38 ++++++ README | 7 +- hooks/pre-commit | 135 +++++++++++++++++++++ hooks/pre-receive | 338 +++++++++++++++++++++++++++++++++++++++++++++++++++++ pre-commit-sbolint | 62 ---------- pre-receive | 338 ----------------------------------------------------- 6 files changed, 512 insertions(+), 406 deletions(-) create mode 100644 HOOKS.txt create mode 100755 hooks/pre-commit create mode 100755 hooks/pre-receive delete mode 100644 pre-commit-sbolint delete mode 100755 pre-receive diff --git a/HOOKS.txt b/HOOKS.txt new file mode 100644 index 0000000..fa371e2 --- /dev/null +++ b/HOOKS.txt @@ -0,0 +1,38 @@ +Git Hooks +========= + +- pre-commit: A git hook every SBo contributor should use, that + automatically calls sbolint and stops you from committing builds that + don't pass sbolint. + +- commit-msg: A git hook every SBo contributor should use, that + checks the formatting of commit messages, and adds the + "category/name: " prefix. + +- prepare-commit-msg: A git hook that adds the "Signed-off-by" line + to every commit message. You should *not* use this unless you are + either an SBo admin, or have received permission from the admins + to "sign off" commits. + +- pre-receive: A git hook that's installed on the SBo git server. + It's a "server-side" hook, so it's not that useful anywhere else, + but it's included here for reference. If you have an intermediate + "staging" git server where you push your changes before pushing + them to SBo or git(hub|lab), you can use this hook there. + +To use these hooks, copy them to your local git worktree (where you +run "git commit", "git push", etc commands), like so: + + cp pre-commit commit-msg /path/to/slackbuilds/.git/hooks + +...and make sure they have +x permission (chmod as needed). + +For documentation on the hooks themselves, see the comments in the +scripts. + +If you ever need to bypass all the git hooks, you can use: + + git commit -n + +Also, pre-commit and commit-msg can be selectively disabled with +environment variables. diff --git a/README b/README index 8fee27e..8768166 100644 --- a/README +++ b/README @@ -17,9 +17,6 @@ Included tools: - sbopkglint: Checks your package, after it's built. -- pre-commit-sbolint: A git pre-commit hook you should use, that - automatically calls sbolint and stops you from committing bad code. - - sbofixinfo: Attempts to automatically fix common .info file problems. - sbodl: Download source files listed in DOWNLOAD and DOWNLOAD_x86_64 @@ -42,9 +39,7 @@ Included tools: incomplete, and not yet installed by "make install". Use it at your own peril. -- pre-receive: A git hook that's installed on the SBo git server. It's - a "server-side" hook, so it's not that useful anywhere else, but - it's included here for reference. +- hooks/*: Git hooks. See HOOKS.txt for documentation. Note that the SBo admins use sbolint and sbopkglint as part of the approval process, when you submit an update. Failure to pass the lint diff --git a/hooks/pre-commit b/hooks/pre-commit new file mode 100755 index 0000000..4cd7aa1 --- /dev/null +++ b/hooks/pre-commit @@ -0,0 +1,135 @@ +#!/bin/bash + +# 20220315 bkw: SBo pre-commit hook, wrapper for sbolint. +# 20260919 bkw: no code changes; update install comments. +# 20261130 bkw: add check for md5sums in the .info file. + +# Installation: + +# This file is part of sbo-maintainer-tools, which you can get +# from SBo: +# https://slackbuilds.org/repository/15.0/system/sbo-maintainer-tools/ + +# Copy this file to /.git/hooks, mode 0755 (or anyway, make it +# executable). + +# Usage: + +# Just do your usual "git commit". When you do, sbolint will run on +# the build you're updating. If it finds any issues, it will cause the +# commit to abort, so you can fix whatever's wrong and try the commit +# again. + +# Since sbolint isn't perfect, you can skip the check for any commit +# by running e.g: + +# SBOLINT=no git commit + +# This script also checks the .info file, to see whether VERSION changed. +# If it has, it checks MD5SUM and/or MD5SUM_x86_64 to see whether they've +# changed. If they haven't, the commit is aborted. This is to guard +# against the common mistake of updating a build's version but forgetting +# to update the md5sums in the .info file. If you are 100% sure your +# commit is correct, you can skip the md5sum check: + +# MD5OK=yes git commit + +# Notes: + +# This really uses bash features, please don't try changing the top +# line to #!/bin/sh, it'll fail (even if /bin/sh is a symlink to +# /bin/bash). + +# There should normally only be one changed build per commit, but this +# rule may get broken when the repo's frozen pending a new Slackware +# release. So use a loop. + +####################################################################### +set -e +exec 1>&2 + +check_info_file() { + local file="$1" # this is the modified version + local origfile="$1".old.$$ # this will be the original + local cksum + + # could be an array but I refuse to use bash's hideous array syntax: + local errmsg + + # if the file was removed, don't try to check it. + [ -e "$file" ] || return 0 + + # reconstitute the file as it was before this commit + cp "$file" "$origfile" + git diff --cached "$file" | patch -s -R -p3 "$origfile" + + # if the file just added, $origfile won't exist after patching. + [ -e "$origfile" ] || return 0 + + source "$origfile" + oldver="$VERSION" + oldmd5="$MD5SUM" + oldmd5_64="$MD5SUM_x86_64" + rm -f "$origfile" + + source "$file" + if [ "$VERSION" != "$oldver" ]; then + if [ -n "$oldmd5" -a -n "$MD5SUM" -a "$oldmd5" = "$MD5SUM" ]; then + errmsg+=" VERSION was updated but MD5SUM was not!\n" + fi + if [ -n "$oldmd5_64" -a -n "$MD5SUM_x86_64" -a "$oldmd5_64" = "$MD5SUM_x86_64" ]; then + errmsg+=" VERSION was updated but MD5SUM_x86_64 was not!\n" + fi + fi + + if [ -z "$MD5SUM" -a -z "$MD5SUM_x86_64" ]; then + errmsg+=" MD5SUM and MD5SUM_x86_64 are both blank!\n" + else + # no quotes: we want to break on whitespace-separated words here. + for cksum in $MD5SUM $MD5SUM_x86_64; do + [[ $cksum =~ ^[a-z0-9]{32}$ ]] || errmsg+=" Invalid md5sum: $cksum\n" + done + fi + + if [ -n "$errmsg" ]; then + echo -e "*** $file:\n$errmsg" + cat </dev/null; then + echo "WARNING: can't find sbolint in PATH, no linting will be done" + else + while read build; do + # If there's no slack-desc or README, assume the build was removed. + # The directory still might exist after a "git rm -rf" because it + # might contain untracked files (e.g. the source tarball). + if [ -e "$build/slack-desc" -o -e "$build/README" ]; then + sbolint "$build" || sbolintfailed=1 + fi + done < <(git diff --cached --name-only | cut -d/ -f1,2 | sort -u) + fi + if [ -n "$sbolintfailed" ]; then + echo "*** sbolint failed, fix the errors or set SBOLINT=no" + echo "*** in the environment to commit anyway." + exit 1 + fi +fi + +exit 0 diff --git a/hooks/pre-receive b/hooks/pre-receive new file mode 100755 index 0000000..52303ab --- /dev/null +++ b/hooks/pre-receive @@ -0,0 +1,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: " or "python/*: ", 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: +# +# 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() { + 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; diff --git a/pre-commit-sbolint b/pre-commit-sbolint deleted file mode 100644 index 1a0c796..0000000 --- a/pre-commit-sbolint +++ /dev/null @@ -1,62 +0,0 @@ -#!/bin/bash - -# 20220315 bkw: SBo pre-commit hook, wrapper for sbolint. -# 20260919 bkw: no code changes; update install comments. - -# Installation: - -# This file is part of sbo-maintainer-tools, which you can get -# from SBo: -# https://slackbuilds.org/repository/15.0/system/sbo-maintainer-tools/ - -# Copy this file to /.git/hooks, mode 0755 (or anyway, make it -# executable). - -# Usage: - -# Just do your usual "git commit". When you do, sbolint will run on -# the build you're updating. If it finds any issues, it will cause the -# commit to abort, so you can fix whatever's wrong and try the commit -# again. - -# Since sbolint isn't perfect, you can skip the check for any commit -# by running e.g: - -# SBOLINT=no git commit - -# You can also run sbolint by itself, and read its documentation with -# "sbolint --docs". - -set -e -exec 1>&2 - -# There should normally only be one changed build per commit, but this -# rule may get broken when the repo's frozen pending a new Slackware -# release. So use a loop. - -# The weird-looking "< <(command)" syntax is why this script must -# have a #!/bin/bash at the top: it won't work with #!/bin/sh, even if -# /bin/sh is a symlink to bash. - -if [ "${SBOLINT:-yes}" = "yes" ]; then - sbolintfailed="" - if ! which sbolint &>/dev/null; then - echo "WARNING: can't find sbolint in PATH, no linting will be done" - else - while read build; do - # if there's no slack-desc or README, assume the build has been removed. - # the directory still might exist after a "git rm -rf" because it - # might contain untracked files (e.g. the source tarball). - if [ -e "$build/slack-desc" -o -e "$build/README" ]; then - sbolint "$build" || sbolintfailed=1 - fi - done < <(git diff --cached --name-only | cut -d/ -f1,2 | sort -u) - fi - if [ -n "$sbolintfailed" ]; then - echo "*** sbolint failed, fix the errors or set SBOLINT=no" - echo "*** in the environment to commit anyway." - exit 1 - fi -fi - -exit 0 diff --git a/pre-receive b/pre-receive deleted file mode 100755 index 52303ab..0000000 --- a/pre-receive +++ /dev/null @@ -1,338 +0,0 @@ -#!/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: " or "python/*: ", 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: -# -# 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() { - 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; -- cgit v1.2.3