Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get error message ''fatal: sha1 information is lacking or useless" when apply a patch using "git am -3"

Tags:

git

I am trying to apply a series of patches from 1 git repository to another git repository using git am -3 "path to a patch". I apply them in order, from patch 1-4, it works fine.

But when I come to 5th patch,I get the error saying "fatal: sha1 information is lacking or useless". I go the to git repository where I apply the patch, I do see the file 'dev/afile'. So I wonder why git is complaining about "sha1 information is lacking or useless (dev/afile.c)" and how can I fix my problem?

 $ git am -3 ~/Tmp/mypatches/0005-fifth.patch
Applying: rpmsg: Allow devices to use custom buffer allocator
fatal: sha1 information is lacking or useless (dev/afile.c).
Repository lacks necessary blobs to fall back on 3-way merge.
Cannot fall back to three-way merge.
Patch failed at 0001 first patch
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".

And why it said "Patch failed at 0001 first patch", when I do "git am -3 ~/Tmp/mypatches/0005-fifth.patch", it completes with no error.

Thank you.

like image 375
michael Avatar asked May 15 '13 18:05

michael


3 Answers

Just did the following and was able to solve this issue:

patch -p1 < example.patch
like image 162
Pini Cheyni Avatar answered Nov 07 '22 08:11

Pini Cheyni


The patch file starting with 0001- cannot be applied cleanly - there is some conflict.

Git wanted to resolve that conflict by looking at commits this patch has been based on, but you don't have those commits in your repository.

Probably the patch has been created from a branch that had commits that were never shared, or either your or submitter's branch has been rebased.

It doesn't matter that patch 0005- can be applied with no error. The error is about 0001- specifically.

like image 30
Kornel Avatar answered Sep 29 '22 05:09

Kornel


I had this when trying to apply patches from one repository into one which had unrelated history (the same project but with rebuilt git history). The reason you get the message fatal: sha1 information is lacking or useless (dev/afile.c) is that when git is trying to do a 3 way merge it needs to access the state of that file. Those files are pointed to by the hashes in the format patch output (e.g.)

diff --git a/dev/afile.c b/dev/afile.c
index ebbd50fc0b7..ef1ca87ead0 100644
--- a/dev/afile.c
+++ b/dev/afile.c

ebbd50fc0b7 and ef1ca87ead0 refer to hashes of the content of the files, not commit hashes.

If you try:

git cat-file blob <hash from patch>

Git will report:

fatal: Not a valid object name <hash from patch>

Git can't find them because those versions of the file are not available in your local repository (hence the message Repository lacks necessary blobs to fall back on 3-way merge.). You can make those objects available in your local repository with:

git remote add old_repo <url>
git fetch old_repo

Now, when you run:

git cat-file blob <hash from patch>

You should be given the contents of that file. Now try your git am command again and it should be able to do 3 way merges.

like image 15
Russell Gallop Avatar answered Nov 07 '22 10:11

Russell Gallop