Skip to contentSkip to conversation

t3703: add test cases for pathspec magic

PATCH In review Nguyễn Thái Ngọc Duy 5 participants 28 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 t3703: add test cases for pathspec magic

That was the intention, but it raises a question: what do we do if a file happens to have the same name with the given magic pathspec, as in the last two tests? People can fall into these cases with "git add *" or something like that.

I'm tempted to reuse "--" the second time to say "no more magic from here". But there may be better way to deal with it.

Also, long magic must be quoted or at least bash will complain. Is there any other syntax option that can be used directly without quoting?

---
 t/t3703-add-magic-pathspec.sh |   68 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 68 insertions(+), 0 deletions(-)
 create mode 100755 t/t3703-add-magic-pathspec.sh
t/t3703-add-magic-pathspec.sh new file +68 -0
new file mode 100755
index 0000000..76108ae
@@ -0,0 +1,68 @@
+#!/bin/sh
+
+test_description='magic pathspec tests using git-add'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	mkdir sub anothersub &&
+	: >sub/foo &&
+	: >anothersub/foo
+'
+
+test_expect_success 'colon alone magic can only used alone' '
+	test_must_fail git add -n sub/foo : &&
+	test_must_fail git add -n : sub/foo
+'
+
+cat >expected <<EOF
+add 'anothersub/foo'
+add 'expected'
+add 'sub/actual'
+add 'sub/foo'
+EOF
+
+test_expect_success 'add :' '
+	(cd sub && git add -n : >actual) &&
+	test_cmp expected sub/actual
+'
+
+test_expect_success 'add :/' '
+	(cd sub && git add -n :/ >actual) &&
+	test_cmp expected sub/actual
+'
+
+cat >expected <<EOF
+add 'anothersub/foo'
+EOF
+
+test_expect_success 'add :/anothersub' '
+	(cd sub && git add -n :/anothersub >actual) &&
+	test_cmp expected sub/actual
+'
+
+test_expect_success 'add :/non-existent' '
+	(cd sub && test_must_fail git add -n :/non-existent)
+'
+
+cat >expected <<EOF
+add 'sub/foo'
+EOF
+
+test_expect_success 'add :(icase)foo' '
+	(cd sub && git add -n ":(icase)FoO" >actual) &&
+	test_cmp expected sub/actual
+'
+
+test_expect_success 'a file with the same (long) magic name exists' '
+	: >":(case)ha" &&
+	git add -n ":(icase)ha"
+'
+
+test_expect_success 'a file with the same (short) magic name exists' '
+	mkdir ":" &&
+	: >":/bar" &&
+	git add -n :/bar
+'
+
+test_done
Signature · 1 line
1.7.4.74.g639db
#1 lore raw

PATCH 1/1 t3703, t4208: add test cases for magic pathspec

While at it, also document ":" syntax.

---
v2.

 Documentation/glossary-content.txt |    3 +
 t/t3703-add-magic-pathspec.sh      |   78 ++++++++++++++++++++++++++++++++++++
 t/t4208-log-magic-pathspec.sh      |   40 ++++++++++++++++++
 3 files changed, 121 insertions(+), 0 deletions(-)
 create mode 100755 t/t3703-add-magic-pathspec.sh
 create mode 100755 t/t4208-log-magic-pathspec.sh
Documentation/glossary-content.txt +3 -0
index 0ca029b..02cea08 100644
@@ -311,6 +311,9 @@ parenthesis `(`, a comma-separated list of zero or more "magic words",
 and a close parentheses `)`, and the remainder is the pattern to match
 against the path.
 +
+A pathspec with only a colon means "there is no pathspec". This form
+cannot be combined with other pathspec.
++
 The "magic signature" consists of an ASCII symbol that is not
 alphanumeric.
 +
t/t3703-add-magic-pathspec.sh new file +78 -0

78 lines not on this page · raw message

t/t4208-log-magic-pathspec.sh new file +40 -0
new file mode 100755
index 0000000..b296a74
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+test_description='magic pathspec tests using git-log'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	test_commit initial &&
+	test_tick &&
+	git commit --allow-empty -m empty &&
+	mkdir sub
+'
+
+test_expect_failure 'git log :/ ambiguous with [ref]:/path' '
+	test_must_fail git log :/ 2>error &&
+	grep ambiguous error
+'
+
+test_expect_failure 'git log :' '
+	git log :
+'
+
+test_expect_success 'git log HEAD -- :/' '
+	cat >expected <<EOF &&
+24b24cf initial
+EOF
+	(cd sub && git log --oneline HEAD -- :/ >../actual) &&
+	test_cmp expected actual
+'
+
+test_expect_success 'git log HEAD -- :' '
+	cat >expected <<EOF &&
+41d179c empty
+24b24cf initial
+EOF
+	(cd sub && git log --oneline HEAD -- : >../actual) &&
+	test_cmp expected actual
+'
+
+test_done
Signature · 1 line
1.7.4.74.g639db
#2 lore raw

