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

Re: [PATCH] worktree: teach find_shared_symref to ignore current worktree

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Jan 19, 2023, 10:48 UTC
Message-ID
<cedc93ec-aa6f-65bf-65be-0dca3d4d0186@dunelm.org.uk>
In-Reply-To
<f209958f-e824-181b-f9d5-6e4bc8e53646@gmail.com>
Hi Rubén
On 18/01/2023 23:50, Rubén Justo wrote:
Show 5 quoted lines
> On 17-ene-2023 15:27:29, Junio C Hamano wrote:
> 
>> being solved. Rather, it is unclear what problem you are solving.
> 
> Sorry, I didn't explain the motivation well in the message.

I'm afraid I'm still not totally clear from this message exactly what the problem is. Having looked at die_if_checked_out() I think it maybe the second of Junio's options

  - If the current branch the the current worktree is checked out in
    a different worktree, we get the current worktree back.
Here is the function:
void die_if_checked_out(const char *branch, int ignore_current_worktree)
{
	struct worktree **worktrees = get_worktrees();
	const struct worktree *wt;
	wt = find_shared_symref(worktrees, "HEAD", branch);
	if (wt && (!ignore_current_worktree || !wt->is_current)) {
		skip_prefix(branch, "refs/heads/", &branch);
		die(_("'%s' is already checked out at '%s'"), branch, wt->path);
	}
	free_worktrees(worktrees);
}

It takes a flag to ignore the current worktree but uses find_shared_symref() which does not have such a flag to see if the branch is checked out. This means that if a branch is checkout out twice find_shared_symref() may return the current worktree rather than the one we're interested in.

If that is the problem you're trying to solve I think it would be better to keep the signature of find_shared_symref() the same and add a helper function that is called by die_if_checked_out() and find_shared_symref() which can optionally ignore the current worktree. The commit message for such a patch should explain you're fixing a bug rather than trying to change the existing behavior.

Best Wishes
Phillip
Show 34 quoted lines
> First, I'm not sure if "ignore_current_worktree" is correct, maybe needs
> to be "prefer_current_worktree".  Having said that, let me use an example.
> 
> With...
> 
> 	$ git worktree add wt1 -b main
> 	$ git worktree add wt2 -f main
> 
> ... we get confuse results:
> 
> 	$ git -C wt1 rebase main main
> 	Current branch main is up to date.
> 	$ git -C wt2 rebase main main
> 	fatal: 'main' is already checked out...
> 
> The problem I'm trying to solve is that find_shared_symref() returns the
> first matching worktree, and a possible matching "current worktree"
> might not be the first matching one.  That's why die_if_checked_out()
> dies with "git -C wt2".
> 
> find_shared_symref() searches through the list of worktrees that
> get_worktrees() composes: first the main worktree and then, as getdir()
> returns them, those in .git/worktrees/*.  The search is sequential and
> once a match is found, it is returned.  And so die_if_checked_out(),
> when asked to "ignore_current_worktree", is going to consider for
> "is_current" the worktree which may or may not be the "current" one,
> depending on the sequence from get_worktree(), and getdir() ultimately.
> 
> If we want to disallow operations on a worktree with a checked out
> branch also on another worktree, we need the "ignore_current_worktree".
> But, and now I'm more in favor of this, if we prefer to allow the
> operation, we need a "prefer_current_worktree", to induce
> find_shared_symref() to return the "current" one if it matches, or any
> other one that matches.
Previous: Rubén JustoNext: Rubén Justo
Message 4 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.