Monday, April 26, 2010

patch (Unix)

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.

Usage context
Developed by a programmer for other programmers, patch was frequently used for updating of 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. It should be noted that 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.

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

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

Thanks to :
http://en.wikipedia.org/wiki/Patch_(Unix)

No comments:

Post a Comment