From ae48c8ee9ecf7866e4fe1ca4d3390d69a1adc2b4 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Wed, 17 Mar 2010 21:04:54 +0100 Subject: [git-tools] Improvements for the pre-commit hook One major issue with the pre-hook so far was partially staged files, because it used filenames for php lint. These changes will make the hook read the file contents from the index instead. Great thanks to David Soria Parra. --- git-tools/hooks/pre-commit | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) (limited to 'git-tools') diff --git a/git-tools/hooks/pre-commit b/git-tools/hooks/pre-commit index 6129470196..1c67a0f3e3 100755 --- a/git-tools/hooks/pre-commit +++ b/git-tools/hooks/pre-commit @@ -25,12 +25,35 @@ fi error=0 -# get a list of staged .php files, omitting file removals -IFS=" " -for file in $(git diff --cached --name-status $against | grep -v -E '^D' | cut -f2 | grep -E '\.php$') +IFS=$'\n' +# get a list of staged files +for line in $(git diff-index --cached --full-index $against) do - # hide output, but show errors - if ! $PHP_BIN -l "$file" >/dev/null + # split needed values + sha=$(echo $line | cut -d' ' -f4) + temp=$(echo $line | cut -d' ' -f5) + status=$(echo $temp | cut -d' ' -f1) + filename=$(echo $temp | cut -d' ' -f2) + + # file extension + ext=$(echo $filename | sed 's/^.*\.//') + + # only check files with php extension + if [ $ext != "php" ] + then + continue + fi + + # do not check deleted files + if [ $status = "D" ] + then + continue + fi + + # check the staged file content for syntax errors + # using php -l (lint) + git cat-file -p $sha | $PHP_BIN -l >/dev/null + if [ $? -ne 0 ] then error=1 fi -- cgit v1.2.1