Subject: [PATCH 04/19] rev-parseopt: convert --all

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

Signed-off-by: Nguyễn Thái Ngọc Duy <[email-masked]>
---
 revision.c | 41 ++++++++++++++++++++++++++++++-----------
 revision.h |  2 ++
 2 files changed, 32 insertions(+), 11 deletions(-)

diff --git a/revision.c b/revision.c
index 65d40c9255..9a346577f6 100644
--- a/revision.c
+++ b/revision.c
@@ -30,6 +30,10 @@
 #include "prio-queue.h"
 #include "hashmap.h"
 
+#define OPT_REV_NOARG(s, l, h, cb) \
+	OPT_CALLBACK_F(s, l, revs, NULL, h, \
+		       PARSE_OPT_NONEG | PARSE_OPT_NOARG, cb)
+
 volatile show_early_output_fn_t show_early_output;
 
 static const char *term_bad;
@@ -2359,6 +2363,26 @@ static int for_each_good_bisect_ref(struct ref_store *refs, each_ref_fn fn, void
 	return for_each_bisect_ref(refs, fn, cb_data, term_good);
 }
 
+static int rev_opt_all(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);
+	BUG_ON_OPT_ARG(arg);
+	handle_refs(refs, revs, flags, refs_for_each_ref);
+	handle_refs(refs, revs, flags, refs_head_ref);
+	if (!revs->single_worktree) {
+		struct all_refs_cb cb;
+
+		init_all_refs_cb(&cb, revs, flags);
+		other_head_refs(handle_one_ref, &cb);
+	}
+	clear_ref_exclusion(&revs->ref_excludes);
+	return 0;
+}
+
 static void make_pseudo_options(struct rev_info *revs)
 {
 	/*
@@ -2372,6 +2396,9 @@ static void make_pseudo_options(struct rev_info *revs)
 	 * register it in the list at the top of handle_revision_opt.
 	 */
 	struct option options[] = {
+		OPT_REV_NOARG(0, "all",
+			      N_("include all refs in refs/ and HEAD"),
+			      rev_opt_all),
 		OPT_END()
 	};
 	ALLOC_ARRAY(revs->pseudo_options, ARRAY_SIZE(options));
@@ -2400,6 +2427,8 @@ static int handle_revision_pseudo_opt(const char *submodule,
 	} else
 		refs = get_main_ref_store(revs->repo);
 
+	revs->pseudo_flags = flags;
+	revs->pseudo_refs = refs;
 	argc = parse_options(argc, argv, revs->prefix,
 			     revs->pseudo_options, NULL,
 			     PARSE_OPT_KEEP_DASHDASH |
@@ -2410,17 +2439,7 @@ static int handle_revision_pseudo_opt(const char *submodule,
 	if (argc)
 		return argc;
 
-	if (!strcmp(arg, "--all")) {
-		handle_refs(refs, revs, *flags, refs_for_each_ref);
-		handle_refs(refs, revs, *flags, refs_head_ref);
-		if (!revs->single_worktree) {
-			struct all_refs_cb cb;
-
-			init_all_refs_cb(&cb, revs, *flags);
-			other_head_refs(handle_one_ref, &cb);
-		}
-		clear_ref_exclusion(&revs->ref_excludes);
-	} else if (!strcmp(arg, "--branches")) {
+	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")) {
diff --git a/revision.h b/revision.h
index 0769c97dee..cec5215c04 100644
--- a/revision.h
+++ b/revision.h
@@ -281,6 +281,8 @@ struct rev_info {
 
 	struct repository *repo;
 	struct option *pseudo_options;
+	int *pseudo_flags;
+	struct ref_store *pseudo_refs;
 };
 
 int ref_excluded(struct string_list *, const char *path);
-- 
2.21.0.1141.gd54ac2cb17