1.\" $OpenBSD: patch.1,v 1.17 2003/10/31 20:20:45 millert Exp $ 2.\" $DragonFly: src/usr.bin/patch/patch.1,v 1.5 2005/09/06 00:20:09 swildner Exp $ 3.\" 4.\" Copyright 1986, Larry Wall 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following condition 8.\" is met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this condition and the following disclaimer. 11.\" 12.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 13.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 14.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 15.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 16.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 17.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 18.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 19.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 20.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 21.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 22.\" SUCH DAMAGE. 23.\" 24.Dd July 23, 2003 25.Dt PATCH 1 26.Os 27.Sh NAME 28.Nm patch 29.Nd apply a diff file to an original 30.Sh SYNOPSIS 31.Nm patch 32.Op Cm options 33.Op Ar origfile Op Ar patchfile 34.Nm patch 35.Pf \*(Lt Ar patchfile 36.Sh DESCRIPTION 37.Nm 38will take a patch file containing any of the four forms of difference 39listing produced by the 40.Xr diff 1 41program and apply those differences to an original file, 42producing a patched version. 43If 44.Ar patchfile 45is omitted, or is a hyphen, the patch will be read from the standard input. 46.Pp 47.Nm 48will attempt to determine the type of the diff listing, unless over-ruled by a 49.Fl c , 50.Fl e , 51.Fl n , 52or 53.Fl u 54option. 55Context diffs (old-style, new-style, and unified) and 56normal diffs are applied directly by the 57.Nm 58program itself, whereas ed diffs are simply fed to the 59.Xr ed 1 60editor via a pipe. 61.Pp 62If the 63.Ar patchfile 64contains more than one patch, 65.Nm 66will try to apply each of them as if they came from separate patch files. 67This means, among other things, that it is assumed that the name of the file 68to patch must be determined for each diff listing, and that the garbage before 69each diff listing will be examined for interesting things such as file names 70and revision level (see the section on 71.Sx Filename Determination 72below). 73.Pp 74The options are as follows: 75.Bl -tag -width Ds 76.It Fl b , Fl Fl backup 77Save a backup copy of the file before it is modified. 78By default the original file is saved with a backup extension of 79.Qq .orig 80unless the file already has a numbered backup, in which case a numbered 81backup is made. 82This is equivalent to specifying 83.Qo Fl V Ar existing Qc . 84This option is currently the default but that will change in a future release. 85.It Fl B , Fl Fl prefix 86Causes the next argument to be interpreted as a prefix to the backup file 87name. 88If this argument is specified, any argument to 89.Fl z 90will be ignored. 91.It Fl c , Fl Fl context 92Forces 93.Nm 94to interpret the patch file as a context diff. 95.It Fl C , Fl Fl check 96Checks that the patch would apply cleanly, but does not modify anything. 97.It Fl d , Fl Fl directory 98Causes 99.Nm 100to interpret the next argument as a directory, and 101.Xr cd 1 102to it before doing anything else. 103.It Fl D , Fl Fl ifdef 104Causes 105.Nm 106to use the 107.Qq #ifdef...#endif 108construct to mark changes. 109The argument following will be used as the differentiating symbol. 110Note that, unlike the C compiler, there must be a space between the 111.Fl D 112and the argument. 113.It Fl e , Fl Fl ed 114Forces 115.Nm 116to interpret the patch file as an 117.Xr ed 1 118script. 119.It Fl E , Fl Fl remove-empty-files 120Causes 121.Nm 122to remove output files that are empty after the patches have been applied. 123This option is useful when applying patches that create or remove files. 124.It Fl f , Fl Fl force 125Forces 126.Nm 127to assume that the user knows exactly what he or she is doing, and to not 128ask any questions. 129It assumes the following: 130skip patches for which a file to patch can't be found; 131patch files even though they have the wrong version for the 132.Qq Prereq: 133line in the patch; 134and assume that patches are not reversed even if they look like they are. 135This option does not suppress commentary; use 136.Fl s 137for that. 138.It Xo 139.Fl F Ns Aq Ar number , 140.Fl Fl fuzz Aq Ar number 141.Xc 142Sets the maximum fuzz factor. 143This option only applies to context diffs, and causes 144.Nm 145to ignore up to that many lines in looking for places to install a hunk. 146Note that a larger fuzz factor increases the odds of a faulty patch. 147The default fuzz factor is 2, and it may not be set to more than 148the number of lines of context in the context diff, ordinarily 3. 149.It Fl i , Fl Fl input 150Causes the next argument to be interpreted as the input file name 151(i.e. a patchfile). 152This option may be specified multiple times. 153.It Fl l , Fl Fl ignore-whitespace 154Causes the pattern matching to be done loosely, in case the tabs and 155spaces have been munged in your input file. 156Any sequence of whitespace in the pattern line will match any sequence 157in the input file. 158Normal characters must still match exactly. 159Each line of the context must still match a line in the input file. 160.It Fl n , Fl Fl normal 161Forces 162.Nm 163to interpret the patch file as a normal diff. 164.It Fl N , Fl Fl forward 165Causes 166.Nm 167to ignore patches that it thinks are reversed or already applied. 168See also 169.Fl R . 170.It Fl o , Fl Fl output 171Causes the next argument to be interpreted as the output file name. 172.It Xo 173.Fl p Ns Aq Ar number , 174.Fl Fl strip Aq Ar number 175.Xc 176Sets the pathname strip count, 177which controls how pathnames found in the patch file are treated, 178in case you keep your files in a different directory than the person who sent 179out the patch. 180The strip count specifies how many slashes are to be stripped from 181the front of the pathname. 182(Any intervening directory names also go away.) 183For example, supposing the file name in the patch file was 184.Pa /u/howard/src/blurfl/blurfl.c : 185.Pp 186Setting 187.Fl p Ns Ar 0 188gives the entire pathname unmodified. 189.Pp 190.Fl p Ns Ar 1 191gives 192.Pp 193.D1 Pa u/howard/src/blurfl/blurfl.c 194.Pp 195without the leading slash. 196.Pp 197.Fl p Ns Ar 4 198gives 199.Pp 200.D1 Pa blurfl/blurfl.c 201.Pp 202Not specifying 203.Fl p 204at all just gives you 205.Pa blurfl.c , 206unless all of the directories in the leading path 207.Pq Pa u/howard/src/blurfl 208exist and that path is relative, 209in which case you get the entire pathname unmodified. 210Whatever you end up with is looked for either in the current directory, 211or the directory specified by the 212.Fl d 213option. 214.It Fl r , Fl Fl reject-file 215Causes the next argument to be interpreted as the reject file name. 216.It Fl R , Fl Fl reverse 217Tells 218.Nm 219that this patch was created with the old and new files swapped. 220(Yes, I'm afraid that does happen occasionally, human nature being what it 221is.) 222.Nm 223will attempt to swap each hunk around before applying it. 224Rejects will come out in the swapped format. 225The 226.Fl R 227option will not work with ed diff scripts because there is too little 228information to reconstruct the reverse operation. 229.Pp 230If the first hunk of a patch fails, 231.Nm 232will reverse the hunk to see if it can be applied that way. 233If it can, you will be asked if you want to have the 234.Fl R 235option set. 236If it can't, the patch will continue to be applied normally. 237(Note: this method cannot detect a reversed patch if it is a normal diff 238and if the first command is an append (i.e. it should have been a delete) 239since appends always succeed, due to the fact that a null context will match 240anywhere. 241Luckily, most patches add or change lines rather than delete them, so most 242reversed normal diffs will begin with a delete, which will fail, triggering 243the heuristic.) 244.It Xo 245.Fl s , Fl Fl quiet , 246.Fl Fl silent 247.Xc 248Makes 249.Nm 250do its work silently, unless an error occurs. 251.It Fl t , Fl Fl batch 252Similar to 253.Fl f , 254in that it suppresses questions, but makes some different assumptions: 255skip patches for which a file to patch can't be found (the same as 256.Fl f ) ; 257skip patches for which the file has the wrong version for the 258.Qq Prereq: 259line in the patch; 260and assume that patches are reversed if they look like they are. 261.It Fl u , Fl Fl unified 262Forces 263.Nm 264to interpret the patch file as a unified context diff (a unidiff). 265.It Fl v , Fl Fl version 266Causes 267.Nm 268to print out its revision header and patch level. 269.It Fl V , Fl Fl version-control 270Causes the next argument to be interpreted as a method for creating 271backup file names. 272The type of backups made can also be given in the 273.Ev PATCH_VERSION_CONTROL 274or 275.Ev VERSION_CONTROL 276environment variables, which are overridden by this option. 277The 278.Fl B 279option overrides this option, causing the prefix to always be used for 280making backup file names. 281The values of the 282.Ev PATCH_VERSION_CONTROL 283and 284.Ev VERSION_CONTROL 285environment variables and the argument to the 286.Fl V 287option are like the GNU Emacs 288.Dq version-control 289variable; they also recognize synonyms that are more descriptive. 290The valid values are (unique abbreviations are accepted): 291.Bl -tag -width Ds -offset indent 292.It t , numbered 293Always make numbered backups. 294.It nil , existing 295Make numbered backups of files that already have them, 296simple backups of the others. 297.It never , simple 298Always make simple backups. 299.El 300.It Xo 301.Fl x Ns Aq Ar number , 302.Fl Fl debug Aq Ar number 303.Xc 304Sets internal debugging flags, and is of interest only to 305.Nm 306patchers. 307.It Fl z , Fl Fl suffix 308Causes the next argument to be interpreted as the backup extension, to be 309used in place of 310.Qq .orig . 311.It Fl Fl posix 312Enables strict 313.St -p1003.2 314conformance, specifically: 315.Bl -enum 316.It 317Backup files are not created unless the 318.Fl b 319option is specified. 320.It 321If unspecified, the file name used is the first of the old, new and 322index files that exists. 323.El 324.El 325.Ss Patch Application 326.Nm 327will try to skip any leading garbage, apply the diff, 328and then skip any trailing garbage. 329Thus you could feed an article or message containing a 330diff listing to 331.Nm patch , 332and it should work. 333If the entire diff is indented by a consistent amount, 334this will be taken into account. 335.Pp 336With context diffs, and to a lesser extent with normal diffs, 337.Nm 338can detect when the line numbers mentioned in the patch are incorrect, 339and will attempt to find the correct place to apply each hunk of the patch. 340As a first guess, it takes the line number mentioned for the hunk, plus or 341minus any offset used in applying the previous hunk. 342If that is not the correct place, 343.Nm 344will scan both forwards and backwards for a set of lines matching the context 345given in the hunk. 346First 347.Nm 348looks for a place where all lines of the context match. 349If no such place is found, and it's a context diff, and the maximum fuzz factor 350is set to 1 or more, then another scan takes place ignoring the first and last 351line of context. 352If that fails, and the maximum fuzz factor is set to 2 or more, 353the first two and last two lines of context are ignored, 354and another scan is made. 355.Pq The default maximum fuzz factor is 2. 356.Pp 357If 358.Nm 359cannot find a place to install that hunk of the patch, it will put the hunk 360out to a reject file, which normally is the name of the output file plus 361.Qq .rej . 362The line numbers on the hunks in the reject file may be different than 363in the patch file: they reflect the approximate location patch thinks the 364failed hunks belong in the new file rather than the old one. 365.Pp 366As each hunk is completed, you will be told whether the hunk succeeded or 367failed, and which line (in the new file) 368.Nm 369thought the hunk should go on. 370If this is different from the line number specified in the diff, 371you will be told the offset. 372A single large offset MAY be an indication that a hunk was installed in the 373wrong place. 374You will also be told if a fuzz factor was used to make the match, in which 375case you should also be slightly suspicious. 376.Ss Filename Determination 377If no original file is specified on the command line, 378.Nm 379will try to figure out from the leading garbage what the name of the file 380to edit is. 381When checking a prospective file name, pathname components are stripped 382as specified by the 383.Fl p 384option and the file's existence and writability are checked relative 385to the current working directory (or the directory specified by the 386.Fl d 387option). 388.Pp 389If the diff is a context or unified diff, 390.Nm 391is able to determine the old and new file names from the diff header. 392For context diffs, the 393.Dq old 394file is specified in the line beginning with 395.Qq *** 396and the 397.Dq new 398file is specified in the line beginning with 399.Qq --- . 400For a unified diff, the 401.Dq old 402file is specified in the line beginning with 403.Qq --- 404and the 405.Dq new 406file is specified in the line beginning with 407.Qq +++ . 408If there is an 409.Qq Index: 410line in the leading garbage (regardless of the diff type), 411.Nm 412will use the file name from that line as the 413.Dq index 414file. 415.Pp 416.Nm 417will choose the file name by performing the following steps, with the first 418match used: 419.Bl -enum 420.It 421If 422.Nm 423is operating in strict 424.St -p1003.2 425mode, the first of the 426.Dq old , 427.Dq new 428and 429.Dq index 430file names that exist is used. 431Otherwise, 432.Nm 433will examine either the 434.Dq old 435and 436.Dq new 437file names or, for a non-context diff, the 438.Dq index 439file name, and choose the file name with the fewest path components, 440the shortest basename, and the shortest total file name length (in that order). 441.It 442If no file exists, 443.Nm 444checks for the existence of the files in an SCCS or RCS directory 445(using the appropriate prefix or suffix) using the criteria specified 446above. 447If found, 448.Nm 449will attempt to get or check out the file. 450.It 451If no suitable file was found to patch, the patch file is a context or 452unified diff, and the old file was zero length, the new file name is 453created and used. 454.It 455If the file name still cannot be determined, 456.Nm 457will prompt the user for the file name to use. 458.El 459.Pp 460Additionally, if the leading garbage contains a 461.Qq Prereq:\ \& 462line, 463.Nm 464will take the first word from the prerequisites line (normally a version 465number) and check the input file to see if that word can be found. 466If not, 467.Nm 468will ask for confirmation before proceeding. 469.Pp 470The upshot of all this is that you should be able to say, while in a news 471interface, the following: 472.Pp 473.Dl | patch -d /usr/src/local/blurfl 474.Pp 475and patch a file in the blurfl directory directly from the article containing 476the patch. 477.Ss Backup Files 478By default, the patched version is put in place of the original, with 479the original file backed up to the same name with the extension 480.Qq .orig , 481or as specified by the 482.Fl B , 483.Fl V , 484or 485.Fl z 486options. 487The extension used for making backup files may also be specified in the 488.Ev SIMPLE_BACKUP_SUFFIX 489environment variable, which is overridden by the options above. 490.Pp 491If the backup file is a symbolic or hard link to the original file, 492.Nm 493creates a new backup file name by changing the first lowercase letter 494in the last component of the file's name into uppercase. 495If there are no more lowercase letters in the name, 496it removes the first character from the name. 497It repeats this process until it comes up with a 498backup file that does not already exist or is not linked to the original file. 499.Pp 500You may also specify where you want the output to go with the 501.Fl o 502option; if that file already exists, it is backed up first. 503.Ss Notes For Patch Senders 504There are several things you should bear in mind if you are going to 505be sending out patches: 506.Pp 507First, you can save people a lot of grief by keeping a 508.Pa patchlevel.h 509file which is patched to increment the patch level as the first diff in the 510patch file you send out. 511If you put a 512.Qq Prereq: 513line in with the patch, it won't let them apply 514patches out of order without some warning. 515.Pp 516Second, make sure you've specified the file names right, either in a 517context diff header, or with an 518.Qq Index: 519line. 520If you are patching something in a subdirectory, be sure to tell the patch 521user to specify a 522.Fl p 523option as needed. 524.Pp 525Third, you can create a file by sending out a diff that compares a 526null file to the file you want to create. 527This will only work if the file you want to create doesn't exist already in 528the target directory. 529.Pp 530Fourth, take care not to send out reversed patches, since it makes people wonder 531whether they already applied the patch. 532.Pp 533Fifth, while you may be able to get away with putting 582 diff listings into 534one file, it is probably wiser to group related patches into separate files in 535case something goes haywire. 536.Sh ENVIRONMENT 537.Bl -tag -width "PATCH_VERSION_CONTROL" -compact 538.It Ev POSIXLY_CORRECT 539When set, 540.Nm 541behaves as if the 542.Fl Fl posix 543option has been specified. 544.It Ev SIMPLE_BACKUP_SUFFIX 545Extension to use for backup file names instead of 546.Qq .orig . 547.It Ev TMPDIR 548Directory to put temporary files in; default is 549.Pa /tmp . 550.It Ev PATCH_VERSION_CONTROL 551Selects when numbered backup files are made. 552.It Ev VERSION_CONTROL 553Same as 554.Ev PATCH_VERSION_CONTROL . 555.El 556.Sh FILES 557.Bl -tag -width "$TMPDIR/patch*" -compact 558.It Pa $TMPDIR/patch* 559.Nm 560temporary files 561.It Pa /dev/tty 562used to read input when 563.Nm 564prompts the user 565.El 566.Sh DIAGNOSTICS 567Too many to list here, but generally indicative that 568.Nm 569couldn't parse your patch file. 570.Pp 571The message 572.Qq Hmm... 573indicates that there is unprocessed text in the patch file and that 574.Nm 575is attempting to intuit whether there is a patch in that text and, if so, 576what kind of patch it is. 577.Pp 578The 579.Nm 580utility exits with one of the following values: 581.Pp 582.Bl -tag -width Ds -compact -offset indent 583.It \&0 584Successful completion. 585.It \&1 586One or more lines were written to a reject file. 587.It \*[Gt]\&1 588An error occurred. 589.El 590.Pp 591When applying a set of patches in a loop it behooves you to check this 592exit status so you don't apply a later patch to a partially patched file. 593.Sh SEE ALSO 594.Xr diff 1 595.Sh AUTHORS 596.An Larry Wall 597with many other contributors. 598.Sh CAVEATS 599.Nm 600cannot tell if the line numbers are off in an ed script, and can only detect 601bad line numbers in a normal diff when it finds a 602.Qq change 603or a 604.Qq delete 605command. 606A context diff using fuzz factor 3 may have the same problem. 607Until a suitable interactive interface is added, you should probably do 608a context diff in these cases to see if the changes made sense. 609Of course, compiling without errors is a pretty good indication that the patch 610worked, but not always. 611.Pp 612.Nm 613usually produces the correct results, even when it has to do a lot of 614guessing. 615However, the results are guaranteed to be correct only when the patch is 616applied to exactly the same version of the file that the patch was 617generated from. 618.Sh BUGS 619Could be smarter about partial matches, excessively deviant offsets and 620swapped code, but that would take an extra pass. 621.Pp 622Check patch mode 623.Pq Fl C 624will fail if you try to check several patches in succession that build on 625each other. 626The entire 627.Nm 628code would have to be restructured to keep temporary files around so that it 629can handle this situation. 630.Pp 631If code has been duplicated (for instance with #ifdef OLDCODE ... #else ... 632#endif), 633.Nm 634is incapable of patching both versions, and, if it works at all, will likely 635patch the wrong one, and tell you that it succeeded to boot. 636.Pp 637If you apply a patch you've already applied, 638.Nm 639will think it is a reversed patch, and offer to un-apply the patch. 640This could be construed as a feature. 641