When you’re developing a Ruby on Rails application on Windows, you have to learn to deal with a lot of minor issues. I used Mercurial (Hg) initially as my source control. However when time came to deploy my application, I found out that Git would have been a much better choice. Both Heroku and Engine Yard make Ruby on Rails so easy to deploy that it would be crazy not to use them. However they integrate with Git and Mercurial support seems a long way off.
So I decided to convert my Mercurial repository to Git using the hg-git Mercurial plugin and ran into a lot of issues along the way. Here is a guide to successfully getting the hg-git plugin to work on a Windows machine:
Install TortoiseHg if you haven’t already
If you’re developing on Windows and using Hg, you probably already have TortoiseHg installed. If not, go ahead and download and install TortoiseHg. Make a note of where you install TortoiseHg, since you need to make some modifications in this directory.
Install the prerequisites for the hg-git plugin
We’re going to use the hg-git Mercurial plugin to allow us to pull from and push to any Git repository. This has a dependence on Mercurial (which is handled by installing TortoiseHg in the previous step) and dulwich which is a library that provides an interface to git repos using pure Python code.
However since you most probably don’t have Python installed on your Windows machine, follow these steps to “install” the dulwich library for use by the hg-git plugin:
- Download the latest source code for dulwich (version 0.4.0 or above) and unzip it to a temporary directory
- IMPORTANT: Read the note at the bottom of the post for troubleshooting. You might need to make a change to a file in the dulwich folder in order for it to work on Windows.
- Under the newly unzipped folder, you should see a directory called “dulwich” which contains all the Python code (you should see a bunch of files with the .py extension)
- In the installation directory for your TortoiseHg installation, you should see a zip file called library.zip. Copy the dulwich folder into this zip file in the root of the zip file.
Download the source code for the hg-git Mercurial plugin
Clone the repository at http://bitbucket.org/durin42/hg-git/ to a location on your hard drive, say C:\hg\hg-git
Setup .hgrc to use the hg-git plugin
Navigate to the hg repository on your hard drive that you want to convert to Git. Under the .hg directory, edit the hgrc file to contain the following three lines:
[extensions]
hgext.bookmarks =
hggit = C:\hg\hg-git\hggit
You need to point hggit in your hgrc file to point to the subdirectory called “hggit” located in the cloned repository (see step 3 above). This folder usually only contains Python code files (with a .py extension)
Pull and push from a Git repository
That’s it! Provided your current working directory is inside the hg repository that you set up, you should be able to pull from and push to any Git repository.
Troubleshooting
1. If you’re getting a error about a function called build_tree in index.py, you need to add the line to the very top of the build_tree(path) method as follows. Once you’ve added this line, you can add the dulwich folder to zip file as in step 2:
def build_tree(path) path = path.replace ("\\", "/")
2. If you’re getting an error like “abort: The system cannot find the file specified”
- Dulwich most probably cannot find ssh in your PATH. You need to make sure to install Cygwin’s OpenSSH and that you can run “ssh” from the command line.
- Make sure that you are using the Git Bash shell as you work through this guide (Credit goes to SPR in the comments for this tip)
3. If you get “Permission denied (publickey)”, it just means that the public key required to access and push to the server cannot be found. To get around this we’re going to run the following commands:
ssh-agent ssh-add <full path to your key file>
Thanks a lot! (especially for step 2.)
I just didn’t know what to do after reading the official pages of hg-git.. Why couldn’t they just write it simply like you did.. Thanks again!
Thank you! I was receiveing the “File not found.” error because Cygwin’s OpenSSH was not installed
I use Bitvise’s Tunnelier and it seems one could alias ‘ssh’ to Tunnelier’s ‘sexec’ instead of having to install Cygwin, which I really don’t want to do. I’ll try it and report back unless anyone knows a really good reason not to.
Thanks for this post! One update: looks like Dulwich is included in recent builds of TortoiseHg. All I needed to do was clone hg-git, add it to hgrc, and go.
[…] use openssh from cygwin, double click your github private key to add it to the keychain.via A guide to getting the hg-git Mercurial plugin to work on a Windows machineWritten by Sander February 12th, 2012 at 4:09 pmPosted in Uncategorized« What Happens When You […]
[…] can find quite a few posts describing how to convert – but many of the steps mentioned in those guides are not needed if you have […]
[…] can find quite a few posts describing how to convert – but many of the steps mentioned in those guides are not needed if you have […]
Was banging my head against the “abort: The system cannot find the file specified” error. Followed this excellent article to the letter. Tried a couple of others which all suggested a similar approach. No joy. Then, finally, squirreled away in a google discussion group – the answer…
I was using the Windows command line dialog. This was the problem. Try typing ‘git’ and it works. Try typing ‘git.exe’ and it fails because the git installation actually uses ‘git.cmd’ to fire off instructions. But hggit / Dulwich / Python goes looking for ‘git.exe’, cannot find it and blows up with the all too familiar error (if only it could have displayed which file it could not find).
The solution is to use the Git Bash shell which does recognize ‘git.exe’.
Thanks, I’ve updated the article and credited you in the troubleshooting section.
[…] I had to set up the hg-git plugin for mercurial but that wasn’t too bad I followed these steps to get it to work however I have python installed so I just installed dulwich normally. Then I […]
Hi!
I followed this tutorial, and I still get the “abort: The system cannot find the file specified” error when try to work with a repository located on my filesystem (well, an SFTP drive mounted with WebDrive to be exact).
If I use the full ssh path: “git+ssh://username@address:port/folder1/folder2/” it is working then but I get the error when using the mounted drive “W:\folder1\folder2\”
I’ve installed Cygwin, but I don’t get the use Git Bash shell.
I’ve tried to install Git from “github.com” and from “git-scm.com” too. But I can’t select Git Bash shell in TortoiseHg settings (I can select cygwin_terminal though).
So how can I use Git Bash shell with TortoiseHg?
Thanks
[…] You may see some references that talk about specifying the bookmarks extension here too, but that has been built into Mercurial since v 1.8. Here are some tips about installing hg-git on Windows. […]
[…] You may see some references that talk about specifying the bookmarks extension here too, but that has been built into Mercurial since v 1.8. Here are some tips about installing hg-git on Windows. […]
[…] I found a simpler solution at http://candidcode.com/2010/01/12/a-guide-to-converting-from-mercurial-hg-to-git-on-a-windows-client/ […]
[…] You may see some references that talk about specifying the bookmarks extension here too, but that has been built into Mercurial since v 1.8. Here are some tips about installing hg-git on Windows. […]