Subject: [PATCH 05/19] rev-parseopt: convert --branches

From: Nguyễn Thái Ngọc Duy <[email-masked]>
Date: Wed, 8 May 2019 18:12:35 +0700

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 revision.c | 38 +++++++++++++++++++++++++++++---------
 1 file changed, 29 insertions(+), 9 deletions(-)

diff --git a/revision.c b/revision.c
index 9a346577f6..7db1109b57 100644
--- a/revision.c
+++ b/revision.c
@@ -34,6 +34,10 @@
 	OPT_CALLBACK_F(s, l, revs, NULL, h, \
 		       PARSE_OPT_NONEG | PARSE_OPT_NOARG, cb)
 
+#define OPT_REV_OPTARG(s, l, a, h, cb) \
+	OPT_CALLBACK_F(s, l, revs, a, h, \
+		       PARSE_OPT_NONEG | PARSE_OPT_OPTARG, cb)
+
 volatile show_early_output_fn_t show_early_output;
 
 static const char *term_bad;
@@ -2383,6 +2387,27 @@ static int rev_opt_all(const struct option *opt, const char *arg, int unset)
 	return 0;
 }
 
+static int rev_opt_branches(const struct option *opt,
+			    const char *arg, int unset)
+{
+	struct rev_info		*revs  = opt->value;
+	int			 flags = *revs->pseudo_flags;
+	struct ref_store	*refs  = revs->pseudo_refs;
+
+	BUG_ON_OPT_NEG(unset);
+	if (arg) {
+		struct all_refs_cb cb;
+
+		init_all_refs_cb(&cb, revs, flags);
+		for_each_glob_ref_in(handle_one_ref, arg, "refs/heads/", &cb);
+		clear_ref_exclusion(&revs->ref_excludes);
+	} else {
+		handle_refs(refs, revs, flags, refs_for_each_branch_ref);
+		clear_ref_exclusion(&revs->ref_excludes);
+	}
+	return 0;
+}
+
 static void make_pseudo_options(struct rev_info *revs)
 {
 	/*
@@ -2399,6 +2424,9 @@ static void make_pseudo_options(struct rev_info *revs)
 		OPT_REV_NOARG(0, "all",
 			      N_("include all refs in refs/ and HEAD"),
 			      rev_opt_all),
+		OPT_REV_OPTARG(0, "branches", N_("<pattern>"),
+			       N_("include all refs in refs/heads (optionally matches pattern)"),
+			       rev_opt_branches),
 		OPT_END()
 	};
 	ALLOC_ARRAY(revs->pseudo_options, ARRAY_SIZE(options));
@@ -2439,10 +2467,7 @@ static int handle_revision_pseudo_opt(const char *submodule,
 	if (argc)
 		return argc;
 
-	if (!strcmp(arg, "--branches")) {
-		handle_refs(refs, revs, *flags, refs_for_each_branch_ref);
-		clear_ref_exclusion(&revs->ref_excludes);
-	} else if (!strcmp(arg, "--bisect")) {
+	if (!strcmp(arg, "--bisect")) {
 		read_bisect_terms(&term_bad, &term_good);
 		handle_refs(refs, revs, *flags, for_each_bad_bisect_ref);
 		handle_refs(refs, revs, *flags ^ (UNINTERESTING | BOTTOM),
@@ -2463,11 +2488,6 @@ static int handle_revision_pseudo_opt(const char *submodule,
 	} else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
 		add_ref_exclusion(&revs->ref_excludes, optarg);
 		return argcount;
-	} else if (skip_prefix(arg, "--branches=", &optarg)) {
-		struct all_refs_cb cb;
-		init_all_refs_cb(&cb, revs, *flags);
-		for_each_glob_ref_in(handle_one_ref, optarg, "refs/heads/", &cb);
-		clear_ref_exclusion(&revs->ref_excludes);
 	} else if (skip_prefix(arg, "--tags=", &optarg)) {
 		struct all_refs_cb cb;
 		init_all_refs_cb(&cb, revs, *flags);
-- 
2.21.0.1141.gd54ac2cb17