diff和patch应用于提取差异和打补丁

对开源或第三方代码进行修改,我们需要将原始代码和修改部分分别上传.此时,需要先将修改的部分使用diff提取出来,记录为patch文件.其他人使用时,下载原始代码和patch文件,通过patch命令恢复为和你修改后同样的代码.

比如,将kernel原始代码的目录记为a,修改后的kernel代码目录记为b.提取差异为kernel.patch文件,可使用命令,

diff -arNu a b > kernel.patch

注意,使用-a选项是为了将二进制文件也作为文本文件处理进行对比.

在原始代码中,需要打上此补丁时,可以使用命令(先进入原始代码目录a中),

cd a; patch -p1 < ../kernel.patch

附上diff命令的说明.

 1 用法:diff [选项]... FILES
 2 Compare FILES line by line.
 3 
 4 Mandatory arguments to long options are mandatory for short options too.
 5       --normal                  output a normal diff (the default)
 6   -q, --brief                   report only when files differ
 7   -s, --report-identical-files  report when two files are the same
 8   -c, -C NUM, --context[=NUM]   output NUM (default 3) lines of copied context
 9   -u, -U NUM, --unified[=NUM]   output NUM (default 3) lines of unified context
10   -e, --ed                      output an ed script
11   -n, --rcs                     output an RCS format diff
12   -y, --side-by-side            output in two columns
13   -W, --width=NUM               output at most NUM (default 130) print columns
14       --left-column             output only the left column of common lines
15       --suppress-common-lines   do not output common lines
16 
17   -p, --show-c-function         show which C function each change is in
18   -F, --show-function-line=RE   show the most recent line matching RE
19       --label LABEL             use LABEL instead of file name
20                                   (can be repeated)
21 
22   -t, --expand-tabs             expand tabs to spaces in output
23   -T, --initial-tab             make tabs line up by prepending a tab
24       --tabsize=NUM             tab stops every NUM (default 8) print columns
25       --suppress-blank-empty    suppress space or tab before empty output lines
26   -l, --paginate                pass output through 'pr' to paginate it
27 
28   -r, --recursive                 recursively compare any subdirectories found
29       --no-dereference            don't follow symbolic links
30   -N, --new-file                  treat absent files as empty
31       --unidirectional-new-file   treat absent first files as empty
32       --ignore-file-name-case     ignore case when comparing file names
33       --no-ignore-file-name-case  consider case when comparing file names
34   -x, --exclude=PAT               exclude files that match PAT
35   -X, --exclude-from=FILE         exclude files that match any pattern in FILE
36   -S, --starting-file=FILE        start with FILE when comparing directories
37       --from-file=FILE1           compare FILE1 to all operands;
38                                     FILE1 can be a directory
39       --to-file=FILE2             compare all operands to FILE2;
40                                     FILE2 can be a directory
41 
42   -i, --ignore-case               ignore case differences in file contents
43   -E, --ignore-tab-expansion      ignore changes due to tab expansion
44   -Z, --ignore-trailing-space     ignore white space at line end
45   -b, --ignore-space-change       ignore changes in the amount of white space
46   -w, --ignore-all-space          ignore all white space
47   -B, --ignore-blank-lines        ignore changes where lines are all blank
48   -I, --ignore-matching-lines=RE  ignore changes where all lines match RE
49 
50   -a, --text                      treat all files as text
51       --strip-trailing-cr         strip trailing carriage return on input
52 
53   -D, --ifdef=NAME                output merged file with '#ifdef NAME' diffs
54       --GTYPE-group-format=GFMT   format GTYPE input groups with GFMT
55       --line-format=LFMT          format all input lines with LFMT
56       --LTYPE-line-format=LFMT    format LTYPE input lines with LFMT
57     These format options provide fine-grained control over the output
58       of diff, generalizing -D/--ifdef.
59     LTYPE is 'old', 'new', or 'unchanged'.  GTYPE is LTYPE or 'changed'.
60     GFMT (only) may contain:
61       %<  lines from FILE1
62       %>  lines from FILE2
63       %=  lines common to FILE1 and FILE2
64       %[-][WIDTH][.[PREC]]{doxX}LETTER  printf-style spec for LETTER
65         LETTERs are as follows for new group, lower case for old group:
66           F  first line number
67           L  last line number
68           N  number of lines = L-F+1
69           E  F-1
70           M  L+1
71       %(A=B?T:E)  if A equals B then T else E
72     LFMT (only) may contain:
73       %L  contents of line
74       %l  contents of line, excluding any trailing newline
75       %[-][WIDTH][.[PREC]]{doxX}n  printf-style spec for input line number
76     Both GFMT and LFMT may contain:
77       %%  %
78       %c'C'  the single character C
79       %c'\OOO'  the character with octal code OOO
80       C    the character C (other characters represent themselves)
81 
82   -d, --minimal            try hard to find a smaller set of changes
83       --horizon-lines=NUM  keep NUM lines of the common prefix and suffix
84       --speed-large-files  assume large files and many scattered small changes
85 
86       --help               display this help and exit
87   -v, --version            output version information and exit
88 
89 FILES are 'FILE1 FILE2' or 'DIR1 DIR2' or 'DIR FILE...' or 'FILE... DIR'.
90 If --from-file or --to-file is given, there are no restrictions on FILE(s).
91 If a FILE is '-', read standard input.
92 如果输入相同,则退出状态为 0;1 表示输入不同;2 表示有错误产生。
93 
94 Report bugs to: bug-diffutils@gnu.org
95 GNU diffutils home page: <http://www.gnu.org/software/diffutils/>
96 General help using GNU software: <http://www.gnu.org/gethelp/>

猜你喜欢

转载自www.cnblogs.com/daemony/p/9569951.html