Blob Blame History Raw
#!/bin/bash

#############################################################################
# Copyright (c) 2008,2009 Novell, Inc.
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# 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, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail,
# you may find current contact information at www.novell.com
#############################################################################
# add scripts/git-hooks/pre-commit-test

cdup=$(git rev-parse --show-cdup) || exit
cd "${cdup:-./}" || exit
GIT_DIR=$(git rev-parse --git-dir) || exit

check_snippet()
{
    # do not grep for the exact line so that those who really don't like
    # the pre-commit check can add '# kernel-source-pre-commit' to silence
    # the warnings
    grep -qs kernel-source-pre-commit "$GIT_DIR"/hooks/pre-commit
}

install_snippet()
{
    local hook="$GIT_DIR"/hooks/pre-commit
    local snippet='
# runs kernel-source-patch-check for each added or modified patch
. "$(dirname "$0")"/kernel-source-pre-commit'
    # fix up old clones that had '$GIT_DIR/hooks' here which doesn't work
    # with older gits
    if grep -qs '"$GIT_DIR"/hooks/' "$hook"; then
        sed -i 's:"\$GIT_DIR"/hooks/:"$(dirname "$0")"/:' "$hook"
    fi
    if check_snippet; then
        return
    fi
    if ! test -x "$hook"; then
        # if the hook was not enabled before, don't run the example code that
        # is usually there
        snippet="$snippet"$'\n'"exit 0"
    fi
    if test -e "$hook"; then
        snippet="$snippet" perl -pi'.orig' -e '
           if (/^[^#]/ && !$inserted) {
               print $ENV{"snippet"} . "\n";
               $inserted = 1;
           }' "$hook"
    else
        (echo '#!/bin/sh'
        echo
        echo "$snippet") >"$hook"
    fi
}

is_eq()
{
    test "$1" -ef "$2" || cmp -s "$1" "$2"
}

check_scripts()
{
    test -x "$GIT_DIR"/hooks/pre-commit && \
    is_eq "$GIT_DIR"/hooks/kernel-source-pre-commit scripts/git-pre-commit
}

# try to create a relative symlink if possible
relative_scripts_dir()
{
    local toplevel=$PWD
    if test "$toplevel/.git" -ef "$GIT_DIR"; then
        echo "../../scripts"
    else
        echo "$toplevel/scripts"
    fi
}

install_scripts()
{
    chmod +x "$GIT_DIR"/hooks/pre-commit
    dir=$(relative_scripts_dir)
    ln -sf "$dir"/git-pre-commit "$GIT_DIR"/hooks/kernel-source-pre-commit
}

set_attribute()
{
    local patten=$1 attribute=$2

    if ! grep -qs "$attribute" "$GIT_DIR"/info/attributes; then
        mkdir -p "$GIT_DIR"/info
        echo "$patten $attribute" >>"$GIT_DIR"/info/attributes
    fi

}

install_changes_merger()
{
    git config merge.rpm-changes.name "*.changes merge driver"
    git config merge.rpm-changes.driver "scripts/rpm-changes-merge.pl %A %O %B"
    set_attribute '*.changes' 'merge=rpm-changes'
}

install_symsets_textconv()
{
    git config diff.symsets.textconv scripts/list-symsets
    set_attribute 'symsets-*.tar.gz' 'diff=symsets'
}
    

case "$1" in
--check)
    check_snippet && check_scripts
    exit
    ;;
"")
    echo "Installing git commit hooks."
    install_snippet
    install_scripts
    echo "Installing kernel-source.changes merge driver."
    install_changes_merger
    echo "Installing symsets diff viewer."
    install_symsets_textconv
    ;;
*)
    echo "Usage: $0 [--check]" >&2
    exit 1
    ;;
esac

# vim: sw=4 : et