Original author(s) | Larry Wall |
---|---|
Developer(s) | Paul Eggert, Wayne Davison, David MacKenzie, Andreas Grünbacher |
Initial release | May 24, 1985 |
Repository | |
Operating system | Unix and Unix-like, Plan 9, MSX-DOS, Microsoft Windows |
Platform | Cross-platform |
Type | Command |
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.
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] [ dubious – discuss ] The Open Software Foundation, which merged into The Open Group, is said to have maintained a derived version.[ dubious – discuss ]
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]
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 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.
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.
To create a patch, one could run the following command in a shell:
$ diff-uoldFilenewFile>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 -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:
$ 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.
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. [7]
grep
is a command-line utility for searching plaintext datasets for lines that match a regular expression. Its name comes from the ed command g/re/p
, which has the same effect. grep
was originally developed for the Unix operating system, but later became available for all Unix-like systems and some others such as OS-9.
Revision Control System (RCS) is an early implementation of a version control system (VCS). It is a set of UNIX commands that allow multiple users to develop and maintain program code or documents. With RCS, users can make their own revisions of a document, commit changes, and merge them. RCS was originally developed for programs but is also useful for text documents or configuration files that are frequently revised.
uniq
is a utility command on Unix, Plan 9, Inferno, and Unix-like operating systems which, when fed a text file or standard input, outputs the text with adjacent identical lines collapsed to one, unique line of text.
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 is like Levenshtein distance in that it tries to determine the smallest set of deletions and insertions to create one file from the other. The utility displays the changes in one of several standard formats, such that both humans or computers can parse the changes, and use them for patching.
In computing, ls
is a command to list computer files and directories in Unix and Unix-like operating systems. It is specified by POSIX and the Single UNIX Specification.
In software development, Make is a command-line interface software tool that performs actions ordered by configured dependencies as defined in a configuration file called a makefile. It is commonly used for build automation to build executable code from source code. But, not limited to building, Make can perform any operation available via the operating system shell.
In the Unix operating system, shar is an archive format created with the Unix shar
utility. A shar file is a type of self-extracting archive, because it is a valid shell script, and executing it will recreate the files. To extract the files, only the standard Unix Bourne shell sh is usually required.
uname is a computer program in Unix and Unix-like computer operating systems that prints the name, version and other details about the current machine and the operating system running on it.
xargs is a command on Unix and most Unix-like operating systems used to build and execute commands from standard input. It converts input from standard input into arguments to a command.
dd is a command-line utility for Unix, Plan 9, Inferno, and Unix-like operating systems and beyond, the primary purpose of which is to convert and copy files. On Unix, device drivers for hardware and special device files appear in the file system just like normal files; dd can also read and/or write from/to these files, provided that function is implemented in their respective driver. As a result, dd can be used for tasks such as backing up the boot sector of a hard drive, and obtaining a fixed amount of random data. The dd program can also perform conversions on the data as it is copied, including byte order swapping and conversion to and from the ASCII and EBCDIC text encodings.
tr is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. It is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set.
wc
is a command in Unix, Plan 9, Inferno, and Unix-like operating systems. The program reads either standard input or a list of computer files and generates one or more of the following statistics: newline count, word count, and byte count. If a list of files is provided, both individual file and total statistics follow.
Source Code Control System (SCCS) is a version control system designed to track changes in source code and other text files during the development of a piece of software. This allows the user to retrieve any of the previous versions of the original source code and the changes which are stored. It was originally developed at Bell Labs beginning in late 1972 by Marc Rochkind for an IBM System/370 computer running OS/360.
diff3 is a Unix utility to compare three files and show any differences among them. diff3 can also merge files, implementing a three-way merge.
In version control, merging is a fundamental operation that reconciles multiple changes made to a version-controlled collection of files. Most often, it is necessary when a file is modified on two independent branches and subsequently merged. The result is a single collection of files that contains both sets of changes.
In computing, more
is a command to view the contents of a text file one screen at a time. It is available on Unix and Unix-like systems, DOS, Digital Research FlexOS, IBM/Toshiba 4690 OS, IBM OS/2, Microsoft Windows and ReactOS. Programs of this sort are called pagers. more
is a very basic pager, originally allowing only forward navigation through a file, though newer implementations do allow for limited backward movement.
tail is a program available on Unix, Unix-like systems, FreeDOS and MSX-DOS used to display the tail end of a text file or piped data.
In computing, sleep is a command in Unix, Unix-like and other operating systems that suspends program execution for a specified time.
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, and others.
The csplit
command in Unix and Unix-like operating systems is a utility that is used to split a file into two or more smaller files determined by context lines.