diff mbox series

[07/19] rev-parseopt: convert --tags

Message ID 20190508111249.15262-8-pclouds@gmail.com (mailing list archive)
State New, archived
Headers show
Series Convert revision.c to parseopt part 1/4 | expand

Commit Message

Duy Nguyen May 8, 2019, 11:12 a.m. UTC
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 revision.c | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)
diff mbox series

Patch

diff --git a/revision.c b/revision.c
index 0d34f81716..5183cdf66d 100644
--- a/revision.c
+++ b/revision.c
@@ -2425,6 +2425,27 @@  static int rev_opt_branches(const struct option *opt,
 	return 0;
 }
 
+static int rev_opt_tags(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/tags/", &cb);
+		clear_ref_exclusion(&revs->ref_excludes);
+	} else {
+		handle_refs(refs, revs, flags, refs_for_each_tag_ref);
+		clear_ref_exclusion(&revs->ref_excludes);
+	}
+	return 0;
+}
+
 static void make_pseudo_options(struct rev_info *revs)
 {
 	/*
@@ -2447,6 +2468,9 @@  static void make_pseudo_options(struct rev_info *revs)
 		OPT_REV_NOARG(0, "bisect",
 			      N_("synonym to refs/bisect/good-* --not refs/bisect/bad"),
 			      rev_opt_bisect),
+		OPT_REV_OPTARG(0, "tags", N_("<pattern>"),
+			       N_("include all refs in refs/tags (optionally matches pattern)"),
+			       rev_opt_tags),
 		OPT_END()
 	};
 	ALLOC_ARRAY(revs->pseudo_options, ARRAY_SIZE(options));
@@ -2487,10 +2511,7 @@  static int handle_revision_pseudo_opt(const char *submodule,
 	if (argc)
 		return argc;
 
-	if (!strcmp(arg, "--tags")) {
-		handle_refs(refs, revs, *flags, refs_for_each_tag_ref);
-		clear_ref_exclusion(&revs->ref_excludes);
-	} else if (!strcmp(arg, "--remotes")) {
+	if (!strcmp(arg, "--remotes")) {
 		handle_refs(refs, revs, *flags, refs_for_each_remote_ref);
 		clear_ref_exclusion(&revs->ref_excludes);
 	} else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
@@ -2502,11 +2523,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, "--tags=", &optarg)) {
-		struct all_refs_cb cb;
-		init_all_refs_cb(&cb, revs, *flags);
-		for_each_glob_ref_in(handle_one_ref, optarg, "refs/tags/", &cb);
-		clear_ref_exclusion(&revs->ref_excludes);
 	} else if (skip_prefix(arg, "--remotes=", &optarg)) {
 		struct all_refs_cb cb;
 		init_all_refs_cb(&cb, revs, *flags);