Home > Education > Subrepositories in Mecurial (hg)

Subrepositories in Mecurial (hg)

December 3rd, 2011 Leave a comment Go to comments

So I initially created a mercurial repository to house my research related publications, presentations, and thesis. However, as these publications generally have coauthors, sharing parts of my repository was necessary. However, I did not want to run two repositories, and would ideally want to keep my current repository structure as it makes sense for me. On the other hand, I do not want to share my whole publication repository with all my coauthors, when what they are really interested in, is the particular publication we are working on. I do however want my collaborators to view my ‘hg log’ history. Fortunately, mercurial (hg) actually can account for this through use of subrepositories. However there is a bit of tricky in setting things up.

1. First, I generated a separate mercurial repository using the current repository, but only for the desired subfolder. The following is a bash shell script that handles moving the logs and desired files over. The first argument is the subfolder you want to make into a repository. The second argument is the path to the parent directory. The third argument is the desired location of the new repository.

echo include $1 > /tmp/myfilemap
echo rename $1 . >> /tmp/myfilemap
hg convert --filemap /tmp/myfilemap $2 $3

2. Next the subfolder is removed first from the parent repoistory via ‘hg remove path/to/subfolder’.
3. Following the instructions from the mercurial wiki, one goes to the root directory of the parent repository and create the necessary .hgsub file. In my case, my subfolder was 2 levels down, so the following was used. The paths are relative the the root of the parent directory.

folder/subfolder = folder/subfolder

4. ‘hg add .hgsub’ in the parent directory.
5. ‘hg clone subfolder’ in the folder/subfolder.
6. ‘hg commit -m “description here”‘

At this point, it seems everything works as desired. Pulling, updating, and cloning the root directory records the changes of the subfolder, while the subfolder can itself be updated/pulled/and pushed in accordance with collaborators.