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

Re: [PATCH] Teach 'git apply' to look at $GIT_DIR/config

From
Junio C Hamano <junkio@cox.net>
Date
Feb 21, 2007, 11:22 UTC
Message-ID
<7vmz37pxf8.fsf@assigned-by-dhcp.cox.net>
In-Reply-To
<Pine.LNX.4.64.0702210014140.6485@iabervon.org>
Daniel Barkalow <barkalow@iabervon.org> writes:
> "git apply" should be able to notice the many clues that this patch 
> doesn't go at the root: (1) it's not -r; (2) it's not a rename, but the 
> filenames aren't the same; (3) there isn't an extra path element to 
> remove.
I would sort-of agree with (1) and (3) but people can do
	cd drivers && diff -u Makefile~ Makefile
so (2) is not a good clue and (3) is not really either.

Especially files like 'Makefile' are everywhere that dwimming and getting it wrong is worse than being stupid but consistent.

We do -p1 by default because the BCP on the kernel list (which is where git came from -- but I suspect many projects follow the convention) is to use "diff a/dir/file b/dir/file". This is also a convenient setup for people who apply git generated (or BCP formatted) patches from the toplevel of the tree.

But when you are dealing with handcrafted patches in a subdirectory, such as the one you would get by:

    $ cd some/dir && edit file.c && diff -u file.c~ file.c >G.patch

you do not have the "a/some/dir b/some/dir" prefix, so -p1 to strip "a/" and "b/" is a wrong thing to begin with, but looking at G.patch you would not even have "some/dir", so no "correct" value exists if we refuse to DWIM (and I refuse to). We support path strip levels with -p<n> option ever since you added it with e36f8b60, so that is Ok.

However, the current version (in v1.5.0.1) of git-apply has a few inconsistencies.

 * When --index (or --cached) is given, and when the patch is a
   git generated (or BCP formatted) patch, it finds out where in
   the working tree you are, and strips the right prefix
   automatically.
 * When there is no --index nor --cached, however, it does not
   bother to find out where in the working tree your $cwd is,
   and assumes you are at the toplevel (you may not even be in a
   git managed tree but are using git-apply as a better "patch",
   in which case there is no way to find out where the toplevel
   is).  You need to give -p<n> to explicitly strip the leading
   path.  This is not a problem but happens to be a feature to
   help applying handcrafted patches, like G.patch above.  Also,
   it is consistent with how GNU patch would behave.  However,
   -p<n> is ignored when the patch is a git generated one, which
   is a minor additional problem.

I was initially in favor of correcting this inconsistency by matching the behaviour of non --index/--cached case to that of the behaviour of --index/--cached case (in other words, making things more convenient for people who apply git generated and/or BCP formatted patches). But Linus talked me out of it --- and I think he is right.

Inconsistency is fixed by making the behaviour more similar to "GNU patch". The behaviour of --index/--cached case is changed so that it does not automatically strip "some/dir" part when you are in a subdirectory, which is how git-apply without these options operates. Of course, ignoring -p<n> for git generated patches no longer makes sense with this change, which is also fixed, so you can use the same -p<n> as you would give to "GNU patch".

What is cooking in 'next' should behave the way described above. If not, patches to fix it is very much welcome ;-).

P.S. I think the list hasn't heard from you for quite some time. Welcome back.

Previous: Daniel BarkalowNext: Daniel Barkalow
Message 24 of 49 in “Teach core.autocrlf to 'git apply'”
  1. Teach core.autocrlf to 'git apply'Junio C Hamano, Feb 17, 2007
  2. Teach 'git apply' to look at $GIT_DIR/configJunio C Hamano, Feb 17, 2007
  3. Jeff KingFeb 17, 2007
  4. Junio C HamanoFeb 17, 2007
  5. Jeff KingFeb 17, 2007
  6. Linus TorvaldsFeb 19, 2007
  7. Shawn O. PearceFeb 19, 2007
  8. Junio C HamanoFeb 19, 2007
  9. Linus TorvaldsFeb 19, 2007
  10. Junio C HamanoFeb 19, 2007
  11. Linus TorvaldsFeb 20, 2007
  12. Junio C HamanoFeb 20, 2007
  13. Johannes SchindelinFeb 20, 2007
  14. Junio C HamanoFeb 20, 2007
  15. Johannes SchindelinFeb 20, 2007
  16. git-apply: require -p<n> when working in a subdirectory.Junio C Hamano, Feb 20, 2007
  17. apply: fix memory leak in prefix_one()Johannes Schindelin, Feb 20, 2007
  18. Junio C HamanoFeb 20, 2007
  19. Johannes SchindelinFeb 20, 2007
  20. git-apply: do not lose cwd when run from a subdirectory.Junio C Hamano, Feb 20, 2007
  21. Junio C HamanoFeb 20, 2007
  22. Linus TorvaldsFeb 20, 2007
  23. Daniel BarkalowFeb 21, 2007
  24. Junio C HamanoFeb 21, 2007
  25. Daniel BarkalowFeb 21, 2007
  26. Linus TorvaldsFeb 21, 2007
  27. Junio C HamanoFeb 21, 2007
  28. git-apply: notice "diff --git" patch againJunio C Hamano, Feb 21, 2007
  29. git-apply: guess correct -p<n> value for non-git patches.Junio C Hamano, Feb 22, 2007
  30. Johannes SchindelinFeb 20, 2007
  31. Simon 'corecode' SchubertFeb 20, 2007
  32. Johannes SchindelinFeb 18, 2007
  33. Junio C HamanoFeb 18, 2007
  34. Johannes SchindelinFeb 18, 2007
  35. Junio C HamanoFeb 18, 2007
  36. Johannes SchindelinFeb 18, 2007
  37. Junio C HamanoFeb 18, 2007
  38. Johannes SchindelinFeb 18, 2007
  39. Junio C HamanoFeb 18, 2007
  40. Johannes SchindelinFeb 18, 2007
  41. Jakub NarebskiFeb 18, 2007
  42. Johannes SchindelinFeb 18, 2007
  43. Junio C HamanoFeb 18, 2007
  44. Johannes SchindelinFeb 18, 2007
  45. Junio C HamanoFeb 18, 2007
  46. Johannes SchindelinFeb 18, 2007
  47. Junio C HamanoFeb 18, 2007
  48. Johannes SchindelinFeb 18, 2007
  49. Junio C HamanoFeb 18, 2007

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.