tag: libify parse_opt_points_at()

Rename 'parse_opt_points_at()' to 'parse_opt_object_name()' and move it from 'tag.c' to 'parse-options'. This now acts as a common parse_opt function which accepts an objectname and stores it into a sha1_array. 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 b2172fdf702426cb4eba093b5504d0a0e9e59746
3 files changed +20 -19
builtin/tag.c
+2 -19
@@ -546,23 +546,6 @@ static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
546 return check_refname_format(sb->buf, 0);
547 }
548
549 -static int parse_opt_points_at(const struct option *opt __attribute__((unused)),
550 - const char *arg, int unset)
551 -{
552 - unsigned char sha1[20];
553 -
554 - if (unset) {
555 - sha1_array_clear(&points_at);
556 - return 0;
557 - }
558 - if (!arg)
559 - return error(_("switch 'points-at' requires an object"));
560 - if (get_sha1(arg, sha1))
561 - return error(_("malformed object name '%s'"), arg);
562 - sha1_array_append(&points_at, sha1);
563 - return 0;
564 -}
565 -
549 static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
550 {
551 int *sort = opt->value;
@@ -625,8 +608,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
608 parse_opt_with_commit, (intptr_t)"HEAD",
609 },
610 {
628 - OPTION_CALLBACK, 0, "points-at", NULL, N_("object"),
629 - N_("print only tags of the object"), 0, parse_opt_points_at
611 + OPTION_CALLBACK, 0, "points-at", &points_at, N_("object"),
612 + N_("print only tags of the object"), 0, parse_opt_object_name
613 },
614 OPT_END()
615 };
parse-options-cb.c
+17
@@ -4,6 +4,7 @@
4 #include "commit.h"
5 #include "color.h"
6 #include "string-list.h"
7 +#include "sha1-array.h"
8
9 /*----- some often used options -----*/
10
@@ -92,6 +93,22 @@ int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
93 return 0;
94 }
95
96 +int parse_opt_object_name(const struct option *opt, const char *arg, int unset)
97 +{
98 + unsigned char sha1[20];
99 +
100 + if (unset) {
101 + sha1_array_clear(opt->value);
102 + return 0;
103 + }
104 + if (!arg)
105 + return -1;
106 + if (get_sha1(arg, sha1))
107 + return error(_("malformed object name '%s'"), arg);
108 + sha1_array_append(opt->value, sha1);
109 + return 0;
110 +}
111 +
112 int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
113 {
114 int *target = opt->value;
parse-options.h
+1
@@ -220,6 +220,7 @@ extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
220 extern int parse_opt_expiry_date_cb(const struct option *, const char *, int);
221 extern int parse_opt_color_flag_cb(const struct option *, const char *, int);
222 extern int parse_opt_verbosity_cb(const struct option *, const char *, int);
223 +extern int parse_opt_object_name(const struct option *, const char *, int);
224 extern int parse_opt_with_commit(const struct option *, const char *, int);
225 extern int parse_opt_tertiary(const struct option *, const char *, int);
226 extern int parse_opt_string_list(const struct option *, const char *, int);