Skip to content

Commit b2172fd

Browse files
KarthikNayakgitster
authored andcommitted
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 <[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 af83baf commit b2172fd

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

builtin/tag.c

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -546,23 +546,6 @@ static int strbuf_check_tag_ref(struct strbuf *sb, const char *name)
546546
return check_refname_format(sb->buf, 0);
547547
}
548548

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-
566549
static int parse_opt_sort(const struct option *opt, const char *arg, int unset)
567550
{
568551
int *sort = opt->value;
@@ -625,8 +608,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
625608
parse_opt_with_commit, (intptr_t)"HEAD",
626609
},
627610
{
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
630613
},
631614
OPT_END()
632615
};

parse-options-cb.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "commit.h"
55
#include "color.h"
66
#include "string-list.h"
7+
#include "sha1-array.h"
78

89
/*----- some often used options -----*/
910

@@ -92,6 +93,22 @@ int parse_opt_with_commit(const struct option *opt, const char *arg, int unset)
9293
return 0;
9394
}
9495

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+
95112
int parse_opt_tertiary(const struct option *opt, const char *arg, int unset)
96113
{
97114
int *target = opt->value;

parse-options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ extern int parse_opt_approxidate_cb(const struct option *, const char *, int);
220220
extern int parse_opt_expiry_date_cb(const struct option *, const char *, int);
221221
extern int parse_opt_color_flag_cb(const struct option *, const char *, int);
222222
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);
223224
extern int parse_opt_with_commit(const struct option *, const char *, int);
224225
extern int parse_opt_tertiary(const struct option *, const char *, int);
225226
extern int parse_opt_string_list(const struct option *, const char *, int);

0 commit comments

Comments
 (0)