From 49fbb7a7698facf8496bc511b1e42214ea97242f Mon Sep 17 00:00:00 2001 From: Eduardo Barretto Date: Fri, 2 Mar 2018 16:32:50 -0300 Subject: [PATCH] Add pre-commit hook and update documentation The pre-commit hook will help developers have their code changes checked for indentation problems. Documentation mentions how the hook can be set up. Signed-off-by: Eduardo Barretto --- .githooks/pre-commit | 58 ++++++++++++++++++++++++++++++++++++++++++++ doc/coding_style.md | 14 +++++++++++ 2 files changed, 72 insertions(+) create mode 100644 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100644 index 00000000..e5257af0 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,58 @@ +#!/bin/sh +# +# Check that the code follows the coding style +# +# If the code does not follow the coding style a warning will be printed. +# + +version=`gnuindent --version 2> /dev/null` +if [ "x$version" = "x" ]; then + version=`indent --version 2> /dev/null` + if [ "x$version" = "x" ] || [[ "$version" != *GNU* ]]; then + echo "Git pre-commit hook:" + echo "Did not find GNU indent, please install it before continuing." + exit 1 + fi + INDENT=indent +else + INDENT=gnuindent +fi + +echo "*******************" + +for file in `git diff-index --cached --name-only HEAD --diff-filter=ACMR`; do + ext=$(expr "$file" : ".*\(\..*\)") + case $ext in + .c|.h) + echo "Checking code style" + # ckfile is the temporary checkout and we indent it + ckfile=`git checkout-index --temp ${file} | cut -f 1` + newfile=`mktemp /tmp/${ckfile}.XXXXXX` || exit 1 + $INDENT $ckfile -o $newfile 2>> /dev/null + $INDENT $newfile 2>> /dev/null + diff -u -p "${ckfile}" "${newfile}" + r=$? + rm "${ckfile}" + rm "${newfile}" + rm "${newfile}~" + if [ $r != 0 ]; then + echo "Warning: Code style error in $file" + read -n1 -p "Do you want to CONTINUE committing? [Y/n]" opt < /dev/tty + case $opt in + n|N) + echo + exit 1 + ;; + *) + ;; + esac + echo "*******************" + fi + exit 0 + ;; + *) + echo "Not checking code style for this type of file" + echo "*******************" + ;; + esac +done diff --git a/doc/coding_style.md b/doc/coding_style.md index 51af970d..0511d9e6 100644 --- a/doc/coding_style.md +++ b/doc/coding_style.md @@ -10,6 +10,20 @@ The inspiration and formatting of this document came from the Linux Kernel coding style document, but make no assumption as the coding style differ from each other on some aspects. +## 0. Setting up automatic code style check + +To help developers on checking if their code changes are following the coding +style format, we created a pre-commit git hook which is shared under .githooks/ +directory. This hook will use GNU indent to check your code changes. + +You might wonder "why we ask for user confirmation?". Well, we don't want to +create a overhead for developers that are working on feature branches and their +code changes are not yet ready for a pull request. + +To set up the pre-commit hook, each developer after cloning the project needs +to run: +$ ln -s ../../.githooks/pre-commit .git/hooks/pre-commit + ## 1. Indentation Tabs are 4 space characters, differently from many projects that define it as 8