How do I copy a migrated branch to another directory?

I have a branch master

(Production) and another one is development

. When I push a commit from master the execution after receiving is executed:

git --work-tree=/var/www/mywebsite.com --git-dir=/var/repo/mywebsite.com.git checkout -f

      

And the site is alive. But I want my branch to development

be copied to / var / www / mywebsite.com.dev.

+3


source to share


1 answer


What you are trying to do manually is what Capistrano is for and they will do it better (for For example, your version is not atomic, so the old / new is added on your website for a while, and capistrano checks your tree in a directory and then specifies a symbolic link to it, which is atomic).

If you stay with "git checkout" then "git checkout development" will checkout the development branch, so you want to run this command when you get a ref update for development. The script will go along the lines:

#!/bin/sh

while read old new refname
do
    case "$refname" in
        refs/heads/master)
            git --work-tree=/var/www/mywebsite.com --git-dir=/var/repo/mywebsite.com.git checkout -f
            ;;
        refs/heads/development)
            git --work-tree=/var/www/mywebsite.com.dev --git-dir=/var/repo/mywebsite.com.git checkout -f refs/heads/development
            ;;
    esac
done

      

We can now improve the script in several ways:



  • In fact, since the hook gets a new ref value, you don't even need the name of the branch, and $ new can be used instead (more reliable if there are multiple update updates during the update).

  • To improve efficiency, we can maintain one index file for each check (so that unmodified files do not need to be actually checked)

  • The - git -dir option can be removed as we are checking the current directory (the hook is done in the repository).

Improved script:

#!/bin/sh

while read old new refname
do
    case "$refname" in
        refs/heads/master)
            GIT_INDEX_FILE="$PWD"/index.master git --work-tree=/tmp/www/mywebsite.com checkout -f $new
            ;;
        refs/heads/development)
            GIT_INDEX_FILE="$PWD"/index.development git --work-tree=/tmp/www/mywebsite.com.dev checkout -f $new
            ;;
    esac
done

      

In relation to the linked object, you can also look at the new value updateInstead

for receive.denyCurrentBranch

, introduced in Git 2.3. See for example the "Push to deploy" section at https://github.com/blog/1957-git-2-3-has-been-released

+1


source







All Articles