-
Notifications
You must be signed in to change notification settings - Fork 4
/
whatisapatch.html
96 lines (95 loc) · 2.91 KB
/
whatisapatch.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<html>
<font face="helvetica">
<title>What is a patch?</title>
<body>
<p align=center><font size=+5>What is a patch?</font>
<hr>
<a href="contents.html">Contents</a>
<font size=+3>
<ul>
<li>There are two programs, "diff" and "patch".
<li>diff compares the lines of two text files, and computes
the difference between these files. That is, if you ask diff
to compute the difference between file A and B, it will output
a set of instructions, P, which will transform A into B.
<li>As a command, it would be "diff -u A B > P"
which you can think of algebraically as: "P = B - A"
or as "compute the editing instructions to transform A into B".
<p>For example, consider two files, A and B:
<li>
<pre>
[scameron@localhost ~]$ cat A
this is line 1
this is line 2
this is line three
[scameron@localhost ~]$ cat B
this is line 0
this is line 1
this is line 2
this is line 3
this is line 4
[scameron@localhost ~]$ diff -u A B
--- A 2011-06-13 07:31:22.799510717 -0500
+++ B 2011-06-13 07:31:46.180511362 -0500
@@ -1,3 +1,5 @@
+this is line 0
this is line 1
this is line 2
-this is line three
+this is line 3
+this is line 4
</pre>
<p><li>The "patch" program is used to apply the instructions
computed by "diff" to a text file.
As a command: "patch A < P" will transform A into B.
<pre>
[scameron@localhost ~]$ patch A < P
patching file A
[scameron@localhost ~]$ diff -u A B
[scameron@localhost ~]$
</pre>
<li>Algebraically, you can think of the above command as "A + P = B"
<li><b>Important Point: The text file which to you apply the patch need not
be identical to the file which was originally used to compute the difference.</b>
<li>Suppose you have two sets of code which are nearly the same but
slightly differenct, A1 and A2. Suppose You make a modification
to A1 to produce B1. Suppose you want to make the same modifcation
to A2 to produce B2. Instead of doing it manually, you can do:
<pre>
diff -u A1 B1 > P
</pre>
At this point, P will contain instructions to transform A1 into B1.
Now we can apply instructions P to file A2 even though A2 is not
identical to A1, and it will mostly work so long as A2 is "close enough" to A1.
<pre>
patch A2 < P > B2
</pre>
<li>Example:
<pre>
[scameron@localhost ~]$ cat C
this is line 0
this is line 1
this is line 2
this is line three
[scameron@localhost ~]$ diff -u A B | patch C
patching file C
Hunk #1 succeeded at 2 (offset 1 line).
[scameron@localhost ~]$ cat C
this is line 0
this is line 0
this is line 1
this is line 2
this is line 3
this is line 4
[scameron@localhost ~]$
</pre>
<li>This is all well and good, but if you have a whole pile of
"diffs" or "patches" and they need to be applied in a particular
order, and maybe you got them from somebody else and they do not
all apply to your code base cleanly, you will find that managing
patches with bare bones "diff" and "patch" will be far too cumbersome
and far too much work. Fortunately there are some better tools.
</ul>
</font>
</body>
</html>