是什么导致git-apply"腐败补丁?"

Mik*_*son 6 git patch

(很长的故事 ...)

我正在将一个巨大的PHP应用程序带到本世纪...... ;-) ......而其他团队正在继续维护它的现有版本.

到目前为止,这导致约275个补丁.麻烦的是,我们所做的一项改变是将<?代码转换为代码<?php,并在整个代码中进行了一些类似的更改.当然,所有这些都阻止了应用补丁,因为(确实......)源代码匹配.

所以,我想写一个小脚本来编辑补丁文件:更改补丁中的标签.

但是,我得到的是corrupt patch.

所以,我想知道的是:是什么导致了这个消息?也就是说,当它出现这个消息时,Git正在寻找什么样的错误?我需要"调整我的调整器"... ...以便编辑的补丁工作. (注意原始的补丁文件,在我调整之前,并没有"腐败",所以它一定是我正在做的事情.)

我的脚本试图改变上述PHP标签<?php echo,以及一个功能名称.只不过是全球的preg-replace.对于Git来说,我不容易看到我可能会说的是"结构性问题".但是,显然,有些事情是.

示例补丁:corrupt patch at line 37...

From 342c5939da8cf4cbe495be7d635cd627bd2a44ed Mon Sep 17 00:00:00 2001
From: xxx <xxx@yyy.com>
Date: Wed, 17 Feb 2016 03:45:31 +0000
Subject: [PATCH 001/275] Make it all work 


