summaryrefslogtreecommitdiffstats
path: root/git_hooks/pre-commit
blob: 3de975be16a949d30d5ea86b87a11e4dba78b663 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env sh
#
# Hook used to indent files before commiting
#


#
# Pre-conditions
#
if test -d ".git/rebase-merge"          || \
   test -d ".git/rebase-apply"          || \
   test -f ".git/MERGE_HEAD"            || \
   test -f ".git/CHERRY_PICK_HEAD"      || \
   test -f ".git/BISECT_LOG"
then
    exit 0
fi

#
# Configuration check
#
if test ! -x "$XMLINDENT"
then
    XMLINDENT="$(git config --get hooks.xmlindent)"
fi
if test ! -x "$XMLINDENT"
then
    echo "Unable to find xmlindent executable on the configuration."
    echo
    echo "Please configure it with :"
    echo "  git config --global hooks.xmlindent C:/path/to/xmlindent"
    echo " or "
    echo "  git config --global hooks.xmlindent /usr/bin/xmlindent"
    echo
fi

if test -z "$(git config --get-all xmlindent.ignored)"
then
    echo "Unable to find xmlindent ignored list on the configuration, ignored"
    echo
    echo "You can configure it with :"
    echo "  git config --add xmlindent.ignored 'scilab/Visual-Studio-settings/*.xml' "
    echo "  git config --add xmlindent.ignored 'scilab/checkstyle/*.xml' "
    echo

    XMLINDENT_IGNORED=""
else
    XMLINDENT_IGNORED="$(find $(git config --get-all xmlindent.ignored))"
fi

if test ! -x "$ASTYLE"
then
    ASTYLE="$(git config --get hooks.astyle)"
fi
if test ! -x "$ASTYLE"
then
    echo "Unable to find astyle executable on the configuration."
    echo
    echo "Please configure it with :"
    echo "  git config --global hooks.astyle C:/path/to/astyle"
    echo " or "
    echo "  git config --global hooks.astyle /usr/bin/astyle"
    echo
fi

if test -z "$(git config --get-all astyle.ignored)"
then
    echo "Unable to find astyle ignored list on the configuration, ignored"
    echo
    echo "You can configure it with :"
    echo "  git config --add astyle.ignored 'scilab/modules/*/src/jni/*.hxx' "
    echo "  git config --add astyle.ignored 'scilab/modules/*/src/jni/*.cpp' "
    echo "  git config --add astyle.ignored 'scilab/modules/*/src/jni/*.c' "
    echo "  git config --add astyle.ignored 'scilab/modules/javasci/src/java/org/scilab/modules/javasci/Call_Scilab*.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/renderer/src/java/org/scilab/modules/renderer/FigureScilabCall*.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/SynopsisLexer.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/XML/XMLLexer.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/c/CLexer.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/java/JavaLexer.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/helptools/src/java/org/scilab/modules/helptools/scilab/ScilabLexer.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/FunctionScanner.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/IndentScanner.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/MatchingBlockScanner.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/scinotes/src/java/org/scilab/modules/scinotes/ScilabLexer.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/Controller.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/JavaController.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/Kind.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/xcos/src/java/org/scilab/modules/xcos/ObjectProperties.java' "
    echo "  git config --add astyle.ignored 'scilab/modules/scicos/src/scicos_sundials/*' "

    echo

    ASTYLE_IGNORED=""
else
    ASTYLE_IGNORED="$(find $(git config --get-all astyle.ignored))"
fi

# indent / format file by type
#
indent() {
    # getting against as the current commit
    if git rev-parse --verify HEAD >/dev/null 2>&1
    then
        local against=HEAD
    else
        # Initial commit: diff against an empty tree object
        local against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
    fi

    # get the modified files per kind filtering out ignored files and call the
    # __indent_XXX helper

    FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.(xcos|xml|xsl)$" |grep -v -F "$XMLINDENT_IGNORED")
    [ -z "$FILES" ] || __indent_xml;

    FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.(h|c|hxx|cpp)$" |grep -v -F "$ASTYLE_IGNORED")
    [ -z "$FILES" ] || __indent_C;

    FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.java$" |grep -v -F "$ASTYLE_IGNORED")
    [ -z "$FILES" ] || __indent_java;

    FILES=$(git diff --cached --name-only --diff-filter=ACM $against |grep -E "\.(sce|sci|tst)$")
    [ -z "$FILES" ] || __indent_scilab;

    return 0;
}

# Indent the file with xmlindent if this is an xcos file
__indent_xml() {
    if test ! -x "$XMLINDENT"
    then
        return 1;
    fi

    echo "Formatting" "$FILES"
    "$XMLINDENT" -w -i 4 $FILES || return 2;
    # xmlindent does not remove trailing whitespaces
    # and might add some on empty lines
    sed -i -e 's/[ \t]*$//' $FILES || return 2;
    git add $FILES || return 3;
}

# Pre process before the indent
__pre_indent() {
    if test ! -x "$ASTYLE"
    then
        return 1;
    fi

    echo "Indenting" $FILES
    return 0;
}

# post process after the indent
__post_indent() {
    git add $FILES
}

COMMON_ASTYLE_ARGS="--pad-header --suffix=none --pad-oper --indent-col1-comments --indent-switches --indent=spaces=4 --add-brackets --formatted"

# Indent the file with `astyle' if this is a C/CPP file
__indent_C() {
    __pre_indent || return 1
    $ASTYLE $COMMON_ASTYLE_ARGS --style=bsd $FILES || return 2
    __post_indent || return 3
    return 0
}

# Indent the file with `astyle' if this is a Java file
__indent_java() {
    __pre_indent || return 1
    $ASTYLE $COMMON_ASTYLE_ARGS --style=java $FILES || return 2
    __post_indent || return 3
    return 0
}

# Indent the file with `scinotes' if this is a Scilab file
__indent_scilab() {
    __pre_indent || return 1

    TMPFILE="scinotes_indent.sce"
    echo "files = [" >$TMPFILE
    printf "'%s'\n" $FILES >>$TMPFILE
    echo "];" >>$TMPFILE

    echo "scinotes(files, ['indent','trailing','quote'])" >>$TMPFILE
    echo "exit(0)" >>$TMPFILE

    if test -f scilab/scilab-bin; then
        scilab/bin/scilab -nw -f $TMPFILE || return 2
    else
        if test -f scilab/bin/WScilex.exe; then
            scilab/bin/wscilex.exe -nw -f $TMPFILE || return 2
        else
            echo "Scilab has not been built."
            rm $TMPFILE
            return 4
        fi
    fi
    rm $TMPFILE

    __post_indent || return 3
    return 0
}

indent