Skip to contentSkip to conversation

pathspec: prevent empty strings as pathspecs

PATCH In review Emily Xie 4 participants 7 messages Jun 19, 2016

lore
Share & tools
lore

Emily Xie

author
#0 lore raw

PATCH 1/1 pathspec: prevent empty strings as pathspecs

For any command that takes a pathspec, passing an empty string will execute the command on all files in the current directory. This results in unexpected behavior. For example, git add "" adds all files to staging, while git rm -rf "" recursively removes all files from the working tree and index. This patch prevents such cases by throwing an error message whenever an empty string is detected as a pathspec.

Thanks-to: Sarah Sharp <[email protected]> and James Sharp <[email protected]>

---
 pathspec.c     | 6 +++++-
 t/t3600-rm.sh  | 4 ++++
 t/t3700-add.sh | 4 ++++
 3 files changed, 13 insertions(+), 1 deletion(-)
pathspec.c +5 -1
index c9e9b6c..11901a2 100644
@@ -402,8 +402,12 @@ void parse_pathspec(struct pathspec *pathspec,
 	}
 
 	n = 0;
-	while (argv[n])
+	while (argv[n]) {
+		if (*argv[n] == '\0') {
+			die("Empty string is not a valid pathspec.");
+		}
 		n++;
+	}
 
 	pathspec->nr = n;
 	ALLOC_ARRAY(pathspec->items, n);
t/t3600-rm.sh +4 -0
index d046d98..1d7dc79 100755
@@ -791,6 +791,10 @@ test_expect_success 'setup for testing rm messages' '
 	git add bar.txt foo.txt
 '
 
+test_expect_success 'rm files with empty string pathspec' '
+  test_must_fail git rm ""
+'
+
 test_expect_success 'rm files with different staged content' '
 	cat >expect <<-\EOF &&
 	error: the following files have staged content different from both the
t/t3700-add.sh +4 -0
index f14a665..49cb080 100755
@@ -207,6 +207,10 @@ test_expect_success POSIXPERM,SANITY 'git add should fail atomically upon an unr
 	! ( git ls-files foo1 | grep foo1 )
 '
 
+test_expect_success 'git add empty string as pathspec must fail' '
+  test_must_fail git add ""
+'
+
 rm -f foo2
 
 test_expect_success POSIXPERM,SANITY 'git add --ignore-errors' '
Signature · 1 line
2.8.4
#1 lore raw
Quoted 8 lines · Emily Xie

For any command that takes a pathspec, passing an empty string will execute the command on all files in the current directory. This results in unexpected behavior. For example, git add "" adds all files to staging, while git rm -rf "" recursively removes all files from the working tree and index. This patch prevents such cases by throwing an error message whenever an empty string is detected as a pathspec.

Quoted 5 lines

Thanks-to: Sarah Sharp <[email protected]> and James Sharp <[email protected]>
---

Overall, lgtm.

The reason for this behavior is that, generally, traversals start at some tree, and if there are no elements in the pathspec, the recursion ends with that tree. Because this is a UX issue, fixing it at the pathspec level seems correct to me.

Quoted 10 lines
 pathspec.c     | 6 +++++-
 t/t3600-rm.sh  | 4 ++++
 t/t3700-add.sh | 4 ++++
 3 files changed, 13 insertions(+), 1 deletion(-)
pathspec.c +0 -0
index c9e9b6c..11901a2 100644

