| patch | |
|---|---|
A screenshot of using patch | |
| Original author | Larry Wall |
| Developers | Paul Eggert, Wayne Davison, David MacKenzie, Andreas Grünbacher |
| Initial release | May 24, 1985; 40 years ago (1985-05-24) |
| Operating system | Unix andUnix-like,Plan 9,MSX-DOS,Microsoft Windows |
| Platform | Cross-platform |
| Type | Command |
patch is ashellcommand that updatestext files according to instructions in a separate file, called apatch file. Thepatch file is a text file that lists the differences between the input file and the desired content. The command is designed to support patch files created viadiff. A user creates a patch file by runningdiff for two versions of a file (original and target) which produces a list of differences thatpatch can later use to generate the target file from the original file plus the patch file. The termpatch is also a verb – meaning to apply a patch.
Developed by a programmer for other programmers, patch was frequently used for updatingsource code to a newer version. Because of this, many people came to associate patches with source code, whereas patches can in fact be applied to any text.Patched files do not accumulate any unneeded text, which is what some people perceive based on the English meaning of the word; patch is as capable of removing text as it is of adding it.
Patches described here should not be confused withbinary patches, which, although can be conceptually similar, are distributed to update binary files comprising the program to a new release.
The originalpatchprogram was written byLarry Wall (who went on to create thePerlprogramming language) and posted tomod.sources[1] (which later becamecomp.sources.unix) in May 1985.patch was added to XPG4, which later becamePOSIX.[2] Wall's code remains the basis of "patch" programs provided inOpenBSD,[3]FreeBSD,[4] and schilytools.[5][dubious –discuss] TheOpen Software Foundation, which merged intoThe Open Group, is said to have maintained a derived version.[dubious –discuss] TheGNU project/FSF maintains its patch, forked from the Larry Wall version. The repository is different from that of GNU diffutils, but the documentation is managed together.[6] Originally written for Unix, command has also been ported toWindows (viaGnuWin32 andUnxUtils) and many other platforms. An implementation is part ofASCII'sMSX-DOS2 Tools forMSX-DOS version 2.[7]
As patch files are text, they can be used with processes such as quality review and modification via a text editor.
In addition to thediff command, a patch file can be produced by other programs, such asSubversion,CVS,RCS,Mercurial andGit.
Patches have been the crucial component of manysource control systems, includingCVS.
When more advanced diffs are used, patches can be applied even to files that have been modified in the meantime, as long as those modifications do not interfere with the patch. This is achieved by using "context diffs" and "unified diffs" (also known as "unidiffs"), which surround each change withcontext, which is the text immediately before and after the changed part. Patch can then use this context to locate the region to be patched even if it has been displaced by changes earlier in the file, using the line numbers in the diffs as a starting point. Because of this property, context and unified diffs are the preferred form of patches for submission to many software projects.
The above features make diff and patch especially popular for exchanging modifications toopen-source software. Outsiders can download the latest publicly available source code, make modifications to it, and send them, in diff form, to the development team. Using diffs, the development team has the ability to effectively review the patches before applying them, and can apply them to a newer code base than the one the outside developer had access to.
The following command creates a patch file,patchFile, that encodes how to generatenewFile fromoldFile. The-u option selects to output in unified diff format.
$diff-uoldFilenewFile>patchFileThe following command applies the changes to the file identified inpatchFile.
$patch<patchFilePatching a file in a subdirectory requires the additional-pnumber option, wherenumber is 1 if the base directory of the source tree is included in the diff, and 0 otherwise.
A patch can be undone, or reversed, with the-R option:
$patch-R<patchFileIf the file used as the original is significantly different from the actual original file, the patch cannot be cleanly be applied. For example, if lines of text are inserted at the beginning, the line numbers referred to in the patch will be incorrect.patch is able to recover from this, by looking at nearby lines to relocate the text to be patched. It will also recover when lines ofcontext (for context and unified diffs) are altered; this is described asfuzz.
patch – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open GroupFreeBSD – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Group