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