编辑已有的补丁文件

5

在IT技术中,编辑.patch文件时使用编辑器是标准操作吗?

场景

我在Yocto应用程序中使用.patch文件,想要对代码库进行一些小的更改,以便将其移植到我的嵌入式设备上。

以下是其中一个补丁(为了简洁起见删除了一些细节):

From 85987c659762939241e4bdd4223e63eb5997b181 Mon Sep 17 00:00:00 2001

OE ships php5 as php

---
 airmar/airmar.php             | 2 +-
 n2kd/n2kd_monitor             | 2 +-
 send-message/format-message   | 2 +-
 util/list-product-information | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/airmar/airmar.php b/airmar/airmar.php
index ccd4b4d..46ed49d 100755
--- a/airmar/airmar.php
+++ b/airmar/airmar.php
@@ -1,4 +1,4 @@
-#!/usr/bin/php5
+#!/usr/bin/env php
 <?php
 if (!is_array($argv))
 {
diff --git a/n2kd/n2kd_monitor b/n2kd/n2kd_monitor
index f8cfd42..4cb4766 100755
--- a/n2kd/n2kd_monitor
+++ b/n2kd/n2kd_monitor
@@ -233,7 +233,7 @@ for (;;)
         open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
         open STDOUT, '>>', $MONITOR_LOGFILE or die "Can't write to $MONITOR_LOGFILE $!";
         open STDERR, '>&STDOUT' or die "Can't dup stdout: $!";
-        exec 'php5', '/usr/local/bin/n2k.php', '-monitor';
+        exec 'php', '/usr/bin/n2k.php', '-monitor';
       }
       if (!$monitor)
       {
diff --git a/send-message/format-message b/send-message/format-message
index 590a815..2d91185 100755
--- a/send-message/format-message
+++ b/send-message/format-message
@@ -1,4 +1,4 @@
-#!/usr/bin/php5
+#!/usr/bin/env php
 <?
 #
 # Format a particular N2K command
diff --git a/util/list-product-information b/util/list-product-information
index d958ae4..a54a0f2 100755
--- a/util/list-product-information
+++ b/util/list-product-information
@@ -1,4 +1,4 @@
-#!/usr/bin/php5
+#!/usr/bin/env php
 <?php
 #
 # A very limited script engine that sends and receives CAN messages.
-- 
2.17.0

这个补丁程序只是在任何出现php5的地方用env php进行替换。
然而,主代码存储库更改了其中一个文件,之前该文件中的shebang如下所示:
   #!/usr/bin/php5

现在经过几次提交后,它不再起作用。

据我所知,当前的补丁不会起作用,因为它会首先寻找要删除的行号和内容,但由于文件中再也找不到上述提到的shebang了,所以会引发错误。(已经尝试过)

可能的方式

  • 一种清晰的方法是使用更新代码克隆存储库,并向代码添加所需的shebang(env php而不是php5),并使用git format-patch -1获得新补丁以进行恢复操作。

然而,这需要很多努力,当更改的文件超过一定数量时,这个过程似乎变得很繁琐。

是否可以使用编辑器编辑补丁(我十分确定不可以)? 或者有没有一些git功能可以直接修改补丁而不是相应的文件?

1个回答

1

有另一种使用 quilt refresh 的替代方法。简而言之,当使用 quilt 时,你会发现所有的补丁都被复制到了一个名为 patches 的目录下(在 quiltrc 中可以配置)。

名为 series 的文件存储着所有的补丁文件名。当你应用补丁时,

quilt push

or

quilt push -a

你将会因错误而退出。假设你有10个补丁,第3个无法直接应用,因为你已经在存储库中有一些更改。
然后,你可以调用:
quilt push -f

该程序将尝试应用所有可能的位置,并将未能应用的行存储在.rej文件中。例如输出:

Applying patch patches/0001-To-apply.patch
patching file README.md
Hunk #2 FAILED at 51.
1 out of 2 hunks FAILED -- saving rejects to file README.md.rej

我在我的README.md文件中有一些代码块。

现在,您可以通过比较原始文件来检查未能干净应用的更改。在上面的案例中,比较的是README.mdREADME.md.rej之间的差异。您可以解决失败的地方并调用

quilt refresh

刷新后,您在patches中的原始补丁文件将更新相应更改,现在您可以继续使用quilt pushquilt push -a

注意:Yocto默认使用quilt,直到使用PATCHTOOL变量进行更改。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接