All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] pathspec: always honor `PATHSPEC_PREFIX_ORIGIN` flag
@ 2017-04-03  8:03 Patrick Steinhardt
  2017-04-03 16:26 ` Brandon Williams
  0 siblings, 1 reply; 4+ messages in thread
From: Patrick Steinhardt @ 2017-04-03  8:03 UTC (permalink / raw)
  To: git
  Cc: Patrick Steinhardt, Nguyễn Thái Ngọc Duy,
	Brandon Williams

Previous to commit 5d8f084a5 (pathspec: simpler logic to prefix original
pathspec elements, 2017-01-04), we were always using the computed
`match` variable to perform pathspec matching whenever
`PATHSPEC_PREFIX_ORIGIN` is set. This is for example useful when passing
the parsed pathspecs to other commands, as the computed `match` may
contain a pathspec relative to the repository root. The commit changed
this logic to only do so when we do have an actual prefix and when
literal pathspecs are deactivated.

But this change may actually break some commands which expect passed
pathspecs to be relative to the repository root. One such case is `git
add --patch`, which now fails when using relative paths from a
subdirectory. For example if executing "git add -p ../foo.c" in a
subdirectory, the `git-add--interactive` command will directly pass
"../foo.c" to `git-ls-files`. As ls-files is executed at the
repository's root, the command will notice that "../foo.c" is outside
the repository and fail.

Fix the issue by again using the computed `match` variable whenever
`PATHSPEC_PREFIX_ORIGIN` is set. This restores behavior previous to
5d8f084a5 and fixes interactive add.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 pathspec.c                 |  6 +++---
 t/t3701-add-interactive.sh | 20 ++++++++++++++++++++
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/pathspec.c b/pathspec.c
index 303efda83..3193e45a6 100644
--- a/pathspec.c
+++ b/pathspec.c
@@ -504,12 +504,12 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
 	 * Prefix the pathspec (keep all magic) and assign to
 	 * original. Useful for passing to another command.
 	 */
-	if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
-	    prefixlen && !get_literal_global()) {
+	if (flags & PATHSPEC_PREFIX_ORIGIN) {
 		struct strbuf sb = STRBUF_INIT;
 
 		/* Preserve the actual prefix length of each pattern */
-		prefix_magic(&sb, prefixlen, element_magic);
+		if (prefixlen && !get_literal_global())
+			prefix_magic(&sb, prefixlen, element_magic);
 
 		strbuf_addstr(&sb, match);
 		item->original = strbuf_detach(&sb, NULL);
diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
index f9528fa00..640088dd6 100755
--- a/t/t3701-add-interactive.sh
+++ b/t/t3701-add-interactive.sh
@@ -436,6 +436,26 @@ test_expect_success 'add -p handles globs' '
 	test_cmp expect actual
 '
 
+test_expect_success 'add -p handles relative paths' '
+	git reset --hard &&
+
+	echo base >root.c &&
+	git add "*.c" &&
+	git commit -m base &&
+
+	echo change >root.c &&
+	mkdir -p subdir &&
+	git -C subdir add -p "../root.c" <<-\EOF &&
+	y
+	EOF
+
+	cat >expect <<-\EOF &&
+	root.c
+	EOF
+	git diff --cached --name-only >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success 'add -p does not expand argument lists' '
 	git reset --hard &&
 
-- 
2.12.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] pathspec: always honor `PATHSPEC_PREFIX_ORIGIN` flag
  2017-04-03  8:03 [PATCH] pathspec: always honor `PATHSPEC_PREFIX_ORIGIN` flag Patrick Steinhardt
@ 2017-04-03 16:26 ` Brandon Williams
  2017-04-04  9:17   ` Patrick Steinhardt
  2017-04-05 13:40   ` Duy Nguyen
  0 siblings, 2 replies; 4+ messages in thread
From: Brandon Williams @ 2017-04-03 16:26 UTC (permalink / raw)
  To: Patrick Steinhardt; +Cc: git, Nguyễn Thái Ngọc Duy

