Skip to content

Commit 90e3951

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 474469e commit 90e3951

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

builtin/show-ref.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,31 @@ struct show_one_options {
3131
};
3232

3333
static void show_one(const struct show_one_options *opts,
34-
const char *refname, const struct object_id *oid)
34+
const struct reference *ref)
3535
{
3636
const char *hex;
3737
struct object_id peeled;
3838

39-
if (!odb_has_object(the_repository->objects, oid,
39+
if (!odb_has_object(the_repository->objects, ref->oid,
4040
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));
4343

4444
if (opts->quiet)
4545
return;
4646

47-
hex = repo_find_unique_abbrev(the_repository, oid, opts->abbrev);
47+
hex = repo_find_unique_abbrev(the_repository, ref->oid, opts->abbrev);
4848
if (opts->hash_only)
4949
printf("%s\n", hex);
5050
else
51-
printf("%s %s\n", hex, refname);
51+
printf("%s %s\n", hex, ref->name);
5252

5353
if (!opts->deref_tags)
5454
return;
5555

56-
if (!peel_iterated_oid(the_repository, oid, &peeled)) {
56+
if (!reference_get_peeled_oid(the_repository, ref, &peeled)) {
5757
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);
5959
}
6060
}
6161

@@ -93,7 +93,7 @@ static int show_ref(const struct reference *ref, void *cbdata)
9393
match:
9494
data->found_match++;
9595

96-
show_one(data->show_one_opts, ref->name, ref->oid);
96+
show_one(data->show_one_opts, ref);
9797

9898
return 0;
9999
}
@@ -175,12 +175,18 @@ static int cmd_show_ref__verify(const struct show_one_options *show_one_opts,
175175

176176
if ((starts_with(*refs, "refs/") || refname_is_safe(*refs)) &&
177177
!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) {
181185
die("'%s' - not a valid ref", *refs);
182-
else
186+
} else {
183187
return 1;
188+
}
189+
184190
refs++;
185191
}
186192

0 commit comments

Comments
 (0)