[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14. Invoking patch

Normally patch is invoked like this:

 
patch <patchfile

The full format for invoking patch is:

 
patch options… [origfile [patchfile]]

If you do not specify patchfile, or if patchfile is `-', patch reads the patch (that is, the diff output) from the standard input.

If you do not specify an input file on the command line, patch tries to figure out from the leading text (any text in the patch that comes before the diff output) which file to edit See section Multiple Patches in a File.

By default, patch replaces the original input file with the patched version, after renaming the original file into a backup file (see section Backup File Names, for a description of how patch names backup files). You can also specify where to put the output with the `-o output-file' or `--output=output-file' option.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.1 Applying Patches in Other Directories

The `-d directory' or `--directory=directory' option to patch makes directory directory the current directory for interpreting both file names in the patch file, and file names given as arguments to other options (such as `-B' and `-o'). For example, while in a news reading program, you can patch a file in the `/usr/src/emacs' directory directly from the article containing the patch like this:

 
| patch -d /usr/src/emacs

Sometimes the file names given in a patch contain leading directories, but you keep your files in a directory different from the one given in the patch. In those cases, you can use the `-pnumber' or `--stripnumber' option to set the file name strip count to number. The strip count tells patch how many slashes, along with the directory names between them, to strip from the front of file names. By default, patch strips off all leading directories, leaving just the base file names, except that when a file name given in the patch is a relative file name and all of its leading directories already exist, patch does not strip off the leading directory. (A relative file name is one that does not start with a slash.)

patch looks for each file (after any slashes have been stripped) in the current directory, or if you used the `-d directory' option, in that directory.

For example, suppose the file name in the patch file is `/gnu/src/emacs/etc/NEWS'. Using `-p' or `-p0' gives the entire file name unmodified, `-p1' gives `gnu/src/emacs/etc/NEWS' (no leading slash), `-p4' gives `etc/NEWS', and not specifying `-p' at all gives `NEWS'.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.2 Backup File Names

Normally, patch renames an original input file into a backup file by appending to its name the extension `.orig', or `~' on systems that do not support long file names. The `-z backup-suffix' or `--suffix=backup-suffix' option uses backup-suffix as the backup extension instead.

Alternately, you can specify the extension for backup files with the SIMPLE_BACKUP_SUFFIX environment variable, which the options override.

`--backup-if-mismatch' makes patch create backups for mismatched files. This is the non-POSIX default. `--no-backup-if-mismatch' prevents patch from creating backups for mismatched files. This is the POSIX default.

patch can also create numbered backup files. With this method, instead of having a single backup of each file, patch makes a new backup file name each time it patches a file. For example, the backups of a file named `sink' would be called, successively, `sink.~1~', `sink.~2~', `sink.~3~', etc.

The `-V backup-style' or `--version-control=backup-style' option takes as an argument a method for creating backup file names. You can alternately control the type of backups that patch makes with the VERSION_CONTROL environment variable, which the `-V' option overrides. The VERSION_CONTROL environment variable and the argument to the `-V' option also recognize synonyms that are more descriptive. The valid values are listed below; unique abbreviations are acceptable.

`t'
`numbered'

Always make numbered backups.

`nil'
`existing'

Make numbered backups of files that already have them, simple backups of the others. This is the default.

`never'
`simple'

Always make simple backups.

Alternately, you can tell patch to prepend a prefix, such as a directory name, to produce backup file names. The `-B backup-prefix' or `--prefix=backup-prefix' option makes backup files by prepending backup-prefix to them. If you use this option, patch ignores any `-b' option that you give.

If the backup file already exists, patch creates a new backup file name by changing the first lowercase letter in the last component of the file name into uppercase. If there are no more lowercase letters in the name, it removes the first character from the name. It repeats this process until it comes up with a backup file name that does not already exist.

If you specify the output file with the `-o' option, that file is the one that is backed up, not the input file.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.3 Reject File Names

The names for reject files (files containing patches that patch could not find a place to apply) are normally the name of the output file with `.rej' appended (or `#' on systems that do not support long file names).

Alternatively, you can tell patch to place all of the rejected patches in a single file. The `-r reject-file' or `--reject-file=reject-file' option uses reject-file as the reject file name.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.4 patch Quoting Style

`--quoting-style=word' determines how file names are quoted in output. word must be one of:

`escape'

Use `C' quoting but omit the double quotes.

`c'

Use `C' quoting.

`literal'

No quoting at all.

`shell'

Quote as for the shell when shell metacharacters are present.

`shell-always'

Quote as for the shell at all times.

`shell' is the default.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.5 Revision Control

-g num or -get=num controls patch's actions when a file is under RCS or SCCS control, and does not exist or is read-only and matches the default version, or when a file is under ClearCase control and does not exist. If num is positive, patch gets (or checks out) the file from the revision control system; if zero, patch ignores RCS, ClearCase, and SCCS and does not get the file; and if negative, patch asks the user whether to get the file. The default value of this option is given by the value of the PATCH_GET environment variable if it is set; if not, the default value is zero if patch is conforming to POSIX, negative otherwise.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.6 Patching Time Stamps

Patch can "fake" the modification time stamps on files it produces, using the time stamps in context diff headers. This can be useful with programs such as make which depend on these time stamps to decide what files need to be rebuilt.

`-T' or `--set-time' sets the modification and access times of patched files from time stamps given in context diff headers, assuming that the context diff headers use local time. This option is not recommended, because patches using local time cannot easily be used by people in other time zones, and because local time stamps are ambiguous when local clocks move backwards during daylight-saving time adjustments.

