Template:Short description Template:Lowercase {{#invoke:Infobox|infobox}}Template:Template other{{#invoke:Check for unknown parameters | check | showblankpositional=1 | unknown = Template:Main other | preview = Page using Template:Infobox software with unknown parameter "_VALUE_"|ignoreblank=y | AsOf | author | background | bodystyle | caption | collapsetext | collapsible | developer | discontinued | engine | engines | genre | included with | language | language count | language footnote | latest preview date | latest preview version | latest release date | latest release version | latest_preview_date | latest_preview_version | latest_release_date | latest_release_version | licence | license | logo | logo alt | logo caption | logo upright | logo size | logo title | logo_alt | logo_caption | logo_upright | logo_size | logo_title | middleware | module | name | operating system | operating_system | other_names | platform | programming language | programming_language | released | replaced_by | replaces | repo | screenshot | screenshot alt | screenshot upright | screenshot size | screenshot title | screenshot_alt | screenshot_upright | screenshot_size | screenshot_title | service_name | size | standard | title | ver layout | website | qid }}Template:Main other The computer tool patch is a Unix program that updates text files according to instructions contained in a separate file, called a patch file. The patch file (also called a patch for short) is a text file that consists of a list of differences and is produced by running the related diff program with the original and updated file as arguments. Updating files with patch is often referred to as applying the patch or simply patching the files.
HistoryEdit
The original patch program was written by Larry Wall (who went on to create the Perl programming language) and posted to mod.sources
<ref>Template:Cite newsgroup</ref> (which later became comp.sources.unix
) in May 1985.
patch was added to XPG4, which later became POSIX.<ref>Template:Man</ref> Wall's code remains the basis of "patch" programs provided in OpenBSD,<ref>http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/patch/ Template:Webarchive OpenBSD patch source</ref> FreeBSD,<ref>Template:Man</ref> and schilytools.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>{{ safesubst:#invoke:Unsubst||date=__DATE__ |$B= Template:Fix }} The Open Software Foundation, which merged into The Open Group, is said to have maintained a derived version.{{ safesubst:#invoke:Unsubst||date=__DATE__ |$B= Template:Fix }}
The GNU 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.<ref>{{#invoke:citation/CS1|citation |CitationClass=web }}</ref>
Usage contextEdit
Developed by a programmer for other programmers, patch was frequently used for updating source 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 with binary patches, which, although can be conceptually similar, are distributed to update binary files comprising the program to a new release.
Patches in software developmentEdit
The diff files that serve as input to patch are readable text files, which means that they can be easily reviewed or modified by humans before use.
In addition to the "diff" program, diffs can also be produced by other programs, such as Subversion, CVS, RCS, Mercurial and Git.
Patches have been the crucial component of many source control systems, including CVS.
Advanced diffsEdit
Template:Further 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 with context, 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 to open-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.
Usage examplesEdit
To create a patch, one could run the following command in a shell: <syntaxhighlight lang="console"> $ diff -u oldFile newFile > mods.diff # -u tells diff to output unified diff format </syntaxhighlight> To apply a patch, one could run the following command in a shell:
<syntaxhighlight lang="console"> $ patch < mods.diff </syntaxhighlight>
This tells patch to apply the changes to the specified files described in mods.diff
. Patches to files in subdirectories require the additional -pnumber
option, where number is 1 if the base directory of the source tree is included in the diff, and 0 otherwise.
Patches can be undone, or reversed, with the '-R
' option:
<syntaxhighlight lang="console"> $ patch -R < mods.diff </syntaxhighlight>
In some cases when the file is not identical to the version the diff was generated against, the patch will not be able to be applied cleanly. 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 of context (for context and unified diffs) are altered; this is described as fuzz.
Ports of patchEdit
Originally written for Unix and Unix-like systems, patch has also been ported to Windows and many other platforms. Windows ports of patch are provided by GnuWin32 and UnxUtils.
A patch
command is also part of ASCII's MSX-DOS2 Tools for MSX-DOS version 2.<ref>MSX-DOS2 Tools User's Manual by ASCII Corporation</ref>
See alsoEdit
- Patch (computing)
- Quilt (software)
- rsync
- xdelta
- List of Unix commands
- IBM Mainframe utility IEBUPDTE a mainframe patch program, created about 20 years earlier (circa ~1964).
ReferencesEdit
External linksEdit
- GNU Diffutils (includes diff and patch); Documentation
- GNU tools for Win32Template:Spaced ndashWin32 port of tools, including diff and patch
- {{#invoke:citation/CS1|citation
|CitationClass=web }}