Re: [PATCH] t3703, t4208: add test cases for magic pathspec

Quoted 12 lines · Nguyễn Thái Ngọc Duy
t/t3703-add-magic-pathspec.sh new file +6 -0
new file mode 100755
index 0000000..3d8c6b8
@@ -0,0 +1,78 @@
+#!/bin/sh
+
+test_description='magic pathspec tests using git-add'
+
+. ./test-lib.sh
+

t/t3703-add-magic-pathspec.sh line 16

Quoted 10 lines
t/t3703-add-magic-pathspec.sh new file +10 -0
+test_expect_success 'setup' '
+	mkdir sub anothersub &&
+	: >sub/foo &&
+	: >anothersub/foo
+'
+
+test_expect_failure 'colon alone magic can only used alone' '
+	test_must_fail git add -n sub/foo : &&
+	test_must_fail git add -n : sub/foo
+'

I don't care too much about this case (it is a user error), but if you promise you will turn this expect-failure to expect-success in a follow-up patch, why not ;-)?

t/t3703-add-magic-pathspec.sh line 28

Quoted 11 lines
t/t3703-add-magic-pathspec.sh new file +11 -0
+cat >expected <<EOF
+add 'anothersub/foo'
+add 'expected'
+add 'sub/actual'
+add 'sub/foo'
+EOF
+
+test_expect_success 'add :' '
+	(cd sub && git add -n : >actual) &&
+	test_cmp expected sub/actual
+'

Shouldn't

	$ git anycmd :

be equivalent to

	$ (cd $(git rev-parse --show-cdup)/. && git anycmd)

for any command? I doubt this test is expecting the right outcome. Shouldn't it result in "Nothing specified, nothing added."?

t/t3703-add-magic-pathspec.sh line 33

Quoted 4 lines
t/t3703-add-magic-pathspec.sh new file +4 -0
+test_expect_success 'add :/' '
+	(cd sub && git add -n :/ >actual) &&
+	test_cmp expected sub/actual
+'

This one is expecting the right thing.

t/t3703-add-magic-pathspec.sh line 46

Quoted 3 lines
t/t3703-add-magic-pathspec.sh new file +3 -0
+test_expect_success 'add :/non-existent' '
+	(cd sub && test_must_fail git add -n :/non-existent)
+'

Just being curious. What should the error message say? Can we make it to say "fatal: pathspec 'non-ex' from root did not match any files"?

t/t3703-add-magic-pathspec.sh line 65

Quoted 3 lines
t/t3703-add-magic-pathspec.sh new file +3 -0
+cat >expected <<EOF
+fatal: pathspec ':(icase)ha' did not match any files
+EOF

When writing a literal, make it a habit to quote EOF to reduce mental load on the reader, like this:

	cat >expected <<\EOF
        ...
        EOF

so that the reader does not have to scan for $var in the text to make sure.

t/t3703-add-magic-pathspec.sh line 69

Quoted 3 lines
t/t3703-add-magic-pathspec.sh new file +3 -0
+test_expect_failure 'show pathspecs exactly what are typed in' '
+	test_cmp expected error
+'

Will this break under gettext-poison?

Quoted 16 lines
t/t4208-log-magic-pathspec.sh new file +10 -0
new file mode 100755
index 0000000..b296a74
@@ -0,0 +1,40 @@
+#!/bin/sh
+
+test_description='magic pathspec tests using git-log'
+
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	test_commit initial &&
+	test_tick &&
+	git commit --allow-empty -m empty &&

t/t4208-log-magic-pathspec.sh line 20

Quoted 10 lines
t/t4208-log-magic-pathspec.sh new file +10 -0
+	mkdir sub
+'
+
+test_expect_failure 'git log :/ ambiguous with [ref]:/path' '
+	test_must_fail git log :/ 2>error &&
+	grep ambiguous error
+'
+test_expect_failure 'git log :' '
+	git log :
+'

These two should expect exactly the same error, I think. ':', ':/' or anything magic will not satisify verify_filename(), and needs a double-dash before it.

We could improve the disambiguation heuristics so that when we do not have a '--' on the command line:

- make sure all the earlier ones are refs and they cannot be a path on the filesystem (otherwise we need a disambiguator "--").

- the first non-ref argument and everything that follows must be either a ':' magic, a string with globbing character, or a path on the filesystem, and none of them can be a ref.

Do you want to take a stab at it?

#26 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 3 lines · Nguyễn Thái Ngọc Duy

That was the intention, but it raises a question: what do we do if a file happens to have the same name with the given magic pathspec, as in the last two tests?

I would have expected that in such a case the user would pass a "\:" or even ":::" to match the file ":", and "\:(rubbish)" to match the file ":(rubbish)". The whole ":" is special thing comes from the observation that a path that begins with a colon is rare, so it is Ok to require the user to do some more work (typing an extra backslash) when he really wants to match with such a thing.

