Skip to contentSkip to conversation

log/show: relative pathnames do not work in rev:path

PATCH In review Alex Riesen 8 participants 43 messages Jun 15, 2016 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

Noticed by a collegue of mine. Consider:

    $ cd $GIT/t
    $ git show 570f32266:t/test-lib.sh    # works
    $ git show 570f32266:test-lib.sh      # does not work
    $ git show 570f32266:./test-lib.sh    # does not work
    $ git show 570f32266:/t/test-lib.sh   # does not work

Considering that the relative path names work as filters (and many agreed on that being useful), it would be nice to allow relative pathnames in blob specifications for git-show and git-cat-file.

(besides the colon is a good delimiter, even tab-completion works with it)

#1 lore raw
Quoted 8 lines · Alex Riesen

Noticed by a collegue of mine. Consider:

    $ cd $GIT/t
    $ git show 570f32266:t/test-lib.sh    # works
    $ git show 570f32266:test-lib.sh      # does not work
    $ git show 570f32266:./test-lib.sh    # does not work
    $ git show 570f32266:/t/test-lib.sh   # does not work
Quoted 5 lines

Considering that the relative path names work as filters (and many agreed on that being useful), it would be nice to allow relative pathnames in blob specifications for git-show and git-cat-file.

(besides the colon is a good delimiter, even tab-completion works with it)

If you think about it a bit, relative path names nor absolute path names does and should not work. 570f32266:t/test-lib.sh means path t/test-lib.sh staring from 570f32266^{tree}. Where you are in the filesystem is not important and matters not for this syntax. Besides if you access other branch file might be not in filesystem (deleted file, or disjoint branch with separate contents like 'todo' or 'html' branch in git.git repository).

Besides,

    $ git show 570f32266:t/test-lib.sh    # works
    $ git show cc5ac8b72:test-lib.sh      # also works

works... but 49d8bcd7a2df5 here is SHA-1 id of a 't/' tree (shortened output of "git rev-parse 570f32266:t").

And I think that with bash-completion from contrib you complete correct pathnames; don't rely on filesystem pathnames completion.

Signature · 3 lines
Jakub Narebski
Poland
ShadeHawk on #git
#2 lore raw

Jakub Narebski, Tue, Dec 18, 2007 18:50:20 +0100:

Quoted 16 lines
Quoted 8 lines · Alex Riesen

Noticed by a collegue of mine. Consider:

    $ cd $GIT/t
    $ git show 570f32266:t/test-lib.sh    # works
    $ git show 570f32266:test-lib.sh      # does not work
    $ git show 570f32266:./test-lib.sh    # does not work
    $ git show 570f32266:/t/test-lib.sh   # does not work
Quoted 5 lines

Considering that the relative path names work as filters (and many agreed on that being useful), it would be nice to allow relative pathnames in blob specifications for git-show and git-cat-file.

(besides the colon is a good delimiter, even tab-completion works with it)

Quoted 7 lines

If you think about it a bit, relative path names nor absolute path names does and should not work. 570f32266:t/test-lib.sh means path t/test-lib.sh staring from 570f32266^{tree}. Where you are in the filesystem is not important and matters not for this syntax. Besides if you access other branch file might be not in filesystem (deleted file, or disjoint branch with separate contents like 'todo' or 'html' branch in git.git repository).

Not convinced. It is *not* the plumbing problem I was trying to describe. They discussion, metaphorically, should not have left the command-line parser.

I think that we have parsing of the blob locators at the wrong level:
so that git-show, git-log and git-diff can handle its pathnames as they handle path filters (relative to cwd), and git-cat-file, git-diff-tree, git-rev-list, etc can handle theirs always relative to the project root.

I actually do not see any problem for git-show (being porcelain-level program) to treat *each and every* path anywhere relatively to the current directory. It is just more comfortable.

Please consider the following patches.

#3 lore raw

PATCH 1/1 Simple support for tree entry specification with relative pathnames

This allows git show to understand something like this:

    $ test -f DIR/file && cd DIR &&  git show rev:file

---

This is a bit too simplistic and can be fooled easily:

    .../t$ git show HEAD:../t/test-lib.sh

wont work. It is short, though.

 cache.h     |    1 +
 revision.c  |    4 ++--
 sha1_name.c |   27 ++++++++++++++++++++++++---
 3 files changed, 27 insertions(+), 5 deletions(-)
cache.h +1 -0
index 39331c2..054f106 100644
@@ -416,6 +416,7 @@ static inline unsigned int hexval(unsigned char c)
 
 extern int get_sha1(const char *str, unsigned char *sha1);
 extern int get_sha1_with_mode(const char *str, unsigned char *sha1, unsigned *mode);
+extern int get_sha1_with_prefix(const char *prefix, const char *name, unsigned char *sha1, unsigned *mode);
 extern int get_sha1_hex(const char *hex, unsigned char *sha1);
 extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
 extern int read_ref(const char *filename, unsigned char *sha1);
revision.c +2 -2
index 7e2f4f1..cac283c 100644
@@ -855,7 +855,7 @@ int handle_revision_arg(const char *arg, struct rev_info *revs,
 		local_flags = UNINTERESTING;
 		arg++;
 	}
-	if (get_sha1_with_mode(arg, sha1, &mode))
+	if (get_sha1_with_prefix(revs->prefix, arg, sha1, &mode))
 		return -1;
 	if (!cant_be_filename)
 		verify_non_filename(revs->prefix, arg);
@@ -1280,7 +1280,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, const ch
 		unsigned char sha1[20];
 		struct object *object;
 		unsigned mode;
-		if (get_sha1_with_mode(def, sha1, &mode))
+		if (get_sha1_with_prefix(revs->prefix, def, sha1, &mode))
 			die("bad default revision '%s'", def);
 		object = get_reference(revs, def, sha1, 0);
 		add_pending_object_with_mode(revs, object, def, mode);
