Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to patch only a particular hunk from a diff

Tags:

diff

patch

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.

like image 675
Cyrus Avatar asked Jan 02 '10 03:01

Cyrus


1 Answers

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.

like image 187
Narendran Gopalakrishnan Avatar answered Oct 23 '22 11:10

Narendran Gopalakrishnan