Skip to content

Commit 6841104

Browse files
KarthikNayakgitster
authored andcommitted
ref-filter: implement '--points-at' option
In 'tag -l' we have '--points-at' option which lets users list only tags of a given object. Implement this option in 'ref-filter.{c,h}' so that other commands can benefit from this. This is duplicated from tag.c, we will eventually remove that when we port tag.c to use ref-filter APIs. Based-on-patch-by: Jeff King <[email protected]> Mentored-by: Christian Couder <[email protected]> Mentored-by: Matthieu Moy <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b2172fd commit 6841104

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

builtin/tag.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ static int match_pattern(const char **patterns, const char *ref)
5656
return 0;
5757
}
5858

59+
/*
60+
* This is currently duplicated in ref-filter.c, and will eventually be
61+
* removed as we port tag.c to use the ref-filter APIs.
62+
*/
5963
static const unsigned char *match_points_at(const char *refname,
6064
const unsigned char *sha1)
6165
{

ref-filter.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,38 @@ static int match_name_as_path(const char **pattern, const char *refname)
842842
return 0;
843843
}
844844

845+
/*
846+
* Given a ref (sha1, refname), check if the ref belongs to the array
847+
* of sha1s. If the given ref is a tag, check if the given tag points
848+
* at one of the sha1s in the given sha1 array.
849+
* the given sha1_array.
850+
* NEEDSWORK:
851+
* 1. Only a single level of inderection is obtained, we might want to
852+
* change this to account for multiple levels (e.g. annotated tags
853+
* pointing to annotated tags pointing to a commit.)
854+
* 2. As the refs are cached we might know what refname peels to without
855+
* the need to parse the object via parse_object(). peel_ref() might be a
856+
* more efficient alternative to obtain the pointee.
857+
*/
858+
static const unsigned char *match_points_at(struct sha1_array *points_at,
859+
const unsigned char *sha1,
860+
const char *refname)
861+
{
862+
const unsigned char *tagged_sha1 = NULL;
863+
struct object *obj;
864+
865+
if (sha1_array_lookup(points_at, sha1) >= 0)
866+
return sha1;
867+
obj = parse_object(sha1);
868+
if (!obj)
869+
die(_("malformed object at '%s'"), refname);
870+
if (obj->type == OBJ_TAG)
871+
tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
872+
if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
873+
return tagged_sha1;
874+
return NULL;
875+
}
876+
845877
/* Allocate space for a new ref_array_item and copy the objectname and flag to it */
846878
static struct ref_array_item *new_ref_array_item(const char *refname,
847879
const unsigned char *objectname,
@@ -875,6 +907,9 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
875907
if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
876908
return 0;
877909

910+
if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
911+
return 0;
912+
878913
/*
879914
* We do not open the object yet; sort may only need refname
880915
* to do its job and the resulting list may yet to be pruned

ref-filter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct ref_array {
4242

4343
struct ref_filter {
4444
const char **name_patterns;
45+
struct sha1_array points_at;
4546
};
4647

4748
struct ref_filter_cbdata {

0 commit comments

Comments
 (0)