worktree: repair copied repository and linked worktrees

For each linked worktree, Git maintains two pointers: (1) <repo>/worktrees/<id>/gitdir which points at the linked worktree, and (2) <worktree>/.git which points back at <repo>/worktrees/<id>. Both pointers are absolute pathnames. Aside from manually manipulating those raw files, it is possible to easily "break" one or both pointers by ignoring the "git worktree move" command and instead manually moving a linked worktree, moving the repository, or moving both. The "git worktree repair" command was invented to handle this case by restoring these pointers to sane values. For the "repair" command, the "git worktree" manual page states: Repair worktree administrative files, if possible, if they have become corrupted or outdated due to external factors. The "if possible" clause was chosen deliberately to convey that the existing implementation may not be able to fix every possible breakage, and to imply that improvements may be made to handle other types of breakage. A recent problem report[*] illustrates a case in which "git worktree repair" not only fails to fix breakage, but actually causes breakage. Specifically, if a repository / main-worktree and linked worktrees are *copied* as a unit (rather than *moved*), then "git worktree repair" run in the copy leaves the copy untouched but botches the pointers in the original repository and the original worktrees. For instance, given this directory structure: orig/ main/ (main-worktree) linked/ (linked worktree) if "orig" is copied (not moved) to "dup", then immediately after the manual copy operation: * orig/main/.git/worktrees/linked/gitdir points at orig/linked/.git * orig/linked/.git points at orig/main/.git/worktrees/linked * dup/main/.git/worktrees/linked/gitdir points at orig/linked/.git * dup/linked/.git points at orig/main/.git/worktrees/linked So, dup/main thinks its linked worktree is orig/linked, and worktree dup/linked thinks its repository / main-worktree is orig/main. "git worktree repair" is reasonably simple-minded; it wants to trust valid-looking pointers, hence doesn't try to second-guess them. In this case, when validating dup/linked/.git, it finds a legitimate repository pointer, orig/main/.git/worktrees/linked, thus trusts that is correct, but does notice that gitdir in that directory doesn't point at dup/linked/.git, so it (incorrectly) _fixes_ orig/main/.git/worktrees/linked/gitdir to point at dup/linked/.git. Similarly, when validating dup/main/.git/worktrees/linked/gitdir, it finds a legitimate worktree pointer, orig/linked/.git, but notices that its .git file doesn't point back at dup/main, thus (incorrectly) _fixes_ orig/linked/.git to point at dup/main/.git/worktrees/linked. Hence, it has modified and broken the linkage between orig/main and orig/linked rather than fixing dup/main and dup/linked as expected. Fix this problem by also checking if a plausible .git/worktrees/<id> exists in the *current* repository -- not just in the repository pointed at by the worktree's .git file -- and comparing whether they are the same. If not, then it is likely because the repository / main-worktree and linked worktrees were copied, so prefer the discovered plausible pointer rather than the one from the existing .git file. [*]: https://lore.kernel.org/git/E1sr5iF-0007zV-2k@binarylane-bailey.stuart.id.au/ Reported-by: Russell Stuart <russell+git.vger.kernel.org@stuart.id.au> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Sunshine committed Sep 23, 2024 at 03:54 UTC 992f7a4fdbadda9f30ff3a8995e966f0562bc73a
3 files changed +59 -2
Documentation/git-worktree.txt
+1 -1
@@ -157,7 +157,7 @@ will reestablish the connection. If multiple linked worktrees are moved,
157 running `repair` from any worktree with each tree's new `<path>` as an
158 argument, will reestablish the connection to all the specified paths.
159 +
160 -If both the main worktree and linked worktrees have been moved manually,
160 +If both the main worktree and linked worktrees have been moved or copied manually,
161 then running `repair` in the main worktree and specifying the new `<path>`
162 of each linked worktree will reestablish all connections in both
163 directions.
t/t2406-worktree-repair.sh
+19
@@ -197,4 +197,23 @@ test_expect_success 'repair moved main and linked worktrees' '
197 test_cmp expect-gitfile sidemoved/.git
198 '
199
200 +test_expect_success 'repair copied main and linked worktrees' '
201 + test_when_finished "rm -rf orig dup" &&
202 + mkdir -p orig &&
203 + git -C orig init main &&
204 + test_commit -C orig/main nothing &&
205 + git -C orig/main worktree add ../linked &&
206 + cp orig/main/.git/worktrees/linked/gitdir orig/main.expect &&
207 + cp orig/linked/.git orig/linked.expect &&
208 + cp -R orig dup &&
209 + sed "s,orig/linked/\.git$,dup/linked/.git," orig/main.expect >dup/main.expect &&
210 + sed "s,orig/main/\.git/worktrees/linked$,dup/main/.git/worktrees/linked," \
211 + orig/linked.expect >dup/linked.expect &&
212 + git -C dup/main worktree repair ../linked &&
213 + test_cmp orig/main.expect orig/main/.git/worktrees/linked/gitdir &&
214 + test_cmp orig/linked.expect orig/linked/.git &&
215 + test_cmp dup/main.expect dup/main/.git/worktrees/linked/gitdir &&
216 + test_cmp dup/linked.expect dup/linked/.git
217 +'
218 +
219 test_done
worktree.c
+39 -1
@@ -683,6 +683,7 @@ void repair_worktree_at_path(const char *path,
683 struct strbuf gitdir = STRBUF_INIT;
684 struct strbuf olddotgit = STRBUF_INIT;
685 char *backlink = NULL;
686 + char *inferred_backlink = NULL;
687 const char *repair = NULL;
688 int err;
689
@@ -698,12 +699,24 @@ void repair_worktree_at_path(const char *path,
699 goto done;
700 }
701
702 + inferred_backlink = infer_backlink(realdotgit.buf);
703 backlink = xstrdup_or_null(read_gitfile_gently(realdotgit.buf, &err));
704 if (err == READ_GITFILE_ERR_NOT_A_FILE) {
705 fn(1, realdotgit.buf, _("unable to locate repository; .git is not a file"), cb_data);
706 goto done;
707 } else if (err == READ_GITFILE_ERR_NOT_A_REPO) {
706 - if (!(backlink = infer_backlink(realdotgit.buf))) {
708 + if (inferred_backlink) {
709 + /*
710 + * Worktree's .git file does not point at a repository
711 + * but we found a .git/worktrees/<id> in this
712 + * repository with the same <id> as recorded in the
713 + * worktree's .git file so make the worktree point at
714 + * the discovered .git/worktrees/<id>. (Note: backlink
715 + * is already NULL, so no need to free it first.)
716 + */
717 + backlink = inferred_backlink;
718 + inferred_backlink = NULL;
719 + } else {
720 fn(1, realdotgit.buf, _("unable to locate repository; .git file does not reference a repository"), cb_data);
721 goto done;
722 }
@@ -712,6 +725,30 @@ void repair_worktree_at_path(const char *path,
725 goto done;
726 }
727
728 + /*
729 + * If we got this far, either the worktree's .git file pointed at a
730 + * valid repository (i.e. read_gitfile_gently() returned success) or
731 + * the .git file did not point at a repository but we were able to
732 + * infer a suitable new value for the .git file by locating a
733 + * .git/worktrees/<id> in *this* repository corresponding to the <id>
734 + * recorded in the worktree's .git file.
735 + *
736 + * However, if, at this point, inferred_backlink is non-NULL (i.e. we
737 + * found a suitable .git/worktrees/<id> in *this* repository) *and* the
738 + * worktree's .git file points at a valid repository *and* those two
739 + * paths differ, then that indicates that the user probably *copied*
740 + * the main and linked worktrees to a new location as a unit rather
741 + * than *moving* them. Thus, the copied worktree's .git file actually
742 + * points at the .git/worktrees/<id> in the *original* repository, not
743 + * in the "copy" repository. In this case, point the "copy" worktree's
744 + * .git file at the "copy" repository.
745 + */
746 + if (inferred_backlink && fspathcmp(backlink, inferred_backlink)) {
747 + free(backlink);
748 + backlink = inferred_backlink;
749 + inferred_backlink = NULL;
750 + }
751 +
752 strbuf_addf(&gitdir, "%s/gitdir", backlink);
753 if (strbuf_read_file(&olddotgit, gitdir.buf, 0) < 0)
754 repair = _("gitdir unreadable");
@@ -727,6 +764,7 @@ void repair_worktree_at_path(const char *path,
764 }
765 done:
766 free(backlink);
767 + free(inferred_backlink);
768 strbuf_release(&olddotgit);
769 strbuf_release(&gitdir);
770 strbuf_release(&realdotgit);