Skip to contentSkip to conversation

add "git worktree repair" command

PATCH Superseded Eric Sunshine 3 participants 22 messages Aug 27, 2020 – Aug 31, 2020 Documentation

lore
Share & tools
lore
#0 lore raw

cover add "git worktree repair" command

The purpose of this patch series is twofold.

First, it adds a "git worktree repair" command to help users recover from situations in which worktree administrative files can become outdated or corrupted due to external factors. Simple examples include
(1) the user moving a worktree manually rather than via "git worktree move" which causes the main worktree (or bare repository) to lose track of the worktree, and (2) moving the main worktree (or bare repository) which results in linked worktrees being unable to find the repository.

Second, it fixes two bugs with "git init --separate-git-dir" when linked worktrees are involved (reported by [1]), both of which cause worktree administrative files to become outdated or corrupted.

An intentional side-effect of the --separate-git-dir fix in patch [5/5] is that it closes an additional loophole not covered by [2] which made it illegal to use --separate-git-dir in conjunction with bare repositories. Peff is Cc:'d because he commented on that thread.

[1]: https://lore.kernel.org/git/CAHbriek39i9NSHRw6DZm0dftk-GkeAYR74c0xyss0vbeDHu1Hw@mail.gmail.com/T/
[2]: https://lore.kernel.org/git/[email protected]/T/

Eric Sunshine (5):
worktree: add skeleton "repair" command
worktree: teach "repair" to fix worktree back-links to main worktree
worktree: teach "repair" to fix outgoing links to worktrees
init: teach --separate-git-dir to repair linked worktrees
init: make --separate-git-dir work from within linked worktree

 Documentation/git-worktree.txt |  26 ++++-
 builtin/init-db.c              |  26 +++++
 builtin/worktree.c             |  29 ++++++
 t/t0001-init.sh                |  28 ++++++
 t/t2406-worktree-repair.sh     | 169 +++++++++++++++++++++++++++++++++
 worktree.c                     | 127 +++++++++++++++++++++++++
 worktree.h                     |  22 +++++
 7 files changed, 425 insertions(+), 2 deletions(-)
 create mode 100755 t/t2406-worktree-repair.sh
Signature · 1 line
2.28.0.461.g40977abb40
#1 lore raw

PATCH 1/5 worktree: add skeleton "repair" command

Worktree administrative files can become corrupted or outdated due to external factors. Although, it is often possible to recover from such situations by hand-tweaking these files, doing so requires intimate knowledge of worktree internals. While information necessary to make such repairs manually can be obtained from git-worktree.txt and gitrepository-layout.txt, we can assist users more directly by teaching git-worktree how to repair its administrative files itself (at least to some extent). Therefore, add a "git worktree repair" command which attempts to correct common problems which may arise due to factors beyond Git's control.

At this stage, the "repair" command is a mere skeleton; subsequent commits will flesh out the functionality.

---
 Documentation/git-worktree.txt |  6 ++++++
 builtin/worktree.c             | 15 +++++++++++++++
 t/t2406-worktree-repair.sh     | 11 +++++++++++
 3 files changed, 32 insertions(+)
 create mode 100755 t/t2406-worktree-repair.sh
Documentation/git-worktree.txt +6 -0
index 6ee6ec7982..ae432d39a8 100644
@@ -15,6 +15,7 @@ SYNOPSIS
 'git worktree move' <worktree> <new-path>
 'git worktree prune' [-n] [-v] [--expire <expire>]
 'git worktree remove' [-f] <worktree>
+'git worktree repair'
 'git worktree unlock' <worktree>
 
 DESCRIPTION
@@ -110,6 +111,11 @@ and no modification in tracked files) can be removed. Unclean working
 trees or ones with submodules can be removed with `--force`. The main
 working tree cannot be removed.
 
+repair::
+
+Repair working tree administrative files, if possible, if they have
+become corrupted or outdated due to external factors.
+
 unlock::
 
 Unlock a working tree, allowing it to be pruned, moved or deleted.
builtin/worktree.c +15 -0
index 378f332b5d..88af412d4f 100644
@@ -1030,6 +1030,19 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
 	return ret;
 }
 
+static int repair(int ac, const char **av, const char *prefix)
+{
+	struct option options[] = {
+		OPT_END()
+	};
+	int rc = 0;
+
+	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
+	if (ac)
+		usage_with_options(worktree_usage, options);
+	return rc;
+}
+
 int cmd_worktree(int ac, const char **av, const char *prefix)
 {
 	struct option options[] = {
@@ -1056,5 +1069,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
 		return move_worktree(ac - 1, av + 1, prefix);
 	if (!strcmp(av[1], "remove"))
 		return remove_worktree(ac - 1, av + 1, prefix);
+	if (!strcmp(av[1], "repair"))
+		return repair(ac - 1, av + 1, prefix);
 	usage_with_options(worktree_usage, options);
 }
t/t2406-worktree-repair.sh new file +11 -0
new file mode 100755
index 0000000000..cc679e1a21
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+test_description='test git worktree repair'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	test_commit init
+'
+
+test_done
Signature · 1 line
2.28.0.461.g40977abb40
#2 lore raw

Re: [PATCH 1/5] worktree: add skeleton "repair" command

Quoted 11 lines · Eric Sunshine

Worktree administrative files can become corrupted or outdated due to external factors. Although, it is often possible to recover from such situations by hand-tweaking these files, doing so requires intimate knowledge of worktree internals. While information necessary to make such repairs manually can be obtained from git-worktree.txt and gitrepository-layout.txt, we can assist users more directly by teaching git-worktree how to repair its administrative files itself (at least to some extent). Therefore, add a "git worktree repair" command which attempts to correct common problems which may arise due to factors beyond Git's control.

Quoted 2 lines

At this stage, the "repair" command is a mere skeleton; subsequent commits will flesh out the functionality.

Sounds good. It looked a bit odd to have skeleton test script only to reserve its test number, but it is just odd and not wrong.

Let's read on.

Quoted 81 lines
---
 Documentation/git-worktree.txt |  6 ++++++
 builtin/worktree.c             | 15 +++++++++++++++
 t/t2406-worktree-repair.sh     | 11 +++++++++++
 3 files changed, 32 insertions(+)
 create mode 100755 t/t2406-worktree-repair.sh
Documentation/git-worktree.txt +6 -0
index 6ee6ec7982..ae432d39a8 100644
@@ -15,6 +15,7 @@ SYNOPSIS
 'git worktree move' <worktree> <new-path>
 'git worktree prune' [-n] [-v] [--expire <expire>]
 'git worktree remove' [-f] <worktree>
+'git worktree repair'
 'git worktree unlock' <worktree>
 
 DESCRIPTION
@@ -110,6 +111,11 @@ and no modification in tracked files) can be removed. Unclean working
 trees or ones with submodules can be removed with `--force`. The main
 working tree cannot be removed.
 
+repair::
+
+Repair working tree administrative files, if possible, if they have
+become corrupted or outdated due to external factors.
+
 unlock::
 
 Unlock a working tree, allowing it to be pruned, moved or deleted.
builtin/worktree.c +15 -0
index 378f332b5d..88af412d4f 100644
@@ -1030,6 +1030,19 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
 	return ret;
 }
 
+static int repair(int ac, const char **av, const char *prefix)
+{
+	struct option options[] = {
+		OPT_END()
+	};
+	int rc = 0;
+
+	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
+	if (ac)
+		usage_with_options(worktree_usage, options);
+	return rc;
+}
+
 int cmd_worktree(int ac, const char **av, const char *prefix)
 {
 	struct option options[] = {
@@ -1056,5 +1069,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
 		return move_worktree(ac - 1, av + 1, prefix);
 	if (!strcmp(av[1], "remove"))
 		return remove_worktree(ac - 1, av + 1, prefix);
+	if (!strcmp(av[1], "repair"))
+		return repair(ac - 1, av + 1, prefix);
 	usage_with_options(worktree_usage, options);
 }
t/t2406-worktree-repair.sh new file +11 -0
new file mode 100755
index 0000000000..cc679e1a21
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+test_description='test git worktree repair'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	test_commit init
+'
+
+test_done
#3 lore raw

Re: [PATCH 1/5] worktree: add skeleton "repair" command

Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Eric Sunshine

At this stage, the "repair" command is a mere skeleton; subsequent commits will flesh out the functionality.

Sounds good. It looked a bit odd to have skeleton test script only to reserve its test number, but it is just odd and not wrong.

The intent wasn't to reserve a test number. Rather, I was worried about reviewer fatigue with patches [2/5] and [3/5] which are lengthy, so I moved whatever boilerplate I could to [1/5] to make the later patches a tiny bit shorter.

#4 lore raw

PATCH 2/5 worktree: teach "repair" to fix worktree back-links to main worktree

In reply to Eric Sunshine

The .git file in a linked worktree is a "gitlink" which points back to the .git/worktrees/<id> entry in the main worktree or bare repository. If a worktree's .git file is deleted or becomes corrupted or outdated, then the linked worktree won't know how to find the repository or any of its own administrative files (such as 'index', 'HEAD', etc.). An easy way for the .git file to become outdated is for the user to move the main worktree or bare repository. Although it is possible to manually update each linked worktree's .git file to reflect the new repository location, doing so requires a level of knowledge about worktree internals beyond what a user should be expected to know offhand.

Therefore, teach "git worktree repair" how to repair broken or outdated worktree .git files automatically. (For this to work, the command must be invoked from within the main worktree or bare repository, or from within a worktree which has not become disconnected from the repository -- such as one which was created after the repository was moved.)

---
 Documentation/git-worktree.txt | 10 ++++-
 builtin/worktree.c             | 11 ++++++
 t/t2406-worktree-repair.sh     | 72 ++++++++++++++++++++++++++++++++++
 worktree.c                     | 53 +++++++++++++++++++++++++
 worktree.h                     | 11 ++++++
 5 files changed, 156 insertions(+), 1 deletion(-)
Documentation/git-worktree.txt +9 -1
index ae432d39a8..acb0ea1c2e 100644
@@ -98,7 +98,10 @@ with `--reason`.
 move::
 
 Move a working tree to a new location. Note that the main working tree
-or linked working trees containing submodules cannot be moved.
+or linked working trees containing submodules cannot be moved with this
+command. (The `git worktree repair` command, however, can reestablish
+the connection with linked working trees if you move the main working
+tree manually.)
 
 prune::
 
@@ -115,6 +118,11 @@ repair::
 
 Repair working tree administrative files, if possible, if they have
 become corrupted or outdated due to external factors.
++
+For instance, if the main working tree (or bare repository) is moved,
+linked working trees will be unable to locate it. Running `repair` in
+the recently-moved main working tree will reestablish the connection
+from linked working trees back to the main working tree.
 
 unlock::
 
builtin/worktree.c +11 -0
index 88af412d4f..62e33eb7f5 100644
@@ -1030,6 +1030,16 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
 	return ret;
 }
 
