1

I have received a patch that I need to apply, however the filenames make it difficult to predict what -p option to use:

--- /home/user/foo/bar/baz.c
+++ /data/myproj/project/version/foo/bar/baz.c

I'm applying this in a directory that contains foo/bar/baz.c, but I'm not sure which patch number to use. -p4 fails, although it's what I would have expected for the second line. p2 also fails, however that's what I would have expected from the first line.

Which one am I supposed to use? Or should I expect a patch to only apply if the number of leading directories is the same for the +++ and ---?

2
  • How was this patch created? Jan 15, 2021 at 18:55
  • I don't know - I was not the creator. I assume some version of diff or git diff since it's in an android repo Jan 15, 2021 at 19:40

1 Answer 1

2

Or should I expect a patch to only apply if the number of leading directories is the same for the +++ and ---?

Yes, man patch mentions that:

   If the recipient is supposed to use the -pN option, do not send  output
   that looks like this:

          diff -Naur v2.0.29/prog/README prog/README
          --- v2.0.29/prog/README   Mon Mar 10 15:13:12 1997
          +++ prog/README   Mon Mar 17 14:58:22 1997

   because  the two file names have different numbers of slashes, and dif‐
   ferent versions of patch interpret  the  file  names  differently.   To
   avoid confusion, send output that looks like this instead:

          diff -Naur v2.0.29/prog/README v2.0.30/prog/README
          --- v2.0.29/prog/README   Mon Mar 10 15:13:12 1997
          +++ v2.0.30/prog/README   Mon Mar 17 14:58:22 1997

What you can do here is normalize the patch and apply it like that:

sed 's,--- /home/user/foo/,--- /data/myproj/project/version/foo/,' orig.patch | patch <other_patch_options>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .