From 5af2c5c572d87e1453f38f6225499fd4518c19eb Mon Sep 17 00:00:00 2001 From: gggeek Date: Wed, 21 Dec 2022 10:46:15 +0000 Subject: [PATCH] add git pre-push hook --- .gitattributes | 1 + .githooks/pre-push.sh | 96 +++++++++++++++++++++++++++++++++++++++++++ NEWS.md | 6 +++ composer.json | 3 ++ 4 files changed, 106 insertions(+) create mode 100755 .githooks/pre-push.sh diff --git a/.gitattributes b/.gitattributes index 54f34572..c776c5d8 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,5 +1,6 @@ *.inc diff=php +.githooks/ export-ignore .github/ export-ignore demo/ export-ignore doc/ export-ignore diff --git a/.githooks/pre-push.sh b/.githooks/pre-push.sh new file mode 100755 index 00000000..cecefd31 --- /dev/null +++ b/.githooks/pre-push.sh @@ -0,0 +1,96 @@ +#!/bin/bash + +# Script to be run as part of the github pre-push hook. +# +# Checks that, if there is a "version-like" tag being pushed, all the files which are supposed to contain the tag do +# actually have the correct tag value in them. If they do not, the push is blocked. +# NB: this does _not_ automatically alter the source files and commit them with the correct tag value, nor prevent the +# tag to be added to the wrong git commit locally (ie. a commit in which the source files have the wrong tag value). +# All it does is prevent the developer from pushing the 'bad tags' to remote repositories, giving him/her the chance to +# manually rectify the situation on the local repo before retrying to push. +# +# @todo could this be run as pre-commit hook instead? We have to test if adding a tag does trigger pre-commit hook... +# @see https://stackoverflow.com/questions/56538621/git-hook-to-check-tag-name +# @see https://stackoverflow.com/questions/8418071/is-there-a-way-to-check-that-a-git-tag-matches-the-content-of-the-corresponding +# for an alternative take (enforcing this with a server-side hook) +# +# NB: remember that this can be run within a windows env too, via fe. the tortoisegit or the git-4-win on the cli! +# git for windows comes with its own copy of common unix utils such as bash, grep. But they are sometimes old and/or +# buggy compared to what one gets in current linux distros :-( +# +# This hook is called with the following parameters: +# +# $1 -- Name of the remote to which the push is being done +# $2 -- URL to which the push is being done +# +# If pushing without using a named remote those arguments will be equal. +# +# Information about the commits which are being pushed is supplied as lines to +# the standard input in the form: +# +# + +# We do not abort the push in case there is an error in this script. No `set -e` +#set -e + +# @todo detect if this is run outside git hook, and give a warning plus explain how to pass in $local_ref $local_oid $remote_ref $remote_oid +# @todo allow a git config parameter to switch on/off a 'verbose mode' +# @todo we could allow the variables `files` and `version_tag_regexp` to be set via git config parameters instead of hardcoded + +# List of files which do contain the version tag +files='NEWS.md src/PhpXmlRpc.php doc/phpxmlrpc_manual.adoc' + +# Regexp use to decide if a git tag is a version label +version_tag_regexp='^v?[0-9]{1,4}\.[0-9]{1,4}(\.[0-9]{1,4})?' + +# Create a string of '0' chars of appropriate length for the current git version +zero="$(git hash-object --stdin