+static void repair_cb(int iserr, const char *path, const char *msg, void *cb_data)
+{
+	if (!iserr)
+		printf_ln(_("repair: %s: %s"), msg, path);
+	else {
+		fprintf_ln(stderr, _("error: %s: %s"), msg, path);
+		*(int *)cb_data = 1;
+	}
+}
+
 static int repair(int ac, const char **av, const char *prefix)
 {
 	struct option options[] = {
@@ -1040,6 +1050,7 @@ static int repair(int ac, const char **av, const char *prefix)
 	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
 	if (ac)
 		usage_with_options(worktree_usage, options);
+	repair_worktrees(repair_cb, &rc);
 	return rc;
 }
 
t/t2406-worktree-repair.sh +72 -0

76 lines not on this page · raw message

worktree.c +53 -0

56 lines not on this page · raw message

worktree.h +11 -0
index 516744c433..7d085c7b91 100644
@@ -89,6 +89,17 @@ int validate_worktree(const struct worktree *wt,
 void update_worktree_location(struct worktree *wt,
 			      const char *path_);
 
+typedef void worktree_repair_cb(int iserr, const char *path, const char *msg,
+				void *cb_data);
+
+/*
+ * Visit each registered linked worktree and repair corruptions. For each
+ * repair made or error encountered while attempting a repair, the callback, if
+ * non-NULL, is called with the path of the worktree and a description of the
+ * repair or error, along with the callback user-data.
+ */
+void repair_worktrees(worktree_repair_cb *, void *cb_data);
+
 /*
  * Free up the memory for worktree(s)
  */
Signature · 1 line
2.28.0.461.g40977abb40
#5 lore raw

Re: [PATCH 2/5] worktree: teach "repair" to fix worktree back-links to main worktree

Quoted 1 line · Eric Sunshine

The .git file in a linked worktree is a "gitlink" which points back to

Please never call it a "gitlink" (which is a word reserved for a cache entry with 160000 mode that typically point at a commit object, an implementation detail of the mechanism to bind a submodule to its superproject) to avoid confusing future readers of our code and documentation. The setup.c code calls it as "gitfile", since it was introduced at b44ebb19 (Add platform-independent .git "symlink", 2008-02-20), I think, and that is how it has been described in the glossary.

Quoted 6 lines

the .git/worktrees/<id> entry in the main worktree or bare repository. If a worktree's .git file is deleted or becomes corrupted or outdated, then the linked worktree won't know how to find the repository or any of its own administrative files (such as 'index', 'HEAD', etc.). An easy way for the .git file to become outdated is for the user to move the main worktree or bare repository.

I am not sure if a directory that used to be a secondary worktree would still appear to be a Git controlled worktree after its ".git" removed, but I guess a ".git" pointing at a wrong place won't, either. I am just curious how much more involved in dealing with the "is deleted" situation than "becomes corrupted" situation.

An obvious way is to ask the users to say "Here in directory D should be a gitfile that points at the primary copy X---D/.git may be missing or corrupt so please go create or fix it" without letting any "auto repository discovery" logic to kick in. For that, I suspect you may have to disable RUN_SETUP in git.c for "worktree" built-in, and run the setup sequence manually for all the existing subcommands except for this new "repair" subcommand. We'll see.

Quoted 10 lines

Although it is possible to manually
update each linked worktree's .git file to reflect the new repository location, doing so requires a level of knowledge about worktree internals beyond what a user should be expected to know offhand.

Therefore, teach "git worktree repair" how to repair broken or outdated worktree .git files automatically. (For this to work, the command must be invoked from within the main worktree or bare repository, or from within a worktree which has not become disconnected from the repository -- such as one which was created after the repository was moved.)

Hmph, ok, that is not quite satisfactory, but it would work. So instead of

user goes to a directory D, thinking it is a valid secondary worktree and wanting to work in it, and finds that Git does not think it is.

user tells "worktree repair" that X is the location of the primary copy, and cwd is supposed to be the top of the working tree of a secondary worktree of it.

"worktree repair" creates/updates ./.git gitfile to point at X.

user starts working.

sequence, the user does

user goes to a directory D, thinking it is a valid secondary worktree and wanting to work in it, and finds that Git does not think it is.

user goes to X, which is the location of the primary copy, and tells "worktree repair" that the directory D ought to be the top of the working tree for a secondary worktree.

"worktree repair" creates/updates D/.git gitfile to point at X.

user comes back to D and starts working.

because "git worktree repair" wants to do the usual setup sequence anyway.

OK.

Documentation/git-worktree.txt with `--reason`. · line 104

Quoted 6 lines

Move a working tree to a new location. Note that the main working tree

Documentation/git-worktree.txt +4 -1
-or linked working trees containing submodules cannot be moved.
+or linked working trees containing submodules cannot be moved with this
+command. (The `git worktree repair` command, however, can reestablish
+the connection with linked working trees if you move the main working
+tree manually.)

... meaning after moving the main working tree, repair can be used to touch-up the submodule directories?

Ah, no. You are saying "git worktree move" still cannot be used to move the main working tree (the ones with submodules in it we do not even care), but as a workaround, the user can "mv" it manually and run "repair" to fix all the secondary worktrees.

Hopefully somewhere in the rest of this series it would become automatic? After all, instead of letting users "mv" things without us knowing what is going on, if we let them say "worktree move" the primary working tree, we know both the source and the destination directories of such a move, and because we know all the secondary worktrees, we can run "worktree repair" on them as part of this "worktree move" of the primary working tree.

It is perfectly fine that it is not happening here in this step to keep each step digestible, of course.

Let's read on.

Documentation/git-worktree.txt repair:: · line 125

Quoted 9 lines
Documentation/git-worktree.txt +5 -0
@@ -115,6 +118,11 @@ repair::
 
 Repair working tree administrative files, if possible, if they have
 become corrupted or outdated due to external factors.
++
+For instance, if the main working tree (or bare repository) is moved,
+linked working trees will be unable to locate it. Running `repair` in
+the recently-moved main working tree will reestablish the connection
+from linked working trees back to the main working tree.

Does "recently-" have a positive value in this paragraph? It makes readers wonder how long the zombie would stay resurrectable, which would encourage to run this command while s/he still remembers that the primary copy got moved, but I am not sure if that is a plus.

builtin/worktree.c static int remove_worktree(int ac, const char **av, const char *prefix) · line 1033

Quoted 9 lines
builtin/worktree.c +1 -0
index 88af412d4f..62e33eb7f5 100644
@@ -1030,6 +1030,16 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
 	return ret;
 }
 
+static void repair_cb(int iserr, const char *path, const char *msg, void *cb_data)

repair_cb as the name for a parameter to repair_worktrees() API function is an excellent name, but as the name for a concrete instance of such a callback function, it is a horrible one. Name it after what it does or what it is for in the context of this particular use of that API function, e.g. "report repair status".

builtin/worktree.c static int remove_worktree(int ac, const char **av, const char *prefix) · line 1040

Quoted 7 lines
builtin/worktree.c +7 -0
+{
+	if (!iserr)
+		printf_ln(_("repair: %s: %s"), msg, path);
+	else {
+		fprintf_ln(stderr, _("error: %s: %s"), msg, path);
+		*(int *)cb_data = 1;
+	}

Spend a local variable would pay for readability, e.g.

	{
		int *exit_status = (int *)cb_data;
		if (!iserr) {
			... report success ...
		} else {
			... report failure ...
			*exit_status = 1;
	        }
	}

would make it clearer that the number '1' is assigned to update the value used as the exit status. With the same callback API, the caller could be counting the number of secondaries failed to be resurrected, in which case assignment of 1 is a potential bug that needs to be updated to

		*(int *)cb_data += 1;

but the reader cannot tell from the generic name cb_data.

Quoted 31 lines
builtin/worktree.c +3 -0
+}
+
 static int repair(int ac, const char **av, const char *prefix)
 {
 	struct option options[] = {
@@ -1040,6 +1050,7 @@ static int repair(int ac, const char **av, const char *prefix)
 	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
 	if (ac)
 		usage_with_options(worktree_usage, options);
+	repair_worktrees(repair_cb, &rc);
 	return rc;
 }
 
t/t2406-worktree-repair.sh +10 -0
index cc679e1a21..9379a63130 100755
@@ -8,4 +8,76 @@ test_expect_success setup '
 	test_commit init
 '
 
+test_expect_success 'skip missing worktree' '
+	test_when_finished "git worktree prune" &&
+	git worktree add --detach missing &&
+	rm -rf missing &&
+	git worktree repair >out 2>err &&
+	test_must_be_empty out &&
+	test_must_be_empty err
+'
+
+test_expect_success "don't clobber .git repo" '

t/t2406-worktree-repair.sh test_expect_success setup ' · line 30

Quoted 10 lines
t/t2406-worktree-repair.sh +10 -0
+	test_when_finished "rm -rf repo && git worktree prune" &&
+	git worktree add --detach repo &&
+	rm -rf repo &&
+	test_create_repo repo &&
+	test_must_fail git worktree repair >out 2>err &&
+	test_must_be_empty out &&
+	test_i18ngrep ".git is not a file" err
+'
+
+test_corrupt_gitlink () {

See gitglossary::gitfile. We'd need to find all references to "gitlink" and "git link" in this series and decide which ones need to be fixed (there might be genuine references to gitlink---I haven't read the series through).

Quoted 5 lines
worktree.c +0 -0
index 62217b4a6b..029ce91fdf 100644

worktree.c int other_head_refs(each_ref_fn fn, void *cb_data) · line 580

Quoted 10 lines
worktree.c +7 -0
@@ -571,3 +571,56 @@ int other_head_refs(each_ref_fn fn, void *cb_data)
 	free_worktrees(worktrees);
 	return ret;
 }
+
+/*
+ * Repair worktree's /path/to/worktree/.git link if missing, corrupt, or not
+ * pointing at <repo>/worktrees/<id>.
+ */
+static void repair_dotgit(struct worktree *wt,
+			  worktree_repair_cb *cb, void *cb_data)

"dotgit" is an OK name in this context, I would think. repair_gitfile is also fine, but "dotgit" may be more explicit.

It is customary to call the callback function "fn", not "cb", which sometimes used as a short-hand for "cb_data".

worktree.c int other_head_refs(each_ref_fn fn, void *cb_data) · line 590

Quoted 10 lines
worktree.c +10 -0
+{
+	struct strbuf dotgit = STRBUF_INIT;
+	struct strbuf repo = STRBUF_INIT;
+	char *backlink;
+	const char *repair = NULL;
+	int err;
+
+	/* missing worktree can't be repaired */
+	if (!file_exists(wt->path))
+		return;

Shouldn't it be a bit stronger? if wt->path is not a directory, it cannot be the top of the working tree of a secondary worktree.

worktree.c int other_head_refs(each_ref_fn fn, void *cb_data) · line 592

Quoted 1 line
worktree.c +1 -0
+	strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);

OK, the need to use git_common_path() is a good justification why it is easier to implement if we initialize ourselves using the primary copy's repository data (i.e. require the user to start "worktree repair" in the primary copy or any working secondary worktrees, and let the normal setup.c sequence to prime us in the repository), instead of allowing the user to start at the secondary worktree whose gitfile got broken.

worktree.c int other_head_refs(each_ref_fn fn, void *cb_data) · line 605

Quoted 12 lines
worktree.c +12 -0
+	strbuf_addf(&dotgit, "%s/.git", wt->path);
+	backlink = xstrdup_or_null(read_gitfile_gently(dotgit.buf, &err));
+
+	if (err == READ_GITFILE_ERR_NOT_A_FILE)
+		cb(1, wt->path, _(".git is not a file"), cb_data);
+	else if (err)
+		repair = _(".git link broken");
+	else if (fspathcmp(backlink, repo.buf))
+		repair = _(".git link incorrect");
+	if (repair) {
+		cb(0, wt->path, repair, cb_data);
+		write_file(dotgit.buf, "gitdir: %s", repo.buf);

Shouldn't write_file() be monitored for its failure, and the failure reported back to the callback function?

worktree.c int other_head_refs(each_ref_fn fn, void *cb_data) · line 614

Quoted 9 lines
worktree.c +9 -0
+	}
+
+	free(backlink);
+	strbuf_release(&repo);
+	strbuf_release(&dotgit);
+}
+
+static void repair_noop_cb(int iserr, const char *path, const char *msg,
+			   void *cb_data) {}

