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

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).