Last active
August 3, 2018 00:24
-
-
Save igorsantos07/4fbfaa49225a1809f99d6ab7d01fc32f to your computer and use it in GitHub Desktop.
Handy git hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
define('ROOT', dirname(dirname(__DIR__))); | |
chdir(ROOT); | |
$pending_migrations = (int)trim(`./artisan migrate:status --no-ansi | grep -c '| N'`); | |
echo "===> Pending migrations: $pending_migrations"; | |
if ($pending_migrations) { | |
$migrations = array_filter(explode("\n", `./artisan migrate:status --no-ansi | grep '| N'`)); | |
$migrations = array_map(function($line) { return ' > '.trim($line, " |N\t"); }, $migrations); | |
echo "\n".join("\n", $migrations)."\n\n"; | |
echo 'Do you want to run them? [y/N] '; | |
$input = @fopen('/dev/tty', 'r'); | |
if (!$input) { | |
die("\nWhoops, non-interactive shell. Don't forget to run the migrations!\n"); | |
} | |
while (!in_array($opt = strtolower(trim(fgets($input))), ['y','yes','n','no',''])) { | |
echo 'I said y/n! '; | |
} | |
switch ($opt) { | |
case 'y': | |
case 'yes': | |
passthru("./artisan migrate"); | |
break; | |
case 'n': | |
case 'no': | |
default: | |
echo "Don't forget them!\n"; | |
break; | |
} | |
} else { | |
echo "\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
define('ROOT', dirname(dirname(__DIR__))); | |
$phinx = 'vendor/bin/phinx'; | |
chdir(ROOT); | |
$pending_migrations = (int)trim(`$phinx status|grep -c down`); | |
echo "===> Pending migrations: $pending_migrations"; | |
if ($pending_migrations) { | |
echo "\n".`$phinx status|grep down`; | |
echo 'Do you want to run them? [y/N] '; | |
$input = @fopen('/dev/tty', 'r'); | |
if (!$input) { | |
die("\nWhoops, non-interactive shell. Don't forget to run the migrations!\n"); | |
} | |
while (!in_array($opt = strtolower(trim(fgets($input))), ['y','yes','n','no',''])) { | |
echo 'I said y/n! '; | |
} | |
switch ($opt) { | |
case 'y': | |
case 'yes': | |
passthru("$phinx migrate"); | |
break; | |
case 'n': | |
case 'no': | |
default: | |
echo "Don't forget them!\n"; | |
break; | |
} | |
} else { | |
echo "\n"; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
if [ $3 == 0 ] || [ $1 == $2 ]; then #file checkout or same refs: no need to update deps | |
exit 0 | |
fi | |
FROM=$(echo $1 | cut -c1-7) | |
TO=$(echo $2 | cut -c1-7) | |
echo "" | |
echo "Checked from $FROM into $TO. Refreshing PHP dependencies..." | |
echo "" | |
# first, updating dependencies | |
docker-compose exec -T php composer install | |
# then, checking migrations waiting to be executed. You may want to comment this if you feel safe locally | |
exec < /dev/tty # enables interactive TTY for the rest of the script (needed for the line below) | |
docker-compose exec app docker/hooks/check-migrations |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
docker-compose exec app docker/hooks/check-migrations |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Summary
post-checkout (shell)
Verifies if that's not a file (
$3
== 0) but a branch ($3
== 1) checkout and calls docker-compose to runcomposer install
on the container.Then, runs the migrations checker.
check-migrations (php)
Verifies if there's any pending Phinx migrations, and asks if it can run those. Written in PHP as it's way more readable than bash-magic, I'm sorry if you don't have PHP on your dev machine :)
Ideally, this should be executed on
post-merge
andpost-checkout
, but it could be slow. Thus, if you feel safe locally (i.e. you know what you're doing and you're not checking out branches from other devs frequently), you can keep it only insidepost-merge
.