• 补丁(patch)的制作与应用


    命令简介

    用到的两个命令是diff和patch。

    diff

    diff可以比较两个东西,并可同时记录下二者的区别。制作补丁时的一般用法和常见选项为:

    diff 【选项】 源文件(夹) 目的文件(夹)
    
    -r
    递归。设置后diff会将两个不同版本源代码目录中的所有对应文件全部都进行一次比较,包括子目录文件。
    -N
    选项确保补丁文件将正确地处理已经创建或删除文件的情况。
    -u
    输出每个修改前后的3行,也可以用-u5等指定输出更多上下文。
    -E, -b, -w, -B, --strip-trailing-cr
    忽略各种空白,可参见文档,按需选用。

    patch

    patch的作用则是将diff记录的结果(即补丁)应用到相应文件(夹)上。最常见的用法为:

    patch -pNUM <patchfile>
    
    -p Num
    忽略几层文件夹,随后详解。
    -E
    选项说明如果发现了空文件,那么就删除它
    -R
    取消打过的补丁。

    为了解释 -p 参数,需要看看如下patch文件片段:

    --- old/modules/pcitable       Mon Sep 27 11:03:56 1999
    +++ new/modules/pcitable       Tue Dec 19 20:05:41 2000

    如果使用参数 -p0,那就表示从当前目录找一个叫做old的文件夹,再在它下面寻找 modules/pcitable 文件来执行patch操作。
    而如果使用参数 -p1,那就表示忽略第一层目录(即不管old),从当前目录寻找 modules 的文件夹,再在它下面找pcitable。

    应用

    利用以上命令,处理单个文件补丁的方法:

    # 产生补丁
    diff -uN from-file to-file >to-file.patch
     
    # 打补丁
    patch -p0 < to-file.patch
     
    # 取消补丁
    patch -RE -p0 < to-file.patch

    对整个文件夹打补丁的情况:

    # 产生补丁
    diff -uNr  from-docu  to-docu  >to-docu.patch
     
    # 打补丁
    cd to-docu
    patch -p1 < to-docu.patch
     
    # 取消补丁
    patch -R -p1 <to-docu.patch

    另外,使用版本控制工具时,可以直接用svn diffgit diff生成补丁文件。

    值得一提的是,由于应用补丁时的目标代码和生成补丁时的代码未必相同,打补丁操作可能失败。补丁失败的文件会以.rej结尾,下面命令可以找出所有rej文件:

    find . -name '*.rej'
    

    patch文件构成

    补丁文件里到底存储了哪些信息呢?看看这个例子:

    --- test0       2006-08-18 09:12:01.000000000 +0800
    +++ test1       2006-08-18 09:13:09.000000000 +0800
    @@ -1,3 +1,4 @@
    +222222
     111111
    -111111
    +222222
     111111
    补丁头
    补丁头是分别由---/+++开头的两行,用来表示要打补丁的文件。---开头表示旧文件,+++开头表示新文件。
    一个补丁文件中的多个补丁
    一个补丁文件中可能包含以---/+++开头的很多节,每一节用来打一个补丁。所以在一个补丁文件中可以包含好多个补丁。
    块是补丁中要修改的地方。它通常由一部分不用修改的东西开始和结束。他们只是用来表示要修改的位置。他们通常以@@开始,结束于另一个块的开始或者一个新的补丁头。
    块的缩进
    块会缩进一列,而这一列是用来表示这一行是要增加还是要删除的。
    块的第一列
    +号表示这一行是要加上的。-号表示这一行是要删除的。没有加号也没有减号表示这里只是引用的而不需要修改。

    实例分析

    单文件补丁

    设当前目录有文件 test0

    111111
    111111
    111111

    和文件test1

    222222
    111111
    222222
    111111

    使用diff创建补丁test1.patch

    diff -uN test0 test1 > test1.patch

    因为是单个文件,故不需要 -r 选项。此命令得到如下补丁:

    --- test0       2006-08-18 09:12:01.000000000 +0800
    +++ test1       2006-08-18 09:13:09.000000000 +0800
    @@ -1,3 +1,4 @@
    +222222
     111111
    -111111
    +222222
     111111

    要应用补丁,只需:

    $ patch -p0 < test1.patch
    patching file test0

    此时test0就和test1一样了。

    如果要取消补丁做出的更改,恢复旧版本:

    $ patch -RE -p0 < test1.patch
    patching file test0

    文件夹补丁

    设有如下环境:

    --prj0/
         test0
         prj0name
    --prj1/
         test1
         prj1name
    

    prj0/prj0name内容为如下三行:

    --------
    prj0/prj0name
    --------
    

    prj1/prj1name内容为如下三行:

    --------
    prj1/prj1name
    --------
    

    用 diff -uNr 创建补丁,

    diff -uNr prj0 prj1 > prj1.patch

    得到的patch文件为:

    diff -uNr prj0/prj0name prj1/prj0name
    --- prj0/prj0name       2006-08-18 09:25:11.000000000 +0800
    +++ prj1/prj0name       1970-01-01 08:00:00.000000000 +0800
    @@ -1,3 +0,0 @@
    ---------
    -prj0/prj0name
    ---------
    diff -uNr prj0/prj1name prj1/prj1name
    --- prj0/prj1name       1970-01-01 08:00:00.000000000 +0800
    +++ prj1/prj1name       2006-08-18 09:26:36.000000000 +0800
    @@ -0,0 +1,3 @@
    +---------
    +prj1/prj1name
    +---------
    diff -uNr prj0/test0 prj1/test0
    --- prj0/test0  2006-08-18 09:23:53.000000000 +0800
    +++ prj1/test0  1970-01-01 08:00:00.000000000 +0800
    @@ -1,3 +0,0 @@
    -111111
    -111111
    -111111
    diff -uNr prj0/test1 prj1/test1
    --- prj0/test1  1970-01-01 08:00:00.000000000 +0800
    +++ prj1/test1  2006-08-18 09:26:00.000000000 +0800
    @@ -0,0 +1,4 @@
    +222222
    +111111
    +222222
    +111111

    如果要应用此补丁,则:

    $ ls
    prj0  prj1  prj1.patch
    $ cd prj0
    $ patch -p1 < ../prj1.patch
    patching file prj0name
    patching file prj1name
    patching file test0
    patching file test1

    此时可用ls看到打补丁后的结果:

    $ ls
    prj1name  test1

    类似的,如果要回滚补丁操作:

    $ patch -R -p1 < ../prj1.patch
    patching file prj0name
    patching file prj1name
    patching file test0
    patching file test1
    $ ls
    prj0name  test0


    转自:http://linux-wiki.cn/wiki/zh-hans/%E8%A1%A5%E4%B8%81(patch)%E7%9A%84%E5%88%B6%E4%BD%9C%E4%B8%8E%E5%BA%94%E7%94%A8

    Usage: patch [OPTION]... [ORIGFILE [PATCHFILE]]

    
    

    Input options:

    
    

    -p NUM --strip=NUM Strip NUM leading components from file names.
    -F LINES --fuzz LINES Set the fuzz factor to LINES for inexact matching.
    -l --ignore-whitespace Ignore white space changes between patch and input.

    
    

    -c --context Interpret the patch as a context difference.
    -e --ed Interpret the patch as an ed script.
    -n --normal Interpret the patch as a normal difference.
    -u --unified Interpret the patch as a unified difference.

    
    

    -N --forward Ignore patches that appear to be reversed or already applied.
    -R --reverse Assume patches were created with old and new files swapped.

    
    

    -i PATCHFILE --input=PATCHFILE Read patch from PATCHFILE instead of stdin.

    
    

    Output options:

    
    

    -o FILE --output=FILE Output patched files to FILE.
    -r FILE --reject-file=FILE Output rejects to FILE.

    
    

    -D NAME --ifdef=NAME Make merged if-then-else output using NAME.
    -m --merge Merge using conflict markers instead of creating reject files.
    -E --remove-empty-files Remove output files that are empty after patching.

    
    

    -Z --set-utc Set times of patched files, assuming diff uses UTC (GMT).
    -T --set-time Likewise, assuming local time.

    
    

    --quoting-style=WORD output file names using quoting style WORD.
    Valid WORDs are: literal, shell, shell-always, c, escape.
    Default is taken from QUOTING_STYLE env variable, or 'shell' if unset.

    
    

    Backup and version control options:

    
    

    -b --backup Back up the original contents of each file.
    --backup-if-mismatch Back up if the patch does not match exactly.
    --no-backup-if-mismatch Back up mismatches only if otherwise requested.

    
    

    -V STYLE --version-control=STYLE Use STYLE version control.
    STYLE is either 'simple', 'numbered', or 'existing'.
    -B PREFIX --prefix=PREFIX Prepend PREFIX to backup file names.
    -Y PREFIX --basename-prefix=PREFIX Prepend PREFIX to backup file basenames.
    -z SUFFIX --suffix=SUFFIX Append SUFFIX to backup file names.

    
    

    -g NUM --get=NUM Get files from RCS etc. if positive; ask if negative.

    
    

    Miscellaneous options:

    
    

    -t --batch Ask no questions; skip bad-Prereq patches; assume reversed.
    -f --force Like -t, but ignore bad-Prereq patches, and assume unreversed.
    -s --quiet --silent Work silently unless an error occurs.
    --verbose Output extra information about the work being done.
    --dry-run Do not actually change any files; just print what would happen.
    --posix Conform to the POSIX standard.

    
    

    -d DIR --directory=DIR Change the working directory to DIR first.
    --reject-format=FORMAT Create 'context' or 'unified' rejects.
    --binary Read and write data in binary mode.
    --read-only=BEHAVIOR How to handle read-only input files: 'ignore' that they
    are read-only, 'warn' (default), or 'fail'.

    
    

    -v --version Output version info.
    --help Output this help.

    
    
    
     
  • 相关阅读:
    Google Style Guides-Shell Style Guide
    支付宝钱包手势password破解实战(root过的手机可直接绕过手势password)
    学习Java JDBC,看这篇就够了
    php学习之道:WSDL具体解释(一)
    Android学习笔记(17):文本框TextView类
    HttpSession的深入分析与研究
    【leetcode】atoi (hard) ★
    【leetcode】Candy(hard) 自己做出来了 但别人的更好
    【leetcode】Substring with Concatenation of All Words (hard) ★
    【leetcode】 Search a 2D Matrix (easy)
  • 原文地址:https://www.cnblogs.com/kuloud/p/3371763.html
Copyright © 2020-2023  润新知