From ba19b63907a4b439be039416cfb5bf57a271639b Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Wed, 10 Nov 2010 11:50:00 +0100 Subject: [PATCH] STYLE: generalize functions in pre-commit-hook - improves code re-usage for pre-receive hook --- bin/tools/pre-commit-hook | 55 ++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/bin/tools/pre-commit-hook b/bin/tools/pre-commit-hook index b0ca86f020..6f4023d7d8 100755 --- a/bin/tools/pre-commit-hook +++ b/bin/tools/pre-commit-hook @@ -49,9 +49,10 @@ # test the specified files/directories for standards conformance. # #------------------------------------------------------------------------------ +hookName="pre-commit" die() { - echo 'pre-commit hook failure' 1>&2 + echo '$hookName hook failure' 1>&2 echo '-----------------------' 1>&2 echo '' 1>&2 echo "$@" 1>&2 @@ -104,7 +105,7 @@ dieOnBadFiles() { if [ -n "$badFiles" ] then - echo 'pre-commit hook failure' 1>&2 + echo '$hookName hook failure' 1>&2 echo '-----------------------' 1>&2 echo "$@" 1>&2 echo '' 1>&2 @@ -116,22 +117,37 @@ dieOnBadFiles() } +# +# qualify 'git grep' to check cached value or from a specific commit +# +gitScope() +{ + if [ "$#" -gt 0 ] + then + echo "$1:" + else + echo "--cached -- " + fi +} + # # check for bad strings, characters, etc # checkIllegalCode() { - echo "pre-commit: check bad strings/characters etc ..." 1>&2 + echo "$hookName: check bad strings/characters etc ..." 1>&2 reBad="(N""abla|"$'\t'")" msgBad="N""abla or " + scope=$(gitScope $@) + badFiles=$( for f in $fileList do # parse line numbers from grep output: # : contents - lines=$(git grep --cached -E -hn -e "$reBad" -- "$f" | + lines=$(git grep -E -hn -e "$reBad" $scope"$f" | sed -e 's@:.*@@' | tr '\n' ' ' ) @@ -149,7 +165,9 @@ checkIllegalCode() checkCopyright() { year=$(date +%Y) - echo "pre-commit: check copyright ..." 1>&2 + echo "$hookName: check copyright ..." 1>&2 + + scope=$(gitScope $@) badFiles=$( for f in $fileList @@ -160,10 +178,10 @@ checkCopyright() # parse line numbers from grep output: # : contents # - lines=$(git grep --cached -F -hn -e Copyright \ + lines=$(git grep -F -hn -e Copyright \ --and -e OpenCFD \ --and --not -e "$year" \ - -- "$f" | + $scope"$f" | sed -e 's@:.*@@' | tr '\n' ' ' ) @@ -180,7 +198,9 @@ checkCopyright() # checkLineLength() { - echo "pre-commit: check line lengths ..." 1>&2 + echo "$hookName: check line lengths ..." 1>&2 + + scope=$(gitScope $@) badFiles=$( for f in $fileList @@ -190,7 +210,7 @@ checkLineLength() (*.[CH]) # parse line numbers from grep output: # : contents - lines=$(git grep --cached -hn -e '^.\{81,\}' -- "$f" | + lines=$(git grep -hn -e '^.\{81,\}' $scope"$f" | sed -e 's@:.*@@' | tr '\n' ' ' ) @@ -209,7 +229,9 @@ checkLineLength() # checkLineLengthNonComments() { - echo "pre-commit: check line lengths ..." 1>&2 + echo "$hookName: check line lengths ..." 1>&2 + + scope=$(gitScope $@) badFiles=$( for f in $fileList @@ -219,9 +241,9 @@ checkLineLengthNonComments() (*.[CH]) # parse line numbers from grep output: # : contents - lines=$(git grep --cached -hn -e '^.\{81,\}' \ + lines=$(git grep -hn -e '^.\{81,\}' \ --and --not -e "^ *//" \ - -- "$f" | + $scope"$f" | sed -e 's@:.*@@' | tr '\n' ' ' ) @@ -234,12 +256,15 @@ checkLineLengthNonComments() dieOnBadFiles "Limit code to 80 columns before pushing" } + # # limit line length to 80-columns, except #directive lines # checkLineLengthNonDirective() { - echo "pre-commit: check line lengths ..." 1>&2 + echo "$hookName: check line lengths ..." 1>&2 + + scope=$(gitScope $@) badFiles=$( for f in $fileList @@ -249,9 +274,9 @@ checkLineLengthNonDirective() (*.[CH]) # parse line numbers from grep output: # : contents - lines=$(git grep --cached -hn -e '^.\{81,\}' \ + lines=$(git grep -hn -e '^.\{81,\}' \ --and --not -e "^ *#" \ - -- "$f" | + $scope"$f" | sed -e 's@:.*@@' | tr '\n' ' ' )