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

Re: [PATCH] refs: unify parse_worktree_ref() and ref_type()

From
Junio C Hamano <gitster@pobox.com>
Date
Sep 12, 2022, 20:13 UTC
Message-ID
<xmqq35cwcpws.fsf@gitster.g>
In-Reply-To
<xmqq1qsge71x.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
Show 22 quoted lines
> Otherwise we fall through with worktree_ref that we have stripped
> main-worktree/ prefix, which means the original input
>
> 	main-worktree/worktrees/foo/blah
>
> is now 
>
> 	worktrees/foo/blah
>
> and the next skip_prefix() will see that it begins with "worktrees/".
> Of course, if the initial input were
>
> 	worktrees/foo/blah
>
> then we wouldn't have skipped main-worktree/ prefix from it, and go
> to the next skip_prefix().  So from here on, we cannot tell which
> case the original input was.
>
> But that is OK.  Asking "give me the ref 'blah' in the worktree 'foo'"
> in the current worktree should yield the same answer to the question
> "give me the ref 'blah' in the worktree 'foo', as if I asked you to
> do so in the main worktree".
This makes me wonder...

I wonder if it makes the resulting code clearer to go fully recursive, unlike the posted code that says "if a recursive call says it is for current, that means it is for main worktree, and otherwise pretend as if the input did not have the prefix".

That is, something like
parse_worktree_ref(...) {
	... prepare name, name_len and ref ...
	if (skip_prefix(worktree_ref, "main-worktree/", &worktree_ref)) {
		parsed = parse_worktree_ref(worktree_ref, name, name_len, ref);
        	switch (parsed) {
		case REF_WORKTREE_CURRENT:
                case REF_WORKTREE_MAIN:
			return REF_WORKTREE_MAIN;
		case REF_WORKTREE_OTHER:
		case REF_WORKTREE_SHARED:
			return parsed;
		}
	}
	if (skip_prefix(worktree_ref, "worktrees/", &worktree_ref)) {
		slash = strchr(worktree_ref, '/');
		parsed = parse_worktree_ref(slash + 1, name, name_len, ref);
        	switch (parsed) {
		case REF_WORKTREE_CURRENT:
			return WORKTREE_OTHER; /* iffy */
                case REF_WORKTREE_MAIN:
			return REF_WORKTREE_MAIN;
		case REF_WORKTREE_OTHER:
		case REF_WORKTREE_SHARED:
			return parsed;
		}
	}
	/* Otherwise the input is like HEAD, MERGE_HEAD, refs/$BLAH */
	... do whatever for these trivial cases ...
}

And while composing this follow-up, I found another thing that is iffy. When you ask for "worktrees/foo/refs/bisect/good", the recursive call in "worktrees/foo" for the remainder says "current", and the posted patch and the above pseudocode would take CURRENT to mean "that other worktree's", and turns it into OTHER i.e. "not ours". But the code does so without even checking what our worktree is called. What happens if we are the owner of the "worktrees/foo" namespace? The same thing for "The main worktree says the given ref is 'current', so we can turn that into 'main'"---if our worktree is 'main', it is also 'current' and does not have to be turned into 'main' (even though it does not hurt).

Previous: Junio C HamanoNext: Junio C Hamano
Message 3 of 9 in “refs: unify parse_worktree_ref() and ref_type()”
  1. refs: unify parse_worktree_ref() and ref_type()Han-Wen Nienhuys via GitGitGadget, Sep 12, 2022
  2. Junio C HamanoSep 12, 2022
  3. Junio C HamanoSep 12, 2022
  4. Junio C HamanoSep 13, 2022
  5. Han-Wen NienhuysSep 19, 2022
  6. Junio C HamanoSep 19, 2022
  7. Han-Wen NienhuysSep 20, 2022
  8. Junio C HamanoSep 21, 2022
  9. refs: unify parse_worktree_ref() and ref_type()Han-Wen Nienhuys via GitGitGadget, Sep 19, 2022

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.