pathspec.c void parse_pathspec(struct pathspec *pathspec, · line 408

Quoted 8 lines
pathspec.c +4 -1
@@ -402,8 +402,12 @@ void parse_pathspec(struct pathspec *pathspec,
 	}
 
 	n = 0;
-	while (argv[n])
+	while (argv[n]) {
+		if (*argv[n] == '\0') {
+			die("Empty string is not a valid pathspec.");
+		}

nit: git style doesn't use {} on one-statement ifs.

Quoted 10 lines
pathspec.c +1 -0
 		n++;
+	}
 
 	pathspec->nr = n;
 	ALLOC_ARRAY(pathspec->items, n);
t/t3600-rm.sh +0 -0
index d046d98..1d7dc79 100755

t/t3600-rm.sh test_expect_success 'setup for testing rm messages' ' · line 797

Quoted 7 lines
t/t3600-rm.sh +4 -0
@@ -791,6 +791,10 @@ test_expect_success 'setup for testing rm messages' '
 	git add bar.txt foo.txt
 '
 
+test_expect_success 'rm files with empty string pathspec' '
+  test_must_fail git rm ""
+'
+

Maybe check the error message here?

#2 lore raw
Quoted 7 lines · David Turner

pathspec.c void parse_pathspec(struct pathspec *pathspec, · line 408

Quoted 4 lines · Emily Xie
pathspec.c +4 -0
+     while (argv[n]) {
+             if (*argv[n] == '\0') {
+                     die("Empty string is not a valid pathspec.");
+             }

nit: git style doesn't use {} on one-statement ifs.

Also, drop the full-stop (period) from the error message and, to be consistent with modern practice, don't capitalize the first word. Finally, the string should be localizable, so wrap it in _(...):

    die(_("empty string not valid pathspec"));
#3 lore raw
In reply to Emily Xie
Quoted 7 lines · Emily Xie

For any command that takes a pathspec, passing an empty string will execute the command on all files in the current directory. This results in unexpected behavior. For example, git add "" adds all files to staging, while git rm -rf "" recursively removes all files from the working tree and index. This patch prevents such cases by throwing an error message whenever an empty string is detected as a pathspec.

This change is likely to break people's existing scripts.

For example, a script may want to inspect some state of the whole working tree and then after the inspection was satisfactory do something that affects paths that are inside the directory you were originally in. You would write something like this:

#!/bin/sh
original=$(git rev-parse --show-prefix)

cd "$(git rev-parse --show-toplevel)"
... do the whole-tree pre-inspection
... using various git commands here

# Then finally do the "action"
git add "$original"

This works even if you happen to be at the top-level of the working tree, but with your patch it suddenly breaks. It may be trivial to update the script to use "." when $original is empty, but that is not an excuse. The important thing is that they need to be told about having to change their script before this change actually happens. Suddenly breaking other people without telling is simply rude and unacceptable.

At least you would need two-step process to introduce a change like this to warn the people whose tools and workflows you are breaking. That is, (1) in one release, you add code to only detect the case you will be changing the behaviour in a later version and give warning messages, and (2) in another release that is several release cycles later, stop warning and actually change the behaviour.

That is, if we assume that passing an empty string by mistake is a problem in the real world that we want to save users from. After all, even though there is _no_ good reason why we _need_ to take 'git add ""' as the same as 'git add .', there is no need not to, either. When a user says 'git add sub/sub/' we would add everything in that directory, 'git add sub/' would add even more, and it is logically consistent for 'git add ""' to add everything. This is a border-line "doctor, if I pass an empty string, it is taken the same way as a dot. -- don't do that then."

A kind of change that satisifies the following criteria:

(1) I can tell that the author of the change really means well.

(2) The problem the change is fixing does not seem to be a huge issue in real life.

(3) The code with the patch would behave is not wrong per-se; it probably is how we would have designed the feature if we knew better.

(4) However, we are not designing things from scratch without existing users, and the change potentially breaks uncountable number of existing users.

is something I really hate having to evaluate. This is one of them.

So, I am of two minds.

I do not mind a two-step breaking of compatibility to address this issue; I would also understand if the author thinks it is not worth the hassle to do so. The sudden behaviour change with this patch alone is however not acceptable, I would think.

#4 lore raw
Quoted 27 lines · Junio C Hamano
Quoted 7 lines · Emily Xie

For any command that takes a pathspec, passing an empty string will execute the command on all files in the current directory. This results in unexpected behavior. For example, git add "" adds all files to staging, while git rm -rf "" recursively removes all files from the working tree and index. This patch prevents such cases by throwing an error message whenever an empty string is detected as a pathspec.

This change is likely to break people's existing scripts.

For example, a script may want to inspect some state of the whole working tree and then after the inspection was satisfactory do something that affects paths that are inside the directory you were originally in. You would write something like this:

#!/bin/sh
original=$(git rev-parse --show-prefix)

cd "$(git rev-parse --show-toplevel)"
... do the whole-tree pre-inspection
... using various git commands here

# Then finally do the "action"
git add "$original"

Quoted 7 lines

This works even if you happen to be at the top-level of the working tree, but with your patch it suddenly breaks. It may be trivial to update the script to use "." when $original is empty, but that is not an excuse. The important thing is that they need to be told about having to change their script before this change actually happens. Suddenly breaking other people without telling is simply rude and unacceptable.

The help for rev-parse --show-prefix says:
"When the command is invoked from a subdirectory, show the path of the current directory relative to the top-level directory."

This doesn't say what it does when invoked at the top-level. Maybe we should just change it to output "./", which would make such scripts work? It would not necessarily fix all possible scripts, but it would fix these. It seem odd to insist on preserving this undocumented behavior of git add/rm.

Quoted 9 lines

At least you would need two-step process to introduce a change like this to warn the people whose tools and workflows you are breaking. That is, (1) in one release, you add code to only detect the case you will be changing the behaviour in a later version and give warning messages, and (2) in another release that is several release cycles later, stop warning and actually change the behaviour.

That is, if we assume that passing an empty string by mistake is a problem in the real world that we want to save users from.

I discovered this when one of our shell-scripts had a variable was accidentally empty. The user I was supporting was very confused. So I think it is a problem in the real world.

#5 lore raw
Quoted 9 lines · David Turner

The help for rev-parse --show-prefix says:
"When the command is invoked from a subdirectory, show the path of the current directory relative to the top-level directory."

This doesn't say what it does when invoked at the top-level. Maybe we should just change it to output "./", which would make such scripts work? It would not necessarily fix all possible scripts, but it would fix these. It seem odd to insist on preserving this undocumented behavior of git add/rm.

For two obvious reasons, you do not want to go there.

As you seem to understand in the above, "--show-prefix" was given merely as an example, so updating its behaviour would not change the fundamental problem with the patch under discussion at all. It will break scripts that knew, documented or not, that pathspec match is a prefix substring match that honors directory boundary and an empty string is defined long time ago to match any path and it does not matter if that definition was made by mistake.

Also, if you change "--show-prefix" output, documented or not, that will break other scripts that knew that checking it against an empty string is a valid way to check if you are already at the top level of the working tree. And I highly suspect that such a change is much harder to advertise and warn existing scripts, compared to the change proposed by the patch under discussion. The output routine in show-prefix knows what it is giving to the caller, but does not know how the caller uses it and what for. Compared to that, ...

Quoted 6 lines
Quoted 6 lines

At least you would need two-step process to introduce a change like this to warn the people whose tools and workflows you are breaking. That is, (1) in one release, you add code to only detect the case you will be changing the behaviour in a later version and give warning messages, and (2) in another release that is several release cycles later, stop warning and actually change the behaviour.

... is a lot easier transition (again, assuming that this change is worth doing in the first place).

author
#6 lore raw

Thanks for the quick responses on this patch!

In response to Junio:

Quoted 11 lines

"At least you would need two-step process to introduce a change like this to warn the people whose tools and workflows you are breaking. That is, (1) in one release, you add code to only detect the case you will be changing the behaviour in a later version and give warning messages, and (2) in another release that is several release cycles later, stop warning and actually change the behaviour....

I do not mind a two-step breaking of compatibility to address this issue; I would also understand if the author thinks it is not worth the hassle to do so. The sudden behaviour change with this patch alone is however not acceptable, I would think."

I understand your hesitance with the original method. If the agreed solution is a two-step implementation, I think it would definitely be worth my time and hassle----in part because I'm particularly excited about the prospect of contributing to Git, but mostly because I do believe that it would be a good improvement.

Given this, I'll edit the patch and re-submit to only emit warning messages for now.

- Emily