Skip to contentSkip to conversation

can I use multiple worktree in same git repo ?

DISCUSSION J. Bakshi 6 participants 9 messages Jun 15, 2016

lore
Share & tools
lore

J. Bakshi

author
#0 lore raw

Hello list,

Please bear with me as I am from svn domain and gradually making myself aware of git.

I have a site under htdocs which is modified by svn with the help of post-commit hook. But the svn folder structure is little different than the actual structure at filesystem. Under htdocs/demo, say I have dir1, dir2 etc... where in svn I have the structure as

[svn]
mysite-repo

|
|----------dir1------|trunk----src (here actually I have the contents of dir1)
|----------|---------|tags
|----------|---------|branches
|
|----------dir2------|trunk----src (here actually I have the contents of dir2)
|----------|---------|tags
|----------|---------|branches

And in file-system dir1 is mapped with (checkout from) and then (svn switch --relocate) mysite-repo/dir1/trunk/src ; dir2 is checkout from and then (svn switch --relocate) mysite-repo/dir2/trunk/src
and so on .....

Now the post-commit hook is tricky.

```````
#!/bin/bash

set -e

# LANG=en_US.UTF-8 for special character support

LANG=en_US.UTF-8 /usr/bin/svn up  /var/www/demo/dir1

LANG=en_US.UTF-8 /usr/bin/svn up  /var/www/demo/dir2

LANG=en_US.UTF-8 /usr/bin/svn up  /var/www/demo/dir3
````````````````

As a result whenever there is a commit , it updates the related folder in filesystem. Say a commit at svn->mysite-repo->dir1->trunk->src ==> modify ==> /var/www/demo/dir1

Can I do the same in git with multiple worktree ? possible ?

Thanks

#1 lore raw
Quoted 4 lines · J. Bakshi

As a result whenever there is a commit , it updates the related folder in filesystem. Say a commit at svn->mysite-repo->dir1->trunk->src ==> modify ==> /var/www/demo/dir1

Can I do the same in git with multiple worktree ? possible ?

Using multiple worktree with the same repo won't work in git because the repo also have worktree-related information. But you can create a central, bare repository, then make several clones from that. Each one is checked out with different branches. You work on a clone and push to the central repo. The post-commit hook in the central repo will do pull from it, for example

for site in dir1 dir2 dir3; do

    cd /var/www/$site && /usr/bin/git pull

done

Also have a look at git-new-workdir in contrib directory of git repository. I don't use it but you might find it useful.

Signature · 1 line
Duy
#2 lore raw
Quoted 13 lines · On Wed, 20 Jul 2011 20:25:55 +0700 Nguyen Thai Ngoc Duy <[email protected]>…
Quoted 4 lines · J. Bakshi

As a result whenever there is a commit , it updates the related folder in filesystem. Say a commit at svn->mysite-repo->dir1->trunk->src ==> modify ==> /var/www/demo/dir1

Can I do the same in git with multiple worktree ? possible ?

Using multiple worktree with the same repo won't work in git because the repo also have worktree-related information. But you can create a central, bare repository, then make several clones from that. Each one is checked out with different branches. You work on a clone and push to the central repo. The post-commit hook in the central repo will do pull from it, for example

Quoted 4 lines

for site in dir1 dir2 dir3; do

    cd /var/www/$site && /usr/bin/git pull

done

Thanks for your response. I am afraid that I can't understand the approach clearly but I must say that I am hopeful to see a direction. Obviously there will be a central git repo which will have its worktree under htdocs at the same server. That's why the post-receive hook will update the website after each push. But the issue is mapping the structure at git which is different than the structure at filesystem. And a push should reflect to its correct folder only.

[git]->[mysite]->dir1/trunk/src => physically map => htdocs/demo/dir1 [git]->[mysite]->dir2/trunk/src => physically map => htdocs/demo/dir2

Please tell me with little more clarification, how can I do this ?

-with kind regards

#3 lore raw
Quoted 2 lines · J. Bakshi

Thanks for your response. I am afraid that I can't understand the approach clearly but I must say that I am hopeful to see a direction. Obviously there will be a central git repo which will have its worktree under htdocs at the same server.

No, the central repository does not need worktree attached (in other words "bare repository"). You modify in a clone from it and push to/pull from it.

Quoted 9 lines

That's why the post-receive
hook will update the website after each push. But the issue is mapping the structure at git which is different than the structure at filesystem. And a push should reflect to its correct folder only.

[git]->[mysite]->dir1/trunk/src => physically map => htdocs/demo/dir1 [git]->[mysite]->dir2/trunk/src => physically map => htdocs/demo/dir2

Please tell me with little more clarification, how can I do this ?

OK, let's create a central repository

GIT_DIR=/somewhere/safe/repo.git git init --bare

Then clone it where development happens

git clone /somewhere/safe/repo.git ~/dev
cd ~/dev

Now we create "dir1" and "dir2" branches, correponding to what you have in the original tree

git checkout -b dir1

# put contents of dir1/trunk/src here, commit
git checkout -b dir2 master
# put contents of dir2/trunk/src here, commit

Then we push everything back to central repo

git push origin +dir1:dir1 +dir2:dir2

Then we clone it to demo sites

cd /var/www
git clone /somewhere/safe/repo.git dir1
git checkout dir1

cd /var/www
git clone /somewhere/safe/repo.git dir2
git checkout dir2

Now you can add a post commit hook to /somewhere/safe/repo.git to move to /var/www/dir[12] and do a "pull". When you push again from ~/dev, /var/www/dir[12] should get updated. Is that what you want?

Signature · 1 line
Duy
author
#4 lore raw
Quoted 27 lines · On Thu, 21 Jul 2011 10:20:13 +0700 Nguyen Thai Ngoc Duy <[email protected]>…
Quoted 2 lines · J. Bakshi

Thanks for your response. I am afraid that I can't understand the approach clearly but I must say that I am hopeful to see a direction. Obviously there will be a central git repo which will have its worktree under htdocs at the same server.

No, the central repository does not need worktree attached (in other words "bare repository"). You modify in a clone from it and push to/pull from it.

Quoted 9 lines

That's why the post-receive
hook will update the website after each push. But the issue is mapping the structure at git which is different than the structure at filesystem. And a push should reflect to its correct folder only.

[git]->[mysite]->dir1/trunk/src => physically map => htdocs/demo/dir1 [git]->[mysite]->dir2/trunk/src => physically map => htdocs/demo/dir2

Please tell me with little more clarification, how can I do this ?

OK, let's create a central repository

GIT_DIR=/somewhere/safe/repo.git git init --bare

Then clone it where development happens

git clone /somewhere/safe/repo.git ~/dev
cd ~/dev

Quoted 8 lines

Now we create "dir1" and "dir2" branches, correponding to what you have in the original tree

git checkout -b dir1

# put contents of dir1/trunk/src here, commit
git checkout -b dir2 master
# put contents of dir2/trunk/src here, commit

Just here, just here... The git should have dir1/trunk/src which should update /var/www/demo/dir1 and dir2/trunk/src which should update /var/www/demo/dir2

Thanks for your detailed clarification, very nice....

#5 lore raw
In reply to Nguyen Thai Ngoc Duy
Quoted 8 lines · Nguyen Thai Ngoc Duy
Quoted 4 lines · J. Bakshi

As a result whenever there is a commit , it updates the related folder in filesystem. Say a commit at svn->mysite-repo->dir1->trunk->src ==> modify ==> /var/www/demo/dir1

Can I do the same in git with multiple worktree ? possible ?

Using multiple worktree with the same repo won't work in git because the repo also have worktree-related information. [...]

[...]

Quoted 2 lines

Also have a look at git-new-workdir in contrib directory of git repository. I don't use it but you might find it useful.

Actually with git-new-workdir you can have multiple working directories associated with single repository, by the way of symlinks.

Note however that if you intend to *work* in those workdirs, they better correspond to different branches... or you can mess something heavy.

Signature · 1 line
Jakub Narebski
#6 lore raw
Quoted 14 lines · Jakub Narebski
Quoted 8 lines · Nguyen Thai Ngoc Duy
Quoted 4 lines · J. Bakshi

As a result whenever there is a commit , it updates the related folder in filesystem. Say a commit at svn->mysite-repo->dir1->trunk->src ==> modify ==> /var/www/demo/dir1

Can I do the same in git with multiple worktree ? possible ?

Using multiple worktree with the same repo won't work in git because the repo also have worktree-related information. [...]

[...]

Quoted 2 lines

Also have a look at git-new-workdir in contrib directory of git repository. I don't use it but you might find it useful.

Quoted 6 lines

Actually with git-new-workdir you can have multiple working directories associated with single repository, by the way of symlinks.

Note however that if you intend to *work* in those workdirs, they better correspond to different branches... or you can mess something heavy.

It would be possible to use the .git-file feature here. Then symlinks are avoided and the solution is portable.

Med vänliga hälsningar
Fredrik

#7 lore raw
Quoted 11 lines · Fredrik Gustafsson
Quoted 6 lines · Jakub Narebski

Actually with git-new-workdir you can have multiple working directories associated with single repository, by the way of symlinks.

Note however that if you intend to *work* in those workdirs, they better correspond to different branches... or you can mess something heavy.

It would be possible to use the .git-file feature here. Then symlinks are avoided and the solution is portable.

Actually it wouldn't. Each new workdir has a separate HEAD and a separate index; it is contents of .git that is symlinked, not .git itself.

Signature · 2 lines
Jakub Narebski
Poland
#8 lore raw
Quoted 11 lines · Jakub Narebski <jnareb <at> gmail.com> writes:
Quoted 9 lines · Fredrik Gustafsson
Quoted 6 lines · Jakub Narebski

Actually with git-new-workdir you can have multiple working directories associated with single repository, by the way of symlinks.

Note however that if you intend to *work* in those workdirs, they better correspond to different branches... or you can mess something heavy.

Quoted 7 lines
Quoted 2 lines

It would be possible to use the .git-file feature here. Then symlinks are avoided and the solution is portable.

Actually it wouldn't. Each new workdir has a separate HEAD and a separate index; it is contents of .git that is symlinked, not .git itself.

Is there any means to separate worktree info from the object repository? E.g., having a .git dir identical to that of a bare repo and a .git_workdir dir with the head, head logs, index and all what makes a nonbare repo different from a bare one? That might make it easier to transform a repo from bare to nonbare in addition to simplifying the logic of git-new-workdir.