git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] setup_git_directory: Setup cwd properly if worktree is found
@ 2007-11-12 11:24 Nguyễn Thái Ngọc Duy
  2007-11-12 11:57 ` Johannes Schindelin
  2007-11-12 12:53 ` Johannes Sixt
  0 siblings, 2 replies; 6+ messages in thread
From: Nguyễn Thái Ngọc Duy @ 2007-11-12 11:24 UTC (permalink / raw)
  To: git; +Cc: Junio C Hamano, Johannes Schindelin

This makes sure (if possible) the current working directory is at
root worktree. "git rev-parse --show-cwd" is added to aid the tests.
Also the first test is for "Add missing inside_work_tree" commit.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 builtin-rev-parse.c |    6 ++++++
 setup.c             |    7 ++++++-
 t/t1501-worktree.sh |   33 +++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+), 1 deletions(-)

diff --git a/builtin-rev-parse.c b/builtin-rev-parse.c
index 8d78b69..933875c 100644
--- a/builtin-rev-parse.c
+++ b/builtin-rev-parse.c
@@ -319,6 +319,12 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 					puts(prefix);
 				continue;
 			}
+			if (!strcmp(arg, "--show-cwd")) {
+				char cwd[PATH_MAX+1];
+				getcwd(cwd, sizeof(cwd));
+				printf("%s\n", cwd);
+				continue;
+			}
 			if (!strcmp(arg, "--show-cdup")) {
 				const char *pfx = prefix;
 				if (!is_inside_work_tree()) {
diff --git a/setup.c b/setup.c
index 6f8f769..d90f65e 100644
--- a/setup.c
+++ b/setup.c
@@ -360,7 +360,12 @@ 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;
+		if (rel && *rel) {
+			if (chdir(get_git_work_tree()))
+				die ("Could not chdir to %s", get_git_work_tree());
+			return strcat(rel, "/");
+		}
+		return NULL;
 	}
 
 	return retval;
diff --git a/t/t1501-worktree.sh b/t/t1501-worktree.sh
index 7ee3820..a4e1f72 100755
--- a/t/t1501-worktree.sh
+++ b/t/t1501-worktree.sh
@@ -30,6 +30,18 @@ test_rev_parse() {
 
 mkdir -p work/sub/dir || exit 1
 mv .git repo.git || exit 1
+export SAVED_WORK_DIR=$(pwd)/work
+
+say "GIT_WORK_TREE without core.worktree"
+
+export GIT_DIR="$(pwd)"/repo.git
+export GIT_CONFIG="$GIT_DIR"/config
+export GIT_WORK_TREE="$(pwd)"/work
+
+test_expect_success 'cwd is set properly' '
+	(cd work/sub &&
+	 test "$(git rev-parse --show-cwd)" = "$SAVED_WORK_DIR")
+'
 
 say "core.worktree = relative path"
 export GIT_DIR=repo.git
@@ -47,6 +59,11 @@ export GIT_CONFIG="$(pwd)"/$GIT_DIR/config
 test_rev_parse 'subdirectory' false false true sub/dir/
 cd ../../.. || exit 1
 
+test_expect_success 'cwd is set properly' '
+	(cd work/sub &&
+	 test "$(GIT_DIR=../../repo.git git rev-parse --show-cwd)" = "$SAVED_WORK_DIR")
+'
+
 say "core.worktree = absolute path"
 export GIT_DIR=$(pwd)/repo.git
 export GIT_CONFIG=$GIT_DIR/config
@@ -58,6 +75,11 @@ cd sub/dir || exit 1
 test_rev_parse 'subdirectory' false false true sub/dir/
 cd ../../.. || exit 1
 
+test_expect_success 'cwd is set properly' '
+	(cd work/sub &&
+	 test "$(git rev-parse --show-cwd)" = "$SAVED_WORK_DIR")
+'
+
 say "GIT_WORK_TREE=relative path (override core.worktree)"
 export GIT_DIR=$(pwd)/repo.git
 export GIT_CONFIG=$GIT_DIR/config
@@ -72,7 +94,13 @@ export GIT_WORK_TREE=../..
 test_rev_parse 'subdirectory' false false true sub/dir/
 cd ../../.. || exit 1
 
+test_expect_success 'cwd is set properly' '
+	(cd work/sub &&
+	 test "$(GIT_WORK_TREE=.. git rev-parse --show-cwd)" = "$SAVED_WORK_DIR")
+'
+
 mv work repo.git/work
+export SAVED_WORK_DIR=$(pwd)/repo.git/work
 
 say "GIT_WORK_TREE=absolute path, work tree below git dir"
 export GIT_DIR=$(pwd)/repo.git
@@ -89,6 +117,11 @@ cd sub/dir || exit 1
 test_rev_parse 'in repo.git/sub/dir' false true true sub/dir/
 cd ../../../.. || exit 1
 
+test_expect_success 'cwd is set properly' '
+	(cd repo.git/work/sub &&
+	 test "$(git rev-parse --show-cwd)" = "$SAVED_WORK_DIR")
+'
+
 test_expect_success 'repo finds its work tree' '
 	(cd repo.git &&
 	 : > work/sub/dir/untracked &&
-- 
1.5.3.5.475.gd7a30

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] setup_git_directory: Setup cwd properly if worktree is found
  2007-11-12 11:24 [PATCH] setup_git_directory: Setup cwd properly if worktree is found Nguyễn Thái Ngọc Duy
@ 2007-11-12 11:57 ` Johannes Schindelin
       [not found]   ` <fcaeb9bf0711120413w180c07e1qbf1b186753593d7@mail.gmail.com>
  2007-11-12 12:53 ` Johannes Sixt
  1 sibling, 1 reply; 6+ messages in thread
From: Johannes Schindelin @ 2007-11-12 11:57 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy; +Cc: git, Junio C Hamano

[-- Attachment #1: Type: TEXT/PLAIN, Size: 1605 bytes --]

Hi,

On Mon, 12 Nov 2007, Nguyễn Thái Ngọc Duy wrote:

> diff --git a/setup.c b/setup.c
> index 6f8f769..d90f65e 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -360,7 +360,12 @@ 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;
> +		if (rel && *rel) {
> +			if (chdir(get_git_work_tree()))
> +				die ("Could not chdir to %s", get_git_work_tree());
> +			return strcat(rel, "/");
> +		}
> +		return NULL;

Hmm.  Maybe this needs a bit more clarification?

When setup_git_directory() returns, the cwd is supposed to be the current 
working tree's root.  Your patch fixes that nicely when the worktree 
setting was overridden by the config (which is read in 
check_repository_format()).

But what about setup_git_directory_gently()?  If the working tree is 
overridden by the config, this function is still bogus, right?

As far as I see, setup_git_directory_gently() only works correctly when 
core.worktree is _not_ set, unless GIT_WORK_TREE is set (which is supposed 
to override the config setting).  Note: I treat GIT_WORK_TREE the same as 
--work-tree, since at that time they are identical.

Maybe the config stuff has to move into _gently()?

Ciao,
Dscho

P.S.: Thanks for picking up the ball.  At some stage I got so fed up with 
the work-tree stuff that I had to take a long break from it.  It is a 
messy concept, and the implementation is messy, partly because of it, and 
partly because I wrote it.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] setup_git_directory: Setup cwd properly if worktree is found
       [not found]   ` <fcaeb9bf0711120413w180c07e1qbf1b186753593d7@mail.gmail.com>
@ 2007-11-12 12:31     ` Johannes Schindelin
  2007-11-27 14:12       ` Nguyen Thai Ngoc Duy
  0 siblings, 1 reply; 6+ messages in thread
From: Johannes Schindelin @ 2007-11-12 12:31 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: git, Junio C Hamano

Hi,

On Mon, 12 Nov 2007, Nguyen Thai Ngoc Duy wrote:

> On Nov 12, 2007 6:57 PM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
>
> > But what about setup_git_directory_gently()?  If the working tree is 
> > overridden by the config, this function is still bogus, right?
> 
> Hmm.. thinking a little bit more. I guess you're right because 
> GIT_WORK_TREE takes precedence over core.worktree. Maybe some more bits 
> for check_repository_format_version(). Tough decision because, from the 
> value of inside_work_tree, we don't know if we can safely skip 
> overriding inside_work_tree.

I was thinking about adding check_repository_format_version() and a check 
for inside_work_tree < 0 with obvious handling in two places, probably as 
a function:  first, when we have a gitdirenv but no work_tree_env, and 
second, at the end of _gently() when we found a git dir but only if 
work_tree_env was not set.

> > As far as I see, setup_git_directory_gently() only works correctly 
> > when core.worktree is _not_ set, unless GIT_WORK_TREE is set (which is 
> > supposed to override the config setting).  Note: I treat GIT_WORK_TREE 
> > the same as --work-tree, since at that time they are identical.
> >
> > Maybe the config stuff has to move into _gently()?
> 
> Well, it could be a bit more complicated because you need to know
> GIT_DIR first before reading config. I'd rather not move as _gently()
> is complicated already.

AFAICT it is not a question of complexity, but of correctness.  Wouldn't 
you agree that the prefix _gently() returns is wrong if we don't fix it?

Besides, it might be needed anyway if we are serious about the version 
check.  This check, however, would have to be done _whenever_ we found a 
git directory, not only when work_tree_env is NULL.

But we could break down _gently() quite nicely.  ATM it both handles the 
cases when gitdirenv was set, and when it was unset.  Even if those are 
really two pretty different situations.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] setup_git_directory: Setup cwd properly if worktree is found
  2007-11-12 11:24 [PATCH] setup_git_directory: Setup cwd properly if worktree is found Nguyễn Thái Ngọc Duy
  2007-11-12 11:57 ` Johannes Schindelin
@ 2007-11-12 12:53 ` Johannes Sixt
  1 sibling, 0 replies; 6+ messages in thread
From: Johannes Sixt @ 2007-11-12 12:53 UTC (permalink / raw)
  To: Nguyễn Thái Ngọc Duy
  Cc: git, Junio C Hamano, Johannes Schindelin

Nguyễn Thái Ngọc Duy schrieb:
> +export SAVED_WORK_DIR=$(pwd)/work
> +export GIT_DIR="$(pwd)"/repo.git
> +export GIT_CONFIG="$GIT_DIR"/config
> +export GIT_WORK_TREE="$(pwd)"/work

These are not very portable; use:

    GIT_DIR="$(pwd)"/repo.git
    GIT_CONFIG="$GIT_DIR"/config
    GIT_WORK_TREE="$(pwd)"/work
    export GIT_DIR GIT_CONFIG GIT_WORK_TREE

The first one also needs $(pwd) quoted.

-- Hannes

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] setup_git_directory: Setup cwd properly if worktree is found
  2007-11-12 12:31     ` Johannes Schindelin
@ 2007-11-27 14:12       ` Nguyen Thai Ngoc Duy
  2007-11-27 14:46         ` Johannes Schindelin
  0 siblings, 1 reply; 6+ messages in thread
From: Nguyen Thai Ngoc Duy @ 2007-11-27 14:12 UTC (permalink / raw)
  To: Johannes Schindelin; +Cc: git, Junio C Hamano

On Nov 12, 2007 7:31 PM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> Hi,
>
> On Mon, 12 Nov 2007, Nguyen Thai Ngoc Duy wrote:
>
> > On Nov 12, 2007 6:57 PM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:
> >
> > > But what about setup_git_directory_gently()?  If the working tree is
> > > overridden by the config, this function is still bogus, right?
> >
> > Hmm.. thinking a little bit more. I guess you're right because
> > GIT_WORK_TREE takes precedence over core.worktree. Maybe some more bits
> > for check_repository_format_version(). Tough decision because, from the
> > value of inside_work_tree, we don't know if we can safely skip
> > overriding inside_work_tree.
>
> I was thinking about adding check_repository_format_version() and a check
> for inside_work_tree < 0 with obvious handling in two places, probably as
> a function:  first, when we have a gitdirenv but no work_tree_env, and
> second, at the end of _gently() when we found a git dir but only if
> work_tree_env was not set.
>
> > > As far as I see, setup_git_directory_gently() only works correctly
> > > when core.worktree is _not_ set, unless GIT_WORK_TREE is set (which is
> > > supposed to override the config setting).  Note: I treat GIT_WORK_TREE
> > > the same as --work-tree, since at that time they are identical.
> > >
> > > Maybe the config stuff has to move into _gently()?
> >
> > Well, it could be a bit more complicated because you need to know
> > GIT_DIR first before reading config. I'd rather not move as _gently()
> > is complicated already.
>
> AFAICT it is not a question of complexity, but of correctness.  Wouldn't
> you agree that the prefix _gently() returns is wrong if we don't fix it?
>
> Besides, it might be needed anyway if we are serious about the version
> check.  This check, however, would have to be done _whenever_ we found a
> git directory, not only when work_tree_env is NULL.

Question time. setup_git_directory_gently() can be happy even if there
is no repository. Now if we move version check into setup_..._gently
and it finds git program is too old to handle the repository, what
would we do? die() like in check_repository_format() or tell the
caller there is no repository?
-- 
Duy

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] setup_git_directory: Setup cwd properly if worktree is found
  2007-11-27 14:12       ` Nguyen Thai Ngoc Duy
