init: call set_git_dir_init() from within init_db()
[gitweb.git] / builtin / init-db.c
index 0b0557138db40fdff9172557c6e470ddcc69c1ba..5cb05d9bd9b9ebe2b64ede739a416f1c599e01e6 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:
@@ -195,8 +195,14 @@ static int create_default_files(const char *template_path)
         * 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);
 
        /*
@@ -305,8 +311,9 @@ 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)
+static int set_git_dir_init(const char *git_dir,
+                           const char *real_git_dir,
+                           int exist_ok)
 {
        if (real_git_dir) {
                struct stat st;
@@ -353,10 +360,14 @@ 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();
+
+       set_git_dir_init(git_dir, real_git_dir, flags & INIT_DB_EXIST_OK);
+
+       git_dir = get_git_dir();
 
        if (git_link)
                separate_git_dir(git_dir);
@@ -576,7 +587,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);
 }