A script that takes a pathname that is meant to be a literal from the user in its variable $x would pass ":(noglob)$x" when it wants to be strict. A script that lets the user say whatever and wants to pass would just pass "$x" along the callchain.

I do not expect this to be an issue in practice, though. Have you seen a script that tries to quote all the possible globbing characters in "$x" before calling into git with the current codebase without this magic?

#27 lore raw

2011/5/8 Junio C Hamano <[email protected]>:

Quoted 13 lines
Quoted 3 lines · Nguyễn Thái Ngọc Duy

That was the intention, but it raises a question: what do we do if a file happens to have the same name with the given magic pathspec, as in the last two tests?

I would have expected that in such a case the user would pass a "\:" or even ":::" to match the file ":", and "\:(rubbish)" to match the file ":(rubbish)".  The whole ":" is special thing comes from the observation that a path that begins with a colon is rare, so it is Ok to require the user to do some more work (typing an extra backslash) when he really wants to match with such a thing.

Quoted 8 lines

A script that takes a pathname that is meant to be a literal from the user in its variable $x would pass ":(noglob)$x" when it wants to be strict.  A script that lets the user say whatever and wants to pass would just pass "$x" along the callchain.

I do not expect this to be an issue in practice, though.  Have you seen a script that tries to quote all the possible globbing characters in "$x" before calling into git with the current codebase without this magic?

Point taken.

Signature · 1 line
Duy
#3 lore raw

Re: [PATCH] t3703, t4208: add test cases for magic pathspec

In reply to Junio C Hamano

2011/5/9 Junio C Hamano <[email protected]>:

Quoted 8 lines
Quoted 4 lines

+test_expect_failure 'colon alone magic can only used alone' '
+     test_must_fail git add -n sub/foo : &&
+     test_must_fail git add -n : sub/foo
+'

I don't care too much about this case (it is a user error), but if you promise you will turn this expect-failure to expect-success in a follow-up patch, why not ;-)?

failed tests are (annoying enough) to be fixed, right? :)

Quoted 14 lines

t/t3703-add-magic-pathspec.sh line 28

Quoted 11 lines
t/t3703-add-magic-pathspec.sh new file +11 -0
+cat >expected <<EOF
+add 'anothersub/foo'
+add 'expected'
+add 'sub/actual'
+add 'sub/foo'
+EOF
+
+test_expect_success 'add :' '
+     (cd sub && git add -n : >actual) &&
+     test_cmp expected sub/actual
+'

Shouldn't

Quoted 8 lines
       $ git anycmd :

be equivalent to

       $ (cd $(git rev-parse --show-cdup)/. && git anycmd)

for any command?  I doubt this test is expecting the right outcome. Shouldn't it result in "Nothing specified, nothing added."?

It's gray area. Yes I'd rather see that behavior, but then a lot of code needs to be audited. Perhaps we should delay introducing ":" until get_pathspec() learns to modify argc. IOW die() for now when users write ":".

Quoted 6 lines
Quoted 3 lines

+test_expect_success 'add :/non-existent' '
+     (cd sub && test_must_fail git add -n :/non-existent) +'

Just being curious. What should the error message say?  Can we make it to say "fatal: pathspec 'non-ex' from root did not match any files"?

My opinion is in the next test. It should show exactly what users type in, ie. "pathspec ':/non-existent' did not match any files". "'non-ex' from root" is nice when there's only one magic. If users specify a few more in one pathspec, the text may become too verbose.

Quoted 5 lines
Quoted 3 lines

+test_expect_failure 'show pathspecs exactly what are typed in' '
+     test_cmp expected error
+'

Will this break under gettext-poison?

Never really known what that poison is. Will grep ":/non-existent" instead.

Quoted 11 lines

t/t4208-log-magic-pathspec.sh line 21

Quoted 7 lines
t/t4208-log-magic-pathspec.sh new file +7 -0
+test_expect_failure 'git log :/ ambiguous with [ref]:/path' '
+     test_must_fail git log :/ 2>error &&
+     grep ambiguous error
+'
+test_expect_failure 'git log :' '
+     git log :
+'

These two should expect exactly the same error, I think. ':', ':/' or anything magic will not satisify verify_filename(), and needs a double-dash before it.

Yes. Error from the latter "fatal: Path '' does not exist (neither on disk nor in the index)." makes me wonder, why does log accept a sha1 syntax that may resolve to non-commit object?

Quoted 11 lines

We could improve the disambiguation heuristics so that when we do not have a '--' on the command line:

 - make sure all the earlier ones are refs and they cannot be a path on the filesystem (otherwise we need a disambiguator "--").

 - the first non-ref argument and everything that follows must be either a ':' magic, a string with globbing character, or a path on the filesystem, and none of them can be a ref.

Do you want to take a stab at it?

A bit busy these days. If you're interested, go ahead.

Signature · 1 line
Duy
#4 lore raw

Re: [PATCH] t3703, t4208: add test cases for magic pathspec

