Michal Marek 9bee79
#!/bin/bash
Michal Marek 9bee79
Michal Marek 0a417c
#############################################################################
Michal Marek 0a417c
# Copyright (c) 2008,2009 Novell, Inc.
Michal Marek 0a417c
# All Rights Reserved.
Michal Marek 0a417c
#
Michal Marek 0a417c
# This program is free software; you can redistribute it and/or
Michal Marek 0a417c
# modify it under the terms of version 2 of the GNU General Public License as
Michal Marek 0a417c
# published by the Free Software Foundation.
Michal Marek 0a417c
#
Michal Marek 0a417c
# This program is distributed in the hope that it will be useful,
Michal Marek 0a417c
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Michal Marek 0a417c
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.   See the
Michal Marek 0a417c
# GNU General Public License for more details.
Michal Marek 0a417c
#
Michal Marek 0a417c
# You should have received a copy of the GNU General Public License
Michal Marek 0a417c
# along with this program; if not, contact Novell, Inc.
Michal Marek 0a417c
#
Michal Marek 0a417c
# To contact Novell about this file by physical or electronic mail,
Michal Marek 0a417c
# you may find current contact information at www.novell.com
Michal Marek 0a417c
#############################################################################
Michal Marek 9bee79
# add scripts/git-hooks/pre-commit-test
Michal Marek 9bee79
Michal Marek d52b9f
cdup=$(git rev-parse --show-cdup) || exit
Michal Marek 9bee79
cd "${cdup:-./}" || exit
Michal Marek d52b9f
GIT_DIR=$(git rev-parse --git-dir) || exit
Michal Marek 9bee79
Michal Marek 9bee79
check_snippet()
Michal Marek 9bee79
{
Michal Marek 9bee79
    # do not grep for the exact line so that those who really don't like
Michal Marek 9bee79
    # the pre-commit check can add '# kernel-source-pre-commit' to silence
Michal Marek 9bee79
    # the warnings
Michal Marek 9bee79
    grep -qs kernel-source-pre-commit "$GIT_DIR"/hooks/pre-commit
Michal Marek 9bee79
}
Michal Marek 9bee79
Michal Marek 9bee79
install_snippet()
Michal Marek 9bee79
{
Michal Koutný 2905fb
    local hookdir="$GIT_DIR"/hooks
Michal Koutný 2905fb
    local hook="$hookdir"/pre-commit
Michal Marek 9bee79
    local snippet='
Michal Marek 9bee79
# runs kernel-source-patch-check for each added or modified patch
Michal Marek 9bee79
. "$(dirname "$0")"/kernel-source-pre-commit'
Michal Marek 9bee79
    # fix up old clones that had '$GIT_DIR/hooks' here which doesn't work
Michal Marek 9bee79
    # with older gits
Michal Marek 9bee79
    if grep -qs '"$GIT_DIR"/hooks/' "$hook"; then
Michal Marek 9bee79
        sed -i 's:"\$GIT_DIR"/hooks/:"$(dirname "$0")"/:' "$hook"
Michal Marek 9bee79
    fi
Michal Marek 9bee79
    if check_snippet; then
Michal Marek 9bee79
        return
Michal Marek 9bee79
    fi
Michal Koutný 2905fb
    mkdir -p "$hookdir"
Michal Marek 9bee79
    if ! test -x "$hook"; then
Michal Marek 9bee79
        # if the hook was not enabled before, don't run the example code that
Michal Marek 9bee79
        # is usually there
Michal Marek 9bee79
        snippet="$snippet"$'\n'"exit 0"
Michal Marek 9bee79
    fi
Michal Marek 9bee79
    if test -e "$hook"; then
Michal Marek 9bee79
        snippet="$snippet" perl -pi'.orig' -e '
Michal Marek 9bee79
           if (/^[^#]/ && !$inserted) {
Michal Marek 9bee79
               print $ENV{"snippet"} . "\n";
Michal Marek 9bee79
               $inserted = 1;
Michal Marek 9bee79
           }' "$hook"
Michal Marek 9bee79
    else
Nikolay Borisov 7c8b43
        (echo '#!/bin/bash'
Michal Marek 9bee79
        echo
Michal Marek 9bee79
        echo "$snippet") >"$hook"
Michal Marek 9bee79
    fi
Michal Marek 9bee79
}
Michal Marek 9bee79
Michal Marek 9bee79
is_eq()
Michal Marek 9bee79
{
Michal Marek 9bee79
    test "$1" -ef "$2" || cmp -s "$1" "$2"
Michal Marek 9bee79
}
Michal Marek 9bee79
Benjamin Poirier 668a35
clean_attributes()
Benjamin Poirier 668a35
{
Benjamin Poirier 668a35
    # Previous versions of this file wrote attributes meant to be distributed
Benjamin Poirier 668a35
    # with the repository into $GIT_DIR/info/attributes rather than
Benjamin Poirier 668a35
    # .gitattributes. In order to avoid the former potentially overriding the
Benjamin Poirier 668a35
    # latter, remove them.
Benjamin Poirier 668a35
    # If you intentionally want to override these attributes with these values,
Benjamin Poirier 668a35
    # make sure that the line in your "attributes" file differs, for example,
Benjamin Poirier 668a35
    # by adding some leading or trailing whitespace to the line.
Benjamin Poirier 668a35
    if [ -e "$GIT_DIR"/info/attributes ]; then
Benjamin Poirier 668a35
        sed -i -e '
Benjamin Poirier 668a35
            /^\*\.changes merge=rpm-changes$/ d
Benjamin Poirier 668a35
            /^symsets-\*\.tar\.gz diff=symsets$/ d
Benjamin Poirier 668a35
        ' "$GIT_DIR"/info/attributes
Benjamin Poirier 668a35
    fi
Benjamin Poirier 668a35
}
Benjamin Poirier 668a35
Benjamin Poirier c8faf9
clean_config()
Benjamin Poirier c8faf9
{
Benjamin Poirier c8faf9
    # Previous versions of this file set configuration values meant to be
Benjamin Poirier c8faf9
    # distributed with the repository into the repository-specific
Benjamin Poirier c8faf9
    # configuration file (.git/config). Currently, configuration values are set
Benjamin Poirier c8faf9
    # in a versioned file (extra-gitconfig) which is included from the
Benjamin Poirier c8faf9
    # repository-specific config. Check if the old scheme is in place and
Benjamin Poirier c8faf9
    # update it.
Benjamin Poirier c8faf9
    if ! git config --local --get "include.path" >/dev/null || \
Benjamin Poirier c8faf9
        ! git config --local --get-all "include.path" | \
Benjamin Poirier c8faf9
        grep -q "../extra-gitconfig"; then
Benjamin Poirier c8faf9
        if git config --local --get "merge.rpm-changes.name" >/dev/null; then
Jean Delvare 19cc66
            git config --local --remove-section "merge.rpm-changes"
Benjamin Poirier c8faf9
        fi
Benjamin Poirier c8faf9
        if git config --local --get "diff.symsets.textconv" >/dev/null; then
Jean Delvare 19cc66
            git config --local --remove-section "diff.symsets"
Benjamin Poirier c8faf9
        fi
Benjamin Poirier c8faf9
        git config --add "include.path" "../extra-gitconfig"
Benjamin Poirier c8faf9
    fi
Benjamin Poirier c8faf9
}
Benjamin Poirier c8faf9
Michal Marek 9bee79
check_scripts()
Michal Marek 9bee79
{
Benjamin Poirier 668a35
    clean_attributes
Benjamin Poirier c8faf9
    clean_config
Benjamin Poirier 668a35
Michal Marek 9bee79
    test -x "$GIT_DIR"/hooks/pre-commit && \
Michal Marek 5073e1
    is_eq "$GIT_DIR"/hooks/kernel-source-pre-commit scripts/git-pre-commit
Michal Marek 9bee79
}
Michal Marek 9bee79
Michal Marek 9bee79
# try to create a relative symlink if possible
Michal Marek 9bee79
relative_scripts_dir()
Michal Marek 9bee79
{
Michal Marek 9bee79
    local toplevel=$PWD
Michal Marek d52b9f
    if test "$toplevel/.git" -ef "$GIT_DIR"; then
Michal Marek d52b9f
        echo "../../scripts"
Michal Marek 9bee79
    else
Michal Marek 9bee79
        echo "$toplevel/scripts"
Michal Marek 9bee79
    fi
Michal Marek 9bee79
}
Michal Marek 9bee79
Michal Marek 9bee79
install_scripts()
Michal Marek 9bee79
{
Michal Marek 9bee79
    chmod +x "$GIT_DIR"/hooks/pre-commit
Michal Marek 9bee79
    dir=$(relative_scripts_dir)
Michal Marek 9bee79
    ln -sf "$dir"/git-pre-commit "$GIT_DIR"/hooks/kernel-source-pre-commit
Michal Marek 9bee79
}
Michal Marek 9bee79
Michal Marek 9bee79
case "$1" in
Michal Marek 9bee79
--check)
Michal Marek 9bee79
    check_snippet && check_scripts
Michal Marek 9bee79
    exit
Michal Marek 9bee79
    ;;
Michal Marek 9bee79
"")
Michal Marek 9bee79
    echo "Installing git commit hooks."
Michal Marek 9bee79
    install_snippet
Michal Marek 9bee79
    install_scripts
Benjamin Poirier c8faf9
    echo "Installing git config include directive."
Benjamin Poirier c8faf9
    clean_config
Michal Marek 9bee79
    ;;
Michal Marek 9bee79
*)
Michal Marek 9bee79
    echo "Usage: $0 [--check]" >&2
Michal Marek 9bee79
    exit 1
Michal Marek 9bee79
    ;;
Michal Marek 9bee79
esac
Michal Marek 9bee79
Michal Marek 9bee79
# vim: sw=4 : et