《Linux diff與patch的深入分析》

來源:互聯網
上載者:User

diff的輸出格式分為傳統格式和統一格式

1)diff的傳統格式輸出.

############################################
cat before.txt
輸出:
This is a line to be deleted
This is a line that will be changed
This is a line that will be unchanged

cat after.txt
輸出:
This is a line that has been changed
This is a line that will be unchanged
This is a line that has been added
############################################
diff before.txt after.txt
輸 出:
1,2c1
< This is a line to be deleted
< This is a line that will be changed
---
> This is a line that has been changed
3a3
> This is a line that has been added
############################################

注釋:
傳統格式的輸出
1,2c1是指替換第1個檔案的第1,2行到第2個檔案的第2行,這裡的1,2是指第1個檔案的第1,2行,c是替換的意思,最後的1是第2個檔案的第1行
<號是指第1個檔案更改或刪除的行
---號是分割兩個檔案
>號是第2個檔案中增加或刪除的行
3a3是指將第2個檔案的第3行插入到第一個檔案的第3行
也就是說第1個檔案的:
< This is a line to be deleted
< This is a line that will be changed
被替換成第2個檔案的:
> This is a line that has been changed
由於第1個檔案的第3行和第2個檔案的第2行一致,所以不做修改.
由於第2個檔案的第3行是第1個檔案所不具有的,所以在第1個檔案的最後一行增加:
> This is a line that has been added

2)patch命令的應用

用diff的傳統格式輸出:
#################################
diff before.txt after.txt >mypatch.txt
#################################

用patch修補before.txt檔案,使before.txt和after.txt一致.
#################################
cat mypatch.txt |patch before.txt
輸出:
patching file before.txt
#################################

比較兩個檔案,現在是一致的了.
#################################
cmp before.txt after.txt
#################################

用patch命令恢複before.txt.
#################################
patch -R before.txt <mypatch.txt
輸出:
patching file before.txt
#################################

注:-R標記告訴patch在反向上應用區別或者撤銷patch.

再比較兩個檔案,現在不一致了.
#################################
cmp before.txt after.txt
輸出:
before.txt after.txt differ: byte 17, line 1
#################################

3)diff的統一格式輸出.

#################################
diff -u before.txt after.txt |tee mypatch.diff
輸出:
--- before.txt  2009-06-20 05:21:49.000000000 +0800
+++ after.txt   2009-06-20 04:03:16.000000000 +0800
@@ -1,3 +1,3 @@
-This is a line to be deleted
-This is a line that will be changed
+This is a line that has been changed
 This is a line that will be unchanged
+This is a line that has been added
#################################

注釋:
diff -u選項是統一格式輸出.
--- before.txt  2009-06-20 05:21:49.000000000 +0800
--- before.txt是指舊檔案
+++ after.txt   2009-06-20 04:03:16.000000000 +0800
+++ after.txt是指新檔案.
@@ -1,3 +1,3 @@
@@ -1,3是指第1個檔案一共有3行,+1,3 @@是指第2個檔案一共有3行.
-This is a line to be deleted
-This is a line that will be changed
是被刪除的行
+This is a line that has been changed
是增加的行
 This is a line that will be unchanged
沒有-號和+號是指該行不變,因為after.txt和before.txt都有這行.
+This is a line that has been added
是增加的行
diff的統一格式比較與輸出是按順序進行的.

4)diff命令在目錄中的應用.

建立old和new目錄,old目錄包含了初始內容,new目錄包含檔案的最新版本.
##########################################
mkdir old new
echo "This is one. It's unchanged." | tee old/one new/one
echo "This is two. It will change." > old/two
echo "This is two. It changed.">new/two
echo "This is three. It's new" > new/three
##########################################

建立修補檔案
##########################################
diff -Nur old/ new/ >mypatch.diff
##########################################
注:-r選項按照檔案目錄遞迴建立修補檔案.
-u還是統一模式
-N是指當diff遇到一個只存在於兩個樹中的一個樹中的檔案時,預設情況下跳過檔案並且列印一個警告到stderr.
這個行為可以通過-N選項來更改,這也導致了diff認為丟失的檔案實際上是存在的,但它是空的.採用這種方式,
一個修補檔案可以包括已經建立的檔案.然後應用Hotfix建立新的檔案.

