I normally use Mercurial Queues to maintain patches on top of source trees for applications that I need to modify before compiling for use for whatever reason (compiler fixes, system tweaks, non-mainstream features, etc). Although Mercurial Queues can be used to manage thousands of patches, the number of patches I work with is usually less than ten in this situation, so this work flow is optimized for a smaller number of patches. It also requires the rebase extension (bundled with "modern" versions of Mercurial) to be enabled, as well as Mercurial Queues itself.
Initial Setup
We're going to assume that the official application source is not contained in a Mercurial repository. If it is, you can just clone that and skip the steps in this section. The commands below are based on the workflow I use, just to convey the general idea of what to do.
First, grab the latest version of the application you need to modify, and extract it into a directory. In this case I'll be modifying some_application, because I have no imagination. Since this will be the repository in which you'll be making your changes, you may want to remove the version from the folder name, if present.
cd custom_builds
tar xvf some_application-version.tar.bz2
mv some_application-version some_application
cd some_application
Create a Mercurial repository here.
hg init
Custom Changes
Now you can make your changes, adding as many patches as necessary. Please see the MQ official documentation for more information on these commands,
hg qnew [patch name] # create a new patch
hg qrefresh # refresh the current patch
hg qrecord # refresh the current patch (at the patch hunk level)
hg qdelete # remove the current patch
hg qpush # push the next patch onto the stack
hg qpop # pop the current patch off of the stack
hg qseries # list all of the patches in the series
hg qapplied # list all of the patches that are currently applied
After your changes are set up to your liking, you can compile and enjoy the application.
Please note that there are many more Mercurial Queues commands; to get a full listing you can type hg help mq.
Handling Updates to the Core Source Code
Say it's been a few months and there have been a few significant changes to some_application, and a new version is out. The kind of changes you would like to be able to use.
Clone your existing repository (without the patches) to create an area in which to apply the core updates.
hg clone some_application some_application-new_version
Extract the source code for the new version over the files in your cloned repository, and update it.
tar xvf some_application-new_version
cd some_application-new_version
hg addremove # tell Mercurial to add new files and remove ones that were deleted
hg commit -m 'Updated to new version.'
Now you can go back to your main repository, and pull in the updated changes from the clone. Thanks to the rebase extension, the pull command can be used to update the changes to the core code, then merge in your managed patches on top using the regular merge tools.
hg qpush -a # apply all of your patches
hg pull --rebase ../some_application-new_version
...do any manual merging necessary...
After this process is complete your patches will apply cleanly onto the updated source tree. You can delete the some_application-new_version folder if you wish, or keep it around for archival purposes.
This process makes maintaining a series of patches relatively painless in terms of merging and keeping the code up to date. Based on the experiences of others, similar processes scale well, so I assume this one does as well.
For more information and additional work flows, check out the links below:
Mercurial Definitive Guide - Managing Change with Mercurial Queues (a little outdated, but useful).
Mozilla's MQ Notes
David Herron's Introduction to MQ
Git User's Guide to Mercurial Queues
Random Notes on Mercurial Queues
