remote: convert fetch refspecs to struct refspec

Convert the set of fetch 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 e5349abf93dfde8052bbcabb4be1dba77feb7053
4 files changed +32 -49
builtin/fetch.c
+10 -10
@@ -407,8 +407,8 @@ static struct ref *get_ref_map(struct transport *transport,
407 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
408 fetch_refspec_nr = refmap_nr;
409 } else {
410 - fetch_refspec = transport->remote->fetch;
411 - fetch_refspec_nr = transport->remote->fetch_refspec_nr;
410 + fetch_refspec = transport->remote->fetch.items;
411 + fetch_refspec_nr = transport->remote->fetch.nr;
412 }
413
414 for (i = 0; i < fetch_refspec_nr; i++)
@@ -421,16 +421,16 @@ static struct ref *get_ref_map(struct transport *transport,
421 struct branch *branch = branch_get(NULL);
422 int has_merge = branch_has_merge_config(branch);
423 if (remote &&
424 - (remote->fetch_refspec_nr ||
424 + (remote->fetch.nr ||
425 /* Note: has_merge implies non-NULL branch->remote_name */
426 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
427 - for (i = 0; i < remote->fetch_refspec_nr; i++) {
428 - get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
429 - if (remote->fetch[i].dst &&
430 - remote->fetch[i].dst[0])
427 + for (i = 0; i < remote->fetch.nr; i++) {
428 + get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
429 + if (remote->fetch.items[i].dst &&
430 + remote->fetch.items[i].dst[0])
431 *autotags = 1;
432 if (!i && !has_merge && ref_map &&
433 - !remote->fetch[0].pattern)
433 + !remote->fetch.items[0].pattern)
434 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
435 }
436 /*
@@ -1166,8 +1166,8 @@ static int do_fetch(struct transport *transport,
1166 if (ref_count) {
1167 prune_refs(refs, ref_count, ref_map, transport->url);
1168 } else {
1169 - prune_refs(transport->remote->fetch,
1170 - transport->remote->fetch_refspec_nr,
1169 + prune_refs(transport->remote->fetch.items,
1170 + transport->remote->fetch.nr,
1171 ref_map,
1172 transport->url);
1173 }
builtin/remote.c
+9 -9
@@ -333,10 +333,10 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
333 struct ref *ref, *stale_refs;
334 int i;
335
336 - for (i = 0; i < states->remote->fetch_refspec_nr; i++)
337 - if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
336 + for (i = 0; i < states->remote->fetch.nr; i++)
337 + if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1))
338 die(_("Could not get fetch map for refspec %s"),
339 - states->remote->fetch_refspec[i]);
339 + states->remote->fetch.raw[i]);
340
341 states->new_refs.strdup_strings = 1;
342 states->tracked.strdup_strings = 1;
@@ -347,8 +347,8 @@ static int get_ref_states(const struct ref *remote_refs, struct ref_states *stat
347 else
348 string_list_append(&states->tracked, abbrev_branch(ref->name));
349 }
350 - stale_refs = get_stale_heads(states->remote->fetch,
351 - states->remote->fetch_refspec_nr, fetch_map);
350 + stale_refs = get_stale_heads(states->remote->fetch.items,
351 + states->remote->fetch.nr, fetch_map);
352 for (ref = stale_refs; ref; ref = ref->next) {
353 struct string_list_item *item =
354 string_list_append(&states->stale, abbrev_branch(ref->name));
@@ -590,8 +590,8 @@ static int migrate_file(struct remote *remote)
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++)
594 - git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0);
593 + for (i = 0; i < remote->fetch.raw_nr; i++)
594 + git_config_set_multivar(buf.buf, remote->fetch.raw[i], "^$", 0);
595 if (remote->origin == REMOTE_REMOTES)
596 unlink_or_warn(git_path("remotes/%s", remote->name));
597 else if (remote->origin == REMOTE_BRANCHES)
@@ -646,11 +646,11 @@ static int mv(int argc, const char **argv)
646 strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
647 git_config_set_multivar(buf.buf, NULL, NULL, 1);
648 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
649 - for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
649 + for (i = 0; i < oldremote->fetch.raw_nr; i++) {
650 char *ptr;
651
652 strbuf_reset(&buf2);
653 - strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
653 + strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
654 ptr = strstr(buf2.buf, old_remote_context.buf);
655 if (ptr) {
656 refspec_updated = 1;
remote.c
+12 -26
@@ -77,23 +77,9 @@ 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_fetch_refspec(struct remote *remote, const char *ref)
81 -{
82 - ALLOC_GROW(remote->fetch_refspec,
83 - remote->fetch_refspec_nr + 1,
84 - remote->fetch_refspec_alloc);
85 - remote->fetch_refspec[remote->fetch_refspec_nr++] = ref;
86 -}
87 -
80 void add_prune_tags_to_fetch_refspec(struct remote *remote)
81 {
90 - int nr = remote->fetch_refspec_nr;
91 - int bufsize = nr + 1;
92 - int size = sizeof(struct refspec_item);
93 -
94 - remote->fetch = xrealloc(remote->fetch, size * bufsize);
95 - memcpy(&remote->fetch[nr], tag_refspec, size);
96 - add_fetch_refspec(remote, xstrdup(TAG_REFSPEC));
82 + refspec_append(&remote->fetch, TAG_REFSPEC);
83 }
84
85 static void add_url(struct remote *remote, const char *url)
@@ -169,6 +155,7 @@ static struct remote *make_remote(const char *name, int len)
155 ret->prune_tags = -1; /* unspecified */
156 ret->name = xstrndup(name, len);
157 refspec_init(&ret->push, REFSPEC_PUSH);
158 + refspec_init(&ret->fetch, REFSPEC_FETCH);
159
160 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
161 remotes[remotes_nr++] = ret;
@@ -271,7 +258,7 @@ static void read_remotes_file(struct remote *remote)
258 else if (skip_prefix(buf.buf, "Push:", &v))
259 refspec_append(&remote->push, skip_spaces(v));
260 else if (skip_prefix(buf.buf, "Pull:", &v))
274 - add_fetch_refspec(remote, xstrdup(skip_spaces(v)));
261 + refspec_append(&remote->fetch, skip_spaces(v));
262 }
263 strbuf_release(&buf);
264 fclose(f);
@@ -310,13 +297,15 @@ static void read_branches_file(struct remote *remote)
297 frag = "master";
298
299 add_url_alias(remote, strbuf_detach(&buf, NULL));
313 - add_fetch_refspec(remote, xstrfmt("refs/heads/%s:refs/heads/%s",
314 - frag, remote->name));
300 + strbuf_addf(&buf, "refs/heads/%s:refs/heads/%s",
301 + frag, remote->name);
302 + refspec_append(&remote->fetch, buf.buf);
303
304 /*
305 * Cogito compatible push: push current HEAD to remote #branch
306 * (master if missing)
307 */
308 + strbuf_reset(&buf);
309 strbuf_addf(&buf, "HEAD:refs/heads/%s", frag);
310 refspec_append(&remote->push, buf.buf);
311 remote->fetch_tags = 1; /* always auto-follow */
@@ -411,7 +400,8 @@ static int handle_config(const char *key, const char *value, void *cb)
400 const char *v;
401 if (git_config_string(&v, key, value))
402 return -1;
414 - add_fetch_refspec(remote, v);
403 + refspec_append(&remote->fetch, v);
404 + free((char *)v);
405 } else if (!strcmp(subkey, "receivepack")) {
406 const char *v;
407 if (git_config_string(&v, key, value))
@@ -578,7 +568,6 @@ static struct remote *remote_get_1(const char *name,
568 add_url_alias(ret, name);
569 if (!valid_remote(ret))
570 return NULL;
581 - ret->fetch = parse_fetch_refspec(ret->fetch_refspec_nr, ret->fetch_refspec);
571 return ret;
572 }
573
@@ -609,9 +598,6 @@ int for_each_remote(each_remote_fn fn, void *priv)
598 struct remote *r = remotes[i];
599 if (!r)
600 continue;
612 - if (!r->fetch)
613 - r->fetch = parse_fetch_refspec(r->fetch_refspec_nr,
614 - r->fetch_refspec);
601 result = fn(r, priv);
602 }
603 return result;
@@ -790,7 +776,7 @@ char *apply_refspecs(struct refspec_item *refspecs, int nr_refspec,
776
777 int remote_find_tracking(struct remote *remote, struct refspec_item *refspec)
778 {
793 - return query_refspecs(remote->fetch, remote->fetch_refspec_nr, refspec);
779 + return query_refspecs(remote->fetch.items, remote->fetch.nr, refspec);
780 }
781
782 static struct ref *alloc_ref_with_prefix(const char *prefix, size_t prefixlen,
@@ -1588,7 +1574,7 @@ static const char *tracking_for_push_dest(struct remote *remote,
1574 {
1575 char *ret;
1576
1591 - ret = apply_refspecs(remote->fetch, remote->fetch_refspec_nr, refname);
1577 + ret = apply_refspecs(remote->fetch.items, remote->fetch.nr, refname);
1578 if (!ret)
1579 return error_buf(err,
1580 _("push destination '%s' on remote '%s' has no local tracking branch"),
@@ -2222,7 +2208,7 @@ static int remote_tracking(struct remote *remote, const char *refname,
2208 {
2209 char *dst;
2210
2225 - dst = apply_refspecs(remote->fetch, remote->fetch_refspec_nr, refname);
2211 + dst = apply_refspecs(remote->fetch.items, remote->fetch.nr, refname);
2212 if (!dst)
2213 return -1; /* no tracking ref for refname at remote */
2214 if (read_ref(dst, oid))
remote.h
+1 -4
@@ -30,10 +30,7 @@ struct remote {
30
31 struct refspec push;
32
33 - const char **fetch_refspec;
34 - struct refspec_item *fetch;
35 - int fetch_refspec_nr;
36 - int fetch_refspec_alloc;
33 + struct refspec fetch;
34
35 /*
36 * -1 to never fetch tags