Skip to contentSkip to conversation

Excluding paths with wildcard not working with add -p

PATCH In review Robert Dailey 3 participants 7 messages May 29, 2022 – May 30, 2022

lore
Share & tools
lore
#0 lore raw

If I run the command:

    git add -p -- ':^*.cs'

I get an error:

fatal: empty string is not a valid pathspec. please use . instead if you meant to match all paths
Cannot close git diff-index --cached --numstat --summary HEAD -- :(exclude,prefix:0)*.cs () at C:/Program Files/Git/mingw64/libexec/git-core\git-add--interactive line 242.

However, it works if I remove -p. Also observed this works too:

    git add -p -- ':*.cs'

So it looks like we have a corner case where I can't do a patch add while excluding files. Is this intentional?

#1 lore raw
Quoted 12 lines · Robert Dailey

If I run the command:

    git add -p -- ':^*.cs'

I get an error:

fatal: empty string is not a valid pathspec. please use . instead if you meant to match all paths
Cannot close git diff-index --cached --numstat --summary HEAD -- :(exclude,prefix:0)*.cs () at C:/Program Files/Git/mingw64/libexec/git-core\git-add--interactive line 242.

Quoted 6 lines

However, it works if I remove -p. Also observed this works too:

    git add -p -- ':*.cs'

So it looks like we have a corner case where I can't do a patch add while excluding files. Is this intentional?

I do not think so.

Does this command

    git add -p -- . ':^*.cs'

work as you expect?

#2 lore raw
Quoted 5 lines · Junio C Hamano

Does this command

    git add -p -- . ':^*.cs'

work as you expect?

