From db8c557e4c6fee0a66c78863d4082dc17ff22d57 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Wed, 10 Mar 2010 20:07:10 +0100 Subject: [PATCH 01/11] [git-tools] pre-commit hook for syntax checking This pre-commit hook utilises PHP's command-line -l (lint) option, which checks for syntax errors. In case of an error the commit is rejected and the error displayed. Testing is welcome. --- git-tools/hooks/pre-commit | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100755 git-tools/hooks/pre-commit diff --git a/git-tools/hooks/pre-commit b/git-tools/hooks/pre-commit new file mode 100755 index 0000000000..4a070d130c --- /dev/null +++ b/git-tools/hooks/pre-commit @@ -0,0 +1,44 @@ +#!/bin/sh +# +# A hook to disallow php syntax errors to be committed +# by running php -l (lint) on them. It requires php-cli +# to be installed. +# +# This is a pre-commit hook. +# +# To install this you can either copy or symlink it to +# $GIT_DIR/hooks, example: +# +# ln -s ../../git-tools/hooks/pre-commit \\ +# .git/hooks/pre-commit + +# necessary check for initial commit +if git rev-parse --verify HEAD >/dev/null 2>&1 +then + against=HEAD +else + # Initial commit: diff against an empty tree object + against=4b825dc642cb6eb9a060e54bf8d69288fbee4904 +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$') +do + # store lint result in a temp file + tempfile="/tmp/$(basename $0).$$.tmp" + if ! php -l "$file" >/dev/null 2>"$tempfile" + then + error=1 + cat "$tempfile" + fi + rm -f "$tempfile" +done +unset IFS + +if [ $error -eq 1 ] +then + exit 1 +fi From 6df10358aa7f9c544b188b51d6d1e8b3e66c8a28 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Wed, 10 Mar 2010 21:37:55 +0100 Subject: [PATCH 02/11] [git-tools] use mktemp in pre-commit (thanks nn-) --- git-tools/hooks/pre-commit | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/git-tools/hooks/pre-commit b/git-tools/hooks/pre-commit index 4a070d130c..937d2d2dc0 100755 --- a/git-tools/hooks/pre-commit +++ b/git-tools/hooks/pre-commit @@ -28,13 +28,12 @@ IFS=" " for file in $(git diff --cached --name-status $against | grep -v -E '^D' | cut -f2 | grep -E '\.php$') do # store lint result in a temp file - tempfile="/tmp/$(basename $0).$$.tmp" + tempfile=$(mktemp -t "$(basename $0).XXXXXX") if ! php -l "$file" >/dev/null 2>"$tempfile" then error=1 cat "$tempfile" fi - rm -f "$tempfile" done unset IFS From f9192bed79c2d5a0c1e8388bcdc35be3d21aa9a8 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Thu, 11 Mar 2010 19:44:21 +0100 Subject: [PATCH 03/11] [git-tools] Some pre-commit enhancements, abolish tempfile --- git-tools/hooks/pre-commit | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/git-tools/hooks/pre-commit b/git-tools/hooks/pre-commit index 937d2d2dc0..6129470196 100755 --- a/git-tools/hooks/pre-commit +++ b/git-tools/hooks/pre-commit @@ -12,6 +12,8 @@ # ln -s ../../git-tools/hooks/pre-commit \\ # .git/hooks/pre-commit +PHP_BIN=php + # necessary check for initial commit if git rev-parse --verify HEAD >/dev/null 2>&1 then @@ -27,12 +29,10 @@ error=0 IFS=" " for file in $(git diff --cached --name-status $against | grep -v -E '^D' | cut -f2 | grep -E '\.php$') do - # store lint result in a temp file - tempfile=$(mktemp -t "$(basename $0).XXXXXX") - if ! php -l "$file" >/dev/null 2>"$tempfile" + # hide output, but show errors + if ! $PHP_BIN -l "$file" >/dev/null then error=1 - cat "$tempfile" fi done unset IFS From 6a9304021f41cd8319ccb009bb24792ffc5438bc Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sat, 13 Mar 2010 13:04:44 +0100 Subject: [PATCH 04/11] [git-tools] Improvements on prepare-commt-msg hook prepare-commit-hook now uses `git symbolic-ref HEAD` instead of reading $GIT_DIR/HEAD directly. This seems to be a more portable solution. Thanks to Chris (cs278/ToonArmy) for the suggestion. --- git-tools/hooks/prepare-commit-msg | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/git-tools/hooks/prepare-commit-msg b/git-tools/hooks/prepare-commit-msg index e1e05d67b8..354b539cc1 100755 --- a/git-tools/hooks/prepare-commit-msg +++ b/git-tools/hooks/prepare-commit-msg @@ -10,15 +10,25 @@ # # ln -s ../../git-tools/hooks/prepare-commit-msg \\ # .git/hooks/prepare-commit-msg -# -# Make sure it is executable. -# strip off ref: refs/heads/ -branch="$(cat $GIT_DIR/HEAD | sed 's/ref: refs\/heads\///g')" +# get branch name +branch="$(git symbolic-ref HEAD)" +# exit if no branch name is present +# (eg. detached HEAD) +if [ $? -ne 0 ] +then + exit +fi + +# strip off refs/heads/ +branch="$(echo "$branch" | sed "s/refs\/heads\///g")" + +# add [branchname] to commit message # * only run when normal commit is made (without -m or -F; # not a merge, etc.) # * also make sure the branch name begins with bug/ or feature/ -if [ "$2" = "" ] && [ $(echo "$branch" | grep -e '^\(bug\|feature\)/') ]; then - echo "[$branch] $(cat $1)" > "$1" +if [ "$2" = "" ] && [ $(echo "$branch" | grep -e "^\(bug\|feature\)/") ] +then + echo "[$branch] $(cat "$1")" > "$1" fi From ae48c8ee9ecf7866e4fe1ca4d3390d69a1adc2b4 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Wed, 17 Mar 2010 21:04:54 +0100 Subject: [PATCH 05/11] [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(-) 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 From a06ec8c7fd3313acb9a69de6ac51f4486b5f79a3 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sat, 3 Apr 2010 01:51:37 +0100 Subject: [PATCH 06/11] [git-tools] This script requires bash to run, so point directly to bash. --- git-tools/hooks/pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-tools/hooks/pre-commit b/git-tools/hooks/pre-commit index 1c67a0f3e3..929789e312 100755 --- a/git-tools/hooks/pre-commit +++ b/git-tools/hooks/pre-commit @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash # # A hook to disallow php syntax errors to be committed # by running php -l (lint) on them. It requires php-cli From 5568b2134bfc8aa477d92e573d8a7d3d13264b44 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sat, 3 Apr 2010 01:53:12 +0100 Subject: [PATCH 07/11] [git-tools] Display what parse errors were found. --- git-tools/hooks/pre-commit | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/git-tools/hooks/pre-commit b/git-tools/hooks/pre-commit index 929789e312..a7deea43c5 100755 --- a/git-tools/hooks/pre-commit +++ b/git-tools/hooks/pre-commit @@ -24,6 +24,7 @@ else fi error=0 +errors="" IFS=$'\n' # get a list of staged files @@ -52,15 +53,19 @@ do # check the staged file content for syntax errors # using php -l (lint) - git cat-file -p $sha | $PHP_BIN -l >/dev/null + result=$(git cat-file -p $sha | $PHP_BIN -l) if [ $? -ne 0 ] then error=1 + # Swap back in correct filenames + errors+=${result//in - on/"$filename"} fi done unset IFS if [ $error -eq 1 ] then + echo -e "PHP Syntax check failed:"; + echo -e "$errors" | grep "^Parse error:" exit 1 fi From b6920b7ca8a6859bf9f76247b882dbd9331a9c78 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sat, 3 Apr 2010 02:12:21 +0100 Subject: [PATCH 08/11] [git-tools] Use env to find the correct paths to binaries. --- git-tools/hooks/pre-commit | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/git-tools/hooks/pre-commit b/git-tools/hooks/pre-commit index a7deea43c5..151c604c30 100755 --- a/git-tools/hooks/pre-commit +++ b/git-tools/hooks/pre-commit @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash # # A hook to disallow php syntax errors to be committed # by running php -l (lint) on them. It requires php-cli @@ -53,7 +53,7 @@ do # check the staged file content for syntax errors # using php -l (lint) - result=$(git cat-file -p $sha | $PHP_BIN -l) + result=$(git cat-file -p $sha | /usr/bin/env $PHP_BIN -l) if [ $? -ne 0 ] then error=1 From 3e5236dcd62f1d0b18cd3c92661a2287b5bbf2d6 Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sat, 3 Apr 2010 02:13:48 +0100 Subject: [PATCH 09/11] [git-tools] Prepend the branch to the commit message for all branches. --- git-tools/hooks/prepare-commit-msg | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-tools/hooks/prepare-commit-msg b/git-tools/hooks/prepare-commit-msg index 354b539cc1..033cb187c7 100755 --- a/git-tools/hooks/prepare-commit-msg +++ b/git-tools/hooks/prepare-commit-msg @@ -28,7 +28,7 @@ branch="$(echo "$branch" | sed "s/refs\/heads\///g")" # * only run when normal commit is made (without -m or -F; # not a merge, etc.) # * also make sure the branch name begins with bug/ or feature/ -if [ "$2" = "" ] && [ $(echo "$branch" | grep -e "^\(bug\|feature\)/") ] +if [ "$2" = "" ] then echo "[$branch] $(cat "$1")" > "$1" fi From 8091e3166630c70310908668c4d35d1d8d948e0f Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sat, 3 Apr 2010 21:53:09 +0200 Subject: [PATCH 10/11] [git-tools] do not display stderr --- git-tools/hooks/pre-commit | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git-tools/hooks/pre-commit b/git-tools/hooks/pre-commit index 151c604c30..c8d78ed490 100755 --- a/git-tools/hooks/pre-commit +++ b/git-tools/hooks/pre-commit @@ -53,7 +53,7 @@ do # check the staged file content for syntax errors # using php -l (lint) - result=$(git cat-file -p $sha | /usr/bin/env $PHP_BIN -l) + result=$(git cat-file -p $sha | /usr/bin/env $PHP_BIN -l 2>/dev/null) if [ $? -ne 0 ] then error=1 From 11de6a46b10c5bdb77b2ee7f9e970b849e0f4d74 Mon Sep 17 00:00:00 2001 From: Igor Wiedler Date: Sat, 3 Apr 2010 22:06:25 +0200 Subject: [PATCH 11/11] [git-tools] add note about PHP_BIN using env --- git-tools/hooks/pre-commit | 1 + 1 file changed, 1 insertion(+) diff --git a/git-tools/hooks/pre-commit b/git-tools/hooks/pre-commit index c8d78ed490..23ab8d6cdb 100755 --- a/git-tools/hooks/pre-commit +++ b/git-tools/hooks/pre-commit @@ -12,6 +12,7 @@ # ln -s ../../git-tools/hooks/pre-commit \\ # .git/hooks/pre-commit +# NOTE: this is run through /usr/bin/env PHP_BIN=php # necessary check for initial commit