Use Github with Mercurial

A week or two ago I broke one of my git repositories. Because the solution cost me a lot of time, too many nerves and two commits, I have now returned to Mercurial. This tool may not be as powerful as Git, but the error messages and documentation are much easier to understand in my opinion.

However, I still want to use Github because of its widespread use. Be it to publish something there myself or to participate in third-party projects. Mercurial offers a plugin called hg-git for this purpose. With it, you can download a Git repository and it is automatically converted into a Mercurial repository locally. If you now make changes, they are then changed to be compatible with Git before being uploaded to a Git repository. Setting up hg-git is also quite simple.

For the following instructions, I assume that both Mercurial and python-dulwich are installed on the computer and that the directory Projects exists in the home directory. Of course, you can give the directory a different name.

First, change to the Projects directory in the terminal emulator of your choice and download the hg-git files there.

cd ~/Projects
hg clone hg-git

Alternatively, you can install hg-git via the package management of your distribution.

Next, add the following content to the .hgrc file in the home directory. If the file does not yet exist, simply create it and enter the three lines.

hgext.bookmarks =
hggit = ~/Projects/hg-git/hggit

Line two activates the plugin Bookmarks, which in this case simulates branches. Line three activates the hg-git plugin that we just downloaded.

In the Projects directory, we now download a Git repository with the following command (please adapt the Github address and the target directory fryboyter.git to your own circumstances).

hg clone fryboyter.hg

Because this converts from Git to Merurial, it may take some time depending on the size of the repository.

Next, change to the directory in which the Mercurial repository now created is located. In this example, this is fryboyter.hg. Finally, execute the command hg bookmark -f main there. Instead of main, you must enter the name of the main branch. Otherwise Mercurial will not recognise any changes during a push.

Now everything should work. In my tests, I was able to pull from Github and push to Github without any problems. That’s all I really need in my case. What I noticed during the whole process is that the local Mercurial repository is less than 30 MB in size. The local Git repository, on the other hand, takes up a bit more than 60 MB of space. That’s not the end of the world, but it’s still an interesting detail.

OSBN | General

Hooks under Mercurial

Yesterday I had published an article about the version control Mercurial. The project in question was managed with Git. There were also some hooks present. For example, you can use them to execute commands automatically when a new commit is uploaded. For this you use the hook “post-receive” as I recently described it in an article. Tux also commented that this is also possible with Mercurial.

Absolutely correct in itself, but either the documentation is in need of improvement or I didn’t get it. I assume the latter at this point.

Under the whole thing is described. Every time I add a commit, a hook should be executed. So I entered the following in the configuration file hgrc.

commit = /pfad/zur/Datei/commithook

Under /path/to/file/ I created the commithook file and entered the script that should be executed. Then I made the file executable.

But it didn’t work. The commits arrived, but the hook was not executed. After some Google-Fu I noticed why. The commit hook only seems to work if you commit it directly in the repository. Not if you push it in, though.

Therefore you have to enter the following into the file hgrc.

changegroup = /pfad/zur/Datei/commithook

Herewith the hook is always executed when committing.

OSBN | General

Convert Git to Mercurial

It often happens that someone switches from one version control system to Git. Sometimes, however, someone wants to switch from Git to another solution. Recently I had such a request. Mercurial was wanted.

The relevant git repository was still quite fresh, but there were already commits in the low three-digit range. Too many to create manually in Mercurial.

Which is not necessary. Mercurial offers the possibility to convert.

Let’s assume that the home directory contains the directory repository. In this directory there is the directory blog.git where the Git repository is located.

First you have to create the subdirectory blog.hg in the repository directory and switch to it. There you create a Mercurial repository with “hg init”. This creates the directory .hg. In this directory you create the file hgrc with the following content.


This enables the extension with which the conversion can be performed.

Now execute the following command in the directory blog.hg.

hg convert --datesort ~/repository/blog.git ~/repository/blog.hg

It’ll take a while. But if everything goes as planned, you should get a Mercurial repository with all commits, files, etc. as in the Git repository. If you look at the directory blog.hg, you will still only see the subdirectory .hg. The problem can be solved by executing the command “hg update”. If you then look in the directory blog.hg you can also see all the files you originally added to the Git repository.

OSBN | General