# [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree

6 messages from 2007-01-09 to 2007-01-10. Participants: Johannes Schindelin, Alex Riesen, Junio C Hamano.
Thread: https://gitlist.dev/t/6306

## Johannes Schindelin, 2007-01-09 14:03

Subject: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <Pine.LNX.4.63.0701091502030.22628@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0701091502030.22628%40wbgn013.biozentrum.uni-wuerzburg.de

```

If the cache tree is not up-to-date, it will be updated first. So, now

        $ git show :Documentation/

will in effect show what files/directories are in the index' version
of the directory Documentation. The commands

        $ git show :/
        $ git show :

are equivalent and show the index' idea of the root directory.

Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>

---

	'tis a resend of an earlier patch, but without support for the
	bogus ":." as equivalent to ":/".

	I find this feature highly convenient when I just want to see
	what files the index contains.

 sha1_name.c |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)

diff --git a/sha1_name.c b/sha1_name.c
index 6d7cd78..6baa7a4 100644
--- a/sha1_name.c
+++ b/sha1_name.c
@@ -5,6 +5,7 @@
 #include "blob.h"
 #include "tree-walk.h"
 #include "refs.h"
+#include "cache-tree.h"
 
 static int find_short_object_filename(int len, const char *name, unsigned char *sha1)
 {
@@ -532,7 +533,7 @@ int get_sha1(const char *name, unsigned char *sha1)
 	 */
 	if (name[0] == ':') {
 		int stage = 0;
-		struct cache_entry *ce;
+		struct cache_entry *ce = NULL;
 		int pos;
 		if (namelen < 3 ||
 		    name[2] != ':' ||
@@ -561,6 +562,23 @@ int get_sha1(const char *name, unsigned char *sha1)
 			}
 			pos++;
 		}
+		if (namelen > 0 && cp[namelen - 1] == '/')
+			namelen--;
+		if (namelen == 0 || (ce && ce_namelen(ce) > namelen &&
+					ce->name[namelen] == '/' &&
+					!memcmp(ce->name, cp, namelen))) {
+			struct cache_tree *tree =
+				cache_tree_find(active_cache_tree, cp);
+			if (!cache_tree_fully_valid(tree)) {
+				ret = cache_tree_update(active_cache_tree,
+						active_cache, active_nr, 0, 0);
+				if (ret < 0)
+					return ret;
+				tree = cache_tree_find(active_cache_tree, cp);
+			}
+			hashcpy(sha1, tree->sha1);
+			return 0;
+		}
 		return -1;
 	}
 	for (cp = name, bracket_depth = 0; *cp; cp++) {

```

## Alex Riesen, 2007-01-09 17:25

Subject: Re: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <81b0412b0701090925p158e87an14da5cde33436f30@mail.gmail.com>
URL: https://gitlist.dev/e/81b0412b0701090925p158e87an14da5cde33436f30%40mail.gmail.com
In-Reply-To: <Pine.LNX.4.63.0701091502030.22628@wbgn013.biozentrum.uni-wuerzburg.de>

```
On 1/9/07, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> 	'tis a resend of an earlier patch, but without support for the
> 	bogus ":." as equivalent to ":/".

Thanks :)

> 	I find this feature highly convenient when I just want to see
> 	what files the index contains.

I find it very useful too.

```

## Junio C Hamano, 2007-01-09 23:47

Subject: Re: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <7vodp7iwzu.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vodp7iwzu.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <Pine.LNX.4.63.0701091502030.22628@wbgn013.biozentrum.uni-wuerzburg.de>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> 	'tis a resend of an earlier patch, but without support for the
> 	bogus ":." as equivalent to ":/".
>
> 	I find this feature highly convenient when I just want to see
> 	what files the index contains.

I do not understand; do you mean ls-files?

In any case, I wonder if this does a sane thing if you asked
"git show :3:t/" on a fully merged index.

