]> git.ipfire.org Git - thirdparty/git.git/commitdiff
Merge branch 'nd/init-core-worktree-in-multi-worktree-world'
authorJunio C Hamano <gitster@pobox.com>
Mon, 3 Oct 2016 20:30:34 +0000 (13:30 -0700)
committerJunio C Hamano <gitster@pobox.com>
Mon, 3 Oct 2016 20:30:35 +0000 (13:30 -0700)
"git init" tried to record core.worktree in the repository's
'config' file when GIT_WORK_TREE environment variable was set and
it was different from where GIT_DIR appears as ".git" at its top,
but the logic was faulty when .git is a "gitdir:" file that points
at the real place, causing trouble in working trees that are
managed by "git worktree".  This has been corrected.

* nd/init-core-worktree-in-multi-worktree-world:
  init: kill git_link variable
  init: do not set unnecessary core.worktree
  init: kill set_git_dir_init()
  init: call set_git_dir_init() from within init_db()
  init: correct re-initialization from a linked worktree

builtin/clone.c
builtin/init-db.c
cache.h
t/t0001-init.sh

index 28ce9383a14ced7ea3f135bdc0d42226aaf790af..fb75f7ee64a29e4bb79f163e82c73bb8a7bd5280 100644 (file)
@@ -935,16 +935,10 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                set_git_work_tree(work_tree);
        }
 
-       junk_git_dir = git_dir;
+       junk_git_dir = real_git_dir ? real_git_dir : git_dir;
        if (safe_create_leading_directories_const(git_dir) < 0)
                die(_("could not create leading directories of '%s'"), git_dir);
 
-       set_git_dir_init(git_dir, real_git_dir, 0);
-       if (real_git_dir) {
-               git_dir = real_git_dir;
-               junk_git_dir = real_git_dir;
-       }
-
        if (0 <= option_verbosity) {
                if (option_bare)
                        fprintf(stderr, _("Cloning into bare repository '%s'...\n"), dir);
@@ -970,7 +964,11 @@ int cmd_clone(int argc, const char **argv, const char *prefix)
                }
        }
 
-       init_db(option_template, INIT_DB_QUIET);
+       init_db(git_dir, real_git_dir, option_template, INIT_DB_QUIET);
+
+       if (real_git_dir)
+               git_dir = real_git_dir;
+
        write_config(&option_config);
 
        git_config(git_default_config, NULL);
index 72e81447ae84c8e69799fff5a6fe7050241bea9b..2399b97d902668a08c3c881d544f4d6a88f4e263 100644 (file)
@@ -22,7 +22,6 @@
 static int init_is_bare_repository = 0;
 static int init_shared_repository = -1;
 static const char *init_db_template_dir;
-static const char *git_link;
 
 static void copy_templates_1(struct strbuf *path, struct strbuf *template,
                             DIR *dir)
@@ -138,7 +137,7 @@ static void copy_templates(const char *template_dir)
                goto close_free_return;
        }
 
-       strbuf_addstr(&path, get_git_dir());
+       strbuf_addstr(&path, get_git_common_dir());
        strbuf_complete(&path, '/');
        copy_templates_1(&path, &template_path, dir);
 close_free_return:
@@ -171,7 +170,8 @@ static int needs_work_tree_config(const char *git_dir, const char *work_tree)
        return 1;
 }
 
-static int create_default_files(const char *template_path)
+static int create_default_files(const char *template_path,
+                               const char *original_git_dir)
 {
        struct stat st1;
        struct strbuf buf = STRBUF_INIT;
@@ -264,7 +264,7 @@ static int create_default_files(const char *template_path)
                /* allow template config file to override the default */
                if (log_all_ref_updates == -1)
                        git_config_set("core.logallrefupdates", "true");
-               if (needs_work_tree_config(get_git_dir(), work_tree))
+               if (needs_work_tree_config(original_git_dir, work_tree))
                        git_config_set("core.worktree", work_tree);
        }
 
@@ -312,34 +312,7 @@ static void create_object_directory(void)
        strbuf_release(&path);
 }
 
