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