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

Re: [PATCH v5 5/6] worktree: teach "add" to check out existing branches

From
Thomas Gummerer <t.gummerer@gmail.com>
Date
Mar 30, 2018, 14:04 UTC
Message-ID
<20180330140415.GC2629@hank>
In-Reply-To
<CAPig+cTmGvkGkcdH45mRO6uwEK2UyR1FQ_fiD=2wgz8Pt_WeOg@mail.gmail.com>
On 03/27, Eric Sunshine wrote:
Show 66 quoted lines
> On Sun, Mar 25, 2018 at 9:49 AM, Thomas Gummerer <t.gummerer@gmail.com> wrote:
> > Currently 'git worktree add <path>' creates a new branch named after the
> > basename of the path by default.  If a branch with that name already
> > exists, the command refuses to do anything, unless the '--force' option
> > is given.
> >
> > However we can do a little better than that, and check the branch out if
> > it is not checked out anywhere else.  This will help users who just want
> > to check an existing branch out into a new worktree, and save a few
> > keystrokes.
> > [...]
> > Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
> > ---
> > diff --git a/builtin/worktree.c b/builtin/worktree.c
> > @@ -317,7 +318,10 @@ static int add_worktree(const char *path, const char *refname,
> > -       if (opts->new_branch)
> > +       if (opts->checkout_existing_branch)
> > +               fprintf_ln(stderr, _("checking out branch '%s'"),
> > +                          refname);
> 
> This fprintf_ln() can easily fit on one line; no need to wrap
> 'refname' to the next one.
> 
> Not worth a re-roll, though.
> 
> > +       else if (opts->new_branch)
> >                 fprintf_ln(stderr, _("creating branch '%s'"), opts->new_branch);
> > diff --git a/t/t2025-worktree-add.sh b/t/t2025-worktree-add.sh
> > @@ -198,13 +198,26 @@ test_expect_success '"add" with <branch> omitted' '
> > -test_expect_success '"add" auto-vivify does not clobber existing branch' '
> > +test_expect_success '"add" checks out existing branch of dwimd name' '
> >         test_commit c1 &&
> >         test_commit c2 &&
> >         git branch precious HEAD~1 &&
> > -       test_must_fail git worktree add precious &&
> > +       git worktree add precious &&
> >         test_cmp_rev HEAD~1 precious &&
> > -       test_path_is_missing precious
> > +       (
> > +               cd precious &&
> > +               test_cmp_rev precious HEAD
> > +       )
> > +'
> 
> Looking at this more closely, once it stops being a "don't clobber
> precious branch" test, it's doing more than it needs to do, thus is
> confusing for future readers. The setup -- creating two commits and
> making "precious" point one commit back -- was very intentional and
> allowed the test to verify not only that the worktree wasn't created
> but that "precious" didn't change to reference a different commit.
> However, _none_ of this matters once it becomes a dwim'ing test; the
> unnecessary code is confusing since it doesn't make sense in the
> context of dwim'ing. I _think_ the entire test can collapse to:
> 
>     git branch existing &&
>     git worktree add existing &&
>     (
>         cd existing &&
>         test_cmp_rev existing HEAD
>     )
> 
> In other words, this patch should drop the "precious" test altogether
> and just introduce a new dwim'ing test (and drop patch 6/6).
> 
> I do think that with the potential confusion to future readers, this
> does deserve a re-roll.

Hmm I do think that it's important to have the branch we create the new working tree from different from the branch of the main working tree, to make sure we don't accidentally overwrite an existing branch, and to show that the new branch is checked out.

Maybe I'm overly paranoid though?
Show 9 quoted lines
> > +test_expect_success '"add" auto-vivify fails with checked out branch' '
> > +       git checkout -b test-branch &&
> > +       test_must_fail git worktree add test-branch &&
> > +       test_path_is_missing test-branch
> > +'
> > +
> > +test_expect_success '"add --force" with existing dwimd name doesnt die' '
> > +       git worktree add --force test-branch
> >  '
Previous: Eric SunshineNext: Thomas Gummerer
Message 53 of 112 in “worktree: teach "add" to check out existing branches”
  1. worktree: teach "add" to check out existing branchesThomas Gummerer, Jan 21, 2018
  2. Robert P. J. DayJan 21, 2018
  3. Duy NguyenJan 22, 2018
  4. Thomas GummererJan 22, 2018
  5. 0/3 worktree: teach "add" to check out existing branchesThomas Gummerer, Feb 4, 2018
  6. 1/3 worktree: improve message when creating a new worktreeThomas Gummerer, Feb 4, 2018
  7. Duy NguyenFeb 5, 2018
  8. Thomas GummererFeb 5, 2018
  9. Junio C HamanoFeb 5, 2018
  10. Eric SunshineFeb 7, 2018
  11. Thomas GummererFeb 9, 2018
  12. Duy NguyenFeb 9, 2018
  13. Duy NguyenFeb 10, 2018
  14. 2/3 worktree: be clearer when "add" dwim-ery kicks inThomas Gummerer, Feb 4, 2018
  15. 3/3 worktree: teach "add" to check out existing branchesThomas Gummerer, Feb 4, 2018
  16. Duy NguyenFeb 5, 2018
  17. Junio C HamanoFeb 5, 2018
  18. Thomas GummererFeb 5, 2018
  19. Duy NguyenFeb 6, 2018
  20. Thomas GummererFeb 9, 2018
  21. 0/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 17, 2018
  22. 1/4 worktree: improve message when creating a new worktreeThomas Gummerer, Mar 17, 2018
  23. 2/4 worktree: be clearer when "add" dwim-ery kicks inThomas Gummerer, Mar 17, 2018
  24. 3/4 worktree: factor out dwim_branch functionThomas Gummerer, Mar 17, 2018
  25. 4/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 17, 2018
  26. 0/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 17, 2018
  27. 1/4 worktree: improve message when creating a new worktreeThomas Gummerer, Mar 17, 2018
  28. Duy NguyenMar 19, 2018
  29. Junio C HamanoMar 19, 2018
  30. Eric SunshineMar 20, 2018
  31. Thomas GummererMar 24, 2018
  32. 3/4 worktree: factor out dwim_branch functionThomas Gummerer, Mar 17, 2018
  33. 2/4 worktree: be clearer when "add" dwim-ery kicks inThomas Gummerer, Mar 17, 2018
  34. Eric SunshineMar 20, 2018
  35. Eric SunshineMar 20, 2018
  36. Eric SunshineMar 20, 2018
  37. Thomas GummererMar 24, 2018
  38. 4/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 17, 2018
  39. Eric SunshineMar 20, 2018
  40. Thomas GummererMar 24, 2018
  41. 0/6 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 25, 2018
  42. 1/6 worktree: improve message when creating a new worktreeThomas Gummerer, Mar 25, 2018
  43. 2/6 worktree: be clearer when "add" dwim-ery kicks inThomas Gummerer, Mar 25, 2018
  44. Eric SunshineMar 27, 2018
  45. Thomas GummererMar 30, 2018
  46. 3/6 worktree: remove force_new_branch from struct add_optsThomas Gummerer, Mar 25, 2018
  47. Eric SunshineMar 27, 2018
  48. Thomas GummererMar 30, 2018
  49. 4/6 worktree: factor out dwim_branch functionThomas Gummerer, Mar 25, 2018
  50. Eric SunshineMar 27, 2018
  51. 5/6 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 25, 2018
  52. Eric SunshineMar 27, 2018
  53. Thomas GummererMar 30, 2018
  54. 6/6 t2025: rename now outdated branch nameThomas Gummerer, Mar 25, 2018
  55. Eric SunshineMar 27, 2018
  56. Thomas GummererMar 30, 2018
  57. 0/6 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 31, 2018
  58. 1/6 worktree: remove extra members from struct add_optsThomas Gummerer, Mar 31, 2018
  59. 3/6 worktree: improve message when creating a new worktreeThomas Gummerer, Mar 31, 2018
  60. Eric SunshineApr 8, 2018
  61. 2/6 reset: introduce show-new-head-line optionThomas Gummerer, Mar 31, 2018
  62. Junio C HamanoApr 2, 2018
  63. Thomas GummererApr 2, 2018
  64. Thomas GummererApr 2, 2018
  65. Junio C HamanoApr 2, 2018
  66. Thomas GummererApr 2, 2018
  67. 6/6 worktree: teach "add" to check out existing branchesThomas Gummerer, Mar 31, 2018
  68. 5/6 fixup! worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 1, 2018
  69. Eric SunshineApr 9, 2018
  70. Thomas GummererApr 9, 2018
  71. Eric SunshineApr 9, 2018
  72. Eric SunshineApr 8, 2018
  73. Thomas GummererApr 8, 2018
  74. 4/6 worktree: be clearer when "add" dwim-ery kicks inThomas Gummerer, Mar 31, 2018
  75. 5/6 worktree: factor out dwim_branch functionThomas Gummerer, Mar 31, 2018
  76. Eric SunshineApr 8, 2018
  77. Thomas GummererApr 8, 2018
  78. Eric SunshineApr 9, 2018
  79. Thomas GummererApr 9, 2018
  80. Thomas GummererApr 9, 2018
  81. Eric SunshineApr 9, 2018
  82. Thomas GummererApr 11, 2018
  83. Eric SunshineApr 11, 2018
  84. Thomas GummererApr 11, 2018
  85. Eric SunshineApr 11, 2018
  86. 0/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 15, 2018
  87. 1/4 worktree: remove extra members from struct add_optsThomas Gummerer, Apr 15, 2018
  88. 3/4 worktree: factor out dwim_branch functionThomas Gummerer, Apr 15, 2018
  89. 2/4 worktree: improve message when creating a new worktreeThomas Gummerer, Apr 15, 2018
  90. Junio C HamanoApr 16, 2018
  91. Thomas GummererApr 23, 2018
  92. Eric SunshineApr 23, 2018
  93. Thomas GummererApr 23, 2018
  94. 4/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 15, 2018
  95. Eric SunshineApr 23, 2018
  96. 0/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 23, 2018
  97. 1/4 worktree: remove extra members from struct add_optsThomas Gummerer, Apr 23, 2018
  98. Eric SunshineApr 24, 2018
  99. 2/4 worktree: improve message when creating a new worktreeThomas Gummerer, Apr 23, 2018
  100. Eric SunshineApr 24, 2018
  101. 3/4 worktree: factor out dwim_branch functionThomas Gummerer, Apr 23, 2018
  102. 4/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 23, 2018
  103. Eric SunshineApr 24, 2018
  104. 0/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 24, 2018
  105. 1/4 worktree: remove extra members from struct add_optsThomas Gummerer, Apr 24, 2018
  106. 2/4 worktree: improve message when creating a new worktreeThomas Gummerer, Apr 24, 2018
  107. 3/4 worktree: factor out dwim_branch functionThomas Gummerer, Apr 24, 2018
  108. 4/4 worktree: teach "add" to check out existing branchesThomas Gummerer, Apr 24, 2018
  109. Eric SunshineApr 27, 2018
  110. Thomas GummererApr 28, 2018
  111. Junio C HamanoApr 30, 2018
  112. Junio C HamanoMar 18, 2018

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.