1.创建目录 demo
cd demo
有bug文件a.txt:
当前目录为demo
mkdir -p old/a/p vi old/a/p/foo.txt old_line_1 old_line_2
bug 修复后的foo.txt:
当前目录为demo
mkdir -p new/a/p vi new/a/p/foo.txt new_line_1 new_line_2
补丁产生:
当前目录为demo
LC_ALL=C TZ=UTC0 diff -Naur old new > foo.patch
补丁内容:
当前目录为demo
[root@monitor demo]# cat foo.patch
diff -Naur old/a/b/foo.txt new/a/b/foo.txt --- old/a/b/foo.txt 2016-05-26 06:57:56.999206666 +0000 +++ new/a/b/foo.txt 2016-05-26 06:59:03.160497528 +0000 @@ -1,2 +1,2 @@ -old_line_1 -old_line_2 +new_line_1 +new_line_2
模拟打补丁:(补丁针对 old/a/b.foo.txt文件打补丁)
当前目录demo
[root@monitor demo]# patch -p0 <foo.patch patching file old/a/b/foo.txt
当前目录demo
[root@monitor demo]# mkdir xx
[root@monitor demo]# cp foo.patch xx
[root@monitor demo]# patch -p0 < xx/foo.patch
patching file old/a/b/foo.txt
cd xx
当前目录为demo/xx
[root@monitor xx]# patch -p0 < foo.patch
can't find file to patch at input line 4
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|diff -Naur old/a/b/foo.txt new/a/b/foo.txt
|--- old/a/b/foo.txt 2016-05-26 06:57:56.999206666 +0000
|+++ new/a/b/foo.txt 2016-05-26 06:59:03.160497528 +0000
说明在当前目录不能找到 old/a/b/foo.txt (在demo/xx目录下找到文件与目录去补丁
P0:你当前的目录位置,去找old/a/b/foo.txt
p1:你当前目录位置去找a/b/foo.txt,p1会掉old/,到当前目录找a/b/foo.txt
p2:你当前目录位置去找a/b/foo.txt,p2会掉old/a,到当前目录找b/foo.txt
p3:你当前目录位置去找a/b/foo.txt,p3会掉old/a/b,到当前目录找foo.txt
结论:决定补丁如何补:你的补丁所在的目录(你该把补丁放在那里)
P(N)决定去查找要补丁的文件路径,不同的N,会掉某部分路径后,再在当前目录,找已除掉后路径,找文件去补丁
P(N)与当前目录关系很大,与补丁在那里没有关系
patch -d xx P(N) 〈 XX 可以命令行上指定 工作目录