Skip to content

Instantly share code, notes, and snippets.

@abtris
Created April 6, 2013 06:07
Show Gist options
  • Save abtris/5325068 to your computer and use it in GitHub Desktop.
Save abtris/5325068 to your computer and use it in GitHub Desktop.
Gitolite hooks to check php syntax, with xml and git-notify to jenkins
#!/bin/bash
NOBOLD="\033[0m"
BOLD="\033[1m"
BLACK="\033[30m"
GREY="\033[0m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
MAGENTA="\033[35m"
CYAN="\033[36m"
WHITE="\033[37m"
# V +1007
# Peff helped:
# http://thread.gmane.org/gmane.comp.version-control.git/118626
syntax_check_php="php -l"
syntax_check_xml="xmllint --noout"
tmp=$(mktemp /tmp/git.update.XXXXXX)
log=$(mktemp /tmp/git.update.log.XXXXXX)
tree=$(mktemp /tmp/git.diff-tree.XXXXXX)
git diff-tree -r "$2" "$3" > $tree
echo
echo diff-tree:
cat $tree
exit_status=0
while read old_mode new_mode old_sha1 new_sha1 status name
do
# skip lines showing parent commit
test -z "$new_sha1" && continue
# skip deletions
[ "$new_sha1" = "0000000000000000000000000000000000000000" ] && continue
# Only test .php files
if [[ $name =~ [.]php$|phtml$|inc$ ]]
then
git cat-file blob $new_sha1 > $tmp
set -o pipefail
$syntax_check_php $tmp 2>&1 | sed 's|/tmp/git.update.......:\([0-9]*\)$|JOJOMOJO:\1|'> $log
if [[ $? != 0 ]]
then
echo
echo -e "$(cat $log | sed 's|JOJOMOJO|'\\${RED}${name}\\${NOBOLD}'|')" >&2
echo -e "For more details run this: ${CYAN} git diff $old_sha1 $new_sha1 ${NOBOLD}" >&2
echo
exit_status=1
fi
fi
if [[ $name =~ [.]xml$|xsl(t)?$ ]]
then
git cat-file blob $new_sha1 > $tmp
set -o pipefail
$syntax_check_xml $tmp 2>&1 | sed 's|/tmp/git.update.......:\([0-9]*\)$|JOJOMOJO:\1|'> $log
if [[ $? != 0 ]]
then
echo
echo -e "$(cat $log | sed 's|JOJOMOJO|'\\${RED}${name}\\${NOBOLD}'|')" >&2
echo -e "For more details run this: ${CYAN} git diff $old_sha1 $new_sha1 ${NOBOLD}" >&2
echo
exit_status=1
fi
fi
done < $tree
rm -f $log $tmp $tree
# polling jenkins
REPOSITORY_BASENAME=$(basename "$PWD")
curl http://jenkins.firma.cz/git/notifyCommit?url=ssh://git@git.firma.cz/$REPOSITORY_BASENAME
exit $exit_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment