Use diff and patch tools to maintain source code

Source: Internet
Author: User

In Unix systems, you can use many methods to maintain the source code version. Of course, the most common method is the well-known CVs, but in fact, simple version maintenance does not need to use complex CVs and other specialized version maintenance tools. The diff and patch tools in the UNIX standard can completely back up and upgrade the code.

Diff compares two text files (or directory comparison) in the unit of "line" and outputs the differences in a certain format to the standard output. Patch can read this output, update the source file (directory) according to the target file (directory) according to certain commands. The Linux kernel source code is updated in this way. We can download the Bzip2 package of the latest kernel patch file on This article uses gnudiffutils 2.7 and patch 2.5 as examples to introduce the use of diff and patch tools.

1. Diff

Diff can be used to compare two files or compare each file in two directories. You can also use the-R (-- Recursive) parameter to nest the comparison in the directory. When comparing directories, except for comparing files with the same name, the files with different names are treated as new files. For comparison of C program files, diff also provides special parameters (-P, -- show-C-function) to identify the function names of the differences.

There are three types of diff output formats: list mode, command mode, and context mode. Command mode can be divided into two types: Ed Command Format and RVS (Revision Control System, Version Control System) command format. The context mode is also divided into the old version and the new version. The following example shows the differences between different formats:

The Command Format records the commands that need to be executed from test1 to Test2, while the context mode is generally more readable. The main difference recorded by the command format is the two, the upper and lower lines (configurable) of the modified part are usually recorded for comparison. See the following example:

The new version is more compact than the old version. The upgrade of the Linux kernel source code is organized by diff according to the new version's context format. For example, the specific commands used in patch-2.4.16 are:

diff -Nur linux-2.4.15 linux

Parameter n indicates that if a file only appears in one directory, it is assumed that it is an empty file in another directory. utable shows the unified format, and r indicates nested use in the directory, the linux-2.4.15 is obviously the Directory Name of the old core, while Linux is the Directory Name of the new core.

Back to Top

2. Patch

Although the relationship between patch and diff is not specified, patch usually uses the diff result to complete patching, which is closely related to the patch itself supporting multiple diff output file formats. Patch reads the patch command file (standard input is supported) to modify the target file. Generally, the diff command is used to compare the old and new versions. The patch command file uses the output file of diff to ensure that the original version is consistent with the new version.

The standard patch format is

patch [options] [originalfile] [patchfile]

If the patchfile is empty, the content of the patchfile will be read from the standard input; if the originalfile is empty, the file name to be patched will be read from the patchfile (certainly from the standard input. Therefore, if you want to modify a directory, you must record each file name under the directory in the patchfile. In most cases, patches are used in the following simple method:

patch -p[num] <patchfile

The patch command can ignore the redundant information in the file and retrieve the diff format and the file name of the required patch, the file name is determined by the order of the files specified in "source file", "target file", and "index:" In the redundant information in the diff parameter. That is to say, for the following diff result file (Linux kernel source code 2.4.16 upgrade package, part ):

diff -Nur linux-2.4.15/Makefile linux/Makefile--- linux-2.4.15/Makefile       Thu Nov 22 17:22:58 2001+++ linux/Makefile      Sat Nov 24 16:21:53 2001@@ -1,7 +1,7 @@ VERSION = 2 PATCHLEVEL = 4-SUBLEVEL = 15-EXTRAVERSION =-greased-turkey+SUBLEVEL = 16+EXTRAVERSION = KERNELRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)……

Patch first tries to check whether the linux-2.4.15/MAKEFILE file under the current directory (or the directory specified by the-D parameter) exists, if not, tries to operate on the Linux/MAKEFILE file, the content of index: Is read only when neither of the two does not exist (or posixly_correct environment variables are set) (this file is not identified ).

The-p Parameter mentioned above determines whether to use the prefix directory information of the read source file name. If the-p parameter is not provided, all directory information is ignored.-P0 (or-P 0) indicates that all the path information is used.-P1 ignores the previous directory "/" and so on. A file name such as/usr/src/linux-2.4.15/makefile uses the linux-2.4.15/makefile as the file to be patched when the-P3 parameter is provided.

For the Linux kernel source code 2.4.16, assume that the source code directory is in/usr/src/Linux, you can use "patch-P0 <patch-2.4.16" when the current directory is/usr/src. When the current directory is/usr/src/Linux, "patch-P1 <patch-2.4.16" can also work properly.

Patch can directly operate the diff output files in the context format and mixed ed format, and submit the files in the Ed format to the ed program through pipelines for operations (I do not know how to handle the files in the RC format at the moment ).

Back to Top

3. Use diff and patch to upgrade source code.

Here is only a simple example to illustrate how to use the diff/patch tool to maintain the source code upgrade.

Assume that the program-1.0 directory is an old version. The new version is located in the program-2.0 directory. Place the two directories in the same parent directory and execute the following command in the parent directory:

diff -Nur program-1.0 program-2.0 >program-2.0.patch

Will generate a patch file for the program-2.0.patch, publish the patch file (of course you can first compress it into Bzip2 format ).

If the program-2.0.patch.bz2 file is obtained, it is executed at the same level in the program-1.0 directory:

bzcat program-2.0.patch.bz2 | patch -p0

This completes the upgrade from 1.0 to 2.0.

If you want to restore to the original version, you can use the-R (-- reverse) parameter, but it is only valid for diff files in the context format. Another backup parameter can also be used, but in simple applications, the entire directory backup may be more convenient.

    • Patch manual page
    • Diff manual page

Use diff and patch tools to maintain source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.