Last active
September 11, 2023 01:38
-
-
Save jeffam/17b94bf1a1dee26ce88f to your computer and use it in GitHub Desktop.
Drupal content snapshots for git branches
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 | |
/** | |
* Git post-checkout hook that maintains Drupal db snapshots for each branch. | |
* | |
* INSTALLATION: | |
* cd repo/root/directory | |
* mkdir snapshots | |
* echo "snapshots/*.sql" >> .git/info/exclude | |
* chmod +x /path/to/branch_snaps_post-checkout.php | |
* cd .git/hooks && ln -s /path/to/branch_snaps_post-checkout.php ./post-checkout | |
*/ | |
$prev_head = $argv[1]; | |
$new_head = $argv[2]; | |
$checkout_type = $argv[3]; | |
$snaps_enabled = (bool) trim(`git config branchsnaps.enable`); | |
if (!$snaps_enabled) { | |
echo "Branch snapshots are disabled. Run `git config branchsnaps.enable 1` to enable."; | |
return; | |
} | |
// We only care if we're checking out a branch from a different one. | |
if ($checkout_type == 1 && $prev_head !== $new_head) { | |
$snapshot_dir = getcwd() . '/snapshots/'; | |
$prev_branch = trim(`git name-rev --name-only $prev_head`); | |
$new_branch = trim(`git name-rev --name-only $new_head`); | |
echo "Taking snapshot of $prev_branch and restoring snapshot of $new_branch...\n"; | |
// Create a snapshot of the db for the prev branch. | |
`drush sql:dump --skip-tables-key=common --structure-tables-key=common --result-file={$snapshot_dir}{$prev_branch}.sql`; | |
// Restore the snapshot of the new branch, if it exists. | |
if (file_exists("{$snapshot_dir}{$new_branch}.sql")) { | |
`drush sql:query --file={$snapshot_dir}{$new_branch}.sql`; | |
} | |
echo "Run `git config branchsnaps.enable 0` to disable branch snapshots.\n"; | |
} | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi
Thanks for this code
I am new to git and haven't see the branchsnaps.enable before and can not find it on google