1. 程式人生 > >diff和patch應用於提取差異和打補丁

diff和patch應用於提取差異和打補丁

and HERE link nor function small repeat col info

對開源或第三方代碼進行修改,我們需要將原始代碼和修改部分分別上傳.此時,需要先將修改的部分使用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/>

diff和patch應用於提取差異和打補丁