---
 catalog/includes/modules/shipping/upsFreeGround.php | 2 +-
 catalog/product_info_v3.php                         | 6 +++---
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/catalog/includes/modules/shipping/upsFreeGround.php b/catalog/includes/modules/shipping/upsFreeGround.php
index 45a6da4..55ccecb 100755
--- a/catalog/includes/modules/shipping/upsFreeGround.php
+++ b/catalog/includes/modules/shipping/upsFreeGround.php
@@ -194,7 +194,7 @@ function quote($method = '') {

         // Can probably combine this with the above, eventually
         $allFreeBW2016Plaques = false;
-           if (STORES_ID == 10) {
+           if ((STORES_ID == 10) || (STORES_ID == 26)) {
             $allFreeBW2016Plaques = true;
             foreach ($order->products as $aProduct) {
                    $thisNote = $aProduct['product_specific_notes'];
diff --git a/catalog/product_info_v3.php b/catalog/product_info_v3.php
index 09d88de..10d9b76 100644
--- a/catalog/product_info_v3.php
+++ b/catalog/product_info_v3.php
@@ -186,7 +186,7 @@ function doRequestComplete() {
        }
    }
 <?php -if ((STORES_ID == 10) && (in_array($products_id, $bwFreePlaqueIDList2016))) { ?>
+if (((STORES_ID == 10) || (STORES_ID == 26)) && (in_array($products_id, $bwFreePlaqueIDList2016))) { ?>
 function doCheckBW2016PlaqueProperty() {
    thePropertyNumber = document.getElementById('propertyToCheck');
    if (thePropertyNumber.value == "") {
@@ -1426,7 +1426,7 @@ if($combo_count>0) { ?>
                        ?>
                        </div> <!-- div_add_to_cart -->
            </div> <!-- cart_info_row2 -->
-           <?php if ((STORES_ID == 10) && (in_array( $products_id, $bwFreePlaqueIDList2016))) {
+           <?php if (((STORES_ID == 10) || (STORES_ID == 26)) && (in_array( $products_id, $bwFreePlaqueIDList2016))) {
                // First, let's see if we are "sold out"
                $query = "select bw_plaque_2016_id from bw_plaque_2016 where first_one_free='1' limit 1";
                $bwpRes = tep_db_query( $query);
@@ -1629,7 +1629,7 @@ DIVCONTAINER;
 </table> <!--pageTable for sure -->

 <script type='text/javascript'>
-   <?php if ((STORES_ID == 10) && (in_array( $products_id, $bwFreePlaqueIDList2016))) { ?>
+   <?php if (((STORES_ID == 10) || (STORES_ID == 26)) && (in_array( $products_id, $bwFreePlaqueIDList2016))) { ?>
    function doFreePlaquePriceChange() {
        // Change the quantity to 1
        $('quantityToAdd').setValue('1');
-- 
2.6.4 (Apple Git-63)
Run Code Online (Sandbox Code Playgroud)

Edw*_*son 9

tl; dr:我怀疑你在转换仅包含的线时以某种方式移除了一条线<?.

您的脚本已删除修补程序中的重要行,或者您已更改标题.差异的第一个大块catalog/product_info_v3.php是畸形的.

它的标题是:

@@ -186,7 +186,7 @@ function doRequestComplete() {
Run Code Online (Sandbox Code Playgroud)

其中表示有关如何将hunk对应于preimage(原始文件)和postimage(通过应用此补丁生成)的信息.原像信息由前缀-,并且-186,7,表明该大块包含来自原像7行,在线路186开始该postimage信息由前缀+,并且是+186,7,表明这大块将发出7行到postimage,从第186行开始.

这7行可以包括上下文(它们是共同的行,以空格为前缀),仅存在于preimage中的行(以a为前缀-),或仅存在于postimage中的行(以a为前缀+).

查看大块然后用其类型标记每一行:

  context:         }
  context:     }
  context:  <?php -if ((STORES_ID == 10) && (in_array($products_id, $bwFreePlaqueIDList2016))) { ?>
postimage: +if (((STORES_ID == 10) || (STORES_ID == 26)) && (in_array($products_id, $bwFreePlaqueIDList2016))) { ?>
  context:  function doCheckBW2016PlaqueProperty() {
  context:     thePropertyNumber = document.getElementById('propertyToCheck');
  context:     if (thePropertyNumber.value == "") {
Run Code Online (Sandbox Code Playgroud)

这些context线条同样将在原像和后像中.所以原像有6行,后像有7行.

但你的标题说,原始图像有7行!因此标题错误或指令错误.(并且Git期望在第37行看到另一行的原像,但它是一个新的标题行,所以Git确定你的补丁文件已损坏.)

此修补程序中的行表示您if要将行添加到posimage中,而pretimage中不存在该行.如果这是正确的,那么你已经破坏了标题,它应该-186,6 +186,7表明有一条线被添加到postimage.

或者,如果您正在更改if行,则您已省略其原像状态,并且您应该在postimage行上方有一条线.

仔细观察,看起来你实际上错过了上面一行之后的换行符<?php.您的上下文可能不是<?php -if ...因为我怀疑它不是有效的PHP,前面有一个连字符if.

怀疑这个大块应该是这样的:

@@ -186,7 +186,7 @@ function doRequestComplete() {
       }
   }
 <?php
-if ((STORES_ID == 10) && (in_array($products_id, $bwFreePlaqueIDList2016))) { ?>
+if (((STORES_ID == 10) || (STORES_ID == 26)) && (in_array($products_id, $bwFreePlaqueIDList2016))) { ?>
 function doCheckBW2016PlaqueProperty() {
    thePropertyNumber = document.getElementById('propertyToCheck');
    if (thePropertyNumber.value == "") {
Run Code Online (Sandbox Code Playgroud)

哪个没有腐败,现在有7行preimage(六个上下文行,加上仅存在于前缀中的前缀的行-)和7行postimage(六行上下文行,加上仅有的行)存在于前缀中的前缀+.)

所以现在我们有一个合法的补丁,因为指令与标题匹配.


Luc*_*Luc 7

当我尝试粘贴 ( Ctrl+C-> Ctrl+V) 补丁时出现此错误:

$ git patch
[Ctrl+Shift+V to paste in terminal]
[Ctrl+D to end input]
Run Code Online (Sandbox Code Playgroud)

它会告诉我最后一行的补丁已损坏。

解决方案是Enter在使用之前添加一个Ctrl+D来结束输入。它缺少最后的空白行。