init: kill set_git_dir_init()
[gitweb.git] / builtin / init-db.c
index b2d8d40a6750e5794dd9bfbfbcb31924a7eec4ba..68c1ae330cda61ac188973aa926105cb1b9ee8cb 100644 (file)
@@ -138,7 +138,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:
@@ -191,16 +191,25 @@ static int create_default_files(const char *template_path)
        /* Just look for `init.templatedir` */
        git_config(git_init_db_config, NULL);
 
-       /* First copy the templates -- we might have the default
+       /*
+        * First copy the templates -- we might have the default
         * config file there, in which case we would want to read
         * from it after installing.
+        *
+        * Before reading that config, we also need to clear out any cached
+        * values (since we've just potentially changed what's available on
+        * disk).
         */
        copy_templates(template_path);
-
+       git_config_clear();
+       reset_shared_repository();
        git_config(git_default_config, NULL);
-       is_bare_repository_cfg = init_is_bare_repository;
 
-       /* reading existing config may have overwrote it */
+       /*
+        * We must make sure command-line options continue to override any
+        * values we might have just re-read from the config.
+        */
+       is_bare_repository_cfg = init_is_bare_repository;
        if (init_shared_repository != -1)
                set_shared_repository(init_shared_repository);
 
@@ -302,33 +311,6 @@ 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)
 {
        struct stat st;
@@ -350,10 +332,34 @@ 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;
+
+       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;
+       git_dir = get_git_dir();
 
        if (git_link)
                separate_git_dir(git_dir);
@@ -573,7 +579,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);
 }