{{Short description|Command for applying changes to text files}} {{lowercase}} {{Infobox software | name = patch | logo = | screenshot = TuxOnIce patching.png | screenshot size = | caption = A screenshot of using patch | author = Larry Wall | developer = Paul Eggert, Wayne Davison, David MacKenzie, Andreas Grünbacher | released = {{Start date and age|1985|05|24}} | latest release version = | latest release date = | operating system = Unix and Unix-like, Plan 9, MSX-DOS, Microsoft Windows | platform = Cross-platform | genre = Command | license = | website = }} <code>'''patch'''</code> is a shell command that updates text files according to instructions in a separate file, called a ''patch file''. The patch 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 via <code>diff</code>. A user creates a patch file by running <code>diff</code> for two versions of a file (original and target), which produces a list of differences that <code>patch</code> can later use to generate the target file from the original file plus the patch file. The term ''patch'' is also a verb for applying a patch.
Developed by a programmer for other programmers, <code>patch</code> 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 they can be conceptually similar, are distributed to update binary files comprising the program to a new release.
The original <code>patch</code> program was written by Larry Wall (who went on to create the Perl programming language) and posted to <code>mod.sources</code><ref>{{cite newsgroup|url=https://groups.google.com/g/mod.sources/c/xSQM63e39YY/m/apNNJSkJi0gJ |newsgroup=mod.sources |title=patch version 1.3 |first=Larry |last=Wall |author-link=Larry Wall |date=May 8, 1985 |access-date=2024-07-14 |url-status=live |archive-url=https://web.archive.org/web/20230404223018/https://groups.google.com/g/mod.sources/c/xSQM63e39YY/m/apNNJSkJi0gJ |archive-date=2023-04-04}}</ref> (which later became <code>comp.sources.unix</code>) in May 1985. <code>patch</code> was added to XPG4, which later became POSIX.<ref>{{man|cu|patch|SUS}}</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/ {{Webarchive|url=https://web.archive.org/web/20170607120135/http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/patch/ |date=2017-06-07 }} OpenBSD patch source</ref> FreeBSD,<ref>{{man|1|FreeBSD}}</ref> and schilytools.<ref>{{cite web | url=https://codeberg.org/schilytools/schilytools/src/branch/master/patch/patch.c | title=Schilytools | access-date=2023-05-08 | archive-date=2023-05-08 | archive-url=https://web.archive.org/web/20230508085937/https://codeberg.org/schilytools/schilytools/src/branch/master/patch/patch.c | url-status=live }}</ref>{{dubious|wall-vs-gnu|date=August 2024}} The Open Software Foundation, which merged into The Open Group, is said to have maintained a derived version.{{dubious|wall-vs-gnu|date=August 2024}} 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>{{cite web | url=https://git.savannah.gnu.org/cgit/patch.git/tree/src/patch.c | title=Patch.c\SRC - patch.git - GNU patch | access-date=2023-05-08 | archive-date=2023-05-08 | archive-url=https://web.archive.org/web/20230508085936/https://git.savannah.gnu.org/cgit/patch.git/tree/src/patch.c | url-status=live }}</ref> Originally written for Unix, the command has also been ported to Windows (via GnuWin32 and UnxUtils) and many other platforms. An implementation is part of ASCII's ''MSX-DOS2 Tools'' for MSX-DOS version 2.<ref>[https://archive.org/details/MSXDOS2TOOLS MSX-DOS2 Tools User's Manual by ASCII Corporation]</ref>
==Use== As patch files are text, they can be used with processes such as quality review and modification via a text editor.
In addition to the <code>diff</code> command, a patch file can 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 diffs=== {{further|diff#Variations}} 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.
==Examples== The following command creates a patch file, {{code|patchFile}}, that encodes how to generate {{code|newFile}} from {{code|oldFile}}. The {{code|-u}} option selects to output in unified diff format.
<syntaxhighlight lang="console"> $ diff -u oldFile newFile > patchFile </syntaxhighlight>
The following command applies the changes to the file identified in <code>patchFile</code>.
<syntaxhighlight lang="console"> $ patch < patchFile </syntaxhighlight>
Patching a file in a subdirectory requires the additional <code>-p''number''</code> option, where ''number'' 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 <code>-R</code> option:
<syntaxhighlight lang="console"> $ patch -R < patchFile </syntaxhighlight>
If the file used as the original is significantly different from the actual original file, the patch cannot be cleanly applied. For example, if lines of text are inserted at the beginning, the line numbers referred to in the patch will be incorrect. {{code|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''.
==See also== * Quilt (software) * rsync * xdelta * List of POSIX commands * IBM Mainframe utility IEBUPDTE, a mainframe patch program, created about 20 years earlier (circa ~1964).
==References== {{Reflist}}
==External links== {{Wikibooks|Guide to Unix|Commands}} *[https://www.gnu.org/software/diffutils/ GNU Diffutils] (includes diff and patch); [https://www.gnu.org/software/diffutils/manual/html_node/Merging-with-patch.html#Merging-with-patch Documentation] *[https://gnuwin32.sourceforge.net/ GNU tools for Win32]{{spaced ndash}}Win32 port of tools, including diff and patch * {{cite web|url=https://invisible-island.net/diffstat/ |title=DIFFSTAT – make histogram from diff-output |last=Dickey |first=Thomas E. |access-date=2020-05-01 |url-status=live |archive-url=https://web.archive.org/web/20200122063404/https://invisible-island.net/diffstat/ |archive-date=2020-01-22 |postscript=none}}
{{Unix commands}}
Category:1984 software Category:Patch utilities Category:Plan 9 commands Category:Unix SUS2008 utilities