I must confess something… I appear to have unintentionally fibbed in my last post, when I said “This method has the added benefit of making patches cleaner.” It has come to my attention that when using the script on files which have already been patched, manually applying a patch on top (as you might do if only part of a patch you want to port is rejected) can lead to cruft in the final patch.

The “patch” command basically assumes that we have no version control. Thus, if it detects anything even slightly amiss (such as a line number offset) it makes a backup of the file, even if it successfully patches the original. This backup usually ends in a ~, giving, for example, “Makefile~” as the backup of a patched “Makefile”. When mercurial adds the files to the repo, it includes *~. So if the patch you want to port touches any of the same files, it will modify the *~ file, causing mercurial to believe that a valid change was made, and thus to include that change in the diff it produces after you’re finished hacking on the sources.

The fix is very simple:

#!/bin/sh
# start up the repo
hg init
# remove files who’s existence is only due to patch doing backups
find . -name ‘*~’ -delete
# add all files to the repo, but don’t be overly verbose about it
hg addremove > /dev/null
# commit all files to the repo
hg ci -m “initial commit”

Of course, this also has the benefit of making the checkout a wee bit cleaner while working on it :)