Diagnosing mercurial patch rejects

All timestamps are based on your local time of:

Posted by: stak
Tags: mozilla
Posted on: 2013-01-07 13:47:37

Even though I've switched to using git for my day-to-day mozilla work, there are times when I have to apply patches to my mercurial tree using mq. Sometimes applying the patches fails because something changed, and it's not easy to figure out what changed, specially if the patch is large. To solve this problem, I wrote a little bash/perl script, inspect-rejects.

To use it, stick it on your $PATH, and invoke it on the source file for which rejects were generated. For example, if rejects were generated for layout/base/nsRefreshDriver.cpp, invoke it on that file:

$ hg qpush --move try-avi
applying try-avi
patching file layout/base/nsRefreshDriver.cpp
Hunk #1 FAILED at 936
1 out of 1 hunks FAILED -- saving rejects to file layout/base/nsRefreshDriver.cpp.rej
patch failed, unable to continue (try -v)
patch failed, rejects left in working dir
errors during apply, please fix and refresh try-avi
$ inspect-rejects layout/base/nsRefreshDriver.cpp

What this does is print out the "original" version of the code (context lines and "-" lines) from the reject file into a temporary file, and then opens vimdiff with that temp file and the source file as it is in your tree. This allows you to compare what the patch thinks your file should look like and what your file actually looks like, and allows you to quickly identify differences. You can even edit your in-tree file directly in the vimdiff editor to match what the patch expects it to look like, and then run patch on the rejects file to apply it!

$ inspect-rejects layout/base/nsRefreshDriver.cpp
2 files to edit
# edit layout/base/nsRefreshDriver.cpp to match the expected context
$ pushd layout/base && patch < nsRefreshDriver.cpp.rej && popd
~/zspace/mozilla/layout/base ~/zspace/mozilla
patching file nsRefreshDriver.cpp

If you do this, you'll probably want to undo your changes afterwards - I leave automating this part as an exercise to the reader.

Posted by Neil Rashbrook at 2013-01-08 06:01:32
If you're lucky then the patch will have been exported against an existing changeset and you can update to that changeset, import the patch and then rebase it.
Allowed expansions in comments/replies: [i]italic[/i], [u]underline[/u], [b]bold[/b], [code]code[/code], [sub]subscript[/sub], [sup]superscript[/sup], [url=http://some.url]linked text[/url]
Human verification: Sum of twelve and thirty-five =

[ Add a new comment ]

(c) Kartikaya Gupta, 2004-2024. User comments owned by their respective posters. All rights reserved.
You are accessing this website via IPv4. Consider upgrading to IPv6!