Quoted 11 lines · Nguyen Thai Ngoc Duy

2011/5/9 Junio C Hamano <[email protected]>:

Quoted 8 lines
Quoted 4 lines

+test_expect_failure 'colon alone magic can only used alone' '
+     test_must_fail git add -n sub/foo : &&
+     test_must_fail git add -n : sub/foo
+'

I don't care too much about this case (it is a user error), but if you promise you will turn this expect-failure to expect-success in a follow-up patch, why not ;-)?

failed tests are (annoying enough) to be fixed, right? :)

Didn't I say it is not my itch already?

#5 lore raw

Re: [PATCH] t3703, t4208: add test cases for magic pathspec

In reply to Nguyen Thai Ngoc Duy
Quoted 4 lines · Nguyen Thai Ngoc Duy
Quoted 4 lines

Shouldn't

       $ git anycmd :
Quoted 8 lines
Quoted 6 lines

be equivalent to

       $ (cd $(git rev-parse --show-cdup)/. && git anycmd)

for any command?  I doubt this test is expecting the right outcome. Shouldn't it result in "Nothing specified, nothing added."?

It's gray area.

I do not see anything gray in it.

"git add :" in subdirectory is simply broken because we have used "No remaining command line argument means no pathspec" when deciding to stop and say "Nothing specified, nothing added". That was a sensible assumption back then, but it needs to be updated to take the new "The remaining command line argument may say that there is no pathspec" reality into account.

#6 lore raw

cover magic pathspec updates

In reply to Nguyễn Thái Ngọc Duy

This is a series to update the magic pathspec topic, primarily the area around the empty ':' pathspec.

The failing tests from Duy's are mostly due to the fact that this round is not converting the codebase fully and still use char **pathspec in places.

As get_pathspec() gives only a munged string for pattern matching, while losing the original pathspec information such as up to which point in the resulting string came from the prefix (hence it must match literally even under icase) or what the original string looked like, there is no way for these tests, especially the ones to check failure mode and the information in the error report, to pass.

As NEEDSWORK comment in prefix_pathspec() says, all the callers need to be converted to use "struct pathspec" interface, so that they can preserve the original information for error reporting purposes (and also to pass to external subprocesses --- see how pathspec is used to spawn the "add -i" helper for example).

Other than that, I think the current code is probably more or less safe to dogfood with known rough edges.

Junio C Hamano (8):
grep: use get_pathspec() correctly get_pathspec(): an empty ':' pathspec should stand alone count_pathspec(): return number of elements in pathspec add ":" is a pathspec that is too wide
git rm ":" is like specifying nothing
clean ":" is like specifying nothing
mv ":" ":" is like moving nothing from nowhere to nowhere checkout ":" is not giving any pathspec

Nguyễn Thái Ngọc Duy (1):
t3703, t4208: add test cases for magic pathspec

 Documentation/glossary-content.txt |    3 +
 builtin/add.c                      |    4 +-
 builtin/checkout.c                 |    9 +---
 builtin/clean.c                    |    1 +
 builtin/grep.c                     |    8 +---
 builtin/mv.c                       |    6 ++-
 builtin/rm.c                       |    4 +-
 cache.h                            |    3 +-
 setup.c                            |   18 ++++++++-
 t/t3703-add-magic-pathspec.sh      |   78 ++++++++++++++++++++++++++++++++++++
 t/t4208-log-magic-pathspec.sh      |   45 +++++++++++++++++++++
 11 files changed, 160 insertions(+), 19 deletions(-)
 create mode 100755 t/t3703-add-magic-pathspec.sh
 create mode 100755 t/t4208-log-magic-pathspec.sh
Signature · 1 line
1.7.5.1.290.g1b565
#7 lore raw

PATCH 1/9 grep: use get_pathspec() correctly

When there is no remaining string in argv, get_pathspec(prefix, argv) will return a two-element array that has prefix as the first element, so there is no need to re-roll that logic in the code that uses get_pathspec().

---
 builtin/grep.c |    8 +-------
 1 files changed, 1 insertions(+), 7 deletions(-)
builtin/grep.c +1 -7
index 0bf8c01..222dd6d 100644
@@ -956,13 +956,7 @@ int cmd_grep(int argc, const char **argv, const char *prefix)
 			verify_filename(prefix, argv[j]);
 	}
 
-	if (i < argc)
-		paths = get_pathspec(prefix, argv + i);
-	else if (prefix) {
-		paths = xcalloc(2, sizeof(const char *));
-		paths[0] = prefix;
-		paths[1] = NULL;
-	}
+	paths = get_pathspec(prefix, argv + i);
 	init_pathspec(&pathspec, paths);
 	pathspec.max_depth = opt.max_depth;
 	pathspec.recursive = 1;
Signature · 1 line
1.7.5.1.290.g1b565
#8 lore raw

PATCH 2/9 get_pathspec(): an empty ':' pathspec should stand alone

