Skip to content

Instantly share code, notes, and snippets.

@grahamc

grahamc/hook.sh Secret

Created October 30, 2017 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grahamc/b5d2a10350c6f607486f3b93710ba34f to your computer and use it in GitHub Desktop.
Save grahamc/b5d2a10350c6f607486f3b93710ba34f to your computer and use it in GitHub Desktop.
grahamc@Morbo> cat .git/hooks/pre-push ~/projects/nixpkgs
#!/nix/store/qp5fw57d38bd1n07ss4zxh88zg67c3vg-bash-4.4-p12/bin/bash
#
# 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.
PATH="/nix/store/vp8y0rh8rq7vikbbrpf28jfym79xq9z8-coreutils-8.28/bin:/nix/store/la2d2akppv70g8xd1d8vhd53iqskin9r-git-2.14.3/bin"
set -eu
remote="$1"
url="$2"
z40=0000000000000000000000000000000000000000
pass() {
echo "@";
exit 0
}
fail() {
echo "$@";
exit 1
}
debug() {
if [ "x${SAFE_MODE:-x}" != "xx" ]; then
echo "DEBUG: $@" >&2
fi
}
debug "Inputs:"
debug " remote=$remote"
debug " url=$url"
if [ "x${SAFE_MODE:-x}" != "xx" ]; then
debug "Enabling safe-mode because SAFE_MODE is set"
else
# Don't push new branches to NixOS/Nixpkgs
case "$url" in
*NixOS/nixpkgs.git)
debug "Safe mode activated."
;;
*)
debug "$url doesn't appear to be upstream, go wild"
exit 0
esac
fi
while read local_ref local_sha remote_ref remote_sha
do
debug "Handling:"
debug " local_ref=$local_ref"
debug " local_sha=$local_sha"
debug " remote_ref=$remote_ref"
debug " remote_sha=$remote_sha"
if [ "$local_sha" = $z40 ]
then
case "$remote_ref" in
'refs/heads/master')
fail "Refusing to delete master"
;;
refs/heads/release-*)
fail "Don't delete releases!"
;;
*)
pass "Delete permitted by fall-through"
;;
esac
else
if [ "$remote_sha" = $z40 ]
then
fail "Don't create new branches upstream"
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha"
# Check for WIP commit
commits=`git rev-list "$range"`
count=$(echo "$commits" | wc -l)
if [ "$count" -gt 1 ]; then
git log "$range"
echo "-----------------------------------"
echo "You tried to push ${count} commits."
echo "Please review the above diff, and if you still"
echo "want to push, run git push with --no-verify"
fail
fi
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment