Skip to content

Commit

Permalink
show-ref: migrate to parse-options
Browse files Browse the repository at this point in the history
Also make the docs more consistent with the usage message. While we're
here remove the zero initializers from the static variables as they're
unnecessary.

Signed-off-by: Stephen Boyd <bebarino@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Stephen Boyd authored and Junio C Hamano committed Jun 21, 2009
1 parent c5764c0 commit 69932bc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 81 deletions.
15 changes: 7 additions & 8 deletions Documentation/git-show-ref.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ SYNOPSIS
--------
[verse]
'git show-ref' [-q|--quiet] [--verify] [-h|--head] [-d|--dereference]
[-s|--hash] [--abbrev] [--tags] [--heads] [--] <pattern>...
'git show-ref' --exclude-existing[=pattern]
[-s|--hash[=<n>]] [--abbrev[=<n>]] [--tags]
[--heads] [--] <pattern>...
'git show-ref' --exclude-existing[=<pattern>] < ref-list

DESCRIPTION
-----------
Expand Down Expand Up @@ -48,7 +49,7 @@ OPTIONS
appended.

-s::
--hash::
--hash[=<n>]::

Only show the SHA1 hash, not the reference name. When combined with
--dereference the dereferenced tag will still be shown after the SHA1.
Expand All @@ -59,20 +60,18 @@ OPTIONS
Aside from returning an error code of 1, it will also print an error
message if '--quiet' was not specified.

--abbrev::
--abbrev=len::
--abbrev[=<n>]::

Abbreviate the object name. When using `--hash`, you do
not have to say `--hash --abbrev`; `--hash=len` would do.
not have to say `--hash --abbrev`; `--hash=n` would do.

-q::
--quiet::

Do not print any results to stdout. When combined with '--verify' this
can be used to silently check if a reference exists.

--exclude-existing::
--exclude-existing=pattern::
--exclude-existing[=<pattern>]::

Make 'git-show-ref' act as a filter that reads refs from stdin of the
form "^(?:<anything>\s)?<refname>(?:\^\{\})?$" and performs the
Expand Down
133 changes: 60 additions & 73 deletions builtin-show-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
#include "object.h"
#include "tag.h"
#include "string-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 [-q|--quiet] [--verify] [-h|--head] [-d|--dereference] [-s|--hash[=<n>]] [--abbrev[=<n>]] [--tags] [--heads] [--] [pattern*] ",
"git show-ref --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 int deref_tags, show_head, tags_only, heads_only, found_match, verify,
quiet, hash_only, abbrev, exclude_arg;
static const char **pattern;
static const char *exclude_existing_arg;

static void show_one(const char *refname, const unsigned char *sha1)
{
Expand Down Expand Up @@ -150,79 +156,60 @@ static int exclude_existing(const char *match)
return 0;
}

static int hash_callback(const struct option *opt, const char *arg, int unset)
{
hash_only = 1;
/* Use full length SHA1 if no argument */
if (!arg)
return 0;
return parse_opt_abbrev_cb(opt, arg, unset);
}

static int exclude_existing_callback(const struct option *opt, const char *arg,
int unset)
{
exclude_arg = 1;
*(const char **)opt->value = arg;
return 0;
}

static int help_callback(const struct option *opt, const char *arg, int unset)
{
return -1;
}

static const struct option show_ref_options[] = {
OPT_BOOLEAN(0, "tags", &tags_only, "only show tags (can be combined with heads)"),
OPT_BOOLEAN(0, "heads", &heads_only, "only show heads (can be combined with tags)"),
OPT_BOOLEAN(0, "verify", &verify, "stricter reference checking, "
"requires exact ref path"),
OPT_BOOLEAN('h', "head", &show_head, "show the HEAD reference"),
OPT_BOOLEAN('d', "dereference", &deref_tags,
"dereference tags into object IDs"),
{ OPTION_CALLBACK, 's', "hash", &abbrev, "n",
"only show SHA1 hash using <n> digits",
PARSE_OPT_OPTARG, &hash_callback },
OPT__ABBREV(&abbrev),
OPT__QUIET(&quiet),
{ OPTION_CALLBACK, 0, "exclude-existing", &exclude_existing_arg,
"pattern", "show refs from stdin that aren't in local repository",
PARSE_OPT_OPTARG | PARSE_OPT_NONEG, exclude_existing_callback },
{ OPTION_CALLBACK, 0, "help-all", NULL, NULL, "show usage",
PARSE_OPT_HIDDEN | PARSE_OPT_NOARG, help_callback },
OPT_END()
};

int cmd_show_ref(int argc, const char **argv, const char *prefix)
{
int i;
argc = parse_options(argc, argv, prefix, show_ref_options,
show_ref_usage, PARSE_OPT_NO_INTERNAL_HELP);

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);
}
if (exclude_arg)
return exclude_existing(exclude_existing_arg);

pattern = argv;
if (!*pattern)
pattern = NULL;

if (verify) {
if (!pattern)
Expand Down

0 comments on commit 69932bc

Please sign in to comment.