From b2d6ae091b466fdab005477c913c24a3d5ccc9d7 Mon Sep 17 00:00:00 2001 From: Mark Olesen <Mark.Olesen@Germany> Date: Wed, 21 Jul 2010 12:58:43 +0200 Subject: [PATCH] ENH: made Andy's pre-receive hook into a pre-commit hook - allows fixes before trying to push --- bin/tools/pre-commit-hook | 183 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 183 insertions(+) create mode 100755 bin/tools/pre-commit-hook diff --git a/bin/tools/pre-commit-hook b/bin/tools/pre-commit-hook new file mode 100755 index 00000000000..5460f7b38d4 --- /dev/null +++ b/bin/tools/pre-commit-hook @@ -0,0 +1,183 @@ +#!/bin/bash +#---------------------------------*- sh -*------------------------------------- +# ========= | +# \\ / F ield | OpenFOAM: The Open Source CFD Toolbox +# \\ / O peration | +# \\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd. +# \\/ M anipulation | +#------------------------------------------------------------------------------ +# License +# This file is part of OpenFOAM. +# +# OpenFOAM is free software: you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# OpenFOAM is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>. +# +# Script +# pre-commit-hook +# +# Description +# pre-commit hook for git. +# Copy or link this file as ".git/hooks/pre-commit" +# +# Eg, +# ( +# cd $WM_PROJECT_DIR/.git/hooks && +# ln -sf ../../bin/tools/pre-commit-hook pre-commit +# ) +# +# Hook receives: empty +# +# Checks for +# - illegal code, e.g. <TAB> +# - copyright is current, e.g. if present, contains XXX-<current-year> +# - columns greater than 80 for *.[CH] files +# +# Note +# Using "git commit --no-verify" it is possible to override the hook. +# +#------------------------------------------------------------------------------ +die() +{ + echo 'pre-commit hook failure' 1>&2 + echo '-----------------------' 1>&2 + echo '' 1>&2 + echo "$@" 1>&2 + exit 1 +} + +#----------------------------------------------------------------------------- +# Check content that will be added by this commit. + +if git rev-parse --verify -q HEAD > /dev/null +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +fi + +# list of all files +fileList=$(git diff-index --name-only $against --) +unset badFiles + +# join badFiles with this +NL=$'\n ' + +showBadFiles() +{ + if [ -n "$badFiles" ] + then + echo 'pre-commit hook failure' 1>&2 + echo '-----------------------' 1>&2 + echo "$@" 1>&2 + echo '' 1>&2 + echo "File(s):$badFiles" 1>&2 + echo '' 1>&2 + exit 1 + fi +} + +# +# check for bad characters or strings +# +checkIllegalCode() +{ + TAB=$'\t' + BAD=("Nabla" "$TAB") + BADmsg=("Nabla" "<TAB>") + n=${#BAD[@]}; + + unset errorType + for (( i = 0; i < $n; i++ )) + do + for f in $fileList + do + illegalWord="${BAD[$i]}" + if git grep -q --cached "$illegalWord" -- "$f" + then + errorType="${BADmsg[$i]}" + badFiles="$badFiles$NL$f" + break 2 + fi + done + done + + showBadFiles "Remove/correct '$errorType' references before pushing" +} + + +# +# check that OpenCFD copyright is current +# +checkCopyright() +{ + year=$(date +%Y) + + for f in $fileList + do + present=`git grep --cached Copyright -- "$f" | grep OpenCFD` + if [ -n "$present" ] + then + echo "$present" | grep -q "$year" || badFiles="$badFiles$NL$f" + fi + done + + showBadFiles "Update copyright year before pushing, e.g. XXXX-$year" +} + + +# +# limit line length to 80-columns, except C++ comment lines +# +checkLineLength() +{ + for f in $fileList + do + # limit to *.[CH] files + case "$f" in + *.[CH]) + ## detect long lines, but ignore C++ comment-lines + ## extract line numbers + longLines=$(git grep -n --cached ".\{81,\}" -- "$f" | + sed -ne '\@^[^:]*:[^:]*: *//.*@b; s@^[^:]*:\([0-9]*\):.*@\1@p' | + tr '\n' ' ' + ) + if [ -n "$longLines" ] + then + badFiles="$badFiles$NL$f -- lines: $longLines" + fi + ;; + esac + done + + showBadFiles "Limit code to 80 columns before pushing" +} + + +# do all checks +#~~~~~~~~~~~~~~ + +# use builtin whitespace checks to avoid trailing space +bad=$(git diff-index --check --cached $against --) || die "$bad" + +# check for illegal code, e.g. <TAB>, Nabla etc +checkIllegalCode + +# if OpenCFD copyright exists, ensure it contains correct year +checkCopyright + +# ensure code conforms to 80 col max +checkLineLength + + +#------------------------------------------------------------------------------ -- GitLab