Diff/patch
Use diff/patch to patch the code
1. Use of diff
Diff can complete the comparison function and generate patch files
Format: Diff [Option] oldfile newfile
Common options include:
-R recursively processes Directories
-U output is in a unified format. Diff has two formats: "traditional" and "unified". Currently, "Uniform" format is generally used. In comparison, the file generated in a unified format is large, but it contains more information, which is helpful for reading and positioning.
-N: The patch contains the entire new file.
-Patch A contains binary files.
Diff usually redirects to a file and uses the patch as the suffix, which is called a patch file.
Example:
/* Oldfile hello. C */
Void main ()
{
Printf ("Hello the world! ");
}
/* Newfile hello-new.c */
Void main ()
{
Printf ("Hello the world! ");
}
Use the following command to generate the patch file hello. Patch
$ Diff-u hello. c hello-new.c> hello. Patch
Diff can compare the entire directory to generate a patch file
For example, there are two directories: hello-1.0 and hello-1.1. Hello-1.1 is an update of hello-1.0.
Command:
$ Diff-runa hello-1.0 hello-1.1> hello-1.1.patch
2. Patch usage
The command to apply the patch to the original code is patch
Patch [-B] suffix
If the patch fails, the patch patches the successful rows, and the failed rows are saved. if the rej is a suffix, the file is folded and the backup of the original file is generated. If the rej is successful, no backup is generated. -Option B can specify the suffix.
Note: The directory where the patch is run should be consistent with that when the patch is generated using diff. For example, if the patch file is generated in the upper directory of the hello-1.0 directory, the patch file should also be created in this directory.