16

Is there a way to apply a single hunk from a diff to a file? For example, say I do a diff from file A and B, and that produces three chunks of differences, each denoted with something like...

@@ -971,30 +977,28 @@

...(in the case of unified diffs). I'd then want to be able to feed that diff into stdin, and ask patch to only apply hunk N.

The manual method would be to cut-and-paste the interesting hunks, but I'm not after that kind of a solution.

khosrow
  • 8,799
  • 4
  • 21
  • 24
  • In my past experience, it's just not very hard to read a context diff into an editor and prune it down. I don't really see the advantage of a complex argument structure for patch over that. Is there something in particular you are trying to do that makes this intractable? – bmargulies Jan 02 '10 at 03:55
  • Let's put it this way - if patch *did* support it - I *would* use it. I have some vim macros that remove diff syntax so it's not too bad, but it would be easier to just type a single command. – khosrow Jan 02 '10 at 04:08

3 Answers3

12

filterdiff might help.

It allows extraction of subset of patches matching a variety of requirements, from one/many patch files. For example, here we extract from the file unified_diff.patch, the patches applicable to files with name matching one_file.c, only to lines 950 to 1050 of the original file:

filterdiff -i *one_file.c --lines=950,1050 unified_diff.patch

To extract specific/range of hunks:

filterdiff --hunks=1,3,5-8,15 file.patch

Extracting patches from mail messages:

filterdiff message-with-diff-in-the-body > file.patch

etc.

Melebius
  • 6,183
  • 4
  • 39
  • 52
0

Some of the GUI diff/patch tools have a way to select blocks or even individual lines. I know TortoiseDiff from TortoiseSVN can work this way. I think I've seen windiff do it, but it's been a while since I had to use it.

As far as command line tools, I have not seen anything that will do what you are asking.

David Hogue
  • 1,791
  • 1
  • 14
  • 23
  • I couldn't find anything in the man page, but felt that it must exist - and it's just my inability to find it. So - patch itself has no native support for this - is that accurate? – khosrow Jan 02 '10 at 03:52
0

Emacs can not only edit diffs intelligently (including the ever-useful split-hunk operation), but can apply them one hunk at a time itself (avoiding the need to switch from the editor to run patch).

Davis Herring
  • 36,443
  • 4
  • 48
  • 76