home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / patch / patch.man < prev    next >
Text File  |  1990-03-10  |  14KB  |  317 lines

  1.                                                          PATCH(1)
  2.  
  3.  
  4.  
  5. NAME
  6.      patch - a program for applying a diff file to an original
  7.  
  8. SYNOPSIS
  9.      patch [options] orig patchfile [+ [options] orig]
  10.  
  11.      but usually just
  12.  
  13.      patch <patchfile
  14.  
  15. DESCRIPTION
  16.      Patch will take a patch file containing any of the three
  17.      forms of difference listing produced by the diff program and
  18.      apply those differences to an original file, producing a
  19.      patched version.  By default, the patched version is put in
  20.      place of the original, with the original file backed up to
  21.      the same name with the extension ".orig" or "~" , or as
  22.      specified by the -b switch.  You may also specify where you
  23.      want the output to go with a -o switch.  If patchfile is
  24.      omitted, or is a hyphen, the patch will be read from stan-
  25.      dard input.
  26.  
  27.      Upon startup, patch will attempt to determine the type of
  28.      the diff listing, unless over-ruled by a -c, -e, or -n
  29.      switch.  Context diffs and normal diffs are applied by the
  30.      patch program itself, while ed diffs are simply fed to the
  31.      ed editor via a pipe.
  32.  
  33.      Patch will try to skip any leading garbage, apply the diff,
  34.      and then skip any trailing garbage.  Thus you could feed an
  35.      article or message containing a diff listing to patch, and
  36.      it should work.  If the entire diff is indented by a con-
  37.      sistent amount, this will be taken into account.
  38.  
  39.      With context diffs, and to a lesser extent with normal
  40.      diffs, patch can detect when the line numbers mentioned in
  41.      the patch are incorrect, and will attempt to find the
  42.      correct place to apply each hunk of the patch.  As a first
  43.      guess, it takes the line number mentioned for the hunk, plus
  44.      or minus any offset used in applying the previous hunk.  If
  45.      that is not the correct place, patch will scan both forwards
  46.      and backwards for a set of lines matching the context given
  47.      in the hunk.  First patch looks for a place where all lines
  48.      of the context match.  If no such place is found, and it's a
  49.      context diff, and the maximum fuzz factor is set to 1 or
  50.      more, then another scan takes place ignoring the first and
  51.      last line of context.  If that fails, and the maximum fuzz
  52.      factor is set to 2 or more, the first two and last two lines
  53.      of context are ignored, and another scan is made.  (The
  54.      default maximum fuzz factor is 2.) If patch cannot find a
  55.      place to install that hunk of the patch, it will put the
  56.      hunk out to a reject file, which normally is the name of the
  57.      output file plus ".rej" or "#" .  (Note that the rejected
  58.      hunk will come out in context diff form whether the input
  59.      patch was a context diff or a normal diff.  If the input was
  60.      a normal diff, many of the contexts will simply be null.)
  61.      The line numbers on the hunks in the reject file may be dif-
  62.      ferent than in the patch file: they reflect the approximate
  63.      location patch thinks the failed hunks belong in the new
  64.      file rather than the old one.
  65.  
  66.      As each hunk is completed, you will be told whether the hunk
  67.      succeeded or failed, and which line (in the new file) patch
  68.      thought the hunk should go on.  If this is different from
  69.      the line number specified in the diff you will be told the
  70.      offset.  A single large offset MAY be an indication that a
  71.      hunk was installed in the wrong place.  You will also be
  72.      told if a fuzz factor was used to make the match, in which
  73.      case you should also be slightly suspicious.
  74.  
  75.      If no original file is specified on the command line, patch
  76.      will try to figure out from the leading garbage what the
  77.      name of the file to edit is.  In the header of a context
  78.      diff, the filename is found from lines beginning with "***"
  79.      or "---", with the shortest name of an existing file win-
  80.      ning.  Only context diffs have lines like that, but if there
  81.      is an "Index:" line in the leading garbage, patch will try
  82.      to use the filename from that line.  The context diff header
  83.      takes precedence over an Index line.  If no filename can be
  84.      intuited from the leading garbage, you will be asked for the
  85.      name of the file to patch.
  86.  
  87.      (If the original file cannot be found, but a suitable SCCS
  88.      or RCS file is handy, patch will attempt to get or check out
  89.      the file.)
  90.  
  91.      Additionally, if the leading garbage contains a "Prereq: "
  92.      line, patch will take the first word from the prerequisites
  93.      line (normally a version number) and check the input file to
  94.      see if that word can be found.  If not, patch will ask for
  95.      confirmation before proceeding.
  96.  
  97.      The upshot of all this is that you should be able to say,
  98.      while in a news interface, the following:
  99.  
  100.           | patch -d /usr/src/local/blurfl
  101.  
  102.      and patch a file in the blurfl directory directly from the
  103.      article containing the patch.
  104.  
  105.      If the patch file contains more than one patch, patch will
  106.      try to apply each of them as if they came from separate
  107.      patch files.  This means, among other things, that it is
  108.      assumed that the name of the file to patch must be
  109.      determined for each diff listing, and that the garbage
  110.      before each diff listing will be examined for interesting
  111.      things such as filenames and revision level, as mentioned
  112.      previously.  You can give switches (and another original
  113.      file name) for the second and subsequent patches by separat-
  114.      ing the corresponding argument lists by a '+'.  (The argu-
  115.      ment list for a second or subsequent patch may not specify a
  116.      new patch file, however.)
  117.  
  118.      Patch recognizes the following switches:
  119.  
  120.      -b   causes the next argument to be interpreted as the
  121.           backup extension, to be used in place of ".orig" or
  122.           "~".
  123.  
  124.      -B   causes the next argument to be interpreted as a prefix
  125.           to the backup file name. If this argument is specified
  126.           any argument from -b will be ignored.  This argument is
  127.           an extension to Larry Wall's patch v2.0.1.4, patchlevel
  128.           8, made by M. Greim (greim@sbsvax.uucp).
  129.  
  130.      -c   forces patch to interpret the patch file as a context
  131.           diff.
  132.  
  133.      -d   causes patch to interpret the next argument as a direc-
  134.           tory, and cd to it before doing anything else.
  135.  
  136.      -D   causes patch to use the "#ifdef...#endif" construct to
  137.           mark changes.  The argument following will be used as
  138.           the differentiating symbol.  Note that, unlike the C
  139.           compiler, there must be a space between the -D and the
  140.           argument.
  141.  
  142.      -e   forces patch to interpret the patch file as an ed
  143.           script.
  144.  
  145.      -f   forces patch to assume that the user knows exactly what
  146.           he or she is doing, and to not ask any questions.  It
  147.           does not suppress commentary, however.  Use -s for
  148.           that.
  149.  
  150.      -F<number>
  151.           sets the maximum fuzz factor.  This switch only applies
  152.           to context diffs, and causes patch to ignore up to that
  153.           many lines in looking for places to install a hunk.
  154.           Note that a larger fuzz factor increases the odds of a
  155.           faulty patch.  The default fuzz factor is 2, and it may
  156.           not be set to more than the number of lines of context
  157.           in the context diff, ordinarily 3.
  158.  
  159.      -l   causes the pattern matching to be done loosely, in case
  160.           the tabs and spaces have been munged in your input
  161.           file.  Any sequence of whitespace in the pattern line
  162.           will match any sequence in the input file.  Normal
  163.           characters must still match exactly.  Each line of the
  164.           context must still match a line in the input file.
  165.  
  166.      -n   forces patch to interpret the patch file as a normal
  167.           diff.
  168.  
  169.      -N   causes patch to ignore patches that it thinks are
  170.           reversed or already applied.  See also -R .
  171.  
  172.      -o   causes the next argument to be interpreted as the out-
  173.           put file name.
  174.  
  175.      -p<number>
  176.           sets the pathname strip count, which controls how path-
  177.           names found in the patch file are treated, in case the
  178.           you keep your files in a different directory than the
  179.           person who sent out the patch.  The strip count speci-
  180.           fies how many slashes are to be stripped from the front
  181.           of the pathname.  (Any intervening directory names also
  182.           go away.) For example, supposing the filename in the
  183.           patch file was
  184.  
  185.                /u/howard/src/blurfl/blurfl.c
  186.  
  187.           setting -p or -p0 gives the entire pathname unmodified,
  188.           -p1 gives
  189.  
  190.                u/howard/src/blurfl/blurfl.c
  191.  
  192.           without the leading slash, -p4 gives
  193.  
  194.                blurfl/blurfl.c
  195.  
  196.           and not specifying -p at all just gives you "blurfl.c".
  197.           Whatever you end up with is looked for either in the
  198.           current directory, or the directory specified by the -d
  199.           switch.
  200.  
  201.      -r   causes the next argument to be interpreted as the
  202.           reject file name.
  203.  
  204.      -R   tells patch that this patch was created with the old
  205.           and new files swapped.  (Yes, I'm afraid that does hap-
  206.           pen occasionally, human nature being what it is.) Patch
  207.           will attempt to swap each hunk around before applying
  208.           it.  Rejects will come out in the swapped format.  The
  209.           -R switch will not work with ed diff scripts because
  210.           there is too little information to reconstruct the
  211.           reverse operation.
  212.  
  213.           If the first hunk of a patch fails, patch will reverse
  214.           the hunk to see if it can be applied that way.  If it
  215.           can, you will be asked if you want to have the -R
  216.           switch set.  If it can't, the patch will continue to be
  217.           applied normally.  (Note: this method cannot detect a
  218.           reversed patch if it is a normal diff and if the first
  219.           command is an append (i.e. it should have been a
  220.           delete) since appends always succeed, due to the fact
  221.           that a null context will match anywhere.  Luckily, most
  222.           patches add or change lines rather than delete them, so
  223.           most reversed normal diffs will begin with a delete,
  224.           which will fail, triggering the heuristic.)
  225.  
  226.      -s   makes patch do its work silently, unless an error
  227.           occurs.
  228.  
  229.      -S   causes patch to ignore this patch from the patch file,
  230.           but continue on looking for the next patch in the file.
  231.           Thus
  232.  
  233.                patch -S + -S + <patchfile
  234.  
  235.           will ignore the first and second of three patches.
  236.  
  237.      -v   causes patch to print out it's revision header and
  238.           patch level.
  239.  
  240.      -x<number>
  241.           sets internal debugging flags, and is of interest only
  242.           to patch patchers.
  243.  
  244. ENVIRONMENT
  245.      No environment variables are used by patch.
  246.  
  247. FILES
  248.      /tmp/patch*
  249.  
  250. SEE ALSO
  251.      diff(1)
  252.  
  253. NOTES FOR PATCH SENDERS
  254.      There are several things you should bear in mind if you are
  255.      going to be sending out patches.  First, you can save people
  256.      a lot of grief by keeping a patchlevel.h file which is
  257.      patched to increment the patch level as the first diff in
  258.      the patch file you send out.  If you put a Prereq: line in
  259.      with the patch, it won't let them apply patches out of order
  260.      without some warning.  Second, make sure you've specified
  261.      the filenames right, either in a context diff header, or
  262.      with an Index: line.  If you are patching something in a
  263.      subdirectory, be sure to tell the patch user to specify a -p
  264.      switch as needed.  Third, you can create a file by sending
  265.      out a diff that compares a null file to the file you want to
  266.      create.  This will only work if the file you want to create
  267.      doesn't exist already in the target directory.  Fourth, take
  268.      care not to send out reversed patches, since it makes people
  269.      wonder whether they already applied the patch.  Fifth, while
  270.      you may be able to get away with putting 582 diff listings
  271.      into one file, it is probably wiser to group related patches
  272.      into separate files in case something goes haywire.
  273.  
  274. DIAGNOSTICS
  275.      Too many to list here, but generally indicative that patch
  276.      couldn't parse your patch file.
  277.  
  278.      The message "Hmm..." indicates that there is unprocessed
  279.      text in the patch file and that patch is attempting to
  280.      intuit whether there is a patch in that text and, if so,
  281.      what kind of patch it is.
  282.  
  283.      Patch will exit with a non-zero status if any reject files
  284.      were created.  When applying a set of patches in a loop it
  285.      behooves you to check this exit status so you don't apply a
  286.      later patch to a partially patched file.
  287.  
  288. CAVEATS
  289.      Patch cannot tell if the line numbers are off in an ed
  290.      script, and can only detect bad line numbers in a normal
  291.      diff when it finds a "change" or a "delete" command.  A con-
  292.      text diff using fuzz factor 3 may have the same problem.
  293.      Until a suitable interactive interface is added, you should
  294.      probably do a context diff in these cases to see if the
  295.      changes made sense.  Of course, compiling without errors is
  296.      a pretty good indication that the patch worked, but not
  297.      always.
  298.  
  299.      Patch usually produces the correct results, even when it has
  300.      to do a lot of guessing.  However, the results are
  301.      guaranteed to be correct only when the patch is applied to
  302.      exactly the same version of the file that the patch was gen-
  303.      erated from.
  304.  
  305. BUGS
  306.      Could be smarter about partial matches, excessively deviant
  307.      offsets and swapped code, but that would take an extra pass.
  308.  
  309.      If code has been duplicated (for instance with #ifdef OLD-
  310.      CODE ... #else ...  #endif), patch is incapable of patching
  311.      both versions, and, if it works at all, will likely patch
  312.      the wrong one, and tell you that it succeeded to boot.
  313.  
  314.      If you apply a patch you've already applied, patch will
  315.      think it is a reversed patch, and offer to un-apply the
  316.      patch.  This could be construed as a feature.
  317.