]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/show-ref: convert to use `reference_get_peeled_oid()`
authorPatrick Steinhardt <ps@pks.im>
Thu, 23 Oct 2025 07:16:17 +0000 (09:16 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 4 Nov 2025 15:32:25 +0000 (07:32 -0800)
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>
builtin/show-ref.c

index 4803b5e59865f60641b48d4e5c5a8971ddb5eb7c..4d4984e4e0c244b32d46affc658a699bb04f1c82 100644 (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++;
        }