git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2] apply: allow "new file" patches on i-t-a entries

From
Raymond E. Pasco <ray@ameretat.dev>
Date
Aug 5, 2020, 00:32 UTC
Message-ID
<C4ON23BIKMVK.2ZESQJ1FB5PVA@ziyou.local>
In-Reply-To
<xmqqeeomq8dr.fsf@gitster.c.googlers.com>
On Tue Aug 4, 2020 at 7:49 PM EDT, Junio C Hamano wrote:
> How exactly does "git add -p" fail for such a patch? What operation
> does it exactly want to do ("apply --cached"???) and is it "apply"
> that is wrong, or is it "git add -p" that fails to remove the i-t-a
> entry from the index before running "git apply" that is at fault?

Yes, "add -p" uses "apply --cached". I do believe this belongs in apply, both because all "add -p" really does is assemble things to be fed to apply and also for the more detailed reasons below.

The index and the filesystem are both able to represent "no file" and "a file exists" states, but the index has an additional state (i-t-a) with no direct representation in the worktree. By (correctly) emitting "new file" patches when comparing a file to an i-t-a index entry, we are setting down the rule that a "new file" patch is not merely the diff between "no file" and "a file exists", but also the diff between i-t-a and "a file exists".

Similarly, "deleted file" patches are the diff between "a file exists" and "no file exists", but they are also the diff between i-t-a and "no file exists" - if you add -N a file and then delete it from the worktree, "deleted file" is what git diff (correctly) shows. As a consequence of these rules, "new file" and "deleted file" diffs are now the only diffs that validly apply to an i-t-a entry. So apply needs to handle them (in "--cached" mode, anyway).

But the worktree lives in the filesystem, where there are no i-t-a entries. So the question seems to me to be whether "no file" in the worktree matches an i-t-a entry in the index for the purposes of "add --index". I count a couple options here:

- Nothing on the filesystem can accurately match an i-t-a entry in the
  index, so all attempts at "apply --index" when there is an i-t-a in
  the index fail with "file: does not match index". "apply --cached",
  which "add -p" uses, applies only to the index and continues to work.
  I think I prefer this one; additionally, the comment in read-cache.c
  indicate that this is supposed to be the case already, so I just need
  to make sure this check is not skipped on "new file" patches.
- The current (as of this patch) behavior: a "new file" patch applies
  both to an i-t-a in the index, and to the lack of a file in the
  worktree. This may seem strange, but it may also seem strange that an
  identical new file patch, which can be applied either to just the
  worktree or just the index successfully, fails when applied to both at
  the same time with "apply --index". However, this is precisely what is
  done anyway by "apply --index" when there are no i-t-a entries
  involved, so I lean towards i-t-a entries never matching the worktree.
Patch for the first option in progress.
Previous: Junio C HamanoNext: Raymond E. Pasco
Message 7 of 53 in “apply: Allow "new file" patches on i-t-a entries”
  1. apply: Allow "new file" patches on i-t-a entriesRaymond E. Pasco, Aug 4, 2020
  2. Junio C HamanoAug 4, 2020
  3. Raymond E. PascoAug 4, 2020
  4. apply: allow "new file" patches on i-t-a entriesRaymond E. Pasco, Aug 4, 2020
  5. apply: allow "new file" patches on i-t-a entriesRaymond E. Pasco, Aug 4, 2020
  6. Junio C HamanoAug 4, 2020
  7. Raymond E. PascoAug 5, 2020
  8. 0/3 apply: handle i-t-a entries in indexRaymond E. Pasco, Aug 6, 2020
  9. 1/3 apply: allow "new file" patches on i-t-a entriesRaymond E. Pasco, Aug 6, 2020
  10. 2/3 apply: make i-t-a entries never match worktreeRaymond E. Pasco, Aug 6, 2020
  11. Junio C HamanoAug 6, 2020
  12. Raymond E. PascoAug 6, 2020
  13. 3/3 t4140: test apply with i-t-a pathsRaymond E. Pasco, Aug 6, 2020
  14. Junio C HamanoAug 6, 2020
  15. Raymond E. PascoAug 7, 2020
  16. 0/3 apply: handle i-t-a entries in indexRaymond E. Pasco, Aug 8, 2020
  17. 1/3 apply: allow "new file" patches on i-t-a entriesRaymond E. Pasco, Aug 8, 2020
  18. Phillip WoodAug 8, 2020
  19. 2/3 apply: make i-t-a entries never match worktreeRaymond E. Pasco, Aug 8, 2020
  20. Phillip WoodAug 8, 2020
  21. Raymond E. PascoAug 8, 2020
  22. Phillip WoodAug 8, 2020
  23. Raymond E. PascoAug 8, 2020
  24. Phillip WoodAug 9, 2020
  25. Junio C HamanoAug 9, 2020
  26. git-apply.txt: correct description of --cachedRaymond E. Pasco, Aug 10, 2020
  27. Junio C HamanoAug 10, 2020
  28. Phillip WoodAug 12, 2020
  29. Junio C HamanoAug 12, 2020
  30. Raymond E. PascoAug 12, 2020
  31. Phillip WoodAug 12, 2020
  32. 3/3 t4140: test apply with i-t-a pathsRaymond E. Pasco, Aug 8, 2020
  33. Phillip WoodAug 23, 2020
  34. 1/1 diff-lib: use worktree mode in diffs from i-t-a entriesRaymond E. Pasco, Aug 8, 2020
  35. Martin ÅgrenAug 8, 2020
  36. Raymond E. PascoAug 8, 2020
  37. Martin ÅgrenAug 8, 2020
  38. Junio C HamanoAug 9, 2020
  39. t4069: test diff behavior with i-t-a pathsRaymond E. Pasco, Aug 10, 2020
  40. diff-lib: use worktree mode in diffs from i-t-a entriesRaymond E. Pasco, Aug 10, 2020
  41. diff-lib: use worktree mode in diffs from i-t-a entriesRaymond E. Pasco, Aug 10, 2020
  42. Junio C HamanoAug 10, 2020
  43. Eric SunshineAug 10, 2020
  44. Eric SunshineAug 10, 2020
  45. Junio C HamanoAug 10, 2020
  46. Eric SunshineAug 10, 2020
  47. Junio C HamanoAug 10, 2020
  48. Raymond E. PascoAug 10, 2020
  49. Eric SunshineAug 10, 2020
  50. Junio C HamanoAug 11, 2020
  51. 0/2 apply: reject modification diffs to i-t-a entriesRaymond E. Pasco, Aug 8, 2020
  52. 1/2 apply: reject modification diffs to i-t-a entriesRaymond E. Pasco, Aug 8, 2020
  53. 2/2 t4140: test failure of diff from empty blob to i-t-a pathRaymond E. Pasco, Aug 8, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.