Merge pull request #1246 from rouault/write_plt
[openjpeg.git] / scripts / prepare-commit.sh
1 #!/usr/bin/env bash
2 ###########################################################################
3 #    prepare-commit.sh
4 #    ---------------------
5 #    Date                 : August 2008
6 #    Copyright            : (C) 2008 by Juergen E. Fischer
7 #    Email                : jef at norbit dot de
8 ###########################################################################
9 #                                                                         #
10 #   This program is free software; you can redistribute it and/or modify  #
11 #   it under the terms of the GNU General Public License as published by  #
12 #   the Free Software Foundation; either version 2 of the License, or     #
13 #   (at your option) any later version.                                   #
14 #                                                                         #
15 ###########################################################################
16
17 TOPLEVEL=$(git rev-parse --show-toplevel)
18
19 PATH=$TOPLEVEL/scripts:$PATH
20
21 cd $TOPLEVEL
22
23 # GNU prefix command for mac os support (gsed, gsplit)
24 GP=
25 if [[ "$OSTYPE" =~ darwin* ]]; then
26   GP=g
27 fi
28
29 if ! type -p astyle.sh >/dev/null; then
30   echo astyle.sh not found
31   exit 1
32 fi
33
34 if ! type -p colordiff >/dev/null; then
35   colordiff()
36   {
37     cat "$@"
38   }
39 fi
40
41 if [ "$1" = "-c" ]; then
42   echo "Cleaning..."
43   remove_temporary_files.sh
44 fi
45
46 set -e
47
48 # determine changed files
49 MODIFIED=$(git status --porcelain| ${GP}sed -ne "s/^ *[MA]  *//p" | sort -u)
50 #MODIFIED=$(find src -name "*.h")
51
52 if [ -z "$MODIFIED" ]; then
53   echo nothing was modified
54   exit 0
55 fi
56
57 # save original changes
58 REV=$(git log -n1 --pretty=%H)
59 git diff >sha-$REV.diff
60
61 ASTYLEDIFF=astyle.$REV.diff
62 >$ASTYLEDIFF
63
64 # reformat
65 i=0
66 N=$(echo $MODIFIED | wc -w)
67 for f in $MODIFIED; do
68   (( i++ )) || true
69
70   case "$f" in
71   thirdparty/*)
72     echo $f skipped
73     continue
74     ;;
75
76   *.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py)
77     ;;
78
79   *)
80     continue
81     ;;
82   esac
83
84   m=$f.$REV.prepare
85
86   cp $f $m
87   ASTYLEPROGRESS=" [$i/$N]" astyle.sh $f
88   if diff -u $m $f >>$ASTYLEDIFF; then
89     # no difference found
90     rm $m
91   fi
92 done
93
94 if [ -s "$ASTYLEDIFF" ]; then
95   if tty -s; then
96     # review astyle changes
97     colordiff <$ASTYLEDIFF | less -r
98   else
99     echo "Files changed (see $ASTYLEDIFF)"
100   fi
101   exit 1
102 else
103   rm $ASTYLEDIFF
104 fi
105
106
107 # If there are whitespace errors, print the offending file names and fail.
108 exec git diff-index --check --cached HEAD --
109
110 exit 0
111
112 # vim: set ts=8 noexpandtab :