sha1_name.c +24 -3
index 13e1164..358aab7 100644
@@ -656,6 +656,12 @@ int get_sha1(const char *name, unsigned char *sha1)
 
 int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
 {
+	return get_sha1_with_prefix(NULL, name, sha1, mode);
+}
+
+int get_sha1_with_prefix(const char *prefix, const char *name, unsigned char *sha1, unsigned *mode)
+{
+	char *prefixpath;
 	int ret, bracket_depth;
 	int namelen = strlen(name);
 	const char *cp;
@@ -664,6 +670,9 @@ int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
 	ret = get_sha1_1(name, namelen, sha1);
 	if (!ret)
 		return ret;
+
+	prefixpath = prefix ? xmalloc(strlen(prefix) + namelen + 1): NULL;
+
 	/* sha1:path --> object name of path in ent sha1
 	 * :path -> object name of path in index
 	 * :[0-3]:path -> object name of path in index at stage
@@ -685,6 +694,10 @@ int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
 		namelen = namelen - (cp - name);
 		if (!active_cache)
 			read_cache();
+		if (prefix) {
+			namelen = sprintf(prefixpath, "%s%s", prefix, cp);
+			cp = prefixpath;
+		}
 		pos = cache_name_pos(cp, namelen);
 		if (pos < 0)
 			pos = -pos - 1;
@@ -696,10 +709,12 @@ int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
 			if (ce_stage(ce) == stage) {
 				hashcpy(sha1, ce->sha1);
 				*mode = ntohl(ce->ce_mode);
+				free(prefixpath);
 				return 0;
 			}
 			pos++;
 		}
+		free(prefixpath);
 		return -1;
 	}
 	for (cp = name, bracket_depth = 0; *cp; cp++) {
@@ -712,9 +727,15 @@ int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
 	}
 	if (*cp == ':') {
 		unsigned char tree_sha1[20];
-		if (!get_sha1_1(name, cp-name, tree_sha1))
-			return get_tree_entry(tree_sha1, cp+1, sha1,
-					      mode);
+		if (!get_sha1_1(name, cp-name, tree_sha1)) {
+			if (!prefix)
+				ret = get_tree_entry(tree_sha1, cp + 1, sha1, mode);
+			else {
+				sprintf(prefixpath, "%s%s", prefix, cp + 1);
+				ret = get_tree_entry(tree_sha1, prefixpath, sha1, mode);
+				free(prefixpath);
+			}
+		}
 	}
 	return ret;
 }
Signature · 1 line
1.5.4.rc0.86.g30f5
#4 lore raw

PATCH 1/1 Introduce pathexpand: syntax-level chdir into the given cwd

---

This will be used by the following patch. I actually already have sent this one in, as suggestion for some problem back then. It is a bit generic, so it gets its own patch.

 cache.h |    2 ++
 path.c  |   59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+), 0 deletions(-)
cache.h +2 -0
index 054f106..25ce5da 100644
@@ -383,6 +383,8 @@ static inline int is_absolute_path(const char *path)
 	return path[0] == '/';
 }
 const char *make_absolute_path(const char *path);
+/* Returns the analog of "cd path" from a directory "cwd" */
+extern char *pathexpand(const char *cwd, const char *path);
 
 /* Read and unpack a sha1 file into memory, write memory to a sha1 file */
 extern int sha1_object_info(const unsigned char *, unsigned long *);
path.c +59 -0

62 lines not on this page · raw message

Signature · 1 line
1.5.4.rc0.86.g30f5
#5 lore raw

PATCH 1/1 Use pathexpand to preparse the relative pathnames in blob references

---

This, OTOH, is a bit intrusive and changes the current behaviour a bit too far. git-show cannot use the absolute pathnames in blob locators at all now, which I consider bad. An obvious way to use rev:/path is blocked by Johannes' get_sha1_oneline. It would have worked, though.

 sha1_name.c |   21 +++++++--------------
 1 files changed, 7 insertions(+), 14 deletions(-)
sha1_name.c +7 -14
index 358aab7..369e7d0 100644
@@ -661,7 +661,7 @@ int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
 
 int get_sha1_with_prefix(const char *prefix, const char *name, unsigned char *sha1, unsigned *mode)
 {
-	char *prefixpath;
+	char *prefixpath = NULL;
 	int ret, bracket_depth;
 	int namelen = strlen(name);
 	const char *cp;
@@ -671,8 +671,6 @@ int get_sha1_with_prefix(const char *prefix, const char *name, unsigned char *sh
 	if (!ret)
 		return ret;
 
-	prefixpath = prefix ? xmalloc(strlen(prefix) + namelen + 1): NULL;
-
 	/* sha1:path --> object name of path in ent sha1
 	 * :path -> object name of path in index
 	 * :[0-3]:path -> object name of path in index at stage
@@ -694,10 +692,9 @@ int get_sha1_with_prefix(const char *prefix, const char *name, unsigned char *sh
 		namelen = namelen - (cp - name);
 		if (!active_cache)
 			read_cache();
-		if (prefix) {
-			namelen = sprintf(prefixpath, "%s%s", prefix, cp);
-			cp = prefixpath;
-		}
+		prefixpath = pathexpand(prefix, cp);
+		namelen = strlen(prefixpath);
+		cp = prefixpath;
 		pos = cache_name_pos(cp, namelen);
 		if (pos < 0)
 			pos = -pos - 1;
@@ -728,13 +725,9 @@ int get_sha1_with_prefix(const char *prefix, const char *name, unsigned char *sh
 	if (*cp == ':') {
 		unsigned char tree_sha1[20];
 		if (!get_sha1_1(name, cp-name, tree_sha1)) {
-			if (!prefix)
-				ret = get_tree_entry(tree_sha1, cp + 1, sha1, mode);
-			else {
-				sprintf(prefixpath, "%s%s", prefix, cp + 1);
-				ret = get_tree_entry(tree_sha1, prefixpath, sha1, mode);
-				free(prefixpath);
-			}
+			prefixpath = pathexpand(prefix, cp + 1);
+			ret = get_tree_entry(tree_sha1, prefixpath, sha1, mode);
+			free(prefixpath);
 		}
 	}
 	return ret;
Signature · 1 line
1.5.4.rc0.86.g30f5
#6 lore raw

Re: [PATCH] Use pathexpand to preparse the relative pathnames in blob references

Quoted 4 lines · Alex Riesen

This, OTOH, is a bit intrusive and changes the current behaviour a bit too far. git-show cannot use the absolute pathnames in blob locators at all now, which I consider bad. An obvious way to use rev:/path is blocked by Johannes' get_sha1_oneline. It would have worked, though.

IMO, this is backwards. The default should be absolute naming (after all, you have already rooted it at a tree by saying HEAD:), and you should treat '.' as a short-hand for "my current prefix". IOW, this works as before:

  cd t && git show HEAD:t/test-lib.sh

but this would now work:

  cd t && git show HEAD:./test-lib.sh

and of course supporting '..' could be added, as well.

This works under the assumption that you don't have tree entries of '.' or '..'; I don't think the data structure enforces any such assumption, but I doubt you could easily create such a tree without hacking the git tools (and you would have to be insane to do so anyway).

-Peff

PS I didn't just think of this...I'm pretty sure this discussion came up sometime in the past year and somebody more clever than I thought of it.

#7 lore raw

Re: [PATCH] Use pathexpand to preparse the relative pathnames in blob references

In reply to Alex Riesen
Quoted 4 lines · Alex Riesen

This, OTOH, is a bit intrusive and changes the current behaviour a bit too far. git-show cannot use the absolute pathnames in blob locators at all now, which I consider bad. An obvious way to use rev:/path is blocked by Johannes' get_sha1_oneline. It would have worked, though.

Last May Junio proposed the current :/ should be changed to :? since it actually searches backwards. Then :/ would be an absolute path, and : could default to relative (like Unix command line).

Because of this incompatibility I think he wanted to delay it until 1.5.6 or 1.6 (see old thread).

Signature · 1 line
Dana L. How  [email protected]  +1 650 804 5991 cell
#8 lore raw

Re: [PATCH] Simple support for tree entry specification with relative pathnames

In reply to Alex Riesen

ACK from me...

I submitted a similar patch last May 4 which also changed sha1_name.c to do this. The patch added a config variable to control this (probably not desirable). The patch also handled leading/embedded . and .. .

In p4 you can say
p4 <operation> file#rev
and file is interpreted relatively.

I wanted to be able to say
git <operation> tree:file
and have file interpreted relatively.
This should only happen when you are inside the work tree.

Good luck!

Dana

Quoted 12 lines · Alex Riesen

This allows git show to understand something like this:

    $ test -f DIR/file && cd DIR &&  git show rev:file

---

This is a bit too simplistic and can be fooled easily:

    .../t$ git show HEAD:../t/test-lib.sh

wont work. It is short, though.

Signature · 1 line
Dana L. How  [email protected]  +1 650 804 5991 cell
#9 lore raw

Re: [PATCH] Simple support for tree entry specification with relative pathnames

Dana How, Tue, Dec 18, 2007 22:03:04 +0100:

Quoted 1 line

ACK from me...

But NAK from me. Definitely not in this form. Please consider the patches *only* as an RFC.

Quoted 4 lines

I submitted a similar patch last May 4 which also changed sha1_name.c to do this. The patch added a config variable to control this (probably not desirable). The patch also handled leading/embedded . and .. .

This one (the last in series) too.

Quoted 8 lines

In p4 you can say
p4 <operation> file#rev
and file is interpreted relatively.

I wanted to be able to say
git <operation> tree:file
and have file interpreted relatively.
This should only happen when you are inside the work tree.

It is pure coincedence the syntaxes look similar. They are very deeply different in Git and Perforce.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#11 lore raw

Re: [PATCH] Simple support for tree entry specification with relative pathnames

Dana How, Wed, Dec 19, 2007 00:39:13 +0100:

Quoted 1 line

So has this idea been shut down again?

So it seems. Definitely not for 1.5.*

Quoted 2 lines

I don't have the time to update my May 4 patch right now, but you are welcome to look at it and see if it has anything useful.

It is very similar.

#12 lore raw
In reply to Alex Riesen
Quoted 26 lines · Alex Riesen

Jakub Narebski, Tue, Dec 18, 2007 18:50:20 +0100:

Quoted 17 lines
Quoted 8 lines · Alex Riesen

Noticed by a collegue of mine. Consider:

    $ cd $GIT/t
    $ git show 570f32266:t/test-lib.sh    # works
    $ git show 570f32266:test-lib.sh      # does not work
    $ git show 570f32266:./test-lib.sh    # does not work
    $ git show 570f32266:/t/test-lib.sh   # does not work
Quoted 6 lines

Considering that the relative path names work as filters (and many agreed on that being useful), it would be nice to allow relative pathnames in blob specifications for git-show and git-cat-file.

(besides the colon is a good delimiter, even tab-completion works with it)

Quoted 7 lines

If you think about it a bit, relative path names nor absolute path names does and should not work. 570f32266:t/test-lib.sh means path t/test-lib.sh staring from 570f32266^{tree}. Where you are in the filesystem is not important and matters not for this syntax. Besides if you access other branch file might be not in filesystem (deleted file, or disjoint branch with separate contents like 'todo' or 'html' branch in git.git repository).

Quoted 7 lines

Not convinced. It is *not* the plumbing problem I was trying to describe. They discussion, metaphorically, should not have left the command-line parser.

I think that we have parsing of the blob locators at the wrong level:
so that git-show, git-log and git-diff can handle its pathnames as they handle path filters (relative to cwd),

What cwd? <path> in <tree-ish>:<path> syntax is "relative" to <tree-ish>. IMHO "<tree-ish>:<path>" should be considered (and is considered) as one object: current working directory doesn't matter at all there, contrary to "<tree-ish> -- <pathspec>" where it is natural that <pathspec> is relative to current working directory.

What should git do in your proposal when we are on master branch in Documentation subdirectory, and want to check TODO file in todo branch? "git show todo:TODO" is most natural IMHO.

Note that for true <tree> as <tree-ish> you just don't know where in the working area directory hierarchy <tree> can be. This means you do't know relation of <tree> and <path> in <tree>:<path> to cwd.

Quoted 3 lines

and git-cat-file,
git-diff-tree, git-rev-list, etc can handle theirs always relative to the project root.

Not "relative to project root". Relative to tree-ish used on right hand side in <tree-ish>:<path> extended SHA-1 syntax. It is usually project root, because when you specify <commit> or <tag> as <tree-ish> it refers to top/root directory of a project.

Quoted 3 lines

I actually do not see any problem for git-show (being porcelain-level program) to treat *each and every* path anywhere relatively to the current directory. It is just more comfortable.

This breaks backward compatibility, hard. And IMHO breaks layers.

But if (big if) it was to be implemented, default behavior should be unchanged, and relative to the cwd (layers!) should use new syntax, for example

     $ cd $GIT/t
     $ git show 570f32266:t/test-lib.sh    # works
     $ git show 570f32266:test-lib.sh      # should not work
     $ git show 570f32266:./test-lib.sh    # should work
     $ git show 570f32266:/t/test-lib.sh   # should perhaps work

Currently ":/<text>" (but not "<ref>:/<text>") is taken; see git-rev-parse(1), "Specifying revisions".

Signature · 2 lines
Jakub Narebski
Poland
#13 lore raw

Jakub Narebski, Tue, Dec 18, 2007 22:24:26 +0100:

Quoted 6 lines · Alex Riesen
Quoted 3 lines

I think that we have parsing of the blob locators at the wrong level:
so that git-show, git-log and git-diff can handle its pathnames as they handle path filters (relative to cwd),

What cwd? <path> in <tree-ish>:<path> syntax is "relative" to <tree-ish>.

But the act of running "git-show <tree-ish>:<path>" does have a working directory relative to the project root. And usually the relative directory makes a lot of sense in git-show commands.

Quoted 3 lines

What should git do in your proposal when we are on master branch in Documentation subdirectory, and want to check TODO file in todo branch? "git show todo:TODO" is most natural IMHO.

Yes, and that's why I NAKed the patches in the mail to Dana. I just hope the problem gets some attention. Maybe I even get something out of it, maybe not. It is not that hard to keep the patches in my tree.

Quoted 3 lines

Note that for true <tree> as <tree-ish> you just don't know where in the working area directory hierarchy <tree> can be. This means you do't know relation of <tree> and <path> in <tree>:<path> to cwd.

I understand. But... How often do you think people use git show with a tree which was not pointed by a commit?

Quoted 8 lines
Quoted 3 lines

and git-cat-file,
git-diff-tree, git-rev-list, etc can handle theirs always relative to the project root.

Not "relative to project root". Relative to tree-ish used on right hand side in <tree-ish>:<path> extended SHA-1 syntax. It is usually project root, because when you specify <commit> or <tag> as <tree-ish> it refers to top/root directory of a project.

I know. My problem: it is also awkward. git-show :test-l<Tab>ib.sh just does not do what I expect. Nor does git cat-file HEAD:test-l<Tab>ib.sh. And git cat-file HEAD:t/test-l<Tab> does not work at all. And this is very simple example. Normally the pathnames are about 100 characters long.

You know, it maybe as much correct as you wish, but is not very usable (and no, I can't use the contributed completion. For lots of reasons).

Quoted 5 lines
Quoted 3 lines

I actually do not see any problem for git-show (being porcelain-level program) to treat *each and every* path anywhere relatively to the current directory. It is just more comfortable.

This breaks backward compatibility, hard. And IMHO breaks layers.

Maybe they should be broken in porcelain...

Quoted 7 lines

But if (big if) it was to be implemented, default behavior should be unchanged, and relative to the cwd (layers!) should use new syntax, for example

     $ cd $GIT/t
     $ git show 570f32266:t/test-lib.sh    # works
     $ git show 570f32266:test-lib.sh      # should not work

Well... Frankly, I suggest changing this for porcelain-level commands (show and diff) and leave it as it is for plumbing.

Quoted 1 line
     $ git show 570f32266:./test-lib.sh    # should work

Definitely. I even implemented a patch to allow just this, but scraped
it: it looked a bit small and the syntax is not obvious to the user. Maybe that is what I end up with, though.

Quoted 2 lines
     $ git show 570f32266:/t/test-lib.sh   # should perhaps work
Currently ":/<text>" (but not "<ref>:/<text>") is taken; see

Yes, and it becomes more and more an obstacle. With just one user standing, AFAICS (/me considers Dscho's assassination for moment... Nah... Maybe poison?)

#14 lore raw
Quoted 7 lines · Alex Riesen

Jakub Narebski, Tue, Dec 18, 2007 22:24:26 +0100:

Quoted 4 lines

Not "relative to project root". Relative to tree-ish used on right hand side in <tree-ish>:<path> extended SHA-1 syntax. It is usually project root, because when you specify <commit> or <tag> as <tree-ish> it refers to top/root directory of a project.

Quoted 5 lines

I know. My problem: it is also awkward. git-show :test-l<Tab>ib.sh just does not do what I expect. Nor does git cat-file HEAD:test-l<Tab>ib.sh. And git cat-file HEAD:t/test-l<Tab> does not work at all. And this is very simple example. Normally the pathnames are about 100 characters long.

Doesn't bash completion (or zsh compltion) for git implement this? Let me check... it does

 $ . contrib/completion/git-completion.bash
 $ git cat-file -p HEAD:t/test-<TAB>
 $ git cat-file -p HEAD:t/test-lib.sh    # autocompleted

It does work correctly also from within t/ directory.

Signature · 2 lines
Jakub Narebski
Poland
#15 lore raw
In reply to Alex Riesen

Hi,

Quoted 7 lines · Alex Riesen

Jakub Narebski, Tue, Dec 18, 2007 22:24:26 +0100:

Quoted 6 lines · Alex Riesen
Quoted 4 lines

I think that we have parsing of the blob locators at the wrong
level: so that git-show, git-log and git-diff can handle its pathnames as they handle path filters (relative to cwd),

Quoted 5 lines
Quoted 2 lines

What cwd? <path> in <tree-ish>:<path> syntax is "relative" to <tree-ish>.

But the act of running "git-show <tree-ish>:<path>" does have a working directory relative to the project root.

Not necessarily. My primary use of "git show <tree-ish>:<path>" (yes, I already use the dash-less form ;-) is in _bare_ repositories.

And I still maintain that expecting <tree-ish>:<path> to take the current relative path into account would be just like if you expected

C:\> cd WINDOWS
C:\WINDOWS> dir D:system32

to show you the contents of D:\WINDOWS\system32.

Or another, less Windowsy example:

	$ cd /usr/bin
	$ scp home:bash ./

No, this does not copy home:/usr/bin/bash but home:$HOME/bash.

Hth,
Dscho

#16 lore raw
Quoted 14 lines · Johannes Schindelin
Quoted 7 lines · Alex Riesen

Jakub Narebski, Tue, Dec 18, 2007 22:24:26 +0100:

Quoted 3 lines

What cwd? <path> in <tree-ish>:<path> syntax is "relative" to <tree-ish>.

But the act of running "git-show <tree-ish>:<path>" does have a working directory relative to the project root.

Not necessarily. My primary use of "git show <tree-ish>:<path>" (yes, I already use the dash-less form ;-) is in _bare_ repositories.

And I still maintain that expecting <tree-ish>:<path> to take the current relative path into account would be just like if you expected

[...]

Quoted 4 lines
	$ cd /usr/bin
	$ scp home:bash ./

No, this does not copy home:/usr/bin/bash but home:$HOME/bash.

Great example! In scp <machine>:<path>, <path> by default is relative to the login (ssh) directory on <machine>. In git's <tree-ish>:<path>, <path> by default is relative to <tree-ish>.

Although Linus argument about thinking that cwd affects translation from _commit_ sha1 to _tree-ish_ is also sound. Nevertheless I'd rather have separate syntax for cwd-relative paths, i.e. <commit>:./<relpath>.

Signature · 2 lines
Jakub Narebski
Poland
#17 lore raw
Quoted 3 lines · Jakub Narebski

Although Linus argument about thinking that cwd affects translation from _commit_ sha1 to _tree-ish_ is also sound. Nevertheless I'd rather have separate syntax for cwd-relative paths, i.e. <commit>:./<relpath>.

Cool -- so adding recognition for ":." will support relative paths when useful and will not interfere with any current syntax.

Thanks,

Signature · 1 line
Dana L. How  [email protected]  +1 650 804 5991 cell
#18 lore raw
In reply to Johannes Schindelin
Quoted 13 lines · Johannes Schindelin
Quoted 2 lines · Alex Riesen

But the act of running "git-show <tree-ish>:<path>" does have a working directory relative to the project root.

Not necessarily. My primary use of "git show <tree-ish>:<path>" (yes, I already use the dash-less form ;-) is in _bare_ repositories.

And I still maintain that expecting <tree-ish>:<path> to take the current relative path into account would be just like if you expected

C:\> cd WINDOWS
C:\WINDOWS> dir D:system32

Quoted 8 lines

to show you the contents of D:\WINDOWS\system32.

Or another, less Windowsy example:

        $ cd /usr/bin
        $ scp home:bash ./

No, this does not copy home:/usr/bin/bash but home:$HOME/bash.

Both of your counterexamples use 2 disjoint directory trees:
C: vs D:, or trees on different machines.

The cases we are talking about are all subtrees of the working tree. There is a useful cwd suffix.

Don't you think that
git <op> commit:./file.c
could occasionally be more convenient than
git <op> commit:very/long/and/boring/path/equal/to/cwd/file.c
?

Thanks,

Signature · 1 line
Dana L. How  [email protected]  +1 650 804 5991 cell
#19 lore raw

Hi,

Quoted 18 lines · Dana How
Quoted 17 lines · Johannes Schindelin
Quoted 2 lines · Alex Riesen

But the act of running "git-show <tree-ish>:<path>" does have a working directory relative to the project root.

Not necessarily. My primary use of "git show <tree-ish>:<path>" (yes, I already use the dash-less form ;-) is in _bare_ repositories.

And I still maintain that expecting <tree-ish>:<path> to take the current relative path into account would be just like if you expected

C:\> cd WINDOWS
C:\WINDOWS> dir D:system32

to show you the contents of D:\WINDOWS\system32.

Or another, less Windowsy example:

Quoted 7 lines
Quoted 4 lines
        $ cd /usr/bin
        $ scp home:bash ./

No, this does not copy home:/usr/bin/bash but home:$HOME/bash.

Both of your counterexamples use 2 disjoint directory trees:
C: vs D:, or trees on different machines.

Well, the first actually only uses 1 "disjoint" directory tree. You did not address the concern of the bare repository.

Quoted 2 lines

The cases we are talking about are all subtrees of the working tree. There is a useful cwd suffix.

Not necessarily. You can be in a subdirectory that was not even created in _any_ revision. Or you can access a different branch with a different history.

It boils down to this: if you need relative paths _only_, you narrowed yourself very much in your use cases.

Quoted 5 lines

Don't you think that
git <op> commit:./file.c
could occasionally be more convenient than
git <op> commit:very/long/and/boring/path/equal/to/cwd/file.c
?

Actually, it depends on the <op>. And guess what, for those operations that I would like to have that, it already works! "grep", "diff", "log".

Ciao,
Dscho

#20 lore raw
In reply to Dana How
Quoted 6 lines · Dana How

Both of your counterexamples use 2 disjoint directory trees:
C: vs D:, or trees on different machines.

The cases we are talking about are all subtrees of the working tree. There is a useful cwd suffix.

No.

The cases we're talking of are *not* subtrees of the working tree.

The SHA1 of a commit may well be a totally disjoint tree. Try it in the git repository with something like

git show origin/man:man1/git-fsck.1

or

git show origin/todo:TODO

and realize that Dscho's examples of using the DOS/Windows drive letters is actually a really good example of what that <commit>:<pathname> syntax is.

In fact, you can very well think of the commit/tree as a "drive letter". It really does go into another namespace entirely. It's just that often that namespace does bear some relationship to the currently checked out branch. But that's just an "often", it's not at all a given.

Quoted 4 lines

Don't you think that
git <op> commit:./file.c
could occasionally be more convenient than
git <op> commit:very/long/and/boring/path/equal/to/cwd/file.c

It's not about "convenience". It's about *sanity* and good design. And the fact is, that "commit:path" format really has nothing to do with the CWD in the general case.

Linus

#21 lore raw
Quoted 10 lines · Linus Torvalds
Quoted 2 lines · Dana How

The cases we are talking about are all subtrees of the working tree. There is a useful cwd suffix.

No.

The cases we're talking of are *not* subtrees of the working tree.

The SHA1 of a commit may well be a totally disjoint tree. Try it in the git repository with something like

Agreed, but note you wrote *may*.

I'd like to move some stuff currently in a p4 repository into git. The directory structure within the repo is 13 levels deep; I didn't design it nor can I change it.

For the majority of the cases of interest to me git already accepts relative paths. However, one thing people do often in p4 (or any SCM) is look at (or compare, etc) specific revisions. Unfortunately, these are not part of branches or commits, they are just file-specific revisions (don't get me started on p4 "branches"). The equivalent in git is to use a commit name (or a tag) and then name the file. The basic commit:file syntax doesn't accept relative paths. I am not specifically hung up on the commit:./path syntax; I just want some notation that will get those 13 directories from $cwd instead of making me type them again. Yes, sometimes that might not make sense to request.

There was some mention of bash/zsh completions Unfortunately, much of our CAD environment is not configured in bash/zsh, so although I use bash for some scripting,
it's not the default for command-line, and won't be used by others I need to drag along with me...

Quoted 5 lines

In fact, you can very well think of the commit/tree as a "drive letter". It really does go into another namespace entirely. It's just that often that namespace does bear some relationship to the currently checked out branch. But that's just an "often", it's not at all a given.

Quoted 8 lines
Quoted 4 lines

Don't you think that
git <op> commit:./file.c
could occasionally be more convenient than
git <op> commit:very/long/and/boring/path/equal/to/cwd/file.c

It's not about "convenience". It's about *sanity* and good design. And the fact is, that "commit:path" format really has nothing to do with the CWD in the general case.

Yes I frequently get to be one of the people here pushing for (and sometimes losing the case for) "good design".
But I will never be able to successfully argue for retyping 13 directories already in the cwd because it's the "good" or "sane" thing to do.

Given that the root of the current working tree, the commit, the cwd, and the path (suffix) given on the command line are all known precisely, it does not seem dangerous to come up with an exact rule to combine them which is only triggered by some specific syntax.

This does not need to work in bare repositories.

Thanks,

Signature · 3 lines
Dana L. How  [email protected]  +1 650 804 5991 cell

(Junio removed from cc: at his request)
#22 lore raw

Dana How, Wed, Dec 19, 2007 02:52:23 +0100:

Quoted 7 lines

Given that the root of the current working tree, the commit, the cwd, and the path (suffix) given on the command line are all known precisely, it does not seem dangerous to come up with an exact rule to combine them which is only triggered by some specific syntax.

This does not need to work in bare repositories.

And it wont in both patches (your and mine): they don't have prefix (aka cwd) at all. The bare repos are just no problem, the pathnames there are always project-absolute.

#23 lore raw
In reply to Dana How
Quoted 19 lines · Dana How
Quoted 11 lines · Linus Torvalds
Quoted 2 lines · Dana How

The cases we are talking about are all subtrees of the working tree. There is a useful cwd suffix.

No.

The cases we're talking of are *not* subtrees of the working tree.

The SHA1 of a commit may well be a totally disjoint tree. Try it in the git repository with something like

Agreed, but note you wrote *may*.

I'd like to move some stuff currently in a p4 repository into git. The directory structure within the repo is 13 levels deep; I didn't design it nor can I change it.

Quoted 11 lines

For the majority of the cases of interest to me git already accepts relative paths. However, one thing people do often in p4 (or any SCM) is look at (or compare, etc) specific revisions. Unfortunately, these are not part of branches or commits, they are just file-specific revisions (don't get me started on p4 "branches"). The equivalent in git is to use a commit name (or a tag) and then name the file. The basic commit:file syntax doesn't accept relative paths. I am not specifically hung up on the commit:./path syntax; I just want some notation that will get those 13 directories from $cwd instead of making me type them again. Yes, sometimes that might not make sense to request.

[...]

I think new feature like this should be postponed after 1.5.4 is out; we are now in feature freeze (only bugfixes are accepted).

That said, does git-showrel solution proposed by Johannes Schindelin in
Message-ID: <[email protected]>
http://permalink.gmane.org/gmane.comp.version-control.git/68840
work for you?

Below version of git-showrel script which uses proposed 'commit:./relpath' syntax (it could be improved, of course):

cat > git-showrel <<\EOF
#!/bin/sh

rel=$(git rev-parse --show-prefix 2>/dev/null)
git show $(echo "$@" | sed -e "s!:./!:${rel}!")

EOF

Signature · 2 lines
Jakub Narebski
Poland
#24 lore raw
Quoted 5 lines · Jakub Narebski
Quoted 4 lines · Dana How

I'd like to move some stuff currently in a p4 repository into git. The directory structure within the repo is 13 levels deep; I didn't design it nor can I change it.

Quoted 8 lines
Quoted 5 lines

... The
basic commit:file syntax doesn't accept relative paths. I am not specifically hung up on the commit:./path syntax; I just want some notation that will get those 13 directories from $cwd instead of making me type them again.

I think new feature like this should be postponed after 1.5.4 is out; we are now in feature freeze (only bugfixes are accepted).

OK. This is all the conversation that resulted from Alex's RFC.

Quoted 9 lines

That said, does git-showrel solution proposed by Johannes Schindelin in
Message-ID: <[email protected]>
http://permalink.gmane.org/gmane.comp.version-control.git/68840
work for you?

Below version of git-showrel script which uses proposed 'commit:./relpath' syntax (it could be improved, of course):

Quoted 7 lines

cat > git-showrel <<\EOF
#!/bin/sh

rel=$(git rev-parse --show-prefix 2>/dev/null)
git show $(echo "$@" | sed -e "s!:./!:${rel}!")

EOF

It's definitely true I could use this for now. In the long run (meaning after the feature freeze) I don't view this as adequate for 2 reasons:
(1) I would like a consistent interpretation of commit:path wherever it is accepted; and
(2) If a novice types bad arguments to git-showrel , they are probably going to be very confused by its error messages which are a response to a munged version of their command line.

Thanks,

Signature · 1 line
Dana L. How  [email protected]  +1 650 804 5991 cell
#25 lore raw
Quoted 17 lines · Dana How
Quoted 16 lines · Jakub Narebski

That said, does git-showrel solution proposed by Johannes Schindelin in
Message-ID: <[email protected]>
http://permalink.gmane.org/gmane.comp.version-control.git/68840
work for you?

Below version of git-showrel script which uses proposed 'commit:./relpath' syntax (it could be improved, of course):

cat> git-showrel <<\EOF
#!/bin/sh

rel=$(git rev-parse --show-prefix 2>/dev/null)
git show $(echo "$@" | sed -e "s!:./!:${rel}!")

Quoted 7 lines
Quoted 1 line

EOF

It's definitely true I could use this for now. In the long run (meaning after the feature freeze) I don't view this as adequate for 2 reasons:
(1) I would like a consistent interpretation of commit:path wherever it is accepted; and

Of course this is only interim solution, after 1.5.4 is out, we are out of feature freeze, and <path>:./<relpath> is in.

Quoted 3 lines

(2) If a novice types bad arguments to git-showrel , they are probably going to be very confused by its error messages which are a response to a munged version of their command line.

Actually git-showrel should change only the _last_ argument, passing all other unchanged to git-show. But this requires something more than simplest script as above...

Unfortunately my shell script hackery is not up to the task ;-(

Signature · 2 lines
Jakub Narebski
Poland
#26 lore raw

PATCH 1/1 sha1_name: grok <revision>:./<relative-path>

In reply to Dana How

When you are in a deeply-nested directory structure, and just want to reference a blob in a past revision, it can be pretty slow to type out "HEAD~29:/bla/blub/.../that-file".

This patch makes "HEAD~29:./that-file" substitute the current prefix for "./". If there is not working directory, the prefix is empty.

Note that this patch does not handle "../", and neither do I plan to.

---

On Tue, 18 Dec 2007, Dana How wrote:

> On Dec 18, 2007 5:16 PM, Linus Torvalds
> <[email protected]> wrote:
> > On Tue, 18 Dec 2007, Dana How wrote:
> >
> > > The cases we are talking about are all subtrees of the > > > working tree. There is a useful cwd suffix.
> >
> > No.
> >
> > The cases we're talking of are *not* subtrees of the working > > tree.
> >
> > The SHA1 of a commit may well be a totally disjoint tree. Try > > it in the git repository with something like
>
> Agreed, but note you wrote *may*.

Okay, this is a proposed patch. It leaves the existing "HEAD:<path>" handling alone, and only touches "HEAD:./<path>", which would have been invalid anyway (except if you hacked your objects database to include a tree named ".").

Note: this patch is not meant for application directly. It should be split into get_current_prefix() as one patch, and the sha1_name.c stuff as the second. (Not only to boost my ohloh statistics, but because they are logically two separate things.)

Note, too: this is a quick and little-bit-dirty patch, not well tested. Particularly, I was unable to trigger the "No <path> in <rev>" error path, so I am not confident that this handling is correct.

Note also: in contrast to Alex' approach, this will not only work for git-show, but for all callers of get_sha1().

 cache.h     |    1 +
 setup.c     |   16 +++++++++++++---
 sha1_name.c |   17 ++++++++++++++---
 3 files changed, 28 insertions(+), 6 deletions(-)
cache.h +1 -0
index 39331c2..83a2c31 100644
@@ -225,6 +225,7 @@ extern char *get_index_file(void);
 extern char *get_graft_file(void);
 extern int set_git_dir(const char *path);
 extern const char *get_git_work_tree(void);
+extern const char *get_current_prefix(void);
 
 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
 
setup.c +13 -3
index b59dbe7..fb9b680 100644
@@ -3,6 +3,12 @@
 
 static int inside_git_dir = -1;
 static int inside_work_tree = -1;
+static const char *current_prefix;
+
+const char *get_current_prefix()
+{
+	return current_prefix;
+}
 
 const char *prefix_path(const char *prefix, int len, const char *path)
 {
@@ -267,6 +273,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
 				/* config may override worktree */
 				if (check_repository_format_gently(nongit_ok))
 					return NULL;
+				current_prefix = retval;
 				return retval;
 			}
 			if (check_repository_format_gently(nongit_ok))
@@ -279,7 +286,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
 			if (chdir(work_tree_env) < 0)
 				die ("Could not chdir to %s", work_tree_env);
 			strcat(buffer, "/");
-			return retval;
+			current_prefix = retval;
+			return current_prefix;
 		}
 		if (nongit_ok) {
 			*nongit_ok = 1;
@@ -339,7 +347,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
 	offset++;
 	cwd[len++] = '/';
 	cwd[len] = 0;
-	return cwd + offset;
+	current_prefix = cwd + offset;
+	return current_prefix;
 }
 
 int git_config_perm(const char *var, const char *value)
@@ -396,7 +405,8 @@ const char *setup_git_directory(void)
 		if (retval && chdir(retval))
 			die ("Could not jump back into original cwd");
 		rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
-		return rel && *rel ? strcat(rel, "/") : NULL;
+		current_prefix = rel && *rel ? strcat(rel, "/") : NULL;
+		return current_prefix;
 	}
 
 	return retval;
