diff and patch学习笔记
2010-08-11 22:23
405 查看
先说一下,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.
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.
相关文章推荐
- linux下diff和patch命令学习笔记
- caffe学习笔记6:Blob,layer and net以及对应配置文件的编写
- WPF and Silverlight 学习笔记(十九):WPF更换主题
- 子空间学习论文笔记02:Laplacian Eigenmaps for Dimensionality Reduction and Data Representation
- [分布式系统学习]阅读笔记 Distributed systems for fun and profit 之一 基本概念
- LUA学习笔记2-Iterators and the Generic for
- Quartz学习笔记(三)—— TriggerListeners and JobListeners
- 【NDN安全】Interest Flooding Attack and Countermeasures in Named Data Networking 学习笔记
- 【VS2010学习笔记】【异常处理】general error c1010070: Failed to load and parse the manifest.
- 高级H5游戏开发(Advanced game design with html5 and javascript )读书学习笔记(1)--es6相关
- linux下的程序补丁工具diff and patch
- 林轩田之机器学习课程笔记(why can machines learn之noise and error)(32之8)
- neural networks and deep learning 学习笔记
- ArcGIS Server Help 之 Geodatabase and ArcSDE 学习笔记
- 步步为营 .NET 代码重构学习笔记 五、分解函数和替换算法(Replace Method And Substitute Algorithm)
- Neural Networks and Deep Learning 学习笔记(七)
- 【CNTK】CNTK学习笔记之图像识别-树叶识别Train and Test
- Android 学习笔记之AndBase框架学习(三) 使用封装好的函数完成Http请求..
- iOS Programming 学习笔记 - 02 Delegate and UITextField
- winpcap学习笔记--(Opening an adapter and capturing the packets)