Linux 永久反转补丁文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3902388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Permanently reversing a patch-file
提问by Oliver Charlesworth
Sometimes, for whatever reason, I have to produce patch-files (under Linux) that are in the wrong direction. I know that I can deal with this by using the -R
switch when applying it via patch
, but it would be nice if there were a way of permanently reversing the patch-file. Is there a utility that can do this, or e.g. a regex that would be guaranteed to work?
有时,无论出于何种原因,我必须生成方向错误的补丁文件(在 Linux 下)。我知道我可以通过-R
在通过 应用它时使用开关来处理这个问题patch
,但是如果有一种永久反转补丁文件的方法会很好。是否有可以执行此操作的实用程序,或者例如可以保证正常工作的正则表达式?
UPDATE
更新
Lie Ryan has suggested a neat way of doing this. However, it requires access to the original source file(s). So I suppose I should update my question to state that I'm more after a way of achieving this given only the patch-file itself.
Lie Ryan提出了一种巧妙的方法来做到这一点。但是,它需要访问原始源文件。所以我想我应该更新我的问题,以说明我更想通过补丁文件本身来实现这一目标。
采纳答案by camh
You can use the tool interdiff(1)
from patchutils. In particular, the man page for interdiff
says:
您可以使用该工具interdiff(1)
从patchutils。特别是,手册页interdiff
说:
To reverse a patch, use /dev/null for diff2.
要反转补丁,请对 diff2 使用 /dev/null。
So,
所以,
$ interdiff -q file.patch /dev/null > reversed.patch
The -q / --quiet
prevents the insertion of reverted:
lines.
所述-q / --quiet
防止插入reverted:
线。
回答by Lie Ryan
Try:
尝试:
patch -R file.txt file.patch
diff file.txt.orig file.txt > file.patch.rev
// you can then `rm file.txt.orig file.patch`
EDIT:
编辑:
To reverse a unified diff, you need to change three things:
要反转统一差异,您需要更改三件事:
- the patch header
- the chunk header
- the + to - and - to +
- 补丁头
- 块头
- + 到 - 和 - 到 +
So here's how a patch header for a looks like:
所以这是一个补丁头的样子:
--- b.asm 2010-09-24 12:03:43.000000000 +1000
+++ a.asm 2010-09-24 23:28:43.000000000 +1000
you need to reverse it so it looks like this:
您需要反转它,使其看起来像这样:
--- a.asm 2010-09-24 23:28:43.000000000 +1000
+++ b.asm 2010-09-24 12:03:43.000000000 +1000
basically switch the order, and switch +++ to --- and vice versa.
基本上切换顺序,然后将+++切换到---,反之亦然。
Next, the chunk header:
接下来是块头:
@@ -29,5 +27,7 @@
You need to reverse the numbers, so it look like this:
你需要反转数字,所以它看起来像这样:
@@ -27,7 +29,5 @@
basically, switch the number pairs
基本上,切换数字对
and last, switch every line beginning with + and every line beginning with -.
最后,切换以 + 开头的每一行和以 - 开头的每一行。
EDIT:
编辑:
to switch the chunk header, you can do:
要切换块头,您可以执行以下操作:
sed -e "s/@@ -\([0-9]\+,[0-9]\+\) +\([0-9]\+,[0-9]\+\) @@/@@ - + @@/"
to switch + to - and - to +, you can do:
将 + 切换为 - 并将 - 切换为 +,您可以执行以下操作:
sed -e "s/^+/P/" -e "s/^-/+/" -e "s/^P/-/"
FINALLY:
最后:
to reverse the patch header, do:
要反转补丁标头,请执行以下操作:
head -2 orig.diff | tac | sed -e "s/+++/PPP/" -e "s/---/+++/" -e "s/PPP/---/" > head
tail orig.diff -n+3 > tail
cat head tail > headtail
rm head tail
So, finally, our (quick and dirty) script looks like:
所以,最后,我们的(快速而肮脏的)脚本看起来像:
#!/usr/bin/env sh
F=""
head -2 $F | tac | sed -e "s/+++/PPP/" -e "s/---/+++/" -e "s/PPP/---/" > $F.head
tail $F -n+3 | sed -e "s/@@ -\([0-9]\+,[0-9]\+\) +\([0-9]\+,[0-9]\+\) @@/@@ - + @@/" -e "s/^+/P/" -e "s/^-/+/" -e "s/^P/-/" > $F.tail
cat $F.head $F.tail
rm $F.head $F.tail
I tested it, and it seems to work.
我测试了它,它似乎有效。
though, to make things more maintainable, and cleaner:
不过,为了使事情更易于维护和清洁:
#!/usr/bin/env sh
swap() {
sed -e "s/^/PPP/" -e "s/^//" -e "s/^PPP//"
}
file_header() {
head -2 | tac | swap +++ ---
}
fix_chunk_header() {
sed -e "s/@@ -\([0-9]\+,[0-9]\+\) +\([0-9]\+,[0-9]\+\) @@/@@ - + @@/"
}
fix_lines() {
swap + -
}
file=""
file_header $file
tail $file -n+3 | fix_chunk_header | fix_lines
回答by tinker_fairy
I had applied a patch patch -N -p0 < path/file.patch
but i started facing compilation issues due to incomplete code all i did was to run this command patch -p0 -R < path/file.patch
. Referred this link
我已经应用了一个补丁,patch -N -p0 < path/file.patch
但由于代码不完整,我开始面临编译问题,我所做的只是运行这个命令patch -p0 -R < path/file.patch
。参考了这个链接