Diff and patch usage 1/2

Source: Internet
Author: User

If you don't refresh it, you forget to take this opportunity.

 

Diff

Diff is an essential tool for generating source code patches. The command format is:

Diff [command line option] new file of the original file

Common command line options are as follows:

-R recursive processing Directory-unified format of U output (uniied format)

-N patch contains new files-a patch can contain binary files

Its output is on stdout, so you may need to redirect it to a file. Diff outputs can be classified into "traditional format" and "unified format". Most of them use the unified format:

Traditional format example:

[Hahalee @ builder] $ diff a.txt B .txt

1A2

> Here we insert a new line

3d3

<Why not this third line?

Unified format example:

[Hahalee @ builder] $ diff-u a.txt B .txt

--- A.txt Thu APR 6 15:58:34 2000

++ B .txt Thu APR 6 15:57:53 2000

@-1, 3 + 1, 3 @@

This is line one

+ Here we insert a new line

And this is line two

-Why not this third line?

The comparison shows that patch files in the traditional format are relatively small, and there is no redundant information except the rows to be deleted/inserted. The unified format saves the context (the default is the upper and lower three rows, at least two rows are required). In this way, the line number mismatch can be allowed during the patch. In addition, the original file and the current file are clearly marked with --- and ++ at the beginning of the patch file, which is also easy to read. To use a uniform format, use the u Switch.

Generally, we need to modify the entire software package and generate a patch file. The following is a typical operation process. The following command line switches are used:

Tar xzvf software.tar.gz # expand the original software package. The directory is software.

CP _ A Software-orig # make a backup before modification

CD Software

[Modify, test...]

CD ..

Diff _ Runa software-orig software> software-my.patch

Now we can save the software-my.patch as the result of this modification, as for the original package, you don't have to save. When you need to modify the patch next time, you can use the patch command to pack the patch into the original package and continue to work. For example, if you are working on Linux kernel, you do not have to save the source code after dozens of megabytes of modifications each time. This is one of the advantages. The second advantage is the convenience of maintenance. Because the uniied patch format has a certain fuzzy matching capability, it can reduce the maintenance workload caused by the upgrade of the original software package (see later)

Patch

The patch command works with diff to apply the generated patch to the existingCode. Common command line options:

Patch [command line option] [file to be patched [Patch]

-PN patch level (N is a number)-B [suffix] to generate a backup. The default value is. orig.

To illustrate what a patch level is, let's look at the header mark of a patch file.

Diff-runa XC. orig/config/CF/imake. Cf XC. BSD/config/CF/imake. cf

--- XC. orig/config/CF/imake. Cf Fri Jul 30 12:45:47 1999

++ XC. New/config/CF/imake. Cf Fri Jan 21 13:48:44 2000

If this patch is applied directly, it will go to XC. imake. cf file. Assume that the root directory of your source code tree is the default XC instead of XC. orig, except for mvxc XC. in addition to orig, is there a simple way to apply this patch? Patch level is set for this purpose: patch will cut the target path name to start with Patch
Level (separated ). In this example, you can use the following command:

Cd xc; patch _ P1 </pathname/xxx. Patch
Complete the operation. Note that the patch file is not specifiedProgramInput redirection is used because it is read from stdin by default.

If the patch is successful, no backup file is created by default (Note: The patch tool in FreeBSD saves the backup by default). If you need it, add the "B" switch. In this way, the file before modification is backed up by the name of "original file name. orig. If you like other extension names, you can also use the "B suffix" to specify them.

If the patch fails, the patch will route the successful patch to the patch, and generate a backup file and A. rej file unconditionally .. The rej file contains no patch lines successfully submitted and must be manually patched. This situation may occur when the original code is upgraded.

Binary File Description: binary files can be stored in the patch file in the original way. Diff can be generated (added with the-A option), and patch can also be identified. If you think such a patch file is too ugly, one of the solutions is to use uencode to process the binary file.

Diff compares two text files (or directories) in the unit of behavior and outputs the differences to the standard output,
The patch can read these outputs and update the source file (directory) according to certain instructions.

Diff compares two text files (or directories) in the unit of behavior and outputs the differences to the standard output,
The patch can read these outputs and update the source file (directory) according to certain instructions.
Diffutils-2.7.2-2
Patches-2.5.4-10

1, diff
Options:
-R -- Recursive
-P -- show-C-function // identifies the function name of the difference (only used for C Programs)
-E -- Ed // ed Command Format: Diff-e test1 Test2
// Mixed ed Command Format (No parameter) diff test1 Test2
-F // The simple format of the RFM command diff-F test1 Test2 Revision Control
System
-C -- Context // earlier version of the context format diff-C test1 Test2
-U -- uniied // new version of context format diff-u test1 Test2
-N // if a file only appears in one directory, it is assumed that it is an empty file in another directory.

There are three output formats for DIFF: list mode, command mode, and context mode.
Command modes: Ed command mode and RVS (Revision Control System)
The context modes include the old and new versions.
The command mode records the commands that need to be executed from test1 to test2. the context mode is more readable.
Linux KernelSource codeDiff is organized according to the context mode of the new version,
Eg. Diff-Nur linux-2.4.15 Linux

2. Patch
Patch [Options] [originalfile] [patchfile]
If the patchfile is empty, it is read from the standard input.
The following format is generally used:
Patch-P [num] <patchfile
If the p parameter is not provided, all directory information is ignored,
-P0 indicates that all path information is used.
-P1 indicates ignoring the previous directories of the first "/", and so on.

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,
It is not clear how to process the RFM file.

Eg.
Bzcat linux-2.6.1.patch.bz2 | patch-P0

If you want to restore to the original version, you can use the-R (-- reverse) parameter.
However, it is only valid for diff files in the context format.
Eg. Patch-r <program-1.2.patch

3. Use diff-patch and CVS together
In this case, do not include the CVS directory.
Eg.
Patch-Nur Program-1.0 program-1.1 -- exclude = CVS

 

It should have referenced some largeArticleBut I don't know the source. Sorry

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: info-contact@alibabacloud.com 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.