setup: clean up setup_discovered_git_dir()
If core.bare is true, discard the discovered worktree, move back to original cwd. 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
9951d3b37ec7298384640b11d07a990ef031fe83
2 files changed
+48
-48
setup.c
+36
-36
@@ -362,39 +362,27 @@ static const char *setup_explicit_git_dir(const char *gitdirenv,
362
return retval;
363
}
364
365
-static int cwd_contains_git_dir(const char **gitfile_dirp)
365
+static const char *setup_discovered_git_dir(const char *gitdir,
366
+ char *cwd, int offset, int len,
367
+ int *nongit_ok)
368
{
367
- const char *gitfile_dir = read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
368
- *gitfile_dirp = gitfile_dir;
369
- if (gitfile_dir) {
370
- if (set_git_dir(gitfile_dir))
371
- die("Repository setup failed");
372
- return 1;
373
- }
374
- if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT)) {
375
- *gitfile_dirp = DEFAULT_GIT_DIR_ENVIRONMENT;
376
- return 1;
377
- }
378
- return 0;
379
-}
369
+ if (check_repository_format_gently(gitdir, nongit_ok))
370
+ return NULL;
371
381
-static const char *setup_discovered_git_dir(const char *work_tree_env,
382
- const char *gitdir,
383
- int offset, int len,
384
- char *cwd, int *nongit_ok)
385
-{
386
- int root_len;
387
- char *work_tree;
372
+ /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
373
+ if (is_bare_repository_cfg > 0) {
374
+ set_git_dir(offset == len ? gitdir : make_absolute_path(gitdir));
375
+ if (chdir(cwd))
376
+ die_errno("Could not come back to cwd");
377
+ return NULL;
378
+ }
379
380
+ /* #0, #1, #5, #8, #9, #12, #13 */
381
+ set_git_work_tree(".");
382
+ if (strcmp(gitdir, DEFAULT_GIT_DIR_ENVIRONMENT))
383
+ set_git_dir(gitdir);
384
inside_git_dir = 0;
390
- if (!work_tree_env)
391
- inside_work_tree = 1;
392
- root_len = offset_1st_component(cwd);
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;
385
+ inside_work_tree = 1;
386
if (offset == len)
387
return NULL;
388
@@ -456,8 +444,8 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
444
const char *work_tree_env = getenv(GIT_WORK_TREE_ENVIRONMENT);
445
const char *env_ceiling_dirs = getenv(CEILING_DIRECTORIES_ENVIRONMENT);
446
static char cwd[PATH_MAX+1];
459
- const char *gitdirenv;
460
- const char *gitfile_dir;
447
+ const char *gitdirenv, *ret;
448
+ char *gitfile;
449
int len, offset, ceil_offset;
450
dev_t current_device = 0;
451
int one_filesystem = 1;
@@ -502,11 +490,23 @@ static const char *setup_git_directory_gently_1(int *nongit_ok)
490
if (one_filesystem)
491
current_device = get_device_or_die(".", NULL);
492
for (;;) {
505
- if (cwd_contains_git_dir(&gitfile_dir))
506
- return setup_discovered_git_dir(work_tree_env,
507
- gitfile_dir,
508
- offset, len,
509
- cwd, nongit_ok);
493
+ gitfile = (char*)read_gitfile_gently(DEFAULT_GIT_DIR_ENVIRONMENT);
494
+ if (gitfile)
495
+ gitdirenv = gitfile = xstrdup(gitfile);
496
+ else {
497
+ if (is_git_directory(DEFAULT_GIT_DIR_ENVIRONMENT))
498
+ gitdirenv = DEFAULT_GIT_DIR_ENVIRONMENT;
499
+ }
500
+
501
+ if (gitdirenv) {
502
+ ret = setup_discovered_git_dir(gitdirenv,
503
+ cwd, offset, len,
504
+ nongit_ok);
505
+ free(gitfile);
506
+ return ret;
507
+ }
508
+ free(gitfile);
509
+
510
if (is_git_directory("."))
511
return setup_bare_git_dir(cwd, offset, len, nongit_ok);
512
t/t1510-repo-setup.sh
+12
-12
@@ -2350,7 +2350,7 @@ EOF
2350
test_repo 16
2351
'
2352
2353
-test_expect_failure '#16.2: in subdir' '
2353
+test_expect_success '#16.2: in subdir' '
2354
cat >16/sub/expected <<EOF &&
2355
setup: git_dir: $TRASH_DIRECTORY/16/.git
2356
setup: worktree: (null)
@@ -2479,7 +2479,7 @@ EOF
2479
test_repo 17
2480
'
2481
2482
-test_expect_failure '#17.2: in subdir' '
2482
+test_expect_success '#17.2: in subdir' '
2483
cat >17/sub/expected <<EOF &&
2484
setup: git_dir: $TRASH_DIRECTORY/17/.git
2485
setup: worktree: (null)
@@ -2944,7 +2944,7 @@ EOF
2944
test_repo 20
2945
'
2946
2947
-test_expect_failure '#20.2: in subdir' '
2947
+test_expect_success '#20.2: in subdir' '
2948
cat >20/sub/expected <<EOF &&
2949
setup: git_dir: $TRASH_DIRECTORY/20/.git
2950
setup: worktree: (null)
@@ -3074,7 +3074,7 @@ EOF
3074
test_repo 21
3075
'
3076
3077
-test_expect_failure '#21.2: in subdir' '
3077
+test_expect_success '#21.2: in subdir' '
3078
cat >21/sub/expected <<EOF &&
3079
setup: git_dir: $TRASH_DIRECTORY/21/.git
3080
setup: worktree: (null)
@@ -3716,7 +3716,7 @@ test_expect_success '#24: setup' '
3716
cd ..
3717
'
3718
3719
-test_expect_failure '#24: at root' '
3719
+test_expect_success '#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_failure '#24: in subdir' '
3729
+test_expect_success '#24: in subdir' '
3730
cat >24/sub/expected <<EOF &&
3731
setup: git_dir: $TRASH_DIRECTORY/24.git
3732
setup: worktree: (null)
@@ -3766,7 +3766,7 @@ test_expect_success '#25: setup' '
3766
cd ..
3767
'
3768
3769
-test_expect_failure '#25: at root' '
3769
+test_expect_success '#25: at root' '
3770
cat >25/expected <<EOF &&
3771
setup: git_dir: $TRASH_DIRECTORY/25.git
3772
setup: worktree: (null)
@@ -3776,7 +3776,7 @@ EOF
3776
test_repo 25
3777
'
3778
3779
-test_expect_failure '#25: in subdir' '
3779
+test_expect_success '#25: in subdir' '
3780
cat >25/sub/expected <<EOF &&
3781
setup: git_dir: $TRASH_DIRECTORY/25.git
3782
setup: worktree: (null)
@@ -4151,7 +4151,7 @@ test_expect_success '#28: setup' '
4151
cd ..
4152
'
4153
4154
-test_expect_failure '#28: at root' '
4154
+test_expect_success '#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_failure '#28: in subdir' '
4164
+test_expect_success '#28: in subdir' '
4165
cat >28/sub/expected <<EOF &&
4166
setup: git_dir: $TRASH_DIRECTORY/28.git
4167
setup: worktree: (null)
@@ -4201,7 +4201,7 @@ test_expect_success '#29: setup' '
4201
cd ..
4202
'
4203
4204
-test_expect_failure '#29: at root' '
4204
+test_expect_success '#29: at root' '
4205
cat >29/expected <<EOF &&
4206
setup: git_dir: $TRASH_DIRECTORY/29.git
4207
setup: worktree: (null)
@@ -4211,7 +4211,7 @@ EOF
4211
test_repo 29
4212
'
4213
4214
-test_expect_failure '#29: in subdir' '
4214
+test_expect_success '#29: in subdir' '
4215
cat >29/sub/expected <<EOF &&
4216
setup: git_dir: $TRASH_DIRECTORY/29.git
4217
setup: worktree: (null)