On 01/20/2015 03:56 PM, Assaf Gordon wrote:
<...>
Here's my attempt at a git update hook which conditionally allows 
non-fast-forward commits on branches (and even 'master', based on 
configuration).
<...>

I was alerted off-list that I haven't supplied a license to the previously 
attached script.

The script is modified from the template git hook scripts which are created 
every time 'git init' is run.
Without any additional information, I have to assume it is licensed the same as 
git itself: GPLv2.
My modifications are under the same license.
If anyone has more concrete information, let me know, and I'll send an updated 
script.

It would be relatively easy to simply re-write the script from scratch, thus 
avoiding this issue.

- Assaf

#!/bin/sh

## The original file is assumed to be copyrighed by Junio C Hamano 
<[email protected]>
## https://github.com/git/git/commit/8d5afef0f9b3a252f7b90406d35c295dc698e26d
## and licensed the same as 'git' itself: GPLv2 (not later)
##
## The modified script is based on the following revision:
## https://github.com/git/git/blob/v1.9.1/templates/hooks--update.sample
##
## Additional modifications are
##   Copyright (C) 2015 Assaf Gordon
## Licensed under the same license as 'hooks--update.sample'

#
# --- Command line
refname="$1"
oldrev="$2"
newrev="$3"

# --- Safety check
if [ -z "$GIT_DIR" ]; then
    echo "Don't run this script from the command line." >&2
    echo " (if you want, you could supply GIT_DIR then run" >&2
    echo "  $0 <ref> <oldrev> <newrev>)" >&2
    exit 1
fi

if [ -z "$refname" -o -z "$oldrev" -o -z "$newrev" ]; then
    echo "usage: $0 <ref> <oldrev> <newrev>" >&2
    exit 1
fi

# Get the full path of the repository
repository=$(readlink -f "$GIT_DIR")

##
## Custom Configuration Options
##

## Allow non-fast-forward commits on branches (except 'master')
## To enable:
##   git config hooks.allowbranchnonfastforward true
allow_branch_nonfastforward=$(git config --bool hooks.allowbranchnonfastforward)

## Allow non-fast-forward commits on the master branch
## This should be 'false' by default, and only enabled under special
## circumstances.
## To enable:
##  git config hooks.allowmasternonfastforward true
allow_master_nonfastforward=$(git config --bool hooks.allowmasternonfastforward)

## When a master-branch is updated with non-fast-forward commit,
## notify this email address (e.g [email protected])
[email protected]


# --- Check types
# if $newrev is 0000...0000, it's a commit to delete a ref.
zero="0000000000000000000000000000000000000000"
if [ "$newrev" = "$zero" ]; then
    newrev_type=delete
else
    newrev_type=$(git cat-file -t $newrev)
fi

case "$refname","$newrev_type" in
    refs/tags/*,commit)
        # un-annotated tag
        ;;
    refs/tags/*,delete)
        # delete tag
        ;;
    refs/tags/*,tag)
        # annotated tag
        ;;
    refs/heads/*,commit)
        # commit to a branch
        #  'revlist' should be empty on a fast-forward commit
        revlist=$(git rev-list "$newrev".."$oldrev")
        branch=${refname##refs/heads/}

        # Non-fast-forard commit?
        if test -n "$revlist" ; then

            # non-fast-forward to the master branch?
            if test "x$branch" = "xmaster" ; then

                if test "x$allow_master_nonfastforward" != "xtrue" ; then
                    echo \
"*** Rejected: non-fast-forward on the master branch are disabled in Savannah.
    See http://savannah.gnu.org/maintenance/XXXXXXX for details."
                    exit 1
                fi

                # A non-fast-forward commit is allowed for the master
                # branch of this repository.
                # To regulate funkiness, send an email notification.
                echo "This is an automated message.

The master branch of '$repository' is being modified with a
non-fast-forward commit.
time: $(date -uR)
old-revision: $oldrev
new-revision: $newrev" \
                    | mail -s 'Master branch update: $repository' \
                            "$master_update_notification"
            else
                # non-fast-foward to a non-master branch?

                if test "x$allow_branch_nonfastforward" != "xtrue" ; then
                    echo \
"*** Rejected: non-fast-forward commits on non-master branches are disabled
    for this repository.
    See http://savannah.gnu.org/maintenance/XXXXXXX for details."
                    exit 1
                fi
            fi
        fi
        ;;
    refs/heads/*,delete)
        # delete branch
        ;;
    refs/remotes/*,commit)
        # tracking branch
        ;;
    refs/remotes/*,delete)
        # delete tracking branch
        ;;
    *)
        # Anything else (is there anything else?)
        echo "*** Update hook: unknown type of update to ref $refname of type 
$newrev_type" >&2
        exit 1
        ;;
esac

# --- Finished
exit 0

Reply via email to