Git 错误:“致命:第 36 行的补丁损坏”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18142870/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 08:53:14  来源:igfitidea点击:

Git error: "fatal: corrupt patch at line 36"

gitgit-gui

提问by wassup

I have a Java file that ends like this:

我有一个像这样结尾的 Java 文件:

    }
}

And I mistakenly erased the newline at the end some time ago, but it was fine just until today when I got an error message from Git-GUI when commiting

前段时间我错误地删除了末尾的换行符,但是直到今天我在提交时收到来自 Git-GUI 的错误消息时还好

fatal: corrupt patch at line 36

I tried adding the missing newline, but Git seems not to be able to handle it right:

我尝试添加缺少的换行符,但 Git 似乎无法正确处理:

Before adding newline:

添加换行符之前:

     }
 }
\ No newline at end of file

After adding newline:

添加换行符后:

     }
-}
\ No newline at end of file
+}

And it still gives me that error.

它仍然给我那个错误。

I tried reverting changes and adding only the newline without other changes to the file, but it didn't help either.

我尝试恢复更改并仅添加换行符而不对文件进行其他更改,但这也无济于事。

EDIT:Adding two or even three newlines doesn't help too.

编辑:添加两个甚至三个换行符也无济于事。

EDIT2:This error occurs only when commiting lines within the last hunk.

EDIT2:仅在最后一个块中提交行时才会发生此错误。

采纳答案by wassup

Ok, sorry for not checking it thoroughly.

好的,抱歉没有仔细检查。

I tried adding and commiting as I would normally do, but without Git-GUI, using command-line, and it worked.

我尝试像往常一样添加和提交,但没有 Git-GUI,使用命令行,它工作正常。

And so, I'd recommend everyone that has problems with Git-GUI not to do like I did and check it through command-line before posting.

因此,我建议所有对 Git-GUI 有问题的人不要像我那样做,并在发布之前通过命令行检查它。

回答by Eugen Konkov

This is happens when you edit '-' lines.
When you remove '-' and forget to add ' ' (space) instead of it

当您编辑“-”行时会发生这种情况。
当您删除“-”并忘记添加“”(空格)而不是它时

Open your patch and check that all lines you want to leave untouched are started with ' ' (space)

打开您的补丁并检查您要保留的所有行是否以“ ”(空格)开头

UPDATE

更新

It is also possible that your editor has the option: "Delete spaces at the end of line". So, when you save the patch in your editor:

您的编辑器也可能具有以下选项:“删除行尾的空格”。因此,当您在编辑器中保存补丁时:

-Line with space at end <--- NOTICE: Here there is one space at the end
+Line with no space at end<--- Here there's no space

Your editor will remove trailing space and patch become like this:

你的编辑器将删除尾随空格,补丁变成这样:

-Line with space at end<--- Here no space. Patch will FAIL!!!
+Line with no space at end<--- Here no space also

This patch will FAIL because the origin file has no line:

此补丁将失败,因为原始文件没有行:

-Line with space at end<---

instead it has:

相反,它有:

-Line with space at end <--- 

回答by ejwilson

Another potential issue, especially when editing using a regular text editor, is failing to deal with the numbers at the beginning of the hunk, which denote how many lines are in the old code and how many are in the new code, as well as where it starts in each. If the numbers don't match up, you get the fatal: corrupt patch at line xerror.

另一个潜在问题,尤其是在使用常规文本编辑器进行编辑时,无法处理大块开头的数字,这些数字表示旧代码中有多少行,新代码中有多少行,以及在哪里它从每个开始。如果数字不匹配,则会fatal: corrupt patch at line x出现错误。

For example, @@ -32,9 +54,15 @@tells it to find the code to be replaced at line 32 and for the next 9 lines in the original file, but in the edited file to have fifteen lines starting at line 54. If you add or remove any lines, you'll have to edit those numbers as well.

例如,@@ -32,9 +54,15 @@告诉它在原始文件中的第 32 行和接下来的 9 行中找到要替换的代码,但在编辑后的文件中,从第 54 行开始有 15 行。如果添加或删除任何行,您将还必须编辑这些数字。

While I haven't done any real research into it or ever used git gui, it's conceivable that, since lines which don't end in newlines aren't technically lines according to some standards, you'd need to change one or both of those numbers by one to get it to apply correctly.

虽然我没有对它进行任何真正的研究或使用过 git gui,但可以想象的是,由于不以换行符结尾的行在技术上不是根据某些标准的行,您需要更改其中的一个或两个这些数字一个一个,以使其正确应用。

回答by Jan Hudec

commitdoes not do anything with patches. It does not even do anything with their content. The commit only formats the tree and commit objects and adjusts the HEAD and the ref it points to. So it's not commit itself that gives this error.

commit不对补丁做任何事情。它甚至没有对他们的内容做任何事情。提交只格式化树和提交对象,并调整 HEAD 和它指向的引用。因此,导致此错误的不是提交本身。

It is not addeither, because while it hashes the new file content, it operates on the new content and does not care about differences at all.

