From the Canyon Edge -- :-Dustin

Monday, March 28, 2011

Bikeshed: apply-patch (auto detect strip level and apply the specified patch)

It's been a while since I blogged a Bikeshed post, and I still have plenty of utilities to cover :-)

But this one just made it into Ubuntu Natty (11.04), and my bash history says that I've used it 30+ times already this week...

I think Larry Wall's diff and patch utilities are among the most important programs in all of free software.  These are the most fundamental tools that allow us to modify code, share those modifications with one another in both a human and machine readable format, and apply those changes elsewhere.  It's not uncommon for me to use the two of them more than a hundred times in a work day.

And yet there's two things that bother me about patch...
  1. the -p|--strip parameter should be auto detected, if unspecified
  2. usually, I just want to pass a file in as a parameter using tab-completion, rather than piping it or redirecting it from standard input
So, I'd like to introduce you to the apply-patch utility, which addresses both (1) and (2)!  It takes a patch or a diff file as an argument, and then quietly tries to apply the patch using --dry-run, for -p=0..16, until it finds the correct strip level.  Once it does, it actually applies the patch.


kirkland@x201:/tmp/foo/patch-2.6$ apply-patch debian/patches/lenny-options 
patching file src/patch.c
patching file

Useful to anyone besides me?

I'm pretty sure I could hack patch.c and pch.c in the upstream source for patch to do this auto detection of strip level, but I wonder if upstream would take it?



  1. You make awesome utilities.

    Upstream might be more receptive to an additional command-line arg that turns on autostrip, rather than making it default.

  2. Nice! I often find that I want to wget a patch and apply it. I use a crappy script for doing this, but I often find i need to adjust the strip level, depending on the patch source.

    I could either convert wpatch to pipe into apply-patch... or my favoured option.. apply-patch learns to support remote retrieval of patches... whatcha think? :)

    $ cat ~/bin/wpatch
    wget -qO- "$1" | patch -p1

  3. Daviey, brilliant idea :-) Can you open an LP bug? I have some code for adding 'apply-patch URL' support tested and working :-) I'd love to commit it with an LP bug number!


  4. Daviey:


  5. Couldn't hurt to ask upstream, I think. Any existing script with a patch command not specifying a -p* shouldn't be any more broken if it autodetected, so it doesn't seem like the change would do harm.

  6. Nice tool, gentoo uses something similiar in it's eutils.eclass (a set of helper functions for the package manager) you might find some inspiration there: (search for "epatch")
    BTW: you can specify -i to give patch a filename instead of piping stdin


Please do not use blog comments for support requests! Blog comments do not scale well to this effect.

Instead, please use Launchpad for Bugs and StackExchange for Questions.