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

Re: Making git apply always work relative to current directory

From
Junio C Hamano <gitster@pobox.com>
Date
Mar 5, 2016, 19:31 UTC
Message-ID
<xmqq4mckvhzq.fsf@gitster.mtv.corp.google.com>
In-Reply-To
<20160305181125.GA48536@vauxhall.crustytoothpaste.net>
"brian m. carlson" <sandals@crustytoothpaste.net> writes:
> If I run git apply --no-index --verbose <patch>, it succeeds in both
> cases, but when I'm in the git repository, it *silently does nothing*.

That originally sounded peculiar to me and I suspected it to be a bug, but it looks like a designed-in feature and with us since

commit edf2e37002eeb30a2ccad5db3b3e1fe41cdc7eb0
Author: Junio C Hamano <junkio@cox.net>
Date:   Fri Nov 25 23:14:15 2005 -0800
    git-apply: work from subdirectory.
    
    When applying a patch to index file, we need to know where GIT_DIR is;
    use setup_git_directory() to find it out.  This also allows us to work
    from a subdirectory if we wanted to.
    
    When git-apply is run from a subdirectory, it applies the given patch
    only to the files under the current directory and below.
    
    Signed-off-by: Junio C Hamano <junkio@cox.net>

So exclusion by use_patch() for paths outside the current directory seems to be a feature; the log message does not say "why", but if I have to guess, the reasoning was probably "The old world order was that the command has to always be run from the top level. A user who wants to run it from a subdirectory must be doing so for a reason, e.g. 'I am currently working in this directory, do not touch outside this area'". In any case, I suspect that the existing tooling people built over the past 10 years around "git apply" already depends on this behaviour, so we cannot lightly change it.

Is it so hard to temporarily go up to the root, run "git apply", and come back? You can use "--no-index --directory=trash" for both cases that way.

Previous: brian m. carlsonNext: brian m. carlson
Message 2 of 3 in “Making git apply always work relative to current directory”
  1. brian m. carlsonMar 5, 2016
  2. Junio C HamanoMar 5, 2016
  3. brian m. carlsonMar 5, 2016

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.