{"thread":{"id":"19881","subject":"[PATCH 2/2] show-ref: migrate to parse-options","startedAt":"2009-06-21T04:40:45Z","lastAt":"2009-06-21T04:40:46Z","messageCount":2,"participants":["Stephen Boyd"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"116689","messageId":"1245559246-11039-1-git-send-email-bebarino@gmail.com","threadId":"19881","inReplyTo":null,"subject":"[PATCH 1/2] git-show-ref.txt: remove word and make consistent","fromName":"Stephen Boyd","fromEmail":"bebarino@gmail.com","sentAt":"2009-06-21T04:40:45Z","receivedAt":"2009-06-21T04:40:45Z","isPatch":true,"sender":{"key":"bebarino@gmail.com","avatar":"https://avatars.githubusercontent.com/u/38832?v=4"},"body":"Under is better than in because of the nested nature of the .git\ndirectory.\n\n\"also using\" sounds a little odd, plus we say combined with later on so\njust use that.\n\nSigned-off-by: Stephen Boyd <bebarino@gmail.com>\n---\n Documentation/git-show-ref.txt |    4 ++--\n 1 files changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/Documentation/git-show-ref.txt b/Documentation/git-show-ref.txt\nindex 2f173ff..98e294a 100644\n--- a/Documentation/git-show-ref.txt\n+++ b/Documentation/git-show-ref.txt\n@@ -24,7 +24,7 @@ The --exclude-existing form is a filter that does the inverse, it shows the\n refs from stdin that don't exist in the local repository.\n \n Use of this utility is encouraged in favor of directly accessing files under\n-in the `.git` directory.\n+the `.git` directory.\n \n OPTIONS\n -------\n@@ -50,7 +50,7 @@ OPTIONS\n -s::\n --hash::\n \n-\tOnly show the SHA1 hash, not the reference name. When also using\n+\tOnly show the SHA1 hash, not the reference name. When combined with\n \t--dereference the dereferenced tag will still be shown after the SHA1.\n \n --verify::\n-- \n1.6.3.2.316.gda4e\n"},{"id":"116688","messageId":"1245559246-11039-2-git-send-email-bebarino@gmail.com","threadId":"19881","inReplyTo":"1245559246-11039-1-git-send-email-bebarino@gmail.com","subject":"[PATCH 2/2] show-ref: migrate to parse-options","fromName":"Stephen Boyd","fromEmail":"bebarino@gmail.com","sentAt":"2009-06-21T04:40:46Z","receivedAt":"2009-06-21T04:40:46Z","isPatch":true,"sender":{"key":"bebarino@gmail.com","avatar":"https://avatars.githubusercontent.com/u/38832?v=4"},"body":"Also make the docs more consistent with the usage message. While we're\nhere remove the zero initializers from the static variables as they're\nunnecessary.\n\nSigned-off-by: Stephen Boyd <bebarino@gmail.com>\n---\n Documentation/git-show-ref.txt |   15 ++---\n builtin-show-ref.c             |  133 ++++++++++++++++++----------------------\n 2 files changed, 67 insertions(+), 81 deletions(-)\n\ndiff --git a/Documentation/git-show-ref.txt b/Documentation/git-show-ref.txt\nindex 98e294a..f4429bd 100644\n--- a/Documentation/git-show-ref.txt\n+++ b/Documentation/git-show-ref.txt\n@@ -9,8 +9,9 @@ SYNOPSIS\n --------\n [verse]\n 'git show-ref' [-q|--quiet] [--verify] [-h|--head] [-d|--dereference]\n-\t     [-s|--hash] [--abbrev] [--tags] [--heads] [--] <pattern>...\n-'git show-ref' --exclude-existing[=pattern]\n+\t     [-s|--hash[=<n>]] [--abbrev[=<n>]] [--tags]\n+\t     [--heads] [--] <pattern>...\n+'git show-ref' --exclude-existing[=<pattern>] < ref-list\n \n DESCRIPTION\n -----------\n@@ -48,7 +49,7 @@ OPTIONS\n \tappended.\n \n -s::\n---hash::\n+--hash[=<n>]::\n \n \tOnly show the SHA1 hash, not the reference name. When combined with\n \t--dereference the dereferenced tag will still be shown after the SHA1.\n@@ -59,11 +60,10 @@ OPTIONS\n \tAside from returning an error code of 1, it will also print an error\n \tmessage if '--quiet' was not specified.\n \n---abbrev::\n---abbrev=len::\n+--abbrev[=<n>]::\n \n \tAbbreviate the object name.  When using `--hash`, you do\n-\tnot have to say `--hash --abbrev`; `--hash=len` would do.\n+\tnot have to say `--hash --abbrev`; `--hash=n` would do.\n \n -q::\n --quiet::\n@@ -71,8 +71,7 @@ OPTIONS\n \tDo not print any results to stdout. When combined with '--verify' this\n \tcan be used to silently check if a reference exists.\n \n---exclude-existing::\n---exclude-existing=pattern::\n+--exclude-existing[=<pattern>]::\n \n \tMake 'git-show-ref' act as a filter that reads refs from stdin of the\n \tform \"^(?:<anything>\\s)?<refname>(?:\\^\\{\\})?$\" and performs the\ndiff --git a/builtin-show-ref.c b/builtin-show-ref.c\nindex dc76c50..c46550c 100644\n--- a/builtin-show-ref.c\n+++ b/builtin-show-ref.c\n@@ -4,12 +4,18 @@\n #include \"object.h\"\n #include \"tag.h\"\n #include \"string-list.h\"\n+#include \"parse-options.h\"\n \n-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\";\n+static const char * const show_ref_usage[] = {\n+\t\"git show-ref [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [pattern*] \",\n+\t\"git show-ref --exclude-existing[=pattern] < ref-list\",\n+\tNULL\n+};\n \n-static int deref_tags = 0, show_head = 0, tags_only = 0, heads_only = 0,\n-\tfound_match = 0, verify = 0, quiet = 0, hash_only = 0, abbrev = 0;\n+static int deref_tags, show_head, tags_only, heads_only, found_match, verify,\n+\t   quiet, hash_only, abbrev, exclude_arg;\n static const char **pattern;\n+static const char *exclude_existing_arg;\n \n static void show_one(const char *refname, const unsigned char *sha1)\n {\n@@ -150,79 +156,60 @@ static int exclude_existing(const char *match)\n \treturn 0;\n }\n \n+static int hash_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\thash_only = 1;\n+\t/* Use full length SHA1 if no argument */\n+\tif (!arg)\n+\t\treturn 0;\n+\treturn parse_opt_abbrev_cb(opt, arg, unset);\n+}\n+\n+static int exclude_existing_callback(const struct option *opt, const char *arg,\n+\t\t\t\t     int unset)\n+{\n+\texclude_arg = 1;\n+\t*(const char **)opt->value = arg;\n+\treturn 0;\n+}\n+\n+static int help_callback(const struct option *opt, const char *arg, int unset)\n+{\n+\treturn -1;\n+}\n+\n+static const struct option show_ref_options[] = {\n+\tOPT_BOOLEAN(0, \"tags\", &tags_only, \"only show tags (can be combined with heads)\"),\n+\tOPT_BOOLEAN(0, \"heads\", &heads_only, \"only show heads (can be combined with tags)\"),\n+\tOPT_BOOLEAN(0, \"verify\", &verify, \"stricter reference checking, \"\n+\t\t    \"requires exact ref path\"),\n+\tOPT_BOOLEAN('h', \"head\", &show_head, \"show the HEAD reference\"),\n+\tOPT_BOOLEAN('d', \"dereference\", &deref_tags,\n+\t\t    \"dereference tags into object IDs\"),\n+\t{ OPTION_CALLBACK, 's', \"hash\", &abbrev, \"n\",\n+\t  \"only show SHA1 hash using <n> digits\",\n+\t  PARSE_OPT_OPTARG, &hash_callback },\n+\tOPT__ABBREV(&abbrev),\n+\tOPT__QUIET(&quiet),\n+\t{ OPTION_CALLBACK, 0, \"exclude-existing\", &exclude_existing_arg,\n+\t  \"pattern\", \"show refs from stdin that aren't in local repository\",\n+\t  PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback },\n+\t{ OPTION_CALLBACK, 0, \"help-all\", NULL, NULL, \"show usage\",\n+\t  PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },\n+\tOPT_END()\n+};\n+\n int cmd_show_ref(int argc, const char **argv, const char *prefix)\n {\n-\tint i;\n+\targc = parse_options(argc, argv, prefix, show_ref_options,\n+\t\t\t     show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP);\n \n-\tfor (i = 1; i < argc; i++) {\n-\t\tconst char *arg = argv[i];\n-\t\tif (*arg != '-') {\n-\t\t\tpattern = argv + i;\n-\t\t\tbreak;\n-\t\t}\n-\t\tif (!strcmp(arg, \"--\")) {\n-\t\t\tpattern = argv + i + 1;\n-\t\t\tif (!*pattern)\n-\t\t\t\tpattern = NULL;\n-\t\t\tbreak;\n-\t\t}\n-\t\tif (!strcmp(arg, \"-q\") || !strcmp(arg, \"--quiet\")) {\n-\t\t\tquiet = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(arg, \"-h\") || !strcmp(arg, \"--head\")) {\n-\t\t\tshow_head = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(arg, \"-d\") || !strcmp(arg, \"--dereference\")) {\n-\t\t\tderef_tags = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(arg, \"-s\") || !strcmp(arg, \"--hash\")) {\n-\t\t\thash_only = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!prefixcmp(arg, \"--hash=\") ||\n-\t\t    (!prefixcmp(arg, \"--abbrev\") &&\n-\t\t     (arg[8] == '=' || arg[8] == '\\0'))) {\n-\t\t\tif (arg[2] != 'h' && !arg[8])\n-\t\t\t\t/* --abbrev only */\n-\t\t\t\tabbrev = DEFAULT_ABBREV;\n-\t\t\telse {\n-\t\t\t\t/* --hash= or --abbrev= */\n-\t\t\t\tchar *end;\n-\t\t\t\tif (arg[2] == 'h') {\n-\t\t\t\t\thash_only = 1;\n-\t\t\t\t\targ += 7;\n-\t\t\t\t}\n-\t\t\t\telse\n-\t\t\t\t\targ += 9;\n-\t\t\t\tabbrev = strtoul(arg, &end, 10);\n-\t\t\t\tif (*end || abbrev > 40)\n-\t\t\t\t\tusage(show_ref_usage);\n-\t\t\t\tif (abbrev < MINIMUM_ABBREV)\n-\t\t\t\t\tabbrev = MINIMUM_ABBREV;\n-\t\t\t}\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(arg, \"--verify\")) {\n-\t\t\tverify = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(arg, \"--tags\")) {\n-\t\t\ttags_only = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(arg, \"--heads\")) {\n-\t\t\theads_only = 1;\n-\t\t\tcontinue;\n-\t\t}\n-\t\tif (!strcmp(arg, \"--exclude-existing\"))\n-\t\t\treturn exclude_existing(NULL);\n-\t\tif (!prefixcmp(arg, \"--exclude-existing=\"))\n-\t\t\treturn exclude_existing(arg + 19);\n-\t\tusage(show_ref_usage);\n-\t}\n+\tif (exclude_arg)\n+\t\treturn exclude_existing(exclude_existing_arg);\n+\n+\tpattern = argv;\n+\tif (!*pattern)\n+\t\tpattern = NULL;\n \n \tif (verify) {\n \t\tif (!pattern)\n-- \n1.6.3.2.316.gda4e\n"}]}