Jun 20, 2012

Moving a repo from SVN to Hg

I currently use SVN (aka Subversion) with my projects hosted on ProjectLocker. For a variety of reasons I got interested in Hg (aka Mercurial) for my source code control. One big impediment to switching source code control systems is losing your existing history. Here are the steps I went through to move all of my current revisions for a single project from Subversion to Hg.

Moving the History

I originally thought I could sync directly from ProjectLocker to a local Hg repository but it didn't work, I had to create a local copy of my svn repo. In the steps below I'm using the actual paths I used just for simplicity, obviously your path names will vary. [Note: The steps below are best if you just want to get one project out of an svn repo and move it to Hg. If you're going to move a bunch a projects, there's a more efficient process that I'll go over in an upcoming post. It will still help you to read this post to be familiar with the detailed steps. The alternate approach only changes a few things].

  1. Commit any changes still outstanding up to your old svn repo.
  2. Create a folder for your local copy of your old svn repo G:\Test\PulseGenLib
  3. Overview of this step: Open a Command prompt and move to that folder. Use svnadmin to create an empty repository. Create an empty pre-revprop-change.bat hook file. Here are the explicit commands: 
    1. cd G:\Test\PulseGenLib
    2. svnadmin create G:\Test\PulseGenLib
    3. touch  pre-revprop-change.bat
  4. Move this new empty hook  file into G:\Test\PulseGenLib\hooks
  5. Back in the Command Prompt initialize the new svn repo to the single project of interest. Then you will use svnsync to download all the revisions. This points out one of the weaknesses of a SCC system like SVN. Even if the project you're interested in is new with only a handful of revisions the sync has to walk through all the revisions. In my case that was only 1500 but it still took about 20 minutes.  Thank goodness I didn't move the history from my own svn server when I started on ProjectLocker.
    1. svnsync init file:///G:/Test/PulseGenLib https://free1.projectlocker.com/Company/svn/Folder/Project
      (Of course this should be a real path to your project on your host provider. I usually just use TortoiseSvn and right-click on my local project folder and select Repo-Browser to make sure I get the proper path. You should see – "copied properties for revision 0 after executing this.)
    2. svnsync sync file:///G:/Test/PulseGenLib
  6. Start the TortoiseHG Workbench.  Select File –> Settings. Select Extensions from  from the list on the left. Check the 'convert' checkbox and click OK. You'll only need to do this once as this method does it in the  Global settings.
  7. Go back to the command prompt
    1. G:
    2. cd G:\Test
    3. hg convert file:///G:/Test/PulseGenLib

    You should see some converting messages and a revision number for every commit in your project. (Not every commit in the SVN repo).  Your _hg folder will now have all the revisions.

  8. Using TortoiseHg you can right click on the folder and do an Update. Examine the structure to make sure the structure imposed by your svn cloning is to your liking. If not you can move files and folders around by right-clicking and using the TortoiseHg->Rename File… command.
  9. Log in to your BitBuckets account and create a new repository. 
  10. I use ssh (so I don't have to type names and passwords – but you can use https ) and since I've previously used PuttyGen to generate keys I fire up Pageant (if it isn't running) and right click and add my key. Then I can grab the ssh URL from BitBucket and push this repository up. That looks something like:
    hg push ssh://hg@bitbucket.org/username/target_repo.

 

Switching Over Your Eclipse Project Folder

I use both Eclipse (for embedded C++ work) and Visual Studio for C# and related PC development work. I do all the above steps in new folders and haven't yet touched my development folders. Now it's time to get the folder usable by Eclipse. (I'll save Visual Studio for another post). I wanted to gradually start using projects from Hg instead of SVN so I wanted  to keep using the same Eclipse workspace.

  1. Open Eclipse and  right-click the old project and select delete. If you want to move the folder to a safe backup don't delete the files on disk but you will want to then move the old folder off to a safe backup location.
  2. Using Windows explorer create a new folder with the same name as the old folder in the same directory.
  3. Right-click and use TortoiseHg->Clone…  Paste in the URL provided by BitBucket into the Source text box. Verify the destination is correct in the destination text box and then click Clone.
  4. Go back to Eclipse and select File->Import… Under General select the Existing Projects into Workspace option.  Select the Select root directory: radio button and browse to the newly cloned project directory. Do NOT select the Copy projects into workspace check box.  Click the Finish button.
  5. Go back to Windows Explorer and right click on the project folder and  use TortoiseHg->Edit Ignore Filter I typically add Release and Debug and a special NetBurner file called htmldata.cpp. Also I use a PowerShell script and a prebuild step to create a version.h file that I don't want in the repo so I add that as well.  Add the ignore filter to the repo  and do a commit.
  6. Back to Eclipse, right-click Team->Share Project , select Mercurial
  7. Right click project Team->Push. Enter the ssh url. For some reason my username field always comes up with "hg" so I type a username BitBucket will recognize and then click Finish or Next. The username isn't used for authentication but BitBucket does use it so show who did the commit.

Troubleshooting

My first attempt at running svnsync appeared to fail. It stopped after 359 revisions.  I  used task manager to find the process called svnsync and kill it.  The destination repository was then locked so I had to use

     svn pd svn:sync-lock --revprop -r 0 file:///G:/Test/PulseGenLib_hg

to unlock it.

Turns out I just wasn't being patient enough. Revision 359 has a gobsmack of data in it so it takes a while (5 minutes or more) for the sync process to filter through it. 

About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.