Linux diff patch

/*****************************************************************************
 *                            Linux diff patch
 * 声明:
 *     经常需要给代码打补丁,但是发现自己不会打补丁,经常看着补丁改代码,效率
 * 那是一个低啊,不会就学学呗,反正patch有-R回退,不怕出错。
 *
 *                                      2015-12-28 深圳 南山平山村 曾剑锋
 ****************************************************************************/

                    \\\\\\-*- 目录 -*-//////
                    |  参考文章:
                    |  一、cat main1.c
                    |  二、cat main2.c
                    |  三、execute diff:
                    |  四、execute patch:
                    |  五、patch back:
                    -----------------------

参考文章:
    1. 用Diff和Patch工具维护源码
        http://www.ibm.com/developerworks/cn/linux/l-diffp/index.html
    2. diff和patch使用指南
        http://www.cnblogs.com/cute/archive/2011/04/29/2033011.html
    3. 补丁格式 diff patch
        http://blog.sina.com.cn/s/blog_51cea4040101atql.html

一、cat main1.c
    #include <stdio.h>
    
    int main( int argc, char **argv ) 
    {
        printf( " zengjf test for diff and patch.\n" );
    }

二、cat main2.c
    #include <stdio.h>
    
    int main( int argc, char **argv ) 
    {
    
    
    
        printf( " zengjf test for diff and patch.\n" );
    }

三、execute diff:
    1. $: diff -Nur main1.c main2.c > main.patch
    2. 在当前目录下生成了main.patch文件:
        --- main1.c    2015-12-28 20:36:49.388152208 +0800
        +++ main2.c    2015-12-28 20:25:17.376180914 +0800
        @@ -2,5 +2,8 @@
         
         int main( int argc, char **argv ) 
         {
        +
        +
        +
             printf( " zengjf test for diff and patch.\n" );
         }

四、execute patch:
    1. 当前目录下有:main1.c main2.c main.patch
    2. cat main1.c
        #include <stdio.h>

        int main( int argc, char **argv ) 
        {
            printf( " zengjf test for diff and patch.\n" );
        }
    3. $: patch -p0 < main.patch                                    <right>
        patching file main1.c
    4. cat main1.c
        #include <stdio.h>

        int main( int argc, char **argv ) 
        {
        
        
        
            printf( " zengjf test for diff and patch.\n" );
        }
    5. $: patch p0 < main.patch                                     <error>
        patching file p0
        Hunk #1 FAILED at 2.
out of hunk FAILED -- saving rejects to file p0.rej
    6. $: patch -p1 < main.patch                                    <error>
        can't find file to patch at input line 3
        Perhaps you used the wrong -p or --strip option?
        The text leading up to this was:
        ----------------------------------
        |--- main1.c     2015-12-28 20:36:49.388152208 +0800
        |--- main2.c     2015-12-28 20:25:17.376180914 +0800
        ----------------------------------
        File to patch: <cursor>
    7. $: patch -p0 main.patch                                      <error>
        ^C

五、patch back:
    1. 当前目录下有:main1.c main2.c main.patch
    2. cat main1.c
        #include <stdio.h>

        int main( int argc, char **argv ) 
        {
        
        
        
            printf( " zengjf test for diff and patch.\n" );
        }
    3. $: patch -p0 -R < main.patch    
        patching file main1.c
    4. cat main1.c
        #include <stdio.h>

        int main( int argc, char **argv ) 
        {
            printf( " zengjf test for diff and patch.\n" );
        }

六、实例分析:
    1. patch 部分内容:
        diff --git a/proguard.flags b/proguard.flags
        index 6d41d17..ffbc39a 100644
        --- a/proguard.flags
        +++ b/proguard.flags
        @@ -11,4 +11,4 @@
         -keep class com.android.settings.MasterClearConfirm
         -keep class com.android.settings.accounts.*
         -keep class com.android.settings.fuelgauge.*
        -
        +-keep class com.android.settings.ethernet.*
    2. 解析:@@ -11,4 +11,4 @@
        1. @@ -表示代表a/proguard.flags文件;
        2. 11代表下面显示的代码是从a/proguard.flags文件的第11行开始;
        3. 4代表a/proguard.flags被操作了;
        4. +代表b/proguard.flags文件
        5. 11代表下面显示的代码是从b/proguard.flags文件的第11行开始;
        6. 4代表b/proguard.flags被操作了;
        7. @@ 代表结束

 

posted on 2015-12-28 21:17  zengjf  阅读(443)  评论(0编辑  收藏  举报

导航