Merge branch 'ab/designated-initializers-more'
Code clean-up. * ab/designated-initializers-more: builtin/remote.c: add and use SHOW_INFO_INIT builtin/remote.c: add and use a REF_STATES_INIT urlmatch.[ch]: add and use URLMATCH_CONFIG_INIT builtin/blame.c: refactor commit_info_init() to COMMIT_INFO_INIT macro daemon.c: refactor hostinfo_init() to HOSTINFO_INIT macro
This commit is contained in:
111
builtin/remote.c
111
builtin/remote.c
@ -347,6 +347,14 @@ struct ref_states {
|
||||
int queried;
|
||||
};
|
||||
|
||||
#define REF_STATES_INIT { \
|
||||
.new_refs = STRING_LIST_INIT_DUP, \
|
||||
.stale = STRING_LIST_INIT_DUP, \
|
||||
.tracked = STRING_LIST_INIT_DUP, \
|
||||
.heads = STRING_LIST_INIT_DUP, \
|
||||
.push = STRING_LIST_INIT_DUP, \
|
||||
}
|
||||
|
||||
static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
|
||||
{
|
||||
struct ref *fetch_map = NULL, **tail = &fetch_map;
|
||||
@ -358,9 +366,6 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
|
||||
die(_("Could not get fetch map for refspec %s"),
|
||||
states->remote->fetch.raw[i]);
|
||||
|
||||
states->new_refs.strdup_strings = 1;
|
||||
states->tracked.strdup_strings = 1;
|
||||
states->stale.strdup_strings = 1;
|
||||
for (ref = fetch_map; ref; ref = ref->next) {
|
||||
if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
|
||||
string_list_append(&states->new_refs, abbrev_branch(ref->name));
|
||||
@ -409,7 +414,6 @@ static int get_push_ref_states(const struct ref *remote_refs,
|
||||
|
||||
match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE);
|
||||
|
||||
states->push.strdup_strings = 1;
|
||||
for (ref = push_map; ref; ref = ref->next) {
|
||||
struct string_list_item *item;
|
||||
struct push_info *info;
|
||||
@ -452,7 +456,6 @@ static int get_push_ref_states_noquery(struct ref_states *states)
|
||||
if (remote->mirror)
|
||||
return 0;
|
||||
|
||||
states->push.strdup_strings = 1;
|
||||
if (!remote->push.nr) {
|
||||
item = string_list_append(&states->push, _("(matching)"));
|
||||
info = item->util = xcalloc(1, sizeof(struct push_info));
|
||||
@ -486,7 +489,6 @@ static int get_head_names(const struct ref *remote_refs, struct ref_states *stat
|
||||
refspec.force = 0;
|
||||
refspec.pattern = 1;
|
||||
refspec.src = refspec.dst = "refs/heads/*";
|
||||
states->heads.strdup_strings = 1;
|
||||
get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
|
||||
matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
|
||||
fetch_map, 1);
|
||||
@ -973,26 +975,31 @@ static int get_remote_ref_states(const char *name,
|
||||
}
|
||||
|
||||
struct show_info {
|
||||
struct string_list *list;
|
||||
struct ref_states *states;
|
||||
struct string_list list;
|
||||
struct ref_states states;
|
||||
int width, width2;
|
||||
int any_rebase;
|
||||
};
|
||||
|
||||
#define SHOW_INFO_INIT { \
|
||||
.list = STRING_LIST_INIT_DUP, \
|
||||
.states = REF_STATES_INIT, \
|
||||
}
|
||||
|
||||
static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
|
||||
{
|
||||
struct show_info *info = cb_data;
|
||||
int n = strlen(item->string);
|
||||
if (n > info->width)
|
||||
info->width = n;
|
||||
string_list_insert(info->list, item->string);
|
||||
string_list_insert(&info->list, item->string);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int show_remote_info_item(struct string_list_item *item, void *cb_data)
|
||||
{
|
||||
struct show_info *info = cb_data;
|
||||
struct ref_states *states = info->states;
|
||||
struct ref_states *states = &info->states;
|
||||
const char *name = item->string;
|
||||
|
||||
if (states->queried) {
|
||||
@ -1019,7 +1026,7 @@ static int show_remote_info_item(struct string_list_item *item, void *cb_data)
|
||||
static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
|
||||
{
|
||||
struct show_info *show_info = cb_data;
|
||||
struct ref_states *states = show_info->states;
|
||||
struct ref_states *states = &show_info->states;
|
||||
struct branch_info *branch_info = branch_item->util;
|
||||
struct string_list_item *item;
|
||||
int n;
|
||||
@ -1032,7 +1039,7 @@ static int add_local_to_show_info(struct string_list_item *branch_item, void *cb
|
||||
if (branch_info->rebase >= REBASE_TRUE)
|
||||
show_info->any_rebase = 1;
|
||||
|
||||
item = string_list_insert(show_info->list, branch_item->string);
|
||||
item = string_list_insert(&show_info->list, branch_item->string);
|
||||
item->util = branch_info;
|
||||
|
||||
return 0;
|
||||
@ -1087,7 +1094,7 @@ static int add_push_to_show_info(struct string_list_item *push_item, void *cb_da
|
||||
show_info->width = n;
|
||||
if ((n = strlen(push_info->dest)) > show_info->width2)
|
||||
show_info->width2 = n;
|
||||
item = string_list_append(show_info->list, push_item->string);
|
||||
item = string_list_append(&show_info->list, push_item->string);
|
||||
item->util = push_item->util;
|
||||
return 0;
|
||||
}
|
||||
@ -1215,9 +1222,7 @@ static int show(int argc, const char **argv)
|
||||
OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
|
||||
OPT_END()
|
||||
};
|
||||
struct ref_states states;
|
||||
struct string_list info_list = STRING_LIST_INIT_NODUP;
|
||||
struct show_info info;
|
||||
struct show_info info = SHOW_INFO_INIT;
|
||||
|
||||
argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
|
||||
0);
|
||||
@ -1228,26 +1233,22 @@ static int show(int argc, const char **argv)
|
||||
if (!no_query)
|
||||
query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
|
||||
|
||||
memset(&states, 0, sizeof(states));
|
||||
memset(&info, 0, sizeof(info));
|
||||
info.states = &states;
|
||||
info.list = &info_list;
|
||||
for (; argc; argc--, argv++) {
|
||||
int i;
|
||||
const char **url;
|
||||
int url_nr;
|
||||
|
||||
get_remote_ref_states(*argv, &states, query_flag);
|
||||
get_remote_ref_states(*argv, &info.states, query_flag);
|
||||
|
||||
printf_ln(_("* remote %s"), *argv);
|
||||
printf_ln(_(" Fetch URL: %s"), states.remote->url_nr > 0 ?
|
||||
states.remote->url[0] : _("(no URL)"));
|
||||
if (states.remote->pushurl_nr) {
|
||||
url = states.remote->pushurl;
|
||||
url_nr = states.remote->pushurl_nr;
|
||||
printf_ln(_(" Fetch URL: %s"), info.states.remote->url_nr > 0 ?
|
||||
info.states.remote->url[0] : _("(no URL)"));
|
||||
if (info.states.remote->pushurl_nr) {
|
||||
url = info.states.remote->pushurl;
|
||||
url_nr = info.states.remote->pushurl_nr;
|
||||
} else {
|
||||
url = states.remote->url;
|
||||
url_nr = states.remote->url_nr;
|
||||
url = info.states.remote->url;
|
||||
url_nr = info.states.remote->url_nr;
|
||||
}
|
||||
for (i = 0; i < url_nr; i++)
|
||||
/*
|
||||
@ -1260,57 +1261,57 @@ static int show(int argc, const char **argv)
|
||||
printf_ln(_(" Push URL: %s"), _("(no URL)"));
|
||||
if (no_query)
|
||||
printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
|
||||
else if (!states.heads.nr)
|
||||
else if (!info.states.heads.nr)
|
||||
printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
|
||||
else if (states.heads.nr == 1)
|
||||
printf_ln(_(" HEAD branch: %s"), states.heads.items[0].string);
|
||||
else if (info.states.heads.nr == 1)
|
||||
printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string);
|
||||
else {
|
||||
printf(_(" HEAD branch (remote HEAD is ambiguous,"
|
||||
" may be one of the following):\n"));
|
||||
for (i = 0; i < states.heads.nr; i++)
|
||||
printf(" %s\n", states.heads.items[i].string);
|
||||
for (i = 0; i < info.states.heads.nr; i++)
|
||||
printf(" %s\n", info.states.heads.items[i].string);
|
||||
}
|
||||
|
||||
/* remote branch info */
|
||||
info.width = 0;
|
||||
for_each_string_list(&states.new_refs, add_remote_to_show_info, &info);
|
||||
for_each_string_list(&states.tracked, add_remote_to_show_info, &info);
|
||||
for_each_string_list(&states.stale, add_remote_to_show_info, &info);
|
||||
if (info.list->nr)
|
||||
for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info);
|
||||
for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info);
|
||||
for_each_string_list(&info.states.stale, add_remote_to_show_info, &info);
|
||||
if (info.list.nr)
|
||||
printf_ln(Q_(" Remote branch:%s",
|
||||
" Remote branches:%s",
|
||||
info.list->nr),
|
||||
info.list.nr),
|
||||
no_query ? _(" (status not queried)") : "");
|
||||
for_each_string_list(info.list, show_remote_info_item, &info);
|
||||
string_list_clear(info.list, 0);
|
||||
for_each_string_list(&info.list, show_remote_info_item, &info);
|
||||
string_list_clear(&info.list, 0);
|
||||
|
||||
/* git pull info */
|
||||
info.width = 0;
|
||||
info.any_rebase = 0;
|
||||
for_each_string_list(&branch_list, add_local_to_show_info, &info);
|
||||
if (info.list->nr)
|
||||
if (info.list.nr)
|
||||
printf_ln(Q_(" Local branch configured for 'git pull':",
|
||||
" Local branches configured for 'git pull':",
|
||||
info.list->nr));
|
||||
for_each_string_list(info.list, show_local_info_item, &info);
|
||||
string_list_clear(info.list, 0);
|
||||
info.list.nr));
|
||||
for_each_string_list(&info.list, show_local_info_item, &info);
|
||||
string_list_clear(&info.list, 0);
|
||||
|
||||
/* git push info */
|
||||
if (states.remote->mirror)
|
||||
if (info.states.remote->mirror)
|
||||
printf_ln(_(" Local refs will be mirrored by 'git push'"));
|
||||
|
||||
info.width = info.width2 = 0;
|
||||
for_each_string_list(&states.push, add_push_to_show_info, &info);
|
||||
QSORT(info.list->items, info.list->nr, cmp_string_with_push);
|
||||
if (info.list->nr)
|
||||
for_each_string_list(&info.states.push, add_push_to_show_info, &info);
|
||||
QSORT(info.list.items, info.list.nr, cmp_string_with_push);
|
||||
if (info.list.nr)
|
||||
printf_ln(Q_(" Local ref configured for 'git push'%s:",
|
||||
" Local refs configured for 'git push'%s:",
|
||||
info.list->nr),
|
||||
info.list.nr),
|
||||
no_query ? _(" (status not queried)") : "");
|
||||
for_each_string_list(info.list, show_push_info_item, &info);
|
||||
string_list_clear(info.list, 0);
|
||||
for_each_string_list(&info.list, show_push_info_item, &info);
|
||||
string_list_clear(&info.list, 0);
|
||||
|
||||
free_remote_ref_states(&states);
|
||||
free_remote_ref_states(&info.states);
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -1337,8 +1338,7 @@ static int set_head(int argc, const char **argv)
|
||||
if (!opt_a && !opt_d && argc == 2) {
|
||||
head_name = xstrdup(argv[1]);
|
||||
} else if (opt_a && !opt_d && argc == 1) {
|
||||
struct ref_states states;
|
||||
memset(&states, 0, sizeof(states));
|
||||
struct ref_states states = REF_STATES_INIT;
|
||||
get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
|
||||
if (!states.heads.nr)
|
||||
result |= error(_("Cannot determine remote HEAD"));
|
||||
@ -1377,14 +1377,13 @@ static int set_head(int argc, const char **argv)
|
||||
static int prune_remote(const char *remote, int dry_run)
|
||||
{
|
||||
int result = 0;
|
||||
struct ref_states states;
|
||||
struct ref_states states = REF_STATES_INIT;
|
||||
struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
|
||||
struct string_list_item *item;
|
||||
const char *dangling_msg = dry_run
|
||||
? _(" %s will become dangling!")
|
||||
: _(" %s has become dangling!");
|
||||
|
||||
memset(&states, 0, sizeof(states));
|
||||
get_remote_ref_states(remote, &states, GET_REF_STATES);
|
||||
|
||||
if (!states.stale.nr) {
|
||||
|
Reference in New Issue
Block a user