Add support for submodules.
[git-scripts.git] / pre-commit
blobc0efacdb17054225fe8e699697a910decd1f0ab0
1 #!/bin/bash
3 GNATROOT=/opt/gnat/2015
5 PATH=$GNATROOT/bin:$HOME/bin:$PATH
7 res=0
9 REP=/tmp/_$$
11 while true; do
12 # Either we are in a Git repository or in a submodule. Note that only
13 # submodules into the root directory are supported.
14 if [ -d .git -o -d ../.git/modules/$(basename $PWD) ]; then
15 break;
17 cd ..
18 done
20 # Repository name
21 PWD=`pwd`
22 REPOSIT_NAME=`basename $PWD`
24 [ "$REPOSIT_NAME" == "" ] && echo No repository found && exit 1;
26 # Set Style_Checker options
28 OWEB="-H -cP -cY -l256"
30 # The copyright pattern to check
31 CPYR=""
33 # Pathnames matching the regexp will be excluded from the test
34 EXCLUDE=""
36 case "$REPOSIT_NAME" in
37 v2p) SC_OPTS="-ign out -ign tmplt -ign sed -ign txt \
38 -lang Ada -d -cp -cy -sp -gnat05 -lang TXML $OWEB -lang Script -H \
39 -lang XML $OWEB -lang HTML $OWEB -lang XSD $OWEB -lang CSS $OWEB"
40 PSTATUS=true
42 *) SC_OPTS="-ign out -ign tmplt -ign sed -ign txt \
43 -lang Ada -H -cP -cY -sp -gnat05 -lang TXML $OWEB \
44 -lang XML $OWEB -lang HTML $OWEB -lang XSD $OWEB -lang CSS $OWEB"
46 esac
48 # Source .git-pre-commit hook for user's defined rules
50 [ -f $HOME/.git-pre-commit ] && source $HOME/.git-pre-commit $REPOSIT_NAME
52 [ -z "$PSTATUS" ] && \
53 echo "not project status, add entry for $REPOSIT_NAME in ~/.git-pre-commit" && exit 1
55 [ "$PSTATUS" == "false" ] && exit 0
57 # Process each file
59 function check() {
60 oid=$3
61 id=$4
62 mode=$(echo $5 | cut -c1)
64 ofile=$6
66 if [[ "$mode" = "R" ]]; then
67 nfile="$7"
68 else
69 nfile="$6"
72 # Check if a sub-module, in this case nothing to do
74 if [[ ! $2 < 160000 ]]; then
75 return 0;
78 bfile=$(basename $nfile)
80 # Check if it is a file to ignore
82 if [[ -n "$EXCLUDE" && `echo "$nfile" | grep --regexp="$EXCLUDE"` ]]; then
83 return 0;
86 # Skip deleted files as no check to be done
88 if [[ "$mode" = "D" ]]; then
89 echo $ofile >> $REP/.files.removed
90 return 0;
93 # A renamed file is removed and added
95 if [[ "$mode" = "R" ]]; then
96 echo $ofile >> $REP/.files.removed
97 echo $nfile >> $REP/.files.added
100 git show $id > $REP/$bfile
102 # If the MANIFEST keep it around
104 if [[ "$nfile" = "$MANIFEST" ]]; then
105 cp $REP/$bfile $REP/.manifest
108 # If a new file, record it
110 if [[ "$mode" == "A" ]]; then
111 echo $nfile >> $REP/.files.added
114 # For Ada file, check change status, we normalize the old and new version
115 # of the unit and check the diff. No diff means that the changes are only
116 # reformatting and/or style fixes.
118 if [[ ! -z $(which normalize) && "$mode" != "A" ]]; then
119 file_ext=${bfile##*.}
120 if [[ "$file_ext" == "adb" || "$file_ext" == "ads" ]]; then
121 git show $oid > $REP/ofile
122 normalize $REP/ofile > $REP/n_ofile
123 normalize $REP/$bfile > $REP/n_nfile
124 diff -q $REP/n_ofile $REP/n_nfile > /dev/null
125 if [ $? = 0 ]; then
126 echo "$nfile: has no semantic changes (style changes only)"
131 # style checker
133 ( cd $REP; style_checker $SC_OPTS -n "$nfile" "$bfile"; )
134 res=$(($res + $?))
135 rm -f $REP/$bfile
138 if [[ -z $COMMIT_EMAIL ]]; then
139 COMMIT_EMAIL=$(git log -1 --reverse --format="%ce")
142 CURRENT_EMAIL=$(git config user.email)
144 [[ "$CURRENT_EMAIL" != "$COMMIT_EMAIL" ]] && \
145 echo "wrong e-mail: expected $COMMIT_EMAIL found $CURRENT_EMAIL" && exit 1
147 mkdir $REP
149 # Get files to be commited
150 # <N1> <N2> <Id-OLD> <Id-NEW> <status> <Filename>
151 git diff-index -M --cached HEAD > $REP/.files
153 while read list
155 check $list
156 done < $REP/.files
158 # Check for MANIFEST if needed
160 if [[ -n "$MANIFEST" && -f $REP/.files.added ]]; then
161 if [[ ! -f $REP/.manifest ]]; then
162 echo ""
163 echo Files added, but $MANIFEST not updated, consider adding:
164 git diff --cached --name-only --diff-filter=A
165 res=1
166 else
167 while read fadded
169 grep --quiet ^$fadded\$ $REP/.manifest
170 if [[ $? = 1 ]]; then
171 echo File $fadded added but missing in $MANIFEST
172 res=1
174 done < $REP/.files.added
178 if [[ -n "$MANIFEST" && -f $REP/.files.removed ]]; then
179 if [[ ! -f $REP/.manifest ]]; then
180 echo ""
181 echo Files removed, but $MANIFEST not updated, condider removing:
182 git diff --cached --name-only --diff-filter=D
183 res=1
184 else
185 while read fremoved
187 grep --quiet ^$fremoved\$ $REP/.manifest
188 if [[ $? = 0 ]]; then
189 echo File $fremoved removed but still in $MANIFEST
190 res=1
192 done < $REP/.files.removed
196 rm -fr $REP
198 exit $res