diff和patch
1,diff [命令行选项] 原始文件 新文件
由于历史原因,diff有三种格式:
* 正常格式(normal diff)
* 上下文格式-c(context diff)
* 合并格式-u(unified diff)
常用命令行选项如下:
-r 递归处理目录
-u 输出统一格式(unified format)
-N patch里包含新文件
-a patch里可以包含二进制文件
2,patch:
patch -p参数 <补丁文件(相对路径,绝对路径皆可)
或者 patch [options] 目标文件 补丁文件
说明:
(1) ”-p参数“由patch命令的执行目录(工作目录)和补丁头共同决定(补丁头是不变的,其实就是由patch命令的执行目录--当前目录决定)。
参数-p来指定从第几层开始搜索目标文件(寻找被打补丁的目标文件)。比如有一个patch文件的补丁头是这样的:
--- oldfile_path
+++ local/squid/need_patched_file
如果使用参数-p0,就表示从当前目录,找一个叫作local的目录,在它下面找一个叫squid的目录,再在它下面找一个叫need_patched_file的文件(目标文件就这样通过“-p参数”找到了)
如果使用参数-p1,就表示忽略第一层,从当前目录找一个叫squid的目录,在它下面找一个叫need_patched_file的文件。这样会忽略掉补丁头提到的local目录。
如果使用参数-p2,依此类推。
(2) 打补丁时首先打开补丁文件,根据补丁头和当前工作目录确定"-p参数",一般情况下,如果当前目录有需要打补丁的软件包(该软件包的顶极目录),则使用“-p0”即可
(3) 如果patch成功,缺省是不建备份文件的(注:FreeBSD下的patch工具缺省是保存备份),如果你需要,可以加上 b 开关。这样把修改前的文件以“原文件名.orig”的名字做备份。如果你喜欢其它后缀名,也可以用“b 后缀”来指定。
(4) 关于二进制文件的说明:binary文件可以原始方式存入patch文件。diff可以生成(加-a选项),patch也可以识别。如果觉得这样的patch文件太难看,解决方法之一是用uuencode处理该binary文件。
其他常用参数:-R:卸载patch包
-N: 可以让你打patch文件多遍 Ignore patches that seem to be reversed or already applied.
--dry-run:尝试patch软件,并不真正修改软件
--verbose 冗余显示更多信息
-pnum的详细解释man手册: 即-pN 表示去掉目标文件路径前面N个“目录分隔符”斜线,即"/";即只要目标文件给出的是绝对路径,只要用-p0即可
Strip the smallest prefix containing num leading slashes from each file name found in the patch file. A
sequence of one or more adjacent slashes is counted as a single slash. This controls how file names found
in the patch file are treated, in case you keep your files in a different directory than the person who sent
out the patch. For example, supposing the file name in the patch file was
/u/howard/src/blurfl/blurfl.c
setting -p0 gives the entire file name unmodified, -p1 gives
u/howard/src/blurfl/blurfl.c
without the leading slash, -p4 gives
blurfl/blurfl.c
and not specifying -p at all just gives you blurfl.c.
参考:diff有三种格式 读懂diff