##########################################
more mypatch.diff
輸出:
diff -Nur old/three new/three
--- old/three   1970-01-01 08:00:00.000000000 +0800
+++ new/three   2009-06-20 06:55:34.000000000 +0800
@@ -0,0 +1 @@
+This is three. It's new
diff -Nur old/two new/two
--- old/two     2009-06-20 06:55:08.000000000 +0800
+++ new/two     2009-06-20 06:55:21.000000000 +0800
@@ -1 +1 @@
-This is two. It will change.
+This is two. It changed.
##########################################
注釋:
diff -Nur old/three new/three是指下面比較old/three new/three兩個檔案.
因為沒有old/three檔案,所以在old/three中增加+This is three. It's new
diff -Nur old/two new/two是指下面比較old/two new/two兩個檔案
因為old/two與new/two的第3行不一致,所以刪除This is two. It will change.增加This is two. It changed.

打補丁到old目錄,建立old/three以及更改old/two
##########################################
patch --dir old< mypatch.diff
ls -l     old/
輸出:
one    three  two
##########################################

恢複old目錄的內容,包括刪除old/three,以及恢複old/two檔案
##########################################
patch --dir old -R <mypatch.diff
輸出:
ls -l old/
one  two 
##########################################

5)檢查和合并更改

用vim反白單個字元的更改來表示區別.

##########################################
vim -d after.txt before.txt
##########################################

用gui工具gvimdiff來顯示兩個檔案.

##########################################
gvimdiff after.txt before.txt
##########################################

建立檔案orig.c

##########################################
vi orig.c
void foo(void)
{
    printf("This will be changed by me. /n");

    printf("This will be unchanged,/n");

    printf("This will be changed by you./n");
}
##########################################

複製檔案orig.c到me.c,更改第4行為printf("This was changed by me. /n");
##########################################
vi me.c
void foo(void)
{
    printf("This was changed by me. /n");

    printf("This will be unchanged,/n");

    printf("This will be changed by you./n");
}
##########################################

複製檔案orig.c到you.c,更改第7行為printf("This was changed by you./n");
##########################################
vi you.c
void foo(void)
{
    printf("This will be changed by me. /n");

    printf("This will be unchanged,/n");

    printf("This was changed by you./n");
}
##########################################

版本工具如cvs,subversion使用GNU合并工具稱為diff3.
##########################################
diff3 me.c orig.c you.c
輸出:
====1
1:3c
      printf("This was changed by me. /n");
2:3c
3:3c
      printf("This will be changed by me. /n");
====3
1:7c
2:7c
      printf("This will be changed by you./n");
3:7c
      printf("This was changed by you./n");

注:
在沒有參數的情況下,diff3產生的輸出說明了那行更改.
====1和====3指明造成同原始檔案不同的是哪一個修改檔案.
編號方式基於參數序列.
也就是第1個檔案和第3個檔案與原檔案不同.
1:3c
      printf("This was changed by me. /n");
3:3c
      printf("This will be changed by me. /n");
1:3c表示第1個檔案的第3行與3:3c表示的第3個檔案的第3行不同.
為什麼不顯示與原檔案的比較呢。因為第3個檔案的第3行與源檔案(第2個檔案)相同.所以與哪個檔案比較無所謂了.

2:7c
      printf("This will be changed by you./n");
3:7c
      printf("This was changed by you./n");
2:7c表示第2個檔案的第7行與3:7c表示的第3個檔案的第7行不同.

diff3會試圖為我們進行合并.合并是在源檔案的基礎上,依據兩個新檔案進行修改
源檔案是第二個檔案,第一個檔案和第三個檔案可以互換,但他們必須有共同的祖先,就是第二個檔案.

#######################################
diff3 -m me.c orig.c you.c |cat -n
輸出:
     1    void foo(void)
     2    {
     3        printf("This was changed by me. /n");
     4   
     5        printf("This will be unchanged,/n");
     6   
     7        printf("This was changed by you./n");
     8    }
########################################

為了測試更複雜的環境,建立一個檔案orig.c.1
內容如下:
########################################
vi orig.c.1
void foo(void)
{
    printf("This will be changed by both of us./n");
}
########################################

用diff3 -m再次比較輸出,如下:
########################################
diff3 -m me.c orig.c.1 you.c
void foo(void)
{
<<<<<<< me.c
    printf("This was changed by me. /n");

    printf("This will be unchanged,/n");

    printf("This will be changed by you./n");
||||||| orig.c.1
    printf("This will be changed by both of us./n");
=======
    printf("This will be changed by me. /n");

    printf("This will be unchanged,/n");

    printf("This was changed by you./n");
>>>>>>> you.c
}
########################################

注釋:以上的格式,同cvs update,需要人工合并檔案的格式是一致的.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.