patch and diff

When programs are distributed, it’s almost inevitable that users will discover bugs or that the author
will want to issue enhancements and updates. When authors distribute programs as binaries, they often simply ship new binaries. Sometimes (all too often), vendors simply release a new version of the program, often with an obscure revision reference and little information about what has changed.
On the other hand, distributing your software as source code is an excellent idea because it allows people to see how you have implemented things and how you have used features. It also allows people to check exactly what programs are doing and to reuse parts of the source code (providing they comply with the licensing agreement).
However, with the source of the Linux kernel weighing in at tens of megabytes of compressed source code, shipping an updated set of kernel sources would involve considerable resources, when, in fact, probably only a small percentage of the source has changed between each release.
Fortunately, there is a utility program for solving this problem: patch. It was written by Larry Wall, who also wrote the Perl programming language. The patch command allows you to distribute just the differ- ences between the two versions so that anyone with version 1 of a file and a difference file for version 1 to version 2 can use the patch command to generate version 2 for themselves.

If you start with version 1 of a file,

This is file one
line 2
line 3
line 5 
line 6

and then produce version 2,

This is file two
line 2
line 3
line 4 
line 5 
line 6
line 7
       

you can create a difference listing with the diff command:

diff -u file1.txt file2.txt > diffs

The diffs file contains

--- file.txt	2020-01-01 20:00:15.000000000 +0800
+++ file2.txt	2020-01-01 20:01:27.000000000 +0800
@@ -1,5 +1,7 @@
-This is file one
+This is file two
 line 2
 line 3
+line 4 
 line 5 
 line 6
+line 7

This is actually a set of editor commands for changing one file into another. Suppose you have file1.c and the diffs file. You can update your file using patch as follows:

patch file1.txt diffs

The patch command has now changed file1.c to be the same as file2.c.
patch has an extra trick: the ability to unpatch. Suppose you decide you don’t like the changes and want
your original file1.c back. No problem; just use patch again, using the -R (reverse patch) option:

patch -R file1.txt diffs

file1.c is returned to its original state.
The patch command has several other options, but is generally very good at deciding from its input what you’re trying to do and then simply “doing the right thing.” If patch ever fails, it creates a file with the .rej extension containing the parts that couldn’t be patched.
When you’re dealing with software patches, it’s a good idea to use the diff -c option, which produces a “context diff.” This provides a number of lines before and after each change so that patch can verify that the context matches before applying the patch. The patch is also easier to read.
If you find and fix a bug in a program, it’s easier, more precise, and more polite to send the author a patch rather than just a description of the fix.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章