Skip to content

Commit 5afcb90

Browse files
KarthikNayakgitster
authored andcommitted
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 <[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 d325406 commit 5afcb90

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

builtin/branch.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,10 @@ static void rename_branch(const char *oldname, const char *newname, int force)
745745
strbuf_release(&newsection);
746746
}
747747

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+
*/
748752
static int opt_parse_merge_filter(const struct option *opt, const char *arg, int unset)
749753
{
750754
merge_filter = ((opt->long_name[0] == 'n')

ref-filter.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,3 +1134,22 @@ int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset)
11341134
s->atom = parse_ref_filter_atom(arg, arg+len);
11351135
return 0;
11361136
}
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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ struct ref_filter_cbdata {
5050
struct ref_filter *filter;
5151
};
5252

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+
5362
/*
5463
* API for filtering a set of refs. Based on the type of refs the user
5564
* 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
7180
int parse_opt_ref_sorting(const struct option *opt, const char *arg, int unset);
7281
/* Default sort option based on refname */
7382
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);
7485

7586
#endif /* REF_FILTER_H */

0 commit comments

Comments
 (0)