Patch (Unix): Difference between revisions

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
imported>Nthep
Reverted 1 edit by 27.55.78.105 (talk): Rvv
 
imported>Minerva P. Malveen
Not an improvement, Undid revision 1317849950 by 85.115.166.99 (talk)
 
Line 1: Line 1:
{{Short description|Unix utility to apply changes to text files}}
{{Short description|Command for applying changes to text files}}
{{lowercase|title=patch (Unix)}}
{{lowercase|title=patch (Unix)}}
{{Infobox software
{{Infobox software
Line 18: Line 18:
| website =  
| website =  
}}
}}
The computer tool '''patch''' is a [[Unix]] [[computer program|program]] that updates [[text file]]s 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.
<code>'''patch'''</code> is a [[shell (computing)|shell]] [[command (computing)|command]] that updates [[text file]]s according to instructions in a separate file, called a ''patch file''. The [[patch (computing)|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 {{endash}} meaning to apply a patch.


==History==
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.
The original patch 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
Patches described here should not be confused with [[Patch (computing)|binary patches]], which, although can be conceptually similar, are distributed to update binary files comprising the program to a new release.
 
The original <code>patch</code> [[computer program|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
  |newsgroup=mod.sources
  |title=patch version 1.3
  |title=patch version 1.3
Line 33: Line 36:
  |archive-url=https://web.archive.org/web/20230404223018/https://groups.google.com/g/mod.sources/c/xSQM63e39YY/m/apNNJSkJi0gJ
  |archive-url=https://web.archive.org/web/20230404223018/https://groups.google.com/g/mod.sources/c/xSQM63e39YY/m/apNNJSkJi0gJ
  |archive-date=2023-04-04
  |archive-date=2023-04-04
}}</ref> (which later became <code>comp.sources.unix</code>) in May 1985.
}}</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 }}</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]]/[[Free Software Foundation|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 }}</ref> Originally written for Unix, command has also been ported to [[Microsoft Windows|Windows]] (via [[GnuWin32]] and [[UnxUtils]]) and many other platforms. An implementation is part of [[ASCII Corporation|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>
 
patch 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 }}</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]]/[[Free Software Foundation|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 }}</ref>
 
==Usage context==
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 [[Patch (computing)|binary patches]], which, although can be conceptually similar, are distributed to update binary files comprising the program to a new release.


==Patches in software development==
==Use==
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.
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 "[[diff]]" program, diffs can also be produced by other programs, such as [[Subversion (software)|Subversion]], [[Concurrent Versions System|CVS]], [[Revision Control System|RCS]], [[Mercurial (software)|Mercurial]] and [[Git (software)|Git]].
In addition to the <code>diff</code> command, a patch file can be produced by other programs, such as [[Subversion (software)|Subversion]], [[Concurrent Versions System|CVS]], [[Revision Control System|RCS]], [[Mercurial (software)|Mercurial]] and [[Git (software)|Git]].


Patches have been the crucial component of many [[source control]] systems, including [[Concurrent Versions System|CVS]].
Patches have been the crucial component of many [[source control]] systems, including [[Concurrent Versions System|CVS]].
Line 57: Line 51:
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.
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 examples==
==Examples==
To create a patch, one could run the following command in a shell:
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">
<syntaxhighlight lang="console">
$ diff -u oldFile newFile > mods.diff  # -u tells diff to output unified diff format
$ diff -u oldFile newFile > patchFile
</syntaxhighlight>
</syntaxhighlight>
To apply a patch, one could run the following command in a shell:
 
The following command applies the changes to the file identified in <code>patchFile</code>.


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ patch < mods.diff
$ patch < patchFile
</syntaxhighlight>
</syntaxhighlight>


This tells patch to apply the changes to the specified files described in <code>mods.diff</code>. Patches to files in subdirectories require 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.
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.


