From: Eric Sunshine <sunshine@sunshineco.com>
To: "Rubén Justo" <rjusto@gmail.com>
Cc: Git List <git@vger.kernel.org>,
Junio C Hamano <gitster@pobox.com>,
Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: Re: [PATCH v3 4/4] switch: reject if the branch is already checked out elsewhere (test)
Date: Tue, 14 Feb 2023 23:17:42 -0500 [thread overview]
Message-ID: <CAPig+cQpizjmhmDKb=HPrcYqqRq7JpvC-NZvY7B9eBbG+NrfKw@mail.gmail.com> (raw)
In-Reply-To: <b18a5710-2791-f892-8460-dda7ea41d88a@gmail.com>
On Sat, Feb 4, 2023 at 6:33 PM Rubén Justo <rjusto@gmail.com> wrote:
> Since 5883034 (checkout: reject if the branch is already checked out
> elsewhere) in normal use, we do not allow multiple worktrees having the
> same checked out branch.
>
> A bug has recently been fixed that caused this to not work as expected.
>
> Let's add a test to notice if this changes in the future.
>
> Signed-off-by: Rubén Justo <rjusto@gmail.com>
> ---
> diff --git a/t/t2060-switch.sh b/t/t2060-switch.sh
> @@ -146,4 +146,33 @@ test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
> +test_expect_success 'switch back when temporarily detached and checked out elsewhere ' '
> + test_when_finished "
> + git worktree remove wt1 &&
> + git worktree remove wt2 &&
> + git branch -d shared
> + git checkout -
> + " &&
> + git checkout -b shared &&
A few comments...
The test_when_finished() call has an odd mix of &&-chain and missing &&-chain.
If you do use &&-chaining inside test_when_finished(), you have to be
careful that _none_ of the cleanup commands fail, otherwise
test_when_finished() itself will fail, making the entire test fail,
_even if_ the test body succeeded. So, &&-chaining the commands in
this test_when_finished() is undesirable since any of the `git
worktree remove` and `git branch -d` commands could potentially fail.
Better would be to drop the &&-chain and ensure that the final command
succeeds.
It may be a good idea to use `||:` at the end of each command as
documentation for readers that any of the cleanup commands might fail,
which could happen if something in the body of the test fails.
The `git branch -d shared` cleanup command fails unconditionally
because it's still the checked-out branch in the directory. You need
to swap the `git branch -d shared` and `git checkout -` commands.
Taking all the above points into account, a possible rewrite would be:
test_when_finished "
git worktree remove wt1 ||:
git worktree remove wt2 ||:
git checkout - ||:
git branch -d shared ||:
" &&
git checkout -b shared &&
> + test_commit shared-first &&
> + HASH1=$(git rev-parse --verify HEAD) &&
> + test_commit shared-second &&
> + test_commit shared-third &&
> + HASH2=$(git rev-parse --verify HEAD) &&
> + git worktree add wt1 -f shared &&
> + git -C wt1 bisect start &&
> + git -C wt1 bisect good $HASH1 &&
> + git -C wt1 bisect bad $HASH2 &&
> + git worktree add wt2 -f shared &&
> + git -C wt2 bisect start &&
> + git -C wt2 bisect good $HASH1 &&
> + git -C wt2 bisect bad $HASH2 &&
> + # we test in both worktrees to ensure that works
> + # as expected with "first" and "next" worktrees
> + test_must_fail git -C wt1 switch shared &&
> + git -C wt1 switch --ignore-other-worktrees shared &&
> + test_must_fail git -C wt2 switch shared &&
> + git -C wt2 switch --ignore-other-worktrees shared
> +'
next prev parent reply other threads:[~2023-02-15 4:20 UTC|newest]
Thread overview: 40+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-17 0:36 [PATCH] worktree: teach find_shared_symref to ignore current worktree Rubén Justo
2023-01-17 23:27 ` Junio C Hamano
2023-01-18 23:50 ` Rubén Justo
2023-01-19 10:48 ` Phillip Wood
2023-01-19 23:18 ` Rubén Justo
2023-01-22 1:20 ` [PATCH v2 0/3] fix die_if_checked_out() when ignore_current_worktree Rubén Justo
2023-01-22 1:23 ` [PATCH v2 1/3] branch: " Rubén Justo
2023-01-22 1:50 ` Junio C Hamano
2023-01-22 11:51 ` Rubén Justo
2023-01-22 19:58 ` Junio C Hamano
2023-01-22 23:21 ` Rubén Justo
2023-01-24 10:35 ` Phillip Wood
2023-01-26 3:07 ` Rubén Justo
2023-01-22 1:28 ` [PATCH v2 2/3] rebase: refuse to switch to a branch already checked out elsewhere (test) Rubén Justo
2023-01-22 1:28 ` [PATCH v2 3/3] switch: reject if the branch is " Rubén Justo
2023-02-04 23:19 ` [PATCH v3 0/4] fix die_if_checked_out() when ignore_current_worktree Rubén Justo
2023-02-04 23:25 ` [PATCH v3 1/4] worktree: introduce is_shared_symref() Rubén Justo
2023-02-07 10:44 ` Phillip Wood
2023-02-04 23:25 ` [PATCH v3 2/4] branch: fix die_if_checked_out() when ignore_current_worktree Rubén Justo
2023-02-06 16:56 ` Ævar Arnfjörð Bjarmason
2023-02-06 23:09 ` Rubén Justo
2023-02-07 10:50 ` Phillip Wood
2023-02-07 12:58 ` Ævar Arnfjörð Bjarmason
2023-02-04 23:26 ` [PATCH v3 3/4] rebase: refuse to switch to a branch already checked out elsewhere (test) Rubén Justo
2023-02-06 16:59 ` Ævar Arnfjörð Bjarmason
2023-02-06 23:16 ` Rubén Justo
2023-02-07 10:52 ` Phillip Wood
2023-02-08 0:43 ` Rubén Justo
2023-02-08 5:19 ` Junio C Hamano
2023-02-08 22:09 ` Rubén Justo
2023-02-04 23:26 ` [PATCH v3 4/4] switch: reject if the branch is " Rubén Justo
2023-02-15 4:17 ` Eric Sunshine [this message]
2023-02-15 22:17 ` Rubén Justo
2023-02-25 14:14 ` [PATCH v4 0/4] fix die_if_checked_out() when ignore_current_worktree Rubén Justo
2023-02-25 14:21 ` [PATCH v4 1/4] worktree: introduce is_shared_symref() Rubén Justo
2023-02-25 14:22 ` [PATCH v4 2/4] branch: fix die_if_checked_out() when ignore_current_worktree Rubén Justo
2023-02-25 14:22 ` [PATCH v4 3/4] rebase: refuse to switch to a branch already checked out elsewhere (test) Rubén Justo
2023-02-25 14:22 ` [PATCH v4 4/4] switch: reject if the branch is " Rubén Justo
2023-02-25 22:50 ` [PATCH v4 0/4] fix die_if_checked_out() when ignore_current_worktree Junio C Hamano
2023-02-27 0:00 ` Rubén Justo
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
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAPig+cQpizjmhmDKb=HPrcYqqRq7JpvC-NZvY7B9eBbG+NrfKw@mail.gmail.com' \
--to=sunshine@sunshineco.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=phillip.wood@dunelm.org.uk \
--cc=rjusto@gmail.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.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).