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

Re: [PATCH v3 4/4] switch: reject if the branch is already checked out elsewhere (test)

From
Eric Sunshine <sunshine@sunshineco.com>
Date
Feb 15, 2023, 04:17 UTC
Message-ID
<CAPig+cQpizjmhmDKb=HPrcYqqRq7JpvC-NZvY7B9eBbG+NrfKw@mail.gmail.com>
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:
Show 20 quoted lines
> 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 &&
Show 20 quoted lines
> +       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
> +'
Previous: Rubén JustoNext: Rubén Justo
Message 32 of 40 in “worktree: teach find_shared_symref to ignore current worktree”
  1. worktree: teach find_shared_symref to ignore current worktreeRubén Justo, Jan 17, 2023
  2. Junio C HamanoJan 17, 2023
  3. Rubén JustoJan 18, 2023
  4. Phillip WoodJan 19, 2023
  5. Rubén JustoJan 19, 2023
  6. 0/3 fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Jan 22, 2023
  7. 1/3 branch: fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Jan 22, 2023
  8. Junio C HamanoJan 22, 2023
  9. Rubén JustoJan 22, 2023
  10. Junio C HamanoJan 22, 2023
  11. Rubén JustoJan 22, 2023
  12. Phillip WoodJan 24, 2023
  13. Rubén JustoJan 26, 2023
  14. 2/3 rebase: refuse to switch to a branch already checked out elsewhere (test)Rubén Justo, Jan 22, 2023
  15. 3/3 switch: reject if the branch is already checked out elsewhere (test)Rubén Justo, Jan 22, 2023
  16. 0/4 fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Feb 4, 2023
  17. 1/4 worktree: introduce is_shared_symref()Rubén Justo, Feb 4, 2023
  18. Phillip WoodFeb 7, 2023
  19. 2/4 branch: fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Feb 4, 2023
  20. Ævar Arnfjörð BjarmasonFeb 6, 2023
  21. Rubén JustoFeb 6, 2023
  22. Phillip WoodFeb 7, 2023
  23. Ævar Arnfjörð BjarmasonFeb 7, 2023
  24. 3/4 rebase: refuse to switch to a branch already checked out elsewhere (test)Rubén Justo, Feb 4, 2023
  25. Ævar Arnfjörð BjarmasonFeb 6, 2023
  26. Rubén JustoFeb 6, 2023
  27. Phillip WoodFeb 7, 2023
  28. Rubén JustoFeb 8, 2023
  29. Junio C HamanoFeb 8, 2023
  30. Rubén JustoFeb 8, 2023
  31. 4/4 switch: reject if the branch is already checked out elsewhere (test)Rubén Justo, Feb 4, 2023
  32. Eric SunshineFeb 15, 2023
  33. Rubén JustoFeb 15, 2023
  34. 0/4 fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Feb 25, 2023
  35. 1/4 worktree: introduce is_shared_symref()Rubén Justo, Feb 25, 2023
  36. 2/4 branch: fix die_if_checked_out() when ignore_current_worktreeRubén Justo, Feb 25, 2023
  37. 3/4 rebase: refuse to switch to a branch already checked out elsewhere (test)Rubén Justo, Feb 25, 2023
  38. 4/4 switch: reject if the branch is already checked out elsewhere (test)Rubén Justo, Feb 25, 2023
  39. Junio C HamanoFeb 25, 2023
  40. Rubén JustoFeb 27, 2023

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

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