Patches can be undone, or reversed, with the '<code>-R</code>' option:
A patch can be undone, or reversed, with the <code>-R</code> option:


<syntaxhighlight lang="console">
<syntaxhighlight lang="console">
$ patch -R < mods.diff
$ patch -R < patchFile
</syntaxhighlight>
</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'''.
If 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. {{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''.
 
==Ports of patch==
Originally written for Unix and [[Unix-like]] systems, patch has also been ported to [[Microsoft Windows|Windows]] and many other platforms. Windows ports of patch are provided by [[GnuWin32]] and [[UnxUtils]].
 
A <code>patch</code> command is also part of [[ASCII Corporation|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>


==See also==
==See also==
* [[Patch (computing)]]
* [[Quilt (software)]]
* [[Quilt (software)]]
* [[rsync]]
* [[rsync]]
* [[xdelta]]
* [[xdelta]]
* [[List of Unix commands]]
* [[List of POSIX commands]]
* [[IBM mainframe utility programs#IEBUPDTE|IBM Mainframe utility IEBUPDTE]] a mainframe patch program, created about 20 years earlier (circa ~1964).
* [[IBM mainframe utility programs#IEBUPDTE|IBM Mainframe utility IEBUPDTE]] a mainframe patch program, created about 20 years earlier (circa ~1964).


Line 97: Line 87:
{{Wikibooks|Guide to Unix|Commands}}
{{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://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]
*[http://gnuwin32.sourceforge.net/ GNU tools for Win32]{{spaced ndash}}Win32 port of tools, including diff and patch
*[https://gnuwin32.sourceforge.net/ GNU tools for Win32]{{spaced ndash}}Win32 port of tools, including diff and patch
* {{cite web
* {{cite web
   |url=https://invisible-island.net/diffstat/
   |url=https://invisible-island.net/diffstat/

Latest revision as of 18:36, 20 October 2025

Template:Short description Template:Lowercase Script error: No such module "Infobox".Template:Template other Script error: No such module "Check for unknown parameters".Script error: No such module "Check for conflicting parameters". patch 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 diff. A user creates a patch file by running diff for two versions of a file (original and target) which produces a list of differences that patch can later use to generate the target file from the original file plus the patch file. The term patch is also a verb

  1. REDIRECT Template:En dash

Template:R protected meaning to apply a patch.

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.

The original patch program was written by Larry Wall (who went on to create the Perl programming language) and posted to mod.sources[1] (which later became comp.sources.unix) in May 1985. patch was added to XPG4, which later became POSIX.[2] Wall's code remains the basis of "patch" programs provided in OpenBSD,[3] FreeBSD,[4] and schilytools.[5]Script error: No such module "Unsubst". The Open Software Foundation, which merged into The Open Group, is said to have maintained a derived version.Script error: No such module "Unsubst". 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.[6] Originally written for Unix, 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.[7]

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 diff 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

Script error: No such module "labelled list hatnote". 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, patchFile, that encodes how to generate newFile from oldFile. The -u option selects to output in unified diff format.

$ diff -u oldFile newFile > patchFile

The following command applies the changes to the file identified in patchFile.

$ patch < patchFile

Patching a file in a subdirectory requires the additional -pnumber 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 -R option:

$ patch -R < patchFile

If 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 of context (for context and unified diffs) are altered; this is described as fuzz.

See also

References

<templatestyles src="Reflist/styles.css" />

  1. Script error: No such module "citation/CS1".
  2. patch – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group
  3. http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/patch/ Template:Webarchive OpenBSD patch source
  4. FreeBSD – Shell and Utilities Reference, The Single UNIX Specification, Version 5 from The Open Group
  5. Script error: No such module "citation/CS1".
  6. Script error: No such module "citation/CS1".
  7. MSX-DOS2 Tools User's Manual by ASCII Corporation

Script error: No such module "Check for unknown parameters".

External links

Template:Sister project

Script error: No such module "Navbox".