Patch (Unix)
   HOME

TheInfoList



OR:

The computer tool patch is a
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
program that updates
text file A text file (sometimes spelled textfile; an old alternative name is flat file) is a kind of computer file that is structured as a sequence of lines of electronic text. A text file exists stored as data within a computer file system. In ope ...
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 In computing, the utility diff is a data comparison tool that computes and displays the differences between the contents of files. Unlike edit distance notions used for other purposes, diff is line-oriented rather than character-oriented, but i ...
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.


History

The original patch program was written by
Larry Wall Larry Arnold Wall (born September 27, 1954) is an American computer programmer, linguist, and author known for creating the Perl programming language and the patch tool. Early life and education Wall grew up in Los Angeles and Bremerton, Wash ...
(who went on to create the
Perl Perl is a high-level, general-purpose, interpreted, dynamic programming language. Though Perl is not officially an acronym, there are various backronyms in use, including "Practical Extraction and Reporting Language". Perl was developed ...
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
) and posted to mod.sources (which later became comp.sources.unix) in May 1985. patch was added to XPG4, which later became
POSIX The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
. Wall's code remains the basis of "patch" programs provided in
OpenBSD OpenBSD is a security-focused operating system, security-focused, free software, Unix-like operating system based on the Berkeley Software Distribution (BSD). Theo de Raadt created OpenBSD in 1995 by fork (software development), forking NetBSD ...
,
FreeBSD FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
, and schilytools. The Open Software Foundation, which merged into
The Open Group The Open Group is a global consortium that seeks to "enable the achievement of business objectives" by developing " open, vendor-neutral technology standards and certifications." It has 900+ member organizations and provides a number of services ...
, is said to have maintained a derived version. The
GNU project The GNU Project ( ) is a free software, mass collaboration project announced by Richard Stallman on September 27, 1983. Its goal is to give computer users freedom and control in their use of their computers and Computer hardware, computing dev ...
/ 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.


Usage context

Developed by a programmer for other programmers, patch was frequently used for updating
source code In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer. Since a computer, at base, only ...
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 development

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 In computing, the utility diff is a data comparison tool that computes and displays the differences between the contents of files. Unlike edit distance notions used for other purposes, diff is line-oriented rather than character-oriented, but i ...
" program, diffs can also be produced by other programs, such as
Subversion Subversion () refers to a process by which the values and principles of a system in place are contradicted or reversed in an attempt to sabotage the established social order and its structures of Power (philosophy), power, authority, tradition, h ...
, CVS, RCS, Mercurial and Git. Patches have been the crucial component of many source control systems, including CVS.


Advanced diffs

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 Open-source software (OSS) is Software, computer software that is released under a Open-source license, license in which the copyright holder grants users the rights to use, study, change, and Software distribution, distribute the software an ...
. 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

To create a patch, one could run the following command in a shell: $ diff -u oldFile newFile > mods.diff # -u tells diff to output unified diff format To apply a patch, one could run the following command in a shell: $ patch < mods.diff This tells patch to apply the changes to the specified files described in mods.diff. Patches to files in subdirectories require the additional -p''number'' 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: $ patch -R < mods.diff 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 patch

Originally written for Unix and
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
systems, patch has also been ported to
Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
and many other platforms. Windows ports of patch are provided by GnuWin32 and UnxUtils. A patch command is also part of
ASCII ASCII ( ), an acronym for American Standard Code for Information Interchange, is a character encoding standard for representing a particular set of 95 (English language focused) printable character, printable and 33 control character, control c ...
's ''MSX-DOS2 Tools'' for MSX-DOS version 2.MSX-DOS2 Tools User's Manual by ASCII Corporation
/ref>


See also

*
Patch (computing) A patch is data that is intended to be used to modify an existing software resource such as a computer program, program or a computer file, file, often to fix software bug, bugs and security vulnerability, security vulnerabilities. A patch may b ...
* Quilt (software) * rsync * xdelta *
List of Unix commands This is a list of the shell commands of the most recent version of the Portable Operating System Interface (POSIX) IEEE Std 1003.1-2024 which is part of the Single UNIX Specification (SUS). These commands are implemented in many shells on moder ...
* IBM Mainframe utility IEBUPDTE a mainframe patch program, created about 20 years earlier (circa ~1964).


References


External links


GNU Diffutils
(includes diff and patch)
GNU tools for Win32
in32 port of tools, including diff and patch * {{Unix commands 1984 software Patch utilities Plan 9 commands Unix SUS2008 utilities