Skip to contentSkip to conversation

worktree: provide better prefix to go back to original cwd

PATCH In review Nguyễn Thái Ngọc Duy 4 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 worktree: provide better prefix to go back to original cwd

When both GIT_DIR and GIT_WORK_TREE are set, if cwd is outside worktree, prefix (the one passed to every builtin commands) will be set to NULL, which means "user stays at worktree topdir".

As a consequence, command line arguments are supposed to be relative to worktree topdir, not current working directory. Not very intuitive. Moreover, output from such situation is (again) relative to worktree topdir. Users are expected to understand that.

This patch allows builtin commands access to original cwd even if it's outside worktree, via cwd_to_worktree and worktree_to_cwd fields. As the name implies, if you stay at original cwd, "cd $(cwd_to_worktree)" would take you to worktree topdir and vice versa.

---
startup_info->cwd_to_worktree would be as same as opt.submodule_prefix in your 2/3 patch.

 builtin/rev-parse.c        |   10 ++++
 cache.h                    |    2 +
 setup.c                    |  108 ++++++++++++++++++++++++++++++++++++++++++--
 t/t1510-worktree-prefix.sh |   52 +++++++++++++++++++++
 4 files changed, 168 insertions(+), 4 deletions(-)
 create mode 100755 t/t1510-worktree-prefix.sh
builtin/rev-parse.c +10 -0
index a5a1c86..525610e 100644
@@ -623,6 +623,16 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 					puts(prefix);
 				continue;
 			}
