{"thread":{"id":"28096","subject":"\"git apply --check\" successes but git am says \"does not match index\"","startedAt":"2011-08-14T09:36:30Z","lastAt":"2011-08-16T19:50:53Z","messageCount":7,"participants":["Zemacsh","Jeff King","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"173489","messageId":"loom.20110814T113311-277@post.gmane.org","threadId":"28096","inReplyTo":null,"subject":"\"git apply --check\" successes but git am says \"does not match index\"","fromName":"Zemacsh","fromEmail":"ruini.xue@gmail.com","sentAt":"2011-08-14T09:36:30Z","receivedAt":"2011-08-14T09:36:30Z","isPatch":false,"sender":{"key":"ruini.xue@gmail.com","avatar":null},"body":"Hi, all, \n\nI met a problem with git am.\n\nBefore applying a mbox patch, \"git apply --check\" reports OK. Then, I run 'git \nam', however, it complains \"does not match index\". Actually, both working tree \nand index are clean. what might be the problem?\n\nIf I run \"git am --abort\" now, and re-turn \"git am\". To my surprise, everything \ngoes well.\n\nBest\n\n-- zemacsh\n"},{"id":"173562","messageId":"20110815232318.GA4699@sigill.intra.peff.net","threadId":"28096","inReplyTo":"loom.20110814T113311-277@post.gmane.org","subject":"Re: \"git apply --check\" successes but git am says \"does not match index\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2011-08-15T23:23:18Z","receivedAt":"2011-08-15T23:23:18Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Sun, Aug 14, 2011 at 09:36:30AM +0000, Zemacsh wrote:\n\n> Before applying a mbox patch, \"git apply --check\" reports OK. Then, I run 'git \n> am', however, it complains \"does not match index\". Actually, both working tree \n> and index are clean. what might be the problem?\n> \n> If I run \"git am --abort\" now, and re-turn \"git am\". To my surprise, everything \n> goes well.\n\nHmm. I don't think this has anything to do with the \"apply --check\". But\nrather the problem is that \"git am\" doesn't ever refresh the index. For\nexample:\n\n  git init repo &&\n  cd repo &&\n  echo one >file && git add . && git commit -m one &&\n  echo two >file && git add . && git commit -m two &&\n  git format-patch -1 --stdout >patch &&\n  git reset --hard HEAD^ &&\n  sleep 1 &&\n  touch file &&\n  git am patch\n\nThis fails with:\n\n  Applying: two\n  error: file: does not match index\n  Patch failed at 0001 two\n\nRunning \"git am --abort\" resets the index, which freshens it, and then a\nfurther \"git am\" works:\n\n  $ git am --abort\n  $ git am patch\n  Applying: two\n\nWe should perhaps call \"update-index --refresh\" at the start to avoid\nthese sorts of false positives. Probably it should happen whenever we\n\"git am --continue\", as well. But for efficiency reasons, not between\neach patch.\n\nI dunno. Do people want to call \"git am\" in a tight loop, where the\nindex refresh would be a problem? I would think they should instead feed\na whole mbox in one go.\n\n-Peff\n"},{"id":"173564","messageId":"7vhb5ijkq0.fsf@alter.siamese.dyndns.org","threadId":"28096","inReplyTo":"20110815232318.GA4699@sigill.intra.peff.net","subject":"Re: \"git apply --check\" successes but git am says \"does not match index\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-08-15T23:52:55Z","receivedAt":"2011-08-15T23:52:55Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> We should perhaps call \"update-index --refresh\" at the start to avoid\n> these sorts of false positives. Probably it should happen whenever we\n> \"git am --continue\", as well. But for efficiency reasons, not between\n> each patch.\n>\n> I dunno. Do people want to call \"git am\" in a tight loop, where the\n> index refresh would be a problem? I would think they should instead feed\n> a whole mbox in one go.\n\nI am kind of surprised that we have not done the 'refresh once upfront'\nalready and nobody ever run into this for the past 5 years. It seems that\nI inherited that behaviour from git-applymbox ;-)\n\nIt is sensible to refresh once at the beginning and also when restarting\nwith \"am --resolved\".\n"},{"id":"173565","messageId":"20110816001306.GA23695@sigill.intra.peff.net","threadId":"28096","inReplyTo":"7vhb5ijkq0.fsf@alter.siamese.dyndns.org","subject":"Re: \"git apply --check\" successes but git am says \"does not match index\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2011-08-16T00:13:07Z","receivedAt":"2011-08-16T00:13:07Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Aug 15, 2011 at 04:52:55PM -0700, Junio C Hamano wrote:\n\n> I am kind of surprised that we have not done the 'refresh once upfront'\n> already and nobody ever run into this for the past 5 years. It seems that\n> I inherited that behaviour from git-applymbox ;-)\n\nIt's a pretty rare set of circumstances:\n\n  1. You make a file stat-dirty, but don't actually change its contents.\n\n  2. You don't run any index-refreshing porcelains.\n\n  3. You apply a patch that touches that file.\n\n> It is sensible to refresh once at the beginning and also when restarting\n> with \"am --resolved\".\n\nThe patch below does this. I think this makes the \"update-index\" call in\ngit-rebase.sh:522 redundant when the \"am\" backend is used. But it is\nstill needed for the other backends. I wonder if \"git rebase\" actually\nsuffers from the same problem, since it seems to refresh only on\n--continue, but not at the beginning.\n\n-- >8 --\nSubject: [PATCH] am: refresh the index at start and --resolved\n\nIf a file is unchanged but stat-dirty, we may erroneously\nfail to apply patches, thinking that they conflict with a\ndirty working tree.\n\nThis patch adds a call to \"update-index --refresh\". It comes\nas late as possible, so that we don't bother with it for\nthinks like \"git rebase --abort\", or when mbox-splitting\nfails. However, it does come before we actually start\napplying patches, meaning we will only call it once when we\nstart applying patches (or any time we return to \"am\" after\nhaving resolved conflicts), and not once per patch.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n git-am.sh |    2 ++\n 1 files changed, 2 insertions(+), 0 deletions(-)\n\ndiff --git a/git-am.sh b/git-am.sh\nindex 463c741..6592424 100755\n--- a/git-am.sh\n+++ b/git-am.sh\n@@ -511,6 +511,8 @@ else\n \tfi\n fi\n \n+git update-index -q --refresh\n+\n case \"$resolved\" in\n '')\n \tcase \"$HAS_HEAD\" in\n-- \n1.7.6.10.g62f04\n"},{"id":"173570","messageId":"7vd3g6j8sc.fsf@alter.siamese.dyndns.org","threadId":"28096","inReplyTo":"20110816001306.GA23695@sigill.intra.peff.net","subject":"Re: \"git apply --check\" successes but git am says \"does not match index\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-08-16T04:10:43Z","receivedAt":"2011-08-16T04:10:43Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> ... I wonder if \"git rebase\" actually\n> suffers from the same problem,...\n\nDoesn't it require a spiffy clean work tree before even starting?\n"},{"id":"173571","messageId":"20110816041434.GA5141@sigill.intra.peff.net","threadId":"28096","inReplyTo":"7vd3g6j8sc.fsf@alter.siamese.dyndns.org","subject":"Re: \"git apply --check\" successes but git am says \"does not match index\"","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2011-08-16T04:14:34Z","receivedAt":"2011-08-16T04:14:34Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Aug 15, 2011 at 09:10:43PM -0700, Junio C Hamano wrote:\n\n> Jeff King <peff@peff.net> writes:\n> \n> > ... I wonder if \"git rebase\" actually\n> > suffers from the same problem,...\n> \n> Doesn't it require a spiffy clean work tree before even starting?\n\nAh, you're right. I was confused that the continue case called\nupdate-index, but the regular case did not. But it calls\nrequire_clean_work_tree, which refreshes the index (in addition to\nchecking the dirty state, of course). So it's fine.\n\n-Peff\n"},{"id":"173634","messageId":"7vty9hgmoy.fsf@alter.siamese.dyndns.org","threadId":"28096","inReplyTo":"20110816001306.GA23695@sigill.intra.peff.net","subject":"Re: \"git apply --check\" successes but git am says \"does not match index\"","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2011-08-16T19:50:53Z","receivedAt":"2011-08-16T19:50:53Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> Subject: [PATCH] am: refresh the index at start and --resolved\n\nThanks; applied.\n"}]}