1

I understand that patch allows patching applications when some path components are removed.

While -p0 argument will treat a path as is:

/var/www/html/example.com

-p1 will remove the root slash, so the path we work with becomes:

var/www/html/example.com

Why would one want to remove the root slash (or any previous components of a path), and not just patch with a full path?

I can see a logic of deepening in a directory tree by a relative path, but I miss if it will be practical or useful in the vast majority of cases.

  • Related: https://unix.stackexchange.com/q/167216/117549 – Jeff Schaller Oct 28 '18 at 14:55
  • @JeffSchaller (dear Jeff), I really don't agree that the revised question is similar to the one you linked to (as it seems to me to ask only on the "what" and not on the "why". I ask only on the "why" here. Care to help me improve my question please? –  Oct 28 '18 at 23:13
  • 1
    Seems to me that the [accepted answer](https://unix.stackexchange.com/a/26504/100397) answers the _why_ too. At least for your #2. – roaima Oct 29 '18 at 00:19
  • And a quick reading of `man patch` trivially answers (1): "Specifying −p 0 shall cause the full pathname to be used. If −p is not specified, only the basename (the final pathname component) shall be used." – muru Oct 29 '18 at 01:21
  • @muru I admit I was mistaken not to read the man and ask a better question. I thank you for noting this difference (I find interesting) that not using any `-pX` argument will take just the last component. –  Oct 29 '18 at 11:11
  • @JeffSchaller do you think the question is better now?... –  Oct 29 '18 at 11:12
  • @JohnDoea do you understand directory structures? Relative and absolute path names? – Jeff Schaller Oct 29 '18 at 11:22
  • Yes... I do... I just don't see why cutting a root slash would likely to be helpful (unless one wants a deeper path than the current one). –  Oct 29 '18 at 14:29

2 Answers2

4

The patch utility is a POSIX utility, and the Rationale section in the standard specification of the utility gives an example of the usage of the -p option:

The -p option makes it possible to customize a patch file to local user directory structures without manually editing the patch file. For example, if the filename in the patch file was:

/curds/whey/src/blurfl/blurfl.c

Setting -p 0 gives the entire pathname unmodified; -p 1 gives:

curds/whey/src/blurfl/blurfl.c

without the leading <slash>, -p 4 gives:

blurfl/blurfl.c

and not specifying -p at all gives:

blurfl.c

The OpenBSD manual also has a similar but slightly expanded example of when using -p may be useful (my emphasis):

-p strip-count, --strip strip-count

Sets the pathname strip count, which controls how pathnames found in the patch file are treated, in case you keep your files in a different directory than the person who sent out the patch. The strip count specifies how many slashes are to be stripped from the front of the pathname. (Any intervening directory names also go away.) For example, supposing the file name in the patch file was /u/howard/src/blurfl/blurfl.c:

Setting -p0 gives the entire pathname unmodified.

-p1 gives

u/howard/src/blurfl/blurfl.c

without the leading slash.

-p4 gives

blurfl/blurfl.c

It goes on to say:

Not specifying -p at all just gives you blurfl.c, unless all of the directories in the leading path (u/howard/src/blurfl) exist and that path is relative, in which case you get the entire pathname unmodified. Whatever you end up with is looked for either in the current directory, or the directory specified by the -d option.

That is, if you're given a patch with absolute paths to the files that it applies to, and you keep those files in a different directory, then you may use -p with the appropriate number to "offset" the paths in the patch from the directory specified by -d (or the current directory).

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
0

The patch command reads a source file's instructions on how to change a file, then applies the changes(for more details). And passing -pN flag skip N leading slashes from the filenames present in the patch file.
It's about a directory part in diff header. In your case diff header contan this string: /var/www/html/example.com. If you use -p1 flag, patch utility will strip from this string leading directory and you will get a var/www/html/example.com, which you want patch. With -p0 parameter will patch don't strip from this string nothing and it will not work properly.

For example :
For proper invocation patching of SSH2.xs file(patch file located here : rt.cpan.org/Public/Bug/Display.html?id=36614) :

patch -p1 < net-ssh2-0.18-perl5.10.patch

patch file contain a directory Net-SSH-0.18.orig which you have remove before patching and above must be executed from directory where SSH2.x2 file is there.

Reference :
1. -p0 vs -p1
2. Man page - IBM

finn
  • 453
  • 3
  • 7