Patch (Unix)
   HOME

TheInfoList



OR:

The computer tool patch is a
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser 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, and ot ...
program Program, programme, programmer, or programming may refer to: Business and management * Program management, the process of managing several related projects * Time management * Program, a part of planning Arts and entertainment Audio * Progra ...
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 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 it ...
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 and author. He created the Perl programming language. Personal life Wall grew up in Los Angeles and then Bremerton, Washington, before starting higher education at S ...
(who went on to create the
Perl Perl is a family of two high-level, general-purpose, interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it also referred to its redesigned "sister language", Perl 6, before the latter's name was offici ...
programming language) and posted to mod.sources (which later became comp.sources.unix) in May 1985. A variant of the program (but not the only one) is part of 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 computing devices by collaborati ...
and is maintained by the FSF.


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 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 it ...
" 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 transform the established social order and its structures of power, authority, hierarchy, and social norms. Sub ...
, CVS,
RCS RCS may refer to: Organisations *Racing Club de Strasbourg Alsace * Radio Corporation of Singapore *Radcliffe Choral Society * Rawmarsh Community School *Red Crescent Society *Red Cross Society * Representation of Czechs and Slovaks, a football t ...
,
Mercurial Mercurial is a distributed revision control tool for software developers. It is supported on Microsoft Windows and Unix-like systems, such as FreeBSD, macOS, and Linux. Mercurial's major design goals include high performance and scalability, d ...
and
Git Git () is a distributed version control system: tracking changes in any set of files, usually used for coordinating work among programmers collaboratively developing source code during software development. Its goals include speed, data in ...
. 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 computer software that is released under a license in which the copyright holder grants users the rights to use, study, change, and distribute the software and its source code to anyone and for any purpose. Op ...
. 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 systems, patch has also been ported to
Windows Windows is a group of several proprietary graphical operating system families developed and marketed by Microsoft. Each family caters to a certain sector of the computing industry. For example, Windows NT for consumers, Windows Server for serv ...
and many other platforms. Windows ports of patch are provided by
GnuWin32 The G''nu''W''in''32 project provides native ports in the form of executable computer programs, patches, and source code for various GNU and open source tools and software, much of it modified to run on the 32-bit Windows platform. The ports inc ...
and
UnxUtils UnxUtils is a collection of ports of common GNU Unix-like utilities to native Win32, with executables only depending on the Microsoft C- runtime msvcrt.dll. The collection was last updated externally on April 15, 2003, by Karl M. Syring. The mo ...
. A patch command is also part of
ASCII ASCII ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because of ...
's ''MSX-DOS2 Tools'' for
MSX-DOS MSX-DOS is a discontinued disk operating system developed by Microsoft for the 8-bit home computer standard MSX, and is a cross between MS-DOS 1.25 and CP/M-80 2. MSX-DOS MSX-DOS and the extended BASIC with 3½-inch floppy disk support ...
version 2.MSX-DOS2 Tools User's Manual by ASCII Corporation
/ref>


See also

*
Patch (computing) A patch is a set of changes to a computer program or its supporting data designed to update, fix, or improve it. This includes fixing security vulnerabilities and other bugs, with such patches usually being called bugfixes or bug fixes. Patche ...
*
Quilt (software) Quilt is a software utility for managing a series of changes to the source code of any computer program. Such changes are often referred to as " patches" or "patch sets". Quilt can take an arbitrary number of patches as input and condense them ...
*
rsync rsync is a utility for efficiently transferring and synchronizing files between a computer and a storage drive and across networked computers by comparing the modification times and sizes of files. It is commonly found on Unix-like operat ...
*
xdelta xdelta is a command line program for delta encoding, which generates the difference between two files. This is similar to diff and patch, but it is targeted for binary files and does not generate human readable output. It was first released in 1 ...
*
List of Unix commands This is a list of Unix commands as specified by IEEE Std 1003.1-2008, which is part of the Single UNIX Specification (SUS). These commands can be found on Unix operating systems and most Unix-like operating systems. List See also * List of G ...
* 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 Unix SUS2008 utilities Patch utilities