git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 4/5] init: teach --separate-git-dir to repair linked worktrees

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Aug 31, 2020, 06:57 UTC
Message-ID
<20200831065800.62502-5-sunshine@sunshineco.com>
In-Reply-To
<20200831065800.62502-1-sunshine@sunshineco.com>

A linked worktree's .git file is a "gitfile" pointing at the .git/worktrees/<id> directory within the repository. When `git init --separate-git-dir=<path>` is used on an existing repository to relocate the repository's .git/ directory to a different location, it neglects to update the .git files of linked worktrees, thus breaking the worktrees by making it impossible for them to locate the repository. Fix this by teaching --separate-git-dir to repair the .git file of each linked worktree to point at the new repository location.

Reported-by: Henré Botha <henrebotha@gmail.com>
Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
---
 builtin/init-db.c |  2 ++
 t/t0001-init.sh   | 11 +++++++++++
 2 files changed, 13 insertions(+)
diff --git a/builtin/init-db.c b/builtin/init-db.c
index bbc9bc78f9..7b915d88ab 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -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);
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 50222a10c5..e489eb4ddb 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -405,6 +405,17 @@ test_expect_success SYMLINKS 're-init to move gitdir symlink' '
 	test_path_is_dir realgitdir/refs
 '
 
+test_expect_success 're-init to move gitdir with linked worktrees' '
+	test_when_finished "rm -rf mainwt linkwt seprepo" &&
+	git init mainwt &&
+	test_commit -C mainwt gumby &&
+	git -C mainwt worktree add --detach ../linkwt &&
+	git -C mainwt init --separate-git-dir ../seprepo &&
+	git -C mainwt rev-parse --git-common-dir >expect &&
+	git -C linkwt rev-parse --git-common-dir >actual &&
+	test_cmp expect actual
+'
+
 test_expect_success MINGW '.git hidden' '
 	rm -rf newdir &&
 	(
-- 
2.28.0.531.g41c3d8a546
Previous: Eric SunshineNext: Junio C Hamano
Message 21 of 22 in “add "git worktree repair" command”
  1. 0/5 add "git worktree repair" commandEric Sunshine, Aug 27, 2020
  2. 1/5 worktree: add skeleton "repair" commandEric Sunshine, Aug 27, 2020
  3. Junio C HamanoAug 27, 2020
  4. Eric SunshineAug 27, 2020
  5. 5/5 init: make --separate-git-dir work from within linked worktreeEric Sunshine, Aug 27, 2020
  6. 4/5 init: teach --separate-git-dir to repair linked worktreesEric Sunshine, Aug 27, 2020
  7. 2/5 worktree: teach "repair" to fix worktree back-links to main worktreeEric Sunshine, Aug 27, 2020
  8. Junio C HamanoAug 27, 2020
  9. Eric SunshineAug 30, 2020
  10. 3/5 worktree: teach "repair" to fix outgoing links to worktreesEric Sunshine, Aug 27, 2020
  11. Junio C HamanoAug 27, 2020
  12. Eric SunshineAug 30, 2020
  13. Junio C HamanoAug 31, 2020
  14. Johannes SchindelinAug 28, 2020
  15. Eric SunshineAug 28, 2020
  16. 0/5 add "git worktree repair" commandEric Sunshine, Aug 31, 2020
  17. 1/5 worktree: add skeleton "repair" commandEric Sunshine, Aug 31, 2020
  18. 3/5 worktree: teach "repair" to fix outgoing links to worktreesEric Sunshine, Aug 31, 2020
  19. 5/5 init: make --separate-git-dir work from within linked worktreeEric Sunshine, Aug 31, 2020
  20. 2/5 worktree: teach "repair" to fix worktree back-links to main worktreeEric Sunshine, Aug 31, 2020
  21. 4/5 init: teach --separate-git-dir to repair linked worktreesEric Sunshine, Aug 31, 2020
  22. Junio C HamanoAug 31, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.