add两者都不是,因为当它对新文件内容进行哈希处理时,它对新内容进行操作并且根本不关心差异。

The only think that cares about differences is the default pre-commit hook that checks that you are not adding trailing whitespace and few similar problems. You can skip that check by calling git commit --no-verify. But you'd have to have enabled it in the first place and you'd probably know it.

唯一关心差异的想法是默认的预提交钩子,它检查您没有添加尾随空格和一些类似的问题。您可以通过调用 跳过该检查git commit --no-verify。但是您必须首先启用它并且您可能知道它。

回答by Adowrath

I just had a similar problem (probably equal due to the workings of git gui) to this, which may be useful to anyone having it as well.

我刚刚遇到了类似的问题(可能与 git gui 的工作原理相同),这对任何拥有它的人也可能有用。

When patching my pom.xml via git add -e pom.xml, the patch was the following.

通过 修补我的 pom.xml 时git add -e pom.xml,补丁如下。

diff --git a/pom.xml b/pom.xml
index 3dba69a..a9c8ebb 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,26 +1,48 @@
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>adowrath</groupId>
     <artifactId>project-name</artifactId>
     <version>0.0.1</version>
+    <properties>
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+    </properties>
     <build>
         <sourceDirectory>src/main/java</sourceDirectory>
         <testSourceDirectory>src/test/java</testSourceDirectory>
         <plugins>
             <plugin>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <version>3.6.1</version>
                 <configuration>
                     <source>1.8</source>
                     <target>1.8</target>
                 </configuration>
             </plugin>
             <plugin>
+                <groupId>org.jacoco</groupId>
+                <artifactId>jacoco-maven-plugin</artifactId>
+                <version>0.7.9</version>
+                <executions>
+                    <execution>
+                        <goals>
+                            <goal>prepare-agent</goal>
+                        </goals>
+                    </execution>
+                    <execution>
+                        <id>report</id>
+                        <phase>test</phase>
+                        <goals>
+                            <goal>report</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+            <plugin>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>2.16</version>
                 <configuration>
                     <includes>
                         <include>**/Test*.java</include>
                         <include>**/*Test.java</include>
                         <include>**/*Tests.java</include>
@@ -32,9 +54,15 @@
     </build>
     <dependencies>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.12</version>
         </dependency>
+        <dependency>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>2.5.5</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>

And I wanted to remove the last block with the Mockito-dependency. If I just remove the lines itself, it always gives me an error reporting to line 64:

我想删除具有 Mockito 依赖项的最后一个块。如果我只是删除行本身,它总是给我一个错误报告到第 64 行:

fatal: corrupt patch at line 64
fatal: Could not apply '.git/ADD_EDIT.patch'

Line 64 is the last line in the patch file, so the line after<project>.

64号线是在补丁文件的最后一行,太行之后<project>

The solution was to simply remove the whole trunk, so everything from the @@-line downwards, and it worked immediately.

解决方案是简单地移除整个主干,因此从@@ 行开始的所有内容都可以立即生效。

I hope this helps.

我希望这有帮助。

回答by baldguy99

I had this same issue, and finallyfigured out what it was. Some of my lines were indented with tabs instead of spaces. After changing all my indentation to spaces, it worked.

我有同样的问题,终于弄清楚是什么了。我的一些行用制表符而不是空格缩进。将所有缩进更改为空格后,它起作用了。

回答by VonC

And I wanted to remove the last block with the Mockito-dependency.
If I just remove the lines itself, it always gives me an error reporting to line 64:

fatal: corrupt patch at line 64
fatal: Could not apply '.git/ADD_EDIT.patch'

我想删除具有 Mockito 依赖项的最后一个块。
如果我只是删除行本身,它总是给我一个错误报告到第 64 行:

fatal: corrupt patch at line 64
fatal: Could not apply '.git/ADD_EDIT.patch'

You should not see that kind of error now (Q1 2019) with Git 2.21+, since before 2.21, "git add -e" got confused when the change it wants to let the user edit is smaller than the previous change that was left over in a temporary file.

您现在(2019 年第一季度)使用 Git 2.21+ 不应该看到这种错误,因为在 2.21 之前,git add -e当它想让用户编辑的更改小于临时文件中遗留的先前更改时,“ ” 会感到困惑.

add --edit: truncate the patch file

If there is already a .git/ADD_EDIT.patchfile, we fail to truncate it properly, which could result in very funny errors.

Of course, this file should not be left lying around.
But at least in one case, there was a stale copy, larger than the current diff. So the result was a corrupt diff.

Let's just truncate the file when we write it and not worry about it too much.

add --edit: 截断补丁文件

如果已经有一个.git/ADD_EDIT.patch文件,我们无法正确截断它,这可能会导致非常有趣的错误。

当然,这个文件不应该被闲置。
但至少在一种情况下,有一个陈旧的副本,比当前的差异大。所以结果是一个损坏的差异。

让我们在写文件的时候截断文件,不要太担心。