-int set_git_dir_init(const char *git_dir, const char *real_git_dir,
-                    int exist_ok)
-{
-       if (real_git_dir) {
-               struct stat st;
-
-               if (!exist_ok && !stat(git_dir, &st))
-                       die(_("%s already exists"), git_dir);
-
-               if (!exist_ok && !stat(real_git_dir, &st))
-                       die(_("%s already exists"), real_git_dir);
-
-               /*
-                * make sure symlinks are resolved because we'll be
-                * moving the target repo later on in separate_git_dir()
-                */
-               git_link = xstrdup(real_path(git_dir));
-               set_git_dir(real_path(real_git_dir));
-       }
-       else {
-               set_git_dir(real_path(git_dir));
-               git_link = NULL;
-       }
-       startup_info->have_repository = 1;
-       return 0;
-}
-
-static void separate_git_dir(const char *git_dir)
+static void separate_git_dir(const char *git_dir, const char *git_link)
 {
        struct stat st;
 
@@ -360,13 +333,31 @@ static void separate_git_dir(const char *git_dir)
        write_file(git_link, "gitdir: %s", git_dir);
 }
 
-int init_db(const char *template_dir, unsigned int flags)
+int init_db(const char *git_dir, const char *real_git_dir,
+           const char *template_dir, unsigned int flags)
 {
        int reinit;
-       const char *git_dir = get_git_dir();
+       int exist_ok = flags & INIT_DB_EXIST_OK;
+       char *original_git_dir = xstrdup(real_path(git_dir));
+
+       if (real_git_dir) {
+               struct stat st;
+
+               if (!exist_ok && !stat(git_dir, &st))
+                       die(_("%s already exists"), git_dir);
+
+               if (!exist_ok && !stat(real_git_dir, &st))
+                       die(_("%s already exists"), real_git_dir);
 
-       if (git_link)
-               separate_git_dir(git_dir);
+               set_git_dir(real_path(real_git_dir));
+               git_dir = get_git_dir();
+               separate_git_dir(git_dir, original_git_dir);
+       }
+       else {
+               set_git_dir(real_path(git_dir));
+               git_dir = get_git_dir();
+       }
+       startup_info->have_repository = 1;
 
        safe_create_dir(git_dir, 0);
 
@@ -379,7 +370,7 @@ int init_db(const char *template_dir, unsigned int flags)
         */
        check_repository_format();
 
-       reinit = create_default_files(template_dir);
+       reinit = create_default_files(template_dir, original_git_dir);
 
        create_object_directory();
 
@@ -419,6 +410,7 @@ int init_db(const char *template_dir, unsigned int flags)
                               git_dir, len && git_dir[len-1] != '/' ? "/" : "");
        }
 
+       free(original_git_dir);
        return 0;
 }
 
@@ -586,7 +578,6 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
                        set_git_work_tree(work_tree);
        }
 
-       set_git_dir_init(git_dir, real_git_dir, 1);
-
-       return init_db(template_dir, flags);
+       flags |= INIT_DB_EXIST_OK;
+       return init_db(git_dir, real_git_dir, template_dir, flags);
 }
diff --git a/cache.h b/cache.h
index ed3d5dfce1b9eb2d6273edac45901632304d4be2..6fc0e5ae68a6965c8be3b822400145095b56f755 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -526,9 +526,10 @@ extern void verify_non_filename(const char *prefix, const char *name);
 extern int path_inside_repo(const char *prefix, const char *path);
 
 #define INIT_DB_QUIET 0x0001
+#define INIT_DB_EXIST_OK 0x0002
 
-extern int set_git_dir_init(const char *git_dir, const char *real_git_dir, int);
-extern int init_db(const char *template_dir, unsigned int flags);
+extern int init_db(const char *git_dir, const char *real_git_dir,
+                  const char *template_dir, unsigned int flags);
 
 extern void sanitize_stdfds(void);
 extern int daemonize(void);
index 8ffbbea4d65dbd8328563da53ef2959dd39ce796..b8fc588b1922760ade8502fda3dc465f6ebf2887 100755 (executable)
@@ -393,4 +393,21 @@ test_expect_success 'remote init from does not use config from cwd' '
        test_cmp expect actual
 '
 
+test_expect_success 're-init from a linked worktree' '
+       git init main-worktree &&
+       (
+               cd main-worktree &&
+               test_commit first &&
+               git worktree add ../linked-worktree &&
+               mv .git/info/exclude expected-exclude &&
+               cp .git/config expected-config &&
+               find .git/worktrees -print | sort >expected &&
+               git -C ../linked-worktree init &&
+               test_cmp expected-exclude .git/info/exclude &&
+               test_cmp expected-config .git/config &&
+               find .git/worktrees -print | sort >actual &&
+               test_cmp expected actual
+       )
+'
+
 test_done