fetch: refactor the population of peer ref OIDs

Populate peer ref OIDs in get_ref_map instead of do_fetch. Besides tightening scopes of variables in the code, this also prepares for get_ref_map being able to be called multiple times within do_fetch. Signed-off-by: Brandon Williams <bmwill@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Brandon Williams committed Jun 27, 2018 at 15:30 UTC 14b8ced3765b8916683b445f5cc3d6d80acc5f93
1 file changed +18 -18
builtin/fetch.c
+18 -18
@@ -351,6 +351,7 @@ static struct ref *get_ref_map(struct transport *transport,
351 /* opportunistically-updated references: */
352 struct ref *orefs = NULL, **oref_tail = &orefs;
353
354 + struct string_list existing_refs = STRING_LIST_INIT_DUP;
355 const struct ref *remote_refs;
356
357 if (rs->nr)
@@ -458,7 +459,23 @@ static struct ref *get_ref_map(struct transport *transport,
459 tail = &rm->next;
460 }
461
461 - return ref_remove_duplicates(ref_map);
462 + ref_map = ref_remove_duplicates(ref_map);
463 +
464 + for_each_ref(add_existing, &existing_refs);
465 + for (rm = ref_map; rm; rm = rm->next) {
466 + if (rm->peer_ref) {
467 + struct string_list_item *peer_item =
468 + string_list_lookup(&existing_refs,
469 + rm->peer_ref->name);
470 + if (peer_item) {
471 + struct object_id *old_oid = peer_item->util;
472 + oidcpy(&rm->peer_ref->old_oid, old_oid);
473 + }
474 + }
475 + }
476 + string_list_clear(&existing_refs, 1);
477 +
478 + return ref_map;
479 }
480
481 #define STORE_REF_ERROR_OTHER 1
@@ -1110,14 +1127,10 @@ static void backfill_tags(struct transport *transport, struct ref *ref_map)
1127 static int do_fetch(struct transport *transport,
1128 struct refspec *rs)
1129 {
1113 - struct string_list existing_refs = STRING_LIST_INIT_DUP;
1130 struct ref *ref_map;
1115 - struct ref *rm;
1131 int autotags = (transport->remote->fetch_tags == 1);
1132 int retcode = 0;
1133
1119 - for_each_ref(add_existing, &existing_refs);
1120 -
1134 if (tags == TAGS_DEFAULT) {
1135 if (transport->remote->fetch_tags == 2)
1136 tags = TAGS_SET;
@@ -1136,18 +1149,6 @@ static int do_fetch(struct transport *transport,
1149 if (!update_head_ok)
1150 check_not_current_branch(ref_map);
1151
1139 - for (rm = ref_map; rm; rm = rm->next) {
1140 - if (rm->peer_ref) {
1141 - struct string_list_item *peer_item =
1142 - string_list_lookup(&existing_refs,
1143 - rm->peer_ref->name);
1144 - if (peer_item) {
1145 - struct object_id *old_oid = peer_item->util;
1146 - oidcpy(&rm->peer_ref->old_oid, old_oid);
1147 - }
1148 - }
1149 - }
1150 -
1152 if (tags == TAGS_DEFAULT && autotags)
1153 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1154 if (prune) {
@@ -1183,7 +1184,6 @@ static int do_fetch(struct transport *transport,
1184 }
1185
1186 cleanup:
1186 - string_list_clear(&existing_refs, 1);
1187 return retcode;
1188 }
1189