sha1_name.c +14 -3
index 13e1164..6f61d26 100644
@@ -712,9 +712,20 @@ int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
 	}
 	if (*cp == ':') {
 		unsigned char tree_sha1[20];
-		if (!get_sha1_1(name, cp-name, tree_sha1))
-			return get_tree_entry(tree_sha1, cp+1, sha1,
-					      mode);
+		if (!get_sha1_1(name, cp-name, tree_sha1)) {
+			const char *prefix;
+			if (!prefixcmp(cp + 1, "./") &&
+					(prefix = get_current_prefix())) {
+				unsigned char subtree_sha1[20];
+				if (get_tree_entry(tree_sha1, prefix,
+							subtree_sha1, mode))
+					return error("No '%s' in '%.*s'",
+							prefix, cp-name, name);
+				memcpy(tree_sha1, subtree_sha1, 20);
+				cp += 2;
+			}
+			return get_tree_entry(tree_sha1, cp+1, sha1, mode);
+		}
 	}
 	return ret;
 }
Signature · 1 line
1.5.4.rc0.72.g536e9
#27 lore raw

Re: [PATCH v0] sha1_name: grok <revision>:./<relative-path>

Quoted 8 lines · Johannes Schindelin

When you are in a deeply-nested directory structure, and just want to reference a blob in a past revision, it can be pretty slow to type out "HEAD~29:/bla/blub/.../that-file".

