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