[pygi] Install pre-commit hook that checks the code changes for style conformance



commit 5f0f9a9c9145a129a063b041424c3109a24d9ead
Author: Tomeu Vizoso <tomeu vizoso collabora co uk>
Date:   Wed May 26 13:20:27 2010 +0200

    Install pre-commit hook that checks the code changes for style conformance

 autogen.sh      |    7 +++++++
 pre-commit.hook |   39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 0 deletions(-)
---
diff --git a/autogen.sh b/autogen.sh
index 955b538..992defb 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -148,6 +148,13 @@ do
   fi
 done
 
+# install pre-commit hook for doing clean commits
+if test ! \( -x .git/hooks/pre-commit -a -L .git/hooks/pre-commit \);
+then
+    rm -f .git/hooks/pre-commit
+    ln -s ../../pre-commit.hook .git/hooks/pre-commit
+fi
+
 conf_flags="--enable-maintainer-mode"
 
 if test x$NOCONFIGURE = x; then
diff --git a/pre-commit.hook b/pre-commit.hook
new file mode 100755
index 0000000..6eafd52
--- /dev/null
+++ b/pre-commit.hook
@@ -0,0 +1,39 @@
+#!/bin/sh
+#
+# Check that the code follows a consistant code style
+#
+
+# Check for existence of astyle, and error out if not present.
+if ! builtin type -P astyle; then
+      echo "PyGI git pre-commit hook:"
+      echo "Did not find astyle, please install it before continuing."
+      exit 1
+fi
+
+ASTYLE_PARAMETERS="-p -d -c -S -U -M60"
+
+echo "--Checking style--"
+for file in `git-diff-index --cached --name-only HEAD --diff-filter=ACMR| grep "\.c$"` ; do
+    # nf is the temporary checkout. This makes sure we check against the
+    # revision in the index (and not the checked out version).
+    nf=`git checkout-index --temp ${file} | cut -f 1`
+    newfile=`mktemp /tmp/${nf}.XXXXXX` || exit 1
+    astyle ${ASTYLE_PARAMETERS} < $nf > $newfile 2>> /dev/null
+    diff -u -p "${nf}" "${newfile}"
+    r=$?
+    rm "${newfile}"
+    rm "${nf}"
+    if [ $r != 0 ] ; then
+echo "================================================================================================="
+echo " Code style error in: $file                                                                      "
+echo "                                                                                                 "
+echo " Please fix before committing. Don't forget to run git add before trying to commit again.        "
+echo " If the whole file is to be committed, this should work (run from the top-level directory):      "
+echo "                                                                                                 "
+echo "   astyle ${ASTYLE_PARAMETERS} $file; git add $file; git commit"
+echo "                                                                                                 "
+echo "================================================================================================="
+        exit 1
+    fi
+done
+echo "--Checking style pass--"



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]