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