@matheusbsbs / matheus-bagliano / commits / 835183603b
Fork 0

Remove all logic from get_git_work_tree()

This logic is now only used by cmd_init_db(). setup_* functions do not rely on it any more. Move all the logic to cmd_init_db() and turn get_git_work_tree() into a simple function. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Nov 26, 2010 at 22:32 UTC 835183603b4aed84797f60c91c5ebd79be7b2e11
3 files changed +19 -28
builtin/init-db.c
+11 -3
@@ -414,6 +414,7 @@ static const char *const init_db_usage[] = {
414 int cmd_init_db(int argc, const char **argv, const char *prefix)
415 {
416 const char *git_dir;
417 + const char *work_tree;
418 const char *template_dir = NULL;
419 unsigned int flags = 0;
420 const struct option init_db_options[] = {
@@ -480,8 +481,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
481 * without --bare. Catch the error early.
482 */
483 git_dir = getenv(GIT_DIR_ENVIRONMENT);
483 - if ((!git_dir || is_bare_repository_cfg == 1)
484 - && getenv(GIT_WORK_TREE_ENVIRONMENT))
484 + work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
485 + if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
486 die("%s (or --work-tree=<directory>) not allowed without "
487 "specifying %s (or --git-dir=<directory>)",
488 GIT_WORK_TREE_ENVIRONMENT,
@@ -496,7 +497,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
497 if (is_bare_repository_cfg < 0)
498 is_bare_repository_cfg = guess_repository_type(git_dir);
499
499 - startup_info->setup_explicit = 1;
500 if (!is_bare_repository_cfg) {
501 if (git_dir) {
502 const char *git_dir_parent = strrchr(git_dir, '/');
@@ -511,10 +511,18 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
511 if (!getcwd(git_work_tree_cfg, PATH_MAX))
512 die_errno ("Cannot access current working directory");
513 }
514 + if (work_tree)
515 + set_git_work_tree(make_absolute_path(work_tree));
516 + else
517 + set_git_work_tree(git_work_tree_cfg);
518 if (access(get_git_work_tree(), X_OK))
519 die_errno ("Cannot access work tree '%s'",
520 get_git_work_tree());
521 }
522 + else {
523 + if (work_tree)
524 + set_git_work_tree(make_absolute_path(work_tree));
525 + }
526
527 set_git_dir(make_absolute_path(git_dir));
528
cache.h
-1
@@ -1119,7 +1119,6 @@ const char *split_cmdline_strerror(int cmdline_errno);
1119 /* git.c */
1120 struct startup_info {
1121 int have_repository;
1122 - int setup_explicit;
1122 };
1123 extern struct startup_info *startup_info;
1124
environment.c
+8 -24
@@ -137,36 +137,20 @@ static int git_work_tree_initialized;
137 */
138 void set_git_work_tree(const char *new_work_tree)
139 {
140 + if (git_work_tree_initialized) {
141 + new_work_tree = make_absolute_path(new_work_tree);
142 + if (strcmp(new_work_tree, work_tree))
143 + die("internal error: work tree has already been set\n"
144 + "Current worktree: %s\nNew worktree: %s",
145 + work_tree, new_work_tree);
146 + return;
147 + }
148 git_work_tree_initialized = 1;
141 - free(work_tree);
149 work_tree = xstrdup(make_absolute_path(new_work_tree));
143 - is_bare_repository_cfg = 0;
150 }
151
152 const char *get_git_work_tree(void)
153 {
148 - if (startup_info && !startup_info->setup_explicit) {
149 - if (is_bare_repository_cfg == 1)
150 - return NULL;
151 - if (work_tree)
152 - is_bare_repository_cfg = 0;
153 - return work_tree;
154 - }
155 -
156 - if (!git_work_tree_initialized) {
157 - work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
158 - /* core.bare = true overrides implicit and config work tree */
159 - if (!work_tree && is_bare_repository_cfg < 1) {
160 - work_tree = git_work_tree_cfg;
161 - /* make_absolute_path also normalizes the path */
162 - if (work_tree && !is_absolute_path(work_tree))
163 - work_tree = xstrdup(make_absolute_path(git_path("%s", work_tree)));
164 - } else if (work_tree)
165 - work_tree = xstrdup(make_absolute_path(work_tree));
166 - git_work_tree_initialized = 1;
167 - if (work_tree)
168 - is_bare_repository_cfg = 0;
169 - }
154 return work_tree;
155 }
156