1#!/usr/bin/env bash
2###########################################################################
3#    prepare-commit.sh
4#    ---------------------
5#    Date                 : August 2008
6#    Copyright            : (C) 2008 by Juergen E. Fischer
7#    Email                : jef at norbit dot de
8###########################################################################
9#                                                                         #
10#   This program is free software; you can redistribute it and/or modify  #
11#   it under the terms of the GNU General Public License as published by  #
12#   the Free Software Foundation; either version 2 of the License, or     #
13#   (at your option) any later version.                                   #
14#                                                                         #
15###########################################################################
16
17TOPLEVEL=$(git rev-parse --show-toplevel)
18
19PATH=$TOPLEVEL/scripts:$PATH
20
21cd $TOPLEVEL
22
23# GNU prefix command for mac os support (gsed, gsplit)
24GP=
25if [[ "$OSTYPE" =~ darwin* ]]; then
26  GP=g
27fi
28
29if ! type -p astyle.sh >/dev/null; then
30  echo astyle.sh not found
31  exit 1
32fi
33
34if ! type -p colordiff >/dev/null; then
35  colordiff()
36  {
37    cat "$@"
38  }
39fi
40
41if [ "$1" = "-c" ]; then
42  echo "Cleaning..."
43  remove_temporary_files.sh
44fi
45
46set -e
47
48# determine changed files
49MODIFIED=$(git status --porcelain| ${GP}sed -ne "s/^ *[MA]  *//p" | sort -u)
50#MODIFIED=$(find src -name "*.h")
51
52if [ -z "$MODIFIED" ]; then
53  echo nothing was modified
54  exit 0
55fi
56
57# save original changes
58REV=$(git log -n1 --pretty=%H)
59git diff >sha-$REV.diff
60
61ASTYLEDIFF=astyle.$REV.diff
62>$ASTYLEDIFF
63
64# reformat
65i=0
66N=$(echo $MODIFIED | wc -w)
67for f in $MODIFIED; do
68  (( i++ )) || true
69
70  case "$f" in
71  thirdparty/*)
72    echo $f skipped
73    continue
74    ;;
75
76  *.cpp|*.c|*.h|*.cxx|*.hxx|*.c++|*.h++|*.cc|*.hh|*.C|*.H|*.sip|*.py)
77    ;;
78
79  *)
80    continue
81    ;;
82  esac
83
84  m=$f.$REV.prepare
85
86  cp $f $m
87  ASTYLEPROGRESS=" [$i/$N]" astyle.sh $f
88  if diff -u $m $f >>$ASTYLEDIFF; then
89    # no difference found
90    rm $m
91  fi
92done
93
94if [ -s "$ASTYLEDIFF" ]; then
95  if tty -s; then
96    # review astyle changes
97    colordiff <$ASTYLEDIFF | less -r
98  else
99    echo "Files changed (see $ASTYLEDIFF)"
100  fi
101  exit 1
102else
103  rm $ASTYLEDIFF
104fi
105
106
107# If there are whitespace errors, print the offending file names and fail.
108exec git diff-index --check --cached HEAD --
109
110exit 0
111
112# vim: set ts=8 noexpandtab :
113