patch and diff

本文深入探讨了Patch工具的使用方法和优势,包括如何通过Patch仅分发软件版本间的差异,节省资源,以及如何逆向应用Patch恢复原始文件。此外,还介绍了如何使用Diff命令创建差异列表,以及在软件修补过程中使用Patch的高级技巧。

When programs are distributed, it’s almost inevitable that users will discover bugs or that the author
will want to issue enhancements and updates. When authors distribute programs as binaries, they often simply ship new binaries. Sometimes (all too often), vendors simply release a new version of the program, often with an obscure revision reference and little information about what has changed.
On the other hand, distributing your software as source code is an excellent idea because it allows people to see how you have implemented things and how you have used features. It also allows people to check exactly what programs are doing and to reuse parts of the source code (providing they comply with the licensing agreement).
However, with the source of the Linux kernel weighing in at tens of megabytes of compressed source code, shipping an updated set of kernel sources would involve considerable resources, when, in fact, probably only a small percentage of the source has changed between each release.
Fortunately, there is a utility program for solving this problem: patch. It was written by Larry Wall, who also wrote the Perl programming language. The patch command allows you to distribute just the differ- ences between the two versions so that anyone with version 1 of a file and a difference file for version 1 to version 2 can use the patch command to generate version 2 for themselves.

If you start with version 1 of a file,

This is file one
line 2
line 3
line 5 
line 6

and then produce version 2,

This is file two
line 2
line 3
line 4 
line 5 
line 6
line 7
       

you can create a difference listing with the diff command:

diff -u file1.txt file2.txt > diffs

The diffs file contains

--- file.txt	2020-01-01 20:00:15.000000000 +0800
+++ file2.txt	2020-01-01 20:01:27.000000000 +0800
@@ -1,5 +1,7 @@
-This is file one
+This is file two
 line 2
 line 3
+line 4 
 line 5 
 line 6
+line 7

This is actually a set of editor commands for changing one file into another. Suppose you have file1.c and the diffs file. You can update your file using patch as follows:

patch file1.txt diffs

The patch command has now changed file1.c to be the same as file2.c.
patch has an extra trick: the ability to unpatch. Suppose you decide you don’t like the changes and want
your original file1.c back. No problem; just use patch again, using the -R (reverse patch) option:

patch -R file1.txt diffs

file1.c is returned to its original state.
The patch command has several other options, but is generally very good at deciding from its input what you’re trying to do and then simply “doing the right thing.” If patch ever fails, it creates a file with the .rej extension containing the parts that couldn’t be patched.
When you’re dealing with software patches, it’s a good idea to use the diff -c option, which produces a “context diff.” This provides a number of lines before and after each change so that patch can verify that the context matches before applying the patch. The patch is also easier to read.
If you find and fix a bug in a program, it’s easier, more precise, and more polite to send the author a patch rather than just a description of the fix.

在处理 `patch` 和 `diff` 文件时,筛选内容的关键在于理解这些文件的格式和用途,并使用适当的工具或方法提取所需信息。以下是几种常见的筛选方法,结合了工具的功能和实际应用场景。 ### 1. 使用 `grep` 提取特定行 `patch` 和 `diff` 文件通常以特定的符号标记修改内容,例如 `+` 表示新增行,`-` 表示删除行。可以使用 `grep` 提取这些标记的行: ```bash # 提取所有新增行 grep '^+' file.patch # 提取所有删除行 grep '^-' file.diff ``` 这种方法适用于快速查看文件中新增或删除的内容。 ### 2. 使用 `diff` 命令筛选差异部分 如果需要筛选两个文件之间的特定差异,可以通过 `diff` 命令结合选项实现。例如,使用 `-y` 选项并列显示差异: ```bash diff -y file1.txt file2.txt ``` 还可以使用 `-u` 选项生成统一格式的 `diff` 输出,便于后续处理: ```bash diff -u file1.txt file2.txt > diff_output.patch ``` ### 3. 使用脚本提取 `git` 生成的 `patch` 内容 在 `Git` 中生成的 `patch` 文件通常包含多个文件的修改内容。可以通过编写脚本筛选特定文件的修改内容。例如,以下脚本提取某个文件的修改部分: ```bash #!/bin/bash PATCH_FILE="change.patch" TARGET_FILE="path/to/target_file.txt" # 提取指定文件的 patch 内容 sed -n "/^diff --git a\/$TARGET_FILE/,/^diff --git/p" "$PATCH_FILE" | sed '$d' ``` 此脚本通过 `sed` 提取 `patch` 文件中特定文件的修改内容。 ### 4. 使用 `awk` 提取 `diff` 文件的上下文 `diff` 文件通常包含上下文信息(例如 `@@ -start,length +start,length @@`)。可以使用 `awk` 提取特定上下文的修改内容: ```bash awk '/^@@/ {flag=1} flag && /^+/ {print} /^diff/ {flag=0}' file.diff ``` 此命令提取所有新增行,并结合上下文标记进行筛选。 ### 5. 使用 `patch` 工具分析 `patch` 文件 `patch` 工具本身可以解析 `patch` 文件的结构。通过结合 `patch` 和其他工具(如 `awk` 或 `sed`),可以提取特定部分的修改内容: ```bash patch --dry-run -p1 < file.patch | grep '^+' ``` 此命令模拟应用 `patch` 文件,并提取所有新增行。 ### 6. 使用 `Python` 脚本解析 `diff` 和 `patch` 文件 对于更复杂的筛选需求,可以使用 `Python` 脚本解析 `diff` 和 `patch` 文件的格式。例如,以下脚本提取所有新增行: ```python import sys def extract_additions(patch_file): with open(patch_file, 'r') as f: for line in f: if line.startswith('+') and not line.startswith('+++'): print(line.strip()) if __name__ == "__main__": extract_additions(sys.argv[1]) ``` 运行脚本时传入 `patch` 或 `diff` 文件路径: ```bash python extract_additions.py file.patch ``` ### 7. 使用 `git` 提取特定提交的 `patch` 内容 如果 `patch` 文件是通过 `Git` 生成的,可以使用 `git` 命令提取特定提交的修改内容: ```bash git show <commit-hash> -- path/to/file.txt ``` 此命令提取指定提交中某个文件的修改内容。 ### 8. 使用 `vim` 或 `diff` 工具查看 `patch` 和 `diff` 文件 `vim` 支持直接查看 `patch` 文件,并通过插件(如 `vim-diff-enhanced`)增强可读性。此外,可以使用 `diff` 工具(如 `meld` 或 `kdiff3`)图形化查看和筛选内容。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值