hg qedit

On his blog, Paul O’Shannessy came up with an ‘hg qedit’ alias that opens up an editor on your .hg/patches/series file for reordering your patch queue. It’s a nice simple solution to a common problem, so obviously I felt compelled to muck it up.

Here’s my version, for insertion into your ~/.hgrc:

[alias]
qedit = !S=$(hg root)/.hg/patches/series; cp $S $S.bak && perl -pale 'BEGIN { chomp(@a = qx(hg qapplied -q)); die if $?; @a{@a}=(); }; s/^/# (applied) / if exists $a{$F[0]}' $S > $S.new && ${EDITOR-vim} $S.new && sed -e 's/^# .applied. //' $S.new > $S
                                                                                                                                                                                                                   # Did you see this by scrolling over?
                                                                                                                                                                                                                   # I want better code snippet support

This fixes the main problem with zpao’s solution, which is that it’s too clean and simple.

No, wait, that’s not a problem.

The problem is that when I edit my series file, I often forget that I have some patches applied and end up reordering applied patches, which makes a complete mess. The above alias opens up an editor on your series file, only it also inserts comments showing which patches are already applied. (If you really, really want to mess yourself up, go ahead and reorder the commented lines. You’ll get what you deserve.)

Here’s what my queue looks like when editing the series file:

# (applied) better-dtrace-probes
# (applied) try-enable-dtrace
# (applied) bug-650078-no-remote
bug-677985-callouts
bug-677949-gc-roots
hack-stackiter

Come to think of it, mq really shouldn’t let you mess up that way in the first place. It knows the original patch names for your applied patches (unless you are really determined to make your life difficult, and commit things on top without going through mq at all). It could detect when you reordered applied patches, and just undo what you did. And call you names. But maybe that would slow things down.

Update: it wasn’t working for jlebar, which turned out to be because he had added qapplied=-v to his [defaults] section. The above is now fixed for that scenario by adding a -q flag to hg qapplied.

Tags: ,

4 comments

  1. Or you could upgrade to Mercurial 1.6

    Yes, qpush –move was mentioned in the post I linked to, and I use it frequently. It’s good when it’s what you want — namely, pulling one or a couple patches out of your queue and applying them. But I often want to “sink” a patch to near the bottom, to get it out of the way, and using qpush –move for every intervening patch is a PITA. There are also times when I want to swap two subsequences of patches. I can’t say I edit my patch queue often, but it’s not rare either.

    stgit has long had a couple of additional commands that made it very rare to need to manually edit the patch queue: ‘stg float’ and ‘stg sink’. I think they even deal with moving applied patches around.

  2. Hm, it doesn’t work on my mac. Or rather, it works just like Paul’s qeidt.

  3. Just tried this on my Linux box. I get:

    cp: missing destination file operand after `/home/jlebar/code/moz/ff-1/src/.hg/patches/series{,.bak}’

    I presume this is a shell dialect issue?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.