From: Nguyễn Thái Ngọc Duy Date: Fri, 26 Nov 2010 15:32:35 +0000 (+0700) Subject: setup: limit get_git_work_tree()'s to explicit setup case only X-Git-Tag: v1.7.4-rc0~4^2~9 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=e6aea2dba27798f5d1eca32435e407541caca400;p=git.git setup: limit get_git_work_tree()'s to explicit setup case only get_git_work_tree() takes input as core.worktree, core.bare, GIT_WORK_TREE and decides correct worktree setting. Unfortunately it does not do its job well. core.worktree and GIT_WORK_TREE should only be taken into account, if GIT_DIR is set (which is handled by setup_explicit_git_dir). For other setup cases, only core.bare matters. Add a temporary variable setup_explicit to adjust get_git_work_tree() behavior as such. This variable will be gone once setup_* rework is done. Also remove is_bare_repository_cfg check in set_git_work_tree() to ease the rework. We are going to check for core.bare and core.worktree early before setting worktree. For example, if core.bare is true, no need to set worktree. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- diff --git a/builtin/init-db.c b/builtin/init-db.c index 9d4886c71..ea064784d 100644 --- a/builtin/init-db.c +++ b/builtin/init-db.c @@ -496,6 +496,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix) if (is_bare_repository_cfg < 0) is_bare_repository_cfg = guess_repository_type(git_dir); + startup_info->setup_explicit = 1; if (!is_bare_repository_cfg) { if (git_dir) { const char *git_dir_parent = strrchr(git_dir, '/'); diff --git a/cache.h b/cache.h index 123dd4bb9..b2cdda714 100644 --- a/cache.h +++ b/cache.h @@ -1119,6 +1119,7 @@ const char *split_cmdline_strerror(int cmdline_errno); /* git.c */ struct startup_info { int have_repository; + int setup_explicit; }; extern struct startup_info *startup_info; diff --git a/environment.c b/environment.c index de5581fe5..d811049a7 100644 --- a/environment.c +++ b/environment.c @@ -137,8 +137,6 @@ static int git_work_tree_initialized; */ void set_git_work_tree(const char *new_work_tree) { - if (is_bare_repository_cfg >= 0) - die("cannot set work tree after initialization"); git_work_tree_initialized = 1; free(work_tree); work_tree = xstrdup(make_absolute_path(new_work_tree)); @@ -147,6 +145,14 @@ void set_git_work_tree(const char *new_work_tree) const char *get_git_work_tree(void) { + if (startup_info && !startup_info->setup_explicit) { + if (is_bare_repository_cfg == 1) + return NULL; + if (work_tree) + is_bare_repository_cfg = 0; + return work_tree; + } + if (!git_work_tree_initialized) { work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT); /* core.bare = true overrides implicit and config work tree */ diff --git a/setup.c b/setup.c index 49a1a2525..c7d71986c 100644 --- a/setup.c +++ b/setup.c @@ -331,6 +331,8 @@ static const char *setup_explicit_git_dir(const char *gitdirenv, static char buffer[1024 + 1]; const char *retval; + if (startup_info) + startup_info->setup_explicit = 1; if (PATH_MAX - 40 < strlen(gitdirenv)) die("'$%s' too big", GIT_DIR_ENVIRONMENT); if (!is_git_directory(gitdirenv)) { @@ -382,12 +384,15 @@ static const char *setup_discovered_git_dir(const char *work_tree_env, char *cwd, int *nongit_ok) { int root_len; + char *work_tree; inside_git_dir = 0; if (!work_tree_env) inside_work_tree = 1; root_len = offset_1st_component(cwd); - git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len); + work_tree = xstrndup(cwd, offset > root_len ? offset : root_len); + set_git_work_tree(work_tree); + free(work_tree); if (check_repository_format_gently(gitdir, nongit_ok)) return NULL; if (offset == len) @@ -627,7 +632,8 @@ const char *setup_git_directory(void) const char *retval = setup_git_directory_gently(NULL); /* If the work tree is not the default one, recompute prefix */ - if (inside_work_tree < 0) { + if ((!startup_info || startup_info->setup_explicit) && + inside_work_tree < 0) { static char buffer[PATH_MAX + 1]; char *rel; if (retval && chdir(retval)) diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index fbab9c7e6..a5f10a587 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -132,7 +132,7 @@ test_expect_success '#1: setup' ' cd .. ' -test_expect_failure '#1: at root' ' +test_expect_success '#1: at root' ' cat >1/expected <1/sub/expected <4/expected <4/sub/expected <5/expected <5/sub/expected <9/expected <9/sub/expected <12/expected <12/sub/expected <13/expected <13/sub/expected <17/.git/expected <17/.git/wt/expected <17/.git/wt/sub/expected <17/.git/expected <17/.git/wt/expected <17/.git/wt/sub/expected <17/expected <20/.git/expected <20/.git/wt/expected <20/.git/wt/sub/expected <21/.git/expected <21/.git/wt/expected <21/.git/wt/sub/expected <21/.git/expected <21/.git/wt/expected <21/.git/wt/sub/expected <21/expected <24/expected <24/sub/expected <28/expected <28/sub/expected <