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