Splitting A Patch

Posted by Benjamin Close on November 13, 2008 under Other | 8 Comments to Read

Have you ever been working on code, gone off on a side tangent and then realized you don’t have a valid patch of the code you’ve been working on? So you run diff -du, cvs diff, git diff or some other diff and suddenly realize you have a mix of old changes and new changes. Some you want to submit, others you don’t. What you really want to do is break those changes apart and apply only the ones you want.

Sadly, there is no tool to do that.. until now!

SplitPatch.rb

Peter Hutterer, whilst working at the Wearable Computer Lab at University of South Australia faced this very problem. So he wrote the program, splitpatch.

splitpatch will allow you to take a diff of a file and either:

  • Split the patch up into change to the respective patched files
  • Split the patch up into individual hunks, where each hunk can be applied separately

Usage

To use splitpatch, you’ll need ruby installed. Then simply use:

splitpatch.rb YOURPATCHFILE

To split YOURPATCHFILE into a valid patch per file contained within the patch. Or

splitpatch.rb --hunks YOURPATCHFILE

To split the patch file into individual chunks which can be applied, using patch

Download

Splitpatch can be downloaded from:

http://github.com/benjsc/splitpatch/

Patch File Format

For those who don’t think splitpatch will do what they want, here’s at least some info about the format of a patch file.

@@ -143,6 +143,13 @@
  • the first number is the starting line for this hunk in oldfile
  • the second number is the number of original source lines in this hunk (this includes lines marked with “-“)
  • the third number is the starting line for this hunk in newfile
  • the last number is the number of lines after the hunk has been applied.

Many thanks to Peter Hutterer for this valuable trip into a patch.



Donations keep this site alive

  • sage said,

    thanks a bunch, this was exactly what i was looking for; saved me from having to write a splitter myself 😉

  • Łukasz Pełszyński said,

    One of the most useful utilities, thank you!

  • Rog said,

    Hmmm… this fails for me as it doesn’t put file headers at the top of each output hunk/patch file – so GIT apply won’t deal with it.

  • otubo said,

    Same problem with git apply Rog said. 🙁

  • Benjamin Close said,

    With Git you can at least apply partially apply patches using:

    git stash
    git checkout -b junk
    git apply < thepatch git checkout master git rebase -i junk The rebase -i will allow you to selectively add patches After you've done that: git stash apply.

  • Evan said,

    Just what I was looking for.

    As an aside, consider changing the shebang to use ‘env’:
    #!/usr/bin/env ruby
    so that people like me who have ruby in other locations don’t have to modify it.

  • Jack said,

    Fails with the error
    $./splitpatch.rb patch1.patch
    /usr/share/rubygems/rubygems/custom_require.rb:36:in `require’: cannot load such file — file.rb (LoadError)
    from /usr/share/rubygems/rubygems/custom_require.rb:36:in `require’
    from 1351683/splitpatch.rb:25:in `’

  • Benjamin Close said,

    Your missing some ruby packages.

Add A Comment

*