Diff and patch User Guide t O ^ MS2
Oldlinux forum-a Forum on Early Linux kernel code development. VJ
Diff and Patch are a pair of tools. in mathematics, diff is a difference operation for two sets, and patch is a sum operation for two sets. % I0f @/
Diff compares the differences between two files or file sets and records them to generate a diff file, which is also a patch file we often call patch files. 'U
Patch can apply the diff file to one of the original two sets to obtain another set. For example, after diff, file a and file B generate patch file C, which is equivalent to a-B = C, then the patch process is B + C = A or A-C = B. 1d8
Therefore, as long as we can get any two of the three files A, B, and C, we can use the diff and patch tools to generate another file .; U
Oldlinux forum-a Forum on Early Linux kernel code development. '*] L
This is the beauty of diff and patch. The following describes the usage of the two tools: wl46b
Oldlinux forum-a Forum on Early Linux kernel code development. Htex
1. Diff usage6g [E9
Oldlinux forum-a Forum on Early Linux kernel code development. > Ulg
Diff can be followed by two file names or two directory names. If it is a directory name and a file name, it only applies to files with the same name under that directory. Dr _
Oldlinux forum-a Forum on Early Linux kernel code development. SQ ~ +
If there are two directories, they act on all the files under the directory without recursion. If we want Recursive Execution, we need to use the-R parameter. "
Oldlinux forum-a Forum on Early Linux kernel code development. RQ> V (H
Command diff a B> C. Generally, A is the original file, B is the modified file, and C is called the patch file of. Z
The diff file format generated without adding any parameters is a simple format, which only marks different lines and content. We need a more detailed format to identify different context environments, which is more conducive to improving the recognition capability of patch commands. In this case, you can use the-C switch.> PSU
Oldlinux forum-a Forum on Early Linux kernel code development. ^ 5g % v
2. Patch usage2 V
Oldlinux forum-a Forum on Early Linux kernel code development. F <&
Patch is used to generate the target file based on the original file and patch file. Or take the previous example as an example>? O =
Oldlinux forum-a Forum on Early Linux kernel code development. &
Patch a C can get B. This step is called to patch a with the name of B as C. O
Oldlinux forum-a Forum on Early Linux kernel code development. S
After step 1, file a becomes file B. What if you want to recover to a after completing the patch? IP
Oldlinux forum-a Forum on Early Linux kernel code development. R! Awde
Patch-r B C can be restored to. W $4
Oldlinux forum-a Forum on Early Linux kernel code development. 6x
So don't worry about losing. [U & KMC
Oldlinux forum-a Forum on Early Linux kernel code development. F
In fact, you do not need to specify the original file when using the patch, because the path and name of the original file have been recorded in the patch file. Patch is smart enough to recognize it. But sometimes it may be a small problem. For example, the diff directory may already contain the name of the original directory, but we will enter the directory when patching and then use the patch, you need to tell the patch command how to process the path in the patch file. You can use the-PN switch to tell the patch command to ignore the number of path separators. Example:. *; o!
Oldlinux forum-a Forum on Early Linux kernel code development. "AE/FL
The a file is under dir_a, and the modified B file is under dir_ B. Generally, dir_a and dir_ B are in the same level directory. To diff all files in the directory at a time, run the following commands in the parent directory of dir_a and dir_ B: Ra> J
Oldlinux forum-a Forum on Early Linux kernel code development. Oabx &
Diff-RC dir_a dir_ B> C} $ W
Oldlinux forum-a Forum on Early Linux kernel code development. Xdu
At this time, patch file C records the path of the original file as dir_a/a m. B.
Oldlinux forum-a Forum on Early Linux kernel code development. U9
Now another user obtains file a and file C. The directory where File A is located is dir_a. Generally, he prefers to perform the patch operation under the dir_a Directory, which will execute the H/h
Oldlinux forum-a Forum on Early Linux kernel code development. I
Patch <Co % lsso
Oldlinux forum-a Forum on Early Linux kernel code development. Z % L
However, at this time, the patch analyzes the records in the C file and considers the original file as./dir_a/a, but it is actually./A. At this time, the patch will not find the original file. To avoid this problem, we can use the-P1 parameter as follows:
Oldlinux forum-a Forum on Early Linux kernel code development. B8}
Patch-P1 <CT ~ S {Z'
Oldlinux forum-a Forum on Early Linux kernel code development. '| Y
In this case, the patch ignores the 1st "/" Contents and considers the original file as./A. This is correct. Pn
Oldlinux forum-a Forum on Early Linux kernel code development. Hnv <
Note the following points:8 P <?
Oldlinux forum-a Forum on Early Linux kernel code development. N % YG % B
1. if multiple patches are created at a time, these patches are ordered in order .? FL
2. Do not modify the original file before the patch %
3. If the original file recorded in the patch does not match the version of the original file you obtained (it is easy to appear), you can try to use the patch. If you are lucky, you can succeed. In most cases, the patch will generate a rej file, where the record fails, you can manually modify it. LW] Cf:
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.