ENH: improve speed/flexibility of pre-commit-hook

- only search indexed files (BUGFIX)
- search for long-lines is faster when regex is anchored
- use git grep logic instead of sed logic where possible

- add support for running the hook manually as well.
  For example,
      bin/tools/pre-commit-hook  applications/test src/OpenFOAM/db

- add some feedback to pre-commit hook:
  The hook can be really slow, at least let people know that something
  is happening
This commit is contained in:
Mark Olesen
2010-08-05 10:30:32 +02:00
parent fef1613ed9
commit 1866a2b0fd

View File

@ -45,6 +45,9 @@
# Note
# Using "git commit --no-verify" it is possible to override the hook.
#
# By supplying arguments to the hook, it can also be used to manually
# test the specified files/directories for standards conformance.
#
#------------------------------------------------------------------------------
die()
{
@ -58,7 +61,7 @@ die()
#-----------------------------------------------------------------------------
# Check content that will be added by this commit.
if git rev-parse --verify -q HEAD > /dev/null
if git rev-parse --verify HEAD > /dev/null 2>&1
then
against=HEAD
else
@ -66,10 +69,25 @@ else
against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi
# list of all files
fileList=$(git diff-index --name-only $against --)
unset badFiles
# called manually with arguments for the files/directories to be tested?
if [ "$#" -gt 0 ]
then
case "$1" in
-h | -help)
die "interactive usage: supply list of files/directories to check"
;;
esac
# obtain list of all specified files/directories
fileList=$(git ls-files -- $@ 2>/dev/null)
else
# list of all files to be committed
fileList=$(git diff-index --cached --name-only $against --)
fi
# echo "files: $fileList" 1>&2
unset badFiles
# join list of files with this amount of space
Indent=" "
@ -97,16 +115,18 @@ dieOnBadFiles()
#
checkIllegalCode()
{
echo "pre-commit: check bad strings/characters etc ..." 1>&2
reBad="(N""abla|"$'\t'")"
msgBad="N""abla or <TAB>"
badFiles=$(
for f in $fileList
do
# parse line numbers from this:
# path/fileName:<lineNr>: contents
lines=$(git grep --cached -n -E -e "$reBad" -- "$f" |
sed -e 's@^[^:]*:\([0-9]*\):.*@\1@' |
# parse line numbers from grep output:
# <lineNr>: contents
lines=$(git grep --cached -E -hn -e "$reBad" -- "$f" |
sed -e 's@:.*@@' |
tr '\n' ' '
)
[ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
@ -123,18 +143,22 @@ checkIllegalCode()
checkCopyright()
{
year=$(date +%Y)
echo "pre-commit: check copyright ..." 1>&2
badFiles=$(
for f in $fileList
do
# parse line numbers from this:
# path/fileName:<lineNr>: contents
# for Copyright lines without the current year
lines=$(git grep --cached -n -e Copyright -- "$f" |
sed -n \
-e '/OpenCFD/{ ' \
-e "/$year/b" \
-e 's@^[^:]*:\([0-9]*\):.*@\1@p }' |
# NB: need to have OpenCFD on a separate line to prevent
# this check being caught by itself!
#
# parse line numbers from grep output:
# <lineNr>: contents
#
lines=$(git grep --cached -F -hn -e Copyright \
--and -e OpenCFD \
--and --not -e "$year" \
-- "$f" |
sed -e 's@:.*@@' |
tr '\n' ' '
)
[ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
@ -150,16 +174,18 @@ checkCopyright()
#
checkLineLength()
{
echo "pre-commit: check line lengths ..." 1>&2
badFiles=$(
for f in $fileList
do
# limit to *.[CH] files
case "$f" in
(*.[CH])
# parse line numbers from this:
# path/fileName:<lineNr>: contents
lines=$(git grep --cached -n -e ".\{81,\}" -- "$f" |
sed -e 's@^[^:]*:\([0-9]*\):.*@\1@' |
# parse line numbers from grep output:
# <lineNr>: contents
lines=$(git grep --cached -hn -e '^.\{81,\}' -- "$f" |
sed -e 's@:.*@@' |
tr '\n' ' '
)
[ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
@ -177,18 +203,20 @@ checkLineLength()
#
checkLineLengthNonComments()
{
echo "pre-commit: check line lengths ..." 1>&2
badFiles=$(
for f in $fileList
do
# limit to *.[CH] files
case "$f" in
(*.[CH])
# parse line numbers from this (strip comment lines):
# path/fileName:<lineNr>: contents
lines=$(git grep --cached -n -e ".\{81,\}" -- "$f" |
sed -n \
-e '\@^[^:]*:[^:]*: *//.*@b' \
-e 's@^[^:]*:\([0-9]*\):.*@\1@p' |
# parse line numbers from grep output:
# <lineNr>: contents
lines=$(git grep --cached -hn -e '^.\{81,\}' \
--and --not -e "^ *//" \
-- "$f" |
sed -e 's@:.*@@' |
tr '\n' ' '
)
[ -n "$lines" ] && echo "$Indent$f -- lines: $lines"
@ -205,18 +233,20 @@ checkLineLengthNonComments()
#
checkLineLengthNonDirective()
{
echo "pre-commit: check line lengths ..." 1>&2
badFiles=$(
for f in $fileList
do
# limit to *.[CH] files
case "$f" in
(*.[CH])
# parse line numbers from this (strip comment lines):
# path/fileName:<lineNr>: contents
lines=$(git grep --cached -n -e ".\{81,\}" -- "$f" |
sed -n \
-e '\@^[^:]*:[^:]*: *#.*@b' \
-e 's@^[^:]*:\([0-9]*\):.*@\1@p' |
# parse line numbers from grep output:
# <lineNr>: contents
lines=$(git grep --cached -hn -e '^.\{81,\}' \
--and --not -e "^ *#" \
-- "$f" |
sed -e 's@:.*@@' |
tr '\n' ' '
)
[ -n "$lines" ] && echo "$Indent$f -- lines: $lines"