#!/bin/sh
# ------------------------------------------------------------------------------
#
# Git pre-commit hook, installed by Git-MQ, from git-pre-commit.shar
#
# CAUTION: Do NOT modify or replace this pre-commit hook!  All Git-MQ commands
# will verify its integrity, and will abort if its hash signature has changed;
# any site-specific pre-commit hooks which may be desired should be relocated
# to the $GIT_DIR/hooks/pre-commit.dir subdirectory, whence this Git-MQ hook
# will invoke them.
#
# ------------------------------------------------------------------------------
#
# $Id$
#
# Written by Keith Marshall <keith@users.osdn.me>
# Copyright (C) 2019, Keith Marshall
#
#
# This file is part of the Git-MQ program suite.
#
# The Git-MQ program suite is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public Licence
# as published by the Free Software Foundation, either version 3 of
# the Licence, or (at your option) any later version.
#
# The Git-MQ program suite is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public Licence for more details.
#
# You should have received a copy of the GNU General Public Licence
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# ------------------------------------------------------------------------------
#
# Before considering any repository-specific pre-commit hook, we MUST ensure
# that no commit is permitted when any Git-MQ managed patch has been applied.
#
  mq_hook=`git qtop 2> /dev/null` && {
    echo >&2 "git-mq: patch series to '$mq_hook' has been applied"
    echo >&2 "git-mq: fatal: cannot commit over applied patches"
    exit 2
  }

# If, and only if, we get past the preceding Git-MQ specific check, then we
# may consider any repository-specific pre-commit which may have been set up
# in the $GIT_DIR/hooks/pre-commit.dir subdirectory; initially assume that
# any such hook will permit the commit to proceed, while still providing
# a status code capture mechanism for recission of this permission.
#
  mq_status_code=0
  mq_status_update() { test $1 -gt $mq_status_code && mq_status_code=$1; }

# We may now invoke an arbitrary sequence of pre-commit hooks, in normal
# directory sort order; note that this differs from normal git convention,
# which provides for only one pre-commit script to be run, in that this
# script is that one, and it may delegate to any number of subsidiaries,
# all of which will be invoked, (excluding any which lack the executable
# attribute, or are named with a ".sample" suffix); any one within the
# sequence is empowered to rescind permission to commit.
#
  for mq_hook in "$GIT_DIR/hooks/pre-commit.dir/"*
  do case "$mq_hook" in *.sample) ;; *)
       test -x "$mq_hook" && { "$mq_hook" || mq_status_update $?; } ;;
     esac
  done

# Only if all pre-commit hooks returned zero exit codes, thus leaving the
# captured exit status code unchanged from its initial default value, will
# permission to commit be ratified.
#
  exit $mq_status_code
#
# ------------------------------------------------------------------------------
# $RCSfile$: end of file