Even the body is empty, just follow the coding guidelines, i.e.

	static void function_name(parameter list)
	{
		/* nothing */
	}

worktree.c int other_head_refs(each_ref_fn fn, void *cb_data) · line 626

Quoted 11 lines
worktree.c +11 -0
+void repair_worktrees(worktree_repair_cb *cb, void *cb_data)
+{
+	struct worktree **worktrees = get_worktrees();
+	struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
+
+	if (!cb)
+		cb = repair_noop_cb;
+	for (; *wt; wt++)
+		repair_dotgit(*wt, cb, cb_data);
+	free_worktrees(worktrees);
+}

This "repair"s only in one direction. Manual movement of secondary worktrees, if we want to support repairing, needs the user to tell where the new location of the secondary is, and we need a code to update the record of the location of the secondary kept at the main worktree, which is not needed in the repair implemented by this step.

Quoted 12 lines
worktree.h +4 -0
index 516744c433..7d085c7b91 100644
@@ -89,6 +89,17 @@ int validate_worktree(const struct worktree *wt,
 void update_worktree_location(struct worktree *wt,
 			      const char *path_);
 
+typedef void worktree_repair_cb(int iserr, const char *path, const char *msg,
+				void *cb_data);
+
+/*

worktree.h int validate_worktree(const struct worktree *wt, · line 105

Quoted 10 lines
worktree.h +7 -0
+ * Visit each registered linked worktree and repair corruptions. For each
+ * repair made or error encountered while attempting a repair, the callback, if
+ * non-NULL, is called with the path of the worktree and a description of the
+ * repair or error, along with the callback user-data.
+ */
+void repair_worktrees(worktree_repair_cb *, void *cb_data);
+
 /*
  * Free up the memory for worktree(s)
  */

Looking good.

Thanks.

#6 lore raw

Re: [PATCH 2/5] worktree: teach "repair" to fix worktree back-links to main worktree

Quoted 11 lines · Junio C Hamano
Quoted 1 line · Eric Sunshine

The .git file in a linked worktree is a "gitlink" which points back to

Please never call it a "gitlink" (which is a word reserved for a cache entry with 160000 mode that typically point at a commit object, an implementation detail of the mechanism to bind a submodule to its superproject) to avoid confusing future readers of our code and documentation. The setup.c code calls it as "gitfile", since it was introduced at b44ebb19 (Add platform-independent .git "symlink", 2008-02-20), I think, and that is how it has been described in the glossary.

Thanks. I don't know why my brain latched onto "gitlink" even though I noted the difference in the name read_gitfile_gently().

Quoted 11 lines
Quoted 6 lines

the .git/worktrees/<id> entry in the main worktree or bare repository. If a worktree's .git file is deleted or becomes corrupted or outdated, then the linked worktree won't know how to find the repository or any of its own administrative files (such as 'index', 'HEAD', etc.). An easy way for the .git file to become outdated is for the user to move the main worktree or bare repository.

I am not sure if a directory that used to be a secondary worktree would still appear to be a Git controlled worktree after its ".git" removed, but I guess a ".git" pointing at a wrong place won't, either.

The directory itself will be disconnected from its object database, thus non-working, however, its .git/worktrees/<id> entry will still be present, so the repository will still consider it a linked worktree.

Quoted 2 lines

I am just curious how much more involved in dealing with the "is deleted" situation than "becomes corrupted" situation.

They are handled identically. "git worktree repair" simply visits each .git/worktrees/<id> entry and checks if the /path/to/worktree/.git gitfile points back to .git/worktrees/<id>. If it doesn't -- whether because the gitfile is missing, corrupted, or simply pointing at the wrong location -- it recreates the gitfile with the correct content.

Quoted 10 lines

sequence, the user does

user goes to a directory D, thinking it is a valid secondary worktree and wanting to work in it, and finds that Git does not think it is.

user goes to X, which is the location of the primary copy, and tells "worktree repair" that the directory D ought to be the top of the working tree for a secondary worktree.

Quoted 6 lines

"worktree repair" creates/updates D/.git gitfile to point at X.

user comes back to D and starts working.

because "git worktree repair" wants to do the usual setup sequence anyway.

Correct. Perhaps not 100% intuitive now that you mention it but that sort of interface could be built atop the machinery provided by this patch series, and I don't think this initial implementation needs to have all the bells and whistles. Even with what is implemented by this series, support becomes much easier -- simply ask the user to run "git worktree repair" in the main worktree (or in any functional worktree) -- than the situation presently which requires precise instructions about how to patch administrative files by hand.

Quoted 10 lines

Documentation/git-worktree.txt with `--reason`. · line 104

Quoted 6 lines

Move a working tree to a new location. Note that the main working tree

Documentation/git-worktree.txt +4 -1
-or linked working trees containing submodules cannot be moved.
+or linked working trees containing submodules cannot be moved with this
+command. (The `git worktree repair` command, however, can reestablish
+the connection with linked working trees if you move the main working
+tree manually.)

... meaning after moving the main working tree, repair can be used to touch-up the submodule directories?

Quoted 4 lines

Ah, no. You are saying "git worktree move" still cannot be used to move the main working tree (the ones with submodules in it we do not even care), but as a workaround, the user can "mv" it manually and run "repair" to fix all the secondary worktrees.

Seeing as the meaning wasn't clear upon your first reading, perhaps I should remove this change? Or rewrite it in some way?

Quoted 10 lines

Hopefully somewhere in the rest of this series it would become automatic? After all, instead of letting users "mv" things without us knowing what is going on, if we let them say "worktree move" the primary working tree, we know both the source and the destination directories of such a move, and because we know all the secondary worktrees, we can run "worktree repair" on them as part of this "worktree move" of the primary working tree.

It is perfectly fine that it is not happening here in this step to keep each step digestible, of course.

Duy's original implementation did automatically repair worktree administrative files but it ended up being buggy (I don't recall the precise details) and discussion between the two of you resulted in the functionality being removed with the possibility of resurrecting it later. (Unfortunately, I can't find references to any of the discussion right now.)

Automatic repair was definitely on my mind as I wrote this series, but as something which could possibly be added later atop the machinery implemented by this series.

Quoted 6 lines
Quoted 1 line

+test_corrupt_gitlink () {

See gitglossary::gitfile. We'd need to find all references to "gitlink" and "git link" in this series and decide which ones need to be fixed (there might be genuine references to gitlink---I haven't read the series through).

They all need to be fixed. There aren't any genuine gitlink references in the series.

Quoted 5 lines

worktree.c int other_head_refs(each_ref_fn fn, void *cb_data) · line 580

Quoted 2 lines
worktree.c +2 -0
+static void repair_dotgit(struct worktree *wt,
+            worktree_repair_cb *cb, void *cb_data)

"dotgit" is an OK name in this context, I would think. repair_gitfile is also fine, but "dotgit" may be more explicit.

I think I originally called the function repair_gitlink() but changed it to "dotgit" to be more explicit. But now that I know "gitfile" is the correct term, repair_gitfile() seems preferable.

Quoted 6 lines

worktree.c int other_head_refs(each_ref_fn fn, void *cb_data) · line 590

Quoted 3 lines
worktree.c +3 -0
+   /* missing worktree can't be repaired */
+   if (!file_exists(wt->path))
+       return;

Shouldn't it be a bit stronger? if wt->path is not a directory, it cannot be the top of the working tree of a secondary worktree.

I'm not quite sure what you're asking. This code is saying that if the path recorded in .git/worktrees/<id>/gitdir doesn't exist, then there simply isn't anything we can repair.

Quoted 9 lines
Quoted 1 line

+ strbuf_realpath(&repo, git_common_path("worktrees/%s", wt->id), 1);

OK, the need to use git_common_path() is a good justification why it is easier to implement if we initialize ourselves using the primary copy's repository data (i.e. require the user to start "worktree repair" in the primary copy or any working secondary worktrees, and let the normal setup.c sequence to prime us in the repository), instead of allowing the user to start at the secondary worktree whose gitfile got broken.

Yes, and this does highlight that there are conditions under which "git worktree repair" simply won't operate. I did consider handling a number of such conditions but ultimately decided that I'd rather keep this initial implementation as simple and straightforward as possible. Such improvements can be built atop.

Quoted 6 lines

worktree.c int other_head_refs(each_ref_fn fn, void *cb_data) · line 605

Quoted 3 lines
worktree.c +3 -0
+   if (repair) {
+       cb(0, wt->path, repair, cb_data);
+       write_file(dotgit.buf, "gitdir: %s", repo.buf);

Shouldn't write_file() be monitored for its failure, and the failure reported back to the callback function?

Yes and no. write_file() will die() if it can't write the file. That's not necessarily ideal behavior for a command which wants to repair _all_ problems, and the decision to use write_file() was not one I made lightly. The bigger picture is that, although "git worktree repair" ideally should not abort when attempting repairs, there are a number of library functions called by "git worktree repair" which undesirably die(). And writing all that functionality from scratch or enhancing those functions to have "gentle" modes would have made this series far longer and far more complex. So, I eventually decided to keep the implementation simple, with the idea that such enhancements can be made later as needed and as we gain experience with the command.

Quoted 10 lines
Quoted 10 lines
worktree.c +10 -0
+void repair_worktrees(worktree_repair_cb *cb, void *cb_data)
+{
+   struct worktree **worktrees = get_worktrees();
+   struct worktree **wt = worktrees + 1; /* +1 skips main worktree */
+
+   if (!cb)
+       cb = repair_noop_cb;
+   for (; *wt; wt++)
+       repair_dotgit(*wt, cb, cb_data);
+   free_worktrees(worktrees);
Quoted 8 lines
Quoted 1 line

+}

This "repair"s only in one direction. Manual movement of secondary worktrees, if we want to support repairing, needs the user to tell where the new location of the secondary is, and we need a code to update the record of the location of the secondary kept at the main worktree, which is not needed in the repair implemented by this step.

Correct. Patch [3/5] implements repair in the other direction.

Quoted 1 line

Looking good.

Thanks for the thoughtful review comments (including the ones to which I didn't respond directly -- they will be addressed by the re-roll itself).

#7 lore raw

PATCH 3/5 worktree: teach "repair" to fix outgoing links to worktrees

In reply to Eric Sunshine

The .git/worktrees/<id>/gitdir file points at the location of a linked worktree's .git file. Its content must be of the form /path/to/worktree/.git (from which the location of the worktree itself can be derived by stripping the "/.git" suffix). If the gitdir file is deleted or becomes corrupted or outdated, then Git will be unable to find the linked worktree. An easy way for the gitdir file to become outdated is for the user to move the worktree manually (without using "git worktree move"). Although it is possible to manually update the gitdir file to reflect the new linked worktree location, doing so requires a level of knowledge about worktree internals beyond what a user should be expected to know offhand.

Therefore, teach "git worktree repair" how to repair broken or outdated .git/worktrees/<id>/gitdir files automatically. (For this to work, the command must either be invoked from within the worktree whose gitdir file requires repair, or from within the main or any linked worktree by providing the path of the broken worktree as an argument to "git worktree repair".)

---
 Documentation/git-worktree.txt | 14 ++++--
 builtin/worktree.c             |  7 ++-
 t/t2406-worktree-repair.sh     | 86 ++++++++++++++++++++++++++++++++++
 worktree.c                     | 74 +++++++++++++++++++++++++++++
 worktree.h                     | 11 +++++
 5 files changed, 187 insertions(+), 5 deletions(-)
Documentation/git-worktree.txt +11 -3
index acb0ea1c2e..a43a0af0af 100644
@@ -15,7 +15,7 @@ SYNOPSIS
 'git worktree move' <worktree> <new-path>
 'git worktree prune' [-n] [-v] [--expire <expire>]
 'git worktree remove' [-f] <worktree>
-'git worktree repair'
+'git worktree repair' [<path>...]
 'git worktree unlock' <worktree>
 
 DESCRIPTION
@@ -114,7 +114,7 @@ and no modification in tracked files) can be removed. Unclean working
 trees or ones with submodules can be removed with `--force`. The main
 working tree cannot be removed.
 
-repair::
+repair [<path>...]::
 
 Repair working tree administrative files, if possible, if they have
 become corrupted or outdated due to external factors.
@@ -123,6 +123,13 @@ For instance, if the main working tree (or bare repository) is moved,
 linked working trees will be unable to locate it. Running `repair` in
 the recently-moved main working tree will reestablish the connection
 from linked working trees back to the main working tree.
++
+Similarly, if a linked working tree is moved without using `git worktree
+move`, the main working tree (or bare repository) will be unable to
+locate it. Running `repair` within the recently-moved working tree will
+reestablish the connection. If multiple linked working trees are moved,
+running `repair` from any working tree with each tree's new `<path>` as
+an argument, will reestablish the connection to all the specified paths.
 
 unlock::
 
@@ -317,7 +324,8 @@ in the entry's directory. For example, if a linked working tree is moved
 to `/newpath/test-next` and its `.git` file points to
 `/path/main/.git/worktrees/test-next`, then update
 `/path/main/.git/worktrees/test-next/gitdir` to reference `/newpath/test-next`
-instead.
+instead. Better yet, run `git worktree repair` to reestablish the connection
+automatically.
 
 To prevent a `$GIT_DIR/worktrees` entry from being pruned (which
 can be useful in some situations, such as when the
builtin/worktree.c +5 -2
index 62e33eb7f5..19bbc246ad 100644
@@ -1042,15 +1042,18 @@ static void repair_cb(int iserr, const char *path, const char *msg, void *cb_dat
 
 static int repair(int ac, const char **av, const char *prefix)
 {
+	const char **p;
+	const char *self[] = { ".", NULL };
 	struct option options[] = {
 		OPT_END()
 	};
 	int rc = 0;
 
 	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
-	if (ac)
-		usage_with_options(worktree_usage, options);
 	repair_worktrees(repair_cb, &rc);
+	p = ac > 0 ? av : self;
+	for (; *p; p++)
+		repair_worktree_at_path(*p, repair_cb, &rc);
 	return rc;
 }
 
t/t2406-worktree-repair.sh +86 -0

90 lines not on this page · raw message

worktree.c +74 -0

77 lines not on this page · raw message

worktree.h +11 -0
index 7d085c7b91..9cb7f05741 100644
@@ -100,6 +100,17 @@ typedef void worktree_repair_cb(int iserr, const char *path, const char *msg,
  */
 void repair_worktrees(worktree_repair_cb *, void *cb_data);
 
+/*
+ * Repair administrative files corresponding to the worktree at the given path.
+ * The worktree's .git link pointing at the repository must be intact for the
+ * repair to succeed. Useful for re-associating an orphaned worktree with the
+ * repository if the worktree has been moved manually (without using "git
+ * worktree move"). For each repair made or error encountered while attempting
+ * a repair, the callback, if non-NULL, is called with the path of the worktree
+ * and a description of the repair or error, along with the callback user-data.
+ */
+void repair_worktree_at_path(const char *, worktree_repair_cb *, void *cb_data);
+
 /*
  * Free up the memory for worktree(s)
  */
Signature · 1 line
2.28.0.461.g40977abb40
#8 lore raw

Re: [PATCH 3/5] worktree: teach "repair" to fix outgoing links to worktrees

Quoted 12 lines · Eric Sunshine

The .git/worktrees/<id>/gitdir file points at the location of a linked worktree's .git file. Its content must be of the form /path/to/worktree/.git (from which the location of the worktree itself can be derived by stripping the "/.git" suffix). If the gitdir file is deleted or becomes corrupted or outdated, then Git will be unable to find the linked worktree. An easy way for the gitdir file to become outdated is for the user to move the worktree manually (without using "git worktree move"). Although it is possible to manually update the gitdir file to reflect the new linked worktree location, doing so requires a level of knowledge about worktree internals beyond what a user should be expected to know offhand.

Quoted 6 lines

Therefore, teach "git worktree repair" how to repair broken or outdated .git/worktrees/<id>/gitdir files automatically. (For this to work, the command must either be invoked from within the worktree whose gitdir file requires repair, or from within the main or any linked worktree by providing the path of the broken worktree as an argument to "git worktree repair".)

Would git "work" in a corrupt worktree whose gitfile is broken, in the sense that it notices that the cwd is the top of the working tree of a secondary worktree? I can imagine how it would work, starting in one of the functioning worktrees so that git can locate where the primary copy is, with end-user supplied path to a directory that is supposed to be the top of the working tree of a secondary worktree.

Hmph, if the secondary is _moved_, how would "worktree repair $path" would know which <id> the $path corresponds to? Would we just cull all the <id> that do not point at working secondary worktrees and add the $path as if it were a new one by allocating a new <id>, or reusing a randomly chosen <id> that points at a non-existing location?

#9 lore raw

Re: [PATCH 3/5] worktree: teach "repair" to fix outgoing links to worktrees

Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Eric Sunshine

Therefore, teach "git worktree repair" how to repair broken or outdated .git/worktrees/<id>/gitdir files automatically. (For this to work, the command must either be invoked from within the worktree whose gitdir file requires repair, or from within the main or any linked worktree by providing the path of the broken worktree as an argument to "git worktree repair".)

Would git "work" in a corrupt worktree whose gitfile is broken, in the sense that it notices that the cwd is the top of the working tree of a secondary worktree?

No. For this repair to work, the gitfile in the linked worktree must be intact; it must be pointing back at the .git/worktrees/<id> directory so that "git worktree repair" can repair the corresponding .git/worktrees/<id>/gitdir file.

Making it "work" for the case when both the worktree's gitfile is broken and .git/worktrees/<id>/gitdir is broken would require an enhancement like what you mentioned in your review of patch [2/5] in which the user would manually specify the location of the main worktree (or repository). That is something which can be added, but I wanted to keep this initial implementation simple.

Quoted 6 lines

Hmph, if the secondary is _moved_, how would "worktree repair $path" would know which <id> the $path corresponds to? Would we just cull all the <id> that do not point at working secondary worktrees and add the $path as if it were a new one by allocating a new <id>, or reusing a randomly chosen <id> that points at a non-existing location?

Since this can only work if the linked worktree's gitfile is intact, and since the content of the gitfile is the path .git/worktrees/<id>, "git worktree repair" knows the exact <id>, thus the precise .git/worktrees/<id>/gitdir file to repair. It is deterministic; there is no guessing about <id>, and there is no creating a new <id> magically (though I did consider additional repair cases but opted against them for the initial implementation).

#10 lore raw

Re: [PATCH 3/5] worktree: teach "repair" to fix outgoing links to worktrees

Quoted 4 lines · Eric Sunshine

No. For this repair to work, the gitfile in the linked worktree must be intact; it must be pointing back at the .git/worktrees/<id> directory so that "git worktree repair" can repair the corresponding .git/worktrees/<id>/gitdir file.

OK, I missed the fact that we can learn <id> quite easily if we have the gitfile. Thanks.

#11 lore raw

Re: [PATCH 3/5] worktree: teach "repair" to fix outgoing links to worktrees

In reply to Eric Sunshine

Hi Eric,

Quoted 15 lines · Eric Sunshine
worktree.c +7 -0
index 029ce91fdf..6ade4f0d8b 100644
@@ -624,3 +624,77 @@ void repair_worktrees(worktree_repair_cb *cb, void *cb_data)
 		repair_dotgit(*wt, cb, cb_data);
 	free_worktrees(worktrees);
 }
+
+static int is_main_worktree_path(const char *path)
+{
+	struct strbuf target = STRBUF_INIT;
+	struct strbuf main = STRBUF_INIT;
+	int cmp;
+

worktree.c void repair_worktrees(worktree_repair_cb *cb, void *cb_data) · line 643

Quoted 10 lines
worktree.c +10 -0
+	strbuf_add_real_path(&target, path);
+	strbuf_strip_suffix(&target, "/.git");
+	strbuf_add_real_path(&main, get_git_common_dir());
+	strbuf_strip_suffix(&main, "/.git");
+	cmp = fspathcmp(main.buf, target.buf);
+
+	strbuf_release(&main);
+	strbuf_release(&target);
+	return !cmp;
+}

This breaks our linux-gcc job, and I need this on top, to make it even build:

-- snipsnap --
From: Johannes Schindelin <[email protected]>
Subject: [PATCH] fixup??? worktree: teach "repair" to fix outgoing links to worktrees

This is needed to shut up GCC's "‘main’ is usually a function [-Werror=main]" error.

---
 worktree.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
worktree.c +5 -5
index 6ade4f0d8b2b..5471915d4680 100644
@@ -628,16 +628,16 @@ void repair_worktrees(worktree_repair_cb *cb, void *cb_data)
 static int is_main_worktree_path(const char *path)
 {
 	struct strbuf target = STRBUF_INIT;
-	struct strbuf main = STRBUF_INIT;
+	struct strbuf main_worktree = STRBUF_INIT;
 	int cmp;
 
 	strbuf_add_real_path(&target, path);
 	strbuf_strip_suffix(&target, "/.git");
-	strbuf_add_real_path(&main, get_git_common_dir());
-	strbuf_strip_suffix(&main, "/.git");
-	cmp = fspathcmp(main.buf, target.buf);
+	strbuf_add_real_path(&main_worktree, get_git_common_dir());
+	strbuf_strip_suffix(&main_worktree, "/.git");
+	cmp = fspathcmp(main_worktree.buf, target.buf);
 
-	strbuf_release(&main);
+	strbuf_release(&main_worktree);
 	strbuf_release(&target);
 	return !cmp;
 }

--
2.28.0.windows.1

#12 lore raw

Re: [PATCH 3/5] worktree: teach "repair" to fix outgoing links to worktrees

Quoted 5 lines · Johannes Schindelin
Quoted 1 line · Eric Sunshine

+ struct strbuf main = STRBUF_INIT;

This is needed to shut up GCC's "‘main’ is usually a function [-Werror=main]" error.

D'oh, thanks. I got hit by this just a couple months ago[1] and (for some reason) was even looking at [1] the same day I submitted this patch. Will fix it in the re-roll.

[1]: https://lore.kernel.org/git/[email protected]/

#13 lore raw

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

In reply to Eric Sunshine

A linked worktree's .git file is a "gitlink" 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.

---
 builtin/init-db.c |  2 ++
 t/t0001-init.sh   | 11 +++++++++++
 2 files changed, 13 insertions(+)
builtin/init-db.c +2 -0
index bbc9bc78f9..7b915d88ab 100644
@@ -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);
t/t0001-init.sh +11 -0
index 50222a10c5..e489eb4ddb 100755
@@ -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 &&
 	(
Signature · 1 line
2.28.0.461.g40977abb40
#14 lore raw

PATCH 5/5 init: make --separate-git-dir work from within linked worktree

In reply to Eric Sunshine

The intention of git init --separate-work-dir=<path> is to move the .git/ directory to a location outside of the main worktree. When used within a linked worktree, however, rather than moving the .git/ directory as intended, it instead incorrectly moves the worktree's .git/worktrees/<id> directory to <path>, thus disconnecting the linked worktree from its parent repository and breaking the worktree in the process since its local .git file no longer points at a location at which it can find the object database. Fix this broken behavior.

An intentional side-effect of this change is that it also closes a loophole not caught by ccf236a23a (init: disallow --separate-git-dir with bare repository, 2020-08-09) in which the check to prevent --separate-git-dir being used in conjunction with a bare repository was unable to detect the invalid combination when invoked from within a linked worktree. Therefore, add a test to verify that this loophole is closed, as well.

---
 builtin/init-db.c | 24 ++++++++++++++++++++++++
 t/t0001-init.sh   | 21 +++++++++++++++++++--
 2 files changed, 43 insertions(+), 2 deletions(-)
builtin/init-db.c +24 -0
index 7b915d88ab..6a94d20a2e 100644
@@ -642,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, 0);
+		}
+		strbuf_release(&sb);
+	}
+
 	if (is_bare_repository_cfg < 0)
 		is_bare_repository_cfg = guess_repository_type(git_dir);
 
t/t0001-init.sh +19 -2
index e489eb4ddb..2f7c3dcd0f 100755
@@ -329,6 +329,15 @@ test_expect_success 'implicit bare & --separate-git-dir incompatible' '
 	test_i18ngrep "incompatible" err
 '
 
+test_expect_success 'bare & --separate-git-dir incompatible within worktree' '
+	test_when_finished "rm -rf bare.git linkwt seprepo" &&
+	test_commit gumby &&
+	git clone --bare . bare.git &&
+	git -C bare.git worktree add --detach ../linkwt &&
+	test_must_fail git -C linkwt init --separate-git-dir seprepo 2>err &&
+	test_i18ngrep "incompatible" err
+'
+
 test_lazy_prereq GETCWD_IGNORES_PERMS '
 	base=GETCWD_TEST_BASE_DIR &&
 	mkdir -p $base/dir &&
@@ -405,15 +414,23 @@ 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' '
+sep_git_dir_worktree ()  {
 	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 "$1" 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 're-init to move gitdir with linked worktrees' '
+	sep_git_dir_worktree mainwt
+'
+
+test_expect_success 're-init to move gitdir within linked worktree' '
+	sep_git_dir_worktree linkwt
 '
 
 test_expect_success MINGW '.git hidden' '
Signature · 1 line
2.28.0.461.g40977abb40
#15 lore raw

v2 cover add "git worktree repair" command

In reply to Eric Sunshine

This is a re-roll of [1] which adds a "git worktree repair" command and which fixes bugs with "git init --separate-git-dir" related to worktrees.

Changes since v1:

* employ proper terminology s/gitlink/gitfile/g (junio[2])
* tighten documentation slightly (junio[2])
* rename callback function and typedef s/cb/fn/g (junio[2])
* give concrete callback function more meaningful name (junio[2])
* employ explicit name for callback data (junio[2])
* explicitly check that worktree path is directory, not file, and bail with meaningful error message; add associated test (junio[2])
* fix empty-function style violation (junio[2])
* refine worktree repair callback typedef (sunshine)
* pacify GCC -Werror=main (dscho[3])
* pacify Sparse s/0/NULL/ (ramsay[4])

[1]: https://lore.kernel.org/git/[email protected]/
[2]: https://lore.kernel.org/git/[email protected]/
[3]: https://lore.kernel.org/git/[email protected]/
[4]: https://lore.kernel.org/git/[email protected]/

Eric Sunshine (5):
worktree: add skeleton "repair" command
worktree: teach "repair" to fix worktree back-links to main worktree
worktree: teach "repair" to fix outgoing links to worktrees
init: teach --separate-git-dir to repair linked worktrees
init: make --separate-git-dir work from within linked worktree

 Documentation/git-worktree.txt |  26 ++++-
 builtin/init-db.c              |  26 +++++
 builtin/worktree.c             |  30 ++++++
 t/t0001-init.sh                |  28 ++++++
 t/t2406-worktree-repair.sh     | 179 +++++++++++++++++++++++++++++++++
 worktree.c                     | 135 +++++++++++++++++++++++++
 worktree.h                     |  23 +++++
 7 files changed, 445 insertions(+), 2 deletions(-)
 create mode 100755 t/t2406-worktree-repair.sh

Interdiff against v1:

Documentation/git-worktree.txt +2 -2
index a43a0af0af..f70cda4b36 100644
@@ -121,8 +121,8 @@ become corrupted or outdated due to external factors.
 +
 For instance, if the main working tree (or bare repository) is moved,
 linked working trees will be unable to locate it. Running `repair` in
-the recently-moved main working tree will reestablish the connection
-from linked working trees back to the main working tree.
+the main working tree will reestablish the connection from linked
+working trees back to the main working tree.
 +
 Similarly, if a linked working tree is moved without using `git worktree
 move`, the main working tree (or bare repository) will be unable to
builtin/init-db.c +1 -1
index 6a94d20a2e..cd3e760541 100644
@@ -661,7 +661,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
 			if (chdir(mainwt.buf) < 0)
 				die_errno(_("cannot chdir to %s"), mainwt.buf);
 			strbuf_release(&mainwt);
-			git_dir = strbuf_detach(&sb, 0);
+			git_dir = strbuf_detach(&sb, NULL);
 		}
 		strbuf_release(&sb);
 	}