This patch makes "HEAD~29:./that-file" substitute the current prefix for "./". If there is not working directory, the prefix is empty.

Note that this patch does not handle "../", and neither do I plan to.

I think this is definitely the right approach. Here's a (possibly insane) alternative. Revert the change in get_sha1_with_mode and detect "./" in get_tree_entry:

tree-walk.c +6 -1
index 8d4b673..fc54354 100644
@@ -191,6 +191,7 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch
 	unsigned long size;
 	struct tree_desc t;
 	unsigned char root[20];
+	const char *prefix;
 
 	tree = read_object_with_reference(tree_sha1, tree_type, &size, root);
 	if (!tree)
@@ -202,7 +203,11 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch
 	}
 
 	init_tree_desc(&t, tree, size);
-	retval = find_tree_entry(&t, name, sha1, mode);
+	if (!prefixcmp(name, "./") && (prefix = get_current_prefix()))
+		retval = find_tree_entry(&t, mkpath("%s%s", prefix, name + 2),
+				sha1, mode);
+	else
+		retval = find_tree_entry(&t, name, sha1, mode);
 	free(tree);
 	return retval;
 }

This means that the directory '.' becomes a token replacement for "my current path" in tree paths. So if you are in "foo/bar", and you are looking at a distance commit where the same content was in "baz/foo/bar", you can do:

