Merge branch 'jc/shared-literally'
[gitweb.git] / builtin-init-db.c
index c68a3b1e7409cb15879724d9acef1cecc97ab9fe..4e02b33bb77b8957940562f87bc0b6faaeb3a101 100644 (file)
@@ -17,6 +17,9 @@
 #define TEST_FILEMODE 1
 #endif
 
+static int init_is_bare_repository = 0;
+static int init_shared_repository = -1;
+
 static void safe_create_dir(const char *dir, int share)
 {
        if (mkdir(dir, 0777) < 0) {
@@ -26,7 +29,7 @@ static void safe_create_dir(const char *dir, int share)
                }
        }
        else if (share && adjust_shared_perm(dir))
-               die("Could not make %s writable by group\n", dir);
+               die("Could not make %s writable by group", dir);
 }
 
 static void copy_templates_1(char *path, int baselen,
@@ -37,7 +40,7 @@ static void copy_templates_1(char *path, int baselen,
 
        /* Note: if ".git/hooks" file exists in the repository being
         * re-initialized, /etc/core-git/templates/hooks/update would
-        * cause git-init to fail here.  I think this is sane but
+        * cause "git init" to fail here.  I think this is sane but
         * it means that the set of templates we ship by default, along
         * with the way the namespace under .git/ is organized, should
         * be really carefully chosen.
@@ -115,18 +118,8 @@ static void copy_templates(const char *template_dir)
 
        if (!template_dir)
                template_dir = getenv(TEMPLATE_DIR_ENVIRONMENT);
-       if (!template_dir) {
-               /*
-                * if the hard-coded template is relative, it is
-                * interpreted relative to the exec_dir
-                */
-               template_dir = DEFAULT_GIT_TEMPLATE_DIR;
-               if (!is_absolute_path(template_dir)) {
-                       struct strbuf d = STRBUF_INIT;
-                       strbuf_addf(&d, "%s/%s", git_exec_path(), template_dir);
-                       template_dir = strbuf_detach(&d, NULL);
-               }
-       }
+       if (!template_dir)
+               template_dir = system_path(DEFAULT_GIT_TEMPLATE_DIR);
        if (!template_dir[0])
                return;
        strcpy(template_path, template_dir);
@@ -137,8 +130,7 @@ static void copy_templates(const char *template_dir)
        }
        dir = opendir(template_path);
        if (!dir) {
-               fprintf(stderr, "warning: templates not found %s\n",
-                       template_dir);
+               warning("templates not found %s", template_dir);
                return;
        }
 
@@ -151,8 +143,8 @@ static void copy_templates(const char *template_dir)
 
        if (repository_format_version &&
            repository_format_version != GIT_REPO_VERSION) {
-               fprintf(stderr, "warning: not copying templates of "
-                       "a wrong format version %d from '%s'\n",
+               warning("not copying templates of "
+                       "a wrong format version %d from '%s'",
                        repository_format_version,
                        template_dir);
                closedir(dir);
@@ -201,6 +193,11 @@ static int create_default_files(const char *template_path)
        copy_templates(template_path);
 
        git_config(git_default_config, NULL);
+       is_bare_repository_cfg = init_is_bare_repository;
+
+       /* reading existing config may have overwrote it */
+       if (init_shared_repository != -1)
+               shared_repository = init_shared_repository;
 
        /*
         * We would have created the above under user's umask -- under
@@ -287,6 +284,8 @@ int init_db(const char *template_dir, unsigned int flags)
 
        safe_create_dir(get_git_dir(), 0);
 
+       init_is_bare_repository = is_bare_repository();
+
        /* Check to see if the repository version is right.
         * Note that a newly created repository does not have
         * config file, so this will not fail.  What we are catching
@@ -315,12 +314,15 @@ int init_db(const char *template_dir, unsigned int flags)
                 * and compatibility values for PERM_GROUP and
                 * PERM_EVERYBODY.
                 */
-               if (shared_repository == PERM_GROUP)
+               if (shared_repository < 0)
+                       /* force to the mode value */
+                       sprintf(buf, "0%o", -shared_repository);
+               else if (shared_repository == PERM_GROUP)
                        sprintf(buf, "%d", OLD_PERM_GROUP);
                else if (shared_repository == PERM_EVERYBODY)
                        sprintf(buf, "%d", OLD_PERM_EVERYBODY);
                else
-                       sprintf(buf, "0%o", shared_repository);
+                       die("oops");
                git_config_set("core.sharedrepository", buf);
                git_config_set("receive.denyNonFastforwards", "true");
        }
@@ -366,7 +368,7 @@ static int guess_repository_type(const char *git_dir)
 }
 
 static const char init_db_usage[] =
-"git-init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
+"git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]]";
 
 /*
  * If you want to, you can share the DB area with any number of branches.
@@ -391,15 +393,18 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
                        setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
                                                sizeof(git_dir)), 0);
                } else if (!strcmp(arg, "--shared"))
-                       shared_repository = PERM_GROUP;
+                       init_shared_repository = PERM_GROUP;
                else if (!prefixcmp(arg, "--shared="))
-                       shared_repository = git_config_perm("arg", arg+9);
+                       init_shared_repository = git_config_perm("arg", arg+9);
                else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
                        flags |= INIT_DB_QUIET;
                else
                        usage(init_db_usage);
        }
 
+       if (init_shared_repository != -1)
+               shared_repository = init_shared_repository;
+
        /*
         * GIT_WORK_TREE makes sense only in conjunction with GIT_DIR
         * without --bare.  Catch the error early.