builtin/worktree.c +7 -6
index 19bbc246ad..8165343145 100644
@@ -1030,13 +1030,14 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
 	return ret;
 }
 
-static void repair_cb(int iserr, const char *path, const char *msg, void *cb_data)
+static void report_repair(int iserr, const char *path, const char *msg, void *cb_data)
 {
-	if (!iserr)
+	if (!iserr) {
 		printf_ln(_("repair: %s: %s"), msg, path);
-	else {
+	} else {
+		int *exit_status = (int *)cb_data;
 		fprintf_ln(stderr, _("error: %s: %s"), msg, path);
-		*(int *)cb_data = 1;
+		*exit_status = 1;
 	}
 }
 
@@ -1050,10 +1051,10 @@ static int repair(int ac, const char **av, const char *prefix)
 	int rc = 0;
 
 	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
-	repair_worktrees(repair_cb, &rc);
+	repair_worktrees(report_repair, &rc);
 	p = ac > 0 ? av : self;
 	for (; *p; p++)
-		repair_worktree_at_path(*p, repair_cb, &rc);
+		repair_worktree_at_path(*p, report_repair, &rc);
 	return rc;
 }
 
t/t2406-worktree-repair.sh +20 -10
index 87bd8fc526..1fe468bfe8 100755
@@ -17,6 +17,16 @@ test_expect_success 'skip missing worktree' '
 	test_must_be_empty err
 '
 
+test_expect_success 'worktree path not directory' '
+	test_when_finished "git worktree prune" &&
+	git worktree add --detach notdir &&
+	rm -rf notdir &&
+	>notdir &&
+	test_must_fail git worktree repair >out 2>err &&
+	test_must_be_empty out &&
+	test_i18ngrep "not a directory" err
+'
+
 test_expect_success "don't clobber .git repo" '
 	test_when_finished "rm -rf repo && git worktree prune" &&
 	git worktree add --detach repo &&
@@ -27,7 +37,7 @@ test_expect_success "don't clobber .git repo" '
 	test_i18ngrep ".git is not a file" err
 '
 
-test_corrupt_gitlink () {
+test_corrupt_gitfile () {
 	butcher=$1 &&
 	problem=$2 &&
 	repairdir=${3:-.} &&
@@ -43,30 +53,30 @@ test_corrupt_gitlink () {
 }
 
 test_expect_success 'repair missing .git file' '
-	test_corrupt_gitlink "rm -f corrupt/.git" ".git link broken"
+	test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken"
 '
 
 test_expect_success 'repair bogus .git file' '
-	test_corrupt_gitlink "echo \"gitdir: /nowhere\" >corrupt/.git" \
-		".git link broken"
+	test_corrupt_gitfile "echo \"gitdir: /nowhere\" >corrupt/.git" \
+		".git file broken"
 '
 
 test_expect_success 'repair incorrect .git file' '
 	test_when_finished "rm -rf other && git worktree prune" &&
 	test_create_repo other &&
 	other=$(git -C other rev-parse --absolute-git-dir) &&
-	test_corrupt_gitlink "echo \"gitdir: $other\" >corrupt/.git" \
-		".git link incorrect"
+	test_corrupt_gitfile "echo \"gitdir: $other\" >corrupt/.git" \
+		".git file incorrect"
 '
 
 test_expect_success 'repair .git file from main/.git' '
-	test_corrupt_gitlink "rm -f corrupt/.git" ".git link broken" .git
+	test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" .git
 '
 
 test_expect_success 'repair .git file from linked worktree' '
 	test_when_finished "rm -rf other && git worktree prune" &&
 	git worktree add --detach other &&
-	test_corrupt_gitlink "rm -f corrupt/.git" ".git link broken" other
+	test_corrupt_gitfile "rm -f corrupt/.git" ".git file broken" other
 '
 
 test_expect_success 'repair .git file from bare.git' '
@@ -94,14 +104,14 @@ test_expect_success 'repo not found; .git not file' '
 	test_i18ngrep ".git is not a file" err
 '
 
-test_expect_success 'repo not found; .git link broken' '
+test_expect_success 'repo not found; .git file broken' '
 	test_when_finished "rm -rf orig moved && git worktree prune" &&
 	git worktree add --detach orig &&
 	echo /invalid >orig/.git &&
 	mv orig moved &&
 	test_must_fail git worktree repair moved >out 2>err &&
 	test_must_be_empty out &&
-	test_i18ngrep ".git link broken" err
+	test_i18ngrep ".git file broken" err
 '
 
 test_expect_success 'repair broken gitdir' '
worktree.c +33 -25

135 lines not on this page · raw message

worktree.h +11 -10
index 9cb7f05741..ff7b62e434 100644
@@ -89,27 +89,28 @@ int validate_worktree(const struct worktree *wt,
 void update_worktree_location(struct worktree *wt,
 			      const char *path_);
 
-typedef void worktree_repair_cb(int iserr, const char *path, const char *msg,
-				void *cb_data);
+typedef void (* worktree_repair_fn)(int iserr, const char *path,
+				    const char *msg, void *cb_data);
 
 /*
  * Visit each registered linked worktree and repair corruptions. For each
- * repair made or error encountered while attempting a repair, the callback, if
- * non-NULL, is called with the path of the worktree and a description of the
- * repair or error, along with the callback user-data.
+ * repair made or error encountered while attempting a repair, the callback
+ * function, if non-NULL, is called with the path of the worktree and a
+ * description of the repair or error, along with the callback user-data.
  */
-void repair_worktrees(worktree_repair_cb *, void *cb_data);
+void repair_worktrees(worktree_repair_fn, void *cb_data);
 
 /*
  * Repair administrative files corresponding to the worktree at the given path.
- * The worktree's .git link pointing at the repository must be intact for the
+ * The worktree's .git file pointing at the repository must be intact for the
  * repair to succeed. Useful for re-associating an orphaned worktree with the
  * repository if the worktree has been moved manually (without using "git
  * worktree move"). For each repair made or error encountered while attempting
- * a repair, the callback, if non-NULL, is called with the path of the worktree
- * and a description of the repair or error, along with the callback user-data.
+ * a repair, the callback function, if non-NULL, is called with the path of the
+ * worktree and a description of the repair or error, along with the callback
+ * user-data.
  */
-void repair_worktree_at_path(const char *, worktree_repair_cb *, void *cb_data);
+void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
 
 /*
  * Free up the memory for worktree(s)
Signature · 1 line
2.28.0.531.g41c3d8a546
#16 lore raw

PATCH v2 1/5 worktree: add skeleton "repair" command

Worktree administrative files can become corrupted or outdated due to external factors. Although, it is often possible to recover from such situations by hand-tweaking these files, doing so requires intimate knowledge of worktree internals. While information necessary to make such repairs manually can be obtained from git-worktree.txt and gitrepository-layout.txt, we can assist users more directly by teaching git-worktree how to repair its administrative files itself (at least to some extent). Therefore, add a "git worktree repair" command which attempts to correct common problems which may arise due to factors beyond Git's control.

At this stage, the "repair" command is a mere skeleton; subsequent commits will flesh out the functionality.

---
 Documentation/git-worktree.txt |  6 ++++++
 builtin/worktree.c             | 15 +++++++++++++++
 t/t2406-worktree-repair.sh     | 11 +++++++++++
 3 files changed, 32 insertions(+)
 create mode 100755 t/t2406-worktree-repair.sh
Documentation/git-worktree.txt +6 -0
index 6ee6ec7982..ae432d39a8 100644
@@ -15,6 +15,7 @@ SYNOPSIS
 'git worktree move' <worktree> <new-path>
 'git worktree prune' [-n] [-v] [--expire <expire>]
 'git worktree remove' [-f] <worktree>
+'git worktree repair'
 'git worktree unlock' <worktree>
 
 DESCRIPTION
@@ -110,6 +111,11 @@ and no modification in tracked files) can be removed. Unclean working
 trees or ones with submodules can be removed with `--force`. The main
 working tree cannot be removed.
 
+repair::
+
+Repair working tree administrative files, if possible, if they have
+become corrupted or outdated due to external factors.
+
 unlock::
 
 Unlock a working tree, allowing it to be pruned, moved or deleted.
builtin/worktree.c +15 -0
index 378f332b5d..88af412d4f 100644
@@ -1030,6 +1030,19 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
 	return ret;
 }
 
+static int repair(int ac, const char **av, const char *prefix)
+{
+	struct option options[] = {
+		OPT_END()
+	};
+	int rc = 0;
+
+	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
+	if (ac)
+		usage_with_options(worktree_usage, options);
+	return rc;
+}
+
 int cmd_worktree(int ac, const char **av, const char *prefix)
 {
 	struct option options[] = {
@@ -1056,5 +1069,7 @@ int cmd_worktree(int ac, const char **av, const char *prefix)
 		return move_worktree(ac - 1, av + 1, prefix);
 	if (!strcmp(av[1], "remove"))
 		return remove_worktree(ac - 1, av + 1, prefix);
+	if (!strcmp(av[1], "repair"))
+		return repair(ac - 1, av + 1, prefix);
 	usage_with_options(worktree_usage, options);
 }
t/t2406-worktree-repair.sh new file +11 -0
new file mode 100755
index 0000000000..cc679e1a21
@@ -0,0 +1,11 @@
+#!/bin/sh
+
+test_description='test git worktree repair'
+
+. ./test-lib.sh
+
+test_expect_success setup '
+	test_commit init
+'
+
+test_done
Signature · 1 line
2.28.0.531.g41c3d8a546
#17 lore raw

PATCH v2 2/5 worktree: teach "repair" to fix worktree back-links to main worktree

In reply to Eric Sunshine

The .git file in a linked worktree is a "gitfile" which points back to the .git/worktrees/<id> entry in the main worktree or bare repository. If a worktree's .git file is deleted or becomes corrupted or outdated, then the linked worktree won't know how to find the repository or any of its own administrative files (such as 'index', 'HEAD', etc.). An easy way for the .git file to become outdated is for the user to move the main worktree or bare repository. Although it is possible to manually update each linked worktree's .git file to reflect the new repository location, doing so requires a level of knowledge about worktree internals beyond what a user should be expected to know offhand.

Therefore, teach "git worktree repair" how to repair broken or outdated worktree .git files automatically. (For this to work, the command must be invoked from within the main worktree or bare repository, or from within a worktree which has not become disconnected from the repository -- such as one which was created after the repository was moved.)

---
 Documentation/git-worktree.txt | 10 ++++-
 builtin/worktree.c             | 12 +++++
 t/t2406-worktree-repair.sh     | 82 ++++++++++++++++++++++++++++++++++
 worktree.c                     | 61 +++++++++++++++++++++++++
 worktree.h                     | 11 +++++
 5 files changed, 175 insertions(+), 1 deletion(-)
Documentation/git-worktree.txt +9 -1
index ae432d39a8..34fe47cecd 100644
@@ -98,7 +98,10 @@ with `--reason`.
 move::
 
 Move a working tree to a new location. Note that the main working tree
-or linked working trees containing submodules cannot be moved.
+or linked working trees containing submodules cannot be moved with this
+command. (The `git worktree repair` command, however, can reestablish
+the connection with linked working trees if you move the main working
+tree manually.)
 
 prune::
 
@@ -115,6 +118,11 @@ repair::
 
 Repair working tree administrative files, if possible, if they have
 become corrupted or outdated due to external factors.
++
+For instance, if the main working tree (or bare repository) is moved,
+linked working trees will be unable to locate it. Running `repair` in
+the main working tree will reestablish the connection from linked
+working trees back to the main working tree.
 
 unlock::
 
builtin/worktree.c +12 -0
index 88af412d4f..68b0032428 100644
@@ -1030,6 +1030,17 @@ static int remove_worktree(int ac, const char **av, const char *prefix)
 	return ret;
 }
 
+static void report_repair(int iserr, const char *path, const char *msg, void *cb_data)
+{
+	if (!iserr) {
+		printf_ln(_("repair: %s: %s"), msg, path);
+	} else {
+		int *exit_status = (int *)cb_data;
+		fprintf_ln(stderr, _("error: %s: %s"), msg, path);
+		*exit_status = 1;
+	}
+}
+
 static int repair(int ac, const char **av, const char *prefix)
 {
 	struct option options[] = {
@@ -1040,6 +1051,7 @@ static int repair(int ac, const char **av, const char *prefix)
 	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
 	if (ac)
 		usage_with_options(worktree_usage, options);
+	repair_worktrees(report_repair, &rc);
 	return rc;
 }
 
t/t2406-worktree-repair.sh +82 -0

86 lines not on this page · raw message

worktree.c +61 -0

64 lines not on this page · raw message

worktree.h +11 -0
index 516744c433..4fcb01348c 100644
@@ -89,6 +89,17 @@ int validate_worktree(const struct worktree *wt,
 void update_worktree_location(struct worktree *wt,
 			      const char *path_);
 
+typedef void (* worktree_repair_fn)(int iserr, const char *path,
+				    const char *msg, void *cb_data);
+
+/*
+ * Visit each registered linked worktree and repair corruptions. For each
+ * repair made or error encountered while attempting a repair, the callback
+ * function, if non-NULL, is called with the path of the worktree and a
+ * description of the repair or error, along with the callback user-data.
+ */
+void repair_worktrees(worktree_repair_fn, void *cb_data);
+
 /*
  * Free up the memory for worktree(s)
  */
Signature · 1 line
2.28.0.531.g41c3d8a546
#18 lore raw

PATCH v2 3/5 worktree: teach "repair" to fix outgoing links to worktrees

In reply to Eric Sunshine

The .git/worktrees/<id>/gitdir file points at the location of a linked worktree's .git file. Its content must be of the form /path/to/worktree/.git (from which the location of the worktree itself can be derived by stripping the "/.git" suffix). If the gitdir file is deleted or becomes corrupted or outdated, then Git will be unable to find the linked worktree. An easy way for the gitdir file to become outdated is for the user to move the worktree manually (without using "git worktree move"). Although it is possible to manually update the gitdir file to reflect the new linked worktree location, doing so requires a level of knowledge about worktree internals beyond what a user should be expected to know offhand.

Therefore, teach "git worktree repair" how to repair broken or outdated .git/worktrees/<id>/gitdir files automatically. (For this to work, the command must either be invoked from within the worktree whose gitdir file requires repair, or from within the main or any linked worktree by providing the path of the broken worktree as an argument to "git worktree repair".)

---
 Documentation/git-worktree.txt | 14 ++++--
 builtin/worktree.c             |  7 ++-
 t/t2406-worktree-repair.sh     | 86 ++++++++++++++++++++++++++++++++++
 worktree.c                     | 74 +++++++++++++++++++++++++++++
 worktree.h                     | 12 +++++
 5 files changed, 188 insertions(+), 5 deletions(-)
Documentation/git-worktree.txt +11 -3
index 34fe47cecd..f70cda4b36 100644
@@ -15,7 +15,7 @@ SYNOPSIS
 'git worktree move' <worktree> <new-path>
 'git worktree prune' [-n] [-v] [--expire <expire>]
 'git worktree remove' [-f] <worktree>
-'git worktree repair'
+'git worktree repair' [<path>...]
 'git worktree unlock' <worktree>
 
 DESCRIPTION
@@ -114,7 +114,7 @@ and no modification in tracked files) can be removed. Unclean working
 trees or ones with submodules can be removed with `--force`. The main
 working tree cannot be removed.
 
-repair::
+repair [<path>...]::
 
 Repair working tree administrative files, if possible, if they have
 become corrupted or outdated due to external factors.
@@ -123,6 +123,13 @@ For instance, if the main working tree (or bare repository) is moved,
 linked working trees will be unable to locate it. Running `repair` in
 the main working tree will reestablish the connection from linked
 working trees back to the main working tree.
++
+Similarly, if a linked working tree is moved without using `git worktree
+move`, the main working tree (or bare repository) will be unable to
+locate it. Running `repair` within the recently-moved working tree will
+reestablish the connection. If multiple linked working trees are moved,
+running `repair` from any working tree with each tree's new `<path>` as
+an argument, will reestablish the connection to all the specified paths.
 
 unlock::
 
@@ -317,7 +324,8 @@ in the entry's directory. For example, if a linked working tree is moved
 to `/newpath/test-next` and its `.git` file points to
 `/path/main/.git/worktrees/test-next`, then update
 `/path/main/.git/worktrees/test-next/gitdir` to reference `/newpath/test-next`
-instead.
+instead. Better yet, run `git worktree repair` to reestablish the connection
+automatically.
 
 To prevent a `$GIT_DIR/worktrees` entry from being pruned (which
 can be useful in some situations, such as when the
builtin/worktree.c +5 -2
index 68b0032428..8165343145 100644
@@ -1043,15 +1043,18 @@ static void report_repair(int iserr, const char *path, const char *msg, void *cb
 
 static int repair(int ac, const char **av, const char *prefix)
 {
+	const char **p;
+	const char *self[] = { ".", NULL };
 	struct option options[] = {
 		OPT_END()
 	};
 	int rc = 0;
 
 	ac = parse_options(ac, av, prefix, options, worktree_usage, 0);
-	if (ac)
-		usage_with_options(worktree_usage, options);
 	repair_worktrees(report_repair, &rc);
+	p = ac > 0 ? av : self;
+	for (; *p; p++)
+		repair_worktree_at_path(*p, report_repair, &rc);
 	return rc;
 }
 
t/t2406-worktree-repair.sh +86 -0

90 lines not on this page · raw message

worktree.c +74 -0

77 lines not on this page · raw message

worktree.h +12 -0
index 4fcb01348c..ff7b62e434 100644
@@ -100,6 +100,18 @@ typedef void (* worktree_repair_fn)(int iserr, const char *path,
  */
 void repair_worktrees(worktree_repair_fn, void *cb_data);
 
+/*
+ * Repair administrative files corresponding to the worktree at the given path.
+ * The worktree's .git file pointing at the repository must be intact for the
+ * repair to succeed. Useful for re-associating an orphaned worktree with the
+ * repository if the worktree has been moved manually (without using "git
+ * worktree move"). For each repair made or error encountered while attempting
+ * a repair, the callback function, if non-NULL, is called with the path of the
+ * worktree and a description of the repair or error, along with the callback
+ * user-data.
+ */
+void repair_worktree_at_path(const char *, worktree_repair_fn, void *cb_data);
+
 /*
  * Free up the memory for worktree(s)
  */
Signature · 1 line
2.28.0.531.g41c3d8a546
#19 lore raw

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

In reply to Eric Sunshine

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.

---
 builtin/init-db.c |  2 ++
 t/t0001-init.sh   | 11 +++++++++++
 2 files changed, 13 insertions(+)
builtin/init-db.c +2 -0
index bbc9bc78f9..7b915d88ab 100644
@@ -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);
t/t0001-init.sh +11 -0
index 50222a10c5..e489eb4ddb 100755
@@ -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 &&
 	(
Signature · 1 line
2.28.0.531.g41c3d8a546
#20 lore raw

PATCH v2 5/5 init: make --separate-git-dir work from within linked worktree

In reply to Eric Sunshine

The intention of git init --separate-work-dir=<path> is to move the .git/ directory to a location outside of the main worktree. When used within a linked worktree, however, rather than moving the .git/ directory as intended, it instead incorrectly moves the worktree's .git/worktrees/<id> directory to <path>, thus disconnecting the linked worktree from its parent repository and breaking the worktree in the process since its local .git file no longer points at a location at which it can find the object database. Fix this broken behavior.

An intentional side-effect of this change is that it also closes a loophole not caught by ccf236a23a (init: disallow --separate-git-dir with bare repository, 2020-08-09) in which the check to prevent --separate-git-dir being used in conjunction with a bare repository was unable to detect the invalid combination when invoked from within a linked worktree. Therefore, add a test to verify that this loophole is closed, as well.

---
 builtin/init-db.c | 24 ++++++++++++++++++++++++
 t/t0001-init.sh   | 21 +++++++++++++++++++--
 2 files changed, 43 insertions(+), 2 deletions(-)
builtin/init-db.c +24 -0
index 7b915d88ab..cd3e760541 100644
@@ -642,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);
 
t/t0001-init.sh +19 -2
index e489eb4ddb..2f7c3dcd0f 100755
@@ -329,6 +329,15 @@ test_expect_success 'implicit bare & --separate-git-dir incompatible' '
 	test_i18ngrep "incompatible" err
 '
 
+test_expect_success 'bare & --separate-git-dir incompatible within worktree' '
+	test_when_finished "rm -rf bare.git linkwt seprepo" &&
+	test_commit gumby &&
+	git clone --bare . bare.git &&
+	git -C bare.git worktree add --detach ../linkwt &&
+	test_must_fail git -C linkwt init --separate-git-dir seprepo 2>err &&
+	test_i18ngrep "incompatible" err
+'
+
 test_lazy_prereq GETCWD_IGNORES_PERMS '
 	base=GETCWD_TEST_BASE_DIR &&
 	mkdir -p $base/dir &&
@@ -405,15 +414,23 @@ 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' '
+sep_git_dir_worktree ()  {
 	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 "$1" 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 're-init to move gitdir with linked worktrees' '
+	sep_git_dir_worktree mainwt
+'
+
+test_expect_success 're-init to move gitdir within linked worktree' '
+	sep_git_dir_worktree linkwt
 '
 
 test_expect_success MINGW '.git hidden' '
Signature · 1 line
2.28.0.531.g41c3d8a546
#21 lore raw
In reply to Eric Sunshine
Quoted 3 lines · Eric Sunshine

This is a re-roll of [1] which adds a "git worktree repair" command and which fixes bugs with "git init --separate-git-dir" related to worktrees.

These look good to me. Thanks.