remote: convert push refspecs to struct refspec

Convert the set of push refspecs stored in 'struct remote' to use
'struct refspec'.

Signed-off-by: Brandon Williams <bmwill@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Brandon Williams 2018-05-16 15:58:00 -07:00 committed by Junio C Hamano
parent 16eefc8eb3
commit 6bdb304b10
4 changed files with 28 additions and 37 deletions

View File

@ -79,11 +79,11 @@ static const char *map_refspec(const char *ref,
if (count_refspec_match(ref, local_refs, &matched) != 1) if (count_refspec_match(ref, local_refs, &matched) != 1)
return ref; return ref;
if (remote->push) { if (remote->push.nr) {
struct refspec_item query; struct refspec_item query;
memset(&query, 0, sizeof(struct refspec_item)); memset(&query, 0, sizeof(struct refspec_item));
query.src = matched->name; query.src = matched->name;
if (!query_refspecs(remote->push, remote->push_refspec_nr, &query) && if (!query_refspecs(remote->push.items, remote->push.nr, &query) &&
query.dst) { query.dst) {
struct strbuf buf = STRBUF_INIT; struct strbuf buf = STRBUF_INIT;
strbuf_addf(&buf, "%s%s:%s", strbuf_addf(&buf, "%s%s:%s",
@ -436,9 +436,9 @@ static int do_push(const char *repo, int flags,
} }
if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) { if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
if (remote->push_refspec_nr) { if (remote->push.raw_nr) {
refspec = remote->push_refspec; refspec = remote->push.raw;
refspec_nr = remote->push_refspec_nr; refspec_nr = remote->push.raw_nr;
} else if (!(flags & TRANSPORT_PUSH_MIRROR)) } else if (!(flags & TRANSPORT_PUSH_MIRROR))
setup_default_push_refspecs(remote); setup_default_push_refspecs(remote);
} }

View File

@ -388,8 +388,8 @@ static int get_push_ref_states(const struct ref *remote_refs,
local_refs = get_local_heads(); local_refs = get_local_heads();
push_map = copy_ref_list(remote_refs); push_map = copy_ref_list(remote_refs);
match_push_refs(local_refs, &push_map, remote->push_refspec_nr, match_push_refs(local_refs, &push_map, remote->push.raw_nr,
remote->push_refspec, MATCH_REFS_NONE); remote->push.raw, MATCH_REFS_NONE);
states->push.strdup_strings = 1; states->push.strdup_strings = 1;
for (ref = push_map; ref; ref = ref->next) { for (ref = push_map; ref; ref = ref->next) {
@ -435,14 +435,14 @@ static int get_push_ref_states_noquery(struct ref_states *states)
return 0; return 0;
states->push.strdup_strings = 1; states->push.strdup_strings = 1;
if (!remote->push_refspec_nr) { if (!remote->push.nr) {
item = string_list_append(&states->push, _("(matching)")); item = string_list_append(&states->push, _("(matching)"));
info = item->util = xcalloc(1, sizeof(struct push_info)); info = item->util = xcalloc(1, sizeof(struct push_info));
info->status = PUSH_STATUS_NOTQUERIED; info->status = PUSH_STATUS_NOTQUERIED;
info->dest = xstrdup(item->string); info->dest = xstrdup(item->string);
} }
for (i = 0; i < remote->push_refspec_nr; i++) { for (i = 0; i < remote->push.nr; i++) {
struct refspec_item *spec = remote->push + i; const struct refspec_item *spec = &remote->push.items[i];
if (spec->matching) if (spec->matching)
item = string_list_append(&states->push, _("(matching)")); item = string_list_append(&states->push, _("(matching)"));
else if (strlen(spec->src)) else if (strlen(spec->src))
@ -586,8 +586,8 @@ static int migrate_file(struct remote *remote)
git_config_set_multivar(buf.buf, remote->url[i], "^$", 0); git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
strbuf_reset(&buf); strbuf_reset(&buf);
strbuf_addf(&buf, "remote.%s.push", remote->name); strbuf_addf(&buf, "remote.%s.push", remote->name);
for (i = 0; i < remote->push_refspec_nr; i++) for (i = 0; i < remote->push.raw_nr; i++)
git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0); git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0);
strbuf_reset(&buf); strbuf_reset(&buf);
strbuf_addf(&buf, "remote.%s.fetch", remote->name); strbuf_addf(&buf, "remote.%s.fetch", remote->name);
for (i = 0; i < remote->fetch_refspec_nr; i++) for (i = 0; i < remote->fetch_refspec_nr; i++)

View File

@ -77,14 +77,6 @@ static const char *alias_url(const char *url, struct rewrites *r)
return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len); return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
} }
static void add_push_refspec(struct remote *remote, const char *ref)
{
ALLOC_GROW(remote->push_refspec,
remote->push_refspec_nr + 1,
remote->push_refspec_alloc);
remote->push_refspec[remote->push_refspec_nr++] = ref;
}
static void add_fetch_refspec(struct remote *remote, const char *ref) static void add_fetch_refspec(struct remote *remote, const char *ref)
{ {
ALLOC_GROW(remote->fetch_refspec, ALLOC_GROW(remote->fetch_refspec,
@ -175,9 +167,11 @@ static struct remote *make_remote(const char *name, int len)
ret = xcalloc(1, sizeof(struct remote)); ret = xcalloc(1, sizeof(struct remote));
ret->prune = -1; /* unspecified */ ret->prune = -1; /* unspecified */
ret->prune_tags = -1; /* unspecified */ ret->prune_tags = -1; /* unspecified */
ret->name = xstrndup(name, len);
refspec_init(&ret->push, REFSPEC_PUSH);
ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc); ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
remotes[remotes_nr++] = ret; remotes[remotes_nr++] = ret;
ret->name = xstrndup(name, len);
hashmap_entry_init(ret, lookup_entry.hash); hashmap_entry_init(ret, lookup_entry.hash);
replaced = hashmap_put(&remotes_hash, ret); replaced = hashmap_put(&remotes_hash, ret);
@ -275,7 +269,7 @@ static void read_remotes_file(struct remote *remote)
if (skip_prefix(buf.buf, "URL:", &v)) if (skip_prefix(buf.buf, "URL:", &v))
add_url_alias(remote, xstrdup(skip_spaces(v))); add_url_alias(remote, xstrdup(skip_spaces(v)));
else if (skip_prefix(buf.buf, "Push:", &v)) else if (skip_prefix(buf.buf, "Push:", &v))
add_push_refspec(remote, xstrdup(skip_spaces(v))); refspec_append(&remote->push, skip_spaces(v));
else if (skip_prefix(buf.buf, "Pull:", &v)) else if (skip_prefix(buf.buf, "Pull:", &v))
add_fetch_refspec(remote, xstrdup(skip_spaces(v))); add_fetch_refspec(remote, xstrdup(skip_spaces(v)));
} }
@ -323,8 +317,10 @@ static void read_branches_file(struct remote *remote)
* Cogito compatible push: push current HEAD to remote #branch * Cogito compatible push: push current HEAD to remote #branch
* (master if missing) * (master if missing)
*/ */
add_push_refspec(remote, xstrfmt("HEAD:refs/heads/%s", frag)); strbuf_addf(&buf, "HEAD:refs/heads/%s", frag);
refspec_append(&remote->push, buf.buf);
remote->fetch_tags = 1; /* always auto-follow */ remote->fetch_tags = 1; /* always auto-follow */
strbuf_release(&buf);
} }
static int handle_config(const char *key, const char *value, void *cb) static int handle_config(const char *key, const char *value, void *cb)
@ -409,7 +405,8 @@ static int handle_config(const char *key, const char *value, void *cb)
const char *v; const char *v;
if (git_config_string(&v, key, value)) if (git_config_string(&v, key, value))
return -1; return -1;
add_push_refspec(remote, v); refspec_append(&remote->push, v);
free((char *)v);
} else if (!strcmp(subkey, "fetch")) { } else if (!strcmp(subkey, "fetch")) {
const char *v; const char *v;
if (git_config_string(&v, key, value)) if (git_config_string(&v, key, value))
@ -542,9 +539,9 @@ const char *remote_ref_for_branch(struct branch *branch, int for_push,
pushremote_for_branch(branch, NULL); pushremote_for_branch(branch, NULL);
struct remote *remote = remote_get(remote_name); struct remote *remote = remote_get(remote_name);
if (remote && remote->push_refspec_nr && if (remote && remote->push.nr &&
(dst = apply_refspecs(remote->push, (dst = apply_refspecs(remote->push.items,
remote->push_refspec_nr, remote->push.nr,
branch->refname))) { branch->refname))) {
if (explicit) if (explicit)
*explicit = 1; *explicit = 1;
@ -582,7 +579,6 @@ static struct remote *remote_get_1(const char *name,
if (!valid_remote(ret)) if (!valid_remote(ret))
return NULL; return NULL;
ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec); ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
return ret; return ret;
} }
@ -616,9 +612,6 @@ int for_each_remote(each_remote_fn fn, void *priv)
if (!r->fetch) if (!r->fetch)
r->fetch = parse_fetch_refspec(r->fetch_refspec_nr, r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
r->fetch_refspec); r->fetch_refspec);
if (!r->push)
r->push = parse_push_refspec(r->push_refspec_nr,
r->push_refspec);
result = fn(r, priv); result = fn(r, priv);
} }
return result; return result;
@ -1613,11 +1606,11 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
_("branch '%s' has no remote for pushing"), _("branch '%s' has no remote for pushing"),
branch->name); branch->name);
if (remote->push_refspec_nr) { if (remote->push.nr) {
char *dst; char *dst;
const char *ret; const char *ret;
dst = apply_refspecs(remote->push, remote->push_refspec_nr, dst = apply_refspecs(remote->push.items, remote->push.nr,
branch->refname); branch->refname);
if (!dst) if (!dst)
return error_buf(err, return error_buf(err,

View File

@ -3,6 +3,7 @@
#include "parse-options.h" #include "parse-options.h"
#include "hashmap.h" #include "hashmap.h"
#include "refspec.h"
enum { enum {
REMOTE_UNCONFIGURED = 0, REMOTE_UNCONFIGURED = 0,
@ -27,10 +28,7 @@ struct remote {
int pushurl_nr; int pushurl_nr;
int pushurl_alloc; int pushurl_alloc;
const char **push_refspec; struct refspec push;
struct refspec_item *push;
int push_refspec_nr;
int push_refspec_alloc;
const char **fetch_refspec; const char **fetch_refspec;
struct refspec_item *fetch; struct refspec_item *fetch;