Subject: Re: git-apply does not work in a sub-directory of a Git repository

From: Junio C Hamano <[email-masked]>
Date: Thu, 24 Mar 2016 10:32:35 -0700

Junio C Hamano <[email-masked]> writes:

> So a better alternative may be to conditionally disable the "Paths
> outside are not touched regardless of --include" logic, i.e. we
> exclude paths outside by default just as before, but if there is at
> least one explicit "--include" given, we skip this "return 0".
>
> That way, we do not have to commit to turning --include/--exclude to
> pathspec (which I agree is a huge change in behaviour that may not
> be a good idea) and we do not have to add "--full-tree" that is only
> understood by "apply" but not other commands that operate on the
> current directory by default.

And the necessary change to do so may look like this.  With this:

    $ git show >P
    $ git reset --hard HEAD^
    $ cd t
    $ git apply -v ../P
    $ git apply -v --include=\* ../P

seem to work as expected.

diff --git a/builtin/apply.c b/builtin/apply.c
index c993333..1af3f7e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1955,8 +1955,8 @@ static int use_patch(struct patch *p)
 	const char *pathname = p->new_name ? p->new_name : p->old_name;
 	int i;
 
-	/* Paths outside are not touched regardless of "--include" */
-	if (0 < prefix_length) {
+	/* Paths outside are not touched when there is no explicit "--include" */
+	if (!has_include && 0 < prefix_length) {
 		int pathlen = strlen(pathname);
 		if (pathlen <= prefix_length ||
 		    memcmp(prefix, pathname, prefix_length))