`-Z' or `--set-utc' sets the modification and access times of patched files from time stamps given in context diff headers, assuming that the context diff headers use Coordinated Universal Time (UTC, often known as GMT). Also see the -T or -set-time option.

The `-Z' or `--set-utc' and `-T' or `--set-time' options normally refrain from setting a file's time if the file's original time does not match the time given in the patch header, or if its contents do not match the patch exactly. However, if the `-f' or `--force option' is given, the file time is set regardless.

Due to the limitations of diff output format, these options cannot update the times of files whose contents have not changed. Also, if you use these options, you should remove (e.g. with make clean) all files that depend on the patched files, so that later invocations of make do not get confused by the patched files' times.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

14.7 Options to patch

Here is a summary of all of the options that patch accepts. Older versions of patch do not accept long-named options or the `-t', `-E', or `-V' options.

Multiple single-letter options that do not take an argument can be combined into a single command line argument (with only one dash). Brackets ([ and ]) indicate that an option takes an optional argument.

`-b'
`--backup'

Always back up originals. See section Backup File Names.

`-B backup-prefix'
`--prefix=backup-prefix'

Use backup-prefix as a prefix to the backup file name. See section Backup File Names.

`--backup-if-mismatch'

Make a backup of the original file if the patch does not match it. This is the non-POSIX default. See section Backup File Names.

`--binary'

Use binary mode for all files (except /dev/tty and standard output). Has no effect on POSIX systems such as Linux. See section Binary Files and Forcing Text Comparisons.

`-c'
`--context'

Interpret the patch file as a context diff. See section Selecting the patch Input Format.

`-d directory'
`--directory=directory'

Makes directory directory the current directory for interpreting both file names in the patch file, and file names given as arguments to other options. See section Applying Patches in Other Directories.

`-D name'
`--ifdef=name'

Make merged if-then-else output using format. See section Merging Files with If-then-else.

`--dry-run'

Display the results of patching the files without actually doing so.

`-e'
`--ed'

Interpret the patch file as an ed script. See section Selecting the patch Input Format.

`-E'
`--remove-empty-files'

Remove output files that are empty after the patches have been applied. See section Removing Empty Files.

`-f'
`--force'

Assume that the user knows exactly what he or she is doing, and do not ask any questions. See section Messages and Questions from patch.

`-F lines'
`--fuzz=lines'

Set the maximum fuzz factor to lines. See section Helping patch Find Inexact Matches.

`-g'
`--get=num'

When num is positive get input files from a revision control system. When it is zero do not do so. When it is negative ask the user.

`--help'

Print a summary of the options that patch recognizes, then exit.

`-i patchfile'
`--input=patchfile'

Get the patch from patchfile rather than standard input. See section Options to patch.

`-l'
`--ignore-whitespace'

Let any sequence of white space in the patch file match any sequence of white space in the input file. See section Applying Patches with Changed White Space.

`-n'
`--normal'

Interpret the patch file as a normal diff. See section Selecting the patch Input Format.

`-N'
`--forward'

Ignore patches that patch thinks are reversed or already applied. See also `-R'. See section Applying Reversed Patches.

`--no-backup-if-mismatch'

Don't backup of the original file if the patch does not match it. This is the POSIX default. See section Backup File Names.

`-o output-file'
`--output=output-file'

Use output-file as the output file name. See section Options to patch.

`-p number'
`--strip=number'

Set the file name strip count to number. See section Applying Patches in Other Directories.

`--posix'

Comply with POSIX requirements.

`--quoting-style=word'

Quote names in diagnostics with style word. See section patch Quoting Style.

`-r reject-file'
`--reject-file=reject-file'

Use reject-file as the reject file name. See section Reject File Names.

`-R'
`--reverse'

Assume that this patch was created with the old and new files swapped. See section Applying Reversed Patches.

`-s'
`--silent'
`--quiet'

Work silently unless an error occurs. See section Messages and Questions from patch.

`-t'
`--batch'

Do not ask any questions. See section Messages and Questions from patch.

`-T'
`--set-time'

Use the time stamps in context diff headers to set the access and modification times of patched files. Assume local time. See section Patching Time Stamps.

`-u'
`--unified'

Interpret the patch file as a unified diff. See section Selecting the patch Input Format.

`-v'
`--version'

Output the revision header and patch level of patch.

`-V backup-style'
`--version-control=backup-style'

Select the kind of backups to make. See section Backup File Names.

`--verbose'

Print extra diagnostics. See section Messages and Questions from patch.

`-x number'
`--debug=number'

Set internal debugging flags. Of interest only to patch patchers.

`-Y prefix'
`--basename-prefix=prefix'

Prepend base names of backup files with prefix. See section Backup File Names.

`-z backup-suffix'
`--suffix=backup-suffix'

Use backup-suffix as the backup extension instead of `.orig' or `~'. See section Backup File Names.

`-Z'
`--set-utc'

Use the time stamps in context diff headers to set the access and modification times of patched files. Assume UTC. See section Patching Time Stamps.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Build Daemon user on October, 25 2005 using texi2html 1.76.