git show distant:baz/./file

This is probably insane because:
- this is a fairly unlikely use case
- get_tree_entry gets called in a lot of places, and I have no idea if there will be some crazy fallouts.

So it is probably not worth pursuing, but maybe somebody else can think of a good use.

-Peff

#28 lore raw

Re: [PATCH v0] sha1_name: grok <revision>:./<relative-path>

Quoted 21 lines · Jeff King
Quoted 6 lines · Johannes Schindelin

When you are in a deeply-nested directory structure, and just want to reference a blob in a past revision, it can be pretty slow to type out "HEAD~29:/bla/blub/.../that-file".

This patch makes "HEAD~29:./that-file" substitute the current prefix for "./". If there is not working directory, the prefix is empty.

I think this is definitely the right approach. Here's a (possibly insane) alternative. Revert the change in get_sha1_with_mode and detect "./" in get_tree_entry:

[..]

This means that the directory '.' becomes a token replacement for "my current path" in tree paths. So if you are in "foo/bar", and you are looking at a distance commit where the same content was in "baz/foo/bar", you can do:

git show distant:baz/./file

Quoted 7 lines

This is probably insane because:
- this is a fairly unlikely use case
- get_tree_entry gets called in a lot of places, and I have no idea if there will be some crazy fallouts.

So it is probably not worth pursuing, but maybe somebody else can think of a good use.

