add_packed_git: convert strcpy into xsnprintf
[gitweb.git] / builtin / init-db.c
index 9966522b4a97a0d1ae430e581125fbfd1d5609ac..e7d0e31f46e9890a267fa508fa43d184d563afed 100644 (file)
@@ -4,6 +4,7 @@
  * Copyright (C) Linus Torvalds, 2005
  */
 #include "cache.h"
+#include "refs.h"
 #include "builtin.h"
 #include "exec_cmd.h"
 #include "parse-options.h"
@@ -182,6 +183,20 @@ static int git_init_db_config(const char *k, const char *v, void *cb)
        return 0;
 }
 
+/*
+ * If the git_dir is not directly inside the working tree, then git will not
+ * find it by default, and we need to set the worktree explicitly.
+ */
+static int needs_work_tree_config(const char *git_dir, const char *work_tree)
+{
+       if (!strcmp(work_tree, "/") && !strcmp(git_dir, "/.git"))
+               return 0;
+       if (skip_prefix(git_dir, work_tree, &git_dir) &&
+           !strcmp(git_dir, "/.git"))
+               return 0;
+       return 1;
+}
+
 static int create_default_files(const char *template_path)
 {
        const char *git_dir = get_git_dir();
@@ -247,7 +262,8 @@ static int create_default_files(const char *template_path)
        }
 
        /* This forces creation of new config file */
-       sprintf(repo_version_string, "%d", GIT_REPO_VERSION);
+       xsnprintf(repo_version_string, sizeof(repo_version_string),
+                 "%d", GIT_REPO_VERSION);
        git_config_set("core.repositoryformatversion", repo_version_string);
 
        path[len] = 0;
@@ -274,10 +290,8 @@ 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 (!starts_with(git_dir, work_tree) ||
-                   strcmp(git_dir + strlen(work_tree), "/.git")) {
+               if (needs_work_tree_config(git_dir, work_tree))
                        git_config_set("core.worktree", work_tree);
-               }
        }
 
        if (!reinit) {
@@ -350,7 +364,6 @@ int set_git_dir_init(const char *git_dir, const char *real_git_dir,
 static void separate_git_dir(const char *git_dir)
 {
        struct stat st;
-       FILE *fp;
 
        if (!stat(git_link, &st)) {
                const char *src;
@@ -366,11 +379,7 @@ static void separate_git_dir(const char *git_dir)
                        die_errno(_("unable to move %s to %s"), src, git_dir);
        }
 
-       fp = fopen(git_link, "w");
-       if (!fp)
-               die(_("Could not create git link %s"), git_link);
-       fprintf(fp, "gitdir: %s\n", git_dir);
-       fclose(fp);
+       write_file(git_link, "gitdir: %s", git_dir);
 }
 
 int init_db(const char *template_dir, unsigned int flags)
@@ -406,13 +415,13 @@ int init_db(const char *template_dir, unsigned int flags)
                 */
                if (shared_repository < 0)
                        /* force to the mode value */
-                       sprintf(buf, "0%o", -shared_repository);
+                       xsnprintf(buf, sizeof(buf), "0%o", -shared_repository);
                else if (shared_repository == PERM_GROUP)
-                       sprintf(buf, "%d", OLD_PERM_GROUP);
+                       xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP);
                else if (shared_repository == PERM_EVERYBODY)
-                       sprintf(buf, "%d", OLD_PERM_EVERYBODY);
+                       xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY);
                else
-                       die("oops");
+                       die("BUG: invalid value for shared_repository");
                git_config_set("core.sharedrepository", buf);
                git_config_set("receive.denyNonFastforwards", "true");
        }
@@ -472,7 +481,7 @@ static int shared_callback(const struct option *opt, const char *arg, int unset)
 }
 
 static const char *const init_db_usage[] = {
-       N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [directory]"),
+       N_("git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]"),
        NULL
 };