Merge pull request #1376 from rouault/gha
[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 ${GITHUB_BASE_REF} origin/${GITHUB_BASE_REF}
25   git branch ${GITHUB_HEAD_REF} origin/${GITHUB_HEAD_REF}
26   BASE_SHA1=$(git rev-parse ${GITHUB_BASE_REF})
27   HEAD_SHA1=$(git rev-parse ${GITHUB_HEAD_REF})
28   FILES=$(git diff --diff-filter=AMR --name-only ${BASE_SHA1}..${HEAD_SHA1} | tr '\n' ' ' )
29 elif [ ! -z  $GITHUB_SHA ]; then
30   echo "GitHub push COMMIT $GITHUB_SHA"
31   FILES=$(git diff --diff-filter=AMR --name-only ${GITHUB_SHA}~1..${GITHUB_SHA} | tr '\n' ' ' )
32 elif [ ! -z  $TRAVIS_PULL_REQUEST_BRANCH ]; then
33   # if on a PR, just analyse the changed files
34   echo "TRAVIS PR BRANCH: $TRAVIS_PULL_REQUEST_BRANCH"
35   FILES=$(git diff --diff-filter=AMR --name-only $(git merge-base HEAD master) | tr '\n' ' ' )
36 elif [ ! -z  $TRAVIS_COMMIT_RANGE  ]; then
37   echo "TRAVIS COMMIT RANGE: $TRAVIS_COMMIT_RANGE"
38   FILES=$(git diff --diff-filter=AMR --name-only ${TRAVIS_COMMIT_RANGE/.../..} | tr '\n' ' ' )
39 fi
40
41 for f in $FILES; do
42         if ! [ -f "$f" ]; then
43                 echo "$f was removed." >>/tmp/ctest-important.log
44                 continue
45         fi
46
47         echo "Checking $f" >>/tmp/ctest-important.log
48         case "$f" in
49         thirdparty*)
50                 echo "$f skipped"
51                 continue
52                 ;;
53
54         *.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py)
55                 ;;
56
57         *)
58                 continue
59                 ;;
60         esac
61
62         m="$f.prepare"
63         cp "$f" "$m"
64         astyle.sh "$f"
65         if diff -u "$m" "$f" >>$ASTYLEDIFF; then
66                 rm "$m"
67         else
68                 echo "File $f needs indentation"
69         fi
70 done
71
72 if [ -s "$ASTYLEDIFF" ]; then
73         echo
74         echo "Required indentation updates:"
75         cat "$ASTYLEDIFF"
76
77         cat <<EOF
78
79 Tips to prevent and resolve:
80 * Enable WITH_ASTYLE in your cmake configuration to format C++ code
81 * Install autopep8 (>= 1.2.1) to format python code
82 * Use "scripts/astyle.sh file" to fix the now badly indented files
83 * Consider using scripts/prepare-commit.sh as pre-commit hook to avoid this
84   in the future (ln -s scripts/prepare-commit.sh .git/hooks/pre-commit) or
85   run it manually before each commit.
86 EOF
87
88         exit 1
89 fi