builtin/show-ref: convert to use `reference_get_peeled_oid()`

The git-show-ref(1) command has multiple different modes: - It knows to show all references matching a pattern. - It knows to list all references that are an exact match to whatever the user has provided. - It knows to check for reference existence. The first two commands use mostly the same infrastructure to print the references via `show_one()`. But while the former mode uses a proper iterator and thus has a `struct reference` available in its context, the latter calls `refs_read_ref()` and thus doesn't. Consequently, we cannot easily use `reference_get_peeled_oid()` to print the peeled value. Adapt the code so that we manually construct a `struct reference` when verifying refs. We wouldn't ever have the peeled value available anyway as we're not using an iterator here, so we can simply plug in the values we _do_ have. With this change we now have a `struct reference` available at both callsites of `show_one()` and can thus pass it, which allows us to use `reference_get_peeled_oid()` instead of `peel_iterated_oid()`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Oct 23, 2025 at 09:16 UTC feaaea4c123e6b94ebbdc2135278946ee9cc8eed
1 file changed +19 -13
builtin/show-ref.c
+19 -13
@@ -31,31 +31,31 @@ struct show_one_options {
31 };
32
33 static void show_one(const struct show_one_options *opts,
34 - const char *refname, const struct object_id *oid)
34 + const struct reference *ref)
35 {
36 const char *hex;
37 struct object_id peeled;
38
39 - if (!odb_has_object(the_repository->objects, oid,
39 + if (!odb_has_object(the_repository->objects, ref->oid,
40 HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
41 - die("git show-ref: bad ref %s (%s)", refname,
42 - oid_to_hex(oid));
41 + die("git show-ref: bad ref %s (%s)", ref->name,
42 + oid_to_hex(ref->oid));
43
44 if (opts->quiet)
45 return;
46
47 - hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev);
47 + hex = repo_find_unique_abbrev(the_repository, ref->oid, opts->abbrev);
48 if (opts->hash_only)
49 printf("%s\n", hex);
50 else
51 - printf("%s %s\n", hex, refname);
51 + printf("%s %s\n", hex, ref->name);
52
53 if (!opts->deref_tags)
54 return;
55
56 - if (!peel_iterated_oid(the_repository, oid, &peeled)) {
56 + if (!reference_get_peeled_oid(the_repository, ref, &peeled)) {
57 hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev);
58 - printf("%s %s^{}\n", hex, refname);
58 + printf("%s %s^{}\n", hex, ref->name);
59 }
60 }
61
@@ -93,7 +93,7 @@ static int show_ref(const struct reference *ref, void *cbdata)
93 match:
94 data->found_match++;
95
96 - show_one(data->show_one_opts, ref->name, ref->oid);
96 + show_one(data->show_one_opts, ref);
97
98 return 0;
99 }
@@ -175,12 +175,18 @@ static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
175
176 if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
177 !refs_read_ref(get_main_ref_store(the_repository), *refs, &oid)) {
178 - show_one(show_one_opts, *refs, &oid);
179 - }
180 - else if (!show_one_opts->quiet)
178 + struct reference ref = {
179 + .name = *refs,
180 + .oid = &oid,
181 + };
182 +
183 + show_one(show_one_opts, &ref);
184 + } else if (!show_one_opts->quiet) {
185 die("'%s' - not a valid ref", *refs);
182 - else
186 + } else {
187 return 1;
188 + }
189 +
190 refs++;
191 }
192