# Making git apply always work relative to current directory

3 messages from 2016-03-05 to 2016-03-05. Participants: brian m. carlson, Junio C Hamano.
Thread: https://gitlist.dev/t/41624

## brian m. carlson, 2016-03-05 18:11

Subject: Making git apply always work relative to current directory
Message-ID: <20160305181125.GA48536@vauxhall.crustytoothpaste.net>
URL: https://gitlist.dev/e/20160305181125.GA48536%40vauxhall.crustytoothpaste.net

```
I have a piece of software which must run out of a given directory.  In
development, this is a git repository, and in production it is not.  I
also have an ignored subdirectory where I would like to use git apply to
apply patches (in both environments).

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*.
I have to provide a --directory argument in order for it to function
underneath the repository, but of course that doesn't work when the
directory isn't within a repository.  --unsafe-paths did not seem to
make a difference.  I'm using Git 2.7.2.

Is there a way to tell git apply that it should apply relative to the
current working directory, no matter what?  I'm happy to send a patch to
either the documentation or git apply if necessary.
-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | https://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: https://keybase.io/bk2204

```

## Junio C Hamano, 2016-03-05 19:31

Subject: Re: Making git apply always work relative to current directory
Message-ID: <xmqq4mckvhzq.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqq4mckvhzq.fsf%40gitster.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.

```

## brian m. carlson, 2016-03-05 19:42

Subject: Re: Making git apply always work relative to current directory
Message-ID: <20160305194200.GB48536@vauxhall.crustytoothpaste.net>
URL: https://gitlist.dev/e/20160305194200.GB48536%40vauxhall.crustytoothpaste.net
In-Reply-To: <xmqq4mckvhzq.fsf@gitster.mtv.corp.google.com>

```
On Sat, Mar 05, 2016 at 11:31:53AM -0800, Junio C Hamano wrote:
> "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.

I was planning to add a --here option (maybe spelled
--current-directory) that would change that behavior, since I figured
that people would be relying on the current behavior.  The man page
clearly indicates that some people are using it as a better GNU patch,
so an option that does that might be useful.

Regardless, I'd say that --verbose should cause git apply to say
something.  I'm not a newbie with Git, and I spent about an hour trying
to figure this out.  I've also been bitten by it before elsewhere.

> 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.

The existing code used patch until we realized that older versions of
GNU patch can't apply certain git diffs.  It's not super easy to change
it, but I suppose we could.
-- 
brian m. carlson / brian with sandals: Houston, Texas, US
+1 832 623 2791 | https://www.crustytoothpaste.net/~bmc | My opinion only
OpenPGP: https://keybase.io/bk2204

```
