]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin/init-db.c
Merge branch 'ea/blame-use-oideq'
[thirdparty/git.git] / builtin / init-db.c
index f70076d38eb3d439b521554b386e3132e33c6ceb..cd3e7605417dad82417294644b9f6de376792379 100644 (file)
@@ -9,6 +9,7 @@
 #include "builtin.h"
 #include "exec-cmd.h"
 #include "parse-options.h"
+#include "worktree.h"
 
 #ifndef DEFAULT_GIT_TEMPLATE_DIR
 #define DEFAULT_GIT_TEMPLATE_DIR "/usr/share/git-core/templates"
@@ -364,6 +365,7 @@ static void separate_git_dir(const char *git_dir, const char *git_link)
 
                if (rename(src, git_dir))
                        die_errno(_("unable to move %s to %s"), src, git_dir);
+               repair_worktrees(NULL, NULL);
        }
 
        write_file(git_link, "gitdir: %s", git_dir);
@@ -563,6 +565,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
 
        argc = parse_options(argc, argv, prefix, init_db_options, init_db_usage, 0);
 
+       if (real_git_dir && is_bare_repository_cfg == 1)
+               die(_("--separate-git-dir and --bare are mutually exclusive"));
+
        if (real_git_dir && !is_absolute_path(real_git_dir))
                real_git_dir = real_pathdup(real_git_dir, 1);
 
@@ -637,6 +642,30 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
        if (!git_dir)
                git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
 
+       /*
+        * When --separate-git-dir is used inside a linked worktree, take
+        * care to ensure that the common .git/ directory is relocated, not
+        * the worktree-specific .git/worktrees/<id>/ directory.
+        */
+       if (real_git_dir) {
+               int err;
+               const char *p;
+               struct strbuf sb = STRBUF_INIT;
+
+               p = read_gitfile_gently(git_dir, &err);
+               if (p && get_common_dir(&sb, p)) {
+                       struct strbuf mainwt = STRBUF_INIT;
+
+                       strbuf_addbuf(&mainwt, &sb);
+                       strbuf_strip_suffix(&mainwt, "/.git");
+                       if (chdir(mainwt.buf) < 0)
+                               die_errno(_("cannot chdir to %s"), mainwt.buf);
+                       strbuf_release(&mainwt);
+                       git_dir = strbuf_detach(&sb, NULL);
+               }
+               strbuf_release(&sb);
+       }
+
        if (is_bare_repository_cfg < 0)
                is_bare_repository_cfg = guess_repository_type(git_dir);
 
@@ -658,6 +687,8 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
                                   get_git_work_tree());
        }
        else {
+               if (real_git_dir)
+                       die(_("--separate-git-dir incompatible with bare repository"));
                if (work_tree)
                        set_git_work_tree(work_tree);
        }