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 <peff@peff.net> Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Karthik Nayak committed
Jul 7, 2015 at 21:36 UTC
68411046b5067de9c378d1f58313f2fae288286c
3 files changed
+40
builtin/tag.c
+4
@@ -56,6 +56,10 @@ static int match_pattern(const char **patterns, const char *ref)
56
return 0;
57
}
58
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
+ */
63
static const unsigned char *match_points_at(const char *refname,
64
const unsigned char *sha1)
65
{
ref-filter.c
+35
@@ -842,6 +842,38 @@ static int match_name_as_path(const char **pattern, const char *refname)
842
return 0;
843
}
844
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
+
877
/* Allocate space for a new ref_array_item and copy the objectname and flag to it */
878
static struct ref_array_item *new_ref_array_item(const char *refname,
879
const unsigned char *objectname,
@@ -875,6 +907,9 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
907
if (*filter->name_patterns && !match_name_as_path(filter->name_patterns, refname))
908
return 0;
909
910
+ if (filter->points_at.nr && !match_points_at(&filter->points_at, oid->hash, refname))
911
+ return 0;
912
+
913
/*
914
* We do not open the object yet; sort may only need refname
915
* to do its job and the resulting list may yet to be pruned
ref-filter.h
+1
@@ -42,6 +42,7 @@ struct ref_array {
42
43
struct ref_filter {
44
const char **name_patterns;
45
+ struct sha1_array points_at;
46
};
47
48
struct ref_filter_cbdata {