#!/bin/bash
# Copyright 2018 Nathan Sommer and Ben Coleman
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


# This hook prevents students from pushing to a branch other than the one
# existing branch.
#
# If this script exits with a 0 exit code the push is accepted, otherwise the
# push is rejected.

# get the ref and name of the existing branch
allowed_ref=$(git symbolic-ref HEAD)
allowed_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')

# this hook receives push information about each ref that is pushed in the
# form <old hash> <new hash> <ref name>
while read old_hash new_hash ref_name
do
    if [ $ref_name != $allowed_ref ]
    then
        echo "ERROR: You may only push to the $allowed_branch branch"
        exit 1
    fi
done

exit 0
