"Go" patch Command

Source: Internet
Author: User

Original URL: http://bbs.chinaunix.net/thread-1945698-1-1.html

Patch
Apply a patch file to file 1 into another file 2 (you need to first generate a patch file with "diff file 1 File 2").
Grammar
patch [Options] [Original file [patch file]]

Describe:
The patch command reads how to change the file's source file to indicate information, and then applies the changes. The source file contains a list of differences (or a diff list) produced by the diff command. The variance list is the result of comparing two files and building instructions about how to correct the differences. By default, the patch command uses source files that are read from standard input, but this setting can be overridden by using the-I flag and the patchfile variable.
The list of differences has three formats: normal, contextual, or Ed editor style. The patch command determines the differential list format unless deprecated by the-C,-e, OR-n flags.
By default, the patched version of the file replaces the original version. When the-B flag is specified, the original file for each patch file is saved in a file of the same name, with a suffix appended to the file name. orig. You can also specify the destination of the output using the-O flag.

Common options:
-R is a recursive option that sets this option, and diff will compare all of the corresponding files in the two different versions of the source code directory, including the subdirectory files.
The-n option ensures that the patch file will correctly handle the case where the file has been created or deleted.
The-u option creates a patch file in a uniform format that is more compact than the default format.
-P0 option to find the destination file (folder) from the current directory (directly using the path specified in the patch file)
The-P1 option ignores the first-level directory, looking from the current directory (removing the patch file to specify the leftmost 1th "/" and all previous contents of the 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 switched over (in effect, the new version is patched to make it the old version)

Example:

* * For individual files:
First, the contents of the two files are displayed as follows:
$ cat Test0
00000000
00000000
00000000
$ cat Test1
00000000
11111111
00000000

* Generate Patches:
$ diff-un test0 test1 >test1.patch
This will generate the Test1 patch file by comparison. The option U here means that the output produced by using the same format is easy to read and easily modified, and n means that the nonexistent file is empty. Even if the file test0 does not exist, patches are generated.

* Turn test0 into test1 files by patching:
$ patch-p0 <test1.patch
or $patch <test1.patch
This way, the content of TEST0 will be the same as the content of Test1, but the file name is still test0. The options for patches are described in more than one file later. The current directory can have test1. If test0 is not present at the time of comparison, then a test0 file is generated.

* Restore the patched test0:
$ patch-re-p0<test1.patch
or $patch-r <test1.patch
In this way, the contents of the test0 will revert to a state that has not been patched previously. There can be test1 in the current directory. The-e option here is to require patches to delete files when the file is empty, which is unnecessary because patches are based on timestamps to determine whether a file exists. If TEST0 is not present at the time of comparison, this will delete the Test0 file.
**

* * Multiple files:
First look at the file structure as follows:
1) Outer directory list:
$ ls-p
prj0/prj1/

2) Sub-directory Prj0 list:
$ ls-p Prj0
Prj0name test0

3) Sub-directory prj1 list:
$ ls-p prj1
Prj1name test1

4) file Prj0/prj0name:
$ cat Prj0/prj0name
--------

Prj0/prj0name

--------

5) file Prj1/prj1name:
$ cat Prj1/prj1name
---------

Prj1/prj1name

---------

6) file Prj0/test0:
$ cat Prj0/test0
0000000
0000000
0000000
0000000
0000000
0000000
0000000

7) file Prj1/test1:
$ cat Prj1/test1
1111111
1111111
1111111
1111111
1111111
1111111
1111111

Examples of Use
========
* Create patches:
$ diff-unr prj0 prj1 > Prj1.patch
The option U here means that the output produced by using the same format is easy to read and easily modified, n means that nonexistent files are treated as empty, r means recursively comparing subdirectories, and the results of comparisons are redirected to file Prj1.patch by standard.
After running, the output is a patch, describing the difference of two files, this patch is the diff parameter of the first file patch into a second file patch file.
The actual process in turn compares two directories under the same name file, if this does not add-n will indicate that Prj0name and test0 only exist in Prj0, Prj1name and test1 only exist in Prj1, which can not be compared, so here in order to be able to compare, plus the-n option.
For the sake of understanding, here are the contents of the Prj1.patch file:
$ cat Prj1.patch
Diff-unr Prj0/prj0name Prj1/prj0name
---prj0/prj0name 2009-08-24 10:44:19.000000000 +0800
+ + + prj1/prj0name 1970-01-01 08:00:00.000000000 +0800
@@ -1,5 +0,0 @@
---------
-
-prj0/prj0name
-
---------
Diff-unr Prj0/prj1name Prj1/prj1name
---prj0/prj1name 1970-01-01 08:00:00.000000000 +0800
+ + + prj1/prj1name 2009-08-24 10:45:05.000000000 +0800
@@ -0,0 +1,5 @@
+---------
+
+prj1/prj1name
+
+---------
Diff-unr prj0/test0 Prj1/test0
---prj0/test0 2009-08-24 11:21:12.000000000 +0800
+ + + prj1/test0 1970-01-01 08:00:00.000000000 +0800
@@ -1,7 +0,0 @@
-0000000
-0000000
-0000000
-0000000
-0000000
-0000000
-0000000
Diff-unr Prj0/test1 Prj1/test1
---prj0/test1 1970-01-01 08:00:00.000000000 +0800
+ + + prj1/test1 2009-08-24 11:21:33.000000000 +0800
@@ -0,0 +1,7 @@
+1111111
+1111111
+1111111
+1111111
+1111111
+1111111
+1111111

* Patch all files in Prj0 into all files in prj1:
The steps are as follows:
1) $ CP prj1.patch./prj0
2) $ cd prj0
3) $ PATCH-P1 < Prj1.patch
Here, the patch file is copied to Prj0, and the file below the folder "becomes" the file under Prj1.
$ ls-p
Prj1name Prj1.patch Test1
The-p option for the patch command is followed by the number n, which means that the first n '/' prefixes of the specified path in the patch file are removed.
For example, the path specified in the patch file is/U/HOWARD/SRC/BLURFL/BLURFL.C, then the path after the P0 option has been processed is the same as the original path, and the path after the P1 option is U/HOWARD/SRC/BLURFL/BLURFL.C, Similarly, the path after P4 processing is: BLURFL/BLURFL.C.
Note: If you run this command in the outer directory, you will create two prj1name and test1 files in the outer directory.

* Restore all the files in the patched prj0 to the file before the original patch:
$ patch-r-p1 < Prj1.patch
After running the file becomes the original file, as follows:
$ ls-p
Prj0name Prj1.patch test0

* Reverse-patch all files in prj1 into all files in prj0:
$ patch-r-p1 < Prj1.patch
After running, the files in prj1 become prj0 files, as follows:
$ ls-p
Prj0name Prj1.patch test0

* Restore the Prj1 files to the original prj1 files:
$ PATCH-P1 < Prj1.patch
After running, the files in the prj1 are restored, as follows:
$ ls-p
Prj1name Prj1.patch Test1

* The content of prj0 is patched into prj1 content in the external directory:
$ls-P
Prj0/prj1.patch
$patch-p0 <prj1.patch
The content in this prj0 becomes the content in the prj1, but the Prj0 directory name is still prj0, as follows:
$ls-P Prj0
Prj1name test1
Note: The Prj1 directory cannot be found under the current folder, or some warning prompts will appear.

* The content of the prj0 is restored to the original prj0 content by the external directory:
$ patch-r-p0 <prj1.patch
So the original file is as follows:
$ ls-p Prj0
Prj0name test0
**

"Go" patch Command

Related Article

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.