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>
This commit is contained in:
Patrick Steinhardt
2025-10-23 09:16:17 +02:00
committed by Junio C Hamano
parent 70b783c3a1
commit feaaea4c12

View File

@@ -31,31 +31,31 @@ struct show_one_options {
};
static void show_one(const struct show_one_options *opts,
const char *refname, const struct object_id *oid)
const struct reference *ref)
{
const char *hex;
struct object_id peeled;
if (!odb_has_object(the_repository->objects, oid,
if (!odb_has_object(the_repository->objects, ref->oid,
HAS_OBJECT_RECHECK_PACKED | HAS_OBJECT_FETCH_PROMISOR))
die("git show-ref: bad ref %s (%s)", refname,
oid_to_hex(oid));
die("git show-ref: bad ref %s (%s)", ref->name,
oid_to_hex(ref->oid));
if (opts->quiet)
return;
hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev);
hex = repo_find_unique_abbrev(the_repository, ref->oid, opts->abbrev);
if (opts->hash_only)
printf("%s\n", hex);
else
printf("%s %s\n", hex, refname);
printf("%s %s\n", hex, ref->name);
if (!opts->deref_tags)
return;
if (!peel_iterated_oid(the_repository, oid, &peeled)) {
if (!reference_get_peeled_oid(the_repository, ref, &peeled)) {
hex = repo_find_unique_abbrev(the_repository, &peeled, opts->abbrev);
printf("%s %s^{}\n", hex, refname);
printf("%s %s^{}\n", hex, ref->name);
}
}
@@ -93,7 +93,7 @@ static int show_ref(const struct reference *ref, void *cbdata)
match:
data->found_match++;
show_one(data->show_one_opts, ref->name, ref->oid);
show_one(data->show_one_opts, ref);
return 0;
}
@@ -175,12 +175,18 @@ static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
!refs_read_ref(get_main_ref_store(the_repository), *refs, &oid)) {
show_one(show_one_opts, *refs, &oid);
}
else if (!show_one_opts->quiet)
struct reference ref = {
.name = *refs,
.oid = &oid,
};
show_one(show_one_opts, &ref);
} else if (!show_one_opts->quiet) {
die("'%s' - not a valid ref", *refs);
else
} else {
return 1;
}
refs++;
}