We used to error out when you give a pathspec with only negative elements in it, like the one you gave above. Later, we tweaked this logic at 859b7f1d (pathspec: don't error out on all-exclusionary pathspec patterns, 2017-02-07) so that we add an empty string as an extra element when your pathspec has only negative elements.

At around the same time, we were migrating from "an empty string is a valid pathspec element that matches everything" to "either a dot or ":/" is used for that purpose, and an empty string is rejected", between d426430e (pathspec: warn on empty strings as pathspec, 2016-06-22) and 9e4e8a64 (pathspec: die on empty strings as pathspec, 2017-06-06). I think 9e4e8a64 was not careful enough to turn the empty string 859b7f1d added to either a dot or ":/"

For the purpose of "add -p", I _think_ adding a "dot" is correct, but depending on the command, the code needs to add ":/".

Here is a quick trial patch, which seems to compile and pass all the tests we have, but the fact that this lingered with us for the past 5 years is a strong sign that we lack coverage in this area, so it may be breaking something else in a big way.

 pathspec.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
pathspec.c +4 -2
index ddeeba7911..1b0ae51aa4 100644
@@ -628,8 +628,10 @@ void parse_pathspec(struct pathspec *pathspec,
 	 * that matches everything. We allocated an extra one for this.
 	 */
 	if (nr_exclude == n) {
-		int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
-		init_pathspec_item(item + n, 0, prefix, plen, "");
+		if (!(flags & PATHSPEC_PREFER_CWD))
+			init_pathspec_item(item + n, 0, NULL, 0, ":/");
+		else
+			init_pathspec_item(item + n, 0, prefix, prefixlen, ".");
 		pathspec->nr++;
 	}
 
#3 lore raw

PATCH 1/1 pathspec: correct an empty string used as a pathspec element

Pathspecs with only negative elements did not work with some commands that pass the pathspec along to a subprocess. For instance,

    $ git add -p -- ':!*.txt'

should add everything except for paths ending in ".txt", but it gets complaint from underlying "diff-index" and aborts.

We used to error out when a pathspec with only negative elements in it, like the one in the above example. Later, 859b7f1d (pathspec:
don't error out on all-exclusionary pathspec patterns, 2017-02-07) updated the logic to add an empty string as an extra element. The intention was to let the extra element to match everything and let the negative ones given by the user to subtract from it.

At around the same time, we were migrating from "an empty string is a valid pathspec element that matches everything" to "either a dot or ":/" is used to match all, and an empty string is rejected", between d426430e (pathspec: warn on empty strings as pathspec, 2016-06-22) and 9e4e8a64 (pathspec: die on empty strings as pathspec, 2017-06-06). I think 9e4e8a64, which happened long after 859b7f1d happened, was not careful enough to turn the empty string 859b7f1d added to either a dot or ":/".

A care should be taken as the definition of "everything" depends on subcommand. For the purpose of "add -p", adding a "." to add everything in the current directory is the right thing to do. But for some other commands, ":/" (i.e. really really everything, even things outside the current subdirectory) is the right choice.

We would break commands in a big way if we get this wrong, so add a handful of test pieces to make sure the resulting code still excludes the paths that are expected and includes "everything" else.

---
 pathspec.c                  |   2 +-
 t/t6132-pathspec-exclude.sh | 181 ++++++++++++++++++++++++++++++++++++
 2 files changed, 182 insertions(+), 1 deletion(-)
pathspec.c +1 -1
index ddeeba7911..84ad9c73cf 100644
@@ -629,7 +629,7 @@ void parse_pathspec(struct pathspec *pathspec,
 	 */
 	if (nr_exclude == n) {
 		int plen = (!(flags & PATHSPEC_PREFER_CWD)) ? 0 : prefixlen;
-		init_pathspec_item(item + n, 0, prefix, plen, "");
+		init_pathspec_item(item + n, 0, prefix, plen, ".");
 		pathspec->nr++;
 	}
 
t/t6132-pathspec-exclude.sh +181 -0

191 lines not on this page · raw message

Signature · 1 line
2.36.1-385-g60203f3fdb
#4 lore raw

RE: [PATCH] pathspec: correct an empty string used as a pathspec element

Quoted 6 lines · Junio C Hamano

Pathspecs with only negative elements did not work with some commands that pass the pathspec along to a subprocess. For instance,

   $ git add -p -- ':!*.txt'

should add everything except for paths ending in ".txt", but it gets

complaint from

Quoted 3 lines

underlying "diff-index" and aborts.

We used to error out when a pathspec with only negative elements in it,

like the

Quoted 2 lines

one in the above example. Later, 859b7f1d (pathspec:
don't error out on all-exclusionary pathspec patterns, 2017-02-07) updated

the

Quoted 1 line

logic to add an empty string as an extra element. The intention was to let

the

Quoted 1 line

extra element to match everything and let the negative ones given by the

user to

Quoted 4 lines

subtract from it.

At around the same time, we were migrating from "an empty string is a valid pathspec element that matches everything" to "either a dot or ":/" is used

to

Quoted 1 line

match all, and an empty string is rejected", between d426430e (pathspec:

warn on

Quoted 2 lines

empty strings as pathspec,
2016-06-22) and 9e4e8a64 (pathspec: die on empty strings as pathspec,

2017-06-

Quoted 1 line

06). I think 9e4e8a64, which happened long after 859b7f1d happened, was

not

Quoted 1 line

careful enough to turn the empty string 859b7f1d added to either a dot or

":/".

Quoted 3 lines

A care should be taken as the definition of "everything" depends on subcommand. For the purpose of "add -p", adding a "." to add everything in

the

Quoted 1 line

current directory is the right thing to do. But for some other commands,

":/" (i.e.

Quoted 1 line

really really everything, even things outside the current subdirectory) is

the right

Quoted 3 lines

choice.

We would break commands in a big way if we get this wrong, so add a handful

of

Quoted 1 line

test pieces to make sure the resulting code still excludes the paths that

are

Quoted 1 line

expected and includes "everything" else.

Thanks for the heads up. I have to check into this for some scripting. Not worried but glad to know.
Thanks,
--Randall