xref: /dragonfly/usr.bin/patch/patch.1 (revision 3641b7ca)
1.\"	$OpenBSD: patch.1,v 1.20 2007/05/31 19:20:14 jmc Exp $
2.\"	$DragonFly: src/usr.bin/patch/patch.1,v 1.9 2007/12/22 19:24:32 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 September 30, 2007
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 Xo
91.Fl B Ar backup-prefix ,
92.Fl Fl prefix Ar backup-prefix
93.Xc
94Causes the next argument to be interpreted as a prefix to the backup file
95name.
96If this argument is specified, any argument to
97.Fl z
98will be ignored.
99.It Fl b , Fl Fl backup
100Save a backup copy of the file before it is modified.
101By default the original file is saved with a backup extension of
102.Qq .orig
103unless the file already has a numbered backup, in which case a numbered
104backup is made.
105This is equivalent to specifying
106.Qo Fl V Cm existing Qc .
107This option is currently the default but that will change in a future release.
108.It Fl C , Fl Fl check
109Checks that the patch would apply cleanly, but does not modify anything.
110.It Fl c , Fl Fl context
111Forces
112.Nm
113to interpret the patch file as a context diff.
114.It Xo
115.Fl D Ar symbol ,
116.Fl Fl ifdef Ar symbol
117.Xc
118Causes
119.Nm
120to use the
121.Qq #ifdef...#endif
122construct to mark changes.
123The argument following will be used as the differentiating symbol.
124Note that, unlike the C compiler, there must be a space between the
125.Fl D
126and the argument.
127.It Xo
128.Fl d Ar directory ,
129.Fl Fl directory Ar directory
130.Xc
131Causes
132.Nm
133to interpret the next argument as a directory, and
134.Xr cd 1
135to 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 can't 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'm 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 can't, 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 can't 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 ,
299.Fl Fl version-control Cm t | nil | never
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.El
331.It Fl v , Fl Fl version
332Causes
333.Nm
334to print out its revision header and patch level.
335.It Xo
336.Fl x Ar number ,
337.Fl Fl debug Ar number
338.Xc
339Sets internal debugging flags, and is of interest only to
340.Nm
341patchers.
342.It Xo
343.Fl z Ar backup-ext ,
344.Fl Fl suffix Ar backup-ext
345.Xc
346Causes the next argument to be interpreted as the backup extension, to be
347used in place of
348.Qq .orig .
349.It Fl Fl posix
350Enables strict
351.St -p1003.2
352conformance, specifically:
353.Bl -enum
354.It
355Backup files are not created unless the
356.Fl b
357option is specified.
358.It
359If unspecified, the file name used is the first of the old, new and
360index files that exists.
361.El
362.El
363.Ss Patch Application
364.Nm
365will try to skip any leading garbage, apply the diff,
366and then skip any trailing garbage.
367Thus you could feed an article or message containing a
368diff listing to
369.Nm ,
370and it should work.
371If the entire diff is indented by a consistent amount,
372this will be taken into account.
373.Pp
374With context diffs, and to a lesser extent with normal diffs,
375.Nm
376can detect when the line numbers mentioned in the patch are incorrect,
377and will attempt to find the correct place to apply each hunk of the patch.
378As a first guess, it takes the line number mentioned for the hunk, plus or
379minus any offset used in applying the previous hunk.
380If that is not the correct place,
381.Nm
382will scan both forwards and backwards for a set of lines matching the context
383given in the hunk.
384First
385.Nm
386looks for a place where all lines of the context match.
387If no such place is found, and it's a context diff, and the maximum fuzz factor
388is set to 1 or more, then another scan takes place ignoring the first and last
389line of context.
390If that fails, and the maximum fuzz factor is set to 2 or more,
391the first two and last two lines of context are ignored,
392and another scan is made.
393.Pq The default maximum fuzz factor is 2.
394.Pp
395If
396.Nm
397cannot find a place to install that hunk of the patch, it will put the hunk
398out to a reject file, which normally is the name of the output file plus
399.Qq .rej .
400(Note that the rejected hunk will come out in context diff form whether the
401input patch was a context diff or a normal diff.
402If the input was a normal diff, many of the contexts will simply be null.)
403The line numbers on the hunks in the reject file may be different than
404in the patch file: they reflect the approximate location patch thinks the
405failed hunks belong in the new file rather than the old one.
406.Pp
407As each hunk is completed, you will be told whether the hunk succeeded or
408failed, and which line (in the new file)
409.Nm
410thought the hunk should go on.
411If this is different from the line number specified in the diff,
412you will be told the offset.
413A single large offset MAY be an indication that a hunk was installed in the
414wrong place.
415You will also be told if a fuzz factor was used to make the match, in which
416case you should also be slightly suspicious.
417.Ss Filename Determination
418If no original file is specified on the command line,
419.Nm
420will try to figure out from the leading garbage what the name of the file
421to edit is.
422When checking a prospective file name, pathname components are stripped
423as specified by the
424.Fl p
425option and the file's existence and writability are checked relative
426to the current working directory (or the directory specified by the
427.Fl d
428option).
429.Pp
430If the diff is a context or unified diff,
431.Nm
432is able to determine the old and new file names from the diff header.
433For context diffs, the
434.Dq old
435file is specified in the line beginning with
436.Qq ***
437and the
438.Dq new
439file is specified in the line beginning with
440.Qq --- .
441For a unified diff, the
442.Dq old
443file is specified in the line beginning with
444.Qq ---
445and the
446.Dq new
447file is specified in the line beginning with
448.Qq +++ .
449If there is an
450.Qq Index:
451line in the leading garbage (regardless of the diff type),
452.Nm
453will use the file name from that line as the
454.Dq index
455file.
456.Pp
457.Nm
458will choose the file name by performing the following steps, with the first
459match used:
460.Bl -enum
461.It
462If
463.Nm
464is operating in strict
465.St -p1003.2
466mode, the first of the
467.Dq old ,
468.Dq new
469and
470.Dq index
471file names that exist is used.
472Otherwise,
473.Nm
474will examine either the
475.Dq old
476and
477.Dq new
478file names or, for a non-context diff, the
479.Dq index
480file name, and choose the file name with the fewest path components,
481the shortest basename, and the shortest total file name length (in that order).
482.It
483If no file exists,
484.Nm
485checks for the existence of the files in an SCCS or RCS directory
486(using the appropriate prefix or suffix) using the criteria specified
487above.
488If found,
489.Nm
490will attempt to get or check out the file.
491.It
492If no suitable file was found to patch, the patch file is a context or
493unified diff, and the old file was zero length, the new file name is
494created and used.
495.It
496If the file name still cannot be determined,
497.Nm
498will prompt the user for the file name to use.
499.El
500.Pp
501Additionally, if the leading garbage contains a
502.Qq Prereq:\ \&
503line,
504.Nm
505will take the first word from the prerequisites line (normally a version
506number) and check the input file to see if that word can be found.
507If not,
508.Nm
509will ask for confirmation before proceeding.
510.Pp
511The upshot of all this is that you should be able to say, while in a news
512interface, the following:
513.Pp
514.Dl | patch -d /usr/src/local/blurfl
515.Pp
516and patch a file in the blurfl directory directly from the article containing
517the patch.
518.Ss Backup Files
519By default, the patched version is put in place of the original, with
520the original file backed up to the same name with the extension
521.Qq .orig ,
522or as specified by the
523.Fl B ,
524.Fl V ,
525or
526.Fl z
527options.
528The extension used for making backup files may also be specified in the
529.Ev SIMPLE_BACKUP_SUFFIX
530environment variable, which is overridden by the options above.
531.Pp
532If the backup file is a symbolic or hard link to the original file,
533.Nm
534creates a new backup file name by changing the first lowercase letter
535in the last component of the file's name into uppercase.
536If there are no more lowercase letters in the name,
537it removes the first character from the name.
538It repeats this process until it comes up with a
539backup file that does not already exist or is not linked to the original file.
540.Pp
541You may also specify where you want the output to go with the
542.Fl o
543option; if that file already exists, it is backed up first.
544.Ss Notes For Patch Senders
545There are several things you should bear in mind if you are going to
546be sending out patches:
547.Pp
548First, you can save people a lot of grief by keeping a
549.Pa patchlevel.h
550file which is patched to increment the patch level as the first diff in the
551patch file you send out.
552If you put a
553.Qq Prereq:
554line in with the patch, it won't let them apply
555patches out of order without some warning.
556.Pp
557Second, make sure you've specified the file names right, either in a
558context diff header, or with an
559.Qq Index:
560line.
561If you are patching something in a subdirectory, be sure to tell the patch
562user to specify a
563.Fl p
564option as needed.
565.Pp
566Third, you can create a file by sending out a diff that compares a
567null file to the file you want to create.
568This will only work if the file you want to create doesn't exist already in
569the target directory.
570.Pp
571Fourth, take care not to send out reversed patches, since it makes people wonder
572whether they already applied the patch.
573.Pp
574Fifth, while you may be able to get away with putting 582 diff listings into
575one file, it is probably wiser to group related patches into separate files in
576case something goes haywire.
577.Sh ENVIRONMENT
578.Bl -tag -width "PATCH_VERSION_CONTROL" -compact
579.It Ev POSIXLY_CORRECT
580When set,
581.Nm
582behaves as if the
583.Fl Fl posix
584option has been specified.
585.It Ev SIMPLE_BACKUP_SUFFIX
586Extension to use for backup file names instead of
587.Qq .orig .
588.It Ev TMPDIR
589Directory to put temporary files in; default is
590.Pa /tmp .
591.It Ev PATCH_VERSION_CONTROL
592Selects when numbered backup files are made.
593.It Ev VERSION_CONTROL
594Same as
595.Ev PATCH_VERSION_CONTROL .
596.El
597.Sh FILES
598.Bl -tag -width "$TMPDIR/patch*" -compact
599.It Pa $TMPDIR/patch*
600.Nm
601temporary files
602.It Pa /dev/tty
603used to read input when
604.Nm
605prompts the user
606.El
607.Sh DIAGNOSTICS
608Too many to list here, but generally indicative that
609.Nm
610couldn't parse your patch file.
611.Pp
612The message
613.Qq Hmm...
614indicates that there is unprocessed text in the patch file and that
615.Nm
616is attempting to intuit whether there is a patch in that text and, if so,
617what kind of patch it is.
618.Pp
619The
620.Nm
621utility exits with one of the following values:
622.Pp
623.Bl -tag -width Ds -compact -offset indent
624.It \&0
625Successful completion.
626.It \&1
627One or more lines were written to a reject file.
628.It \*[Gt]\&1
629An error occurred.
630.El
631.Pp
632When applying a set of patches in a loop it behooves you to check this
633exit status so you don't apply a later patch to a partially patched file.
634.Sh SEE ALSO
635.Xr diff 1
636.Sh STANDARDS
637The
638.Nm
639utility is compliant with the
640.St -p1003.1-2004
641specification,
642but its presence is optional.
643.Pp
644The flags
645.Op Fl CEfstuvBFVxz
646and
647.Op Fl -posix
648are extensions to that specification.
649.Sh AUTHORS
650.An Larry Wall
651with many other contributors.
652.Sh CAVEATS
653.Nm
654cannot tell if the line numbers are off in an ed script, and can only detect
655bad line numbers in a normal diff when it finds a
656.Qq change
657or a
658.Qq delete
659command.
660A context diff using fuzz factor 3 may have the same problem.
661Until a suitable interactive interface is added, you should probably do
662a context diff in these cases to see if the changes made sense.
663Of course, compiling without errors is a pretty good indication that the patch
664worked, but not always.
665.Pp
666.Nm
667usually produces the correct results, even when it has to do a lot of
668guessing.
669However, the results are guaranteed to be correct only when the patch is
670applied to exactly the same version of the file that the patch was
671generated from.
672.Sh BUGS
673Could be smarter about partial matches, excessively deviant offsets and
674swapped code, but that would take an extra pass.
675.Pp
676Check patch mode
677.Pq Fl C
678will fail if you try to check several patches in succession that build on
679each other.
680The entire
681.Nm
682code would have to be restructured to keep temporary files around so that it
683can handle this situation.
684.Pp
685If code has been duplicated (for instance with #ifdef OLDCODE ... #else ...
686#endif),
687.Nm
688is incapable of patching both versions, and, if it works at all, will likely
689patch the wrong one, and tell you that it succeeded to boot.
690.Pp
691If you apply a patch you've already applied,
692.Nm
693will think it is a reversed patch, and offer to un-apply the patch.
694This could be construed as a feature.
695