In reply to Junio C Hamano

"git cmd foo :" is a user error; diagnose it as such.

---
 setup.c |    8 +++++++-
 1 files changed, 7 insertions(+), 1 deletions(-)
setup.c +7 -1
index 51e354c..c1be388 100644
@@ -256,6 +256,7 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
 	const char *entry = *pathspec;
 	const char **src, **dst;
 	int prefixlen;
+	int has_root_widen = 0;
 
 	if (!prefix && !entry)
 		return NULL;
@@ -272,10 +273,15 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
 	dst = pathspec;
 	prefixlen = prefix ? strlen(prefix) : 0;
 	while (*src) {
-		*(dst++) = prefix_pathspec(prefix, prefixlen, *src);
+		const char *elem = prefix_pathspec(prefix, prefixlen, *src);
+		*(dst++) = elem;
+		if (!elem)
+			has_root_widen = 1;
 		src++;
 	}
 	*dst = NULL;
+	if (has_root_widen && src != pathspec + 1)
+		die("an empty ':' pathspec with other pathspecs");
 	if (!*pathspec)
 		return NULL;
 	return pathspec;
Signature · 1 line
1.7.5.1.290.g1b565
#9 lore raw

PATCH 3/9 count_pathspec(): return number of elements in pathspec

In reply to Junio C Hamano

When fed a non-empty argv[], get_pathspec(prefix, argv) used to always return an array of string that has the same number of elements as argv[] had, but with ":" (work at toplevel without any path limit) magic pathspec it can return zero elements. Passing the result from get_pathspec() to this function will always give the correct number of pathspec elements.

---
 cache.h |    3 ++-
 setup.c |   10 ++++++++++
 2 files changed, 12 insertions(+), 1 deletions(-)
cache.h +2 -1
index be6ce72..6170dce 100644
@@ -425,7 +425,8 @@ extern void set_git_work_tree(const char *tree);
 
 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
 
-extern const char **get_pathspec(const char *prefix, const char **pathspec);
+extern const char **get_pathspec(const char *prefix, const char **argv);
+extern int count_pathspec(const char **pathspec);
 extern void setup_work_tree(void);
 extern const char *setup_git_directory_gently(int *);
 extern const char *setup_git_directory(void);
setup.c +10 -0
index c1be388..cab9ddd 100644
@@ -287,6 +287,16 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
 	return pathspec;
 }
 
