Linux patch 命令用于为软件打补丁,使用 diff
命令获取软件的变更。patch 是linux核心升级的方法之一。下面介绍如何制作和应用patch补丁。
安装
yum install -y patch
help
补丁制作命令 diff
diff
可以比较两个文件或文件夹的区别。制作补丁时的一般用法和常见选项为:
diff [选项] 源文件(夹) 目的文件(夹)
- -r 递归,设置后 diff 会将源/目的文件夹的全部文件进行比较,包括子目录中文件
- -N 确保补丁文件正确地处理创建或删除文件的情况
- -u 输出每个修改前后行数,-u 3 :输出修改前后 3 行
- -忽略内容
- -i, –ignore-case ignore case differences in file contents
- -E, –ignore-tab-expansion ignore changes due to tab expansion
- -Z, –ignore-trailing-space ignore white space at line end
- -b, –ignore-space-change ignore changes in the amount of white space
- -w, –ignore-all-space ignore all white space
- -B, –ignore-blank-lines ignore changes where lines are all blank
- -I, –ignore-matching-lines=RE ignore changes where all lines match RE
- –strip-trailing-cr strip trailing carriage return on input
补丁制作命令 git diff
$ git diff > mock.patch
$ cat mock.patch
diff --git a/origin.txt b/origin.txt
index bdf5010..fba3718 100644
--- a/origin.txt # --- 表示旧文件
+++ b/origin.txt # +++ 表示新文件
@@ -1,3 +1,3 @@ # 修改的信息
hello line 1 # 原始文件默认做进一个空格
-this is origin file # - 表示删除的行
+this is new file # +表示新增的行
hello line 2
使用 git format-patch
生成相关 patch,示例如下:
# 最近1次提交
git format-patch HEAD^
# 最近2次提交
git format-patch HEAD^^
# 生成指定(不含)提交之后的所有提交
git format-patch <commit>
# 生成指定(含)提交
git format-patch -1 <commit>
# 生成指定(含)提交之前的n次提交
git format-patch -n <commit>
patch命令
$ patch --help
Usage: patch [OPTION]... [ORIGFILE [PATCHFILE]]
Input options:
-p NUM --strip=NUM Strip NUM leading components from file names.
-F LINES --fuzz LINES Set the fuzz factor to LINES for inexact matching.
-l --ignore-whitespace Ignore white space changes between patch and input.
-c --context Interpret the patch as a context difference.
-e --ed Interpret the patch as an ed script.
-n --normal Interpret the patch as a normal difference.
-u --unified Interpret the patch as a unified difference.
-N --forward Ignore patches that appear to be reversed or already applied.
-R --reverse Assume patches were created with old and new files swapped.
-i PATCHFILE --input=PATCHFILE Read patch from PATCHFILE instead of stdin.
Output options:
-o FILE --output=FILE Output patched files to FILE.
-r FILE --reject-file=FILE Output rejects to FILE.
-D NAME --ifdef=NAME Make merged if-then-else output using NAME.
--merge Merge using conflict markers instead of creating reject files.
-E --remove-empty-files Remove output files that are empty after patching.
-Z --set-utc Set times of patched files, assuming diff uses UTC (GMT).
-T --set-time Likewise, assuming local time.
--quoting-style=WORD output file names using quoting style WORD.
Valid WORDs are: literal, shell, shell-always, c, escape.
Default is taken from QUOTING_STYLE env variable, or 'shell' if unset.
Backup and version control options:
-b --backup Back up the original contents of each file.
--backup-if-mismatch Back up if the patch does not match exactly.
--no-backup-if-mismatch Back up mismatches only if otherwise requested.
-V STYLE --version-control=STYLE Use STYLE version control.
STYLE is either 'simple', 'numbered', or 'existing'.
-B PREFIX --prefix=PREFIX Prepend PREFIX to backup file names.
-Y PREFIX --basename-prefix=PREFIX Prepend PREFIX to backup file basenames.
-z SUFFIX --suffix=SUFFIX Append SUFFIX to backup file names.
-g NUM --get=NUM Get files from RCS etc. if positive; ask if negative.
Miscellaneous options:
-t --batch Ask no questions; skip bad-Prereq patches; assume reversed.
-f --force Like -t, but ignore bad-Prereq patches, and assume unreversed.
-s --quiet --silent Work silently unless an error occurs.
--verbose Output extra information about the work being done.
--dry-run Do not actually change any files; just print what would happen.
--posix Conform to the POSIX standard.
-d DIR --directory=DIR Change the working directory to DIR first.
--reject-format=FORMAT Create 'context' or 'unified' rejects.
--binary Read and write data in binary mode.
--read-only=BEHAVIOR How to handle read-only input files: 'ignore' that they
are read-only, 'warn' (default), or 'fail'.
-x NUM --debug=NUM Set internal debugging flags.
-v --version Output version info.
--help Output this help.
Report bugs to <bug-patch@gnu.org>.
patch 常用参数说明:
patch -pNUM -i <patchfile>
使用示例
# 原始文件 origin.txt,新文件 new.txt
$ cat origin.txt
hello line 1
this is origin file
hello line 2
$ cat new.txt
hello line 1
this is new file
hello line 2
# 制作patch文件
$ diff origin.txt new.txt >> mock.patch
$ cat mock.patch
2c2
< this is origin file
---
> this is new file
# 在 origin.txt 中应用 mock.patch,等同于 patch -p0 origin.txt -i mock.patch
$ patch -p0 origin.txt < mock.patch
patching file origin.txt
# 打 patch 后 origin.txt 内容
$ cat origin.txt
hello line 1
this is new file
hello line 2
# 回滚 mock.patch
$ patch -p0 -R origin.txt < mock.patch
patching file origin.txt
# 回滚后 origin.txt 内容
$ cat origin.txt
hello line 1
this is origin file
hello line 2
注意:若生成 *.rej
文件,表示 patch 应用失败。