[Dev] Re: embarrassing patch question

Ed Hill dev@trilug.org
28 Feb 2002 13:09:08 -0700


Hi John,

I think the patch man page does a fairly good job of explaining it:

       -pnum  or  --strip=num
          Strip   the  smallest  prefix  containing  num  leading
          slashes from each file name found in the patch file.  A
          sequence  of one or more adjacent slashes is counted as
          a single slash.  This controls how file names 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.  For example, supposing the file name in the
          patch file was

             /u/howard/src/blurfl/blurfl.c

          setting -p0 gives the entire file name unmodified,  -p1
          gives

             u/howard/src/blurfl/blurfl.c

          without the leading slash, -p4 gives

             blurfl/blurfl.c

          and  not  specifying -p at all just gives you blurfl.c.
          Whatever you end up with is looked for  either  in  the
          current directory, or the directory specified by the -d
          option.


So, you specify a level in the -p option that is sufficient to strip
away enough leading path entries so that it matches the path that fits
with your current directory setup.  Thus, you never *have* to have a
single file in the same directory name it was in when you created the
patch (if that patch is for a single file)!  Extending the above
example, the pattern is:

  -p0   ==>  /u/howard/src/blurfl/blurfl.c
  -p1   ==>   u/howard/src/blurfl/blurfl.c
  -p2   ==>     howard/src/blurfl/blurfl.c
  -p3   ==>            src/blurfl/blurfl.c
  -p4   ==>                blurfl/blurfl.c
  -p5   ==>                       blurfl.c


For a patch containing a set of changes to multiple files in a directory
tree composed of multiple levels of directories, you will want to keep
the directory names the same up to the root of the tree.  That is, the
patch should apply cleanly if all the subdirs have the same names but
the root directory of the tree can have a different name (which you can
strip away using the appropriate -p[n] setting).

hth,
Ed