For me, I was only interested in the recognition of ./ at the beginning of a path just after : (causing a cwd suffix to be inserted there).

If there were additional /./ or /../ patterns in the result, I think it would be more useful (e.g. for script writers who forgot to run their file arguments thru something like "readlink -f") for them to be squashed out (e.g. in Perl:
s#/(\./)+#/#g;
s#/([^/]*[^./][^/]*/\.\./)+#/#g;
). But this is something that could be added later if desired to the interpretation of all paths, and so seems like a different issue.

Thanks,

Signature · 1 line
Dana L. How  [email protected]  +1 650 804 5991 cell
#29 lore raw

Re: [PATCH v0] sha1_name: grok <revision>:./<relative-path>

Dana How, Wed, Dec 19, 2007 18:40:04 +0100:

Quoted 8 lines

If there were additional /./ or /../ patterns in the result, I think it would be more useful (e.g. for script writers who forgot to run their file arguments thru something like "readlink -f") for them to be squashed out (e.g. in Perl:
s#/(\./)+#/#g;
s#/([^/]*[^./][^/]*/\.\./)+#/#g;
). But this is something that could be added later if desired to the interpretation of all paths, and so seems like a different issue.

This is what the pathexpand in pathexpand-patch does.

#30 lore raw

Re: [PATCH v0] sha1_name: grok <revision>:./<relative-path>

In reply to Johannes Schindelin
Quoted 17 lines · Johannes Schindelin
cache.h +1 -0
index 39331c2..83a2c31 100644
@@ -225,6 +225,7 @@ extern char *get_index_file(void);
 extern char *get_graft_file(void);
 extern int set_git_dir(const char *path);
 extern const char *get_git_work_tree(void);
+extern const char *get_current_prefix(void);
 
 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
 
setup.c +0 -0
index b59dbe7..fb9b680 100644

setup.c line 11

Quoted 9 lines
setup.c +6 -0
@@ -3,6 +3,12 @@
 
 static int inside_git_dir = -1;
 static int inside_work_tree = -1;
+static const char *current_prefix;
+
+const char *get_current_prefix()
+{
+	return current_prefix;
+}

Didn't you just make libification harder?

#31 lore raw

Re: [PATCH v0] sha1_name: grok <revision>:./<relative-path>

Hi,

Quoted 20 lines · Junio C Hamano
Quoted 18 lines · Johannes Schindelin
cache.h +1 -0
index 39331c2..83a2c31 100644
@@ -225,6 +225,7 @@ extern char *get_index_file(void);
 extern char *get_graft_file(void);
 extern int set_git_dir(const char *path);
 extern const char *get_git_work_tree(void);
+extern const char *get_current_prefix(void);
 
 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
 
setup.c +0 -0
index b59dbe7..fb9b680 100644
@@ -3,6 +3,12 @@
 
Quoted 10 lines

setup.c line 11

Quoted 8 lines
setup.c +6 -0
 static int inside_git_dir = -1;
 static int inside_work_tree = -1;
+static const char *current_prefix;
+
+const char *get_current_prefix()
+{
+	return current_prefix;
+}

Didn't you just make libification harder?

Well, yes.

Actually, no:

- I marked this explicitely not ready for application,
- it is not entirely clear if a libgit.a user would not want to set a default prefix, and
- I decided that I will not be the only one who tries to make libification easy. ;-)

Ciao,
Dscho

#32 lore raw

Re: [PATCH v0] sha1_name: grok <revision>:./<relative-path>

In reply to Johannes Schindelin
Quoted 9 lines · Johannes Schindelin

When you are in a deeply-nested directory structure, and just want to reference a blob in a past revision, it can be pretty slow to type out "HEAD~29:/bla/blub/.../that-file".

This patch makes "HEAD~29:./that-file" substitute the current prefix for "./". If there is not working directory, the prefix is empty.

Note that this patch does not handle "../", and neither do I plan to.

Junio's rc1 announcement got me to read this. It would be indeed useful as I usually work in deep subdirs. However, from my user perspective, the right approach is to make <treeish>:path always be relative to current directory. If you want absolute path, use <treeish>:/path. More intuitive but it breaks current behavior. Can we slowly migrate from current absolute-path-by-default behavior to relative-pat- by-default one? (I don't know how to make such migration smoothly though)

Quoted 148 lines

---

On Tue, 18 Dec 2007, Dana How wrote:

> On Dec 18, 2007 5:16 PM, Linus Torvalds
> <[email protected]> wrote:
> > On Tue, 18 Dec 2007, Dana How wrote:
> >
> > > The cases we are talking about are all subtrees of the > > > working tree. There is a useful cwd suffix.
> >
> > No.
> >
> > The cases we're talking of are *not* subtrees of the working > > tree.
> >
> > The SHA1 of a commit may well be a totally disjoint tree. Try > > it in the git repository with something like
>
> Agreed, but note you wrote *may*.

Okay, this is a proposed patch. It leaves the existing "HEAD:<path>" handling alone, and only touches "HEAD:./<path>", which would have been invalid anyway (except if you hacked your objects database to include a tree named ".").

Note: this patch is not meant for application directly. It should be split into get_current_prefix() as one patch, and the sha1_name.c stuff as the second. (Not only to boost my ohloh statistics, but because they are logically two separate things.)

Note, too: this is a quick and little-bit-dirty patch, not well tested. Particularly, I was unable to trigger the "No <path> in <rev>" error path, so I am not confident that this handling is correct.

Note also: in contrast to Alex' approach, this will not only work for git-show, but for all callers of get_sha1().

 cache.h     |    1 +
 setup.c     |   16 +++++++++++++---
 sha1_name.c |   17 ++++++++++++++---
 3 files changed, 28 insertions(+), 6 deletions(-)
cache.h +1 -0
index 39331c2..83a2c31 100644
@@ -225,6 +225,7 @@ extern char *get_index_file(void);
 extern char *get_graft_file(void);
 extern int set_git_dir(const char *path);
 extern const char *get_git_work_tree(void);
+extern const char *get_current_prefix(void);
 
 #define ALTERNATE_DB_ENVIRONMENT "GIT_ALTERNATE_OBJECT_DIRECTORIES"
 
