Git post-receive hook to check each branch in different folders?

The basic setup for our Git workflow is a bare repository on a local network server with two developers pushing / pulling to it.

We would like to automatically checkout every branch that goes to another location on our local network server. To demonstrate:

Clicking "expand" branch copies to the "develop" subdirectory. Pressing the "leading" branch copies to the "master" subfolder.

The problem we are facing is to get an error message after that. Here is what we have at present:

#!/bin/bash while read oldrev newrev ref do branch=`echo $ref | cut -d/ -f3` if [ "master" == "$branch" ]; then GIT_WORK_TREE=/master git checkout -f $branch echo 'Changes pushed master.' fi if [ "develop" == "$branch" ]; then GIT_WORK_TREE=/develop git checkout -f $branch echo 'Changes pushed to develop.' fi done 

Received error:

'remote: fatal: this operation should be performed in the remote: work tree: changes are pending. ''

As expected from this error, nothing has actually been verified.

I also tried post-receive this way, but the same problem:

 #!/bin/bash while read oldrev newrev ref do branch=`echo $ref | cut -d/ -f3` if [ "master" == "$branch" ]; then git --work-tree=/master checkout -f $branch echo 'Changes pushed master.' fi if [ "develop" == "$branch" ]; then git --work-tree=/develop checkout -f $branch echo 'Changes pushed to develop.' fi done 

Can someone explain what I'm doing wrong here (feel free to explain this as you would for a 3 year old :)). Thanks.

To make the answer clearer for future readers , Torek hit him on the head. I used --work-tree=/master to try to get to the folder named "master" inside the root of my bare repo (for example, next to the "branches", "hooks", etc.). As soon as I change this to --work-tree=./master (note the dot before the slash), everything works as I expected.

+7
git git-post-receive
source share
2 answers

You get this because /master and /develop are non-existent directories:

 $ git --work-tree=/nonexistent checkout master fatal: This operation must be run in a work tree 

You can also see my answer to a question about another problem that arises with this approach , which also addresses a small error that you copied from a popular but erroneous post-reception method (using cut to analyze updated ref).

[Your phrasing also makes me wonder if you are thinking of deploying these two branches in a subdirectory in the repository (presumably --bare ) that gets clicked. This is probably not a good idea.]

Another (different) deployment method is to have a β€œreal” git tree at the deployment location. Then instead of git --work-tree=... checkout you do something like this:

 deploy() { local path=$1 branch=$2 (cd $path && unset GIT_DIR && git fetch && git checkout -f origin/$branch) } 

(untested, feel free to experiment and / or modify). This has other, slightly different trade-offs regarding disk space and update windows (which I mention in another answer).

+4
source share

you should put 'GIT_WORK_TREE = / master git checkout -f $ branch' on one line, after which it worked, after reading the message, my post-reception:

 #!/bin/sh while read oldrev newrev ref do branch=`echo $ref | cut -d/ -f3` if [ "master" == "$branch" ]; then GIT_WORK_TREE=/home/tnj/www/www.test.com git checkout -f $branch chmod -R 775 /home/tnj/www/www.test.com/ echo 'changes pushed to www.test.com' fi if [ "develop" == "$branch" ]; then GIT_WORK_TREE=/home/tnj/www/www.test.com.dev git checkout -f $branch chmod -R 775 /home/tnj/www/www.test.com.dev/ echo 'changes pushed to www.test.com.dev' fi done 

It worked great, thanks for your post! :)

+4
source share

All Articles