On Thu, 2002-02-28 at 12:46, John F Davis wrote:
> 
> Hello
> 
> Thanks for the reply, but my original question still exists.
> 
> Here is your answer:
> 
> 2.1 Create the patch
>     diff -u oldfile newfile > filename.patch
>  or diff -urN /olddir /newdir > filename.patch
>  or diff -urN -X excludes /olddir /newdir > filename.patch
> 
> diff options:
>  -u  Output (default 2) lines of unified context.
>  -r  Recursively compare any subdirectories found.
>  -N  Treat absent files as empty.
>  -X FILE  Exclude files that match any pattern in FILE.
> 
> Note: The -u options can be replaced with -c to create a context format
>       diff
>       file with a setting of two lines.
> 
> 2.2 Apply the patch
>     gzip -cd patch.gz | patch -p0
>  or bzip2 -dc patch.bz2 | patch -p0
>  or patch -p0 < filename.patch
> 
> patch options:
>  -p NUM  Strip NUM leading components from file names.
> 
> 
> Here is my original question:
> 
> 
> > I think this is right.  However, it would be great if someone would clear
> > up the bit
> > about the target directory.  Does the target directory have to called the
> > same as the
> > "patched" or "orig" directory?  Is there a way to specify or edit the
> patch
> > file so
> > that the end user can have their own custom directory name.  For
> instance,
> > I used foo and foo.orig, but the end user might have directory names
> > like foo2 or foo.nfs, etc.
> >
> 
> Put another way, in your step 2.2, what is the implied directory name used
>  for the patch?  What is the p1 or p0 bit?  Is that supposed to
> somehow strip the leading direcotry name or something?  When you issue that
>  comand, are you in the parent directory which owns
> the kernel directory to be patched or are you in the directory to be
>  patched?
> 
> eg.:
> 
> ls /usr/local/
> foo foo.orig
> 
> cd foo
> patch -p0 < patchfile
> 
> or
> 
> ls /usr/local
> foo foo.orig patchfile
> patch -p0 < patchfile
> 
> 
> 
> David Filion <filiond@videotron.ca> on 02/28/2002 11:39:42 AM
> 
> 
> 
> 
> 
> To:    John F Davis/Raleigh/IBM@ibmus
> cc:    dev@trilug.org, kernelnewbies@nl.linux.org
> Subject:    Re: embarrassing patch question
> 
> 
> 
> John F Davis wrote:
> >
> > Hello
> >
> > Ok.  I've been using patch and I've asked some questions about it on IRC,
> > but I'm still not
> > sure about how to use it "CORRECTLY" or in the "PRESCRIBED-METHOD".  So,
> > please correct me.
> >
> > I want to create a patch file to give to my co-workers and instructions
> on
> > how to use it.
> >
> > With that said, here is the scenario:
> >
> > I have a original directory called foo.orig and a "up-to-date" directory
> > called foo.latest.
> > So, I created a patch file using this syntax:
> > diff -Naur foo.orig foo.latest > foo.patch
> >
> > Then to apply the patch, I did:
> > patch -p1 foo < foo.patch
> > or
> > patch -p1 foo.orig < foo.patch
> > I can't remember which one I did.
> >
> > I think this is right.  However, it would be great if someone would clear
> > up the bit
> > about the target directory.  Does the target directory have to called the
> > same as the
> > "patched" or "orig" directory?  Is there a way to specify or edit the
> patch
> > file so
> > that the end user can have their own custom directory name.  For
> instance,
> > I used foo and foo.orig, but the end user might have directory names
> > like foo2 or foo.nfs, etc.
> >
> > JD
> >
> > --
> > Kernelnewbies: Help each other learn about the Linux kernel.
> > Archive:       http://mail.nl.linux.org/kernelnewbies/
> > IRC Channel:   irc.openprojects.net / #kernelnewbies
> > Web Page:      http://www.kernelnewbies.org/
> 
> 
> I threw together something a while ago when I was in the same
> situation, I've included it below.  Because we are using Solaris
> at work, it covers the older and newer versions of diff/patch.
> 
> HTH
> David Filion
> 
> --- Start of doc ---
> 
> Creating a patch
> 
> Below is a cheat sheet for creating and applying patch files.  For more
> details about each command, check the commands manpage, info document or
> try "command --help".
> 
> 
> 1.0 Using older versions of diff/patch.
>  The older diff/patch utilities don't understand the newer unified
>  format.  Thus, the older conext format must be used. Thus the -c
>  option that appears.
> 
> 1.1 Create the patch
>  diff -c -b -r /original /new > filename.patch
> 
> diff options:
>  -c  Produces a listing of differences with three lines of context.
>  -b  Ignores trailing blanks (spaces and tabs) and treats other strings
>      of blanks as equivalent.
>  -r  Applies diff recursively to common subdirectories encountered.
> 
> 1.2 Apply the patch
>  patch -c -l -p1 -d /original < filename.patch
> 
> patch options:
>  -c  Interpret the patch file as a context difference (the output of
>      the command diff when the -c or -C options are specified).
>  -l  Cause any sequence of blank characters in the difference script
>      to match any sequence of blank characters in the input file.
>      Other characters will be matched exactly.
>    -d  Change the current directory  to  dir  before processing.
>  -p NUM  Strip NUM leading components from file names.
> 
> 2.0 Using newer/GNU diff and patch utilities.
>  Unlike their older counterparts, the newer/GNU versions of diff
>  understand the unified format of patch file.  This is the format used
>  by the Linux kernel developers.
> 
> 2.1 Create the patch
>     diff -u oldfile newfile > filename.patch
>  or diff -urN /olddir /newdir > filename.patch
>  or diff -urN -X excludes /olddir /newdir > filename.patch
> 
> diff options:
>  -u  Output (default 2) lines of unified context.
>  -r  Recursively compare any subdirectories found.
>  -N  Treat absent files as empty.
>  -X FILE  Exclude files that match any pattern in FILE.
> 
> Note: The -u options can be replaced with -c to create a context format
>       diff
>       file with a setting of two lines.
> 
> 2.2 Apply the patch
>     gzip -cd patch.gz | patch -p0
>  or bzip2 -dc patch.bz2 | patch -p0
>  or patch -p0 < filename.patch
> 
> patch options:
>  -p NUM  Strip NUM leading components from file names.
> 
> 3.0 Creating a patch using CVS.
>  A patch is created in CVS using the rdiff cvs command.
> 
> 3.1 Create the patch.
> 
>  o Create a context (default) format diff of release tags rel1 and
>    rel2 for module.
>  cvs rdiff -r rel1 -r rel2 module
> 
>  o Create a unidiff format diff of release tags rel1 and rel2
>  for module.
>  cvs rdiff -u -r rel1 -r rel2 module
> 
> cvs rdiff options:
>  -r tag  Use revision <tag>
>  -u  Use the unidiff format.
>  -c  Use the context format (default).
> 
> 
> Copyright (c) David Filion (filiond@videotron.ca)
> Created 2001/09/12.
> 
> 
> _______________________________________________
> Dev mailing list
> Dev@trilug.org
> http://www.trilug.org/mailman/listinfo/dev
> 
-- 
Edward H. Hill III, PhD
Post-Doctoral Researcher   |  Email:       ed@eh3.com, ehill@mines.edu
Division of ESE            |  URL:         http://www.eh3.com
Colorado School of Mines   |  Phone:       303-273-3483
Golden, CO  80401          |  Fax:         303-273-3311
Key fingerprint = 5BDE 4DA1 66BE 4F7B BC17  3A0C 932B 7266 1E76 F123