setup.c +13 -3
index b59dbe7..fb9b680 100644
@@ -3,6 +3,12 @@
 
 static int inside_git_dir = -1;
 static int inside_work_tree = -1;
+static const char *current_prefix;
+
+const char *get_current_prefix()
+{
+       return current_prefix;
+}
 
 const char *prefix_path(const char *prefix, int len, const char *path)
 {
@@ -267,6 +273,7 @@ const char *setup_git_directory_gently(int *nongit_ok)
                                /* config may override worktree */
                                if (check_repository_format_gently(nongit_ok))
                                        return NULL;
+                               current_prefix = retval;
                                return retval;
                        }
                        if (check_repository_format_gently(nongit_ok))
@@ -279,7 +286,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
                        if (chdir(work_tree_env) < 0)
                                die ("Could not chdir to %s", work_tree_env);
                        strcat(buffer, "/");
-                       return retval;
+                       current_prefix = retval;
+                       return current_prefix;
                }
                if (nongit_ok) {
                        *nongit_ok = 1;
@@ -339,7 +347,8 @@ const char *setup_git_directory_gently(int *nongit_ok)
        offset++;
        cwd[len++] = '/';
        cwd[len] = 0;
-       return cwd + offset;
+       current_prefix = cwd + offset;
+       return current_prefix;
 }
 
 int git_config_perm(const char *var, const char *value)
@@ -396,7 +405,8 @@ const char *setup_git_directory(void)
                if (retval && chdir(retval))
                        die ("Could not jump back into original cwd");
                rel = get_relative_cwd(buffer, PATH_MAX, get_git_work_tree());
-               return rel && *rel ? strcat(rel, "/") : NULL;
+               current_prefix = rel && *rel ? strcat(rel, "/") : NULL;
+               return current_prefix;
        }
 
        return retval;
sha1_name.c +14 -4
index 13e1164..6f61d26 100644
@@ -712,9 +712,20 @@ int get_sha1_with_mode(const char *name, unsigned char *sha1, unsigned *mode)
        }
        if (*cp == ':') {
                unsigned char tree_sha1[20];
-               if (!get_sha1_1(name, cp-name, tree_sha1))
-                       return get_tree_entry(tree_sha1, cp+1, sha1,
-                                             mode);
+               if (!get_sha1_1(name, cp-name, tree_sha1)) {
+                       const char *prefix;
+                       if (!prefixcmp(cp + 1, "./") &&
+                                       (prefix = get_current_prefix())) {
+                               unsigned char subtree_sha1[20];
+                               if (get_tree_entry(tree_sha1, prefix,
+                                                       subtree_sha1, mode))
+                                       return error("No '%s' in '%.*s'",
+                                                       prefix, cp-name, name);
+                               memcpy(tree_sha1, subtree_sha1, 20);
+                               cp += 2;
+                       }
+                       return get_tree_entry(tree_sha1, cp+1, sha1, mode);
+               }
        }
        return ret;
 }
--

1.5.4.rc0.72.g536e9

-
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

Signature · 1 line
Duy
#33 lore raw

Re: [PATCH v0] sha1_name: grok <revision>:./<relative-path>

Quoted 7 lines · Nguyen Thai Ngoc Duy
Quoted 1 line

Note that this patch does not handle "../", and neither do I plan to.

Junio's rc1 announcement got me to read this. It would be indeed useful as I usually work in deep subdirs. However, from my user perspective, the right approach is to make <treeish>:path always be relative to current directory. If you want absolute path, use <treeish>:/path. More intuitive but it breaks current behavior.

I do not know if you followed the discussion thread, but the <treeish>:relative-path has been shown to be broken semantics, so even if it may be "intuitive", it is intuitive only to people who do not understand the brokenness. Please read the one that Dscho talks about windows drive letter and Linus agrees that is a good analogy.

It might be possible to do <commit>:relative and apply that only to direct user input, but I do not think it is worth the compatibility and complexity hassle.

#34 lore raw

Re: [PATCH v0] sha1_name: grok <revision>:./<relative-path>

Quoted 17 lines · Junio C Hamano
Quoted 7 lines · Nguyen Thai Ngoc Duy
Quoted 1 line

Note that this patch does not handle "../", and neither do I plan to.

Junio's rc1 announcement got me to read this. It would be indeed useful as I usually work in deep subdirs. However, from my user perspective, the right approach is to make <treeish>:path always be relative to current directory. If you want absolute path, use <treeish>:/path. More intuitive but it breaks current behavior.

I do not know if you followed the discussion thread, but the <treeish>:relative-path has been shown to be broken semantics, so even if it may be "intuitive", it is intuitive only to people who do not understand the brokenness. Please read the one that Dscho talks about windows drive letter and Linus agrees that is a good analogy.

Quoted 3 lines

It might be possible to do <commit>:relative and apply that only to direct user input, but I do not think it is worth the compatibility and complexity hassle.

Argh! I missed that thread. Will shut up now :-X Thanks for the pointer.

Signature · 1 line
Duy
#35 lore raw

Re: [PATCH v0] sha1_name: grok <revision>:./<relative-path>

In reply to Nguyen Thai Ngoc Duy

Hi,

Quoted 2 lines · Nguyen Thai Ngoc Duy

[...] from my user perspective, the right approach is to make <treeish>:path always be relative to current directory.

As said by Junio, this would be a bad decision.

BTW please do not quote parts of the email that you do not comment on; it takes half a minute of _everybody_ who tries to read your mail, only to realise that it was time wasted.

Ciao,
Dscho

#36 lore raw
In reply to Johannes Schindelin
Quoted 6 lines · Johannes Schindelin

And I still maintain that expecting <tree-ish>:<path> to take the current relative path into account would be just like if you expected

C:\> cd WINDOWS
C:\WINDOWS> dir D:system32

Quoted 8 lines

to show you the contents of D:\WINDOWS\system32.

Or another, less Windowsy example:

	$ cd /usr/bin
	$ scp home:bash ./

No, this does not copy home:/usr/bin/bash but home:$HOME/bash.

Please do not cc me on this topic, unless it is a patch implementing suggestion from Linus to treat $commit^{tree} as relative to $(cwd) but for only when the user directly specified that in full, including ^{tree} part.

#37 lore raw
In reply to Jakub Narebski
Quoted 6 lines · Jakub Narebski

What cwd? <path> in <tree-ish>:<path> syntax is "relative" to <tree-ish>. IMHO "<tree-ish>:<path>" should be considered (and is considered) as one object: current working directory doesn't matter at all there, contrary to "<tree-ish> -- <pathspec>" where it is natural that <pathspec> is relative to current working directory.

Indeed.

The <treeish>:<path> syntax *is* relative, but it's relative to the exact *treeish* that is given. It has nothing what-so-ever to do with the current working directory, since the user has explicitly given an exact tree object, and trying to fake that out would be actively wrong.

That said, I can kind of understand the wish for something like this, and I suspect that we could make the "commit->tree" translation take the current path into account. In other words, maybe we should have something like this:

	/*
	 * This sequence currently works
	 */
	[torvalds@woody git]$ git rev-parse HEAD
	f9c5a80cdf2265f2df7712fad9f1fb7ef68b4768

	[torvalds@woody git]$ git rev-parse HEAD^{tree}
	051fb0c0dff4371f97f8ad9407f9f1fd335b1682

	[torvalds@woody git]$ git rev-parse HEAD^{tree}:t
	49d8bcd7a2df5c17193b1d002c4a8489d4fa990c

	/*
	 * .. but this would be new
	 */
	[torvalds@woody git]$ cd t
	[torvalds@woody t]$ git rev-parse HEAD^{tree}
	49d8bcd7a2df5c17193b1d002c4a8489d4fa990c

where the magic is *not* done by any "SHA1 path lookup" at all, but is simply done by the commit->tree lookup. At least at that point it would make logical sense (although it would probably be quite painful to implement).

Linus

#38 lore raw
Quoted 8 lines · Linus Torvalds
Quoted 6 lines · Jakub Narebski

What cwd? <path> in <tree-ish>:<path> syntax is "relative" to <tree-ish>. IMHO "<tree-ish>:<path>" should be considered (and is considered) as one object: current working directory doesn't matter at all there, contrary to "<tree-ish> -- <pathspec>" where it is natural that <pathspec> is relative to current working directory.

Quoted 6 lines

Indeed.

The <treeish>:<path> syntax *is* relative, but it's relative to the exact *treeish* that is given. It has nothing what-so-ever to do with the current working directory, since the user has explicitly given an exact tree object, and trying to fake that out would be actively wrong.

