How patches are created and used in Linux

Source: Internet
Author: User
Tags diff patch

First, describe diff and patch. All options in the man online document will not be described here, which is unnecessary. In the 99% period, we only use a few options. Therefore, you must learn these options.

1. Diff

--------------------

Name

Diff-find differences between two files

Synopsis

Diff [Options] From-file to-file

--------------------

Simply put, the diff function is used to compare the differences between the two files, and then record them, that is, the so-called diff patch.
Syntax format: Diff [Option] source file (folder) Destination File (folder), that is, to patch the source file (folder) to make it into the destination file (folder ), the term is "Upgrade ".
The following describes the three most common options:
-R is a recursive option. With this option set, diff compares all the corresponding files in the source code directories of two different versions, including subdirectory files.
-N option ensures that the patch file correctly processes the files that have been created or deleted.
-U option creates patch files in a uniform format, which is more compact than the default format.

2. Patch

------------------

Name

Patch-apply a diff file to an original

Synopsis

Patch [Options] [originalfile [patchfile]

But usually just

Patch-pnum <patchfile>

------------------

In short, patch refers to the use of patches made by diff to realize conversion between source files (CLIPS) and target files (CLIPS. This means that you can have the source file (folder)> target file (folder ),
You can also select the target file (folder)> source file (folder ).
The following describes the most common options:
-P0 option: Find the target file (folder) from the current directory)
-The P1 option should ignore the first-level directory and start searching from the current directory.

**************************************** ********************

Here is an example:

--- Old/modules/pcitable mon Sep 27 11:03:56 1999

++ New/modules/pcitable Tue Dec 19 20:05:41 2000

If you use the-P0 parameter, it means to find a folder named Old from the current directory, and find the pcitable file under modules under it to execute the patch operation.

If the-P1 parameter is used, ignore the first-level directory (regardless of the old directory), find the modules folder from the current directory, and find pcitable under it.
The premise is that the current directory must be the directory where modules is located. The diff patch file can be located anywhere, as long as the path of the diff patch file is specified.
Of course, you can use relative paths or absolute paths. However, I usually use relative paths.
**************************************** ********************

-E indicates that if an empty file is found, delete it.

-The "r" option indicates that the "new" and "old" files in the patch file need to be replaced now (in fact, the new version is patched to make it an old version)

The following examples are used to analyze and solve the problem. There are two types: patching a single file and patching multiple files in the folder.

Environment: log on with an armlinux user under RedHat 9.0.

The directory tree is as follows:

| -- Bootloader

| -- Debug

| -- Images

| -- Kernel

| -- Program

| -- Rootfiles

| -- Software

| -- Source

| -- Sysapps

| -- TMP

'-- Tools

Next, create a patch folder under the Program folder as an experiment, and then enter the patch folder.

1. patch a single file

1. Create test files test0 and test1

[Armlinux @ lqm Patch] $ cat> test0 <EOF

> 111111

> 111111

> 111111

> EOF

[Armlinux @ lqm Patch] $ more test0

111111

111111

111111

[Armlinux @ lqm Patch] $ cat> test1 <EOF

> 222222

> 111111

> 222222

> 111111

> EOF

[Armlinux @ lqm Patch] $ more test1

222222

111111

222222

111111

2. Use diff to create a patch test1.patch

[Armlinux @ lqm Patch] $ diff-UN test0 test1> test1.patch

[Note: The-r option is not required because of a single file. There is no relation between the option sequence, that is, it can be-UN or-nu .]

[Armlinux @ lqm Patch] $ ls

Test0 test1 test1.patch

[Armlinux @ lqm Patch] $ more test1.patch

**************************************** ********************

Patch file structure

Patch Header

The patch header contains two lines starting with ---/++ to indicate the files to be patched. --- The beginning indicates the old file, and the beginning of ++ indicates the new file.

Multiple patches in a patch file

A patch file may contain many sections starting with ---/++, each of which is used for a patch. Therefore, a patch file can contain many patches.

Block

The block is the part to be modified in the patch. It usually starts and ends with something that does not need to be modified. They are only used to indicate the location to be modified.
They usually start with @ and end with another block or a new patch header.

Block indent

Blocks will be reduced into one column, and this column is used to indicate whether to add or delete this row.

First column of the block

+ Indicates that this line is to be added.

-Indicates that this row is to be deleted.

There is no plus sign or minus sign, indicating that this is just referenced and does not need to be modified.

**************************************** ********************

* ** The diff Command records the first creation time of these two files in the patch file, as shown below ***

--- Test0 09:12:01. 000000000 + 0800

++ Test1 09:13:09. 000000000 + 0800

@-1, 3 + 1, 4 @@

+ 222222

111111

-111111

+ 222222

111111

[Armlinux @ lqm Patch] $ patch-P0 <test1.patch

Patching file test0

[Armlinux @ lqm Patch] $ ls

Test0 test1 test1.patch

[Armlinux @ lqm Patch] $ cat test0

222222

111111

222222

111111

3. patches can be removed to restore the old version.

[Armlinux @ lqm Patch] $ patch-re-P0 <test1.patch

Patching file test0

[Armlinux @ lqm Patch] $ ls

Test0 test1 test1.patch

[Armlinux @ lqm Patch] $ cat test0

111111

111111

111111

2. Patch multiple files

1. Create a test folder

[Armlinux @ lqm Patch] $ mkdir prj0

[Armlinux @ lqm Patch] $ CP test0 prj0

[Armlinux @ lqm Patch] $ ls

Prj0 test0 test1 test1.patch

[Armlinux @ lqm Patch] $ CD prj0/

[Armlinux @ lqm prj0] $ ls

Test0

[Armlinux @ lqm prj0] $ cat> prj0name <EOF

> --------

> Prj0/prj0name

> --------

> EOF

[Armlinux @ lqm prj0] $ ls

Prj0name test0

[Armlinux @ lqm prj0] $ cat prj0name

--------

Prj0/prj0name

--------

[Armlinux @ lqm prj0] $ CD ..

[Armlinux @ lqm Patch] $ mkdir prj1

[Armlinux @ lqm Patch] $ CP test1 prj1

[Armlinux @ lqm Patch] $ CD prj1

[Armlinux @ lqm prj1] $ cat> prj1name <EOF

> ---------

> Prj1/prj1name

> ---------

> EOF

[Armlinux @ lqm prj1] $ cat prj1name

---------

Prj1/prj1name

---------

[Armlinux @ lqm prj1] $ CD ..

2. Create a patch

[Armlinux @ lqm Patch] $ diff-unr prj0 prj1> prj1.patch

[Armlinux @ lqm Patch] $ more prj1.patch

Diff-unr prj0/prj0name prj1/prj0name

--- Prj0/prj0name 09:25:11. 000000000 + 0800

++ Prj1/prj0name 08:00:00. 000000000 + 0800

@-1, 3 +, 0 @@

---------

-Prj0/prj0name

---------

Diff-unr prj0/prj1name prj1/prj1name

--- Prj0/prj1name 08:00:00. 000000000 + 0800

++ Prj1/prj1name 09:26:36. 000000000 + 0800

@-+ @@

+ ---------

+ Prj1/prj1name

+ ---------

Diff-unr prj0/test0 prj1/test0

--- Prj0/test0 09:23:53. 000000000 + 0800

++ Prj1/test0 08:00:00. 000000000 + 0800

@-1, 3 +, 0 @@

-111111

-111111

-111111

Diff-unr prj0/test1 prj1/test1

--- Prj0/test1 1970-01-01 08:00:00. 000000000 + 0800

++ Prj1/test1 09:26:00. 000000000 + 0800

@-+ @@

+ 222222

+ 111111

+ 222222

+ 111111

[Armlinux @ lqm Patch] $ ls

Prj0 prj1 prj1.patch test0 test1 test1.patch

[Armlinux @ lqm Patch] $ CP prj1.patch./prj0

[Armlinux @ lqm Patch] $ CD prj0

[Armlinux @ lqm prj0] $ patch-P1 <prj1.patch

Patching file prj0name

Patching file prj1name

Patching file test0

Patching file test1

[Armlinux @ lqm prj0] $ ls

Prj1name prj1.patch test1

[Armlinux @ lqm prj0] $ patch-r-P1 <prj1.patch

Patching file prj0name

Patching file prj1name

Patching file test0

Patching file test1

[Armlinux @ lqm prj0] $ ls

Prj0name prj1.patch test0

-------------------

Summary:

Single file

Diff-UN from-file to-File> to-file.patch

Patch-P0 <to-file.patch

Patch-re-P0 <to-file.patch

Multiple files

Diff-unr from-docu to-docu> to-docu.patch

Patch-P1 <to-docu.patch

Patch-r-P1 <to-docu.patch

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.