Merge branch 'nd/commit-p-doc'
[gitweb.git] / builtin / init-db.c
index 5cb05d9bd9b9ebe2b64ede739a416f1c599e01e6..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)
@@ -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;
@@ -180,13 +180,7 @@ static int create_default_files(const char *template_path)
        char junk[2];
        int reinit;
        int filemode;
-
-       /*
-        * Create .git/refs/{heads,tags}
-        */
-       safe_create_dir(git_path_buf(&buf, "refs"), 1);
-       safe_create_dir(git_path_buf(&buf, "refs/heads"), 1);
-       safe_create_dir(git_path_buf(&buf, "refs/tags"), 1);
+       struct strbuf err = STRBUF_INIT;
 
        /* Just look for `init.templatedir` */
        git_config(git_init_db_config, NULL);
@@ -219,11 +213,18 @@ static int create_default_files(const char *template_path)
         */
        if (get_shared_repository()) {
                adjust_shared_perm(get_git_dir());
-               adjust_shared_perm(git_path_buf(&buf, "refs"));
-               adjust_shared_perm(git_path_buf(&buf, "refs/heads"));
-               adjust_shared_perm(git_path_buf(&buf, "refs/tags"));
        }
 
+       /*
+        * We need to create a "refs" dir in any case so that older
+        * versions of git can tell that this is a repository.
+        */
+       safe_create_dir(git_path("refs"), 1);
+       adjust_shared_perm(git_path("refs"));
+
+       if (refs_init_db(&err))
+               die("failed to set up refs db: %s", err.buf);
+
        /*
         * Create the default symlink from ".git/HEAD" to the "master"
         * branch, if it does not exist yet.
@@ -263,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);
        }
 
@@ -311,35 +312,7 @@ static void create_object_directory(void)
        strbuf_release(&path);
 }
 
-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;
-
-               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;
 
@@ -364,13 +337,27 @@ int init_db(const char *git_dir, const char *real_git_dir,
            const char *template_dir, unsigned int flags)
 {
        int reinit;
+       int exist_ok = flags & INIT_DB_EXIST_OK;
+       char *original_git_dir = xstrdup(real_path(git_dir));
 
-       set_git_dir_init(git_dir, real_git_dir, flags & INIT_DB_EXIST_OK);
+       if (real_git_dir) {
+               struct stat st;
 
-       git_dir = get_git_dir();
+               if (!exist_ok && !stat(git_dir, &st))
+                       die(_("%s already exists"), git_dir);
 
-       if (git_link)
-               separate_git_dir(git_dir);
+               if (!exist_ok && !stat(real_git_dir, &st))
+                       die(_("%s already exists"), real_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);
 
@@ -383,7 +370,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
         */
        check_repository_format();
 
-       reinit = create_default_files(template_dir);
+       reinit = create_default_files(template_dir, original_git_dir);
 
        create_object_directory();
 
@@ -411,15 +398,19 @@ int init_db(const char *git_dir, const char *real_git_dir,
        if (!(flags & INIT_DB_QUIET)) {
                int len = strlen(git_dir);
 
-               /* TRANSLATORS: The first '%s' is either "Reinitialized
-                  existing" or "Initialized empty", the second " shared" or
-                  "", and the last '%s%s' is the verbatim directory name. */
-               printf(_("%s%s Git repository in %s%s\n"),
-                      reinit ? _("Reinitialized existing") : _("Initialized empty"),
-                      get_shared_repository() ? _(" shared") : "",
-                      git_dir, len && git_dir[len-1] != '/' ? "/" : "");
+               if (reinit)
+                       printf(get_shared_repository()
+                              ? _("Reinitialized existing shared Git repository in %s%s\n")
+                              : _("Reinitialized existing Git repository in %s%s\n"),
+                              git_dir, len && git_dir[len-1] != '/' ? "/" : "");
+               else
+                       printf(get_shared_repository()
+                              ? _("Initialized empty shared Git repository in %s%s\n")
+                              : _("Initialized empty Git repository in %s%s\n"),
+                              git_dir, len && git_dir[len-1] != '/' ? "/" : "");
        }
 
+       free(original_git_dir);
        return 0;
 }