diff options
| author | Even Rouault <even.rouault@mines-paris.org> | 2017-05-15 12:09:45 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-15 12:09:45 +0200 |
| commit | 28d2eabca79d06378843d1e94fecfb4a5e22178d (patch) | |
| tree | 98b3b26eacd4a4d153f41d4e63cca93edf7fea17 /scripts | |
| parent | 0ecac46a1db911cbe340c4467000e91cea82c96d (diff) | |
| parent | cde210be392d60fbcd8a3e7cacfa62660a95e8d9 (diff) | |
Merge pull request #919 from rouault/reformat
Add mechanisms to reformat and check code style, and reformat whole codebase (#128)
Diffstat (limited to 'scripts')
| -rw-r--r-- | scripts/astyle.options | 11 | ||||
| -rwxr-xr-x | scripts/astyle.sh | 117 | ||||
| -rwxr-xr-x | scripts/prepare-commit.sh | 112 | ||||
| -rwxr-xr-x | scripts/remove_temporary_files.sh | 41 | ||||
| -rwxr-xr-x | scripts/verify-indentation.sh | 79 |
5 files changed, 360 insertions, 0 deletions
diff --git a/scripts/astyle.options b/scripts/astyle.options new file mode 100644 index 00000000..77f5e125 --- /dev/null +++ b/scripts/astyle.options @@ -0,0 +1,11 @@ +--convert-tabs +--lineend=linux +--indent=spaces=4 +--style=kr +--add-braces +--max-code-length=80 +--break-after-logical +--pad-header +--pad-oper +--unpad-paren +--suffix=none diff --git a/scripts/astyle.sh b/scripts/astyle.sh new file mode 100755 index 00000000..603a1c94 --- /dev/null +++ b/scripts/astyle.sh @@ -0,0 +1,117 @@ +#!/bin/bash +########################################################################### +# astyle.sh +# --------------------- +# Date : August 2008 +# Copyright : (C) 2008 by Juergen E. Fischer +# Email : jef at norbit dot de +########################################################################### +# # +# This program is free software; you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation; either version 2 of the License, or # +# (at your option) any later version. # +# # +########################################################################### + +for ASTYLE in ${OPJSTYLE} $(dirname $0)/opjstyle $(dirname $0)/RelWithDebInfo/opjstyle +do + if type -p $ASTYLE >/dev/null; then + break + fi + ASTYLE= +done + +if [ -z "$ASTYLE" ]; then + echo "opjstyle not found - please enable WITH_ASTYLE in cmake and build it" >&2 + exit 1 +fi + +if type -p tput >/dev/null; then + elcr="$ASTYLEPROGRESS$(tput el)$(tput cr)" +else + elcr="$ASTYLEPROGRESS \r" +fi + +if ! type -p flip >/dev/null; then + if type -p dos2unix >/dev/null; then + flip() { + dos2unix -k $2 + } + else + echo "flip not found" >&2 + flip() { + : + } + fi +fi + +if ! type -p autopep8 >/dev/null; then + echo "autopep8 not found" >&2 + autopep8() { + : + } +fi + +ASTYLEOPTS=$(dirname $0)/astyle.options +if type -p cygpath >/dev/null; then + ASTYLEOPTS="$(cygpath -w $ASTYLEOPTS)" +fi + +set -e + +astyleit() { + $ASTYLE --options="$ASTYLEOPTS" "$1" + #modified=$1.unify_includes_modified + #cp "$1" "$modified" + #scripts/unify_includes.pl "$modified" + #scripts/doxygen_space.pl "$modified" + #diff "$1" "$modified" >/dev/null || mv "$modified" "$1" + #rm -f "$modified" +} + +for f in "$@"; do + case "$f" in + thirdparty/*) + echo -ne "$f skipped $elcr" + continue + ;; + + *.cpp|*.h|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.hpp) + if [ -x "$f" ]; then + chmod a-x "$f" + fi + cmd=astyleit + ;; + + *.py) + #cmd="autopep8 --in-place --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701" + echo -ne "Formatting $f $elcr" + cmd="autopep8 --in-place --ignore=E261,E265,E402,E501" + ;; + + + *) + echo -ne "$f skipped $elcr" + continue + ;; + esac + + if ! [ -f "$f" ]; then + echo "$f not found" >&2 + continue + fi + + if [[ -f $f && `head -c 3 $f` == $'\xef\xbb\xbf' ]]; then + mv $f $f.bom + tail -c +4 $f.bom > $f + echo "removed BOM from $f" + fi + + modified=$f.flip_modified + cp "$f" "$modified" + flip -ub "$modified" + diff "$f" "$modified" >/dev/null || mv "$modified" "$f" + rm -f "$modified" + eval "$cmd '$f'" +done diff --git a/scripts/prepare-commit.sh b/scripts/prepare-commit.sh new file mode 100755 index 00000000..63dac65c --- /dev/null +++ b/scripts/prepare-commit.sh @@ -0,0 +1,112 @@ +#!/usr/bin/env bash +########################################################################### +# prepare-commit.sh +# --------------------- +# Date : August 2008 +# Copyright : (C) 2008 by Juergen E. Fischer +# Email : jef at norbit dot de +########################################################################### +# # +# This program is free software; you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation; either version 2 of the License, or # +# (at your option) any later version. # +# # +########################################################################### + +TOPLEVEL=$(git rev-parse --show-toplevel) + +PATH=$TOPLEVEL/scripts:$PATH + +cd $TOPLEVEL + +# GNU prefix command for mac os support (gsed, gsplit) +GP= +if [[ "$OSTYPE" =~ darwin* ]]; then + GP=g +fi + +if ! type -p astyle.sh >/dev/null; then + echo astyle.sh not found + exit 1 +fi + +if ! type -p colordiff >/dev/null; then + colordiff() + { + cat "$@" + } +fi + +if [ "$1" = "-c" ]; then + echo "Cleaning..." + remove_temporary_files.sh +fi + +set -e + +# determine changed files +MODIFIED=$(git status --porcelain| ${GP}sed -ne "s/^ *[MA] *//p" | sort -u) +#MODIFIED=$(find src -name "*.c") + +if [ -z "$MODIFIED" ]; then + echo nothing was modified + exit 0 +fi + +# save original changes +REV=$(git log -n1 --pretty=%H) +git diff >sha-$REV.diff + +ASTYLEDIFF=astyle.$REV.diff +>$ASTYLEDIFF + +# reformat +i=0 +N=$(echo $MODIFIED | wc -w) +for f in $MODIFIED; do + (( i++ )) || true + + case "$f" in + thirdparty/*) + echo $f skipped + continue + ;; + + *.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py) + ;; + + *) + continue + ;; + esac + + m=$f.$REV.prepare + + cp $f $m + ASTYLEPROGRESS=" [$i/$N]" astyle.sh $f + if diff -u $m $f >>$ASTYLEDIFF; then + # no difference found + rm $m + fi +done + +if [ -s "$ASTYLEDIFF" ]; then + if tty -s; then + # review astyle changes + colordiff <$ASTYLEDIFF | less -r + else + echo "Files changed (see $ASTYLEDIFF)" + fi + exit 1 +else + rm $ASTYLEDIFF +fi + + +# If there are whitespace errors, print the offending file names and fail. +exec git diff-index --check --cached HEAD -- + +exit 0 + +# vim: set ts=8 noexpandtab : diff --git a/scripts/remove_temporary_files.sh b/scripts/remove_temporary_files.sh new file mode 100755 index 00000000..535cccec --- /dev/null +++ b/scripts/remove_temporary_files.sh @@ -0,0 +1,41 @@ +#!/bin/bash +########################################################################### +# remove_git_confict_files.sh +# --------------------- +# Date : April 2012 +# Copyright : (C) 2012 by Tim Sutton +# Email : tim at kartoza dot com +########################################################################### +# # +# This program is free software; you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation; either version 2 of the License, or # +# (at your option) any later version. # +# # +########################################################################### + +# +# A simple script to get rid of QGIS related temporary files left in +# your QGIS source folder by git + +# Tim Sutton, May 2008 +find . \ + \( \ + -name "*.orig" \ + -o -name "*.prepare" \ + -o -name "*.sortinc" \ + -o -name "*.unify_includes_modified" \ + -o -name "*.nocopyright" \ + -o -name "astyle*.diff" \ + -o -name "sha-*.diff" \ + -o -name "*.astyle" \ + -o -name "sha*.diff" \ + -o -name "*.bom" \ + -o -name "*.bak" \ + -o -name "*.rej" \ + -o -name "*.orig" \ + -o -name "*.new" \ + -o -name "*~" \ + \) \ + -print \ + -delete diff --git a/scripts/verify-indentation.sh b/scripts/verify-indentation.sh new file mode 100755 index 00000000..120e1411 --- /dev/null +++ b/scripts/verify-indentation.sh @@ -0,0 +1,79 @@ +#!/bin/bash +cd $(git rev-parse --show-toplevel) + +export PATH=$PATH:$PWD/scripts + +if [ -z "$TRAVIS_COMMIT_RANGE" ]; then + echo "No commit range given" + exit 0 +fi + +if ! type -p astyle.sh >/dev/null; then + echo astyle.sh not found + exit 1 +fi + +set -e + +ASTYLEDIFF=/tmp/astyle.diff +>$ASTYLEDIFF + + +if [[ ! -z $TRAVIS_PULL_REQUEST_BRANCH ]]; then + # if on a PR, just analyse the changed files + echo "TRAVIS PR BRANCH: $TRAVIS_PULL_REQUEST_BRANCH" + FILES=$(git diff --diff-filter=AM --name-only $(git merge-base HEAD master) | tr '\n' ' ' ) +elif [[ ! -z $TRAVIS_COMMIT_RANGE ]]; then + echo "TRAVIS COMMIT RANGE: $TRAVIS_COMMIT_RANGE" + FILES=$(git diff --diff-filter=AM --name-only ${TRAVIS_COMMIT_RANGE/.../..} | tr '\n' ' ' ) +fi + +for f in $FILES; do + if ! [ -f "$f" ]; then + echo "$f was removed." >>/tmp/ctest-important.log + continue + fi + + echo "Checking $f" >>/tmp/ctest-important.log + case "$f" in + thirdparty*) + echo "$f skipped" + continue + ;; + + *.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py) + ;; + + *) + continue + ;; + esac + + m="$f.prepare" + cp "$f" "$m" + astyle.sh "$f" + if diff -u "$m" "$f" >>$ASTYLEDIFF; then + rm "$m" + else + echo "File $f needs indentation" + fi +done + +if [ -s "$ASTYLEDIFF" ]; then + echo + echo "Required indentation updates:" + cat "$ASTYLEDIFF" + + cat <<EOF + +Tips to prevent and resolve: +* Enable WITH_ASTYLE in your cmake configuration to format C++ code +* Install autopep8 (>= 1.2.1) to format python code +* Use "scripts/astyle.sh file" to fix the now badly indented files +* Consider using scripts/prepare-commit.sh as pre-commit hook to avoid this + in the future (ln -s scripts/prepare-commit.sh .git/hooks/pre-commit) or + run it manually before each commit. +EOF + + exit 1 +fi |
