0

I tried to apply a patch of patches (one of which essentially deletes every lines in one file and separately recreates the file with small differences and a different name - it's a rename but more than 50% different) to a file (with git apply --whitespace=fix --reject), but I got this error message:

Checking patch path/to/file/to/patch...
error: while searching for:
<patch contents>
error: patch failed path/to/file/to/patch:1
error: removal patch leaves file contents
error: path/to/file/to/patch patch does not apply

I looked and the contents of the patch were very slightly different to the file being deleted (a line was moved up three, and had 3-4 characters removed), but what does the error message

removal patch leaves file contents

mean? Git knows this is a whole file deletion, but when it applies the patch there're lines left (as in "patch leaves file contents behind")? Or that it somehow goes out of bounds (as in "patch leaves [the] file contents")?

And why can't git figure out what to do? Git uses a similar heuristic to find file renames, I'm surprised there's not a similar process in place. Also using -3 fails to apply all the patches, otherwise I would have had an easier time confirming I found all the changes.

1 Answer 1

1

I looked and the contents of the patch were very slightly different to the file being deleted (a line was moved up three, and had 3-4 characters removed), but what does the error message mean?

The error message means that Git does not know how to apply the patch. Use your own knowledge about how software works to decide how to apply the patch yourself, and then do that.

And why can't git figure out what to do?

Git has instructions that say "delete file X, which looks like this" but the file doesn't look like that. Rather than blindly delete the file anyway, Git errs on the side of caution, saying: Hey, man, I'm just a dumb computer program. The file doesn't look right to me. You're a smart human, you figure it out.

2
  • "Use your own knowledge about how software works to decide how to apply the patch yourself, and then do that." I did, and I have. I just deleted the file 🤷‍♂️ But I wanted to understand: I asked "what does the error message [removal patch leaves file contents] mean", but I realise I wasn't completely clear. Git already does some 'figuring out' when it converts a delete+add as a rename, I was expecting a similar heuristic here - but I take it from your answer there is not. Oct 21, 2022 at 15:37
  • Yes - Git internally doesn't have renames in the first place, it just has delete-and-add, so to rename file F1 to F2 without changing its content, git apply has to delete the old F1 and create a new F2. That's true even if the patch instructions are "rename F1 to F2". As you noted, any time you see "rename" in git diff output, it means Git saw F1-deleted plus F2-created-as-new, and then compared the content of F1 and F2 and found them "similar" (perhaps identical) and called that a rename.
    – torek
    Oct 21, 2022 at 23:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.