diff options
| author | Even Rouault <even.rouault@spatialys.com> | 2017-05-09 15:28:09 +0200 |
|---|---|---|
| committer | Even Rouault <even.rouault@spatialys.com> | 2017-05-09 20:46:16 +0200 |
| commit | d4e54e9f35d532062533f1d369c159810b01d224 (patch) | |
| tree | 7046ff72a41a8b0ad2e40e1d4aa3b191d03f6380 /scripts/prepare-commit.sh | |
| parent | 8650b70e06408d394c1708846b6fc2d86cf14079 (diff) | |
Add mechanisms to reformant and check code style (#128)
Use an internal version of astyle (astyle 3.0). Scripts taken from QGIS.
astyle.options from https://github.com/uclouvain/openjpeg/issues/128
scripts/prepare-commit.sh can be used locally to automatically reformat
edited files.
Travis-CI will run scripts/verify-indentation.sh to verify committed files.
Diffstat (limited to 'scripts/prepare-commit.sh')
| -rwxr-xr-x | scripts/prepare-commit.sh | 112 |
1 files changed, 112 insertions, 0 deletions
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 : |
