Merge branch 'jk/fail-null-clone' into maint
[gitweb.git] / builtin / init-db.c
index 9d4886c71675f2bc50671ea284875ebae9a34735..fbeb380ee2af2b21478e5766a3dd956b8fe06104 100644 (file)
@@ -414,11 +414,12 @@ static const char *const init_db_usage[] = {
 int cmd_init_db(int argc, const char **argv, const char *prefix)
 {
        const char *git_dir;
+       const char *work_tree;
        const char *template_dir = NULL;
        unsigned int flags = 0;
        const struct option init_db_options[] = {
                OPT_STRING(0, "template", &template_dir, "template-directory",
-                               "provide the directory from which templates will be used"),
+                               "directory from which templates will be used"),
                OPT_SET_INT(0, "bare", &is_bare_repository_cfg,
                                "create a bare repository", 1),
                { OPTION_CALLBACK, 0, "shared", &init_shared_repository,
@@ -480,8 +481,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
         * without --bare.  Catch the error early.
         */
        git_dir = getenv(GIT_DIR_ENVIRONMENT);
-       if ((!git_dir || is_bare_repository_cfg == 1)
-           && getenv(GIT_WORK_TREE_ENVIRONMENT))
+       work_tree = getenv(GIT_WORK_TREE_ENVIRONMENT);
+       if ((!git_dir || is_bare_repository_cfg == 1) && work_tree)
                die("%s (or --work-tree=<directory>) not allowed without "
                    "specifying %s (or --git-dir=<directory>)",
                    GIT_WORK_TREE_ENVIRONMENT,
@@ -497,23 +498,29 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
                is_bare_repository_cfg = guess_repository_type(git_dir);
 
        if (!is_bare_repository_cfg) {
-               if (git_dir) {
-                       const char *git_dir_parent = strrchr(git_dir, '/');
-                       if (git_dir_parent) {
-                               char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
-                               git_work_tree_cfg = xstrdup(make_absolute_path(rel));
-                               free(rel);
-                       }
+               const char *git_dir_parent = strrchr(git_dir, '/');
+               if (git_dir_parent) {
+                       char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
+                       git_work_tree_cfg = xstrdup(make_absolute_path(rel));
+                       free(rel);
                }
                if (!git_work_tree_cfg) {
                        git_work_tree_cfg = xcalloc(PATH_MAX, 1);
                        if (!getcwd(git_work_tree_cfg, PATH_MAX))
                                die_errno ("Cannot access current working directory");
                }
+               if (work_tree)
+                       set_git_work_tree(make_absolute_path(work_tree));
+               else
+                       set_git_work_tree(git_work_tree_cfg);
                if (access(get_git_work_tree(), X_OK))
                        die_errno ("Cannot access work tree '%s'",
                                   get_git_work_tree());
        }
+       else {
+               if (work_tree)
+                       set_git_work_tree(make_absolute_path(work_tree));
+       }
 
        set_git_dir(make_absolute_path(git_dir));