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>

Brandon Williams committed May 16, 2018 at 15:58 UTC 6bdb304b106db32b1cff185f2fa1b79e9c2c919c
4 files changed +28 -37
builtin/push.c
+5 -5
@@ -79,11 +79,11 @@ static const char *map_refspec(const char *ref,
79 if (count_refspec_match(ref, local_refs, &matched) != 1)
80 return ref;
81
82 - if (remote->push) {
82 + if (remote->push.nr) {
83 struct refspec_item query;
84 memset(&query, 0, sizeof(struct refspec_item));
85 query.src = matched->name;
86 - if (!query_refspecs(remote->push, remote->push_refspec_nr, &query) &&
86 + if (!query_refspecs(remote->push.items, remote->push.nr, &query) &&
87 query.dst) {
88 struct strbuf buf = STRBUF_INIT;
89 strbuf_addf(&buf, "%s%s:%s",
@@ -436,9 +436,9 @@ static int do_push(const char *repo, int flags,
436 }
437
438 if (!refspec && !(flags & TRANSPORT_PUSH_ALL)) {
439 - if (remote->push_refspec_nr) {
440 - refspec = remote->push_refspec;
441 - refspec_nr = remote->push_refspec_nr;
439 + if (remote->push.raw_nr) {
440 + refspec = remote->push.raw;
441 + refspec_nr = remote->push.raw_nr;
442 } else if (!(flags & TRANSPORT_PUSH_MIRROR))
443 setup_default_push_refspecs(remote);
444 }
builtin/remote.c
+7 -7
@@ -388,8 +388,8 @@ static int get_push_ref_states(const struct ref *remote_refs,
388 local_refs = get_local_heads();
389 push_map = copy_ref_list(remote_refs);
390
391 - match_push_refs(local_refs, &push_map, remote->push_refspec_nr,
392 - remote->push_refspec, MATCH_REFS_NONE);
391 + match_push_refs(local_refs, &push_map, remote->push.raw_nr,
392 + remote->push.raw, MATCH_REFS_NONE);
393
394 states->push.strdup_strings = 1;
395 for (ref = push_map; ref; ref = ref->next) {
@@ -435,14 +435,14 @@ static int get_push_ref_states_noquery(struct ref_states *states)
435 return 0;
436
437 states->push.strdup_strings = 1;
438 - if (!remote->push_refspec_nr) {
438 + if (!remote->push.nr) {
439 item = string_list_append(&states->push, _("(matching)"));
440 info = item->util = xcalloc(1, sizeof(struct push_info));
441 info->status = PUSH_STATUS_NOTQUERIED;
442 info->dest = xstrdup(item->string);
443 }
444 - for (i = 0; i < remote->push_refspec_nr; i++) {
445 - struct refspec_item *spec = remote->push + i;
444 + for (i = 0; i < remote->push.nr; i++) {
445 + const struct refspec_item *spec = &remote->push.items[i];
446 if (spec->matching)
447 item = string_list_append(&states->push, _("(matching)"));
448 else if (strlen(spec->src))
@@ -586,8 +586,8 @@ static int migrate_file(struct remote *remote)
586 git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
587 strbuf_reset(&buf);
588 strbuf_addf(&buf, "remote.%s.push", remote->name);
589 - for (i = 0; i < remote->push_refspec_nr; i++)
590 - git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0);
589 + for (i = 0; i < remote->push.raw_nr; i++)
590 + git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0);
591 strbuf_reset(&buf);
592 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
593 for (i = 0; i < remote->fetch_refspec_nr; i++)
remote.c
+14 -21
@@ -77,14 +77,6 @@ static const char *alias_url(const char *url, struct rewrites *r)
77 return xstrfmt("%s%s", r->rewrite[longest_i]->base, url + longest->len);
78 }
79
80 -static void add_push_refspec(struct remote *remote, const char *ref)
81 -{
82 - ALLOC_GROW(remote->push_refspec,
83 - remote->push_refspec_nr + 1,
84 - remote->push_refspec_alloc);
85 - remote->push_refspec[remote->push_refspec_nr++] = ref;
86 -}
87 -
80 static void add_fetch_refspec(struct remote *remote, const char *ref)
81 {
82 ALLOC_GROW(remote->fetch_refspec,
@@ -175,9 +167,11 @@ static struct remote *make_remote(const char *name, int len)
167 ret = xcalloc(1, sizeof(struct remote));
168 ret->prune = -1; /* unspecified */
169 ret->prune_tags = -1; /* unspecified */
170 + ret->name = xstrndup(name, len);
171 + refspec_init(&ret->push, REFSPEC_PUSH);
172 +
173 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
174 remotes[remotes_nr++] = ret;
180 - ret->name = xstrndup(name, len);
175
176 hashmap_entry_init(ret, lookup_entry.hash);
177 replaced = hashmap_put(&remotes_hash, ret);
@@ -275,7 +269,7 @@ static void read_remotes_file(struct remote *remote)
269 if (skip_prefix(buf.buf, "URL:", &v))
270 add_url_alias(remote, xstrdup(skip_spaces(v)));
271 else if (skip_prefix(buf.buf, "Push:", &v))
278 - add_push_refspec(remote, xstrdup(skip_spaces(v)));
272 + refspec_append(&remote->push, skip_spaces(v));
273 else if (skip_prefix(buf.buf, "Pull:", &v))
274 add_fetch_refspec(remote, xstrdup(skip_spaces(v)));
275 }
@@ -323,8 +317,10 @@ static void read_branches_file(struct remote *remote)
317 * Cogito compatible push: push current HEAD to remote #branch
318 * (master if missing)
319 */
326 - add_push_refspec(remote, xstrfmt("HEAD:refs/heads/%s", frag));
320 + strbuf_addf(&buf, "HEAD:refs/heads/%s", frag);
321 + refspec_append(&remote->push, buf.buf);
322 remote->fetch_tags = 1; /* always auto-follow */
323 + strbuf_release(&buf);
324 }
325
326 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)
405 const char *v;
406 if (git_config_string(&v, key, value))
407 return -1;
412 - add_push_refspec(remote, v);
408 + refspec_append(&remote->push, v);
409 + free((char *)v);
410 } else if (!strcmp(subkey, "fetch")) {
411 const char *v;
412 if (git_config_string(&v, key, value))
@@ -542,9 +539,9 @@ const char *remote_ref_for_branch(struct branch *branch, int for_push,
539 pushremote_for_branch(branch, NULL);
540 struct remote *remote = remote_get(remote_name);
541
545 - if (remote && remote->push_refspec_nr &&
546 - (dst = apply_refspecs(remote->push,
547 - remote->push_refspec_nr,
542 + if (remote && remote->push.nr &&
543 + (dst = apply_refspecs(remote->push.items,
544 + remote->push.nr,
545 branch->refname))) {
546 if (explicit)
547 *explicit = 1;
@@ -582,7 +579,6 @@ static struct remote *remote_get_1(const char *name,
579 if (!valid_remote(ret))
580 return NULL;
581 ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
585 - ret->push = parse_push_refspec(ret->push_refspec_nr, ret->push_refspec);
582 return ret;
583 }
584
@@ -616,9 +612,6 @@ int for_each_remote(each_remote_fn fn, void *priv)
612 if (!r->fetch)
613 r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
614 r->fetch_refspec);
619 - if (!r->push)
620 - r->push = parse_push_refspec(r->push_refspec_nr,
621 - r->push_refspec);
615 result = fn(r, priv);
616 }
617 return result;
@@ -1613,11 +1606,11 @@ static const char *branch_get_push_1(struct branch *branch, struct strbuf *err)
1606 _("branch '%s' has no remote for pushing"),
1607 branch->name);
1608
1616 - if (remote->push_refspec_nr) {
1609 + if (remote->push.nr) {
1610 char *dst;
1611 const char *ret;
1612
1620 - dst = apply_refspecs(remote->push, remote->push_refspec_nr,
1613 + dst = apply_refspecs(remote->push.items, remote->push.nr,
1614 branch->refname);
1615 if (!dst)
1616 return error_buf(err,
remote.h
+2 -4
@@ -3,6 +3,7 @@
3
4 #include "parse-options.h"
5 #include "hashmap.h"
6 +#include "refspec.h"
7
8 enum {
9 REMOTE_UNCONFIGURED = 0,
@@ -27,10 +28,7 @@ struct remote {
28 int pushurl_nr;
29 int pushurl_alloc;
30
30 - const char **push_refspec;
31 - struct refspec_item *push;
32 - int push_refspec_nr;
33 - int push_refspec_alloc;
31 + struct refspec push;
32
33 const char **fetch_refspec;
34 struct refspec_item *fetch;