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

Re: [PATCH v2 3/3] ref-filter: populate symref from iterator

From
John Cai <johncai86@gmail.com>
Date
Aug 6, 2024, 19:49 UTC
Message-ID
<A71B6F98-820D-4692-9712-C6D63F019DCE@gmail.com>
In-Reply-To
<xmqqsevouqsj.fsf@gitster.g>
Hi Junio,
On 1 Aug 2024, at 12:54, Junio C Hamano wrote:
Show 36 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
>
>>> @@ -2852,6 +2852,8 @@ static struct ref_array_item *apply_ref_filter(const char *refname, const struct
>>>  	ref->commit = commit;
>>>  	ref->flag = flag;
>>>  	ref->kind = kind;
>>> +	if (flag & REF_ISSYMREF)
>>> +		ref->symref = xstrdup_or_null(referent);
>>>
>>>  	return ref;
>>>  }
>>
>> What is curious is that we do not lose any code from
>> populate_value() with this change.
>>
>> Is that because of this piece of code near the beginning of it?
>>
>> 	CALLOC_ARRAY(ref->value, used_atom_cnt);
>>
>> 	if (need_symref && (ref->flag & REF_ISSYMREF) && !ref->symref) {
>> 		ref->symref = refs_resolve_refdup(get_main_ref_store(the_repository),
>> 						  ref->refname,
>> 						  RESOLVE_REF_READING,
>> 						  NULL, NULL);
>> 		if (!ref->symref)
>> 			ref->symref = xstrdup("");
>> 	}
>>
>> That is, if we somehow know the value of ref->symref for a ref that
>> is known to be a symbolic ref (and when we know we need symref
>> information in the output), we do not bother calling refs_resolve
>> here to obtain the value.
>
> I forgot to ask the real question.  With your change in place, does
> this "lazily fill ref->symref if it hasn't been discovered yet" code
> still trigger?  Under what condition?  Or is this now a dead code?

Yes that's a good question. I took a look and it seems like in *most* cases by the time populate_value() is called, apply_ref_filter() has already been called that populates the symref member of ref_array_item.

populate_value() gets called by get_ref_atom_value() which gets called by both
1. format_ref_array_item()
2. cmp_ref_sorting()

In the case of [2], the callchain starts with filter_and_format_refs() which calls ref_array_sort() that eventually calls populate_value(). Before ref_array_sort() is called, filter_refs() is called which ends up calling do_filter_refs() with filter_one(), leading to apply_ref_filter().

In the case of [1] however, there are a couple of code paths that call populate_value() without apply_ref_filter() ever being called.

pretty_print_ref() directly calls format_ref_array_item() -> get_ref_atom_value() -> populate_value(). However, apply_ref_filter() is not called which means the symref will not be populated.

Looking through the codebase, this function is only called in builtin/tag.c and bulitin/verify-tag.c in the `git tag -v` codepath. So it seems that if we got rid of this block of code in populate_value(), only in the case where `git tag -v --format='%(symref)'` on a symbolic ref pointing to a tag would the symref be missing.

But I don't even know if this is possible. I tried this locally and got the error:

$ git tag -a -s -m "version 1" v1 refs/heads/master $ git symbolic-ref refs/tags/symbolic-v1 refs/tags/v1 $ git tag -v --format='(%symref)'

error: tag 'refs/tags/symbolic-v1' not found.

So practically speaking, I think we are safe to remove. However, from a future-proof point of view, anyone in the future who calls pretty_print_ref() would also need to be sure to populate the symref member in ref_array_item.

So perhaps from a code durability standpoint we should keep that block?
>
> Thanks.
Previous: Junio C HamanoNext: Junio C Hamano
Message 30 of 44 in “keep track of unresolved value of symbolic-ref in ref iterators”
  1. 0/4 keep track of unresolved value of symbolic-ref in ref iteratorsJohn Cai via GitGitGadget, Jun 6, 2024
  2. 2/4 refs: keep track of unresolved reference value in iteratorsJohn Cai via GitGitGadget, Jun 6, 2024
  3. Jeff KingJun 11, 2024
  4. 1/4 refs: add referent parameter to refs_resolve_ref_unsafeJohn Cai via GitGitGadget, Jun 6, 2024
  5. Junio C HamanoJun 6, 2024
  6. Junio C HamanoJun 6, 2024
  7. John CaiJun 7, 2024
  8. Junio C HamanoJun 7, 2024
  9. Patrick SteinhardtJun 10, 2024
  10. Junio C HamanoJun 10, 2024
  11. John CaiJun 6, 2024
  12. Junio C HamanoJun 6, 2024
  13. Kristoffer HaugsbakkJun 28, 2024
  14. Junio C HamanoJun 28, 2024
  15. Linus ArverJun 30, 2024
  16. Junio C HamanoJun 30, 2024
  17. Jeff KingJun 11, 2024
  18. John CaiJul 30, 2024
  19. 4/4 ref-filter: populate symref from iteratorJohn Cai via GitGitGadget, Jun 6, 2024
  20. 3/4 refs: add referent to each_ref_fnJohn Cai via GitGitGadget, Jun 6, 2024
  21. 0/3 keep track of unresolved value of symbolic-ref in ref iteratorsJohn Cai via GitGitGadget, Aug 1, 2024
  22. 1/3 refs: keep track of unresolved reference value in iteratorsJohn Cai via GitGitGadget, Aug 1, 2024
  23. Junio C HamanoAug 1, 2024
  24. Patrick SteinhardtAug 5, 2024
  25. Junio C HamanoAug 5, 2024
  26. 3/3 ref-filter: populate symref from iteratorJohn Cai via GitGitGadget, Aug 1, 2024
  27. Junio C HamanoAug 1, 2024
  28. Junio C HamanoAug 1, 2024
  29. Junio C HamanoAug 1, 2024
  30. John CaiAug 6, 2024
  31. Junio C HamanoAug 6, 2024
  32. 2/3 refs: add referent to each_ref_fnJohn Cai via GitGitGadget, Aug 1, 2024
  33. 0/3 keep track of unresolved value of symbolic-ref in ref iteratorsJohn Cai via GitGitGadget, Aug 7, 2024
  34. 1/3 refs: keep track of unresolved reference value in iteratorsJohn Cai via GitGitGadget, Aug 7, 2024
  35. Junio C HamanoAug 7, 2024
  36. John CaiAug 8, 2024
  37. 2/3 refs: add referent to each_ref_fnJohn Cai via GitGitGadget, Aug 7, 2024
  38. 3/3 ref-filter: populate symref from iteratorJohn Cai via GitGitGadget, Aug 7, 2024
  39. 0/3 keep track of unresolved value of symbolic-ref in ref iteratorsJohn Cai via GitGitGadget, Aug 9, 2024
  40. 1/3 refs: keep track of unresolved reference value in iteratorsJohn Cai via GitGitGadget, Aug 9, 2024
  41. shejialuoNov 23, 2024
  42. 2/3 refs: add referent to each_ref_fnJohn Cai via GitGitGadget, Aug 9, 2024
  43. 3/3 ref-filter: populate symref from iteratorJohn Cai via GitGitGadget, Aug 9, 2024
  44. Junio C HamanoAug 9, 2024

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.