@@ -242,17 +242,17 @@ static int will_fetch(struct ref **head, const unsigned char *sha1)
242
242
return 0 ;
243
243
}
244
244
245
- static void find_non_local_tags (struct transport * transport ,
246
- struct ref * * head ,
247
- struct ref * * * tail )
245
+ static void find_non_local_tags (const struct ref * refs ,
246
+ struct ref * * head ,
247
+ struct ref * * * tail )
248
248
{
249
249
struct string_list existing_refs = STRING_LIST_INIT_DUP ;
250
250
struct string_list remote_refs = STRING_LIST_INIT_NODUP ;
251
251
const struct ref * ref ;
252
252
struct string_list_item * item = NULL ;
253
253
254
254
for_each_ref (add_existing , & existing_refs );
255
- for (ref = transport_get_remote_refs ( transport , NULL ) ; ref ; ref = ref -> next ) {
255
+ for (ref = refs ; ref ; ref = ref -> next ) {
256
256
if (!starts_with (ref -> name , "refs/tags/" ))
257
257
continue ;
258
258
@@ -326,34 +326,20 @@ static void find_non_local_tags(struct transport *transport,
326
326
string_list_clear (& remote_refs , 0 );
327
327
}
328
328
329
- static struct ref * get_ref_map (struct transport * transport ,
329
+ static struct ref * get_ref_map (struct remote * remote ,
330
+ const struct ref * remote_refs ,
330
331
struct refspec * rs ,
331
332
int tags , int * autotags )
332
333
{
333
334
int i ;
334
335
struct ref * rm ;
335
336
struct ref * ref_map = NULL ;
336
337
struct ref * * tail = & ref_map ;
337
- struct argv_array ref_prefixes = ARGV_ARRAY_INIT ;
338
338
339
339
/* opportunistically-updated references: */
340
340
struct ref * orefs = NULL , * * oref_tail = & orefs ;
341
341
342
- const struct ref * remote_refs ;
343
-
344
- if (rs -> nr )
345
- refspec_ref_prefixes (rs , & ref_prefixes );
346
- else if (transport -> remote && transport -> remote -> fetch .nr )
347
- refspec_ref_prefixes (& transport -> remote -> fetch , & ref_prefixes );
348
-
349
- if (ref_prefixes .argc &&
350
- (tags == TAGS_SET || (tags == TAGS_DEFAULT && !rs -> nr ))) {
351
- argv_array_push (& ref_prefixes , "refs/tags/" );
352
- }
353
-
354
- remote_refs = transport_get_remote_refs (transport , & ref_prefixes );
355
-
356
- argv_array_clear (& ref_prefixes );
342
+ struct string_list existing_refs = STRING_LIST_INIT_DUP ;
357
343
358
344
if (rs -> nr ) {
359
345
struct refspec * fetch_refspec ;
@@ -390,15 +376,14 @@ static struct ref *get_ref_map(struct transport *transport,
390
376
if (refmap .nr )
391
377
fetch_refspec = & refmap ;
392
378
else
393
- fetch_refspec = & transport -> remote -> fetch ;
379
+ fetch_refspec = & remote -> fetch ;
394
380
395
381
for (i = 0 ; i < fetch_refspec -> nr ; i ++ )
396
382
get_fetch_map (ref_map , & fetch_refspec -> items [i ], & oref_tail , 1 );
397
383
} else if (refmap .nr ) {
398
384
die ("--refmap option is only meaningful with command-line refspec(s)." );
399
385
} else {
400
386
/* Use the defaults */
401
- struct remote * remote = transport -> remote ;
402
387
struct branch * branch = branch_get (NULL );
403
388
int has_merge = branch_has_merge_config (branch );
404
389
if (remote &&
@@ -437,7 +422,7 @@ static struct ref *get_ref_map(struct transport *transport,
437
422
/* also fetch all tags */
438
423
get_fetch_map (remote_refs , tag_refspec , & tail , 0 );
439
424
else if (tags == TAGS_DEFAULT && * autotags )
440
- find_non_local_tags (transport , & ref_map , & tail );
425
+ find_non_local_tags (remote_refs , & ref_map , & tail );
441
426
442
427
/* Now append any refs to be updated opportunistically: */
443
428
* tail = orefs ;
@@ -446,7 +431,23 @@ static struct ref *get_ref_map(struct transport *transport,
446
431
tail = & rm -> next ;
447
432
}
448
433
449
- return ref_remove_duplicates (ref_map );
434
+ ref_map = ref_remove_duplicates (ref_map );
435
+
436
+ for_each_ref (add_existing , & existing_refs );
437
+ for (rm = ref_map ; rm ; rm = rm -> next ) {
438
+ if (rm -> peer_ref ) {
439
+ struct string_list_item * peer_item =
440
+ string_list_lookup (& existing_refs ,
441
+ rm -> peer_ref -> name );
442
+ if (peer_item ) {
443
+ struct object_id * old_oid = peer_item -> util ;
444
+ oidcpy (& rm -> peer_ref -> old_oid , old_oid );
445
+ }
446
+ }
447
+ }
448
+ string_list_clear (& existing_refs , 1 );
449
+
450
+ return ref_map ;
450
451
}
451
452
452
453
#define STORE_REF_ERROR_OTHER 1
@@ -756,7 +757,7 @@ static int iterate_ref_map(void *cb_data, struct object_id *oid)
756
757
}
757
758
758
759
static int store_updated_refs (const char * raw_url , const char * remote_name ,
759
- struct ref * ref_map )
760
+ int connectivity_checked , struct ref * ref_map )
760
761
{
761
762
FILE * fp ;
762
763
struct commit * commit ;
@@ -778,10 +779,12 @@ static int store_updated_refs(const char *raw_url, const char *remote_name,
778
779
else
779
780
url = xstrdup ("foreign" );
780
781
781
- rm = ref_map ;
782
- if (check_connected (iterate_ref_map , & rm , NULL )) {
783
- rc = error (_ ("%s did not send all necessary objects\n" ), url );
784
- goto abort ;
782
+ if (!connectivity_checked ) {
783
+ rm = ref_map ;
784
+ if (check_connected (iterate_ref_map , & rm , NULL )) {
785
+ rc = error (_ ("%s did not send all necessary objects\n" ), url );
786
+ goto abort ;
787
+ }
785
788
}
786
789
787
790
prepare_format_display (ref_map );
@@ -933,15 +936,32 @@ static int quickfetch(struct ref *ref_map)
933
936
return check_connected (iterate_ref_map , & rm , & opt );
934
937
}
935
938
936
- static int fetch_refs (struct transport * transport , struct ref * ref_map )
939
+ static int fetch_refs (struct transport * transport , struct ref * ref_map ,
940
+ struct ref * * updated_remote_refs )
937
941
{
938
942
int ret = quickfetch (ref_map );
939
943
if (ret )
940
- ret = transport_fetch_refs (transport , ref_map );
944
+ ret = transport_fetch_refs (transport , ref_map ,
945
+ updated_remote_refs );
941
946
if (!ret )
942
- ret |= store_updated_refs (transport -> url ,
943
- transport -> remote -> name ,
944
- ref_map );
947
+ /*
948
+ * Keep the new pack's ".keep" file around to allow the caller
949
+ * time to update refs to reference the new objects.
950
+ */
951
+ return 0 ;
952
+ transport_unlock_pack (transport );
953
+ return ret ;
954
+ }
955
+
956
+ /* Update local refs based on the ref values fetched from a remote */
957
+ static int consume_refs (struct transport * transport , struct ref * ref_map )
958
+ {
959
+ int connectivity_checked = transport -> smart_options
960
+ ? transport -> smart_options -> connectivity_checked : 0 ;
961
+ int ret = store_updated_refs (transport -> url ,
962
+ transport -> remote -> name ,
963
+ connectivity_checked ,
964
+ ref_map );
945
965
transport_unlock_pack (transport );
946
966
return ret ;
947
967
}
@@ -1087,7 +1107,8 @@ static void backfill_tags(struct transport *transport, struct ref *ref_map)
1087
1107
transport_set_option (transport , TRANS_OPT_FOLLOWTAGS , NULL );
1088
1108
transport_set_option (transport , TRANS_OPT_DEPTH , "0" );
1089
1109
transport_set_option (transport , TRANS_OPT_DEEPEN_RELATIVE , NULL );
1090
- fetch_refs (transport , ref_map );
1110
+ if (!fetch_refs (transport , ref_map , NULL ))
1111
+ consume_refs (transport , ref_map );
1091
1112
1092
1113
if (gsecondary ) {
1093
1114
transport_disconnect (gsecondary );
@@ -1098,13 +1119,12 @@ static void backfill_tags(struct transport *transport, struct ref *ref_map)
1098
1119
static int do_fetch (struct transport * transport ,
1099
1120
struct refspec * rs )
1100
1121
{
1101
- struct string_list existing_refs = STRING_LIST_INIT_DUP ;
1102
1122
struct ref * ref_map ;
1103
- struct ref * rm ;
1104
1123
int autotags = (transport -> remote -> fetch_tags == 1 );
1105
1124
int retcode = 0 ;
1106
-
1107
- for_each_ref (add_existing , & existing_refs );
1125
+ const struct ref * remote_refs ;
1126
+ struct ref * updated_remote_refs = NULL ;
1127
+ struct argv_array ref_prefixes = ARGV_ARRAY_INIT ;
1108
1128
1109
1129
if (tags == TAGS_DEFAULT ) {
1110
1130
if (transport -> remote -> fetch_tags == 2 )
@@ -1120,22 +1140,24 @@ static int do_fetch(struct transport *transport,
1120
1140
goto cleanup ;
1121
1141
}
1122
1142
1123
- ref_map = get_ref_map (transport , rs , tags , & autotags );
1124
- if (!update_head_ok )
1125
- check_not_current_branch (ref_map );
1143
+ if (rs -> nr )
1144
+ refspec_ref_prefixes (rs , & ref_prefixes );
1145
+ else if (transport -> remote && transport -> remote -> fetch .nr )
1146
+ refspec_ref_prefixes (& transport -> remote -> fetch , & ref_prefixes );
1126
1147
1127
- for (rm = ref_map ; rm ; rm = rm -> next ) {
1128
- if (rm -> peer_ref ) {
1129
- struct string_list_item * peer_item =
1130
- string_list_lookup (& existing_refs ,
1131
- rm -> peer_ref -> name );
1132
- if (peer_item ) {
1133
- struct object_id * old_oid = peer_item -> util ;
1134
- oidcpy (& rm -> peer_ref -> old_oid , old_oid );
1135
- }
1136
- }
1148
+ if (ref_prefixes .argc &&
1149
+ (tags == TAGS_SET || (tags == TAGS_DEFAULT && !rs -> nr ))) {
1150
+ argv_array_push (& ref_prefixes , "refs/tags/" );
1137
1151
}
1138
1152
1153
+ remote_refs = transport_get_remote_refs (transport , & ref_prefixes );
1154
+ argv_array_clear (& ref_prefixes );
1155
+
1156
+ ref_map = get_ref_map (transport -> remote , remote_refs , rs ,
1157
+ tags , & autotags );
1158
+ if (!update_head_ok )
1159
+ check_not_current_branch (ref_map );
1160
+
1139
1161
if (tags == TAGS_DEFAULT && autotags )
1140
1162
transport_set_option (transport , TRANS_OPT_FOLLOWTAGS , "1" );
1141
1163
if (prune ) {
@@ -1152,7 +1174,24 @@ static int do_fetch(struct transport *transport,
1152
1174
transport -> url );
1153
1175
}
1154
1176
}
1155
- if (fetch_refs (transport , ref_map )) {
1177
+
1178
+ if (fetch_refs (transport , ref_map , & updated_remote_refs )) {
1179
+ free_refs (ref_map );
1180
+ retcode = 1 ;
1181
+ goto cleanup ;
1182
+ }
1183
+ if (updated_remote_refs ) {
1184
+ /*
1185
+ * Regenerate ref_map using the updated remote refs. This is
1186
+ * to account for additional information which may be provided
1187
+ * by the transport (e.g. shallow info).
1188
+ */
1189
+ free_refs (ref_map );
1190
+ ref_map = get_ref_map (transport -> remote , updated_remote_refs , rs ,
1191
+ tags , & autotags );
1192
+ free_refs (updated_remote_refs );
1193
+ }
1194
+ if (consume_refs (transport , ref_map )) {
1156
1195
free_refs (ref_map );
1157
1196
retcode = 1 ;
1158
1197
goto cleanup ;
@@ -1164,14 +1203,13 @@ static int do_fetch(struct transport *transport,
1164
1203
if (tags == TAGS_DEFAULT && autotags ) {
1165
1204
struct ref * * tail = & ref_map ;
1166
1205
ref_map = NULL ;
1167
- find_non_local_tags (transport , & ref_map , & tail );
1206
+ find_non_local_tags (remote_refs , & ref_map , & tail );
1168
1207
if (ref_map )
1169
1208
backfill_tags (transport , ref_map );
1170
1209
free_refs (ref_map );
1171
1210
}
1172
1211
1173
1212
cleanup :
1174
- string_list_clear (& existing_refs , 1 );
1175
1213
return retcode ;
1176
1214
}
1177
1215
0 commit comments