使用和制作patch文件

使用和制作patch文件

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

使用和制作patch文件

字体: | 打印

原文http://www.linuxsir.org/bbs/showthread.php?t=240802

今天上QQ的时候发现eva不能用了,后来又看到了解决方法,所以想打个补丁,只是不会:-)。后来查了查明确了,写了个总结,分享一下,也不知道曾经有没有这方面的东西,希望我这个不是多余的。

创建补丁文件:



CODE:

diff -Naur 旧的文件夹 新的文件夹 > patch文件
或者
diff -Naur 旧的文件 新的文件 > patch文件

对于文件夹层数的一些限制

在创建patch的时候文件夹的层数应当是一样的,比方

CODE:

--- old/modules/pcitableMon Sep 27 11:03:56 1999
+++ new/modules/pcitableTue Dec 19 20:05:41 2000

这样是能够的。

CODE:

--- old/try1/other/modules/pcitableMon Sep 27 11:03:56 1999
+++ new/modules/pcitableTue Dec 19 20:05:41 2000

这样做可能会有一些问题。

怎样使用patch
对于一个patch文件,有两种经常使用用法:
1.

CODE:

cat new-patch | patch -p02.

2、


CODE:

patch -p0 < new-patch

patch命令里面的层数(-p0?-p1?)
參数-p来指定从第几层開始比較。比方有一个patch文件的补丁头是这种:

CODE:

--- old/modules/pcitableMon Sep 27 11:03:56 1999
+++ new/modules/pcitableTue Dec 19 20:05:41 2000

假设使用參数-p0,就表示从当前文件夹,找一个叫作new的文件夹,在它以下找一个叫modules的文件夹,再在它以下找一个叫pcitableMon的文件夹。
假设使用參数-p1,就表示忽略第一层,从当前文件夹找一个叫modules的文件夹,在它以下找一个叫modules的文件夹。这样会忽略掉补丁头提到的new文件夹。
依此类推。

patch文件的结构
补丁头
补丁头是分别由—/+++开头的两行,用来表示要打补丁的文件。
一个补丁文件里的多个补丁
一个补丁文件里可能包括以—/+++开头的非常多节,每一节用来打一个补丁。所以在一个补丁文件里能够包括好多个补丁。

块是补丁中要改动的地方。它通常由一部分不用改动的东西開始和结束。他们仅仅是用来表示要改动的位置。他们通常以@@開始,结束于还有一个块的開始或者一个新的补丁头。
块的缩进
块会缩进一列,而这一列是用来表示这一行是要添加还是要删除的。
块的第一列
+号表示这一行是要加上的。
-号表示这一行是要删除的。
没有加号也没有减号表示这里仅仅是引用的而不须要改动。

一个patch的样例

CODE:

diff -u old/modules/pcitable new/modules/pcitable
--- old/modules/pcitableMon Sep 27 11:03:56 1999
+++ new/modules/pcitableTue Dec 19 20:05:41 2000
@@ -1,4 +1,6 @@
0x0e110xae10"cpqarray""Compaq|Smart-2/P RAID Controller"
+0x10000x0010"cpqarray""Compaq|Integrated Array Controller"
+0x10110x0046"cpqarray""Compaq|Smart-2/P RAID Controller"
0x0e110xae32"tlan""Compaq|Netelligent 10/100"
0x0e110xae34"tlan""Compaq|Netelligent 10"
0x0e110xae35"tlan""Compaq|Integrated NetFlex-3/P"
@@ -21,6 +23,7 @@
0x10000x000f"ncr53c8xx""Symbios|53c875"
0x10000x0012"ncr53c8xx""Symbios|53c895a"
0x10000x008f"ncr53c8xx""Symbios|53c875J"
+0x10000x000a"sym53c8xx""Symbios|53c1510"
0x10000x0701"yellowfin""Symbios|83C885 gigabit ethernet"
0x10000x0702"yellowfin""Symbios|Yellowfin G-NIC gigabit ethernet"
0x10110x0001"tulip""DEC|DECchip 21050"
--- old/usr/share/kudzu/pcitableSun Sep 26 17:11:23 1999
+++ new/usr/share/kudzu/pcitableTue Dec 19 20:05:41 2000
@@ -15,6 +15,8 @@
0x0e110x3034"unknown""Compaq|QVision 1280/p"
0x0e110x4000"unknown""Compaq|4000 [Triflex]"
0x0e110xa0f3"ignore""Compaq|Triflex PCI to ISA Bridge"
+0x10000x0010"cpqarray""Compaq|Integrated Array Controller"
+0x10110x0046"cpqarray""Compaq|Smart-2/P RAID Controller"
0x0e110xae10"cpqarray""Compaq|Smart-2/P RAID Controller"
0x0e110xae29"unknown""Compaq|MIS-L"
0x0e110xae2a"unknown""Compaq|MPC"
@@ -46,6 +48,7 @@
0x10000x000f"ncr53c8xx""Symbios|53c875"
0x10000x0012"ncr53c8xx""Symbios|53c895a"
0x10000x008f"ncr53c8xx""Symbios|53c875J"
+0x10000x000a"sym53c8xx""Symbios|53c1510"
0x10000x0701"yellowfin""Symbios|83C885 gigabit ethernet"
0x10000x0702"yellowfin""Symbios|Yellowfin G-NIC gigabit ethernet"
0x10000x0901"unknown""Symbios|61C102"


分析
这个样例是由命令

CODE:

diff -u old/modules/pcitable new/modules/pcitable

创建的。只是最好是用命令diff -Naur来取代diff -u。
它改动了两个文件,new/modules/pcitable和new/usr/share/kudzu/pcitable。
第一个补丁头包括两个块,分别添加了两行和一行。

这个是參考了这篇文http://www.cpqlinux.com/patch.html来总结翻译。因为刚刚接触这些东西,非常多地方可能翻译的不恰当,尤其是一些术语,如有发现问题,请给我留言说明,以便我来改正,谢谢。

我也来说两句 查看所有评论 相关评论

  • superuser (2007-2-13 21:12:26)

    Patching (very) Mini Howto
    Introduction

    This very short document is a slightly lengthened version of an email I wrote in 2003 on the subject of patches, I’ve put it here in case it could be useful to anyone else.
    The Howto

    cd into the directory containing the sources you want to patch:

    CODE:

    $ cd madwifiThen you need to pipe the patch file into the program ‘patch’, like this:

    CODE:

    $ cat /path/to/patch/patch.diff | patch -p1The processs for applying gzipped or bzipped patches is almost identical, only you use a modified version of ‘cat’ which can handle the compression thats been used e.g:

    CODE:

    $ bzcat /path/to/patch/patch.bz2 | patch -p1for bzips, or

    CODE:

    $ zcat /path/to/patch/patch.gz2 | patch -p1for the (more common) gzips.

    Another way which might interest some people (which I’ve just descovered) is to do things the other way around, in this way you can do it without running cat:

    CODE:

    $ patch -p1 < patchI’m not exactly sure how to do this with zipped patches just yet however, my experiments would lead me to belive that its more complex than most of what’s above.

    The option -p1 tells the patch program to remove 1 layer of all the filenames from the input, you do this because normally the person who made the patch is one directory closer to / than you. The best way to explain this is too look at the content of a patch (this is part of the wireless extensions patch):

    CODE:

    diff -u -p linux/include/linux/wireless.15.h linux/include/linux/wireless.h
    --- linux/include/linux/wireless.15.h   Fri Jan 10 16:55:07 2003
    +++ linux/include/linux/wireless.h  Wed Apr  2 16:33:31 2003
    The lines mention “linux/include/[…]”, now if you have more than one set of kernel sources on your system, then the one you want to patch will not necessarily be in as directory called ‘linux’, the -p1 option would strip the linux from the filename and just leave:

    CODE:

    diff -u -p include/linux/wireless.15.h include/linux/wireless.h
    --- include/linux/wireless.15.h   Fri Jan 10 16:55:07 2003
    +++ include/linux/wireless.h  Wed Apr  2 16:33:31 2003
    From the above discussion, it should be fairly easy to see that patch strips from the left hand side, and removes n slashes. Where n is the number the ‘p’ option.

    There are a couple of other things which I thinks its handy to know about patch, first is that not all patches will go on ‘cleanly’, sometimes you will get messages like ‘Hunk suceeded at offset 32’, this means that that part of the patch applied, but not in the exact same place as the original author intended. This happens all the time with the madwifi driver from cvs.

    Second is the -R swich for ‘patch’, this:

    CODE:

    $ cat /path/to/patch/patch.diff | patch -p1 -Rwould remove the patch from your current directory tree.
    And finally

    One last thing to remember: I am no expert, and some of this may be a bit wrong, missing something or whatever. Drop me a line if it is. 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118051.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 机器学习:代价函数cost function

    机器学习:代价函数cost function本文系转载,咯有修改原博客地址:http://blog.csdn.net/u012162613/article/details/44239919在此,向原作者表达感谢,致敬!1.从方差代价函数说起代价函数经常用方差代价函数(即采用均方误差MSE),比如对于一个神经元(单输入单输出,sigmoid函数),定义其代价函数为:C=(y−a)22C=\frac{(y-a)^2}{2}其中yy是我们期望

    2022年5月15日
    46
  • 国外的大龄程序员在干什么工作_为什么程序员年龄大了没人要

    国外的大龄程序员在干什么工作_为什么程序员年龄大了没人要在Quora有个帖子:我今年35岁了,是不是太老了,没法加入Google,Facebook,Microsoft或者Apple了?下面的回复让人叹为观止,我摘录几个:萨特南·辛格Google软件工程师(2017–present)不,我在51岁的时候加入了Google,我们团队还有几个比我年长的人!他们都是非常卓越的软件工程师,一生都在编程,并且获得了被认为非…

    2022年9月13日
    2
  • MySQL八股文连环45问,你能坚持第几问?「建议收藏」

    MySQL八股文连环45问,你能坚持第几问?「建议收藏」文人从事多年面试工作,将MySQL面试分享给大家,希望大家顺利拿下offer

    2022年5月18日
    40
  • task scheduler什么意思_女贞子的功效与作用

    task scheduler什么意思_女贞子的功效与作用前言本文隶属于专栏《1000个问题搞定大数据技术体系》,该专栏为笔者原创,引用请注明来源,不足和错误之处请在评论区帮忙指出,谢谢!本专栏目录结构和参考文献请见1000个问题搞定大数据技术体系正文TaskScheduler的核心任务是提交TaskSet到集群运算并汇报结果。(1)为TaskSet创建和维护一个TaskSetManager,并追踪任务的本地性以及错误信息。(2)遇到Straggle任务时,会放到其他节点进行重试。(3)向DAGScheduler

    2022年10月11日
    2
  • 【3】Java面试-Servlet

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 Q1。什么是servlet? Java Servlet是服务器端技术,通过提供对动态响应和数据持久性的支持来扩展We…

    2021年6月23日
    84
  • 免费申请国外免费域名超详细教程

    免费申请国外免费域名超详细教程1.首先申请免费域名网站:https://my.freenom.com/domains.php2.填入域名,这里我们以xcflag为列(尽量选择复杂一点的或者五个字母以上的域名,因为简单的有些域名是需要收费的),点击检查可用性。3.可以看到很多免费的域名(用的谷歌翻译插件,翻译有时候不是很准确,free翻译过来应该是免费而不是自由,之后会写一些关于谷歌插件的笔记,详细讲解)4.我们选择xcflag.tk点击立即获取,稍等一会点击购物车查看绿色按钮5.默认三个月试用,这里下拉框我们选择十二个月

    2022年6月30日
    52

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号