Rob Arnold’s Blog

witty tagline

hg qimport my-bugzilla-patch redux

Some of you may remember my previous post about a little script that will import patches from Bugzilla into your local queue. I haven’t worked on it much since October. Yesterday I took a look into converting it into an extension and you can now see the results. Usage is simpler than before: hg qimportbz 418454 should be all you need to type in most cases.

Most of the command line options have been removed and some have been replaced with Mercurial configuration options. Two still remain for the command line: -r and -n (dry run and patch name) and there is a new one -p (preview) which lets you edit the patch before it gets put on your queue. This lets you fix the commit message and author if need be and ensure that everything looks right before hg sees it.

Installation is simple. Clone the repository to somewhere in your file system. Then edit an hgrc to reference the extension; you can find/create an hgrc in your Mercurial install directory, in your home directory and in a repository’s .hg directory. Under [extensions], you’ll need to add something along the lines of
qimportbz = C:\Users\Rob\qimportbz and now you’ve installed the extension. I found that relative paths did not work; Mercurial would only load the extension with an absolute path. You can run hg help qimportbz to see all the options.

There are some optional hgrc configuration options for the extension. Create a [qimportbz] section. The default values are:
bugzilla = $BUGZILLA if set else bugzilla.mozilla.org
patch_format = bug-%%(bugnum)s
msg_format = Bug %%(bugnum)s - "%%(title)s" [%%(flags)s]

The formats for patch_format and msg_format are the usual python formatting string format when a dictionary is used to supply the data; you will need to escape any % characters though since Python’s configuration parser tries to be smart and interpret them on its own. There are 5 pieces of patch metadata available for use:

“bugnum” : the bug number
“id” : the patch id (internal bugzilla number)
“title” : the bug title
“desc” : the patch description
“flags” : all the flags

Hopefully everything will work…I’ve only tested this on Windows with Mercurial 1.0.1 (which shipped with the MozillaBuild 1.3 environment). All patches are converted to utf-8 which should hopefully alleviate past woes when importing patches with non-ascii characters.

Feedback is welcome! Please let me know if there’s a feature you would like to me to add or if I removed a feature you really liked.

Edit: Thanks to Jason Orendorff for solving the format string issue.

9 comments

9 Comments so far

  1. Mark Banner June 3rd, 2009 5:49 am

    For all those macports users out there, if you get a message such as:

    Fetching http://bugzilla.mozilla.org/show_bug.cgi?ctype=xml&id=111111…abort: error: unknown url type: https

    then try:

    sudo port install py25-socket-ssl

    and that should fix it.

  2. Mark Banner June 3rd, 2009 5:57 am

    I also couldn’t get overriding the format in .hgrc to work. Using your suggestion for patch_format:

    [qimportbz]
    patch_format = bug-%(bugnum)s

    gave me:

    abort: Error in configuration section [qimportbz] parameter ‘patch_format’:
    Bad value substitution:
    section: [qimportbz]
    option : patch_format
    key : bugnum
    rawval :

    Not sure why.

  3. Mark Banner June 3rd, 2009 6:47 am

    Is it me or is importing with multiple patches on a bug not working? Bug 492116 currently has two patches available and whichever I choose I get the second patch :-(

  4. Rob Arnold June 3rd, 2009 12:34 pm

    I’ve now updated the article to address the issue with formatting strings; the % characters need to be escaped because the configuration parser tries to do its own substitutions.

    Also, the issue with multiple patch selection is fixed. I wish Python had stricter variable scoping.

  5. Mark Banner June 4th, 2009 6:16 am

    Thanks Rob, works like a dream now :-)

  6. Boris June 11th, 2009 7:30 am

    So I tried using -p to edit the commit message, and the patch still got imported with the original commit message (the one that was in the diff to start with). Feature or bug? ;)

  7. Zack September 22nd, 2009 3:50 pm

    I hit three more problems:

    – The advice to double % signs in patch_format is incorrect for Mercurial 1.3.1.

    – ui.prompt() doesn’t have a pat= keyword argument in Mercurial 1.3.1. I don’t know what replaced it, if anything.

    – Most seriously, if you import multiple patches, they are added to the series file in reverse order.

  8. Josh Matthews January 13th, 2010 6:19 pm

    When using this now, I get patches showing up as bug-%(bugnum)s in my queue. Any thoughts?

  9. Rob Arnold January 13th, 2010 7:48 pm

    Mercurial’s behavior towards the % in config files changed – in newer versions you no longer need to escape % so I suspect this is the problem you’re having.

Leave a reply