{"id":191,"date":"2011-09-02T13:16:30","date_gmt":"2011-09-02T05:16:30","guid":{"rendered":"http:\/\/ykyi.net\/?p=191"},"modified":"2011-09-02T13:16:30","modified_gmt":"2011-09-02T05:16:30","slug":"diff-patch-and-friendsdiff-patch%e5%92%8c%e4%bb%96%e4%bb%ac%e7%9a%84%e7%9b%b8%e5%85%b3%e5%b7%a5%e5%85%b7%e7%ac%ac%e4%ba%8c%e7%af%87","status":"publish","type":"post","link":"https:\/\/ykyi.net\/?p=191","title":{"rendered":"Diff, Patch, and Friends(diff, patch\u548c\u4ed6\u4eec\u7684\u76f8\u5173\u5de5\u5177)(\u7b2c\u4e8c\u7bc7)"},"content":{"rendered":"<p>\u539f\u6587 http:\/\/www.linuxjournal.com\/article\/1237<br \/>\n\t\u7ffb\u8bd1: www.ykyi.net zausiu<br \/>\n\t<a href=\"http:\/\/ykyi.net\/2011\/02\/diff-patch-and-friendsdiff-patch%e5%92%8c%e4%bb%96%e4%bb%ac%e7%9a%84%e7%9b%b8%e5%85%b3%e5%b7%a5%e5%85%b7%e7%ac%ac%e4%b8%80%e7%af%87\/\" title=\"\u63a5\u4e0a\u7bc7\">\u63a5\u4e0a\u7bc7<\/a><\/p>\n<p>Use the diff program to avoid eyestrain and insanity:<br \/>\n\t\u4f7f\u7528diff\u7a0b\u5e8f\u6765\u907f\u514d\u4f1a\u4ee4\u4eba\u53d1\u75af\u7684\u8089\u773c\u67e5\u627e\u3002<\/p>\n<p>diff -u 1 2<br \/>\n\t&#8212; 1 Sat Apr 20 22:11:53 1996<br \/>\n\t+++ 2 Sat Apr 20 22:12:01 1996<br \/>\n\t-1,9 +1,9<br \/>\n\tEcce Eduardus Ursus scalis nunc tump-tump-tump<br \/>\n\toccipite gradus pulsante post Christophorum<br \/>\n\tRobinum descendens. Est quod sciat unus et solus<br \/>\n\t-modus gradibus desendendi, non nunquam autem<br \/>\n\t+modus gradibus descendendi, nonnunquam autem<br \/>\n\tsentit, etiam alterum modum exstare, dummodo<br \/>\n\t-pulsationibus desinere et de no modo meditari<br \/>\n\t+pulsationibus desinere et de eo modo meditari<br \/>\n\tpossit. Deinde censet alios modos non esse. En,<br \/>\n\tnunc ipse in imo est, vobis ostentari paratus.<br \/>\n\tWinnie ille Pu.<\/p>\n<p>There are several things to notice here:<br \/>\n\t\u8fd9\u91cc\u6709\u51e0\u4e2a\u4e1c\u897f\u8981\u63d0\u4e00\u4e0b\uff1a<\/p>\n<p>*<\/p>\n<p>The file names and last dates of modification are shown in a &ldquo;header&rdquo; 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.<br \/>\n\t\u6587\u4ef6\u540d\u548c\u4e0a\u4ea4\u66f4\u65b0\u65f6\u95f4\u5728\u8f93\u51fa\u4e2d\u524d\u9762\u90e8\u5206\u663e\u793a\u3002\u5982\u679c\u4f60\u53ea\u662f\u6bd4\u8f83\u7531\u7535\u5b50\u90ae\u4ef6\u53d1\u6765\u53d1\u53bb\u7684\u6587\u4ef6\u7684\u8bdd\uff0c\u66f4\u65b0\u65f6\u95f4\u53ef\u80fd\u5e76\u4e0d\u91cd\u8981\u3002\u4f46\u662f\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u786e\u5b9e\u8fd8\u662f\u5f88\u6709\u7528\u7684\u3002<br \/>\n\t*<\/p>\n<p>The file names (in this case, 1 and 2&mdash;are preceded by &#8212; and +++.<br \/>\n\t\u6587\u4ef6\u540d\uff08\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6709\u4e24\u4e2a\u6587\u4ef6\uff0c\u4e00\u4e2a\u540d\u4e3a1\uff0c\u53e6\u4e00\u4e2a\u540d\u4e3a2\uff09\u52a0\u4e0a\u4e86 &#8212; \u548c +++ \u7684\u524d\u7f00\u3002<br \/>\n\t*<\/p>\n<p>After the header comes a line that includes numbers. We will discuss that line later.<br \/>\n\t\u5728\u5934\u90e8\u4e4b\u540e\u6709\u4e00\u884c\u5305\u62ec\u4e86\u4e00\u4e9b\u6570\u5b57\u3002\u6211\u4eec\u5c06\u5728\u4e4b\u540e\u8ba8\u8bba\u5b83\u3002<br \/>\n\t*<\/p>\n<p>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 &#8212; in the header are preceded by a &#8211; character, and vice-versa for lines preceded by a + character. Another way to remember this is to see that the lines preceded by a &#8211; character were removed from the first (&#8212;) file, and those preceded by a + character were added to the second (+++) file.<br \/>\n\t\u6240\u6709\u6ca1\u6709\u66f4\u6539\u7684\u884c\u663e\u793a\u51fa\u6765\u65f6\u4ec5\u4ec5\u52a0\u4e0a\u7a7a\u683c\u4f5c\u524d\u7f00\u3002\u5982\u679c\u662f\u4e0d\u540c\u7684\u884c\u5462\uff0c\u5219\u5728\u6bcf\u4e00\u884c\u524d\u52a0\u4e0a\u6307\u793a\u5b83\u4eec\u51fa\u5904\u7684\u524d\u7f00\u3002\u52a0\u4e0a-\u524d\u7f00\u8868\u793a\u6765\u81ea\u5934\u90e8\u6807\u5fd7\u7684 &#8212; \u6587\u4ef6\uff0c\u5982\u679c\u52a0\u4e0a + \u524d\u7f00\u5219\u8868\u793a\u6765\u81ea\u5934\u90e8\u6807\u5fd7\u7684 +++ \u6587\u4ef6\u3002\u53e6\u5916\u4f60\u4e5f\u53ef\u4ee5\u7406\u89e3\u6210: \u6709 &#8211; \u524d\u7f00\u7684\u884c\u8868\u793a\u4ece\u7b2c\u4e00\u4e2a\u6587\u4ef6(&#8212;)\u4e2d\u5220\u9664\u4e86\uff0c\u800c\u6709 + \u524d\u7f00\u7684\u884c\u8868\u793a\u662f\u5728(+++)\u6587\u4ef6\u4e2d\u6dfb\u52a0\u7684\u3002<br \/>\n\t*<\/p>\n<p>Three spelling changes have been made: &ldquo;desendendi&rdquo; has been corrected to &ldquo;descendendi&rdquo;, &ldquo;non nunquam&rdquo; has been corrected to &ldquo;nonnunquam&rdquo;, and &ldquo;no&rdquo; has been corrected to &ldquo;eo&rdquo;.<br \/>\n\t\u53d1\u73b0\u4e09\u4e2a\u62fc\u5199\u6539\u52a8\u3002<\/p>\n<p>Perhaps the main thing to notice is that you didn&#39;t need this description of how to interpret diff&#39;s output in order to find the differences. It is rather easy to compare two adjacent lines and see the differences.<br \/>\n\t\u53ef\u80fd\u4f60\u6700\u5173\u5fc3\u7684\u4e0d\u662f\u600e\u4e48\u89e3\u6790diff\u7684\u8f93\u5165\u6765\u627e\u5230\u5206\u522b\u3002\u6bd4\u8f83\u4e24\u4e2a\u76f8\u90bb\u7684\u884c\u6765\u627e\u533a\u522b\u662f\u76f8\u5f53\u5bb9\u6613\u7684\u3002<\/p>\n<p>It&#39;s not always this easy<br \/>\n\t\u4f46\u4e8b\u5b9e\u4e0a\u5e76\u4e0d\u603b\u662f\u5982\u6b64\u3002<\/p>\n<p>Unfortunately, if too many adjacent lines have been changed, interpretation isn&#39;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:<br \/>\n\t\u5f88\u4e0d\u5e78\u7684\u662f\u3002\u5982\u679c\u592a\u591a\u7684\u76f8\u90bb\u884c\u6709\u6539\u52a8\u7684\u8bdd\uff0c\u5c31\u4e0d\u662f\u90a3\u4e48\u660e\u663e\u5566\u3002\u4f46\u662f\u5982\u679c\u77e5\u9053\u6bcf\u884c\u662f\u600e\u4e48\u6539\u52a8\u7684\uff0c\u4f60\u5c31\u88ab\u8f7b\u6613\u627e\u5230\u533a\u522b\u3002\u6bd4\u5982\u8fd9\u4e2a\u4f8b\u5b50\uff0c\u6587\u4ef63\u5305\u542b\u4e86\u53d7\u635f\u7684\u5185\u5bb9\uff0c\u800c\u6587\u4ef64(\u5176\u5b9e\u548c\u4e0a\u4e2a\u4f8b\u5b50\u4e2d\u7684\u6587\u4ef62\u4e00\u6a21\u4e00\u6837)\u5305\u542b\u4e86\u6b63\u786e\u7684\u5185\u5bb9\uff0c\u8fde\u7eed\u4e09\u884c\u88ab\u6539\u53d8\u4e86\uff0c\u73b0\u5728\u6bcf\u4e2a\u6709\u6539\u52a8\u7684\u884c\u6ca1\u6709\u5728\u6b63\u786e\u7684\u884c\u4e0a\u9762\u76f4\u63a5\u663e\u793a\u51fa\u6765\u3002<\/p>\n<p>diff -u 3 4<br \/>\n\t&#8212; 3 Sun Apr 21 18:57:08 1996<br \/>\n\t+++ 4 Sun Apr 21 18:56:45 1996<br \/>\n\t-1,9 +1,9<br \/>\n\tEcce Eduardus Ursus scalis nunc tump-tump-tump<br \/>\n\toccipite gradus pulsante post Christophorum<br \/>\n\tRobinum descendens. Est quod sciat unus et solus<br \/>\n\t-modus gradibus desendendi, non nunquam autem<br \/>\n\t-sentit, etiam alterum nodum exitare, dummodo<br \/>\n\t-pulsationibus desinere et de no modo meditari<br \/>\n\t+modus gradibus descendendi, nonnunquam autem<br \/>\n\t+sentit, etiam alterum modum exstare, dummodo<br \/>\n\t+pulsationibus desinere et de eo modo meditari<br \/>\n\tpossit. Deinde censet alios modos non esse. En,<br \/>\n\tnunc ipse in imo est, vobis ostentari paratus.<br \/>\n\tWinnie ille Pu.<\/p>\n<p>It takes a little more work to find the added mistakes; &ldquo;nodum&rdquo; for &ldquo;modum&rdquo; and &ldquo;exitare&rdquo; for &ldquo;exstare&rdquo;. 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&#39;ve done is (potentially) shrink the amount of comparison you have to do.<br \/>\n\t\u8fd9\u5c31\u9700\u8981\u82b1\u8d39\u66f4\u591a\u7684\u5de5\u4f5c\u6765\u627e\u5230\u9519\u8bef\u5566\u3002&ldquo;nodum&rdquo; for &ldquo;modum&rdquo; and &ldquo;exitare&rdquo; for &ldquo;exstare&rdquo;\u3002\u60f3\u8c61\u4e00\u4e0b\uff0c\u5982\u679c\u8fde\u7eed50\u884c\uff0c\u6bcf\u884c\u90fd\u6709\u4e00\u4e2a\u5b57\u6bcd\u7684\u6539\u53d8\uff0c\u90a3\u4e48\u4f60\u8981\u505a\u7684\u5de5\u4f5c\u53c8\u548c\u4f7f\u7528diff\u524d\u4e00\u6837\u561e\u3002\u9010\u5b57\u9010\u5b57\u7684\u67e5\u627e\uff01diff\u4ec5\u4ec5\u5e2e\u4f60\u628a\u66f4\u6539\u8303\u56f4\u9650\u5b9a\u4e86\u800c\u5df2\u3002<\/p>\n<p>Fortunately, there are several tools for finding these kinds of differences more easily. GNU Emacs has &ldquo;word diff&rdquo; functionality. There is also a GNU &ldquo;wdiff&rdquo; program which helps you find these kinds of differences without using Emacs.<br \/>\n\t\u597d\u6709\u597d\u4e9b\u5de5\u5177\u5e2e\u52a9\u4f60\u5f88\u5bb9\u6613\u5b9a\u4f4d\u5230\u8fd9\u4e9b\u5dee\u5f02\u3002GNU Emacs\u5c31\u6709\u4e00\u4e2a&quot;word diff&quot;\u7684\u529f\u80fd\u3002\u8fd8\u6709\u4e00\u4e2a\u53ebwdiff\u7684GNU\u5de5\u5177\u4e5f\u80fd\u5e2e\u52a9\u4f60\u67e5\u627e\u8fd9\u79cd\u7c7b\u578b\u7684\u9519\u8bef\u3002<\/p>\n<p>Let&#39;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:<br \/>\n\t\u8ba9\u6211\u4eec\u5148\u770b\u770bGNU Emacs\u3002<\/p>\n<p>M-x ediff-files RET<br \/>\n\t5 RET<br \/>\n\t6 RET<\/p>\n<p>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.<\/p>\n<p>Figure 1. ediff-files 5 6<\/p>\n<p>GNU wdiff is also very useful, especially if you aren&#39;t running X. A pager (such as less) is all that is required&mdash;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.<br \/>\n\tGNU wdiff\u4e5f\u5f88\u6709\u7528\uff0c\u7279\u522b\u662f\u4f60\u6ca1\u6709\u8fd0\u884cX\u670d\u52a1\u7684\u65f6\u5019\u3002\u4e00\u4e2a\u5206\u5c4f\u5de5\u5177(\u6bd4\u5982 less)\u5c31\u8db3\u591f\u7528\u6765\u67e5\u627e\u5927\u8303\u56f4\u7684\u5dee\u5f02\u4e86\u3002<\/p>\n<p>Figure 2. wdiff -t 5 6<\/p>\n<p>\/\/\/\/\/\/\/<br \/>\n\t\u597d\u8f9b\u82e6\u3002\u4e0d\u60f3\u8bd1\u4e86~~\u4e0d\u8bd1\u4e86\u3002\u81ea\u5df1\u53bb\u770b\u539f\u6587\u5427\u3002\u7d2f\u6b7b\u54e5\u4e86\u3002<\/p>\n<p>copyright ykyi.net<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587 http:\/\/www.linuxjournal.com\/article\/1237 \u7ffb\u8bd1: www.ykyi.net zausiu \u63a5\u4e0a\u7bc7 Use the diff program to avoid eyestrain and insanity: \u4f7f\u7528diff\u7a0b\u5e8f\u6765\u907f\u514d\u4f1a\u4ee4\u4eba\u53d1\u75af\u7684\u8089\u773c\u67e5\u627e\u3002 diff -u 1 2 &#8212; 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 &hellip; <a href=\"https:\/\/ykyi.net\/?p=191\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Diff, Patch, and Friends(diff, patch\u548c\u4ed6\u4eec\u7684\u76f8\u5173\u5de5\u5177)(\u7b2c\u4e8c\u7bc7)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,7],"tags":[28,83],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-tech_articles","category-unix_linux","tag-diff","tag-unix-tools"],"_links":{"self":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=191"}],"version-history":[{"count":0,"href":"https:\/\/ykyi.net\/index.php?rest_route=\/wp\/v2\/posts\/191\/revisions"}],"wp:attachment":[{"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ykyi.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}