Applying a patch to a Repo using Mercurial

As I mentioned in a previous blog post, one of the features for my v0.1 release will be implementing a expand/collapse feature for pushloghtml. Well, turns out that ted has already started working on that. He’s already filed a bug and released a patch for it. According to him its not quite done and a couple of things need to be changed.

So, my first job was to download the patch and apply it to my repo. So I headed over to Bugzilla and looked up the bug. There were 2 patches that I needed to download…

  • One for
  • Another for gitweb_mozilla

Before you read the following take into account that I’ve never applied any kind of a patch before so this is purely from the point of a beginner. So I was looking for a way to download the patch so that I could apply it. But apparently there is no download link available on BugZIlla (it took me almost 20 mins to realize that). They should really look to put in some sort of download link/button. In the end I just ended up using the crude method of “Save Page As” to download the patch file.

Now onto applying the patch. I had no idea how to do that so I tried using tortoiseMerge since I already had that installed. It is apparently capable of applying a patch. I added the path of the diff file and the directory where I wanted it to apply the patch in the two text boxes below and then clicked the OK button. Nothing happened at all. Nothing got loaded, no error message, no nothing. This had me a bit bewildered and frustrated. tortoiseMerge was basically useless.

Now onto the next option which was hg itself. I knew that there was a way since I had read about it in some documentation when I was getting know hg at the start of my project. So I headed over to google to perform a search. I tried various things such as “apply a patch hg”, “patch hg”, “install patch hg” and “mercurial apply patch”. The last one gave me a couple of good results. One was at hgbook and another was documentation about hg queues but neither of them was a straight up guide of how to install patches. I tried many different google searches but I just couldn’t find any simple and easy guide explaining how one would install a patch using hg. Well, here I am to the rescue. The following is a simple guide that gives instructions on installing a simple patch using hg…

  1. Download the .patch or .diff file onto your machine
  2. Navigate to the repo that you want to install the patch to i.e. testRepo (I will using repo name for this example, substitute it with your repo name when you are trying to apply a patch yourself)
  3. Open testRepo.hghgrc (the config file)
  4. Add the following to hgrc and save (This enables queues for hg so that we can use commands that we need, qimport and qpush)[extensions] =
  5. Now open up a command prompt and navigate to your repo directory
  6. Type in hg qimport <full-path-of-patch-file> and press enter
  7. The above command should create a patch folder within testRepo.hgpatches
  8. Next navigate to  testRepo.hgpatches
  9. Type in hg qpush <patch-file-name>
  10. Done! The patch should have been applied (if you get an error such as “abort: local changes found, refresh first” then that means you have made changes to the original files and the patch won’t work)

In my trials and tribulations while trying to apply a patch I had to first figure out what hg queues did and how to use them. I tried to just type in hg qimport but I just got a “command does not exist” error. I didn’t realize that I had to import something until ted gave me a push in the right direction. A simple guide would have really helped.

This entry was posted in Mercurial Project, Open Source and tagged , , , . Bookmark the permalink.

13 Responses to Applying a patch to a Repo using Mercurial

  1. anonymous says:

    Try hg import.

    Next time take a look at hg help.

  2. Sid says:

    Actually I did try using hg import but it didn’t work for me for some reason. Some sort of error, but I can’t recall what it was now.

    Maybe hg import is a more straight forward way of doing it but I couldn’t get it to work

  3. Steve Losh says:

    `hg import` is definitely the right way to import patches. What was the error you were getting?

  4. Jared says:

    `hg import` just worked perfectly for me.

    I think you should update your blog post to reflect the standard way of applying a *.diff file.

  5. Bryan says:

    You can use hg patch (a synonym for hg import) just like you would use the regular old patch command:

    hg patch -p0 patchfile

    I like the –no-commit option to hg patch so I can make sure it all looks good before committing:

    hg patch –no-commit -p0 patchfile

  6. Alena Markus says:

    I am definitely sure that this will work for some but still they might encounter some minor problems which can be solved easily.

  7. i have read your previous post. this one is better than the previous one. looking forward to read more post from you

  8. kimberly74 says:

    Actually, I’m just planning to try the TortoiseMerge. To found out if it really works.

  9. kristine911 says:

    TortoiseMerge all about having the history of your working progress. But to really see and check the history, you must be able to see and check what has changed between two points in time,

  10. Ryan Adams says:

    Thank goodness for bugzilla! I use it frequently for internet marketing consulting. Great post. Thanks for sharing!
    Ryan Adams

  11. richard says:

    ‘hg import’ does not bring any hassle. so what was happening in your case?

  12. William says:

    Thanks for all the information. Nice of you to put the step by step guide. It was so easy for me to follow.

  13. Karissa says:

    I see a lot of interesting articles on your blog. You have to spend a lot of time writing, i know how to save you a lot of time, there is a tool that creates unique, google
    friendly articles in couple of minutes, just search in google

Leave a Reply to Jared Cancel reply

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


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>