diff和patch配合使用(转载备用)

Linux下diff与patch命令的配合使用

在Linux下,diff与patch命令配合使用可以进行简单的代码维护工作。

[A] diff
diff命令用于比较文件的差异,可以用于制作patch文件。但此命令参数众多、格式多样,所以在此仅介绍较常用的格式。
例如有以下2个文件,源文件为old/test.txt,经修改后的新文件为new/test.txt,其内容分别如下:

old/test.txt:
This is the first line!
This is the second line!
This is the third line!
This is the fourth line!
This is the fifth line!
This is the sixth line!
This is the seventh line!
This is the eighth line!
This is the ninth line!
This is the tenth line!
This is the end~

new/test.txt:
This is the first line!
This is the 2 line!
This is the third line!
This is the fourth line!
This is the fifth line!
This is the sixth line!
This is the eighth line!
This is the ninth line!
This is the tenth line!
This is the 11 line!
This is the end~

A.1.1 格式一
命令:
diff old/test.txt new/test.txt
返回:
2c2
< This is the second line!
---
> This is the 2 line!
7d6
< This is the seventh line!
10a10
> This is the 11 line!

注:此为不带任何参数的默认格式。
返回内容中符号与数字的含义分述如下:
c 代表被标示的行被修改,d 代表被标示的行被删除,a 代表被标示的行被插入;字母前的数字代表源文件的行号,字母后的数字代表新文件的行号;< 符号之后的代表源文件的内容,> 符号之后的代表新文件的内容,--- 符号代表修改前后的分隔符。
例如:
上述的2c2部分表示:源文件第2行由This is the second line!改为This is the 2 line!,修改后的行号依然是2。
上述的7d6部分表示:源文件第7行的This is the seventh line!内容被删除,修改后的行号变为6。因为被整行删除,所以少了一行。

A.1.2 格式二
命令:
diff -Nur old/test.txt new/test.txt
返回:
--- old/test.txt        2011-09-29 17:06:20.505459007 +0800
+++ new/test.txt        2011-09-29 17:07:59.113458999 +0800
@@ -1,12 +1,12 @@
 This is the first line!
-This is the second line!
+This is the 2 line!
 This is the third line!
 This is the fourth line!
 This is the fifth line!
 This is the sixth line!
-This is the seventh line!
 This is the eighth line!
 This is the ninth line!
 This is the tenth line!
+This is the 11 line!
 This is the end~

注:此为较常用的格式。各参数含义:N(或--new-file)表示如果某个文件仅在一个目录中出现,则假定其在另一个目录中为空文件;u(或-U<列数>、--unified=<列数>)表示以合并的方式显示文件内容的不同;r(或--recursive)表示比较子目录中的文件。
返回内容中符号与数字的含义分述如下:
开头2行为文件路径与修改时间的描述,--- 之后的代表源文件,+++ 之后的代表新文件。第3行@@之间的部分,- 之后的数字代表源文件的行号范围,+ 之后的数字代表新文件的行号范围。余下的正文部分中,- 之后的行代表源文件的内容,亦即需要删除的内容;+ 之后的行代表新文件的内容,亦即需要添加的内容;不含有符号的各行代表没有变化,只是引用的上下文。

A.1.3 格式三
命令:
git diff old/test.txt new/test.txt
返回:
diff --git a/old/test.txt b/new/test.txt
index fc5b2e4..63e3519 100644
--- a/old/test.txt
+++ b/new/test.txt
@@ -1,12 +1,12 @@
 This is the first line!
-This is the second line!
+This is the 2 line!
 This is the third line!
 This is the fourth line!
 This is the fifth line!
 This is the sixth line!
-This is the seventh line!
 This is the eighth line!
 This is the ninth line!
 This is the tenth line!
+This is the 11 line!
 This is the end~

注:此为git命令,如果当前Linux系统未安装git,则无法使用此格式。此方法与格式二的返回内容相似,但是书写简单,无需添加参数。
返回内容中符号与数字的含义分述如下:
第1行标明了源文件与新文件的路径,系统会自动添加a、b用来标识源文件与新文件,而实际上并不存在a、b目录。
第2行为index索引信息,不影响正文内容,所以无需理会。
第3行及以后的内容与格式二中的对应部分大体一致,在此不再赘述。

A.2 生成patch文件
要想生成patch文件,只需将diff命令的返回内容重定向到一个文本文件中即可。
例如:
diff -Nur old new > test.patch
注:上述命令将目录作为参数进行比较。

[B] patch
patch命令可以利用diff命令生成的patch文件进行文件的更新。
注:如果只返回patching file ***,则代表更新成功;如果还提示Hunk #1 FAILED at *,则表示更新失败,原因可能是文件已进行其他修改,此时就需要根据提示的出错的行号手动更新了。

B.1.1 格式一
通过此格式生成的patch文件由于缺少文件路径的标注,所以需要指定待更新的文件路径。
命令:
patch old/test.txt < test.patch

B.1.2 格式二
通过此格式生成的patch文件中会包含文件路径,所以无需指定文件路径的参数,但是要指定文件路径层级。
命令:
patch -p0 < test.patch
注:参数-p(或--strip=<剥离层级>)表示欲剥离几层路径名称。此例中patch文件中的路径为:old/test.txt,所以当test.patch文件与old目录处于同一目录时,则不需要剥离文件路径,所以用-p0。
如果将test.patch文件移动到old目录下,亦即test.patch文件与test.txt文件处于相同目录时,则需要剥离一层路径,亦即忽略old路径,所以要将参数改为-p1。以此类推。

B.1.3 格式三
与格式二的patch方式相似,不过因其自动添加了一层目录a、b,导致其剥离层级比格式二大1。
命令:
patch -p1 < test.patch

B.2 撤销patch
如果运行patch之后,却发现此patch修改的部分有误,需要还原修改前的文件;或者由于其他原因,需要获取修改前的文件状态。在没有改动的前提下,源文件是可以无损还原的。
命令:
patch -p1 -R < test.patch
注:撤销patch其实很简单,只需要在打patch的命令上添加-R的参数即可。不过前提是:源文件与patch文件没有其他修改,而且源文件与patch文件的相对路径未发生变化。如果相对路径发生变化,需要适当调整-p参数的路径剥离层级。

原文地址:https://www.cnblogs.com/caiyao/p/3810494.html