188_diff以及patch的简单使用

         这应该是最简单的一个工具的使用了,之所以做一个小结也仅仅是因为我自己本身基础不牢了。前一阵子,简单试了一下diff的使用。其实今天的patch是它的搭档,在过去很长的时间里应该在软甲版本维护中发挥过很大的作用。

         测试过程:

root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# ls
a.c
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# cat a.c
#include "stdio.h"


int main(void)
{
    int a;
    int b;
    int c;

    return 0;
}

         以上是原始的文件信息。

root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# cp a.c b.c
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# vim b.c
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# cat b.c
#include "stdio.h"

int main(void)
{
    int a;
    int b;
    int b;
    int b;
    int b;
    int c;

    return 0;
}

         以上是在原始文件的基础上修改,形成了一个新的文件。

root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# ls
a.c  b.c
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# diff -Naur a.c b.c > c.patch
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# ls
a.c  b.c  c.patch
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# cat c.patch
--- a.c 2019-12-15 14:31:12.533611800 +0800
+++ b.c 2019-12-15 15:56:59.692063900 +0800
@@ -5,6 +5,9 @@
 {
     int a;
     int b;
+    int b;
+    int b;
+    int b;
     int c;

         上面的操作中,通过diff工具生成了一个diff文件。

root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# rm b.c
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# ls
a.c  c.patch
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# cp a.c b.c
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# cat b.c
#include "stdio.h"

int main(void)
{
    int a;
    int b;
    int c;

    return 0;
}

root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# patch b.c < c.patch
patching file b.c
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# cat b.c
#include "stdio.h"

int main(void)
{
    int a;
    int b;
    int b;
    int b;
    int b;
    int c;

    return 0;
}

         以上操作:删除了新的文件之后,根据原始文件以及diff文件生成了新的目标文件。之后的文件内容显示看的出合并成功。

root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# patch -R b.c < c.patch
patching file b.c
root@DESKTOP-SMB53T0:/mnt/d/02_Grey/01_hacking/08_git/001# cat b.c
#include "stdio.h"

int main(void)
{
    int a;
    int b;
    int c;

    return 0;
}

上面的操作,通过目标文件以及diff文件恢复了原始文件。

通过上面的过程,有几点功能性的信息至少我们可以总结一下:

  1. Diff文件是可以看得出变更的具体信息的。
  2. Diff文件,可以作为一个差异化的描述文件,通过这个描述文件可以以差异变化的方式把更改的信息提供给他人;
  3. 由于这个diff文件的存在,其实我们可以实现一个类似于现在的软件版本的差异比较、旧版本恢复等简单的功能。
发布了812 篇原创文章 · 获赞 163 · 访问量 172万+

猜你喜欢

转载自blog.csdn.net/grey_csdn/article/details/103604212