From 2f0a8449f37b978eb202082b9113039b00c6931a Mon Sep 17 00:00:00 2001 From: "Xiang, Haihao" Date: Fri, 24 Mar 2017 09:13:24 +0800 Subject: [PATCH] Add pre-commit hook Check that the code follows a consistant coding style before committing. The current command will be interrupted if the commit doesn't follow the code style This fixes https://github.com/01org/intel-vaapi-driver/issues/99 Signed-off-by: Xiang, Haihao --- CONTRIBUTING.md | 34 +++++++++++++++++++++++++++++++++- autogen.sh | 9 +++++++++ hooks/pre-commit.hook | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100755 hooks/pre-commit.hook diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index dde531e..7f3abdf 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,7 +4,39 @@ Intel-vaapi-driver is an open source project licensed under the [MIT License] (h ## Coding Style -Intel-vaapi-driver does not have a defined coding style at this time, but that will be updated. +In our project we follow the Linux coding style with a few changes. You may run 'astyle --style=linux -cnpUH -s4 -M120 ' +to format/indent a single file or run './style_unify' in the top-level directory to handle all .c/.h files in the src directory. + +You will fail to commit your patch if your patch doesn't follow the coding style and the pre-commit hook will prompt you to fix +the coding style. + +For example: + +``` +Checking coding style... + +--- .merge_file_tZMQ4C 2017-03-31 11:02:36.244617415 +0800 ++++ /tmp/.merge_file_tZMQ4C.D0V 2017-03-31 11:02:36.274617276 +0800 +@@ -438,8 +438,7 @@ intel_batchbuffer_align(struct intel_bat + assert((pad_size & 3) == 0); + assert(intel_batchbuffer_space(batch) >= pad_size); + +- while (pad_size >= 4) +- { ++ while (pad_size >= 4) { + intel_batchbuffer_emit_dword(batch, 0); + pad_size -= 4; + } + +************************************************************************** + Coding style error in src/intel_batchbuffer.c + + Please fix the coding style before committing. You may run the command + below to fix the coding style from the top-level directory + + astyle --style=linux -cnpUH -s4 -M120 src/intel_batchbuffer.c +************************************************************************** +``` ## Certificate of Origin diff --git a/autogen.sh b/autogen.sh index 626d213..6214204 100755 --- a/autogen.sh +++ b/autogen.sh @@ -6,6 +6,15 @@ test -z "$srcdir" && srcdir=. ORIGDIR=`pwd` cd "$srcdir" +# install pre-commit hook +SRC_PRE_COMMIT=hooks/pre-commit.hook +GIT_PRE_COMMIT=.git/hooks/pre-commit + +if [ ! \( -x $GIT_PRE_COMMIT -a -L $GIT_PRE_COMMIT \) ]; then + rm -f $GIT_PRE_COMMIT + ln -s ../../$SRC_PRE_COMMIT $GIT_PRE_COMMIT +fi + autoreconf -v --install || exit 1 cd $ORIGDIR || exit $? diff --git a/hooks/pre-commit.hook b/hooks/pre-commit.hook new file mode 100755 index 0000000..0582364 --- /dev/null +++ b/hooks/pre-commit.hook @@ -0,0 +1,37 @@ +#!/bin/sh +# +# Use astyle to check the coding style +# + +ASTYLE=astyle +ASTYLE_PARAMS="--style=linux -cnpUH -s4 -M120" + +if [ -z "`which $ASTYLE 2> /dev/null`" ]; then + echo "git pre-commit hook:" + echo "Don't find $ASTYLE, please install $ASTYLE before committing the changes" + exit 1 +fi + +echo "Checking coding style..." +echo "" +for file in `git diff-index --cached --name-only --diff-filter=ACMR HEAD src/ | grep "\.[ch]$" 2> /dev/null`; do + tmp0file=`git checkout-index --temp ${file} | cut -f 1` + tmp1file=`mktemp /tmp/${tmp0file}.XXX` || exit 1 + $ASTYLE $ASTYLE_PARAMS < ${tmp0file} > ${tmp1file} 2> /dev/null + diff -up "${tmp0file}" "${tmp1file}" + ret=$? + rm -f "${tmp0file}" "${tmp1file}" + if [ $ret != 0 ]; then +echo "" +echo "**************************************************************************" +echo " Coding style error in $file" +echo "" +echo " Please fix the coding style before committing. You may run the command" +echo " below to fix the coding style from the top-level directory" +echo "" +echo " $ASTYLE $ASTYLE_PARAMS $file" +echo "**************************************************************************" + exit 1 + fi +done +echo "PASS!!!" -- 2.11.0