patch file format

linux patch file format

"Patch" refers to a series of differences between files.

After making modifications on the basis of the original file, a patch file is generated (using the diff command) based on the modifications made.

A person who holds the original file applies the patch to the original file (using the patch command), and it becomes a modified file.

1. Create two experiment files

  • Original file: 1.txt
this is a test
for patch file format
so let us go!
  • Modified file: 2.txt
this is a test
for linux patch file format
so let us go!
learn it

2. Generate patch files

diff -u 1.txt 2.txt > diff.patch

vim diff.patch

The content of the generated patch file is as follows:

1 --- 1.txt       2020-07-20 16:45:27.131654289 +0800
2 +++ 2.txt       2020-07-20 16:46:26.348728016 +0800
3 @@ -1,3 +1,4 @@
4  this is a test
5 -for patch file format
6 +for linux patch file format
7  so let us go!
8 +learn it

3. Patch header

1 --- 1.txt       2020-07-20 16:45:27.131654289 +0800
2 +++ 2.txt       2020-07-20 16:46:26.348728016 +0800

"—" means old file (original file)

"+++" means new file (modified file)

The patch header records the file name and creation time of the original file and the modified file.

4. Block

The blocks in the patch are used to illustrate the changes in the file. They usually start with @@开始,结束于另一个块的开始或者一个新的补丁头.

3 @@ -1,3 +1,4 @@
4  this is a test
5 -for patch file format
6 +for linux patch file format
7  so let us go!
8 +learn it

5. Block indentation

3 @@ -1,3 +1,4 @@
4  this is a test
5 -for patch file format
6 +for linux patch file format
7  so let us go!
8 +learn it

The block will be indented by one column. There are three cases for this column:

  • The line starting with "-" means that the line change only exists in the original file, such as line 5

  • The line starting with "+" means that the line change only exists in the modified file, such as line 6 and line 8.

  • A line starting with a space indicates that the changed line exists in both the original file and the modified file, such as lines 4 and 7

From the perspective of patching, this column is used to indicate whether this row is to be added or deleted;

  • The line starting with "-" is to be deleted

  • Lines beginning with "+" are to be added

  • Lines beginning with spaces remain unchanged

Guess you like

Origin blog.csdn.net/sunxiaopengsun/article/details/114031738