mirror of
https://github.com/git/git
synced 2024-08-29 04:29:22 +00:00
builtin/show-ref: convert pattern to a local variable
The `pattern` variable is a global variable that tracks either the reference names (not patterns!) for the `--verify` mode or the patterns for the non-verify mode. This is a bit confusing due to the slightly different meanings. Convert the variable to be local. While this does not yet fix the double meaning of the variable, this change allows us to address it in a subsequent patch more easily by explicitly splitting up the different subcommands of git-show-ref(1). Note that we introduce a `struct show_ref_data` to pass the patterns to `show_ref()`. While this is overengineered now, we will extend this structure in a subsequent patch. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
2e8e77cbac
commit
ff546ebb59
|
@ -20,7 +20,6 @@ static const char * const show_ref_usage[] = {
|
||||||
|
|
||||||
static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
|
static int deref_tags, show_head, tags_only, heads_only, found_match, verify,
|
||||||
quiet, hash_only, abbrev, exclude_arg;
|
quiet, hash_only, abbrev, exclude_arg;
|
||||||
static const char **pattern;
|
|
||||||
static const char *exclude_existing_arg;
|
static const char *exclude_existing_arg;
|
||||||
|
|
||||||
static void show_one(const char *refname, const struct object_id *oid)
|
static void show_one(const char *refname, const struct object_id *oid)
|
||||||
|
@ -50,15 +49,21 @@ static void show_one(const char *refname, const struct object_id *oid)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct show_ref_data {
|
||||||
|
const char **patterns;
|
||||||
|
};
|
||||||
|
|
||||||
static int show_ref(const char *refname, const struct object_id *oid,
|
static int show_ref(const char *refname, const struct object_id *oid,
|
||||||
int flag UNUSED, void *cbdata UNUSED)
|
int flag UNUSED, void *cbdata)
|
||||||
{
|
{
|
||||||
|
struct show_ref_data *data = cbdata;
|
||||||
|
|
||||||
if (show_head && !strcmp(refname, "HEAD"))
|
if (show_head && !strcmp(refname, "HEAD"))
|
||||||
goto match;
|
goto match;
|
||||||
|
|
||||||
if (pattern) {
|
if (data->patterns) {
|
||||||
int reflen = strlen(refname);
|
int reflen = strlen(refname);
|
||||||
const char **p = pattern, *m;
|
const char **p = data->patterns, *m;
|
||||||
while ((m = *p++) != NULL) {
|
while ((m = *p++) != NULL) {
|
||||||
int len = strlen(m);
|
int len = strlen(m);
|
||||||
if (len > reflen)
|
if (len > reflen)
|
||||||
|
@ -180,6 +185,9 @@ static const struct option show_ref_options[] = {
|
||||||
|
|
||||||
int cmd_show_ref(int argc, const char **argv, const char *prefix)
|
int cmd_show_ref(int argc, const char **argv, const char *prefix)
|
||||||
{
|
{
|
||||||
|
struct show_ref_data show_ref_data = {0};
|
||||||
|
const char **patterns;
|
||||||
|
|
||||||
git_config(git_default_config, NULL);
|
git_config(git_default_config, NULL);
|
||||||
|
|
||||||
argc = parse_options(argc, argv, prefix, show_ref_options,
|
argc = parse_options(argc, argv, prefix, show_ref_options,
|
||||||
|
@ -188,38 +196,40 @@ int cmd_show_ref(int argc, const char **argv, const char *prefix)
|
||||||
if (exclude_arg)
|
if (exclude_arg)
|
||||||
return exclude_existing(exclude_existing_arg);
|
return exclude_existing(exclude_existing_arg);
|
||||||
|
|
||||||
pattern = argv;
|
patterns = argv;
|
||||||
if (!*pattern)
|
if (!*patterns)
|
||||||
pattern = NULL;
|
patterns = NULL;
|
||||||
|
|
||||||
if (verify) {
|
if (verify) {
|
||||||
if (!pattern)
|
if (!patterns)
|
||||||
die("--verify requires a reference");
|
die("--verify requires a reference");
|
||||||
while (*pattern) {
|
while (*patterns) {
|
||||||
struct object_id oid;
|
struct object_id oid;
|
||||||
|
|
||||||
if ((starts_with(*pattern, "refs/") || !strcmp(*pattern, "HEAD")) &&
|
if ((starts_with(*patterns, "refs/") || !strcmp(*patterns, "HEAD")) &&
|
||||||
!read_ref(*pattern, &oid)) {
|
!read_ref(*patterns, &oid)) {
|
||||||
show_one(*pattern, &oid);
|
show_one(*patterns, &oid);
|
||||||
}
|
}
|
||||||
else if (!quiet)
|
else if (!quiet)
|
||||||
die("'%s' - not a valid ref", *pattern);
|
die("'%s' - not a valid ref", *patterns);
|
||||||
else
|
else
|
||||||
return 1;
|
return 1;
|
||||||
pattern++;
|
patterns++;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
show_ref_data.patterns = patterns;
|
||||||
|
|
||||||
if (show_head)
|
if (show_head)
|
||||||
head_ref(show_ref, NULL);
|
head_ref(show_ref, &show_ref_data);
|
||||||
if (heads_only || tags_only) {
|
if (heads_only || tags_only) {
|
||||||
if (heads_only)
|
if (heads_only)
|
||||||
for_each_fullref_in("refs/heads/", show_ref, NULL);
|
for_each_fullref_in("refs/heads/", show_ref, &show_ref_data);
|
||||||
if (tags_only)
|
if (tags_only)
|
||||||
for_each_fullref_in("refs/tags/", show_ref, NULL);
|
for_each_fullref_in("refs/tags/", show_ref, &show_ref_data);
|
||||||
} else {
|
} else {
|
||||||
for_each_ref(show_ref, NULL);
|
for_each_ref(show_ref, &show_ref_data);
|
||||||
}
|
}
|
||||||
if (!found_match) {
|
if (!found_match) {
|
||||||
if (verify && !quiet)
|
if (verify && !quiet)
|
||||||
|
|
Loading…
Reference in a new issue