linux下使用diff制作补丁,使用patch打补丁

来源:互联网 发布:mac迅雷自带播放器 编辑:程序博客网 时间:2024/05/16 08:32

我们使用diff命令制作补丁,使用patch命令打补丁。


(1)对单个文件操作,例如有如下两个源文件:

    test1.c:

#include <stdio.h>#include <stdlib.h>int main () {    int a = 1;    printf("value a: %d\n", a);    return 0;}
    test2.c:

#include <stdio.h>#include <stdlib.h>int main () {    int a = 1;    int b = 2;    printf("value a: %d\n", a);    printf("value b: %d\n", b);    return 0;}

生成从test1.c 到test2.c的补丁: diff -u test1.c test2.c > test.patch,则生成了补丁文件:

--- test1.c2017-10-11 15:07:06.197500113 +0800+++ test2.c2017-10-11 15:06:46.138506900 +0800@@ -3,8 +3,10 @@  int main () {     int a = 1;+    int b = 2;      printf("value a: %d\n", a);+    printf("value b: %d\n", b);      return 0; }

对test1.c打补丁:patch -p0 < test.patch,则可以看到 patching file test1.c的打印,表示补丁已经打成功。


(2)对文件夹操作,例如有dir1 dir2两个目录:

    使用diff制作补丁: diff -uNr dir1 dir2 > dir.patch

    使用patch打补丁:cd dir2

                                  patch -p1 < dir.patch


    说明:由于文件夹操作,可能有多级子目录,所以需要递归-r, 可能存在文件删除或者新建,所以需要-N

              由于对文件夹打补丁,最外层目录无需操作,直接对内部其他文件 操作,所以需要-p1忽略最外层目录,且需要先cd dir2



原创粉丝点击