+int count_pathspec(const char **pathspec)
+{
+	int i;
+	if (!pathspec)
+		return 0;
+	for (i = 0; pathspec[i]; i++)
+		; /* just counting */
+	return i;
+}
+
 /*
  * Test if it looks like we're at a git directory.
  * We want to see:
Signature · 1 line
1.7.5.1.290.g1b565
#11 lore raw

PATCH 4/9 add ":" is a pathspec that is too wide

In reply to Junio C Hamano

"git cmd :" should be exactly the same as "git cmd" run from the root level of the working tree. "git add" without any pathspec should not add anything.

---
 builtin/add.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
builtin/add.c +2 -2
index e127d5a..43a8aad 100644
@@ -406,12 +406,12 @@ int cmd_add(int argc, const char **argv, const char *prefix)
 		 (!(addremove || take_worktree_changes)
 		  ? ADD_CACHE_IGNORE_REMOVAL : 0));
 
-	if (require_pathspec && argc == 0) {
+	pathspec = validate_pathspec(argc, argv, prefix);
+	if (require_pathspec && (!argc || !pathspec)) {
 		fprintf(stderr, "Nothing specified, nothing added.\n");
 		fprintf(stderr, "Maybe you wanted to say 'git add .'?\n");
 		return 0;
 	}
-	pathspec = validate_pathspec(argc, argv, prefix);
 
 	if (read_cache() < 0)
 		die("index file corrupt");
Signature · 1 line
1.7.5.1.290.g1b565
#12 lore raw

PATCH 5/9 git rm ":" is like specifying nothing

In reply to Junio C Hamano

"git cmd :" should be exactly the same as running "git cmd" from the root level of the working tree. Do not decide solely on the value of argc (i.e. remaining parameters after options and revs are parsed) to see if the user limited the operation with paths.

---
 builtin/rm.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
builtin/rm.c +3 -1
index ff491d7..c31f915 100644
@@ -162,11 +162,13 @@ int cmd_rm(int argc, const char **argv, const char *prefix)
 		die("index file corrupt");
 
 	pathspec = get_pathspec(prefix, argv);
+	if (!pathspec)
+		die("removing nothing?");
 	refresh_index(&the_index, REFRESH_QUIET, pathspec, NULL, NULL);
 
 	seen = NULL;
 	for (i = 0; pathspec[i] ; i++)
-		/* nothing */;
+		; /* nothing */
 	seen = xcalloc(i, 1);
 
 	for (i = 0; i < active_nr; i++) {
Signature · 1 line
1.7.5.1.290.g1b565
#13 lore raw

PATCH 6/9 clean ":" is like specifying nothing

In reply to Junio C Hamano

Adjust argc that is meant to correspond do the number of elements in the pathspec array.

---
 builtin/clean.c |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
builtin/clean.c +1 -0
index 4a312ab..686cd6b 100644
@@ -96,6 +96,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
 		add_exclude(exclude_list.items[i].string, "", 0, dir.exclude_list);
 
 	pathspec = get_pathspec(prefix, argv);
+	argc = count_pathspec(pathspec);
 
 	fill_directory(&dir, pathspec);
 
Signature · 1 line
1.7.5.1.290.g1b565
#15 lore raw

PATCH 7/9 mv ":" ":" is like moving nothing from nowhere to nowhere

In reply to Junio C Hamano

Make sure the code can deal with get_pathspec() that returns nothing. Strictly speaking it may be a user error, but that is not an excuse to dump core.

---
 builtin/mv.c |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
builtin/mv.c +5 -1
index 93e8995..38af9f0 100644
@@ -77,8 +77,12 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
 		die("index file corrupt");
 
 	source = copy_pathspec(prefix, argv, argc, 0);
-	modes = xcalloc(argc, sizeof(enum update_mode));
+	if (!source)
+		die("copying from nowhere?");
+	modes = xcalloc(count_pathspec(source), sizeof(enum update_mode));
 	dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
+	if (!dest_path)
+		die("copying to nowhere?");
 
 	if (dest_path[0][0] == '\0')
 		/* special case: "." was normalized to "" */
Signature · 1 line
1.7.5.1.290.g1b565
#17 lore raw

PATCH 8/9 checkout ":" is not giving any pathspec

In reply to Junio C Hamano

"git cmd :" should be exactly the same as running "git cmd" from the root level of the working tree. Do not decide solely on the value of argc (i.e. remaining parameters after options and revs are parsed), but make sure we actually do have pathspec to switch operation modes.

---
 builtin/checkout.c |    9 +++------
 1 files changed, 3 insertions(+), 6 deletions(-)
builtin/checkout.c +3 -6
index 2bf02f2..fe46725 100644
@@ -890,6 +890,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	char *conflict_style = NULL;
 	int patch_mode = 0;
 	int dwim_new_local_branch = 1;
+	const char **pathspec;
 	struct option options[] = {
 		OPT__QUIET(&opts.quiet, "suppress progress reporting"),
 		OPT_STRING('b', NULL, &opts.new_branch, "branch",
@@ -1004,12 +1005,8 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)
 	if (opts.track == BRANCH_TRACK_UNSPECIFIED)
 		opts.track = git_branch_track;
 
-	if (argc) {
-		const char **pathspec = get_pathspec(prefix, argv);
-
-		if (!pathspec)
-			die("invalid path specification");
-
+	pathspec = get_pathspec(prefix, argv);
+	if (argc && pathspec) {
 		if (patch_mode)
 			return interactive_checkout(new.name, pathspec, &opts);
 
Signature · 1 line
1.7.5.1.290.g1b565
#18 lore raw

PATCH 9/9 t3703, t4208: add test cases for magic pathspec

In reply to Junio C Hamano

From: Nguyễn Thái Ngọc Duy <[email protected]>

While at it, also document ":" syntax.

---
 Documentation/glossary-content.txt |    3 +
 t/t3703-add-magic-pathspec.sh      |   78 ++++++++++++++++++++++++++++++++++++
 t/t4208-log-magic-pathspec.sh      |   45 +++++++++++++++++++++
 3 files changed, 126 insertions(+), 0 deletions(-)
 create mode 100755 t/t3703-add-magic-pathspec.sh
 create mode 100755 t/t4208-log-magic-pathspec.sh
Documentation/glossary-content.txt +3 -0
index 0ca029b..0318024 100644
@@ -327,6 +327,9 @@ icase;;
 +
 It is envisioned that we will support more types of magic in later
 versions of git.
++
+A pathspec with only a colon means "there is no pathspec". This form
+should not be combined with other pathspec.
 
 [[def_parent]]parent::
 	A <<def_commit_object,commit object>> contains a (possibly empty) list
t/t3703-add-magic-pathspec.sh new file +78 -0

78 lines not on this page · raw message

t/t4208-log-magic-pathspec.sh new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
1.7.5.1.290.g1b565
#10 lore raw

Re: [PATCH 3/9] count_pathspec(): return number of elements in pathspec

In reply to Junio C Hamano
Quoted 5 lines · Junio C Hamano

When fed a non-empty argv[], get_pathspec(prefix, argv) used to always return an array of string that has the same number of elements as argv[] had, but with ":" (work at toplevel without any path limit) magic pathspec it can return zero elements.  Passing the result from get_pathspec() to this function will always give the correct number of pathspec elements.

And use the new function in builtin/rm.c too? A good cleanup while you're touching cmd_rm()..

Signature · 1 line
Duy
#14 lore raw

Re: [PATCH 6/9] clean ":" is like specifying nothing

In reply to Junio C Hamano
Quoted 1 line · Junio C Hamano

Adjust argc that is meant to correspond do the number of elements in

s/do/to ?

Quoted 27 lines

the pathspec array.

---
 builtin/clean.c |    1 +

 1 files changed, 1 insertions(+), 0 deletions(-)
builtin/clean.c +1 -1
index 4a312ab..686cd6b 100644
@@ -96,6 +96,7 @@ int cmd_clean(int argc, const char **argv, const char *prefix)
                add_exclude(exclude_list.items[i].string, "", 0, dir.exclude_list);
 
        pathspec = get_pathspec(prefix, argv);
+       argc = count_pathspec(pathspec);
 
        fill_directory(&dir, pathspec);
 
--

1.7.5.1.290.g1b565

--
To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

#16 lore raw

Re: [PATCH 7/9] mv ":" ":" is like moving nothing from nowhere to nowhere

In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano
builtin/mv.c +0 -0
index 93e8995..38af9f0 100644
@@ -77,8 +77,12 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
                die("index file corrupt");

builtin/mv.c int cmd_mv(int argc, const char **argv, const char *prefix) · line 86

Quoted 10 lines
builtin/mv.c +5 -1
 
        source = copy_pathspec(prefix, argv, argc, 0);
-       modes = xcalloc(argc, sizeof(enum update_mode));
+       if (!source)
+               die("copying from nowhere?");
+       modes = xcalloc(count_pathspec(source), sizeof(enum update_mode));
        dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
+       if (!dest_path)
+               die("copying to nowhere?");
 

This command is interesting. Note to self: adding support for "git mv '*.c' to/there".

I think we could take get_pathspec() out of copy_pathspec() and call it just once (in case get_pathspec() or its successor does fancy things), something roughly like this

builtin/mv.c +0 -0
index 40f33ca..a032789 100644
@@ -32,7 +32,7 @@ static const char **copy_pathspec(const char

*prefix, const char **pathspec,
result[i] = base_name ? strdup(basename(it)) : it;
}
}
- return get_pathspec(prefix, result);
+ return result;
}

static const char *add_slash(const char *path)
@@ -60,7 +60,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
OPT_BOOLEAN('k', NULL, &ignore_errors, "skip move/rename errors"), OPT_END(),
};
- const char **source, **destination, **dest_path;
+ const char **pathspec, **source, **destination, **dest_path;
enum update_mode { BOTH = 0, WORKING_DIRECTORY, INDEX } *modes; struct stat st;
struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
@@ -69,16 +69,20 @@ int cmd_mv(int argc, const char **argv, const char *prefix)

argc = parse_options(argc, argv, prefix, builtin_mv_options,
builtin_mv_usage, 0);
- if (--argc < 1)
+
+ pathspec = get_pathspec(prefix, argv);
+ argc = count_pathspec(pathspec);
+
+ if (--argc < 1 || !pathspec)
usage_with_options(builtin_mv_usage, builtin_mv_options);

newfd = hold_locked_index(&lock_file, 1);
if (read_cache() < 0)
die(_("index file corrupt"));

- source = copy_pathspec(prefix, argv, argc, 0);
+ source = copy_pathspec(prefix, pathspec, argc, 0);
modes = xcalloc(argc, sizeof(enum update_mode));
- dest_path = copy_pathspec(prefix, argv + argc, 1, 0);
+ dest_path = copy_pathspec(prefix, pathspec + argc, 1, 0);

if (dest_path[0][0] == '\0')
/* special case: "." was normalized to "" */

Signature · 1 line
Duy
#19 lore raw

PATCH 1/1 t3703: Skip tests using directory name ":" on Windows

In reply to Junio C Hamano

From: Johannes Sixt <[email protected]>

":" is not allowed in file names on Windows. Detect this case and skip a test if necessary.

---
 t/t3703-add-magic-pathspec.sh |    8 ++++++--
 1 files changed, 6 insertions(+), 2 deletions(-)
t/t3703-add-magic-pathspec.sh +6 -2
index ce5585e..e508246 100755
@@ -44,8 +44,12 @@ test_expect_success 'a file with the same (long) magic name exists' '
 	git add -n "./:(icase)ha"
 '
 
-test_expect_success 'a file with the same (short) magic name exists' '
-	mkdir ":" &&
+if mkdir ":" 2>/dev/null
+then
+	test_set_prereq COLON_DIR
+fi
+
+test_expect_success COLON_DIR 'a file with the same (short) magic name exists' '
 	: >":/bar" &&
 	test_must_fail git add -n :/bar &&
 	git add -n "./:/bar"
Signature · 1 line
1.7.5.1.1644.g7f2ed
#23 lore raw

Re: [PATCH 0/9] magic pathspec updates

In reply to Junio C Hamano
Quoted 2 lines · Junio C Hamano

This is a series to update the magic pathspec topic, primarily the area around the empty ':' pathspec.

I think you missed git-commit and git-reset. "git commit --include :" may get past parse_and_validate_options() (ie. not dying) but later on in prepare_index(), there's no pathspec. I did not check very careful though.

Quoted 2 lines

Other than that, I think the current code is probably more or less safe to dogfood with known rough edges.

Agreed.

Signature · 1 line
Duy
#24 lore raw

Re: [PATCH 0/9] magic pathspec updates

Quoted 9 lines · Nguyen Thai Ngoc Duy
Quoted 2 lines · Junio C Hamano

This is a series to update the magic pathspec topic, primarily the area around the empty ':' pathspec.

I think you missed git-commit and git-reset. "git commit --include :" may get past parse_and_validate_options() (ie. not dying) but later on in prepare_index(), there's no pathspec. I did not check very careful though.

Quoted 4 lines
Quoted 2 lines

Other than that, I think the current code is probably more or less safe to dogfood with known rough edges.

Agreed.

After re-reading the series one more time (including the ones that are already in next), I think teaching get_pathspec() about a lone ":" was probably a bad idea. The only reason we might want to say "there is no pathspec whatsoever" is when a command wants to limit its operation to the current subdirectory and it changes behaviour when run at the root level with and without pathspec (e.g. history simplification).

Note that no such command exists in our vocabulary today, so you need to imagine "git local-log" that acts like "git log" but by default shows history simplified to explain only the subdirectory you are currently in, or something.

Such an application can easily notice that argv[] has only a lone ":" left and do the same thing it does when there is no pathspec, without affecting other (existing) users of get_pathspec().

But the thing is, we do not have such an application like "local-log" now, we know we can easily support such commands when needed without teaching get_pathspec() about a lone ":", and we can be sure that existing commands that know get_pathspec() will never give them an empty pathspec if they feed a non-empty argv[] will not be broken by not teaching get_pathspec() about a lone ":".

I am leaning toward ripping the lone ":" support from the code in "next". I would also remove ":(icase)" from "next". It was only meant to be a POC to show how far we could go only by futzing get_pathspec() function, and was never meant to be a serious implementation of the feature. It should be re-done after we do deeper conversion and use the "struct pathspec" interface not "char **" interface.

#25 lore raw

Re: [PATCH 0/9] magic pathspec updates

Quoted 8 lines · Junio C Hamano

Note that no such command exists in our vocabulary today, so you need to imagine "git local-log" that acts like "git log" but by default shows history simplified to explain only the subdirectory you are currently in, or something.

Such an application can easily notice that argv[] has only a lone ":" left and do the same thing it does when there is no pathspec, without affecting other (existing) users of get_pathspec().

Hmm.. if ":" (the semantics) is only used in few commands, perhaps an option would be better than the cryptic ":".

Quoted 6 lines

I am leaning toward ripping the lone ":" support from the code in "next". I would also remove ":(icase)" from "next". It was only meant to be a POC to show how far we could go only by futzing get_pathspec() function, and was never meant to be a serious implementation of the feature. It should be re-done after we do deeper conversion and use the "struct pathspec" interface not "char **" interface.

OK. But reserve the lone ":" syntax. It's precious. We may have a use case for it some day.

Signature · 1 line
Duy
#20 lore raw

Re: [PATCH 9/9] t3703, t4208: add test cases for magic pathspec

In reply to Junio C Hamano
Quoted 10 lines · Junio C Hamano

+test_expect_success 'add :/' "
+       cat >expected <<-EOF &&
+       add 'anothersub/foo'
+       add 'expected'
+       add 'sub/actual'
+       add 'sub/foo'
+       EOF
+       (cd sub && git add -n :/ >actual) &&
+       test_cmp expected sub/actual
+"

This needs to use test_i18ncmp.

#21 lore raw

Re: [PATCH 9/9] t3703, t4208: add test cases for magic pathspec

Quoted 13 lines · Ævar Arnfjörð Bjarmason
Quoted 10 lines · Junio C Hamano

+test_expect_success 'add :/' "
+       cat >expected <<-EOF &&
+       add 'anothersub/foo'
+       add 'expected'
+       add 'sub/actual'
+       add 'sub/foo'
+       EOF
+       (cd sub && git add -n :/ >actual) &&
+       test_cmp expected sub/actual
+"

Quoted 1 line

This needs to use test_i18ncmp.

I don't think so (at least not yet).

Doesn't it come from read-cache.c::add_to_index()?

#22 lore raw

Re: [PATCH 9/9] t3703, t4208: add test cases for magic pathspec

Quoted 5 lines · Junio C Hamano
Quoted 1 line

This needs to use test_i18ncmp.

I don't think so (at least not yet).

Doesn't it come from read-cache.c::add_to_index()?

Doh, this was because I had a local patch by Jonathan in my tree that I forgot was there. Sorry about the noise.