Home Home > GIT Browse > SLE12-SP5
summaryrefslogtreecommitdiff
blob: 0c129b09ee2888cb53f5a3179769ef41d5e286a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/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"
}

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