]> 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 bbc9bc78f9d5a32ed9d167a79426ba0329bdea95..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);
@@ -640,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);