{"thread":{"id":"56321","subject":"bugreport: git apply -3 confusing \"lacks the necessary blob\"","startedAt":"2021-08-18T18:24:37Z","lastAt":"2021-08-27T17:14:49Z","messageCount":7,"participants":["Jan Kratochvil","Bagas Sanjaya","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"433075","messageId":"YR1OszUm08BMAE1N@host1.jankratochvil.net","threadId":"56321","inReplyTo":null,"subject":"bugreport: git apply -3 confusing \"lacks the necessary blob\"","fromName":"Jan Kratochvil","fromEmail":"jan@jankratochvil.net","sentAt":"2021-08-18T18:17:23Z","receivedAt":"2021-08-18T18:24:37Z","isPatch":false,"sender":{"key":"jan@jankratochvil.net","avatar":null},"body":"Thank you for filling out a Git bug report!\nPlease answer the following questions to help us understand your issue.\n\nWhat did you do before the bug happened? (Steps to reproduce your issue)\ngit apply -3 b.diff\n\nWhat did you expect to happen? (Expected behavior)\n<<<<<<< ours\nc\n||||||| base\na\n=======\nb\n>>>>>>> theirs\n\nWhat happened instead? (Actual behavior)\nerror: patch failed: x:1\nerror: repository lacks the necessary blob to fall back on 3-way merge.\nerror: x: patch does not apply\n\nWhat's different between what you expected and what actually happened?\nThe conflicting patch has not been applied to resolve it manually.\n\nAnything else you want to add:\nThe problem is 'git apply -3' will print confusing \"lacks the necessary blob\"\nmessage despite the real error message should be:\n\"b.diff is missing line starting with 'index', cannot apply by -3/--3way\"\n\nreproducer:\n(set -ex;: rm -rf gitgit;mkdir gitgit;cd gitgit;git init;echo a >x;git add x;git commit -am.;git checkout -b b;echo b >x;git commit -am.;git checkout master;echo c >x;git commit -am.;git diff master^..b|grep -v ^index >b.diff;git apply -3 b.diff || cat b.diff)\n\nPlease review the rest of the bug report below.\nYou can delete any lines you don't wish to share.\n\n\n[System Info]\ngit version:\ngit version 2.31.1\ncpu: x86_64\nno commit associated with this build\nsizeof-long: 8\nsizeof-size_t: 8\nshell-path: /bin/sh\nuname: Linux 5.12.11-300.fc34.x86_64 #1 SMP Wed Jun 16 15:47:58 UTC 2021 x86_64\ncompiler info: gnuc: 11.0\nlibc info: glibc: 2.33\n$SHELL (typically, interactive shell): /bin/bash\n\n\n[Enabled Hooks]\nnot run from a git repository - no hooks to show\n"},{"id":"433204","messageId":"73a31988-d298-0168-43d5-849f91366d7d@gmail.com","threadId":"56321","inReplyTo":"YR1OszUm08BMAE1N@host1.jankratochvil.net","subject":"Re: bugreport: git apply -3 confusing \"lacks the necessary blob\"","fromName":"Bagas Sanjaya","fromEmail":"bagasdotme@gmail.com","sentAt":"2021-08-20T09:19:39Z","receivedAt":"2021-08-20T09:19:44Z","isPatch":false,"sender":{"key":"bagasdotme@gmail.com","avatar":"https://avatars.githubusercontent.com/u/40219486?v=4"},"body":"On 19/08/21 01.17, Jan Kratochvil wrote:\n> reproducer:\n> (set -ex;: rm -rf gitgit;mkdir gitgit;cd gitgit;git init;echo a >x;git add x;git commit -am.;git checkout -b b;echo b >x;git commit -am.;git checkout master;echo c >x;git commit -am.;git diff master^..b|grep -v ^index >b.diff;git apply -3 b.diff || cat b.diff)\n\nI can reproduce your issue on latest Git (2.33.0).\n\nIt seems like you remove `index` line, which **may** contain blob hash \ninformation required for three-way merge with git apply -3.\n\nBut if you don't remove it when generating patch that way, you will get \nexpected conflict when git applying.\n\n-- \nAn old man doll... just what I always wanted! - Clara\n"},{"id":"433205","messageId":"YR91RNbfPwrTZ/3c@host1.jankratochvil.net","threadId":"56321","inReplyTo":"73a31988-d298-0168-43d5-849f91366d7d@gmail.com","subject":"Re: bugreport: git apply -3 confusing \"lacks the necessary blob\"","fromName":"Jan Kratochvil","fromEmail":"jan@jankratochvil.net","sentAt":"2021-08-20T09:26:28Z","receivedAt":"2021-08-20T09:26:44Z","isPatch":false,"sender":{"key":"jan@jankratochvil.net","avatar":null},"body":"On Fri, 20 Aug 2021 11:19:39 +0200, Bagas Sanjaya wrote:\n> On 19/08/21 01.17, Jan Kratochvil wrote:\n> > reproducer:\n> > (set -ex;: rm -rf gitgit;mkdir gitgit;cd gitgit;git init;echo a >x;git add x;git commit -am.;git checkout -b b;echo b >x;git commit -am.;git checkout master;echo c >x;git commit -am.;git diff master^..b|grep -v ^index >b.diff;git apply -3 b.diff || cat b.diff)\n> \n> I can reproduce your issue on latest Git (2.33.0).\n> \n> It seems like you remove `index` line, which **may** contain blob hash\n> information required for three-way merge with git apply -3.\n> \n> But if you don't remove it when generating patch that way, you will get\n> expected conflict when git applying.\n\nYes.\n\nThe problem is when the 'index' line is already removed (as it was always\nbeing removed in my existing setup) it is difficult to find out that is the\nreason of this confusing message. So the error message should be different\nwhen the 'index' line is missing at all.\n\n\nJan\n"},{"id":"433256","messageId":"xmqqtujkui9v.fsf@gitster.g","threadId":"56321","inReplyTo":"73a31988-d298-0168-43d5-849f91366d7d@gmail.com","subject":"Re: bugreport: git apply -3 confusing \"lacks the necessary blob\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-08-20T17:40:12Z","receivedAt":"2021-08-20T17:40:22Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Bagas Sanjaya <bagasdotme@gmail.com> writes:\n\n> On 19/08/21 01.17, Jan Kratochvil wrote:\n>> reproducer:\n>> (set -ex;: rm -rf gitgit;mkdir gitgit;cd gitgit;git init;echo a >x;git add x;git commit -am.;git checkout -b b;echo b >x;git commit -am.;git checkout master;echo c >x;git commit -am.;git diff master^..b|grep -v ^index >b.diff;git apply -3 b.diff || cat b.diff)\n>\n> I can reproduce your issue on latest Git (2.33.0).\n>\n> It seems like you remove `index` line, which **may** contain blob hash\n> information required for three-way merge with git apply -3.\n>\n> But if you don't remove it when generating patch that way, you will\n> get expected conflict when git applying.\n\nYou make it sound like an \"it hurts when I do this, doctor. ---do\nnot do it, then\" exchange ;-).\n\nBut I think Jan is talking about the case where users get a patch\nthat lacks the \"index\" information out of other people's \"diff\"\nimplementation and try to \"apply -3\" without realizing that it is\nnot Git's \"diff\" output.\n\nPerhaps something like the attached patch would be a good start.\n\nThere is another place that relies on the index line (application of\nbinary patch), but the code rejects any patch that does not have the\nfull-length object name with \"... without full index line\".  This\nmessage was originally meant to reject patches with abbreviated\nobject names on the index line, but it would equally apply to one\nwithout the index line (perhaps by accident), so there is no need to\ntouch that one.\n\n\n apply.c | 2 ++\n 1 file changed, 2 insertions(+)\n\ndiff --git i/apply.c w/apply.c\nindex 44bc31d6eb..9972ada57e 100644\n--- i/apply.c\n+++ w/apply.c\n@@ -3566,6 +3566,8 @@ static int try_threeway(struct apply_state *state,\n \t/* Preimage the patch was prepared for */\n \tif (patch->is_new)\n \t\twrite_object_file(\"\", 0, blob_type, &pre_oid);\n+\telse if (!*patch->old_oid_prefix && !*patch->new_oid_prefix)\n+\t\treturn error(_(\"cannot 'apply -3' a patch without the index line\"));\n \telse if (get_oid(patch->old_oid_prefix, &pre_oid) ||\n \t\t read_blob_object(&buf, &pre_oid, patch->old_mode))\n \t\treturn error(_(\"repository lacks the necessary blob to perform 3-way merge.\"));\n\n"},{"id":"433269","messageId":"YR/03XFO3uA5uCF0@host1.jankratochvil.net","threadId":"56321","inReplyTo":"xmqqtujkui9v.fsf@gitster.g","subject":"Re: bugreport: git apply -3 confusing \"lacks the necessary blob\"","fromName":"Jan Kratochvil","fromEmail":"jan@jankratochvil.net","sentAt":"2021-08-20T18:30:53Z","receivedAt":"2021-08-20T18:31:07Z","isPatch":false,"sender":{"key":"jan@jankratochvil.net","avatar":null},"body":"On Fri, 20 Aug 2021 19:40:12 +0200, Junio C Hamano wrote:\n> But I think Jan is talking about the case where users get a patch\n> that lacks the \"index\" information out of other people's \"diff\"\n> implementation and try to \"apply -3\" without realizing that it is\n> not Git's \"diff\" output.\n\nYes.\n\n\n> Perhaps something like the attached patch would be a good start.\n\nIt does fix the problem for me, thanks!\n\n\nJan\n"},{"id":"433932","messageId":"YSkBg3lBAZ2pxCP2@host1.jankratochvil.net","threadId":"56321","inReplyTo":"xmqqtujkui9v.fsf@gitster.g","subject":"Re: bugreport: git apply -3 confusing \"lacks the necessary blob\"","fromName":"Jan Kratochvil","fromEmail":"jan@jankratochvil.net","sentAt":"2021-08-27T15:15:15Z","receivedAt":"2021-08-27T15:15:34Z","isPatch":false,"sender":{"key":"jan@jankratochvil.net","avatar":null},"body":"On Fri, 20 Aug 2021 19:40:12 +0200, Junio C Hamano wrote:\n> But I think Jan is talking about the case where users get a patch\n> that lacks the \"index\" information out of other people's \"diff\"\n> implementation and try to \"apply -3\" without realizing that it is\n> not Git's \"diff\" output.\n\nFor example from Phabricator:\n\thttps://reviews.llvm.org/file/data/3ceoc32b3yv43vk3nw4q/PHID-FILE-lfeeh2qu4vrngdcwwudo/D107456.diff\n\n\n> Perhaps something like the attached patch would be a good start.\n\nDo you plan to check it in?\n\n\nThanks,\nJan Kratochvil\n\n\n> diff --git i/apply.c w/apply.c\n> index 44bc31d6eb..9972ada57e 100644\n> --- i/apply.c\n> +++ w/apply.c\n> @@ -3566,6 +3566,8 @@ static int try_threeway(struct apply_state *state,\n>  \t/* Preimage the patch was prepared for */\n>  \tif (patch->is_new)\n>  \t\twrite_object_file(\"\", 0, blob_type, &pre_oid);\n> +\telse if (!*patch->old_oid_prefix && !*patch->new_oid_prefix)\n> +\t\treturn error(_(\"cannot 'apply -3' a patch without the index line\"));\n>  \telse if (get_oid(patch->old_oid_prefix, &pre_oid) ||\n>  \t\t read_blob_object(&buf, &pre_oid, patch->old_mode))\n>  \t\treturn error(_(\"repository lacks the necessary blob to perform 3-way merge.\"));\n"},{"id":"433938","messageId":"xmqqwno6ajy1.fsf@gitster.g","threadId":"56321","inReplyTo":"YSkBg3lBAZ2pxCP2@host1.jankratochvil.net","subject":"Re: bugreport: git apply -3 confusing \"lacks the necessary blob\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-08-27T17:14:46Z","receivedAt":"2021-08-27T17:14:49Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jan Kratochvil <jan@jankratochvil.net> writes:\n\n> On Fri, 20 Aug 2021 19:40:12 +0200, Junio C Hamano wrote:\n>> But I think Jan is talking about the case where users get a patch\n>> that lacks the \"index\" information out of other people's \"diff\"\n>> implementation and try to \"apply -3\" without realizing that it is\n>> not Git's \"diff\" output.\n>\n> For example from Phabricator:\n> \thttps://reviews.llvm.org/file/data/3ceoc32b3yv43vk3nw4q/PHID-FILE-lfeeh2qu4vrngdcwwudo/D107456.diff\n>\n>\n>> Perhaps something like the attached patch would be a good start.\n>\n> Do you plan to check it in?\n\nMaybe eventually but not yet.  When I say \"a good start\", it is\nbecause it is known to be insufficient.  I haven't had a chance to\nrevisit the issue.\n\n"}]}