> @@ -561,6 +562,23 @@ int get_sha1(const char *name, unsigned char *sha1)
>  			}
>  			pos++;
>  		}
> +		if (namelen > 0 && cp[namelen - 1] == '/')
> +			namelen--;
> +		if (namelen == 0 || (ce && ce_namelen(ce) > namelen &&
> +					ce->name[namelen] == '/' &&
> +					!memcmp(ce->name, cp, namelen))) {

I may be misreading the code, but what does ce point at?  Does
this get the index sort order correctly?  For example, would
this work?

	$ echo >t- && git add t-
        $ git show :t
	$ git show :t/

> +			struct cache_tree *tree =
> +				cache_tree_find(active_cache_tree, cp);
> +			if (!cache_tree_fully_valid(tree)) {
> +				ret = cache_tree_update(active_cache_tree,
> +						active_cache, active_nr, 0, 0);
> +				if (ret < 0)
> +					return ret;

This gracefully errs out when the index is unmerged but fails to
pretend the index knows about trees, if the unmerged part of
index is outside the directory the user specified.

In short, I am not sure if it is worth it, and especially if the
motivation is to pretend as if the index contains trees, I would
be opposed to it.  The index does _not_ contain trees, and
cache-tree is a pure optimization for the next write-tree.
Nothing more.

If it (pretending as if the index contains trees) is just a
means to achieve something else worthy, I would not necessarily
oppose to that goal, but I do not see what it is, and I do not
know if the approach is right...

```

## Johannes Schindelin, 2007-01-10 00:22

Subject: Re: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <Pine.LNX.4.63.0701100108460.22628@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0701100108460.22628%40wbgn013.biozentrum.uni-wuerzburg.de
In-Reply-To: <7vodp7iwzu.fsf@assigned-by-dhcp.cox.net>

```
Hi,

On Tue, 9 Jan 2007, Junio C Hamano wrote:

> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> > 	'tis a resend of an earlier patch, but without support for the
> > 	bogus ":." as equivalent to ":/".
> >
> > 	I find this feature highly convenient when I just want to see
> > 	what files the index contains.
> 
> I do not understand; do you mean ls-files?

It is the same as ls-files, except "show" is shorter, is labeled as 
porcelain, easier to remember, _and_ it runs the pager automatically.

> In any case, I wonder if this does a sane thing if you asked
> "git show :3:t/" on a fully merged index.

No. That slipped through. The code will just ignore the stage (if 
specified) for trees.

> > @@ -561,6 +562,23 @@ int get_sha1(const char *name, unsigned char *sha1)
> >  			}
> >  			pos++;
> >  		}
> > +		if (namelen > 0 && cp[namelen - 1] == '/')
> > +			namelen--;
> > +		if (namelen == 0 || (ce && ce_namelen(ce) > namelen &&
> > +					ce->name[namelen] == '/' &&
> > +					!memcmp(ce->name, cp, namelen))) {
> 
> I may be misreading the code, but what does ce point at?  Does
> this get the index sort order correctly?

It is still at what cache_name_pos(cp, namelen) pointed to, or NULL if 
the entry does not exist (e.g. there is no cache entry, or all cache 
entries are lexicographically smaller).

> For example, would this work?
> 
> 	$ echo >t- && git add t-
>         $ git show :t
> 	$ git show :t/

I think so: The code explicitely checks for a trailing '/'. (See second 
last line of the hunk you quoted.)

> > +			struct cache_tree *tree =
> > +				cache_tree_find(active_cache_tree, cp);
> > +			if (!cache_tree_fully_valid(tree)) {
> > +				ret = cache_tree_update(active_cache_tree,
> > +						active_cache, active_nr, 0, 0);
> > +				if (ret < 0)
> > +					return ret;
> 
> This gracefully errs out when the index is unmerged but fails to
> pretend the index knows about trees, if the unmerged part of
> index is outside the directory the user specified.

That is correct. But in that case, we cannot sanely ask the question "what 
would the tree object look like if we committed right now?"

> In short, I am not sure if it is worth it, and especially if the 
> motivation is to pretend as if the index contains trees, I would be 
> opposed to it.  The index does _not_ contain trees, and cache-tree is a 
> pure optimization for the next write-tree. Nothing more.

Well, it _is_ the method to construct tree objects for committing them.

> If it (pretending as if the index contains trees) is just a means to 
> achieve something else worthy, I would not necessarily oppose to that 
> goal, but I do not see what it is, and I do not know if the approach is 
> right...

I wanted to have it for teaching purposes. And maybe a little bit for 
completeness.

But then it turned out that I even use it before committing, like when I 
ask "what files would be in my next revision?" It is not a question 
arising daily, but sometimes it is interesting to see _before_ committing 
them. (Note that I am not only interested in the _modified_ files.)

But if you feel strongly about that feature, don't take the patch.

Ciao,
Dscho

```

## Junio C Hamano, 2007-01-10 00:36

Subject: Re: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <7vtzyzhg47.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vtzyzhg47.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <Pine.LNX.4.63.0701100108460.22628@wbgn013.biozentrum.uni-wuerzburg.de>

```
Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:

> It is still at what cache_name_pos(cp, namelen) pointed to, or NULL if 
> the entry does not exist (e.g. there is no cache entry, or all cache 
> entries are lexicographically smaller).
>
>> For example, would this work?
>> 
>> 	$ echo >t- && git add t-
>>         $ git show :t
>> 	$ git show :t/
>
> I think so: The code explicitely checks for a trailing '/'. (See second 
> last line of the hunk you quoted.)

The comment was not about the trailing / version, but the one without.
Have you tried them?  

>> > +			struct cache_tree *tree =
>> > +				cache_tree_find(active_cache_tree, cp);
>> > +			if (!cache_tree_fully_valid(tree)) {
>> > +				ret = cache_tree_update(active_cache_tree,
>> > +						active_cache, active_nr, 0, 0);
>> > +				if (ret < 0)
>> > +					return ret;
>> 
>> This gracefully errs out when the index is unmerged but fails to
>> pretend the index knows about trees, if the unmerged part of
>> index is outside the directory the user specified.
>
> That is correct. But in that case, we cannot sanely ask the question "what 
> would the tree object look like if we committed right now?"

But that is not the question you are asking.  "git-ls-files t/"
does work even though you still have cache.h unmerged in the
index.

> But then it turned out that I even use it before committing, like when I 
> ask "what files would be in my next revision?" It is not a question 
> arising daily, but sometimes it is interesting to see _before_ committing 
> them. (Note that I am not only interested in the _modified_ files.)

So it really is about being able to say show while you mean ls-files.

I am not opposed to that goal at all.  I think it makes it
easier to type.  But I think using cache-tree is a wrong
approach.

```

## Johannes Schindelin, 2007-01-10 00:54

Subject: Re: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <Pine.LNX.4.63.0701100151230.22628@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0701100151230.22628%40wbgn013.biozentrum.uni-wuerzburg.de
In-Reply-To: <7vtzyzhg47.fsf@assigned-by-dhcp.cox.net>

```
Hi,

On Tue, 9 Jan 2007, Junio C Hamano wrote:

> Johannes Schindelin <Johannes.Schindelin@gmx.de> writes:
> 
> > It is still at what cache_name_pos(cp, namelen) pointed to, or NULL if 
> > the entry does not exist (e.g. there is no cache entry, or all cache 
> > entries are lexicographically smaller).
> >
> >> For example, would this work?
> >> 
> >> 	$ echo >t- && git add t-
> >>         $ git show :t
> >> 	$ git show :t/
> >
> > I think so: The code explicitely checks for a trailing '/'. (See second 
> > last line of the hunk you quoted.)
> 
> The comment was not about the trailing / version, but the one without.
> Have you tried them?

Of course not! I had the impression that cache_name_pos() just _had_ to 
find the correct spot, but of course you are right: it does not.

> >> > +			struct cache_tree *tree =
> >> > +				cache_tree_find(active_cache_tree, cp);
> >> > +			if (!cache_tree_fully_valid(tree)) {
> >> > +				ret = cache_tree_update(active_cache_tree,
> >> > +						active_cache, active_nr, 0, 0);
> >> > +				if (ret < 0)
> >> > +					return ret;
> >> 
> >> This gracefully errs out when the index is unmerged but fails to
> >> pretend the index knows about trees, if the unmerged part of
> >> index is outside the directory the user specified.
> >
> > That is correct. But in that case, we cannot sanely ask the question "what 
> > would the tree object look like if we committed right now?"
> 
> But that is not the question you are asking.  "git-ls-files t/"
> does work even though you still have cache.h unmerged in the
> index.

Ah! I retract that. I did _not_ want the recursive file list. I wanted the 
equivalent of HEAD:t/ _after_ a commit.

> > But then it turned out that I even use it before committing, like when I 
> > ask "what files would be in my next revision?" It is not a question 
> > arising daily, but sometimes it is interesting to see _before_ committing 
> > them. (Note that I am not only interested in the _modified_ files.)
> 
> So it really is about being able to say show while you mean ls-files.
> 
> I am not opposed to that goal at all.  I think it makes it
> easier to type.  But I think using cache-tree is a wrong
> approach.

Well, you are the master.

Ciao,
Dscho

```
