Skip to content

Commit

Permalink
Add a style-enforcing pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
willglynn committed Jan 17, 2013
1 parent c42edb9 commit c1f6023
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 3 deletions.
47 changes: 47 additions & 0 deletions .git-hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/bin/bash

indent() {
sed 's/^/ /'
}

# Check for existence of astyle, and error out if not present.
if [ ! -x "$(which astyle)" ]; then
echo "git pre-commit hook:"
echo "Did not find astyle, please install it before continuing."
exit 1
fi

ASTYLE_PARAMETERS="-p -H -f -j -z2 -c -k3 -U -A8"
FILE_PATTERN="^(src|test)/.*\.(c|cpp|h)$"

echo "-----> Checking code with astyle"
for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR | egrep $FILE_PATTERN`; do
# nf is the temporary checkout. This makes sure we check against the
# revision in the index (and not the checked out version).
temp_checkout=`git checkout-index --temp ${file} | cut -f 1`
formatted=`mktemp /tmp/${temp_checkout}.formatted` || exit 1
diff=`mktemp /tmp/${temp_checkout}.diff` || exit 1
astyle ${ASTYLE_PARAMETERS} < $temp_checkout > $formatted 2>/dev/null
failed=$?
diff -u -p "${temp_checkout}" "${formatted}" > ${diff}
different=$?
if [ $failed != 0 ]; then
echo " `astyle` failed on: $file"
echo " Please investigate this situation. You'll want:"
echo
echo " astyle ${ASTYLE_PARAMETERS} $file"
elif [ $different != 0 ]; then
echo " Code style error in: $file"
cat "${diff}" | indent | indent
echo
echo " If the whole file is to be committed, you can format and re-stage it with:"
echo
echo " astyle ${ASTYLE_PARAMETERS} -n $file; git add $file"
rm "${temp_checkout}" "${formatted}" "${diff}"
exit 1
else
rm "${temp_checkout}" "${formatted}" "${diff}"
fi
done
echo " astyle passed"

26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ Race Into Space is distributed under GNU General Public License
(GPL) version 2. You can find the terms and conditions in file
`COPYING`.

Contributing
============

We coordinate development through the [GitHub issue
tracker](https://github.com/raceintospace/raceintospace/issues). Feel free to
report bugs, discuss tasks, or pick up work there. If you want to make
changes, please fork, edit, and [send us a pull
request](https://github.com/raceintospace/raceintospace/pull/new/master).

There's a `.git-hooks` directory in the root. This contains a `pre-commit`
hook that verifies code styling before accepting changes. You can add this to
your local repository's `.git/hooks/` directory like:

$ cd raceintospace
$ ln -s ../../.git-hooks/pre-commit .git/hooks/pre-commit

Pull requests that don't match the project code style are still likely to be
accepted after manually formatting and amending your changeset. The formatting
tool (`astyle`) is completely automated; please try to use it.

Building
========

Expand Down Expand Up @@ -47,7 +67,7 @@ On UNIXy systems (including Mac OS X), you can build everything with:
$ make

Linux
=====
-----

We rely on the operating system to provide most of the dependencies. If you're
on a Debian-based system, you can get everything with:
Expand All @@ -57,7 +77,7 @@ on a Debian-based system, you can get everything with:
libprotobuf-dev protobuf-compiler

Mac OS X
========
--------

You need CMake, which is readily obtained from
[Homebrew](http://mxcl.github.com/homebrew/), which you probably have anyway.
Expand All @@ -79,7 +99,7 @@ project file:
$ open raceintospace.xcodeproj

Windows
=======
-------

Ingredients:

Expand Down

0 comments on commit c1f6023

Please sign in to comment.