Extracting Part of Mercurial Repository

As I started to work with Mercurial, I added almost everything in single repository. Time passed and I wanted to move some data to separate one. And this became a problem. There is no obvious way to split repository. However, there is something almost as good - exporting it.

Since I am Windows user, I will give instructions for Windows here. All things should work on Linux also but configuring it to work properly might (and will) differ. I will also assume that you have Mercurial already installed. I tested this with TortoiseHg but other clients should work also.

First step is to enable ConvertExtension on our computer. Just editing "mercurial.ini" under home folder and adding two lines will do:

[extensions]
convert =

If there is "[extensions]" sections inside already, just add "convert =" under it instead of creating new one. Home folder on Windows Vista and 7 is probably located at "C:\Users\YourName". XP users should look under "C:\Documents and settings\YourName". If you moved it somewhere else, search for it yourself. :)

Another file that we need to prepare is one used for filtering out things we do not need. Lets store it at "C:\map.txt" and add following text to it:

include "Electronics/Elsidi"
rename "Electronics/Elsidi" "."

Text in quotes is location of directory in original repository. First line includes only files at directory we wish to extract and second one moves those files to root directory (.) of new repository. Do notice that we use paths relative to root of repository and that path IS case sensitive.

I will assume here that "hg.exe" is somewhere in path so you can execute it without problem. In other cases, just write full path to it every time I write "hg". In my case this is "C:\Program Files (x86)\TortoiseHg\hg".

Last thing to do is executing conversion itself and update of new repository afterward:

hg convert C:\OriginalRepository C:\NewRepository C:\revmap.txt --filemap C:\map.txt
scanning source...
sorting...
converting...
18 Adding initial Android applications.
17 Initial push.
16 Elsidi rev3.
15 Moving Elsidi directories.
14 Elsidi rev6
13 Elsidi rev G.
12 Power (rev B).
11 Last version of UTF8.
10 Added As.
9 Adding Encoded.
8 Adding World.
7 Adding Unicode.
6 Adding In.
5 Adding Joint.
4 Adding Smokes.
3 Adding Device.
2 Adding Android.
1 Added Test.
0 Adding QText

cd C:\NewRepository

hg update
31 files updated, 0 files merged, 0 files removed, 0 files unresolved

With this you should have your new repository ready. Only files under desired path will be there (moved to root) and all history will be preserved.

Revmap.txt is here just so you can continue interrupted process. If you want to start whole process from start just delete both it (revmap.txt) and new repository's directory (C:\NewRepository).

Hope this works for you.

P.S. If you see "C:\NewRepository" with just ".hg" folder inside and nothing else, you probably forgot to execute "hg update" command.

P.P.S. This procedure will leave exported data in original repository. You might remove it yourself but do notice that you will not recover any disk space - nothing is really deleted with Mercurial.

6 thoughts to “Extracting Part of Mercurial Repository”

  1. Hi, this (and other posts by others) helped me extract a sub folder from a Repo – I had done exactly what you had done – put a major project in a large multi-project Repo then needed to supply it to someone else for use on their system. I’m using Windows (XP) for this.
    I did everything as shown – the new repo was created and I did the hg update. This showed 1 files updated rather than all (had several hundred in the source folder).
    The hg info was updated correctly. But NONE of the source and other files in the project subfolder and the subfolders of the project subfolder were copied across.

    I had to copy the entire contents and then Add them all again under commit. I’m not sure this should be necessary – no one else has mentioned it as needed. All seem to have their files copied across.

    I wonder if I have missed something (though my sequence was EXACTLY like ALL the examples I have seen.

Leave a Reply

Your email address will not be published. Required fields are marked *