patch
一. patch的使用
示例:
ygao@pts/ttys003 $ patch -p1 < ~/Desktop/phase2.diff
patching file src/features/com/abc/service/sns/PostAsyncProcessor.java
patching file src/features/com/abc/service/sns/PostEngine.java
patching file src/features/com/abc/service/sns/PostEngineAdapter.java
patching file src/features/com/abc/service/sns/SNSUtil.java
Hunk #7 FAILED at 335.
Hunk #8 succeeded at 363 (offset -2 lines).
Hunk #9 succeeded at 384 (offset -2 lines).
Hunk #10 succeeded at 453 (offset -2 lines).
Hunk #11 succeeded at 465 (offset -2 lines).
Hunk #12 succeeded at 495 (offset -2 lines).
1 out of 12 hunks FAILED -- saving rejects to file src/features/com/abc/service/sns/SNSUtil.java.rej
patching file src/features/com/abc/service/sns/impl/QQServiceFactory.java
patching file src/features/com/abc/service/sns/impl/SINAServiceFactory.java
patching file src/features/com/abc/service/sns/impl/sina/MemberEngineTemplate.java
Hunk #2 succeeded at 83 (offset 2 lines).
Hunk #3 succeeded at 94 (offset 3 lines).
patching file src/tr/com/abc/queries/abcSNSSocialGraphQueries.sql
patching file src/tr/com/abc/service/sns/BaseDTO.java
patching file src/tr/com/abc/service/sns/IPostSignature.java
patching file src/tr/com/abc/service/sns/InvitationDTO.java
patching file src/tr/com/abc/service/sns/QuickRatingDTO.java
patching file src/tr/com/abc/service/sns/RegistrationDTO.java
patching file src/tr/com/abc/service/sns/ReviewDTO.java
patching file src/tr/com/abc/service/sns/WantToGoDTO.java
patching file src/tr/com/abc/service/sns/object/member/SINAProfile.java
patching file src/tr/com/abc/service/sns/object/member/SNSProfile.java
patching file src/tr/com/abc/service/sns/object/post/QQPost.java
打patch注意:
1.只有当前的代码与打patch时的代码一样,该patch才能打上去。
ygao@pts/ttys003 $ vi src/features/com/abc/service/sns/SNSUtil.java.rej
***************
*** 323,333 **** //下面是与现在的代码相比, 之前的代码.
User user = null;
try
{
- user = users.showUserById(credential.getUid());
- }
- catch (Exception e)
- {
- e.printStackTrace();
}
finally
{
--- 335,341 ---- //要打的代码
User user = null;
try
{
+ user = users.showUserById(credential.getUid());
}
finally
{
2.patch的时候,会自动错行匹配,揭示象Hunk #2 succeeded at 83 (offset 2 lines)都是patch成功的。 同时生成一个后缀为.orig的文件。这个文件默认应该是git add了, 所以此时git diff head也会把这个文件patch进去,但这并不需要的。解决的办法是git checkout path/***.orig文件即可。
此时git status会发现该文件已经不存在了。然后再做git diff head打patch。
3.copy片段打patch的方法.
3.1[root@apac-merge abc-site-PRERELEASE]# patch -p1
3.2 直接粘贴patch片段
diff --git a/tests/findbugs/filter.xml b/tests/findbugs/filter.xml
index c9d0c2d..13933fc 100644
--- a/tests/findbugs/filter.xml
+++ b/tests/findbugs/filter.xml
@@ -56,6 +56,10 @@
<Class name="com.abc.availability.elong.ELongAvailabilityFetcher"/>
</Match>
+ <Match>
+ <Bug pattern="SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING"/>
+ <Class name="com.abc.service.sns.db.SNSSocialGraphDB"/>
+ </Match>
<!-- This is a copy of TA's class with local mod. Ignore all. -->
<Match>
<Class name="com.abc.dbsync.SlaveDB1" />
3.3粘贴完后回车,连续按二次CTRL+D会将patch打上去
显示patching file tests/findbugs/filter.xml
二. patch命令参数:
~/main-0.1
~/main-0.2
~/main_0.1_to_0.2.patch
[root@www main-0.1]# vim ~/main_0.1_to_0.2.patch
diff -Naur main-0.1/cos_value.c main-0.2/cos_value.c
--- main-0.1/cos_value.c 2009-06-09 22:52:33.000000000 +0800
+++ main-0.2/cos_value.c 2009-06-12 00:45:10.000000000 +0800
@@ -6,5 +6,5 @@
{
float value; ....(底下省略)....
1.patch文件里的路径。
通过查看patch,发现补丁文件是在~下通过diff main-0.1/cos_value.c main-0.2/cos_value.c > main_0.1_to_0.2.patch生成的.
注意,diff有二种,一种是直接diff file1 file2 ,一种是diff dir1/file1 dir2/file2, 不同的diff会在patch文件中生成不的路径,比如“diff -Naur main-0.1/cos_value.c main-0.2/cos_value.c ”
2. 打patch
由于patch会在”diff -Naur main-0.1/cos_value.c main-0.2/cos_value.c “的路径上通过patch -pN做减法来定位要更新的文件, patch -p1的结果是由main-0.1/cos_value.c变成了cos_value.c。cos_value.c就是当前要更新的文件。
所以正确的打包命令:
[root@www main-0.1]# patch -p1 < ../main_0.1_to_0.2.patch
patching file cos_value.c
patching file main.c
patching file Makefile
patching file sin_value.c
3.关于patch -pN
假如存在路径 : *** /home/guest/example/expatch.old
那么当我下达『 patch -p0 < patch_file 』时,则更新的文件是 『 /home/guest/example/expatch.old 』,如果『 patch -p1 < patch_file』,则更新的文件为 『home/guest/example/expatch.old』,如果『patch -p4 < patch_file』则更新『expatch.old』, 也就是说, -pxx 那个 xx 代表『拿掉几个斜线(/)。
4.-R 是一个回退操作,可以用来恢复打包之前的文件。
patch -p1 < patch_provider.diff
->
patch -R -p1 < patch_provider.diff
5.真实的示例:
需要更新的文件位于$PROJECT/langs/Messages_zh_CN.properties
patch文件:
diff --git a/langs/Messages_zh_CN.properties b/langs/Messages_zh_CN.properties
index af050dc..0535da6 100644
--- a/langs/Messages_zh_CN.properties
+++ b/langs/Messages_zh_CN.properties
.....
进入$PROJECT目录后,执行patch -p1 < patch_file