使用 diff、patch 生成生效补丁文件

最近几天再看uboo和linux内核源码,上来第一条:你要会打补丁吧!

之前用git时用过diff,和Linux命令中的diff其实是一个东西。当我们比较同一个软件的不同版本时,会用到diff。

diff一般处理纯文本文件,且以一行为单位进行比较处理的。先来看看diff的usage(详细用法可以自行man diff):

 diff   [-bBi]   from-file   to-file
选项和参数:

                     from-file :作为原始比对文档的档名;
                     to-file         作为目的比对文档的档名;        注意,from-file 和 to-file 可以 用 - 取代, 代表『Standard input』。
        -b :忽略一行中仅有空白字符的差异,即忽略空白符
        -B :忽略空白行的差异。
         -i :忽略大小写的不同。
光说不练假把式,写个文本文件来体验下diff吧。我这里已经写过好了两个.c文件
example.c

#include "stdio.h"
 
int main(int argc, char *argv[])
{
	printf("hello CentOS_6!\n");
 
	return 0;
 
}

example_new.c

#include "stdio.h"
 
#define  MAX_VALUE  99
 
int main(int argc, char* argv[])
{
	int i =0;
	for(; i < MAX_VALUE; i ++)
	{
	    printf("This is %d times\n", i);
	}
 
	return 0;
}

执行 diff 查看文件差异:

[fc@localhost Documents]$ diff example.c example_new.c 
3c3,5                               //左边example.c的第三行被右边example_new.c的第3-5行替换
< int main(int argc, char *argv[])  //并列出example.c的第三行
---
> #define  MAX_VALUE  99            //列出example_new.c的第3-5行
> 
> int main(int argc, char* argv[])
5c7,11				   //同上啦,不解释
< 	printf("hello CentOS_6!\n");
---
> 	int i =0;
> 	for(; i < MAX_VALUE; i ++)
> 	{
> 	    printf("This is %d times\n", i);
> 	}
8d13				  //example.c的第8行被删除,基准是右边的第13行
< 

既然有了 diff了,刚刚分析了example.c的两个版本了,如果现在想把旧的文档升级成新的文档,该咋整呢?Linux下传统的做法是先比较新旧版本的差异,再将差异文件制作成为补丁文件,再由补丁文件更新旧文件。

[fc@localhost Documents]$ diff -Naur example.c  example_new.c >example.patch
[fc@localhost Documents]$ cat example.patch 
--- example.c	2016-11-15 21:50:37.827008246 -0800
+++ example_new.c	2016-11-15 21:56:11.722007726 -0800
@@ -1,9 +1,14 @@
 #include "stdio.h"
 
-int main(int argc, char *argv[])
+#define  MAX_VALUE  99
+
+int main(int argc, char* argv[])
 {
-	printf("hello CentOS_6!\n");
+	int i =0;
+	for(; i < MAX_VALUE; i ++)
+	{
+	    printf("This is %d times\n", i);
+	}
 
 	return 0;
-
 }

由上可知,一般我们用diff做出来的补丁文件就以.patch后缀,patch命令再用这个.patch文件更新旧文档,它看到 - 会删除,看到 + 会加入。还是先来看看patch的用法:

 patch -pN < patch_file <==更新
 patch -R -pN < patch_file <==还原
选项和参数:
                     -p :后面表示忽略几层目录。
                     -R :代表还原,将新文件还原成旧的版本。

[fc@localhost Documents]$ patch -p0 < example.patch 
patching file example.c
[fc@localhost Documents]$ cat example.c 
#include "stdio.h"
 
#define  MAX_VALUE  99
 
int main(int argc, char* argv[])
{
	int i =0;
	for(; i < MAX_VALUE; i ++)
	{
	    printf("This is %d times\n", i);
	}
 
	return 0;
}

 可以看到打过补丁后 example.c 与 example_new.c 一样了。现在再来个版本回滚:

[fc@localhost Documents]$ patch -R -p0 < example.patch 
patching file example.c
[fc@localhost Documents]$ cat example.c
#include "stdio.h"
 
int main(int argc, char *argv[])
{
	printf("hello CentOS_6!\n");
 
	return 0;
 
}

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值