Patch (Unix)

Last updated
patch
Original author(s) Larry Wall
Developer(s) Paul Eggert, Wayne Davison, David MacKenzie, Andreas Grünbacher
Initial releaseMay 24, 1985;38 years ago (1985-05-24)
Repository
Operating system Unix and Unix-like, Plan 9, MSX-DOS, Microsoft Windows
Platform Cross-platform
Type Command
Website savannah.gnu.org/projects/patch/   OOjs UI icon edit-ltr-progressive.svg

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.

Contents

History

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] The Open Software Foundation, which merged into The Open Group, is said to have maintained a derived version.[ 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]

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" 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.

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

Ports of patch

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]

See also

Related Research Articles

<span class="mw-page-title-main">Cygwin</span> Unix subsystem for Windows machines

Cygwin is a Unix-like environment and command-line interface for Microsoft Windows. Cygwin's purpose is expressed in its motto: "Get that Linux feeling – on Windows".

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.

ls Command to list files and directories in Unix and Unix-like operating systems

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 build automation tool that builds executable programs and libraries from source code by reading files called makefiles which specify how to derive the target program. Though integrated development environments and language-specific compiler features can also be used to manage a build process, Make remains widely used, especially in Unix and Unix-like operating systems.

<span class="mw-page-title-main">Wget</span> Computer command line program for downloading

GNU Wget is a computer program that retrieves content from web servers. It is part of the GNU Project. Its name derives from "World Wide Web" and "get". It supports downloading via HTTP, HTTPS, and FTP.

<span class="mw-page-title-main">History of software configuration management</span>

The history of software configuration management (SCM) in computing can be traced back as early as the 1950s, when CM, originally for hardware development and production control, was being applied to software development. The first software configuration management was most likely done manually. Eventually, software tools were written to manage software changes. History records tend to be based on tools and companies, and lend concepts to a secondary plane.

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.

wc (Unix) Unix command utility

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.

The GNU Core Utilities or coreutils is a package of GNU software containing implementations for many of the basic tools, such as cat, ls, and rm, which are used on Unix-like operating systems.

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.

<span class="mw-page-title-main">Merge (version control)</span>

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.

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.

<span class="mw-page-title-main">Unix</span> Family of computer operating systems

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.

References

  1. Larry Wall (May 24, 1985). "patch version 1.3". Google.com at mod.sources.
  2. patch   Shell and Utilities Reference, The Single UNIX Specification , Version 4 from The Open Group
  3. http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/usr.bin/patch/ Archived 2017-06-07 at the Wayback Machine OpenBSD patch source
  4. FreeBSD   Shell and Utilities Reference, The Single UNIX Specification , Version 4 from The Open Group
  5. "Schilytools".
  6. "Patch.c\SRC - patch.git - GNU patch".
  7. MSX-DOS2 Tools User's Manual by ASCII Corporation