@ 2007-11-27 14:46         ` Johannes Schindelin
  0 siblings, 0 replies; 6+ messages in thread
From: Johannes Schindelin @ 2007-11-27 14:46 UTC (permalink / raw)
  To: Nguyen Thai Ngoc Duy; +Cc: git, Junio C Hamano

Hi,

On Tue, 27 Nov 2007, Nguyen Thai Ngoc Duy wrote:

> Question time. setup_git_directory_gently() can be happy even if there 
> is no repository. Now if we move version check into setup_..._gently and 
> it finds git program is too old to handle the repository, what would we 
> do? die() like in check_repository_format() or tell the caller there is 
> no repository?

In the interest of least surprise, die().

Ciao,
Dscho
 

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-11-27 14:47 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-11-12 11:24 [PATCH] setup_git_directory: Setup cwd properly if worktree is found Nguyễn Thái Ngọc Duy
2007-11-12 11:57 ` Johannes Schindelin
     [not found]   ` <fcaeb9bf0711120413w180c07e1qbf1b186753593d7@mail.gmail.com>
2007-11-12 12:31     ` Johannes Schindelin
2007-11-27 14:12       ` Nguyen Thai Ngoc Duy
2007-11-27 14:46         ` Johannes Schindelin
2007-11-12 12:53 ` Johannes Sixt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).