Skip to contentSkip to conversation

ls-tree path restriction semantics fixes

PATCH In review Jason McMullan 2 participants 7 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ls-tree path restriction semantics fixes

This patch fixes the git-ls-tree semantics to be less stupid, namely:

* ls of a 'tree' path should just return the SHA1 of the tree
* ls of a 'tree' path with a trailing '/' should work properly
* ls of two identical paths should have the same output as ls of a single path. (I consider ls-tree's output to be a hash dictionary)

Also, I added test cases to verify that these issues are fixed.

Old Results:

	$ git-ls-tree t
	040000 tree 4eeb3990955b8badc4c14712b89d8cd9fff02f15    t
	100644 blob 6882e23be568ccf14f3adb0c766139086f2ee952    t/Makefile
	100644 blob 2a94fdb0b83ab5fcbf1a2c6edaf36c2dbe765ec6    t/README
	100644 blob d920c6b3a3bfbb5994244a78d1ad99ce02748122    t/lib-read-tree-m-3way.sh
	...
	$ git-ls-tree t/
	(no output)
	$ git-ls-tree t t
	040000 tree 4eeb3990955b8badc4c14712b89d8cd9fff02f15    t

New Results:

	$ git-ls-tree f
	040000 tree 4eeb3990955b8badc4c14712b89d8cd9fff02f15    t
	$ git-ls-tree t/
	040000 tree 4eeb3990955b8badc4c14712b89d8cd9fff02f15    t
	$ git-ls-tree t t
	040000 tree 4eeb3990955b8badc4c14712b89d8cd9fff02f15    t
ls-tree.c +18 -10
@@ -13,8 +13,6 @@ struct path_prefix {
 	const char *name;
 };
 
-#define DEBUG(fmt, ...)	
-
 static int string_path_prefix(char *buff, size_t blen, struct path_prefix *prefix)
 {
 	int len = 0;
@@ -118,6 +116,8 @@ static void list_recursive(void *buffer,
 			mtype = pathcmp(match[mindex],&this_prefix);
 			if (mtype >= 0) {
 				matched = match[mindex];
+				/* Skip over any duplicates */
+				for (; mindex+1 < matches && strcmp(match[mindex+1],matched)==0; mindex++);
 				break;
 			}
 		}
@@ -140,19 +140,22 @@ static void list_recursive(void *buffer,
 		if (matches && ! matched)
 			continue;
 
-		if (! (eltbuf = read_sha1_file(sha1, elttype, &eltsize)) ) {
-			error("cannot read %s", sha1_to_hex(sha1));
-			continue;
-		}
-
 		/* If this is an exact directory match, we may have
 		 * directory files following this path. Match on them.
-		 * Otherwise, we're at a pach subcomponent, and we need
+		 * Otherwise, we're at a path subcomponent, and we need
 		 * to try to match again.
 		 */
 		if (mtype == 0)
 			mindex++;
 
+		if (matched && matches-mindex==0)
+			continue;
+
+		if (! (eltbuf = read_sha1_file(sha1, elttype, &eltsize)) ) {
+			error("cannot read %s", sha1_to_hex(sha1));
+			continue;
+		}
+
 		list_recursive(eltbuf, elttype, eltsize, &this_prefix, &match[mindex], matches-mindex);
 		free(eltbuf);
 	}
@@ -169,9 +172,14 @@ static int list(unsigned char *sha1,char
 	unsigned long size;
 	int npaths;
 
-	for (npaths = 0; path[npaths] != NULL; npaths++)
-		;
+	/* Count the paths, and any trailling '/' */
+	for (npaths = 0; path[npaths] != NULL; npaths++) {
+		char *cp = strrchr(path[npaths],'/');
+		if (cp != NULL && *(cp+1) == 0)
+			*cp=0;
+	}	
 
+	/* Sort the paths */
 	qsort(path,npaths,sizeof(char *),qcmp);
 
 	buffer = read_object_with_reference(sha1, "tree", &size, NULL);
t/t3100-ls-tree-restrict.sh +26 -5
@@ -84,10 +84,22 @@ test_expect_success \
     'git-ls-tree $tree path2 >current &&
      cat >expected <<\EOF &&
 040000 tree X	path2
-040000 tree X	path2/baz
-100644 blob X	path2/baz/b
-120000 blob X	path2/bazbo
-100644 blob X	path2/foo
+EOF
+     test_output'
+
+test_expect_success \
+    'ls-tree filtered' \
+    'git-ls-tree $tree path2/ >current &&
+     cat >expected <<\EOF &&
+040000 tree X	path2
+EOF
+     test_output'
+
+test_expect_success \
+    'ls-tree filtered' \
+    'git-ls-tree $tree path2 path2 >current &&
+     cat >expected <<\EOF &&
+040000 tree X	path2
 EOF
      test_output'
 
@@ -96,7 +108,16 @@ test_expect_success \
     'git-ls-tree $tree path2/baz >current &&
      cat >expected <<\EOF &&
 040000 tree X	path2/baz
-100644 blob X	path2/baz/b
+EOF
+     test_output'
+
+test_expect_success \
+    'ls-tree filtered' \
+    'git-ls-tree $tree path2 path2/bazbo path2/baz >current &&
+     cat >expected <<\EOF &&
+040000 tree X	path2
+040000 tree X	path2/baz
+120000 blob X	path2/bazbo
 EOF
      test_output'
 
#1 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"JM" == Jason McMullan <[email protected]> writes:

JM> This patch fixes the git-ls-tree semantics to be less stupid, namely:
JM> * ls of a 'tree' path should just return the SHA1 of the tree
JM> * ls of a 'tree' path with a trailing '/' should work properly
JM> * ls of two identical paths should have the same output as ls of
JM> a single path. (I consider ls-tree's output to be a hash dictionary)

I haven't read your code yet, but...

JM> Old Results:

JM> $ git-ls-tree t

JM> 	040000 tree 4eeb3990955b8badc4c14712b89d8cd9fff02f15    t
JM> 	100644 blob 6882e23be568ccf14f3adb0c766139086f2ee952    t/Makefile
JM> 	100644 blob 2a94fdb0b83ab5fcbf1a2c6edaf36c2dbe765ec6    t/README
JM> 	100644 blob d920c6b3a3bfbb5994244a78d1ad99ce02748122    t/lib-read-tree-m-3way.sh

JM> ...

I presume the counterpart to this one in your "New Results" example, which is spelled "git-ls-tree f" is a typo of "git-ls-tree t", but if that is the case I strongly disagree.

What you really want is something similar to '-d' flag to /bin/ls. You are interested in the directory itself not its contents and I think your gripe is that giving a path that matches a tree always descends into it (i.e. there is no way to do the equivalent of "/bin/ls -d t"). I agree that it is a problem, but changing "/bin/ls t" not to show the directory contents of "t" is not a solution.

JM> $ git-ls-tree t/
JM> (no output)

I agree with you that this is not what we want and we should behave the same way as "git-ls-tree t" would in this case.

JM> $ git-ls-tree t t
JM> 040000 tree 4eeb3990955b8badc4c14712b89d8cd9fff02f15 t

I do not know what you wanted to say in this example. Your "Old" and "New" look the same to me.

#2 lore raw
Quoted 7 lines · Junio C Hamano

What you really want is something similar to '-d' flag to /bin/ls. You are interested in the directory itself not its contents and I think your gripe is that giving a path that matches a tree always descends into it (i.e. there is no way to do the equivalent of "/bin/ls -d t"). I agree that it is a problem, but changing "/bin/ls t" not to show the directory contents of "t" is not a solution.

git-ls-tree reporting just the tree's hash is valid, because if
you want everything in that tree, you can just do:

git-ls-tree git-ls-tree HEAD path/dir | (read m t h n; echo $h)

I don't see the problem there.

Quoted 5 lines

JM> $ git-ls-tree t t
JM> 040000 tree 4eeb3990955b8badc4c14712b89d8cd9fff02f15 t

I do not know what you wanted to say in this example. Your "Old" and "New" look the same to me.

The problem was that 't' and 't t' produced *vastly* different output in the old code. 't' would emit everything in the tree, and 't t' would only emit t's hash.

Signature · 2 lines
Jason McMullan <[email protected]>
TimeSys Corporation
#3 lore raw
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"JM" == Jason McMullan <[email protected]> writes:

JM> git-ls-tree reporting just the tree's hash is valid, because if
JM> you want everything in that tree, you can just do:

JM> git-ls-tree git-ls-tree HEAD path/dir | (read m t h n; echo $h)

JM> I don't see the problem there.

I do not see the problem either in Turing sense, but that is like saying you could code anything given an assembler. There is a difference between being possible and being practical.

I do think the current behaviour is broken, so I think we are in half agreement. What I think is the cleanest would be to make "git-ls-tree $tree" behave similarly to what "/bin/ls -a" does. Then we have various combination of options, and also path arguments, to think about. How about doing something like this?

- Running without any paths.

"git-ls-tree $tree" shows everything first level, just like "/bin/ls -a" shows everything in cwd. There is nothing to fix here.

- Running with paths.

"git-ls-tree $tree path1 path2..." should show path$n if path$n is not a tree and everything under path$n including path$n itself if path$n is a tree, just like the way "/bin/ls -a path1 path2..." works. There is major breakage here as you pointed out with your "git-ls-tree $tree t" vs "git-ls-tree $tree t t" example.

- Recursive behaviour without paths.

"git-ls-tree -r $tree" should show everything recursively, just like what "/bin/ls -a -R" does. There is nothing to fix.

- Recursive behaviour with paths.

"git-ls-tree -r $tree path1 path2..." should show everything recursively under path$n, just like what "/bin/ls -a -R path1 path2..." does. Again this is not how it currently works as you pointed out.

- With paths but not descending into them.

"git-ls-tree -d $tree path1 path2..." should show only the named path$n even when path$n is a tree, just like what "/bin/ls -a -R -d path1 path2..." does. This is what is missing from today's git-ls-tree.

#4 lore raw

[PATCH-RFC] Rewrite ls-tree to behave more like "/bin/ls -a"

This is a complete rewrite of ls-tree to make it behave more like what "/bin/ls -a" does in the current working directory.

Namely, the changes are:

- Unlike the old ls-tree behaviour that used paths arguments to restrict output (not that it worked as intended---as pointed out in the mailing list discussion, it was quite incoherent), this rewrite uses paths arguments to specify what to show.

- Without arguments, it implicitly uses the root level as its sole argument ("/bin/ls -a" behaves as if "." is given without argument).

- Without -r (recursive) flag, it shows the named blob (either file or symlink), or the named tree and its immediate children.

- With -r flag, it shows the named path, and recursively descends into it if it is a tree.

- With -d flag, it shows the named path and does not show its children even if the path is a tree, nor descends into it recursively.

This is still request-for-comments patch. There is no mailing list consensus that this proposed new behaviour is a good one.

The patch to t/t3100-ls-tree-restrict.sh illustrates user-visible behaviour changes. Namely:

* "git-ls-tree $tree path1 path0" lists path1 first and then path0. It used to use paths as an output restrictor and showed output in cache entry order (i.e. path0 first and then path1) regardless of the order of paths arguments.

* "git-ls-tree $tree path2" lists path2 and its immediate children but having explicit paths argument does not imply recursive behaviour anymore, hence paths/baz is shown but not paths/baz/b.

---

Documentation/git-ls-tree.txt |   20 +-
ls-tree.c                     |  333 +++++++++++++++++++++++-------------------
t/t3100-ls-tree-restrict.sh   |    3 
tree.c                        |    2 
tree.h                        |    1 
5 files changed, 199 insertions(+), 160 deletions(-)
Documentation/git-ls-tree.txt +12 -8
@@ -4,23 +4,26 @@ v0.1, May 2005
 
 NAME
 ----
-git-ls-tree - Displays a tree object in human readable form
+git-ls-tree - Lists the contents of a tree object.
 
 
 SYNOPSIS
 --------
-'git-ls-tree' [-r] [-z] <tree-ish> [paths...]
+'git-ls-tree' [-d] [-r] [-z] <tree-ish> [paths...]
 
 DESCRIPTION
 -----------
-Converts the tree object to a human readable (and script processable)
-form.
+Lists the contents of a tree object, like what "/bin/ls -a" does
+in the current working directory.
 
 OPTIONS
 -------
 <tree-ish>::
 	Id of a tree.
 
+-d::
+	show only the named tree entry itself, not its children
+
 -r::
 	recurse into sub-trees
 
@@ -28,18 +31,19 @@ OPTIONS
 	\0 line termination on output
 
 paths::
-	Optionally, restrict the output of git-ls-tree to specific
-	paths. Directories will only list their tree blob ids.
-	Implies -r.
+	When paths are given, shows them.  Otherwise implicitly
+	uses the root level of the tree as the sole path argument.
+
 
 Output Format
 -------------
-        <mode>\t	<type>\t	<object>\t	<file>
+        <mode> SP <type> SP <object> TAB <file>
 
 
 Author
 ------
 Written by Linus Torvalds <[email protected]>
+Completely rewritten from scratch by Junio C Hamano <[email protected]>
 
 Documentation
 --------------
ls-tree.c +184 -149

386 lines not on this page · raw message

t/t3100-ls-tree-restrict.sh +1 -2
@@ -74,8 +74,8 @@ test_expect_success \
     'ls-tree filtered' \
     'git-ls-tree $tree path1 path0 >current &&
      cat >expected <<\EOF &&
-100644 blob X	path0
 120000 blob X	path1
+100644 blob X	path0
 EOF
      test_output'
 
@@ -85,7 +85,6 @@ test_expect_success \
      cat >expected <<\EOF &&
 040000 tree X	path2
 040000 tree X	path2/baz
-100644 blob X	path2/baz/b
 120000 blob X	path2/bazbo
 100644 blob X	path2/foo
 EOF
tree.c +1 -1
@@ -133,7 +133,7 @@ int parse_tree_buffer(struct tree *item,
 		}
 		if (obj)
 			add_ref(&item->object, obj);
-
+		entry->parent = NULL; /* needs to be filled by the user */
 		*list_p = entry;
 		list_p = &entry->next;
 	}
tree.h +1 -0
@@ -16,6 +16,7 @@ struct tree_entry_list {
 		struct tree *tree;
 		struct blob *blob;
 	} item;
+	struct tree_entry_list *parent;
 };
 
 struct tree {

------------------------------------------------

#5 lore raw

Re: [PATCH-RFC] Rewrite ls-tree to behave more like "/bin/ls -a"

Quoted 13 lines · Junio C Hamano

- Unlike the old ls-tree behaviour that used paths arguments to restrict output (not that it worked as intended---as pointed out in the mailing list discussion, it was quite incoherent), this rewrite uses paths arguments to specify what to show.

- Without arguments, it implicitly uses the root level as its sole argument ("/bin/ls -a" behaves as if "." is given without argument).

- Without -r (recursive) flag, it shows the named blob (either file or symlink), or the named tree and its immediate children.

Quoted 6 lines

- With -r flag, it shows the named path, and recursively descends into it if it is a tree.

- With -d flag, it shows the named path and does not show its children even if the path is a tree, nor descends into it recursively.

This behavior pattern is very agreeable. I'll take it!

Consider your patch:

Signature · 2 lines
Jason McMullan <[email protected]>
TimeSys Corporation
#6 lore raw

Re: [PATCH-RFC] Rewrite ls-tree to behave more like "/bin/ls -a"

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"JM" == Jason McMullan <[email protected]> writes:

Quoted 5 lines · Junio C Hamano
Quoted 5 lines

- Unlike the old ls-tree behaviour that used paths arguments to restrict output (not that it worked as intended---as pointed out in the mailing list discussion, it was quite incoherent), this rewrite uses paths arguments to specify what to show.
...

JM> This behavior pattern is very agreeable. I'll take it!

Glad to know we are in agreement.

BTW, long after finishing the rewrite, I realized that all of the problems you raised did not exist in the very original version of ls-tree, but were bugs in _your_ patch that added the paths restriction. I was merely cleaning up your mess for you without knowing what I was doing. ;-) Not that I do not like what the resulting code does, though.

I am not going to re-submit this to Linus right now, since he seems to be quiet here and spending more time and attention to the kernel, which is what I want to see. When Linus starts pulling in update for GIT, and if you see this one not applied to his tree, please remind him.