@matheusbsbs / matheus-bagliano / commits / e6aea2dba2
Fork 0

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 <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 e6aea2dba27798f5d1eca32435e407541caca400
5 files changed +51 -37
builtin/init-db.c
+1
@@ -496,6 +496,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
496 if (is_bare_repository_cfg < 0)
497 is_bare_repository_cfg = guess_repository_type(git_dir);
498
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, '/');
cache.h
+1
@@ -1119,6 +1119,7 @@ const char *split_cmdline_strerror(int cmdline_errno);
1119 /* git.c */
1120 struct startup_info {
1121 int have_repository;
1122 + int setup_explicit;
1123 };
1124 extern struct startup_info *startup_info;
1125
environment.c
+8 -2
@@ -137,8 +137,6 @@ static int git_work_tree_initialized;
137 */
138 void set_git_work_tree(const char *new_work_tree)
139 {
140 - if (is_bare_repository_cfg >= 0)
141 - die("cannot set work tree after initialization");
140 git_work_tree_initialized = 1;
141 free(work_tree);
142 work_tree = xstrdup(make_absolute_path(new_work_tree));
@@ -147,6 +145,14 @@ void set_git_work_tree(const char *new_work_tree)
145
146 const char *get_git_work_tree(void)
147 {
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 */
setup.c
+8 -2
@@ -331,6 +331,8 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
331 static char buffer[1024 + 1];
332 const char *retval;
333
334 + if (startup_info)
335 + startup_info->setup_explicit = 1;
336 if (PATH_MAX - 40 < strlen(gitdirenv))
337 die("'$%s' too big", GIT_DIR_ENVIRONMENT);
338 if (!is_git_directory(gitdirenv)) {
@@ -382,12 +384,15 @@ static const char *setup_discovered_git_dir(const char *work_tree_env,
384 char *cwd, int *nongit_ok)
385 {
386 int root_len;
387 + char *work_tree;
388
389 inside_git_dir = 0;
390 if (!work_tree_env)
391 inside_work_tree = 1;
392 root_len = offset_1st_component(cwd);
390 - git_work_tree_cfg = xstrndup(cwd, offset > root_len ? offset : root_len);
393 + work_tree = xstrndup(cwd, offset > root_len ? offset : root_len);
394 + set_git_work_tree(work_tree);
395 + free(work_tree);
396 if (check_repository_format_gently(gitdir, nongit_ok))
397 return NULL;
398 if (offset == len)
@@ -627,7 +632,8 @@ const char *setup_git_directory(void)
632 const char *retval = setup_git_directory_gently(NULL);
633
634 /* If the work tree is not the default one, recompute prefix */
630 - if (inside_work_tree < 0) {
635 + if ((!startup_info || startup_info->setup_explicit) &&
636 + inside_work_tree < 0) {
637 static char buffer[PATH_MAX + 1];
638 char *rel;
639 if (retval && chdir(retval))
t/t1510-repo-setup.sh
+33 -33
@@ -132,7 +132,7 @@ test_expect_success '#1: setup' '
132 cd ..
133 '
134
135 -test_expect_failure '#1: at root' '
135 +test_expect_success '#1: at root' '
136 cat >1/expected <<EOF &&
137 setup: git_dir: .git
138 setup: worktree: $TRASH_DIRECTORY/1
@@ -142,7 +142,7 @@ EOF
142 test_repo 1
143 '
144
145 -test_expect_failure '#1: in subdir' '
145 +test_expect_success '#1: in subdir' '
146 cat >1/sub/expected <<EOF &&
147 setup: git_dir: .git
148 setup: worktree: $TRASH_DIRECTORY/1
@@ -512,7 +512,7 @@ test_expect_success '#4: setup' '
512 cd ..
513 '
514
515 -test_expect_failure '#4: at root' '
515 +test_expect_success '#4: at root' '
516 cat >4/expected <<EOF &&
517 setup: git_dir: .git
518 setup: worktree: $TRASH_DIRECTORY/4
@@ -522,7 +522,7 @@ EOF
522 test_repo 4
523 '
524
525 -test_expect_failure '#4: in subdir' '
525 +test_expect_success '#4: in subdir' '
526 cat >4/sub/expected <<EOF &&
527 setup: git_dir: .git
528 setup: worktree: $TRASH_DIRECTORY/4
@@ -560,7 +560,7 @@ test_expect_success '#5: setup' '
560 cd ..
561 '
562
563 -test_expect_failure '#5: at root' '
563 +test_expect_success '#5: at root' '
564 cat >5/expected <<EOF &&
565 setup: git_dir: .git
566 setup: worktree: $TRASH_DIRECTORY/5
@@ -570,7 +570,7 @@ EOF
570 test_repo 5
571 '
572
573 -test_expect_failure '#5: in subdir' '
573 +test_expect_success '#5: in subdir' '
574 cat >5/sub/expected <<EOF &&
575 setup: git_dir: .git
576 setup: worktree: $TRASH_DIRECTORY/5
@@ -1213,7 +1213,7 @@ test_expect_success '#9: setup' '
1213 cd ..
1214 '
1215
1216 -test_expect_failure '#9: at root' '
1216 +test_expect_success '#9: at root' '
1217 cat >9/expected <<EOF &&
1218 setup: git_dir: $TRASH_DIRECTORY/9.git
1219 setup: worktree: $TRASH_DIRECTORY/9
@@ -1223,7 +1223,7 @@ EOF
1223 test_repo 9
1224 '
1225
1226 -test_expect_failure '#9: in subdir' '
1226 +test_expect_success '#9: in subdir' '
1227 cat >9/sub/expected <<EOF &&
1228 setup: git_dir: $TRASH_DIRECTORY/9.git
1229 setup: worktree: $TRASH_DIRECTORY/9
@@ -1596,7 +1596,7 @@ test_expect_success '#12: setup' '
1596 cd ..
1597 '
1598
1599 -test_expect_failure '#12: at root' '
1599 +test_expect_success '#12: at root' '
1600 cat >12/expected <<EOF &&
1601 setup: git_dir: $TRASH_DIRECTORY/12.git
1602 setup: worktree: $TRASH_DIRECTORY/12
@@ -1606,7 +1606,7 @@ EOF
1606 test_repo 12
1607 '
1608
1609 -test_expect_failure '#12: in subdir' '
1609 +test_expect_success '#12: in subdir' '
1610 cat >12/sub/expected <<EOF &&
1611 setup: git_dir: $TRASH_DIRECTORY/12.git
1612 setup: worktree: $TRASH_DIRECTORY/12
@@ -1646,7 +1646,7 @@ test_expect_success '#13: setup' '
1646 cd ..
1647 '
1648
1649 -test_expect_failure '#13: at root' '
1649 +test_expect_success '#13: at root' '
1650 cat >13/expected <<EOF &&
1651 setup: git_dir: $TRASH_DIRECTORY/13.git
1652 setup: worktree: $TRASH_DIRECTORY/13
@@ -1656,7 +1656,7 @@ EOF
1656 test_repo 13
1657 '
1658
1659 -test_expect_failure '#13: in subdir' '
1659 +test_expect_success '#13: in subdir' '
1660 cat >13/sub/expected <<EOF &&
1661 setup: git_dir: $TRASH_DIRECTORY/13.git
1662 setup: worktree: $TRASH_DIRECTORY/13
@@ -2388,7 +2388,7 @@ test_expect_success '#17.1: setup' '
2388 cd ..
2389 '
2390
2391 -test_expect_failure '#17.1: at .git' '
2391 +test_expect_success '#17.1: at .git' '
2392 cat >17/.git/expected <<EOF &&
2393 setup: git_dir: .
2394 setup: worktree: (null)
@@ -2398,7 +2398,7 @@ EOF
2398 test_repo 17/.git
2399 '
2400
2401 -test_expect_failure '#17.1: in .git/wt' '
2401 +test_expect_success '#17.1: in .git/wt' '
2402 cat >17/.git/wt/expected <<EOF &&
2403 setup: git_dir: $TRASH_DIRECTORY/17/.git
2404 setup: worktree: (null)
@@ -2408,7 +2408,7 @@ EOF
2408 test_repo 17/.git/wt
2409 '
2410
2411 -test_expect_failure '#17.1: in .git/wt/sub' '
2411 +test_expect_success '#17.1: in .git/wt/sub' '
2412 cat >17/.git/wt/sub/expected <<EOF &&
2413 setup: git_dir: $TRASH_DIRECTORY/17/.git
2414 setup: worktree: (null)
@@ -2439,7 +2439,7 @@ test_expect_success '#17.2: setup' '
2439 git config --file="$TRASH_DIRECTORY/17/.git/config" core.bare true
2440 '
2441
2442 -test_expect_failure '#17.2: at .git' '
2442 +test_expect_success '#17.2: at .git' '
2443 cat >17/.git/expected <<EOF &&
2444 setup: git_dir: .
2445 setup: worktree: (null)
@@ -2449,7 +2449,7 @@ EOF
2449 test_repo 17/.git
2450 '
2451
2452 -test_expect_failure '#17.2: in .git/wt' '
2452 +test_expect_success '#17.2: in .git/wt' '
2453 cat >17/.git/wt/expected <<EOF &&
2454 setup: git_dir: $TRASH_DIRECTORY/17/.git
2455 setup: worktree: (null)
@@ -2459,7 +2459,7 @@ EOF
2459 test_repo 17/.git/wt
2460 '
2461
2462 -test_expect_failure '#17.2: in .git/wt/sub' '
2462 +test_expect_success '#17.2: in .git/wt/sub' '
2463 cat >17/.git/wt/sub/expected <<EOF &&
2464 setup: git_dir: $TRASH_DIRECTORY/17/.git
2465 setup: worktree: (null)
@@ -2469,7 +2469,7 @@ EOF
2469 test_repo 17/.git/wt/sub
2470 '
2471
2472 -test_expect_failure '#17.2: at root' '
2472 +test_expect_success '#17.2: at root' '
2473 cat >17/expected <<EOF &&
2474 setup: git_dir: .git
2475 setup: worktree: (null)
@@ -2853,7 +2853,7 @@ test_expect_success '#20.1: setup' '
2853 cd ..
2854 '
2855
2856 -test_expect_failure '#20.1: at .git' '
2856 +test_expect_success '#20.1: at .git' '
2857 cat >20/.git/expected <<EOF &&
2858 setup: git_dir: .
2859 setup: worktree: (null)
@@ -2863,7 +2863,7 @@ EOF
2863 test_repo 20/.git
2864 '
2865
2866 -test_expect_failure '#20.1: in .git/wt' '
2866 +test_expect_success '#20.1: in .git/wt' '
2867 cat >20/.git/wt/expected <<EOF &&
2868 setup: git_dir: $TRASH_DIRECTORY/20/.git
2869 setup: worktree: (null)
@@ -2873,7 +2873,7 @@ EOF
2873 test_repo 20/.git/wt
2874 '
2875
2876 -test_expect_failure '#20.1: in .git/wt/sub' '
2876 +test_expect_success '#20.1: in .git/wt/sub' '
2877 cat >20/.git/wt/sub/expected <<EOF &&
2878 setup: git_dir: $TRASH_DIRECTORY/20/.git
2879 setup: worktree: (null)
@@ -2983,7 +2983,7 @@ test_expect_success '#21.1: setup' '
2983 cd ..
2984 '
2985
2986 -test_expect_failure '#21.1: at .git' '
2986 +test_expect_success '#21.1: at .git' '
2987 cat >21/.git/expected <<EOF &&
2988 setup: git_dir: .
2989 setup: worktree: (null)
@@ -2993,7 +2993,7 @@ EOF
2993 test_repo 21/.git
2994 '
2995
2996 -test_expect_failure '#21.1: in .git/wt' '
2996 +test_expect_success '#21.1: in .git/wt' '
2997 cat >21/.git/wt/expected <<EOF &&
2998 setup: git_dir: $TRASH_DIRECTORY/21/.git
2999 setup: worktree: (null)
@@ -3003,7 +3003,7 @@ EOF
3003 test_repo 21/.git/wt
3004 '
3005
3006 -test_expect_failure '#21.1: in .git/wt/sub' '
3006 +test_expect_success '#21.1: in .git/wt/sub' '
3007 cat >21/.git/wt/sub/expected <<EOF &&
3008 setup: git_dir: $TRASH_DIRECTORY/21/.git
3009 setup: worktree: (null)
@@ -3034,7 +3034,7 @@ test_expect_success '#21.2: setup' '
3034 git config --file="$TRASH_DIRECTORY/21/.git/config" core.bare true
3035 '
3036
3037 -test_expect_failure '#21.2: at .git' '
3037 +test_expect_success '#21.2: at .git' '
3038 cat >21/.git/expected <<EOF &&
3039 setup: git_dir: .
3040 setup: worktree: (null)
@@ -3044,7 +3044,7 @@ EOF
3044 test_repo 21/.git
3045 '
3046
3047 -test_expect_failure '#21.2: in .git/wt' '
3047 +test_expect_success '#21.2: in .git/wt' '
3048 cat >21/.git/wt/expected <<EOF &&
3049 setup: git_dir: $TRASH_DIRECTORY/21/.git
3050 setup: worktree: (null)
@@ -3054,7 +3054,7 @@ EOF
3054 test_repo 21/.git/wt
3055 '
3056
3057 -test_expect_failure '#21.2: in .git/wt/sub' '
3057 +test_expect_success '#21.2: in .git/wt/sub' '
3058 cat >21/.git/wt/sub/expected <<EOF &&
3059 setup: git_dir: $TRASH_DIRECTORY/21/.git
3060 setup: worktree: (null)
@@ -3064,7 +3064,7 @@ EOF
3064 test_repo 21/.git/wt/sub
3065 '
3066
3067 -test_expect_failure '#21.2: at root' '
3067 +test_expect_success '#21.2: at root' '
3068 cat >21/expected <<EOF &&
3069 setup: git_dir: .git
3070 setup: worktree: (null)
@@ -3716,7 +3716,7 @@ test_expect_success '#24: setup' '
3716 cd ..
3717 '
3718
3719 -test_expect_success '#24: at root' '
3719 +test_expect_failure '#24: at root' '
3720 cat >24/expected <<EOF &&
3721 setup: git_dir: $TRASH_DIRECTORY/24.git
3722 setup: worktree: (null)
@@ -3726,7 +3726,7 @@ EOF
3726 test_repo 24
3727 '
3728
3729 -test_expect_success '#24: in subdir' '
3729 +test_expect_failure '#24: in subdir' '
3730 cat >24/sub/expected <<EOF &&
3731 setup: git_dir: $TRASH_DIRECTORY/24.git
3732 setup: worktree: (null)
@@ -4151,7 +4151,7 @@ test_expect_success '#28: setup' '
4151 cd ..
4152 '
4153
4154 -test_expect_success '#28: at root' '
4154 +test_expect_failure '#28: at root' '
4155 cat >28/expected <<EOF &&
4156 setup: git_dir: $TRASH_DIRECTORY/28.git
4157 setup: worktree: (null)
@@ -4161,7 +4161,7 @@ EOF
4161 test_repo 28
4162 '
4163
4164 -test_expect_success '#28: in subdir' '
4164 +test_expect_failure '#28: in subdir' '
4165 cat >28/sub/expected <<EOF &&
4166 setup: git_dir: $TRASH_DIRECTORY/28.git
4167 setup: worktree: (null)