永久反转补丁文件

发布于 2024-09-26 17:38:03 字数 397 浏览 0 评论 0原文

有时,无论出于何种原因,我必须生成方向错误的补丁文件(在 Linux 下)。我知道我可以通过 patch 应用它时使用 -R 开关来处理这个问题,但如果有一种方法可以永久逆转补丁,那就太好了 -文件。是否有一个实用程序可以做到这一点,或者例如可以保证工作的正则表达式?

更新

Lie Ryan建议了一种巧妙的方法来做到这一点。但是,它需要访问原始源文件。所以我想我应该更新我的问题以表明我更想要一种仅在补丁文件本身的情况下实现此目的的方法。

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很快妥协 2024-10-03 17:38:03

您可以使用 patchutils 中的工具 interdiff(1)。特别是,interdiff 的手册页显示:

要反转补丁,请使用 /dev/null
差异2。

因此,

$ interdiff -q file.patch /dev/null > reversed.patch

-q / --quiet 可以防止插入 reverted: 行。

You can use the tool interdiff(1) from patchutils. In particular, the man page for interdiff says:

To reverse a patch, use /dev/null for
diff2.

So,

$ interdiff -q file.patch /dev/null > reversed.patch

The -q / --quiet prevents the insertion of reverted: lines.

不…忘初心 2024-10-03 17:38:03

尝试:

patch -R file.txt file.patch
diff file.txt.orig file.txt > file.patch.rev
// you can then `rm file.txt.orig file.patch`

编辑:

要反转统一差异,您需要更改三件事:

  • 补丁头
  • 块头
  • + 到 - 和 - 到 +

如下所示:

--- b.asm   2010-09-24 12:03:43.000000000 +1000    
+++ a.asm   2010-09-24 23:28:43.000000000 +1000

因此,a 的补丁头 需要反转它,所以它看起来像这样:

--- a.asm   2010-09-24 23:28:43.000000000 +1000
+++ b.asm   2010-09-24 12:03:43.000000000 +1000    

基本上切换顺序,并将 +++ 切换到 --- ,反之亦然。

接下来是块头:

@@ -29,5 +27,7 @@

您需要反转数字,所以它看起来像这样:

@@ -27,7 +29,5 @@

基本上,交换数字对

,最后,交换以 + 开头的每一行和以 - 开头的每一行。

编辑:

要切换块头,您可以执行以下操作:

sed -e "s/@@ -\([0-9]\+,[0-9]\+\) +\([0-9]\+,[0-9]\+\) @@/@@ -\2 +\1 @@/"

将+切换为-,将-切换为+,您可以执行:

sed -e "s/^+/P/" -e "s/^-/+/" -e "s/^P/-/"

FINALLY:

反转补丁头,执行以下操作:

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

所以,最后,我们的(快速而肮脏的)脚本看起来像:

#!/usr/bin/env sh
F="$1"
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]\+\) @@/@@ -\2 +\1 @@/" -e "s/^+/P/" -e "s/^-/+/" -e "s/^P/-/" > $F.tail
cat $F.head $F.tail 
rm $F.head $F.tail

我测试了它,它似乎可以工作。

不过,为了让事情更容易维护、更干净:

#!/usr/bin/env sh
swap() {
    sed -e "s/^$1/PPP/" -e "s/^$2/$1/" -e "s/^PPP/$2/"
}
file_header() {
    head -2 $1 | tac | swap +++ ---
}
fix_chunk_header() {
    sed -e "s/@@ -\([0-9]\+,[0-9]\+\) +\([0-9]\+,[0-9]\+\) @@/@@ -\2 +\1 @@/" 
}
fix_lines() {
    swap + -
}
file="$1"
file_header $file
tail $file -n+3 | fix_chunk_header | fix_lines

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]\+\) @@/@@ -\2 +\1 @@/"

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="$1"
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]\+\) @@/@@ -\2 +\1 @@/" -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/^$1/PPP/" -e "s/^$2/$1/" -e "s/^PPP/$2/"
}
file_header() {
    head -2 $1 | tac | swap +++ ---
}
fix_chunk_header() {
    sed -e "s/@@ -\([0-9]\+,[0-9]\+\) +\([0-9]\+,[0-9]\+\) @@/@@ -\2 +\1 @@/" 
}
fix_lines() {
    swap + -
}
file="$1"
file_header $file
tail $file -n+3 | fix_chunk_header | fix_lines
や三分注定 2024-10-03 17:38:03

我已经应用了补丁 patch -N -p0 path/file.patch 但由于代码不完整,我开始面临编译问题,我所做的就是运行此命令 patch -p0 -R patch -p0 -R 路径/file.patch 。引用了此链接

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

弄潮 2024-10-03 17:38:03

我使用以下命令反转/恢复了原始文件: patch -Ri example.patch

附加的 -p0-p1 选项也可能有帮助。

I reversed / restored original file with: patch -Ri sample.patch

Additional -p0 or -p1 options might be of help as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文