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

10 messages from 2006-12-22 to 2006-12-22. Participants: Johannes Schindelin, Junio C Hamano, Jakub Narebski, Alex Riesen.
Thread: https://gitlist.dev/t/6071

## Johannes Schindelin, 2006-12-22 02:19

Subject: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <Pine.LNX.4.63.0612220318320.19693@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0612220318320.19693%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 three commands

	$ git show :./
	$ git show :.
	$ git show :

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

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 sha1_name.c |   24 +++++++++++++++++++++++-
 1 files changed, 23 insertions(+), 1 deletions(-)

diff --git a/sha1_name.c b/sha1_name.c
index 6d7cd78..e3758cc 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,27 @@ int get_sha1(const char *name, unsigned char *sha1)
 			}
 			pos++;
 		}
+		if (namelen > 0 && cp[namelen - 1] == '/')
+			namelen--;
+		if (namelen == 1 && cp[0] == '.') {
+			namelen = 0;
+			cp = "";
+		}
+		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++) {
-- 
1.4.4.2.ga854-dirty

```

## Junio C Hamano, 2006-12-22 04:29

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

```
(1) Why is this needed?

(2) What does this do when the index is unmerged?

```

## Jakub Narebski, 2006-12-22 08:28

Subject: Re: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <emg4qp$f8v$2@sea.gmane.org>
URL: https://gitlist.dev/e/emg4qp%24f8v%242%40sea.gmane.org
In-Reply-To: <7vzm9g7duz.fsf@assigned-by-dhcp.cox.net>

```
Junio C Hamano wrote:

> (1) Why is this needed?

Probably for completeness.

> (2) What does this do when the index is unmerged?

I think it should show "git ls-files --unmerged --abbrev", perhaps...
-- 
Jakub Narebski
Warsaw, Poland
ShadeHawk on #git

```

## Alex Riesen, 2006-12-22 08:44

Subject: Re: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <20061222084425.GA4644@steel.home>
URL: https://gitlist.dev/e/20061222084425.GA4644%40steel.home
In-Reply-To: <Pine.LNX.4.63.0612220318320.19693@wbgn013.biozentrum.uni-wuerzburg.de>

```
Johannes Schindelin, Fri, Dec 22, 2006 03:19:21 +0100:
> 
> 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 three commands
> 
> 	$ git show :./
> 	$ git show :.
> 	$ git show :
> 
> are all equivalent and show the index' idea of the root directory.

That is a bit unexpected if you're not in the root directory of
repository, but in some subdir of the working directory.
Why root? Why not the current directory relative to root?

```

## Johannes Schindelin, 2006-12-22 12:14

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

```
Hi,

On Thu, 21 Dec 2006, Junio C Hamano wrote:

> (1) Why is this needed?

It was asked recently. It also serves well when you want to show the 
index.

> (2) What does this do when the index is unmerged?

I have no idea, and ATM no time to test. Will do later.

Ciao,
Dscho

```

## Johannes Schindelin, 2006-12-22 12:16

Subject: Re: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <Pine.LNX.4.63.0612221315370.19693@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0612221315370.19693%40wbgn013.biozentrum.uni-wuerzburg.de
In-Reply-To: <emg4qp$f8v$2@sea.gmane.org>

```
Hi,

On Fri, 22 Dec 2006, Jakub Narebski wrote:

> Junio C Hamano wrote:
> 
> > (2) What does this do when the index is unmerged?
> 
> I think it should show "git ls-files --unmerged --abbrev", perhaps...

Nah. I'd rather fail out, saying that because there are unmerged entries, 
there is no valid tree in the index.

Ciao,
Dscho

```

## Johannes Schindelin, 2006-12-22 12:18

Subject: Re: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <Pine.LNX.4.63.0612221316550.19693@wbgn013.biozentrum.uni-wuerzburg.de>
URL: https://gitlist.dev/e/Pine.LNX.4.63.0612221316550.19693%40wbgn013.biozentrum.uni-wuerzburg.de
In-Reply-To: <20061222084425.GA4644@steel.home>

```
Hi,

On Fri, 22 Dec 2006, Alex Riesen wrote:

> Johannes Schindelin, Fri, Dec 22, 2006 03:19:21 +0100:
> > 
> > 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 three commands
> > 
> > 	$ git show :./
> > 	$ git show :.
> > 	$ git show :
> > 
> > are all equivalent and show the index' idea of the root directory.
> 
> That is a bit unexpected if you're not in the root directory of
> repository, but in some subdir of the working directory.
> Why root? Why not the current directory relative to root?

Why root? Because you are not asking for the working directory. Use "ls" 
for that. You are asking for the index. If you git-show a commit, you 
don't expect the output to be restricted by the subdirectory you're in, 
either, right?

CIao,
Dscho

```

## Alex Riesen, 2006-12-22 16:09

Subject: Re: [PATCH] sha1_name(): accept ':directory/' to get at the cache_tree
Message-ID: <20061222160944.GA4651@steel.home>
URL: https://gitlist.dev/e/20061222160944.GA4651%40steel.home
In-Reply-To: <Pine.LNX.4.63.0612221316550.19693@wbgn013.biozentrum.uni-wuerzburg.de>

```
Johannes Schindelin, Fri, Dec 22, 2006 13:18:55 +0100:
> > > 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 three commands
> > > 
> > > 	$ git show :./
> > > 	$ git show :.
> > > 	$ git show :
> > > 
> > > are all equivalent and show the index' idea of the root directory.
> > 
> > That is a bit unexpected if you're not in the root directory of
> > repository, but in some subdir of the working directory.
> > Why root? Why not the current directory relative to root?
> 
> Why root? Because you are not asking for the working directory. Use "ls" 
> for that. You are asking for the index. If you git-show a commit, you 
> don't expect the output to be restricted by the subdirectory you're in, 
> either, right?

Of course I do. You're breaking a very well-known and widely used
idiom here: the "." means current or at least somehow related to the
current directory. Why would you expect "git show :." to show
everything from root on if your current directory is "Documentation"?
Yes, it is a commit which "git show" displays, but the patches/diffs
are restricted by filenames, and that is where the old rules come into
play. And I would actually expect ":/" to filter from the root on, and
":." to mean the current directory.

```

## Jakub Narebski, 2006-12-22 18:05

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

```
Johannes Schindelin wrote:

> On Fri, 22 Dec 2006, Jakub Narebski wrote:
> 
>> Junio C Hamano wrote:
>> 
>>> (2) What does this do when the index is unmerged?
>> 
>> I think it should show "git ls-files --unmerged --abbrev", perhaps...
> 
> Nah. I'd rather fail out, saying that because there are unmerged entries, 
> there is no valid tree in the index.

Well, 'git diff' on unmerged IIRC does 'git diff -cc', so why not 'git show :'
do 'git ls-files --unmerged --abbrev'...
-- 
Jakub Narebski
Poland

```

## Johannes Schindelin, 2006-12-22 19:25

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

```
Hi,

On Thu, 21 Dec 2006, Junio C Hamano wrote:

> (2) What does this do when the index is unmerged?

	$ git show :
	a1: unmerged (f16f906ab60483c100d1241dfc39868de9ec9fcb)
	a1: unmerged (cf84443e49e1b366fac938711ddf4be2d4d1d9e9)
	a1: unmerged (fd7923529855d0b274795ae3349c5e0438333979)
	fatal: ambiguous argument ':': unknown revision or path not in the 
	working tree. Use '--' to separate paths from revisions

We could be a bit nicer, but I think the first three lines speak for 
themselves. This is all stderr BTW.

Ciao,
Dscho

```
