Introduction to the application and fabrication method of patch under Linux

Source: Internet
Author: User
Tags diff diff patch

Introduction to the application and fabrication method of patch under Linux

Because there are several common files to be modified during the u-boot transplant, it is too cumbersome to manually modify them every time. Making patches can solve this problem.

The collection of learning materials is relatively simple, method a similar to this primary problem online information is very rich, Google or Baidu Search, and then select valuable information, method two is to read the man online documents. Complete the collection work, of course, finally to do on their own Linux experiments, compare summary, digestion and absorption for their own things. To remove such a wrong thought: must learn the whole. To know, a learning is impossible, can only learn the most commonly used, in the process of continuous practice in the future gradually rich, and finally reached a relatively high level. The principle of grasping is: to learn, to apply, to promote learning.

First introduce the diff and patch. It is not necessary to introduce all the options in the man's online documentation. In 99% of the time, we will only use a few options. So you have to learn these few 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 two files, and then record down, that is, the so-called diff patch. Syntax Format: diff "option" Source file (folder) purpose file (folder), is to give the source file (folder) to make a patch to become the destination file (folder), the term is "upgrade." Here are three of the most common options:

-R is a recursive option, set this option, diff will compare all the corresponding files in the two different versions of the source code directory, including subdirectory files.

The-n option ensures that the patch file will correctly handle situations where files have been created or deleted.

The-u option creates a patch file in a uniform format that is more compact than the default format.

2, Patch

NAME

Patch-apply a diff file to a original

Synopsis

patch [Options] [Originalfile [Patchfile]]

But usually just

Patch-pnum

Simply put, patch is the use of diff made patches to achieve the source file (folder) and the destination file (folder) conversion. This means that you can have the source file (folder) ――> destination file (folder), can also be the destination file (folder) ――> source files (folders). Here are some of the most common options:

-P0 option to find the destination file (folder) from the current directory

The-P1 option is to ignore the first level directory and start looking from the current directory.

Here is an example to illustrate:

---old/modules/pcitable Mon Sep 27 11:03:56 1999

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

If you use the parameter-p0, it means to look for a folder called old from the current directory and find the pcitable file under modules below it to perform the patch operation.

If you use parameter-p1, that means ignoring the first level directory (that is, regardless of old), looking for the Modules folder from the current directory, and looking for pcitable underneath it. This assumes that the current directory must be the directory where the modules resides. The diff patch file can be anywhere, as long as it indicates the path to the diff patch file. Of course, you can use a relative path, or you can use an absolute path. But I am generally accustomed to using a relative path.

The-e option indicates that if an empty file is found, delete it

The-r option indicates that the "new" file and the "old" file in the patch file are now being swapped (in effect, patching the new version to the old version)

The following combination of specific examples to analyze and solve, divided into two types: for a single file patch and a folder for a number of files to play patches.

Environment: Under the Redhat 9.0 to Armlinux user login.

The directory tree is as follows:

|--Bootloader

| |--DEBUG

|--Images

|--Kernel

|--Program

|--Rootfiles

|--Software

| |-source

|--Sysapps

| |-tmp

'--tools

Below the program folder to establish the patch folder as an experiment, and then enter the Patch folder.

One, patch for a single file

1, the establishment of test documents TEST0, Test1

[Armlinux@lqm patch]$ Cat >>test0<

> 111111

> 111111

> 111111

> EOF

[ARMLINUX@LQM patch]$ more Test0

111111

111111

111111

[Armlinux@lqm patch]$ Cat >>test1<

> 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. The order of options is not related, that is, it can be-un or-nu. 】

[ARMLINUX@LQM patch]$ ls

Test0 test1 Test1.patch

[ARMLINUX@LQM patch]$ more Test1.patch

Structure of the patch file

Patch Head

The patch header is two lines that begin with the---/+++ to indicate the file to be patched. The beginning of the---represents the old file, and +++ begins with the new file.

Multiple patches in a patch file

A patch file may contain many sections that begin with a---/+++, and each section is used to make a patch. So you can include multiple patches in a patch file.

Block

Block is the place to modify in the patch. It usually starts and ends with something that doesn't have to be modified. They are only used to indicate the location to be modified. They usually start with @@, ending with the start of another block or a new patch header.

Indentation of Blocks

Blocks are indented, and this column is used to indicate whether the line is to be added or deleted.

The first column of the Block

The + number indicates that the line is to be added.

-the number indicates that the line is to be deleted.

There is no plus sign and no minus sign indicates that it is only 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 follows * *

---test0 2006-08-18 09:12:01.000000000 +0800

+++ Test1 2006-08-18 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, can remove the patch, 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

Note : More wonderful tutorials Please pay attention to the triple computer tutorial section, triple Computer office group: 189034526 welcome you to join

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.