天天看點

用diff和patch生成patch并應用

當我們想在原檔案和新檔案之間生成patch,并用這個patch檔案,應用在原檔案上快速完成更新時,可以使用以下步驟:

  • 用diff生成patch
    $ echo "12345\ntesting"> old_file 
    $ echo "125testing\ntesting1234"> new_file
    $ # We have 2 common ways to get the patch
    $ # The first way is :
    $ diff old_file new_file > file_patch
    $ # We can see the patch are generated by lines
    $ cat file_patch
    1c1
    < 12345\ntesting
    ---
    > 125testing\ntesting1234
    $ # The second way is :
    $ diff -c old_file new_file > c_file_patch
    $ # Then we can see the patch are generated by contents
    $ cat c_file_patch
    *** old_file    2018-12-27 21:06:57.922518425 +0800
    --- new_file    2018-12-27 21:07:11.551401751 +0800
    ***************
    *** 1 ***
    ! 12345\ntesting
    --- 1 ----
    ! 125testing\ntesting1234
    $ # We also can get one whole patch for files of the whole directories
    $ mkdir -p new_dir old_dir
    $ echo "123" > new_dir/test_1.txt
    $ echo "789testing" > new_dir/test_2.txt
    $ echo "356" > old_dir/test_1.txt
    $ echo "35678" > old_dir/test_2.txt
    $ diff -c -r old_dir/ new_dir > dir_patch
    $ # let use the output
    $ cat dir_patch
    diff -c -r old_dir/test_1.txt new_dir/test_1.txt
    *** old_dir/test_1.txt  2018-12-27 21:55:38.654094697 +0800
    --- new_dir/test_1.txt  2018-12-27 21:55:18.719294247 +0800
    ***************
    *** 1 ****
    ! 356
    --- 1 ----
    ! 123
    diff -c -r old_dir/test_2.txt new_dir/test_2.txt
    *** old_dir/test_2.txt  2018-12-27 21:55:48.594995187 +0800
    --- new_dir/test_2.txt  2018-12-27 21:55:29.630185028 +0800
    ***************
    *** 1 ****
    ! 35678
    --- 1 ----
    ! 789testing
    diff -c -r old_dir/test_1.txt new_dir/test_1.txt
    *** old_dir/test_1.txt  2018-12-27 21:55:38.654094697 +0800
    --- new_dir/test_1.txt  2018-12-27 21:55:18.719294247 +0800
    ***************
    *** 1 ****
    ! 356
    --- 1 ----
    ! 123
    diff -c -r old_dir/test_2.txt new_dir/test_2.txt
    *** old_dir/test_2.txt  2018-12-27 21:55:48.594995187 +0800
    --- new_dir/test_2.txt  2018-12-27 21:55:29.630185028 +0800
    ***************
    *** 1 ****
    ! 35678
    --- 1 ----
    ! 789testing
               
  • 應用patch改變原來的檔案
    $ # patch single file
    $ patch old_file -i c_file_patch 
    patching file old_file
    $ diff old_file new_file 
    $ # No difference
    $ # patch multiple files for directories
    $ patch -i dir_patch -p0
    patching file old_dir/test_1.txt
    patching file old_dir/test_2.txt
    $ diff -c -r old_dir/ new_dir/
    $ # No difference
    $ # Let us see how the '-p' means here
    $ # Let us prepare one directoy
    $ tree old_dir/
    old_dir/
    |-- sub_1
    |   |-- sub_2
    |   |   `-- sub_4
    |   |       `-- test_4.txt
    |   |-- sub_3
    |   |   `-- test_3.txt
    |   `-- test_2.txt
    `-- test_1.txt
    
    4 directories, 4 files
    $ tree new_dir/
    new_dir/
    |-- sub_1
    |   |-- sub_2
    |   |   |-- sub_4
    |   |   |   `-- test_4.txt
    |   |   `-- test_5.txt
    |   |-- sub_3
    |   |   `-- test_3.txt
    |   `-- test_2.txt
    `-- test_1.txt
    
    4 directories, 5 files
    $ # get the patch
    $ diff -c -r old_dir/ new_dir/
    diff -c -r old_dir/sub_1/sub_2/sub_4/test_4.txt new_dir/sub_1/sub_2/sub_4/test_4.txt
    *** old_dir/sub_1/sub_2/sub_4/test_4.txt        2018-12-27 22:19:20.479215655 +0800
    --- new_dir/sub_1/sub_2/sub_4/test_4.txt        2018-12-27 22:21:53.496338977 +0800
    ***************
    *** 1 ****
    ! 123457878786dddddd
    --- 1 ----
    ! 12d3457878786dddddd
    Only in new_dir/sub_1/sub_2: test_5.txt
    diff -c -r old_dir/sub_1/test_2.txt new_dir/sub_1/test_2.txt
    *** old_dir/sub_1/test_2.txt    2018-12-27 22:18:15.191596826 +0800
    --- new_dir/sub_1/test_2.txt    2018-12-27 22:21:26.359494452 +0800
    ***************
    *** 1 ****
    --- 1,2 ----
      123456
    + helloworld
    diff -c -r old_dir/test_1.txt new_dir/test_1.txt
    *** old_dir/test_1.txt  2018-12-27 22:17:45.537770449 +0800
    --- new_dir/test_1.txt  2018-12-27 22:21:14.283563638 +0800
    ***************
    *** 1 ****
    ! 123
    --- 1 ----
    ! 123dddd
    $ # Let us see without the '-p' parameter
    $ patch -i dir_patch 
    can not find file to patch at input line 4
    Perhaps you should have used the -p or --strip option?
    The text leading up to this was:
    --------------------------
    |diff -c -r old_dir/sub_1/sub_2/sub_4/test_4.txt new_dir/sub_1/sub_2/sub_4/test_4.txt
    |*** old_dir/sub_1/sub_2/sub_4/test_4.txt       2018-12-27 22:19:20.479215655 +0800
    |--- new_dir/sub_1/sub_2/sub_4/test_4.txt       2018-12-27 22:21:53.496338977 +0800
    --------------------------
    File to patch: 
    $ # It shows us to use the '-p' parameter
    $ patch -i dir_patch -p0
    patching file old_dir/sub_1/sub_2/sub_4/test_4.txt
    patching file old_dir/sub_1/test_2.txt
    patching file old_dir/test_1.txt
    $ # Let us confirm the result
    $ diff -c -r old_dir/ new_dir/
    Only in new_dir/sub_1/sub_2: test_5.txt
    $ # We can see the existing files are patched. Patch does not create new files
    $ # Why we use 'p0' here?
    $ # Because we create patches and apply patches under the same path
    $ # If we do not do all thing under the same path, we need to specify the '-p' parameters. Let us see the following example 
    $ diff -r -c hello/old_dir/sub_1/test_2.txt hello/new_dir/sub_1/test_2.txt
    *** hello/old_dir/sub_1/test_2.txt      2018-12-27 22:33:01.610511164 +0800
    --- hello/new_dir/sub_1/test_2.txt      2018-12-27 22:21:26.359494452 +0800
    ***************
    *** 1 ****
    --- 1,2 ----
      123456
    + helloworld
    $ diff -r -c hello/old_dir/sub_1/test_2.txt hello/new_dir/sub_1/test_2.txt > p_dir_patch
    $ cp p_dir_patch hello
    $ cd hello
    $ patch -i p_dir_patch -p1
    patching file old_dir/sub_1/sub_2/sub_4/test_4.txt
    patching file old_dir/sub_1/test_2.txt
    $ # Here we use the 'p1', because we need to omit the first slash 'hello'. We have been on the 'hello' directory
    
     
               

繼續閱讀