Tuesday 2 February 2016

Using different magento configuration files via Git Hooks

The post-checkout git hooks runs a script, in any shebang script language, after a checkout is completed.

This can be used to change the magento config based on the branch name. To do this create multiple magento configs in app/etc e.g. local.xml.branch1 & local.xml.branch2

Create the hook

$ sudo vi .git/hooks/post-checkout
insert sample post-checkout
$ sudo chmod 775 ./.git/hooks/post-checkout
$ git checkout master

This hook is invoked when a git checkout is run after having updated the worktree. The hook is given three parameters: the ref of the previous HEAD, the ref of the new HEAD (which may or may not have changed), and a flag indicating whether the checkout was a branch checkout (changing branches, flag=1) or a file checkout (retrieving a file from the index, flag=0). This hook cannot affect the outcome of git checkout.

sample post-checkout

       
#!/bin/sh

MAGE_CONFIG=$PWD/app/etc/local.xml

prev_head=$1
new_head=$2
checkout_type=$3

# 1 branch | 0 file
if [ $checkout_type -eq 0 ]; then
    echo "ignore file checkouts";
    exit;
fi

current_branch=$(git rev-parse --abbrev-ref HEAD)
prev_branch=$(git rev-parse --abbrev-ref @{-1})

if [ "$current_branch" == "$prev_branch" ]; then
 echo "branch "$current_branch" not changed";
 exit;
fi

if [ -n "$current_branch" ]
then
 echo "install config for branch "$current_branch"..."
 if [[ $current_branch =~ ^dev_upgrade_1_9_2_2$ ]]; then
     echo "cp $PWD/app/etc/local.xml.bincani_go_dev_mage_1_9_2_2 $MAGE_CONFIG"
  cp $PWD/app/etc/local.xml.bincani_go_dev_mage_1_9_2_2 $MAGE_CONFIG
 elif [[ $current_branch =~ ^master$ ]]; then
  echo "cp $PWD/app/etc/local.xml.bincani_go_dev_mage_1_9_0_1 $MAGE_CONFIG"
  cp $PWD/app/etc/local.xml.bincani_go_dev_mage_1_9_0_1 $MAGE_CONFIG
 else
  echo "no config"
 fi
 echo "done" 
fi

Testing

Remove the branch comparison check and then checkout the same branch each time.

$ git checkout master

if nothing happens check the file permissions

$ ls ./.git/hooks/post-checkout

Other hooks

applypatch-msg
pre-applypatch
post-applypatch
pre-commit
prepare-commit-msg
commit-msg
post-commit
pre-rebase
post-checkout
post-merge
pre-receive
update
post-receive
post-update
pre-auto-gc
post-rewrite
pre-push

More info here

  • https://git-scm.com/docs/githooks
  • https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks
  • http://longair.net/blog/2011/04/09/missing-git-hooks-documentation/

No comments:

Post a Comment