“fatal: corrupt patch at line XX” when staging sin

2019-02-02 20:15发布

问题:

I'm getting the following error when I'm trying to stage a single line or multiple lines using the git gui (right click -> stage lines for commit). It's not the first time it occure to me, and I've found others facing it.However I couldn't find how to solve it.

Did any one ever encountered this problem? is there something I can do (staging all the file is not a real solution)

Update: Here is a file which gives me the following error when I try to stage the deleted line.

@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:src="@drawable/texture"
    android:tileMode="repeat"
-   android:dither="true"
    >
</bitmap>
\ No newline at end of file

Here is the error message:

fatal: corrupt patch at line 14

strangely the following fine doesn't even have 14 line!? note ending file with a new line didn't solve the problem

回答1:

Apparently Git GUI requires that files end with a newline when staging individual lines.

I'm pretty sure that at at least one point in time, staging individual lines was ok even without a newline at the end of the file, but apparently that's no longer possible. Just ran into this problem myself, having newlines at the end of the file fixes it, and removing them causes it.



回答2:

Actually, this often happens when you edit '-' lines.
When you remove '-' and forget to add ' ' (space) instead of it

Or, by mistake, you add two spaces and you use 'tabs' as identation

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

I have saw that some people use --ignore-space-change --ignore-whitespace --whitespace=fix as workaround but this is another thing you must not mix into.

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

UPD

it also possible your editor has option: "Delete spaces at end line" So, when you in your editor save patch:

-Line with space at end <--- NOTICE: Here one space at the end
+Line with no space at end<--- Here no space

Your editor 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 origin file has no line:

-Line with space at end<---

it has:

-Line with space at end <--- 

UPD2

So if in your patch in next line

android:tileMode="repeat"

Your editor remove trainling space. Patch will FAIL



回答3:

Git GUI fails to add/remove lines from the index when the chunk you are working with is too big (i.e. too many successive lines modified).

My workaround : if the changes are not a "wall of pink" (if there are some unmodified lines in the middle of the chunk), go to edit > options and reduce the number of lines in the context of diffs. If this doesn't work, you are screwed ; use an other tool (like the command line for instance).



回答4:

I was getting this error while staging hunks.

The "whitespace" mentioned here reminded me that I have, in my git diff options, "-w", which skips white-space while diffing.

I removed that flag (using git-gui's "Options..."), and was able to stage hunks without the error. I then added it back (because I usually don't want to see whitespace diffs when examining what has changed).

Not sure this will help you but it's rather harmless to try and easy to undo.



回答5:

Inspired by kyl191's answer I came up with a simple solution for this problem:

  1. add newline character to the end of your file

  2. stash all changes

    git stash
    
  3. add newline character to the end of your file once again, so you have a newline character in your current file as well as in your stashed version

  4. stage the corresponding hunk (contains just your newline)

    git stage <your file>
    
  5. pop your changes from stash

    git stash pop
    

Now you should be able to stage single lines via gui without this error. This works because after you staged just the newline containing part, the diff to the version recovered from stash already sees a file wich ends with a newline character.



回答6:

I had this problem because I tried to add a newline to my patch in emacs and it automatically removed the newline at the end again when I saved it.

If you're using emacs to add a newline to the end of your patch, it may also be removing it again. I opened another text editor to add my newline and it worked.



标签: git git-gui