ref-filter: add parse_opt_merge_filter()

Add 'parse_opt_merge_filter()' to parse '--merged' and '--no-merged' options and write macros for the same. This is copied from 'builtin/branch.c' which will eventually be removed when we port 'branch.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 5afcb90560586765bf21fb09959a8b4497804639
3 files changed +34
builtin/branch.c
+4
@@ -745,6 +745,10 @@ static void rename_branch(const char *oldname, const char *newname, int force)
745 strbuf_release(&newsection);
746 }
747
748 +/*
749 + * This function is duplicated in ref-filter. It will eventually be removed
750 + * when we port branch.c to use ref-filter APIs.
751 + */
752 static int opt_parse_merge_filter(const struct option *opt, const char *arg, int unset)
753 {
754 merge_filter = ((opt->long_name[0] == 'n')
ref-filter.c
+19
@@ -1134,3 +1134,22 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
1134 s->atom = parse_ref_filter_atom(arg, arg+len);
1135 return 0;
1136 }
1137 +
1138 +int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset)
1139 +{
1140 + struct ref_filter *rf = opt->value;
1141 + unsigned char sha1[20];
1142 +
1143 + rf->merge = starts_with(opt->long_name, "no")
1144 + ? REF_FILTER_MERGED_OMIT
1145 + : REF_FILTER_MERGED_INCLUDE;
1146 +
1147 + if (get_sha1(arg, sha1))
1148 + die(_("malformed object name %s"), arg);
1149 +
1150 + rf->merge_commit = lookup_commit_reference_gently(sha1, 0);
1151 + if (!rf->merge_commit)
1152 + return opterror(opt, "must point to a commit", 0);
1153 +
1154 + return 0;
1155 +}
ref-filter.h
+11
@@ -50,6 +50,15 @@ struct ref_filter_cbdata {
50 struct ref_filter *filter;
51 };
52
53 +/* Macros for checking --merged and --no-merged options */
54 +#define _OPT_MERGED_NO_MERGED(option, filter, h) \
55 + { OPTION_CALLBACK, 0, option, (filter), N_("commit"), (h), \
56 + PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG, \
57 + parse_opt_merge_filter, (intptr_t) "HEAD" \
58 + }
59 +#define OPT_MERGED(f, h) _OPT_MERGED_NO_MERGED("merged", f, h)
60 +#define OPT_NO_MERGED(f, h) _OPT_MERGED_NO_MERGED("no-merged", f, h)
61 +
62 /*
63 * API for filtering a set of refs. Based on the type of refs the user
64 * has requested, we iterate through those refs and apply filters
@@ -71,5 +80,7 @@ void show_ref_array_item(struct ref_array_item *info, const char *format, int qu
80 int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
81 /* Default sort option based on refname */
82 struct ref_sorting *ref_default_sorting(void);
83 +/* Function to parse --merged and --no-merged options */
84 +int parse_opt_merge_filter(const struct option *opt, const char *arg, int unset);
85
86 #endif /* REF_FILTER_H */