Skip to content

Commit

Permalink
ref-filter: simplify return type of match_points_at
Browse files Browse the repository at this point in the history
We return the oid that matched, but the sole caller only cares whether
we matched anything at all. This is mostly academic, since there's only
one caller, but the lifetime of the returned pointer is not immediately
clear. Sometimes it points to an oid in a tag struct, which should live
forever. And sometimes to the oid passed in, which only lives as long as
the each_ref_fn callback we're called from.

Simplify this to a boolean return which is more direct and obvious. As a
bonus, this lets us avoid the weird pattern of overwriting our "oid"
parameter in the loop (since we now only refer to the tagged oid one
time, and can just inline the call to get it).

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
peff authored and gitster committed Jul 17, 2023
1 parent 870eb53 commit d9e0062
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions ref-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2330,20 +2330,22 @@ static int for_each_fullref_in_pattern(struct ref_filter *filter,
/*
* Given a ref (oid, refname), check if the ref belongs to the array
* of oids. If the given ref is a tag, check if the given tag points
* at one of the oids in the given oid array.
* at one of the oids in the given oid array. Returns non-zero if a
* match is found.
*
* NEEDSWORK:
* As the refs are cached we might know what refname peels to without
* the need to parse the object via parse_object(). peel_ref() might be a
* more efficient alternative to obtain the pointee.
*/
static const struct object_id *match_points_at(struct oid_array *points_at,
const struct object_id *oid,
const char *refname)
static int match_points_at(struct oid_array *points_at,
const struct object_id *oid,
const char *refname)
{
struct object *obj;

if (oid_array_lookup(points_at, oid) >= 0)
return oid;
return 1;
obj = parse_object_with_flags(the_repository, oid,
PARSE_OBJECT_SKIP_HASH_CHECK);
while (obj && obj->type == OBJ_TAG) {
Expand All @@ -2354,15 +2356,14 @@ static const struct object_id *match_points_at(struct oid_array *points_at,
break;
}

oid = get_tagged_oid(tag);
if (oid_array_lookup(points_at, oid) >= 0)
return oid;
if (oid_array_lookup(points_at, get_tagged_oid(tag)) >= 0)
return 1;

obj = tag->tagged;
}
if (!obj)
die(_("malformed object at '%s'"), refname);
return NULL;
return 0;
}

/*
Expand Down

0 comments on commit d9e0062

Please sign in to comment.