tag.c: use 'ref-filter' data structures

Make 'tag.c' use 'ref-filter' data structures and make changes to support the new data structures. This is a part of the process of porting 'tag.c' to use 'ref-filter' APIs. This is a temporary step before porting 'tag.c' to use 'ref-filter' completely. As this is a temporary step, most of the code introduced here will be removed when 'tag.c' is ported over to use 'ref-filter' APIs. 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 Sep 10, 2015 at 21:18 UTC ac4cc866c8cddbc25b5f213286a0269318dd2485
1 file changed +57 -49
builtin/tag.c
+57 -49
@@ -17,6 +17,7 @@
17 #include "gpg-interface.h"
18 #include "sha1-array.h"
19 #include "column.h"
20 +#include "ref-filter.h"
21
22 static const char * const git_tag_usage[] = {
23 N_("git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<head>]"),
@@ -34,15 +35,6 @@ static const char * const git_tag_usage[] = {
35
36 static int tag_sort;
37
37 -struct tag_filter {
38 - const char **patterns;
39 - int lines;
40 - int sort;
41 - struct string_list tags;
42 - struct commit_list *with_commit;
43 -};
44 -
45 -static struct sha1_array points_at;
38 static unsigned int colopts;
39
40 static int match_pattern(const char **patterns, const char *ref)
@@ -61,19 +53,20 @@ static int match_pattern(const char **patterns, const char *ref)
53 * removed as we port tag.c to use the ref-filter APIs.
54 */
55 static const unsigned char *match_points_at(const char *refname,
64 - const unsigned char *sha1)
56 + const unsigned char *sha1,
57 + struct sha1_array *points_at)
58 {
59 const unsigned char *tagged_sha1 = NULL;
60 struct object *obj;
61
69 - if (sha1_array_lookup(&points_at, sha1) >= 0)
62 + if (sha1_array_lookup(points_at, sha1) >= 0)
63 return sha1;
64 obj = parse_object(sha1);
65 if (!obj)
66 die(_("malformed object at '%s'"), refname);
67 if (obj->type == OBJ_TAG)
68 tagged_sha1 = ((struct tag *)obj)->tagged->sha1;
76 - if (tagged_sha1 && sha1_array_lookup(&points_at, tagged_sha1) >= 0)
69 + if (tagged_sha1 && sha1_array_lookup(points_at, tagged_sha1) >= 0)
70 return tagged_sha1;
71 return NULL;
72 }
@@ -228,12 +221,24 @@ free_return:
221 free(buf);
222 }
223
224 +static void ref_array_append(struct ref_array *array, const char *refname)
225 +{
226 + size_t len = strlen(refname);
227 + struct ref_array_item *ref = xcalloc(1, sizeof(struct ref_array_item) + len + 1);
228 + memcpy(ref->refname, refname, len);
229 + ref->refname[len] = '\0';
230 + REALLOC_ARRAY(array->items, array->nr + 1);
231 + array->items[array->nr++] = ref;
232 +}
233 +
234 static int show_reference(const char *refname, const struct object_id *oid,
235 int flag, void *cb_data)
236 {
234 - struct tag_filter *filter = cb_data;
237 + struct ref_filter_cbdata *data = cb_data;
238 + struct ref_array *array = data->array;
239 + struct ref_filter *filter = data->filter;
240
236 - if (match_pattern(filter->patterns, refname)) {
241 + if (match_pattern(filter->name_patterns, refname)) {
242 if (filter->with_commit) {
243 struct commit *commit;
244
@@ -244,12 +249,12 @@ static int show_reference(const char *refname, const struct object_id *oid,
249 return 0;
250 }
251
247 - if (points_at.nr && !match_points_at(refname, oid->hash))
252 + if (filter->points_at.nr && !match_points_at(refname, oid->hash, &filter->points_at))
253 return 0;
254
255 if (!filter->lines) {
251 - if (filter->sort)
252 - string_list_append(&filter->tags, refname);
256 + if (tag_sort)
257 + ref_array_append(array, refname);
258 else
259 printf("%s\n", refname);
260 return 0;
@@ -264,36 +269,36 @@ static int show_reference(const char *refname, const struct object_id *oid,
269
270 static int sort_by_version(const void *a_, const void *b_)
271 {
267 - const struct string_list_item *a = a_;
268 - const struct string_list_item *b = b_;
269 - return versioncmp(a->string, b->string);
272 + const struct ref_array_item *a = *((struct ref_array_item **)a_);
273 + const struct ref_array_item *b = *((struct ref_array_item **)b_);
274 + return versioncmp(a->refname, b->refname);
275 }
276
272 -static int list_tags(const char **patterns, int lines,
273 - struct commit_list *with_commit, int sort)
277 +static int list_tags(struct ref_filter *filter, int sort)
278 {
275 - struct tag_filter filter;
279 + struct ref_array array;
280 + struct ref_filter_cbdata data;
281 +
282 + memset(&array, 0, sizeof(array));
283 + data.array = &array;
284 + data.filter = filter;
285
277 - filter.patterns = patterns;
278 - filter.lines = lines;
279 - filter.sort = sort;
280 - filter.with_commit = with_commit;
281 - memset(&filter.tags, 0, sizeof(filter.tags));
282 - filter.tags.strdup_strings = 1;
286 + if (filter->lines == -1)
287 + filter->lines = 0;
288
284 - for_each_tag_ref(show_reference, (void *)&filter);
289 + for_each_tag_ref(show_reference, &data);
290 if (sort) {
291 int i;
292 if ((sort & SORT_MASK) == VERCMP_SORT)
288 - qsort(filter.tags.items, filter.tags.nr,
289 - sizeof(struct string_list_item), sort_by_version);
293 + qsort(array.items, array.nr,
294 + sizeof(struct ref_array_item *), sort_by_version);
295 if (sort & REVERSE_SORT)
291 - for (i = filter.tags.nr - 1; i >= 0; i--)
292 - printf("%s\n", filter.tags.items[i].string);
296 + for (i = array.nr - 1; i >= 0; i--)
297 + printf("%s\n", array.items[i]->refname);
298 else
294 - for (i = 0; i < filter.tags.nr; i++)
295 - printf("%s\n", filter.tags.items[i].string);
296 - string_list_clear(&filter.tags, 0);
299 + for (i = 0; i < array.nr; i++)
300 + printf("%s\n", array.items[i]->refname);
301 + ref_array_clear(&array);
302 }
303 return 0;
304 }
@@ -574,17 +579,17 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
579 const char *object_ref, *tag;
580 struct create_tag_options opt;
581 char *cleanup_arg = NULL;
577 - int annotate = 0, force = 0, lines = -1;
582 int create_reflog = 0;
583 + int annotate = 0, force = 0;
584 int cmdmode = 0;
585 const char *msgfile = NULL, *keyid = NULL;
586 struct msg_arg msg = { 0, STRBUF_INIT };
582 - struct commit_list *with_commit = NULL;
587 struct ref_transaction *transaction;
588 struct strbuf err = STRBUF_INIT;
589 + struct ref_filter filter;
590 struct option options[] = {
591 OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
587 - { OPTION_INTEGER, 'n', NULL, &lines, N_("n"),
592 + { OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
593 N_("print <n> lines of each tag message"),
594 PARSE_OPT_OPTARG, NULL, 1 },
595 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete tags"), 'd'),
@@ -606,14 +611,14 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
611
612 OPT_GROUP(N_("Tag listing options")),
613 OPT_COLUMN(0, "column", &colopts, N_("show tag list in columns")),
609 - OPT_CONTAINS(&with_commit, N_("print only tags that contain the commit")),
610 - OPT_WITH(&with_commit, N_("print only tags that contain the commit")),
614 + OPT_CONTAINS(&filter.with_commit, N_("print only tags that contain the commit")),
615 + OPT_WITH(&filter.with_commit, N_("print only tags that contain the commit")),
616 {
617 OPTION_CALLBACK, 0, "sort", &tag_sort, N_("type"), N_("sort tags"),
618 PARSE_OPT_NONEG, parse_opt_sort
619 },
620 {
616 - OPTION_CALLBACK, 0, "points-at", &points_at, N_("object"),
621 + OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
622 N_("print only tags of the object"), 0, parse_opt_object_name
623 },
624 OPT_END()
@@ -622,6 +627,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
627 git_config(git_tag_config, NULL);
628
629 memset(&opt, 0, sizeof(opt));
630 + memset(&filter, 0, sizeof(filter));
631 + filter.lines = -1;
632
633 argc = parse_options(argc, argv, prefix, options, git_tag_usage, 0);
634
@@ -638,7 +645,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
645 usage_with_options(git_tag_usage, options);
646
647 finalize_colopts(&colopts, -1);
641 - if (cmdmode == 'l' && lines != -1) {
648 + if (cmdmode == 'l' && filter.lines != -1) {
649 if (explicitly_enable_column(colopts))
650 die(_("--column and -n are incompatible"));
651 colopts = 0;
@@ -651,18 +658,19 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
658 copts.padding = 2;
659 run_column_filter(colopts, &copts);
660 }
654 - if (lines != -1 && tag_sort)
661 + if (filter.lines != -1 && tag_sort)
662 die(_("--sort and -n are incompatible"));
656 - ret = list_tags(argv, lines == -1 ? 0 : lines, with_commit, tag_sort);
663 + filter.name_patterns = argv;
664 + ret = list_tags(&filter, tag_sort);
665 if (column_active(colopts))
666 stop_column_filter();
667 return ret;
668 }
661 - if (lines != -1)
669 + if (filter.lines != -1)
670 die(_("-n option is only allowed with -l."));
663 - if (with_commit)
671 + if (filter.with_commit)
672 die(_("--contains option is only allowed with -l."));
665 - if (points_at.nr)
673 + if (filter.points_at.nr)
674 die(_("--points-at option is only allowed with -l."));
675 if (cmdmode == 'd')
676 return for_each_tag_name(argv, delete_tag);