QGIS/scripts/prepare-commit.sh

149 lines
3.8 KiB
Bash
Raw Normal View History

2017-01-20 08:03:06 +01:00
#!/usr/bin/env bash
2012-10-04 19:33:47 +02:00
###########################################################################
# 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:$PWD/scripts
if ! tty -s && [[ "$0" =~ /pre-commit ]]; then
exec </dev/tty
fi
2018-06-21 12:49:45 +10:00
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
2013-01-09 16:46:30 +01:00
if [ "$1" = "-c" ]; then
echo "Cleaning..."
remove_temporary_files.sh
2013-01-09 16:46:30 +01:00
fi
set -e
# determine changed files
MODIFIED=$(git status --porcelain| ${GP}sed -ne "s/^ *[MA] *//p" | sort -u)
if [ -z "$MODIFIED" ]; then
echo nothing was modified
exit 0
fi
2018-06-21 12:49:45 +10:00
if [[ -n "$QGIS_CHECK_SPELLING" && -x "${TOPLEVEL}"/scripts/spell_check/check_spelling.sh ]]; then "${TOPLEVEL}"/scripts/spell_check/check_spelling.sh "$MODIFIED"; fi
FILES_CHANGED=0
2017-01-20 08:02:37 +01:00
# save original changes
REV=$(git log -n1 --pretty=%H)
#git diff >sha-"$REV".diff
2011-05-04 21:56:25 +02:00
ASTYLEDIFF=astyle.$REV.diff
2018-06-21 12:49:45 +10:00
true > "$ASTYLEDIFF"
# reformat
2016-03-25 13:55:33 +01:00
i=0
2018-06-21 12:49:45 +10:00
N=$(echo "$MODIFIED" | wc -w)
for f in $MODIFIED; do
(( i++ )) || true
case "$f" in
2018-08-03 08:48:15 +02:00
*.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py|*.mm)
;;
*)
continue
;;
esac
m=$f.$REV.prepare
2018-06-21 12:49:45 +10:00
cp "$f" "$m"
ASTYLEPROGRESS=" [$i/$N]" astyle.sh "$f"
if diff -u "$m" "$f" >>"$ASTYLEDIFF"; then
# no difference found
2018-06-21 12:49:45 +10:00
rm "$m"
fi
done
if [ -s "$ASTYLEDIFF" ]; then
if tty -s; then
# review astyle changes
2018-06-21 12:49:45 +10:00
colordiff <"$ASTYLEDIFF" | less -r
rm "$ASTYLEDIFF"
else
echo "Files changed (see $ASTYLEDIFF)"
fi
FILES_CHANGED=1
else
2018-06-21 12:49:45 +10:00
rm "$ASTYLEDIFF"
fi
2011-05-08 21:01:34 +02:00
# verify SIP files
SIPIFYDIFF=sipify.$REV.diff
2018-06-21 12:49:45 +10:00
true > "$SIPIFYDIFF"
for f in $MODIFIED; do
# if cpp header
if [[ $f =~ ^src\/(core|gui|analysis|server|3d)\/.*\.h$ ]]; then
# look if corresponding SIP file
sip_file=$(${GP}sed -r 's@^src/(core|gui|analysis|server|3d)/@@; s@\.h$@.sip@' <<<"$f" )
2018-06-21 12:49:45 +10:00
pyfile=$(${GP}sed -E 's@([^\/]+\/)*([^\/]+)\.sip@\2.py@;' <<< "$sip_file")
module=$(${GP}sed -r 's@src/(core|gui|analysis|server|3d)/.*$@\1@' <<<"$f" )
2018-06-21 12:49:45 +10:00
if grep -Fq "$sip_file" "${TOPLEVEL}"/python/"${module}"/"${module}"_auto.sip; then
sip_file=$(${GP}sed -r 's@^src/(core|gui|analysis|server|3d)@\1/auto_generated@; s@\.h$@.sip.in@' <<<"$f" )
2017-06-26 09:57:09 +02:00
m=python/$sip_file.$REV.prepare
2018-06-21 12:49:45 +10:00
touch python/"$sip_file"
cp python/"$sip_file" "$m"
"${TOPLEVEL}"/scripts/sipify.pl -s python/"$sip_file" -p python/"${module}"/auto_additions/"${pyfile}" "$f"
if ! diff -u "$m" python/"$sip_file" >>"$SIPIFYDIFF"; then
2017-06-26 09:57:09 +02:00
echo "python/$sip_file is not up to date"
fi
2018-06-21 12:49:45 +10:00
rm "$m"
fi
fi
done
if [[ -s "$SIPIFYDIFF" ]]; then
if tty -s; then
# review astyle changes
2018-06-21 12:49:45 +10:00
colordiff <"$SIPIFYDIFF" | less -r
rm "$SIPIFYDIFF"
else
echo "Files changed (see $ASTYLEDIFF)"
fi
FILES_CHANGED=1
else
2018-06-21 12:49:45 +10:00
rm "$SIPIFYDIFF"
fi
exit $FILES_CHANGED
2017-06-27 10:34:12 +02:00
# vim: set ts=2 expandtab :