On 04/03, Patrick Steinhardt wrote:
> Previous to commit 5d8f084a5 (pathspec: simpler logic to prefix original
> pathspec elements, 2017-01-04), we were always using the computed
> `match` variable to perform pathspec matching whenever
> `PATHSPEC_PREFIX_ORIGIN` is set. This is for example useful when passing
> the parsed pathspecs to other commands, as the computed `match` may
> contain a pathspec relative to the repository root. The commit changed
> this logic to only do so when we do have an actual prefix and when
> literal pathspecs are deactivated.
> 
> But this change may actually break some commands which expect passed
> pathspecs to be relative to the repository root. One such case is `git
> add --patch`, which now fails when using relative paths from a
> subdirectory. For example if executing "git add -p ../foo.c" in a
> subdirectory, the `git-add--interactive` command will directly pass
> "../foo.c" to `git-ls-files`. As ls-files is executed at the
> repository's root, the command will notice that "../foo.c" is outside
> the repository and fail.
> 
> Fix the issue by again using the computed `match` variable whenever
> `PATHSPEC_PREFIX_ORIGIN` is set. This restores behavior previous to
> 5d8f084a5 and fixes interactive add.
> 
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  pathspec.c                 |  6 +++---
>  t/t3701-add-interactive.sh | 20 ++++++++++++++++++++
>  2 files changed, 23 insertions(+), 3 deletions(-)
> 
> diff --git a/pathspec.c b/pathspec.c
> index 303efda83..3193e45a6 100644
> --- a/pathspec.c
> +++ b/pathspec.c
> @@ -504,12 +504,12 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
>  	 * Prefix the pathspec (keep all magic) and assign to
>  	 * original. Useful for passing to another command.
>  	 */
> -	if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
> -	    prefixlen && !get_literal_global()) {
> +	if (flags & PATHSPEC_PREFIX_ORIGIN) {
>  		struct strbuf sb = STRBUF_INIT;
>  
>  		/* Preserve the actual prefix length of each pattern */
> -		prefix_magic(&sb, prefixlen, element_magic);
> +		if (prefixlen && !get_literal_global())
> +			prefix_magic(&sb, prefixlen, element_magic);
>  
>  		strbuf_addstr(&sb, match);
>  		item->original = strbuf_detach(&sb, NULL);

Would it just make sense to drop the requirement that prefixlen be
non-zero?  My problem with this change currently is the ability to get
an original string with is empty (ie "\0") which would cause git to
throw some warnings about not allowing empty strings as pathspecs if
they were then passed on to other processes.

> diff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh
> index f9528fa00..640088dd6 100755
> --- a/t/t3701-add-interactive.sh
> +++ b/t/t3701-add-interactive.sh
> @@ -436,6 +436,26 @@ test_expect_success 'add -p handles globs' '
>  	test_cmp expect actual
>  '
>  
> +test_expect_success 'add -p handles relative paths' '
> +	git reset --hard &&
> +
> +	echo base >root.c &&
> +	git add "*.c" &&
> +	git commit -m base &&
> +
> +	echo change >root.c &&
> +	mkdir -p subdir &&
> +	git -C subdir add -p "../root.c" <<-\EOF &&
> +	y
> +	EOF
> +
> +	cat >expect <<-\EOF &&
> +	root.c
> +	EOF
> +	git diff --cached --name-only >actual &&
> +	test_cmp expect actual
> +'
> +
>  test_expect_success 'add -p does not expand argument lists' '
>  	git reset --hard &&
>  
> -- 
> 2.12.2
> 

-- 
Brandon Williams

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] pathspec: always honor `PATHSPEC_PREFIX_ORIGIN` flag
  2017-04-03 16:26 ` Brandon Williams
@ 2017-04-04  9:17   ` Patrick Steinhardt
  2017-04-05 13:40   ` Duy Nguyen
  1 sibling, 0 replies; 4+ messages in thread
From: Patrick Steinhardt @ 2017-04-04  9:17 UTC (permalink / raw)
  To: Brandon Williams; +Cc: git, Nguyễn Thái Ngọc Duy

[-- Attachment #1: Type: text/plain, Size: 3375 bytes --]

On Mon, Apr 03, 2017 at 09:26:48AM -0700, Brandon Williams wrote:
> On 04/03, Patrick Steinhardt wrote:
> > Previous to commit 5d8f084a5 (pathspec: simpler logic to prefix original
> > pathspec elements, 2017-01-04), we were always using the computed
> > `match` variable to perform pathspec matching whenever
> > `PATHSPEC_PREFIX_ORIGIN` is set. This is for example useful when passing
> > the parsed pathspecs to other commands, as the computed `match` may
> > contain a pathspec relative to the repository root. The commit changed
> > this logic to only do so when we do have an actual prefix and when
> > literal pathspecs are deactivated.
> > 
> > But this change may actually break some commands which expect passed
> > pathspecs to be relative to the repository root. One such case is `git
> > add --patch`, which now fails when using relative paths from a
> > subdirectory. For example if executing "git add -p ../foo.c" in a
> > subdirectory, the `git-add--interactive` command will directly pass
> > "../foo.c" to `git-ls-files`. As ls-files is executed at the
> > repository's root, the command will notice that "../foo.c" is outside
> > the repository and fail.
> > 
> > Fix the issue by again using the computed `match` variable whenever
> > `PATHSPEC_PREFIX_ORIGIN` is set. This restores behavior previous to
> > 5d8f084a5 and fixes interactive add.
> > 
> > Signed-off-by: Patrick Steinhardt <ps@pks.im>
> > ---
> >  pathspec.c                 |  6 +++---
> >  t/t3701-add-interactive.sh | 20 ++++++++++++++++++++
> >  2 files changed, 23 insertions(+), 3 deletions(-)
> > 
> > diff --git a/pathspec.c b/pathspec.c
> > index 303efda83..3193e45a6 100644
> > --- a/pathspec.c
> > +++ b/pathspec.c
> > @@ -504,12 +504,12 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
> >  	 * Prefix the pathspec (keep all magic) and assign to
> >  	 * original. Useful for passing to another command.
> >  	 */
> > -	if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
> > -	    prefixlen && !get_literal_global()) {
> > +	if (flags & PATHSPEC_PREFIX_ORIGIN) {
> >  		struct strbuf sb = STRBUF_INIT;
> >  
> >  		/* Preserve the actual prefix length of each pattern */
> > -		prefix_magic(&sb, prefixlen, element_magic);
> > +		if (prefixlen && !get_literal_global())
> > +			prefix_magic(&sb, prefixlen, element_magic);
> >  
> >  		strbuf_addstr(&sb, match);
> >  		item->original = strbuf_detach(&sb, NULL);
> 
> Would it just make sense to drop the requirement that prefixlen be
> non-zero?  My problem with this change currently is the ability to get
> an original string with is empty (ie "\0") which would cause git to
> throw some warnings about not allowing empty strings as pathspecs if
> they were then passed on to other processes.

You're right. My patch results in:

$ git init repo
$ cd repo
$ touch file
$ git add file
$ git commit -mfile
$ echo foo>file
$ mkdir subdir
$ cd subdir
$ git add -p ..
warning: empty strings as pathspecs will be made invalid...
warning: empty strings as pathspecs will be made invalid...

Dropping only the `prefixlen` condition and then unconditionally
calling `prefix_magic` suffices to fix the actual bug. I've
improved the test to use a pathspec which resolves to "" and
check stderr.

Thanks for catching this!

Regards
Patrick

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] pathspec: always honor `PATHSPEC_PREFIX_ORIGIN` flag
  2017-04-03 16:26 ` Brandon Williams
  2017-04-04  9:17   ` Patrick Steinhardt
@ 2017-04-05 13:40   ` Duy Nguyen
  1 sibling, 0 replies; 4+ messages in thread
From: Duy Nguyen @ 2017-04-05 13:40 UTC (permalink / raw)
  To: Brandon Williams; +Cc: Patrick Steinhardt, Git Mailing List

On Mon, Apr 3, 2017 at 11:26 PM, Brandon Williams <bmwill@google.com> wrote:
> On 04/03, Patrick Steinhardt wrote:
>> Previous to commit 5d8f084a5 (pathspec: simpler logic to prefix original
>> pathspec elements, 2017-01-04), we were always using the computed
>> `match` variable to perform pathspec matching whenever
>> `PATHSPEC_PREFIX_ORIGIN` is set. This is for example useful when passing
>> the parsed pathspecs to other commands, as the computed `match` may
>> contain a pathspec relative to the repository root. The commit changed
>> this logic to only do so when we do have an actual prefix and when
>> literal pathspecs are deactivated.
>>
>> But this change may actually break some commands which expect passed
>> pathspecs to be relative to the repository root. One such case is `git
>> add --patch`, which now fails when using relative paths from a
>> subdirectory. For example if executing "git add -p ../foo.c" in a
>> subdirectory, the `git-add--interactive` command will directly pass
>> "../foo.c" to `git-ls-files`. As ls-files is executed at the
>> repository's root, the command will notice that "../foo.c" is outside
>> the repository and fail.

Oops. Sorry I missed this.

>> @@ -504,12 +504,12 @@ static void init_pathspec_item(struct pathspec_item *item, unsigned flags,
>>        * Prefix the pathspec (keep all magic) and assign to
>>        * original. Useful for passing to another command.
>>        */
>> -     if ((flags & PATHSPEC_PREFIX_ORIGIN) &&
>> -         prefixlen && !get_literal_global()) {
>> +     if (flags & PATHSPEC_PREFIX_ORIGIN) {
>>               struct strbuf sb = STRBUF_INIT;
>>
>>               /* Preserve the actual prefix length of each pattern */
>> -             prefix_magic(&sb, prefixlen, element_magic);
>> +             if (prefixlen && !get_literal_global())
>> +                     prefix_magic(&sb, prefixlen, element_magic);
>>
>>               strbuf_addstr(&sb, match);
>>               item->original = strbuf_detach(&sb, NULL);
>
> Would it just make sense to drop the requirement that prefixlen be
> non-zero?  My problem with this change currently is the ability to get
> an original string with is empty (ie "\0") which would cause git to
> throw some warnings about not allowing empty strings as pathspecs if
> they were then passed on to other processes.

Good catch.

I did wonder if it's a right thing, because the result pathspec is
':(prefix:0)'. After leaving out the magic path, you get an empty
"path" of the pathspec, which probably should be warned because we
wouldn't be able to handle it. For example, "git add ." at root will
give you the path ".", not empty one. Maybe we can't handle the empty
"path" part.

But we have an exception for this already. In "git add :/", the "path"
of the pathspec is still empty ('/' is a magic, not "path" even if it
looks like so) and we handle it just fine. So everything should be
good here.

Patrick, please add a line or two about why you drop prefixlen when
you re-roll (or even better, make it a separate patch; this sounds
like an issue even before the code reorganization changes).
-- 
Duy

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-04-05 13:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-03  8:03 [PATCH] pathspec: always honor `PATHSPEC_PREFIX_ORIGIN` flag Patrick Steinhardt
2017-04-03 16:26 ` Brandon Williams
2017-04-04  9:17   ` Patrick Steinhardt
2017-04-05 13:40   ` Duy Nguyen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.