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 hookdir="$GIT_DIR"/hooks
    local hook="$hookdir"/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
    mkdir -p "$hookdir"
    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/bash'
        echo
        echo "$snippet") >"$hook"
    fi
}

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

clean_attributes()
{
    # Previous versions of this file wrote attributes meant to be distributed
    # with the repository into $GIT_DIR/info/attributes rather than
    # .gitattributes. In order to avoid the former potentially overriding the
    # latter, remove them.
    # If you intentionally want to override these attributes with these values,
    # make sure that the line in your "attributes" file differs, for example,
    # by adding some leading or trailing whitespace to the line.
    if [ -e "$GIT_DIR"/info/attributes ]; then
        sed -i -e '
            /^\*\.changes merge=rpm-changes$/ d
            /^symsets-\*\.tar\.gz diff=symsets$/ d
        ' "$GIT_DIR"/info/attributes
    fi
}

clean_config()
{
    # Previous versions of this file set configuration values meant to be
    # distributed with the repository into the repository-specific
    # configuration file (.git/config). Currently, configuration values are set
    # in a versioned file (extra-gitconfig) which is included from the
    # repository-specific config. Check if the old scheme is in place and
    # update it.
    if ! git config --local --get "include.path" >/dev/null || \
        ! git config --local --get-all "include.path" | \
        grep -q "../extra-gitconfig"; then
        if git config --local --get "merge.rpm-changes.name" >/dev/null; then
            git config --local --remove-section "merge.rpm-changes"
        fi
        if git config --local --get "diff.symsets.textconv" >/dev/null; then
            git config --local --remove-section "diff.symsets"
        fi
        git config --add "include.path" "../extra-gitconfig"
    fi
}

check_scripts()
{
    clean_attributes
    clean_config

    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
}

case "$1" in
--check)
    check_snippet && check_scripts
    exit
    ;;
"")
    echo "Installing git commit hooks."
    install_snippet
    install_scripts
    echo "Installing git config include directive."
    clean_config
    ;;
*)
    echo "Usage: $0 [--check]" >&2
    exit 1
    ;;
esac

# vim: sw=4 : et