Merge branch 'master' of git+ssh://pobry@repo.or.cz/srv/git/git-scripts
[git-scripts.git] / pre-commit
bloba34782d0bc330b0d2b980c11bb8a393606521db4
1 #!/bin/sh
3 GNATROOT=/opt/gnat/2007
5 PATH=$GNATROOT/bin:$HOME/bin:$PATH
7 res=0
9 REP=/tmp/_$$
11 while true; do
12 if [ -d .git ]; then
13 break;
15 cd ..
16 done
18 # Repository name
19 PWD=`pwd`
20 REPOSIT_NAME=`basename $PWD`
22 [ "$REPOSIT_NAME" == "" ] && echo No repository found && exit 1;
24 # Set Style_Checker options
26 OWEB="-H -cP -cY -l256"
28 # The copyright pattern to check
29 CPYR=""
31 # Pathnames matching the regexp will be excluded from the test
32 EXCLUDE=""
34 case "$REPOSIT_NAME" in
35 v2p) SC_OPTS="-ign out -ign tmplt -ign sed -ign txt \
36 -lang Ada -d -cp -cy -sp -gnat05 -lang TXML $OWEB -lang Script -H \
37 -lang XML $OWEB -lang HTML $OWEB -lang XSD $OWEB -lang CSS $OWEB"
39 *) SC_OPTS="-ign out -ign tmplt -ign sed -ign txt \
40 -lang Ada -cp -cy -sp -gnat05 -lang TXML $OWEB \
41 -lang XML $OWEB -lang HTML $OWEB -lang XSD $OWEB -lang CSS $OWEB"
43 esac
45 # Source .git-pre-commit hook for user's defined rules
47 [ -f $HOME/.git-pre-commit ] && source $HOME/.git-pre-commit $REPOSIT_NAME
49 # Process each file
51 function check() {
52 id=$4
53 file=`basename $6`
54 mode=$(echo $5 | cut -c1)
56 # Check if it is a file to ignore
58 if [[ -n "$EXCLUDE" && `echo "$6" | grep --regexp="$EXCLUDE"` ]]; then
59 return 0;
62 # skip deleted/renamed files
64 if [[ "$mode" = "D" || "$mode" = "R" ]]; then
65 echo $6 >> $REP/.files.removed
66 return 0;
69 git show $id > $REP/$file
71 # If the MANIFEST keep it around
73 if [[ "$6" = "$MANIFEST" ]]; then
74 cp $REP/$file $REP/.manifest
77 # If a new file, record it
79 if [[ "$mode" == "A" ]]; then
80 echo $6 >> $REP/.files.added
83 ( cd $REP; style_checker $SC_OPTS -n "$6" "$file"; )
84 res=$(($res + $?))
85 rm -f $REP/$file
88 mkdir $REP
90 # Get files to be commited
91 # <N1> <N2> <Id-OLD> <Id-NEW> <status> <Filename>
92 git diff-index -M --cached HEAD > $REP/.files
94 while read list
96 check $list
97 done < $REP/.files
99 # Check for MANIFEST if needed
101 if [[ -n "$MANIFEST" && -f $REP/.files.added ]]; then
102 if [[ ! -f $REP/.manifest ]]; then
103 echo ""
104 echo Files added, but $MANIFEST not updated, consider adding:
105 git diff --cached --name-only --diff-filter=A
106 res=1
107 else
108 while read fadded
110 grep --quiet $fadded $REP/.manifest
111 if [[ $? = 1 ]]; then
112 echo File $fadded added but missing in $MANIFEST
113 res=1
115 done < $REP/.files.added
119 if [[ -n "$MANIFEST" && -f $REP/.files.removed ]]; then
120 if [[ ! -f $REP/.manifest ]]; then
121 echo ""
122 echo Files removed, but $MANIFEST not updated, condider removing:
123 git diff --cached --name-only --diff-filter=D
124 res=1
125 else
126 while read fremoved
128 grep --quiet $fremoved $REP/.manifest
129 if [[ $? = 0 ]]; then
130 echo File $fremoved removed but still in $MANIFEST
131 res=1
133 done < $REP/.files.removed
137 rm -fr $REP
139 exit $res