ANNOUNCEMENT: Live Wireshark University & Allegro Packets online APAC Wireshark Training Session
July 17th, 2024 | 10:00am-11:55am SGT (UTC+8) | Online

Ethereal-dev: Re: [Ethereal-dev] What's the recommended way to use the patch tool?

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Guy Harris <gharris@xxxxxxxxx>
Date: Tue, 31 Aug 2004 10:42:25 -0700
Ulf Lamping wrote:

Given the file "new.diff" containing a unified diff, what is the right way to call the patch tool?

That depends on what the pathnames in "new.diff" look like. If they're relative to the top-level source directory - for example, if a patch to "prefs.c" just has "prefs.c" as the file name - you'd run it as

	patch -p0 <new.diff

If they're relative to a higher-level directory, you'd replace 0 with the number of higher-level directories in the path, e.g. if the names are "ethereal.orig/prefs.c" and "ethereal.mine/prefs.c", you'd run it with

	patch -p1 <new.diff

If they're relative to a *subdirectory* of the top-level directory, you'd run "patch" in *that* directory and run it with "-p0".

If you run it without "-p" at all, it flattens path names, so that if you have a patch file with patches to "Makefile.am" and "wiretap/Makefile.am", it'll try to apply the first patch to the top-level "Makefile.am" and then apply the "wiretap/Makefile.am" patch to the top-level "Makefile.am" as well. (No, I don't know why "-p0" isn't the default; perhaps their idea was that most software to which you apply patches doesn't have subdirectories in the source tree, and stripping directories makes sure that the patches will apply regardless of how many extraneous directory names are in the path names.)

Is there a difference when the diff file contains one file "diff -u" or more files "diff -ur"?

There isn't a difference in the arguments to "patch".

(However, there is a difference in the number of times you have to run "patch" - and the number of times you have to save a patch file from mail - so I, at least, prefer it when people send a single patch file with patches for all the files to be changed.)

At which position in the filesystem has the patch tool to be called?

If the pathnames are relative to the top-level source directory, or to a directory above that directory, you'd run it in the top-level source directory.

If they're relative to a *subdirectory* - for example, if somebody did a patch to "packet-ip.c" and ran "diff" or "svn diff" in the "epan/dissectors" directory - you'd run it in that subdirectory. (I would really prefer that people *NOT* submit patches like that - especially if they're only patching files that exist in multiple directories, such as "Makefile.am".)