Merge pull request #1484 from mayeut/mandatory-inttypes-stdint
[openjpeg.git] / scripts / verify-indentation.sh
1 #!/bin/bash
2 cd $(git rev-parse --show-toplevel)
3
4 export PATH=$PATH:$PWD/scripts
5
6 if [ -z "$TRAVIS_COMMIT_RANGE" -a -z "$GITHUB_SHA" ]; then
7         echo "No commit range given"
8         exit 0
9 fi
10
11 if ! type -p astyle.sh >/dev/null; then
12         echo astyle.sh not found
13         exit 1
14 fi
15
16 set -e
17
18 ASTYLEDIFF=/tmp/astyle.diff
19 >$ASTYLEDIFF
20
21 if [ ! -z $GITHUB_BASE_REF ] && [ ! -z $GITHUB_HEAD_REF ]; then
22   # on a PR
23   echo "GitHub PR COMMIT RANGE: ${GITHUB_BASE_REF}..${GITHUB_HEAD_REF}"
24   git branch tmp_${GITHUB_BASE_REF} origin/${GITHUB_BASE_REF}
25   BASE_SHA1=$(git rev-parse tmp_${GITHUB_BASE_REF})
26   FILES=$(git diff --diff-filter=AMR --name-only ${BASE_SHA1}..${GITHUB_SHA} | tr '\n' ' ' )
27 elif [ ! -z  $GITHUB_SHA ]; then
28   echo "GitHub push COMMIT $GITHUB_SHA"
29   FILES=$(git diff --diff-filter=AMR --name-only ${GITHUB_SHA}~1..${GITHUB_SHA} | tr '\n' ' ' )
30 elif [ ! -z  $TRAVIS_PULL_REQUEST_BRANCH ]; then
31   # if on a PR, just analyse the changed files
32   echo "TRAVIS PR BRANCH: $TRAVIS_PULL_REQUEST_BRANCH"
33   FILES=$(git diff --diff-filter=AMR --name-only $(git merge-base HEAD master) | tr '\n' ' ' )
34 elif [ ! -z  $TRAVIS_COMMIT_RANGE  ]; then
35   echo "TRAVIS COMMIT RANGE: $TRAVIS_COMMIT_RANGE"
36   FILES=$(git diff --diff-filter=AMR --name-only ${TRAVIS_COMMIT_RANGE/.../..} | tr '\n' ' ' )
37 fi
38
39 for f in $FILES; do
40         if ! [ -f "$f" ]; then
41                 echo "$f was removed." >>/tmp/ctest-important.log
42                 continue
43         fi
44
45         echo "Checking $f" >>/tmp/ctest-important.log
46         case "$f" in
47         thirdparty*)
48                 echo "$f skipped"
49                 continue
50                 ;;
51
52         *.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py)
53                 ;;
54
55         *)
56                 continue
57                 ;;
58         esac
59
60         m="$f.prepare"
61         cp "$f" "$m"
62         astyle.sh "$f"
63         if diff -u "$m" "$f" >>$ASTYLEDIFF; then
64                 rm "$m"
65         else
66                 echo "File $f needs indentation"
67         fi
68 done
69
70 if [ -s "$ASTYLEDIFF" ]; then
71         echo
72         echo "Required indentation updates:"
73         cat "$ASTYLEDIFF"
74
75         cat <<EOF
76
77 Tips to prevent and resolve:
78 * Enable WITH_ASTYLE in your cmake configuration to format C++ code
79 * Install autopep8 (>= 1.2.1) to format python code
80 * Use "scripts/astyle.sh file" to fix the now badly indented files
81 * Consider using scripts/prepare-commit.sh as pre-commit hook to avoid this
82   in the future (ln -s scripts/prepare-commit.sh .git/hooks/pre-commit) or
83   run it manually before each commit.
84 EOF
85
86         exit 1
87 fi