Debian Codegraph @

git-pre-commit

raw
1 #!/bin/sh
2 #
3 # hook script to verify what is about to be committed.
4 # Called by git-commit with no arguments. The hook should
5 # exit with non-zero status after issuing an appropriate message if
6 # it wants to stop the commit.
7 #
8 # To enable this hook, copy it to .git/hooks/pre-commit
9
10 if git-rev-parse --verify HEAD >/dev/null 2>&1
11 then
12 against=HEAD
13 else
14 # Initial commit: diff against an empty tree object
15 against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
16 fi
17
18 # If you want to allow non-ascii filenames set this variable to true.
19 allownonascii=$(git config hooks.allownonascii)
20
21 # Cross platform projects tend to avoid non-ascii filenames; prevent
22 # them from being added to the repository. We exploit the fact that the
23 # printable range starts at the space character and ends with tilde.
24 if [ "$allownonascii" != "true" ] &&
25 # Note that the use of brackets around a tr range is ok here, (it's
26 # even required, for portability to Solaris 10's /usr/bin/tr), since
27 # the square bracket bytes happen to fall in the designated range.
28 test "$(git diff --cached --name-only --diff-filter=A -z $against |
29 LC_ALL=C tr -d '[ -~]\0')"
30 then
31 echo "Error: Attempt to add a non-ascii file name."
32 echo
33 echo "This can cause problems if you want to work"
34 echo "with people on other platforms."
35 echo
36 echo "To be portable it is advisable to rename the file ..."
37 echo
38 echo "If you know what you are doing you can disable this"
39 echo "check using:"
40 echo
41 echo " git config hooks.allownonascii true"
42 echo
43 exit 1
44 fi
45
46 for file in `git diff --cached --name-only`
47 do
48 test -r $file || continue
49 test $file = src/qt4reactor.py && continue
50 if test `expr $file : '.*.py$'` -ne 0
51 then
52 cmakefile=$file
53 if test $cmakefile != src/scoringtest.py -a $cmakefile != src/kajonggtest.py -a $cmakefile != src/setup.py -a $cmakefile != src/winprep.py
54 then
55 if ! grep -w $cmakefile CMakeLists.txt >/dev/null
56 then
57 echo $cmakefile missing in CMakeLists.txt
58 exit 1
59 fi
60 fi
61 if grep -ne '[[:alnum:]]?* *[[:alnum:]]' $file
62 then
63 echo $file has double spaces
64 exit 1
65 fi
66 fi
67 test `expr $file : '.*.svgz'` -ne 0 && continue
68 test `expr $file : '.*.png'` -ne 0 && continue
69 test `expr $file : '.*.ogg'` -ne 0 && continue
70 set - `wc $file`
71 bytes=`expr $3 - 1`
72 set - `dd if=$file bs=1 skip=$bytes count=1 2>/dev/null | od -a`
73 if [ $2 != nl ]
74 then
75 echo "$file: The last line does not end with LF"
76 exit 1
77 fi
78 done
79
80 # checks for trailing whitespace and space before tab:
81 git diff-index --check --cached $against --
82 result=$?
83 if [ $result -ne 0 ]
84 then
85 exit $result
86 fi
87
88 # we want to do the content tests only against cached changes
89 # to be committed, ignoring other changes which are not yet on
90 # the index.
91 # git stash --keep-index is nice but then my working directory
92 # would change twice - before and after committing. And I would
93 # need a wrapper around git commit which could be circumvented.
94 # I prefer something more atomic.
95
96 checkfiles=$(git diff --cached --name-only | grep src/ | fgrep .py | sed 's%src/%%g')
97 if test x"$checkfiles" = x
98 then
99 echo "no changes in source found"
100 exit 0
101 fi
102
103 MIRROR=$(mktemp -dt kajongg-pre-commit.XXXXX)
104 trap "rm -rf $MIRROR" 0
105
106 git checkout-index --prefix=$MIRROR/ -af
107 cd $MIRROR/src
108 ln -s /home/wr/src/kajongg/src/twisted twisted
109
110 if test x$skip_pylint = x
111 then
112 for file in $checkfiles
113 do
114 test $file = qt4reactor.py && continue
115 echo -n pylint3 for $file in `pwd`...
116 pylint3 $file
117 result=$?
118 echo done
119 [ $result -ne 0 ] && break
120 done
121 if [ $result -ne 0 ]
122 then
123 echo "trying pylint over all source files"
124 pylint3 --reports=n *y
125 result=$?
126 fi
127 if [ $result -ne 0 ]
128 then
129 echo "pylint must be quiet"
130 exit $result
131 fi
132 fi
133 ./scoringtest.py
134 ./kajongg.py --demo --rounds=1
135
136 result=$?
137 if [ $result -ne 0 ]
138 then
139 echo "scoringtest.py failed"
140 exit $result
141 fi
142
143 exit 0