+			if (!strcmp(arg, "--cwd-to-worktree")) {
+				if (startup_info->cwd_to_worktree)
+					puts(startup_info->cwd_to_worktree);
+				continue;
+			}
+			if (!strcmp(arg, "--worktree-to-cwd")) {
+				if (startup_info->worktree_to_cwd)
+					puts(startup_info->worktree_to_cwd);
+				continue;
+			}
 			if (!strcmp(arg, "--show-cdup")) {
 				const char *pfx = prefix;
 				if (!is_inside_work_tree()) {
cache.h +2 -0
index 2ef2fa3..8256e1c 100644
@@ -1110,6 +1110,8 @@ const char *split_cmdline_strerror(int cmdline_errno);
 /* git.c */
 struct startup_info {
 	int have_repository;
+	char *cwd_to_worktree; /* chdir("this"); from cwd would return to worktree */
+	char *worktree_to_cwd; /* chdir("this"); from worktree would return to cwd */
 };
 extern struct startup_info *startup_info;
 
setup.c +104 -4

123 lines not on this page · raw message

t/t1510-worktree-prefix.sh new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
1.7.0.2.445.gcbdb3
#1 lore raw
Quoted 8 lines · Nguyễn Thái Ngọc Duy

This patch allows builtin commands access to original cwd even if it's outside worktree, via cwd_to_worktree and worktree_to_cwd fields.

builtin/rev-parse.c +0 -0
@@ -623,6 +623,16 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 					puts(prefix);
 				continue;
 			}

builtin/rev-parse.c int cmd_rev_parse(int argc, const char **argv, const char *prefix) · line 635

Quoted 10 lines
builtin/rev-parse.c +10 -0
+			if (!strcmp(arg, "--cwd-to-worktree")) {
+				if (startup_info->cwd_to_worktree)
+					puts(startup_info->cwd_to_worktree);
+				continue;
+			}
+			if (!strcmp(arg, "--worktree-to-cwd")) {
+				if (startup_info->worktree_to_cwd)
+					puts(startup_info->worktree_to_cwd);
+				continue;
+			}

Nice.

I wonder if this should use something like

	else
		puts(".");

or

	else
		putchar('\n');

. What would be most convenient for scripted callers?

What do these commands do when run from a bare repository? Is the worktree the .git dir in that case, do they fail, or does something else happen?

Are there any examples to illustrate whether teaching --show-prefix to do what your --worktree-to-cwd does would be a good or bad idea? (Just curious.)

cache.h const char *split_cmdline_strerror(int cmdline_errno); · line 1114

Quoted 8 lines
cache.h +2 -0
@@ -1110,6 +1110,8 @@ const char *split_cmdline_strerror(int cmdline_errno);
 /* git.c */
 struct startup_info {
 	int have_repository;
+	char *cwd_to_worktree; /* chdir("this"); from cwd would return to worktree */
+	char *worktree_to_cwd; /* chdir("this"); from worktree would return to cwd */

Comment nit: would

				/* path from original cwd to worktree */
				/* path from worktree to original cwd */

be clearer? But presumably any confused people should be able to find your log message.

setup.c const char *read_gitfile_gently(const char *path) · line 320

Quoted 11 lines
setup.c +5 -0
@@ -313,10 +313,109 @@ const char *read_gitfile_gently(const char *path)
 	return path;
 }
 
+/*
+ * Given "foo/bar" and "hey/hello/world", return "../../hey/hello/world/"
+ * Either path1 or path2 can be NULL
+ */
+static char *make_path_to_path(const char *path1, const char *path2)

Nice. Do we need to worry about:

- alternate directory separators? (hey\hello\world)
- DOS drive prefix? (c:\foo\bar, d:\hey\hello\world)
- relative paths with DOS drive? (c:\foo\bar, d:hello)
- doubled-up directory separators? (hey//hello/world, //foo/bar)
- non-canonical paths? (hey/./hello/../hello/world)

I'm guessing some of the answers are "no", depending on where these paths come from. Compare make_relative_path().
[...]

setup.c const char *read_gitfile_gently(const char *path) · line 418

Quoted 5 lines
setup.c +1 -1
 static const char *setup_explicit_git_dir(const char *gitdirenv,
 				const char *work_tree_env, int *nongit_ok)
 {
-	static char buffer[1024 + 1];
+	static char buffer[PATH_MAX];

Why?

It might make sense to error out a little before PATH_MAX (though later than 1024), to account for subdirs (e.g., objects/). Not sure.

#2 lore raw
Quoted 3 lines · Jonathan Nieder

Are there any examples to illustrate whether teaching --show-prefix to do what your --worktree-to-cwd does would be a good or bad idea? (Just curious.)

Do these additions interact well with the notion of "we are in the working tree" vs "we are outside the working tree"? Even when we happen to know via GIT_WORK_TREE that the root of the working tree is /var/tmp/junk, we should correctly diagnse that we are outside the working tree when we are in /var/tmp/, and require_work_tree should say "no you are not allowed to do this", no?

Quoted 11 lines

cache.h const char *split_cmdline_strerror(int cmdline_errno); · line 1114

Quoted 8 lines
cache.h +2 -0
@@ -1110,6 +1110,8 @@ const char *split_cmdline_strerror(int cmdline_errno);
 /* git.c */
 struct startup_info {
 	int have_repository;
+	char *cwd_to_worktree; /* chdir("this"); from cwd would return to worktree */
+	char *worktree_to_cwd; /* chdir("this"); from worktree would return to cwd */

Comment nit: would

Quoted 4 lines
				/* path from original cwd to worktree */
				/* path from worktree to original cwd */

be clearer?

Much better.

#3 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Jonathan Nieder

Are there any examples to illustrate whether teaching --show-prefix to do what your --worktree-to-cwd does would be a good or bad idea? (Just curious.)

Quoted 6 lines

Do these additions interact well with the notion of "we are in the working tree" vs "we are outside the working tree"?  Even when we happen to know via GIT_WORK_TREE that the root of the working tree is /var/tmp/junk, we should correctly diagnse that we are outside the working tree when we are in /var/tmp/, and require_work_tree should say "no you are not allowed to do this", no?

It should work as it does now. I don't change prefix calculation and chdir() behavior. So when you're at /var/tmp, require_work_tree should say no.

Signature · 1 line
Duy
#4 lore raw
In reply to Jonathan Nieder

2010/10/7 Jonathan Nieder <[email protected]>:

Quoted 28 lines
Quoted 8 lines · Nguyễn Thái Ngọc Duy

This patch allows builtin commands access to original cwd even if it's outside worktree, via cwd_to_worktree and worktree_to_cwd fields.

builtin/rev-parse.c +0 -0
@@ -623,6 +623,16 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
                                       puts(prefix);
                               continue;
                       }

builtin/rev-parse.c int cmd_rev_parse(int argc, const char **argv, const char *prefix) · line 635

Quoted 10 lines
builtin/rev-parse.c +10 -0
+                     if (!strcmp(arg, "--cwd-to-worktree")) {
+                             if (startup_info->cwd_to_worktree)
+                                     puts(startup_info->cwd_to_worktree);
+                             continue;
+                     }
+                     if (!strcmp(arg, "--worktree-to-cwd")) {
+                             if (startup_info->worktree_to_cwd)
+                                     puts(startup_info->worktree_to_cwd);
+                             continue;
+                     }

Nice.

I wonder if this should use something like

       else
               puts(".");

Quoted 6 lines

or

       else
               putchar('\n');

.  What would be most convenient for scripted callers?

I followed the convention in rev-parse.c, specifically --show-prefix code. Don't know if it's good or bad for scripts though.

Quoted 3 lines

What do these commands do when run from a bare repository?  Is the worktree the .git dir in that case, do they fail, or does something else happen?

These two new fields are only set when both GIT_DIR and GIT_WORK_TREE are set, which means non-bare repository. I'll need to handle other setup cases as well, but for subproject-aware grep, this should be enough.

Quoted 3 lines

Are there any examples to illustrate whether teaching --show-prefix to do what your --worktree-to-cwd does would be a good or bad idea? (Just curious.)

I think it's a bad idea. --show-prefix traditionally never returns "../(something)" and scripts (even builtin commands) depend on that. It's better to slowly migrate over the new prefix(es) when it makes sense.

Quoted 14 lines

setup.c const char *read_gitfile_gently(const char *path) · line 320

Quoted 11 lines
setup.c +5 -0
@@ -313,10 +313,109 @@ const char *read_gitfile_gently(const char *path)
       return path;
  }
 
+/*
+ * Given "foo/bar" and "hey/hello/world", return "../../hey/hello/world/"
+ * Either path1 or path2 can be NULL
+ */
+static char *make_path_to_path(const char *path1, const char *path2)

Nice.  Do we need to worry about:

Quoted 8 lines

 - alternate directory separators? (hey\hello\world)
 - DOS drive prefix? (c:\foo\bar, d:\hey\hello\world)
 - relative paths with DOS drive? (c:\foo\bar, d:hello)
 - doubled-up directory separators? (hey//hello/world, //foo/bar)
 - non-canonical paths? (hey/./hello/../hello/world)

I'm guessing some of the answers are "no", depending on where these paths come from.  Compare make_relative_path().

The two last ones are OK. Paths passed to this function come from either getcwd or make_absolute_path(). I'm sure they are canonicalized. But I'll add a comment to note that.

I'll look into backslashes and DOS drives (hmm and UNC path too).

Quoted 8 lines

[...]

Quoted 5 lines

 static const char *setup_explicit_git_dir(const char *gitdirenv, const char *work_tree_env, int *nongit_ok) {
-     static char buffer[1024 + 1];
+     static char buffer[PATH_MAX];

Quoted 4 lines

Why?

It might make sense to error out a little before PATH_MAX (though later than 1024), to account for subdirs (e.g., objects/).  Not sure.

I'm not really POSIX-fluent to tell. But I'm sure in this function there won't be any subdirs.

Signature · 1 line
Duy
#5 lore raw
In reply to Nguyễn Thái Ngọc Duy
Quoted 14 lines · Nguyễn Thái Ngọc Duy

When both GIT_DIR and GIT_WORK_TREE are set, if cwd is outside worktree, prefix (the one passed to every builtin commands) will be set to NULL, which means "user stays at worktree topdir".

As a consequence, command line arguments are supposed to be relative to worktree topdir, not current working directory. Not very intuitive. Moreover, output from such situation is (again) relative to worktree topdir. Users are expected to understand that.

This patch allows builtin commands access to original cwd even if it's outside worktree, via cwd_to_worktree and worktree_to_cwd fields. As the name implies, if you stay at original cwd, "cd $(cwd_to_worktree)" would take you to worktree topdir and vice versa.

Quoted 4 lines

---
startup_info->cwd_to_worktree would be as same as opt.submodule_prefix in your 2/3 patch.

Thanks. I must admit I was struggling a little with implementing this part. I'll be sending out a re-roll of my series shortly and I'll include your patch if Junio doesn't pick it up on its own. I was also planning on re-basing my patches on top of next or pu so more people can actually compile it.