Skip to content

Instantly share code, notes, and snippets.

@clubdesarrolladores
Created November 13, 2014 19:18
Show Gist options
  • Save clubdesarrolladores/851c171fe8a1899281d8 to your computer and use it in GitHub Desktop.
Save clubdesarrolladores/851c171fe8a1899281d8 to your computer and use it in GitHub Desktop.
git hooks
#!/bin/bash
# Put this file at: .git/hooks/post-checkout
# and make it executable
# You can install it system wide too, see http://stackoverflow.com/a/2293578/685587
PREV_COMMIT=$1
POST_COMMIT=$2
GIT_DIR=$(git rev-parse --git-dir)
GIT_DIR_MERGE="$GIT_DIR"/rebase-merge
GIT_DIR_APPLY="$GIT_DIR"/rebase-apply
GIT_MERGE_REBASE=false
[[ (-d "$GIT_DIR_MERGE" && -f "$GIT_DIR_MERGE/interactive") || -d "$GIT_DIR_APPLY" ]] && GIT_MERGE_REBASE=true
NOCOLOR='\e[0m'
REDCOLOR='\e[37;41m'
function composer.lock {
echo -e "$REDCOLOR composer.lock has changed: running composer install $NOCOLOR"
COMPOSER=
if [ -f composer.phar ]; then
COMPOSER="php composer.phar"
fi
which composer > /dev/null 2>&1
if [ $? ]; then
COMPOSER="composer"
fi
if [[ $GIT_MERGE_REBASE = false && -n "$COMPOSER" ]]; then
$COMPOSER install
fi
}
function package.json {
echo -e "$REDCOLOR package.json has changed: running npm install $NOCOLOR"
which npm > /dev/null 2>&1
if [[ $GIT_MERGE_REBASE = false && $? ]]; then
npm install
fi
}
FUNCS=$(declare -F -p | cut -d " " -f 3)
for FUNC in $FUNCS
do
DIFF=$(git diff --shortstat $PREV_COMMIT..$POST_COMMIT $FUNC 2>/dev/null)
if [[ $DIFF != "" ]]; then
$FUNC
fi
done
#!/usr/bin/php
<?php
echo PHP_EOL;
// output a little introduction
echo '>> Starting unit tests' . PHP_EOL;
// get the name for this project; probably the topmost folder name
$projectName = basename(getcwd());
// execute unit tests (it is assumed that a phpunit.xml configuration is present
// in the root of the project)
exec('phpunit -c app', $output, $returnCode); // cwd is assumed here
// if the build failed, output a summary and fail
if ($returnCode !== 0) {
// find the line with the summary; this might not be the last
while (($minimalTestSummary = array_pop($output)) !== null) {
if (strpos($minimalTestSummary, 'Tests:') !== false) {
break;
}
}
// output the status
echo '>> Test suite for ' . $projectName . ' failed:' . PHP_EOL;
echo $minimalTestSummary;
echo chr(27) . '[0m' . PHP_EOL; // disable colors and add a line break
echo PHP_EOL;
// abort the commit
exit(1);
}
echo '>> All tests for ' . $projectName . ' passed.' . PHP_EOL;
echo PHP_EOL;
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment