From: Junio C Hamano <gitster@pobox.com>
To: Eric Sunshine <ericsunshine@charter.net>
Cc: git@vger.kernel.org,
Russell Stuart <russell+git.vger.kernel.org@stuart.id.au>,
Eric Sunshine <sunshine@sunshineco.com>
Subject: Re: [PATCH] worktree: repair copied repository and linked worktrees
Date: Mon, 23 Sep 2024 14:22:39 -0700 [thread overview]
Message-ID: <xmqqh6a6xdy8.fsf@gitster.g> (raw)
In-Reply-To: <20240923075416.54289-1-ericsunshine@charter.net> (Eric Sunshine's message of "Mon, 23 Sep 2024 03:54:16 -0400")
Eric Sunshine <ericsunshine@charter.net> writes:
> 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.
In other words, before futzing with a repository to point at a
(potentially moved) worktree that points back to it, make sure that
the repository is in a corrupt state---if one of the worktrees we
know correctly points back to us already, that pointer should not be
mucked with, or we would end up spreading the damage further?
It does make sense.
> -If both the main worktree and linked worktrees have been moved manually,
> +If both the main worktree and linked worktrees have been moved or copied manually,
> then running `repair` in the main worktree and specifying the new `<path>`
> of each linked worktree will reestablish all connections in both
> directions.
OK.
> diff --git a/worktree.c b/worktree.c
> index 30a947426e..7d1b60ff9f 100644
> --- a/worktree.c
> +++ b/worktree.c
> @@ -683,6 +683,7 @@ void repair_worktree_at_path(const char *path,
> struct strbuf gitdir = STRBUF_INIT;
> struct strbuf olddotgit = STRBUF_INIT;
> char *backlink = NULL;
> + /*
> + * If we got this far, either the worktree's .git file pointed at a
> + * valid repository (i.e. read_gitfile_gently() returned success) or
> + * the .git file did not point at a repository but we were able to
> + * infer a suitable new value for the .git file by locating a
> + * .git/worktrees/<id> in *this* repository corresponding to the <id>
> + * recorded in the worktree's .git file.
> + *
> + * However, if, at this point, inferred_backlink is non-NULL (i.e. we
> + * found a suitable .git/worktrees/<id> in *this* repository) *and* the
> + * worktree's .git file points at a valid repository *and* those two
> + * paths differ, then that indicates that the user probably *copied*
> + * the main and linked worktrees to a new location as a unit rather
> + * than *moving* them. Thus, the copied worktree's .git file actually
> + * points at the .git/worktrees/<id> in the *original* repository, not
> + * in the "copy" repository. In this case, point the "copy" worktree's
> + * .git file at the "copy" repository.
> + */
Tricky, but cannot be avoided in order to help those who "copy".
> diff --git a/t/t2406-worktree-repair.sh b/t/t2406-worktree-repair.sh
> index edbf502ec5..7686e60f6a 100755
> --- a/t/t2406-worktree-repair.sh
> +++ b/t/t2406-worktree-repair.sh
> @@ -197,4 +197,23 @@ test_expect_success 'repair moved main and linked worktrees' '
> test_cmp expect-gitfile sidemoved/.git
> '
>
> +test_expect_success 'repair copied main and linked worktrees' '
> + test_when_finished "rm -rf orig dup" &&
> + mkdir -p orig &&
> + git -C orig init main &&
> + test_commit -C orig/main nothing &&
> + git -C orig/main worktree add ../linked &&
> + cp orig/main/.git/worktrees/linked/gitdir orig/main.expect &&
> + cp orig/linked/.git orig/linked.expect &&
> + cp -R orig dup &&
> + sed "s,orig/linked/\.git$,dup/linked/.git," orig/main.expect >dup/main.expect &&
> + sed "s,orig/main/\.git/worktrees/linked$,dup/main/.git/worktrees/linked," \
> + orig/linked.expect >dup/linked.expect &&
> + git -C dup/main worktree repair ../linked &&
> + test_cmp orig/main.expect orig/main/.git/worktrees/linked/gitdir &&
> + test_cmp orig/linked.expect orig/linked/.git &&
> + test_cmp dup/main.expect dup/main/.git/worktrees/linked/gitdir &&
> + test_cmp dup/linked.expect dup/linked/.git
> +'
> +
> test_done
next prev parent reply other threads:[~2024-09-23 21:22 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-09-23 7:54 [PATCH] worktree: repair copied repository and linked worktrees Eric Sunshine
2024-09-23 8:04 ` Eric Sunshine
2024-09-23 21:22 ` Junio C Hamano [this message]
-- strict thread matches above, loose matches on Subject: below --
2024-10-08 15:30 Caleb White
2024-10-08 15:53 ` Caleb White
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=xmqqh6a6xdy8.fsf@gitster.g \
--to=gitster@pobox.com \
--cc=ericsunshine@charter.net \
--cc=git@vger.kernel.org \
--cc=russell+git.vger.kernel.org@stuart.id.au \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.