Skip to content

Instantly share code, notes, and snippets.

@ibrahimlawal

ibrahimlawal/README.md

Last active Nov 22, 2019
Embed
What would you like to do?
My PHP libraries Git hooks

PRE-REQUISITES

  • php in your path
  • git
  • your php projects should have phpunit installed

INSTALL

  • download all files, rename them removing the .sh extension.
  • chmod +x each of them
  • copy git-mrm and git-ddev to a folder in your executable path e.g. /usr/local/bin
  • copy pre-commit and pre-push to the .git/hooks folder of any PHP projects you want to work with.

USAGE

  • the pre-commit hook will be run every time you attempt a commit
  • the pre-push hook will be run every time you attempt a push
  • you can use the shortcut git mrm to:
    • switch from your working branch to master
    • pull remote master
    • switch to your working branch
    • merge the now-updated master into your working branch
    • push your working branch to a remote branch named after it
  • you can use the shortcut git ddev to:
    • do all git mrm things
    • switch to your dev branch
    • pull remote dev
    • merge your working branch into dev
    • push dev to remote dev
# push to dev after merging a rebased current branch onto remote master (requires mrm)
# update remote and remote dev afterwards
# Author: @ibrahimlawal
branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
if [ -z "$branch_name" ]; then
echo Not on a branch, deploy aborted && exit 1
fi
if [ $branch_name = "master" ] || [ $branch_name = "dev" ]; then
echo On $branch_name, deploy aborted
exit 1
fi
git mrm && git checkout dev && git pull && git merge $branch_name --log --no-edit && git push -u origin dev && git checkout $branch_name && git push -u origin $branch_name
# merge master onto current branch after pulling remote master
# update remote afterwards
# Author: @ibrahimlawal
branch_name=$(git symbolic-ref -q HEAD)
branch_name=${branch_name##refs/heads/}
if [ -z "$branch_name" ]; then
echo Not on a branch, merging remote master aborted
exit 1
fi
if [ $branch_name = "master" ]; then
echo On $branch_name, merging remote master aborted
exit 1
fi
git checkout master && git pull && git checkout $branch_name && git merge master && git push --set-upstream origin $branch_name
#!/bin/sh
RED='\033[0;31m'
NC='\033[0m'
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
########################### CHECK FOR PHP SYNTAX ERRORS ###########################
if !(git diff --cached --name-only --diff-filter=AM $against | grep 'php' | xargs -P 10 -n1 php -l)
then
echo "${RED}"
echo "Found WIP commit in $local_ref, not pushing"
echo "'$commit' has message: \n" `git log -n 1 --pretty=format:%s $commit`
echo "${NC}"
exit 1
fi
###################### RUN PHPUNIT AND EXIT IF ANY TEST FAILS ######################
PHPUNIT_BIN=./vendor/bin/phpunit
if [ ! -x $PHPUNIT_BIN ]; then
echo "${RED}PHPUnit bin not found or executable -> $PHPUNIT_BIN${NC}"
exit 1
fi
UOUTPUT=$($PHPUNIT_BIN)
URETVAL=$?
if [ $URETVAL -ne 0 ]; then
echo "${RED}$UOUTPUT${NC}"
exit $URETVAL
fi
########################### CHECK FOR WHITESPACE ERRORS ###########################
exec git diff-index --check --cached $against --
#!/bin/sh
# An example hook script to verify what is about to be pushed. Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed. If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
# <local ref> <local sha1> <remote ref> <remote sha1>
#
remote="$1"
RED='\033[0;31m'
NC='\033[0m'
url="$2"
z40=0000000000000000000000000000000000000000
while read local_ref local_sha remote_ref remote_sha
do
if [ "$local_sha" = $z40 ]
then
# Handle delete
:
else
if [ "$remote_sha" = $z40 ]
then
# New branch, examine all commits compared to master
range="master..$local_sha"
else
# Update to existing branch, examine new commits
range="$remote_sha..$local_sha"
fi
# Check for WIP commit
commit=`git rev-list -n 1 --grep '^WIP' "$range"`
if [ -n "$commit" ]
then
echo "${RED}"
echo "Found WIP commit in $local_ref, not pushing"
echo "'$commit' has message: \n" `git log -n 1 --pretty=format:%s $commit`
echo "${NC}"
exit 1
fi
# Check for PHP syntax errors
echo "${RED}"
phpsyntaxerrors=`git diff-tree --no-commit-id --name-only -r "$range" | grep 'php' | xargs -P 10 -n1 php -l`
echo "${NC}"
if [[ $phpsyntaxerrors == *"error: "* ]]
then
echo "${RED}Please fix the syntax error(s) and retry the commit.${NC}"
exit 1
fi
fi
done
@ibrahimlawal

This comment has been minimized.

Copy link
Owner Author

@ibrahimlawal ibrahimlawal commented May 18, 2017

Tests for (in this order):

  1. Syntax errors
  2. Failed PHPUNit tests
  3. PSR2 compliance in committed files
  4. Whitespace errors in committed files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment