Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one extract a unified-diff style patch subset?

Tags:

diff

patch

Every time I want to take a subset of a patch, I'm forced to write a script to only extract the indices that I want.

e.g. I have a patch that applies to sub directories 'yay' and 'foo'.

Is there a way to create a new patch or apply only a subset of a patch? i.e. create a new patch from the existing patch that only takes all indices that are under sub directory 'yay'. Or all indices that are not under sub directory 'foo'

If I have a patch like ( excuse the below pseudo-patch):

Index : foo/bar
 yada
 yada
- asdf
+ jkl
 yada
 yada
Index : foo/bah
 blah
 blah
- 28
+ 29
 blah
 blah
 blah
Index : yay/team
 go
 huskies
- happy happy
+ joy joy
 cougars
 suck

How can I extract or apply only the 'yay' subdirectory like:

Index : yay/team
 go
 huskies
- happy happy
+ joy joy
 cougars
 suck

I know if I script up a solution I'll be re-inventing the wheel...

like image 788
Ross Rogers Avatar asked Dec 07 '25 06:12

Ross Rogers


1 Answers

Take a look at the filterdiff utility, which is part of patchutils.

For example, if you have the following patch:

$ cat example.patch
diff -Naur orig/a/bar new/a/bar
--- orig/a/bar  2009-12-02 12:41:38.353745751 -0800
+++ new/a/bar   2009-12-02 12:42:17.845745951 -0800
@@ -1,3 +1,3 @@
 4
-5
+e
 6
diff -Naur orig/a/foo new/a/foo
--- orig/a/foo  2009-12-02 12:41:32.845745768 -0800
+++ new/a/foo   2009-12-02 12:42:25.697995617 -0800
@@ -1,3 +1,3 @@
 1
 2
-3
+c
diff -Naur orig/b/baz new/b/baz
--- orig/b/baz  2009-12-02 12:41:42.993745756 -0800
+++ new/b/baz   2009-12-02 12:42:37.585745735 -0800
@@ -1,3 +1,3 @@
-7
+z
 8
 9

Then you can run the following command to extract the patch for only things in the a directory like this:

$ cat example.patch | filterdiff -i 'new/a/*'
--- orig/a/bar  2009-12-02 12:41:38.353745751 -0800
+++ new/a/bar   2009-12-02 12:42:17.845745951 -0800
@@ -1,3 +1,3 @@
 4
-5
+e
 6
--- orig/a/foo  2009-12-02 12:41:32.845745768 -0800
+++ new/a/foo   2009-12-02 12:42:25.697995617 -0800
@@ -1,3 +1,3 @@
 1
 2
-3
+c
like image 185
Zach Hirsch Avatar answered Dec 10 '25 08:12

Zach Hirsch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!