diff and patch学习笔记

先说一下,diff最常用的一个options组合就是 -Naurp,具体解释可以看笔记。

 

Diff and Patch
     hunk: the differing lines interspersed between the common parts of the files.

     diff options:

     -a : when comparing dirs, if a file does not exist in one dir, treat it like it is empty.(do not ignore nonexist files)
     -E : ignore the difference between TAB and SPACE.
     -b : ignore the difference between one and more that one spaces.
     -w : ignore the whitespaces.
     -B : ignore complete blank lines, which contains only a newline.
     -i : ignore case difference between chars.
     -I REGEX: ignore the lines that matches the 'grep' regex .
     -q | --brief : only reports if files are different. useful when diff dir.

     -r : recursively compare dirs.

     output format:
********
     --normal : only output the hunks. No common parts will be displayed.
               commands:
        LaR : added lines in range R of file2 after L line of file1.
        FcT : replaced lines in range F of file1 with lines in range T of file2.
        RdL : deleted lines in range R of file1 after line L of file2. If you want to change file2 into file1, add lines between range R of file1 after line L of file2.
********
     -C <lines> | --context=<lines> | -c : output the context format. default(-c) is showing 3 lines of context.(patch needs at least 2 lines of context)

     The header of the context format:
    
     *** FROM-FILE FROM-FILE-MODIFICATION-TIME
     --- TO-FILE TO-FILE-MODIFICATION TIME

     The hunks of the output:
   
     ***************
     *** FROM-FILE-LINE-RANGE ****
       FROM-FILE-LINE
       FROM-FILE-LINE...
     --- TO-FILE-LINE-RANGE ----
       TO-FILE-LINE
       TO-FILE-LINE...

     The common parts will begin with two spaces.
`!'
     A line that is part of a group of one or more lines that changed
     between the two files.  There is a corresponding group of lines
     marked with `!' in the part of this hunk for the other file.

`+'
     An "inserted" line in the second file that corresponds to nothing
     in the first file.

`-'
     A "deleted" line in the first file that corresponds to nothing in
     the second file.

*********
     unified format:
     -U <lines> | --unified[=<lines>] | -u : shows the unified format, default lines number is 3.

    
     header of unified format:
     --- FROM-FILE FROM-FILE-MODIFICATION-TIME
     +++ TO-FILE TO-FILE-MODIFICATION-TIME
   
     content of hunks:
     @@ FROM-FILE-RANGE TO-FILE-RANGE @@
      LINE-FROM-EITHER-FILE
      LINE-FROM-EITHER-FILE...

     The meaning of the marks before the lines:(notes that the object to compare with is always the first file.)
`+'
     A line was added here to the first file.

`-'
     A line was removed here from the first file.


     show the heading that the hunks fall in.:
     -F <regex> : specifies the section heading matching the <regex>, which will be added to the output format(only context and unified format).
         The suggested regex of some normal programming languages:
`^[[:alpha:]$_]'
     C, C++, Prolog

`^('
     Lisp

`^@node'
     Texinfo

 

     -p : shows the C function heading, defaults to context format.

     -N : mostly used when comparing dirs. treat the unexisted files as empty files.


******** The mostly uesd options are -Naurp. *************

sdiff : interactively merge two files.
     
     patch: options(notes that patch will automatically find out the file to be applied diff in the patch.)
     -p<num> : strip <num> leading '/'(slash) when applying the changes. if not specified, only consider the part that contains no '/'.
     -d dir : change dir before doing anything.
     -b | --backup : backup the file to be patched instead of replacing it.

     In most case, patch -p<num> <patchfile can complete the task.

 

=============================================

Some tips for patch creator and user:

  1. Before creating the patch, make 2 copies of the file trees. For example, when you have the following file structure:

     top

        / ------- sub1

        |                /-------file.txt *

        |---------sub2

     where file.txt is the file you want to patch. In this case, you should create the same structure for sub1, or you should put the new file in the sub1, like the following:

     top

        / ------- sub1

        |                /-------file.txt *

        |                |------ newfile.txt  **

        |---------sub2

     Then you should be in the top dir and use "diff -Naurp sub1/file.txt sub1/newfile.txt > file.patch" to create the patch.

     If you want to diff two folders, and you want to exclude some files matching some patterns, you could specify the patterns on command line with -x option, i.e., diff -Naurp -x "*debug*" folder1 folder2.

     Or you can write down the patterns in a file, and specify the file as the argument to the -X option, i.e., diff -Naurp -X Ex.txt folder1 folder2.

   2.To use the patch, it's better to put the patch under the position where the creator put it. You can use "patch -p0 <file.patch" to patch the files.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值