Diff, Patch, and Friends(diff, patch和他们的相关工具)(第二篇)

原文 http://www.linuxjournal.com/article/1237
翻译: www.ykyi.net zausiu
接上篇

Use the diff program to avoid eyestrain and insanity:
使用diff程序来避免会令人发疯的肉眼查找。

diff -u 1 2
— 1 Sat Apr 20 22:11:53 1996
+++ 2 Sat Apr 20 22:12:01 1996
-1,9 +1,9
Ecce Eduardus Ursus scalis nunc tump-tump-tump
occipite gradus pulsante post Christophorum
Robinum descendens. Est quod sciat unus et solus
-modus gradibus desendendi, non nunquam autem
+modus gradibus descendendi, nonnunquam autem
sentit, etiam alterum modum exstare, dummodo
-pulsationibus desinere et de no modo meditari
+pulsationibus desinere et de eo modo meditari
possit. Deinde censet alios modos non esse. En,
nunc ipse in imo est, vobis ostentari paratus.
Winnie ille Pu.

There are several things to notice here:
这里有几个东西要提一下:

*

The file names and last dates of modification are shown in a “header” at the top. The dates may not mean anything if you are comparing files that have been passed back and forth by e-mail, but they become very useful in other circumstances.
文件名和上交更新时间在输出中前面部分显示。如果你只是比较由电子邮件发来发去的文件的话,更新时间可能并不重要。但是在某些情况下,确实还是很有用的。
*

The file names (in this case, 1 and 2—are preceded by — and +++.
文件名(在这个例子中,有两个文件,一个名为1,另一个名为2)加上了 — 和 +++ 的前缀。
*

After the header comes a line that includes numbers. We will discuss that line later.
在头部之后有一行包括了一些数字。我们将在之后讨论它。
*

The lines that did not change between files are shown preceded by spaces; those that are different in the different files are shown preceded by a character which shows which file they came from. Lines which exist only in a file whose name is preceded by — in the header are preceded by a – character, and vice-versa for lines preceded by a + character. Another way to remember this is to see that the lines preceded by a – character were removed from the first (—) file, and those preceded by a + character were added to the second (+++) file.
所有没有更改的行显示出来时仅仅加上空格作前缀。如果是不同的行呢,则在每一行前加上指示它们出处的前缀。加上-前缀表示来自头部标志的 — 文件,如果加上 + 前缀则表示来自头部标志的 +++ 文件。另外你也可以理解成: 有 – 前缀的行表示从第一个文件(—)中删除了,而有 + 前缀的行表示是在(+++)文件中添加的。
*

Three spelling changes have been made: “desendendi” has been corrected to “descendendi”, “non nunquam” has been corrected to “nonnunquam”, and “no” has been corrected to “eo”.
发现三个拼写改动。

Perhaps the main thing to notice is that you didn't need this description of how to interpret diff's output in order to find the differences. It is rather easy to compare two adjacent lines and see the differences.
可能你最关心的不是怎么解析diff的输入来找到分别。比较两个相邻的行来找区别是相当容易的。

It's not always this easy
但事实上并不总是如此。

Unfortunately, if too many adjacent lines have been changed, interpretation isn't as immediately obvious; but by knowing that each marked line has been changed in some way, you can figure it out. For instance, in this comparison, where the file 3 contains the damaged contents, and file 4 (identical to file 2 in the previous example) contains the correct contents, three lines in a row are changed, and now each line with a difference is not shown directly above the corrected line:
很不幸的是。如果太多的相邻行有改动的话,就不是那么明显啦。但是如果知道每行是怎么改动的,你就被轻易找到区别。比如这个例子,文件3包含了受损的内容,而文件4(其实和上个例子中的文件2一模一样)包含了正确的内容,连续三行被改变了,现在每个有改动的行没有在正确的行上面直接显示出来。

diff -u 3 4
— 3 Sun Apr 21 18:57:08 1996
+++ 4 Sun Apr 21 18:56:45 1996
-1,9 +1,9
Ecce Eduardus Ursus scalis nunc tump-tump-tump
occipite gradus pulsante post Christophorum
Robinum descendens. Est quod sciat unus et solus
-modus gradibus desendendi, non nunquam autem
-sentit, etiam alterum nodum exitare, dummodo
-pulsationibus desinere et de no modo meditari
+modus gradibus descendendi, nonnunquam autem
+sentit, etiam alterum modum exstare, dummodo
+pulsationibus desinere et de eo modo meditari
possit. Deinde censet alios modos non esse. En,
nunc ipse in imo est, vobis ostentari paratus.
Winnie ille Pu.

It takes a little more work to find the added mistakes; “nodum” for “modum” and “exitare” for “exstare”. Imagine if 50 lines in a row had each had a one-character change, though. This begins to resemble the old job of going through the whole file, character-by-character, looking for changes. All we've done is (potentially) shrink the amount of comparison you have to do.
这就需要花费更多的工作来找到错误啦。“nodum” for “modum” and “exitare” for “exstare”。想象一下,如果连续50行,每行都有一个字母的改变,那么你要做的工作又和使用diff前一样嘞。逐字逐字的查找!diff仅仅帮你把更改范围限定了而已。

Fortunately, there are several tools for finding these kinds of differences more easily. GNU Emacs has “word diff” functionality. There is also a GNU “wdiff” program which helps you find these kinds of differences without using Emacs.
好有好些工具帮助你很容易定位到这些差异。GNU Emacs就有一个"word diff"的功能。还有一个叫wdiff的GNU工具也能帮助你查找这种类型的错误。

Let's look first at GNU Emacs. For this example, files 5 and 6 are exactly the same, respectively, as files 3 and 4 before. I bring up emacs under X (which provides me with colored text), and type:
让我们先看看GNU Emacs。

M-x ediff-files RET
5 RET
6 RET

In the new window which pops up, I press the space bar, which tells Emacs to highlight the differences. Look at Figure 1 and see how easy it is to find each changed word.

Figure 1. ediff-files 5 6

GNU wdiff is also very useful, especially if you aren't running X. A pager (such as less) is all that is required—and that is only required for large differences. The exact same set of files (5 and 6), compared with the command wdiff -t 5 6, is shown in Figure 2.
GNU wdiff也很有用,特别是你没有运行X服务的时候。一个分屏工具(比如 less)就足够用来查找大范围的差异了。

Figure 2. wdiff -t 5 6

///////
好辛苦。不想译了~~不译了。自己去看原文吧。累死哥了。

copyright ykyi.net

Leave a Reply

Your email address will not be published. Required fields are marked *