From 9951d3b37ec7298384640b11d07a990ef031fe83 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Nov 2010 22:32:38 +0700 Subject: [PATCH] setup: clean up setup_discovered_git_dir() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit If core.bare is true, discard the discovered worktree, move back to original cwd. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Junio C Hamano --- setup.c | 72 +++++++++++++++++++++---------------------- t/t1510-repo-setup.sh | 24 +++++++-------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/setup.c b/setup.c index a6cc04473..3ee9c2eec 100644 --- a/setup.c +++ b/setup.c @@ -362,39 +362,27 @@ static const char *setup_explicit_git_dir(const char *gitdirenv, return retval; } -static int cwd_contains_git_dir(const char **gitfile_dirp) +static const char *setup_discovered_git_dir(const char *gitdir, + char *cwd, int offset, int len, + int *nongit_ok) { - const char *gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); - *gitfile_dirp = gitfile_dir; - if (gitfile_dir) { - if (set_git_dir(gitfile_dir)) - die("Repository setup failed"); - return 1; - } - if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) { - *gitfile_dirp = DEFAULT_GIT_DIR_ENVIRONMENT; - return 1; - } - return 0; -} + if (check_repository_format_gently(gitdir, nongit_ok)) + return NULL; -static const char *setup_discovered_git_dir(const char *work_tree_env, - const char *gitdir, - int offset, int len, - char *cwd, int *nongit_ok) -{ - int root_len; - char *work_tree; + /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */ + if (is_bare_repository_cfg > 0) { + set_git_dir(offset == len ? gitdir : make_absolute_path(gitdir)); + if (chdir(cwd)) + die_errno("Could not come back to cwd"); + return NULL; + } + /* #0, #1, #5, #8, #9, #12, #13 */ + set_git_work_tree("."); + if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT)) + set_git_dir(gitdir); inside_git_dir = 0; - if (!work_tree_env) - inside_work_tree = 1; - root_len = offset_1st_component(cwd); - 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; + inside_work_tree = 1; if (offset == len) return NULL; @@ -456,8 +444,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok) const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT); const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT); static char cwd[PATH_MAX+1]; - const char *gitdirenv; - const char *gitfile_dir; + const char *gitdirenv, *ret; + char *gitfile; int len, offset, ceil_offset; dev_t current_device = 0; int one_filesystem = 1; @@ -502,11 +490,23 @@ static const char *setup_git_directory_gently_1(int *nongit_ok) if (one_filesystem) current_device = get_device_or_die(".", NULL); for (;;) { - if (cwd_contains_git_dir(&gitfile_dir)) - return setup_discovered_git_dir(work_tree_env, - gitfile_dir, - offset, len, - cwd, nongit_ok); + gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT); + if (gitfile) + gitdirenv = gitfile = xstrdup(gitfile); + else { + if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) + gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT; + } + + if (gitdirenv) { + ret = setup_discovered_git_dir(gitdirenv, + cwd, offset, len, + nongit_ok); + free(gitfile); + return ret; + } + free(gitfile); + if (is_git_directory(".")) return setup_bare_git_dir(cwd, offset, len, nongit_ok); diff --git a/t/t1510-repo-setup.sh b/t/t1510-repo-setup.sh index a5f10a587..057f95944 100755 --- a/t/t1510-repo-setup.sh +++ b/t/t1510-repo-setup.sh @@ -2350,7 +2350,7 @@ EOF test_repo 16 ' -test_expect_failure '#16.2: in subdir' ' +test_expect_success '#16.2: in subdir' ' cat >16/sub/expected <17/sub/expected <20/sub/expected <21/sub/expected <24/expected <24/sub/expected <25/expected <25/sub/expected <28/expected <28/sub/expected <29/expected <29/sub/expected <