I think the solution is to use the cwd only when the tree-ish refers to a commit.
If it refers explicitly to a tree (or to a tree through a tag w/o going through a commit)
then you don't make any modification.

When it *does* refer to a commit, then for commit:relpath you prefix relpath with the suffix of the cwd which is an extension beyond the root of the working tree.

At the time I thought this through submodules didn't exist. Clearly that case needs to be thought through as well.

Quoted 24 lines

That said, I can kind of understand the wish for something like this, and I suspect that we could make the "commit->tree" translation take the current path into account. In other words, maybe we should have something like this:

        /*
         * This sequence currently works
         */
        [torvalds@woody git]$ git rev-parse HEAD
        f9c5a80cdf2265f2df7712fad9f1fb7ef68b4768

        [torvalds@woody git]$ git rev-parse HEAD^{tree}
        051fb0c0dff4371f97f8ad9407f9f1fd335b1682

        [torvalds@woody git]$ git rev-parse HEAD^{tree}:t
        49d8bcd7a2df5c17193b1d002c4a8489d4fa990c

        /*
         * .. but this would be new
         */
        [torvalds@woody git]$ cd t
        [torvalds@woody t]$ git rev-parse HEAD^{tree}
        49d8bcd7a2df5c17193b1d002c4a8489d4fa990c
Quoted 4 lines

where the magic is *not* done by any "SHA1 path lookup" at all, but is simply done by the commit->tree lookup. At least at that point it would make logical sense (although it would probably be quite painful to implement).

I must be missing something. The old patch I submitted did this. Its defect was that it did NOT make the cwd insertion conditional on whether the tree-ish involved a commit or not (a test which also _seems_ doable, but I don't think I finished it & sent it in).

Thanks,

Signature · 1 line
Dana L. How  [email protected]  +1 650 804 5991 cell
#39 lore raw

Dana How, Tue, Dec 18, 2007 23:08:33 +0100:

Quoted 3 lines

When it *does* refer to a commit, then for commit:relpath you prefix relpath with the suffix of the cwd which is an extension beyond the root of the working tree.

...universally known as "prefix" (the last argument of builtin commands) :)

Quoted 9 lines
Quoted 4 lines

where the magic is *not* done by any "SHA1 path lookup" at all, but is simply done by the commit->tree lookup. At least at that point it would make logical sense (although it would probably be quite painful to implement).

I must be missing something. The old patch I submitted did this. Its defect was that it did NOT make the cwd insertion conditional on whether the tree-ish involved a commit or not (a test which also _seems_ doable, but I don't think I finished it & sent it in).

It is also a bit painful: lots of funtions (sha1_name.c) will have their prototypes changed to get the prefix (aka suffix). And their callers...

#40 lore raw
In reply to Linus Torvalds
Quoted 39 lines · Linus Torvalds
Quoted 6 lines · Jakub Narebski

What cwd? <path> in <tree-ish>:<path> syntax is "relative" to <tree-ish>. IMHO "<tree-ish>:<path>" should be considered (and is considered) as one object: current working directory doesn't matter at all there, contrary to "<tree-ish> -- <pathspec>" where it is natural that <pathspec> is relative to current working directory.

Indeed.

The <treeish>:<path> syntax *is* relative, but it's relative to the exact *treeish* that is given. It has nothing what-so-ever to do with the current working directory, since the user has explicitly given an exact tree object, and trying to fake that out would be actively wrong.

That said, I can kind of understand the wish for something like this, and I suspect that we could make the "commit->tree" translation take the current path into account. In other words, maybe we should have something like this:

	/*
	 * This sequence currently works
	 */
	[torvalds@woody git]$ git rev-parse HEAD
	f9c5a80cdf2265f2df7712fad9f1fb7ef68b4768

	[torvalds@woody git]$ git rev-parse HEAD^{tree}
	051fb0c0dff4371f97f8ad9407f9f1fd335b1682

	[torvalds@woody git]$ git rev-parse HEAD^{tree}:t
	49d8bcd7a2df5c17193b1d002c4a8489d4fa990c

	/*
	 * .. but this would be new
	 */
	[torvalds@woody git]$ cd t
	[torvalds@woody t]$ git rev-parse HEAD^{tree}
	49d8bcd7a2df5c17193b1d002c4a8489d4fa990c
Quoted 4 lines

where the magic is *not* done by any "SHA1 path lookup" at all, but is simply done by the commit->tree lookup. At least at that point it would make logical sense (although it would probably be quite painful to implement).

It is not just painful to implement.

Although I can buy that purely from the user (read: people who do not know how the world works) experience point of view, you have to be extra careful if you do this. There are existing codepaths that take a string that names a treeish from the end user, appends "^{tree}" to that string, and passes the result to get_sha1() to obtain a tree object name they want to operate on (the alternative is parse_tree_indirect() but it forces them to go through the object layer). You will need to update these callers to keep them working from subdirectories.

#41 lore raw
Quoted 8 lines · Junio C Hamano
Quoted 4 lines · Linus Torvalds

where the magic is *not* done by any "SHA1 path lookup" at all, but is simply done by the commit->tree lookup. At least at that point it would make logical sense (although it would probably be quite painful to implement).

It is not just painful to implement.

Quoted 8 lines

Although I can buy that purely from the user (read: people who do not know how the world works) experience point of view, you have to be extra careful if you do this. There are existing codepaths that take a string that names a treeish from the end user, appends "^{tree}" to that string, and passes the result to get_sha1() to obtain a tree object name they want to operate on (the alternative is parse_tree_indirect() but it forces them to go through the object layer). You will need to update these callers to keep them working from subdirectories.

Thanks, I didn't know about those "^{tree}" codepaths.

How about this:
<tree-ish>:./path -> NEW: relative
<tree-ish>:../path -> NEW: relative
<tree-ish>:?pattern -> NEW: same as next (current :/ ) <tree-ish>:/pattern -> unchanged (sha1_name_oneline IIRC) <tree-ish>:path -> unchanged: absolute
This shouldn't need to know if tree-ish references a commit or not.

Someday later, the last 2 cases could be changed to be more like the Unix command line if desired. This is very similar to Junio's response last May (by memory).

Typing an extra "./" is a big improvement over a long path prefix.

Thanks,

Signature · 1 line
Dana L. How  [email protected]  +1 650 804 5991 cell
#42 lore raw
Quoted 2 lines · Dana How

How about this:
<tree-ish>:./path -> NEW: relative

I think making "<tree>:./" mean "replace ./ with the current prefix and retry the usual expansion" is relatively confusion free. You have to work hard to confuse yourself:

	d=$(git rev-parse --verify HEAD:Documentation)
        git rev-parse "$d":./howto ;# would not work
	cd Documentation && git rev-parse "$d":./howto ;# would work
Quoted 1 line

<tree-ish>:../path -> NEW: relative

I would rather avoid up (../) processing if we can, but making "<tree>:" immediately followed by 1 or more "../" mean "take the current prefix, strip away the same number of trailing directory components as we have "../" there, and tuck the remainder in front of the path before trying the usual expansion" would be a natural and relatively confusion free extension of the above. I think I can live with that, too.

Quoted 1 line

<tree-ish>:?pattern -> NEW: same as next (current :/ )

I'd prefer keeping :? (or some other unlikely-in-path letter other than '?') as a special extension introducer character, i.e. leaving the door open to:

<tree>:?(magic)parameter

whose semantics is to be defined later, depending on "magic".

Similarly, we could do the same for

:?(magic)parameter

Quoted 1 line

<tree-ish>:/pattern -> unchanged (sha1_name_oneline IIRC)

I do not think this is oneline. "<commit>:/pattern" could be a natural extension to the current ":/pattern" that instructs "dig from only this commit, not all refs, and find a commit with the oneline," but I do not think it is a good idea. It is too similar to "locate this tree entry from the given tree" syntax.

Quoted 1 line

<tree-ish>:path -> unchanged: absolute

Absolutely ;-)

#43 lore raw
In reply to Dana How

Hi,

Quoted 4 lines · Dana How

[lots about rev:path taking path to be relative to the project root,
preferring it to be relative to the current prefix instead]

Typing an extra "./" is a big improvement over a long path prefix.

Have you tried the path completion?

Alternatively, I suggest making a script

	$ cat > $HOME/bin/git-showrel << \EOF
	#!/bin/sh
	git show "$(echo "$1" | sed "s|:|:$(git rev-parse --show-prefix)|")"
	EOF
	$ chmod a+x $HOME/bin/git-showrel

Then

	$ git showrel HEAD:file

will do what you want.

(An alias will not work, since the cwd will be the project root.)

Hth,
Dscho

P.S.: I just tried "git show HEAD~20:<filename with : in it>" and it did not work... I consider this a more serious issue than the relative path one.