[PATCH 25/25] Make builtin-show-ref.c use parse_options [small backward incompatibility].

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



-h (clashes with auto-help) and is replaced with -H.

Signed-off-by: Pierre Habouzit <madcoder@xxxxxxxxxx>
---
 Documentation/git-show-ref.txt |    4 +-
 builtin-show-ref.c             |  127 ++++++++++++++--------------------------
 2 files changed, 47 insertions(+), 84 deletions(-)

diff --git a/Documentation/git-show-ref.txt b/Documentation/git-show-ref.txt
index 2355aa5..ea6df8a 100644
--- a/Documentation/git-show-ref.txt
+++ b/Documentation/git-show-ref.txt
@@ -29,7 +29,7 @@ in the `.git` directory.
 OPTIONS
 -------
 
--h, --head::
+-H, --head::
 
 	Show the HEAD reference.
 
@@ -44,7 +44,7 @@ OPTIONS
 	Dereference tags into object IDs as well. They will be shown with "^{}"
 	appended.
 
--s, --hash::
+-s, --hash[=len]::
 
 	Only show the SHA1 hash, not the reference name. When also using
 	--dereference the dereferenced tag will still be shown after the SHA1.
diff --git a/builtin-show-ref.c b/builtin-show-ref.c
index 65051d1..26d537b 100644
--- a/builtin-show-ref.c
+++ b/builtin-show-ref.c
@@ -4,12 +4,42 @@
 #include "object.h"
 #include "tag.h"
 #include "path-list.h"
+#include "parse-options.h"
 
-static const char show_ref_usage[] = "git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<length>]] [--abbrev[=<length>]] [--tags] [--heads] [--] [pattern*] < ref-list";
+static const char * const show_ref_usage[] = {
+	"git show-ref [options] [<pattern>...]",
+	"git show-ref [options] --exclude-existing [<pattern>] < ref-list",
+	NULL,
+};
 
 static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,
 	found_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;
-static const char **pattern;
+static char const * exclude_mode = NULL;
+
+static int parse_opt_hash_cb(const struct option *opt, const char *arg,
+							 int unset)
+{
+	hash_only = !unset;
+	/* backward compatibility: --hash does not sets --abbrev */
+	if (!unset && !arg)
+		arg = "0";
+	return parse_opt_abbrev_cb(opt, arg, unset);
+}
+
+static struct option show_ref_opts[] = {
+	OPT__QUIET(&quiet),
+	{ OPTION_CALLBACK, 's', "hash", &abbrev, "n", "only ouput hashes",
+		PARSE_OPT_OPTARG, &parse_opt_hash_cb, 0 },
+	OPT__ABBREV(&abbrev),
+	OPT_BOOLEAN('d', "dereference", &deref_tags, "dereference tags into ids"),
+	OPT_BOOLEAN('H', "head", &show_head, "show the HEAD reference"),
+	OPT_BOOLEAN( 0 , "tags", &tags_only, "limit to refs/tags/* only"),
+	OPT_BOOLEAN( 0 , "heads", &heads_only, "limit to refs/heads/* only"),
+	OPT_BOOLEAN( 0 , "verify", &verify, "stricter reference checking"),
+	{ OPTION_STRING, 0, "exclude-existing", &exclude_mode, "pattern",
+		"work in filtering mode", PARSE_OPT_OPTARG, NULL, (intptr_t)"" },
+	OPT_END(),
+};
 
 static void show_one(const char *refname, const unsigned char *sha1)
 {
@@ -25,6 +55,7 @@ static int show_ref(const char *refname, const unsigned char *sha1, int flag, vo
 	struct object *obj;
 	const char *hex;
 	unsigned char peeled[20];
+	const char **pattern = cbdata;
 
 	if (tags_only || heads_only) {
 		int match;
@@ -149,101 +180,33 @@ static int exclude_existing(const char *match)
 
 int cmd_show_ref(int argc, const char **argv, const char *prefix)
 {
-	int i;
-
-	for (i = 1; i < argc; i++) {
-		const char *arg = argv[i];
-		if (*arg != '-') {
-			pattern = argv + i;
-			break;
-		}
-		if (!strcmp(arg, "--")) {
-			pattern = argv + i + 1;
-			if (!*pattern)
-				pattern = NULL;
-			break;
-		}
-		if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet")) {
-			quiet = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-h") || !strcmp(arg, "--head")) {
-			show_head = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-d") || !strcmp(arg, "--dereference")) {
-			deref_tags = 1;
-			continue;
-		}
-		if (!strcmp(arg, "-s") || !strcmp(arg, "--hash")) {
-			hash_only = 1;
-			continue;
-		}
-		if (!prefixcmp(arg, "--hash=") ||
-		    (!prefixcmp(arg, "--abbrev") &&
-		     (arg[8] == '=' || arg[8] == '\0'))) {
-			if (arg[2] != 'h' && !arg[8])
-				/* --abbrev only */
-				abbrev = DEFAULT_ABBREV;
-			else {
-				/* --hash= or --abbrev= */
-				char *end;
-				if (arg[2] == 'h') {
-					hash_only = 1;
-					arg += 7;
-				}
-				else
-					arg += 9;
-				abbrev = strtoul(arg, &end, 10);
-				if (*end || abbrev > 40)
-					usage(show_ref_usage);
-				if (abbrev < MINIMUM_ABBREV)
-					abbrev = MINIMUM_ABBREV;
-			}
-			continue;
-		}
-		if (!strcmp(arg, "--verify")) {
-			verify = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--tags")) {
-			tags_only = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--heads")) {
-			heads_only = 1;
-			continue;
-		}
-		if (!strcmp(arg, "--exclude-existing"))
-			return exclude_existing(NULL);
-		if (!prefixcmp(arg, "--exclude-existing="))
-			return exclude_existing(arg + 19);
-		usage(show_ref_usage);
-	}
+	argc = parse_options(argc, argv, show_ref_opts, show_ref_usage, 0);
+	if (exclude_mode)
+		return exclude_existing(*exclude_mode ? exclude_mode : NULL);
 
 	if (verify) {
-		if (!pattern)
+		if (!argc)
 			die("--verify requires a reference");
-		while (*pattern) {
+		while (*argv) {
 			unsigned char sha1[20];
 
-			if (!prefixcmp(*pattern, "refs/") &&
-			    resolve_ref(*pattern, sha1, 1, NULL)) {
+			if (!prefixcmp(*argv, "refs/") &&
+			    resolve_ref(*argv, sha1, 1, NULL)) {
 				if (!quiet)
-					show_one(*pattern, sha1);
+					show_one(*argv, sha1);
 			}
 			else if (!quiet)
-				die("'%s' - not a valid ref", *pattern);
+				die("'%s' - not a valid ref", *argv);
 			else
 				return 1;
-			pattern++;
+			argv++;
 		}
 		return 0;
 	}
 
 	if (show_head)
-		head_ref(show_ref, NULL);
-	for_each_ref(show_ref, NULL);
+		head_ref(show_ref, argc ? argv : NULL);
+	for_each_ref(show_ref, argc ? argv : NULL);
 	if (!found_match) {
 		if (verify && !quiet)
 			die("No match");
-- 
1.5.3.4.1231.g62b9a

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux