Raw
1 /*
2 * "git fetch"
3 */
4
5 #define USE_THE_REPOSITORY_VARIABLE
6 #define DISABLE_SIGN_COMPARE_WARNINGS
7
8 #include "builtin.h"
9 #include "advice.h"
10 #include "config.h"
11 #include "gettext.h"
12 #include "environment.h"
13 #include "hex.h"
14 #include "refs.h"
15 #include "refspec.h"
16 #include "object-name.h"
17 #include "odb.h"
18 #include "oidset.h"
19 #include "oid-array.h"
20 #include "commit.h"
21 #include "string-list.h"
22 #include "remote.h"
23 #include "transport.h"
24 #include "run-command.h"
25 #include "parse-options.h"
26 #include "sigchain.h"
27 #include "submodule-config.h"
28 #include "submodule.h"
29 #include "connected.h"
30 #include "strvec.h"
31 #include "utf8.h"
32 #include "pager.h"
33 #include "path.h"
34 #include "pkt-line.h"
35 #include "list-objects-filter-options.h"
36 #include "commit-reach.h"
37 #include "branch.h"
38 #include "promisor-remote.h"
39 #include "commit-graph.h"
40 #include "shallow.h"
41 #include "trace.h"
42 #include "trace2.h"
43 #include "bundle-uri.h"
44
45 #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
46
47 static const char * const builtin_fetch_usage[] = {
48 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
49 N_("git fetch [<options>] <group>"),
50 N_("git fetch --multiple [<options>] [(<repository>|<group>)...]"),
51 N_("git fetch --all [<options>]"),
52 NULL
53 };
54
55 enum {
56 TAGS_UNSET = 0,
57 TAGS_DEFAULT = 1,
58 TAGS_SET = 2
59 };
60
61 enum display_format {
62 DISPLAY_FORMAT_FULL,
63 DISPLAY_FORMAT_COMPACT,
64 DISPLAY_FORMAT_PORCELAIN,
65 };
66
67 struct display_state {
68 struct strbuf buf;
69
70 int refcol_width;
71 enum display_format format;
72
73 char *url;
74 int url_len, shown_url;
75 };
76
77 static uint64_t forced_updates_ms = 0;
78 static int prefetch = 0;
79 static int prune = -1; /* unspecified */
80 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
81
82 static int prune_tags = -1; /* unspecified */
83 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
84
85 static int append, dry_run, force, keep, update_head_ok;
86 static int write_fetch_head = 1;
87 static int verbosity, deepen_relative, set_upstream, refetch;
88 static int progress = -1;
89 static int tags = TAGS_DEFAULT, update_shallow, deepen;
90 static int atomic_fetch;
91 static enum transport_family family;
92 static const char *depth;
93 static const char *deepen_since;
94 static const char *upload_pack;
95 static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
96 static struct strbuf default_rla = STRBUF_INIT;
97 static struct transport *gtransport;
98 static struct transport *gsecondary;
99 static struct refspec refmap = REFSPEC_INIT_FETCH;
100 static struct string_list server_options = STRING_LIST_INIT_DUP;
101 static struct string_list negotiation_restrict = STRING_LIST_INIT_NODUP;
102 static struct string_list negotiation_include = STRING_LIST_INIT_NODUP;
103
104 struct fetch_config {
105 enum display_format display_format;
106 int all;
107 int prune;
108 int prune_tags;
109 int show_forced_updates;
110 int recurse_submodules;
111 int parallel;
112 int submodule_fetch_jobs;
113 };
114
115 static int git_fetch_config(const char *k, const char *v,
116 const struct config_context *ctx, void *cb)
117 {
118 struct fetch_config *fetch_config = cb;
119
120 if (!strcmp(k, "fetch.all")) {
121 fetch_config->all = git_config_bool(k, v);
122 return 0;
123 }
124
125 if (!strcmp(k, "fetch.prune")) {
126 fetch_config->prune = git_config_bool(k, v);
127 return 0;
128 }
129
130 if (!strcmp(k, "fetch.prunetags")) {
131 fetch_config->prune_tags = git_config_bool(k, v);
132 return 0;
133 }
134
135 if (!strcmp(k, "fetch.showforcedupdates")) {
136 fetch_config->show_forced_updates = git_config_bool(k, v);
137 return 0;
138 }
139
140 if (!strcmp(k, "submodule.recurse")) {
141 int r = git_config_bool(k, v) ?
142 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
143 fetch_config->recurse_submodules = r;
144 return 0;
145 }
146
147 if (!strcmp(k, "submodule.fetchjobs")) {
148 fetch_config->submodule_fetch_jobs = parse_submodule_fetchjobs(k, v, ctx->kvi);
149 return 0;
150 } else if (!strcmp(k, "fetch.recursesubmodules")) {
151 fetch_config->recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
152 return 0;
153 }
154
155 if (!strcmp(k, "fetch.parallel")) {
156 fetch_config->parallel = git_config_int(k, v, ctx->kvi);
157 if (fetch_config->parallel < 0)
158 die(_("fetch.parallel cannot be negative"));
159 if (!fetch_config->parallel)
160 fetch_config->parallel = online_cpus();
161 return 0;
162 }
163
164 if (!strcmp(k, "fetch.output")) {
165 if (!v)
166 return config_error_nonbool(k);
167 else if (!strcasecmp(v, "full"))
168 fetch_config->display_format = DISPLAY_FORMAT_FULL;
169 else if (!strcasecmp(v, "compact"))
170 fetch_config->display_format = DISPLAY_FORMAT_COMPACT;
171 else
172 die(_("invalid value for '%s': '%s'"),
173 "fetch.output", v);
174 }
175
176 return git_default_config(k, v, ctx, cb);
177 }
178
179 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
180 {
181 BUG_ON_OPT_NEG(unset);
182
183 /*
184 * "git fetch --refmap='' origin foo"
185 * can be used to tell the command not to store anywhere
186 */
187 refspec_append(opt->value, arg);
188
189 return 0;
190 }
191
192 static void unlock_pack(unsigned int flags)
193 {
194 if (gtransport)
195 transport_unlock_pack(gtransport, flags);
196 if (gsecondary)
197 transport_unlock_pack(gsecondary, flags);
198 }
199
200 static void unlock_pack_atexit(void)
201 {
202 unlock_pack(0);
203 }
204
205 static void unlock_pack_on_signal(int signo)
206 {
207 unlock_pack(TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
208 sigchain_pop(signo);
209 raise(signo);
210 }
211
212 static void add_merge_config(struct ref **head,
213 const struct ref *remote_refs,
214 struct branch *branch,
215 struct ref ***tail)
216 {
217 int i;
218
219 for (i = 0; i < branch->merge_nr; i++) {
220 struct ref *rm, **old_tail = *tail;
221 struct refspec_item refspec;
222
223 for (rm = *head; rm; rm = rm->next) {
224 if (branch_merge_matches(branch, i, rm->name)) {
225 rm->fetch_head_status = FETCH_HEAD_MERGE;
226 break;
227 }
228 }
229 if (rm)
230 continue;
231
232 /*
233 * Not fetched to a remote-tracking branch? We need to fetch
234 * it anyway to allow this branch's "branch.$name.merge"
235 * to be honored by 'git pull', but we do not have to
236 * fail if branch.$name.merge is misconfigured to point
237 * at a nonexisting branch. If we were indeed called by
238 * 'git pull', it will notice the misconfiguration because
239 * there is no entry in the resulting FETCH_HEAD marked
240 * for merging.
241 */
242 memset(&refspec, 0, sizeof(refspec));
243 refspec.src = branch->merge[i]->src;
244 get_fetch_map(remote_refs, &refspec, tail, 1);
245 for (rm = *old_tail; rm; rm = rm->next)
246 rm->fetch_head_status = FETCH_HEAD_MERGE;
247 }
248 }
249
250 static void create_fetch_oidset(struct ref **head, struct oidset *out)
251 {
252 struct ref *rm = *head;
253 while (rm) {
254 oidset_insert(out, &rm->old_oid);
255 rm = rm->next;
256 }
257 }
258
259 struct refname_hash_entry {
260 struct hashmap_entry ent;
261 struct object_id oid;
262 int ignore;
263 char refname[FLEX_ARRAY];
264 };
265
266 static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data UNUSED,
267 const struct hashmap_entry *eptr,
268 const struct hashmap_entry *entry_or_key,
269 const void *keydata)
270 {
271 const struct refname_hash_entry *e1, *e2;
272
273 e1 = container_of(eptr, const struct refname_hash_entry, ent);
274 e2 = container_of(entry_or_key, const struct refname_hash_entry, ent);
275 return strcmp(e1->refname, keydata ? keydata : e2->refname);
276 }
277
278 static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
279 const char *refname,
280 const struct object_id *oid)
281 {
282 struct refname_hash_entry *ent;
283 size_t len = strlen(refname);
284
285 FLEX_ALLOC_MEM(ent, refname, refname, len);
286 hashmap_entry_init(&ent->ent, strhash(refname));
287 oidcpy(&ent->oid, oid);
288 hashmap_add(map, &ent->ent);
289 return ent;
290 }
291
292 static int add_one_refname(const struct reference *ref, void *cbdata)
293 {
294 struct hashmap *refname_map = cbdata;
295
296 (void) refname_hash_add(refname_map, ref->name, ref->oid);
297 return 0;
298 }
299
300 static void refname_hash_init(struct hashmap *map)
301 {
302 hashmap_init(map, refname_hash_entry_cmp, NULL, 0);
303 }
304
305 static int refname_hash_exists(struct hashmap *map, const char *refname)
306 {
307 return !!hashmap_get_from_hash(map, strhash(refname), refname);
308 }
309
310 static void clear_item(struct refname_hash_entry *item)
311 {
312 item->ignore = 1;
313 }
314
315
316 static void add_already_queued_tags(const char *refname,
317 const struct object_id *old_oid UNUSED,
318 const struct object_id *new_oid,
319 void *cb_data)
320 {
321 struct hashmap *queued_tags = cb_data;
322 if (starts_with(refname, "refs/tags/") && new_oid)
323 (void) refname_hash_add(queued_tags, refname, new_oid);
324 }
325
326 static void find_non_local_tags(const struct ref *refs,
327 struct ref_transaction *transaction,
328 struct ref **head,
329 struct ref ***tail)
330 {
331 struct hashmap existing_refs;
332 struct hashmap remote_refs;
333 struct oidset fetch_oids = OIDSET_INIT;
334 struct string_list remote_refs_list = STRING_LIST_INIT_NODUP;
335 struct string_list_item *remote_ref_item;
336 const struct ref *ref;
337 struct refname_hash_entry *item = NULL;
338
339 refname_hash_init(&existing_refs);
340 refname_hash_init(&remote_refs);
341 create_fetch_oidset(head, &fetch_oids);
342
343 refs_for_each_ref(get_main_ref_store(the_repository), add_one_refname,
344 &existing_refs);
345
346 /*
347 * If we already have a transaction, then we need to filter out all
348 * tags which have already been queued up.
349 */
350 if (transaction)
351 ref_transaction_for_each_queued_update(transaction,
352 add_already_queued_tags,
353 &existing_refs);
354
355 for (ref = refs; ref; ref = ref->next) {
356 if (!starts_with(ref->name, "refs/tags/"))
357 continue;
358
359 /*
360 * The peeled ref always follows the matching base
361 * ref, so if we see a peeled ref that we don't want
362 * to fetch then we can mark the ref entry in the list
363 * as one to ignore by setting util to NULL.
364 */
365 if (ends_with(ref->name, "^{}")) {
366 if (item &&
367 !odb_has_object(the_repository->objects, &ref->old_oid, 0) &&
368 !oidset_contains(&fetch_oids, &ref->old_oid) &&
369 !odb_has_object(the_repository->objects, &item->oid, 0) &&
370 !oidset_contains(&fetch_oids, &item->oid))
371 clear_item(item);
372 item = NULL;
373 continue;
374 }
375
376 /*
377 * If item is non-NULL here, then we previously saw a
378 * ref not followed by a peeled reference, so we need
379 * to check if it is a lightweight tag that we want to
380 * fetch.
381 */
382 if (item &&
383 !odb_has_object(the_repository->objects, &item->oid, 0) &&
384 !oidset_contains(&fetch_oids, &item->oid))
385 clear_item(item);
386
387 item = NULL;
388
389 /* skip duplicates and refs that we already have */
390 if (refname_hash_exists(&remote_refs, ref->name) ||
391 refname_hash_exists(&existing_refs, ref->name))
392 continue;
393
394 item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
395 string_list_insert(&remote_refs_list, ref->name);
396 }
397 hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
398
399 /*
400 * We may have a final lightweight tag that needs to be
401 * checked to see if it needs fetching.
402 */
403 if (item &&
404 !odb_has_object(the_repository->objects, &item->oid, 0) &&
405 !oidset_contains(&fetch_oids, &item->oid))
406 clear_item(item);
407
408 /*
409 * For all the tags in the remote_refs_list,
410 * add them to the list of refs to be fetched
411 */
412 for_each_string_list_item(remote_ref_item, &remote_refs_list) {
413 const char *refname = remote_ref_item->string;
414 struct ref *rm;
415 unsigned int hash = strhash(refname);
416
417 item = hashmap_get_entry_from_hash(&remote_refs, hash, refname,
418 struct refname_hash_entry, ent);
419 if (!item)
420 BUG("unseen remote ref?");
421
422 /* Unless we have already decided to ignore this item... */
423 if (item->ignore)
424 continue;
425
426 rm = alloc_ref(item->refname);
427 rm->peer_ref = alloc_ref(item->refname);
428 oidcpy(&rm->old_oid, &item->oid);
429 **tail = rm;
430 *tail = &rm->next;
431 }
432 hashmap_clear_and_free(&remote_refs, struct refname_hash_entry, ent);
433 string_list_clear(&remote_refs_list, 0);
434 oidset_clear(&fetch_oids);
435 }
436
437 static void filter_prefetch_refspec(struct refspec *rs)
438 {
439 int i;
440
441 if (!prefetch)
442 return;
443
444 for (i = 0; i < rs->nr; i++) {
445 struct strbuf new_dst = STRBUF_INIT;
446 char *old_dst;
447 const char *sub = NULL;
448
449 if (rs->items[i].negative)
450 continue;
451 if (!rs->items[i].dst ||
452 (rs->items[i].src &&
453 starts_with(rs->items[i].src,
454 ref_namespace[NAMESPACE_TAGS].ref))) {
455 int j;
456
457 refspec_item_clear(&rs->items[i]);
458
459 for (j = i + 1; j < rs->nr; j++)
460 rs->items[j - 1] = rs->items[j];
461 rs->nr--;
462 i--;
463 continue;
464 }
465
466 old_dst = rs->items[i].dst;
467 strbuf_addstr(&new_dst, ref_namespace[NAMESPACE_PREFETCH].ref);
468
469 /*
470 * If old_dst starts with "refs/", then place
471 * sub after that prefix. Otherwise, start at
472 * the beginning of the string.
473 */
474 if (!skip_prefix(old_dst, "refs/", &sub))
475 sub = old_dst;
476 strbuf_addstr(&new_dst, sub);
477
478 rs->items[i].dst = strbuf_detach(&new_dst, NULL);
479 rs->items[i].force = 1;
480
481 free(old_dst);
482 }
483 }
484
485 static struct ref *get_ref_map(struct remote *remote,
486 const struct ref *remote_refs,
487 struct refspec *rs,
488 int tags, int *autotags)
489 {
490 int i;
491 struct ref *rm;
492 struct ref *ref_map = NULL;
493 struct ref **tail = &ref_map;
494
495 /* opportunistically-updated references: */
496 struct ref *orefs = NULL, **oref_tail = &orefs;
497
498 struct hashmap existing_refs;
499 int existing_refs_populated = 0;
500
501 filter_prefetch_refspec(rs);
502 if (remote)
503 filter_prefetch_refspec(&remote->fetch);
504
505 if (rs->nr) {
506 struct refspec *fetch_refspec;
507
508 for (i = 0; i < rs->nr; i++) {
509 get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
510 if (rs->items[i].dst && rs->items[i].dst[0])
511 *autotags = 1;
512 }
513 /* Merge everything on the command line (but not --tags) */
514 for (rm = ref_map; rm; rm = rm->next)
515 rm->fetch_head_status = FETCH_HEAD_MERGE;
516
517 /*
518 * For any refs that we happen to be fetching via
519 * command-line arguments, the destination ref might
520 * have been missing or have been different than the
521 * remote-tracking ref that would be derived from the
522 * configured refspec. In these cases, we want to
523 * take the opportunity to update their configured
524 * remote-tracking reference. However, we do not want
525 * to mention these entries in FETCH_HEAD at all, as
526 * they would simply be duplicates of existing
527 * entries, so we set them FETCH_HEAD_IGNORE below.
528 *
529 * We compute these entries now, based only on the
530 * refspecs specified on the command line. But we add
531 * them to the list following the refspecs resulting
532 * from the tags option so that one of the latter,
533 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
534 * by ref_remove_duplicates() in favor of one of these
535 * opportunistic entries with FETCH_HEAD_IGNORE.
536 */
537 if (refmap.nr)
538 fetch_refspec = &refmap;
539 else
540 fetch_refspec = &remote->fetch;
541
542 for (i = 0; i < fetch_refspec->nr; i++)
543 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
544 } else if (refmap.nr) {
545 die("--refmap option is only meaningful with command-line refspec(s)");
546 } else {
547 /* Use the defaults */
548 struct branch *branch = branch_get(NULL);
549 int has_merge = branch_has_merge_config(branch);
550 if (remote &&
551 (remote->fetch.nr ||
552 /* Note: has_merge implies non-NULL branch->remote_name */
553 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
554 for (i = 0; i < remote->fetch.nr; i++) {
555 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
556 if (remote->fetch.items[i].dst &&
557 remote->fetch.items[i].dst[0])
558 *autotags = 1;
559 if (!i && !has_merge && ref_map &&
560 !remote->fetch.items[0].pattern)
561 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
562 }
563 /*
564 * if the remote we're fetching from is the same
565 * as given in branch.<name>.remote, we add the
566 * ref given in branch.<name>.merge, too.
567 *
568 * Note: has_merge implies non-NULL branch->remote_name
569 */
570 if (has_merge &&
571 !strcmp(branch->remote_name, remote->name))
572 add_merge_config(&ref_map, remote_refs, branch, &tail);
573 } else if (!prefetch) {
574 ref_map = get_remote_ref(remote_refs, "HEAD");
575 if (!ref_map)
576 die(_("couldn't find remote ref HEAD"));
577 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
578 tail = &ref_map->next;
579 }
580 }
581
582 if (tags == TAGS_SET) {
583 struct refspec_item tag_refspec;
584
585 /* also fetch all tags */
586 refspec_item_init_push(&tag_refspec, TAG_REFSPEC);
587 get_fetch_map(remote_refs, &tag_refspec, &tail, 0);
588 refspec_item_clear(&tag_refspec);
589 } else if (tags == TAGS_DEFAULT && *autotags) {
590 find_non_local_tags(remote_refs, NULL, &ref_map, &tail);
591 }
592
593 /* Now append any refs to be updated opportunistically: */
594 *tail = orefs;
595 for (rm = orefs; rm; rm = rm->next) {
596 rm->fetch_head_status = FETCH_HEAD_IGNORE;
597 tail = &rm->next;
598 }
599
600 /*
601 * apply negative refspecs first, before we remove duplicates. This is
602 * necessary as negative refspecs might remove an otherwise conflicting
603 * duplicate.
604 */
605 if (rs->nr)
606 ref_map = apply_negative_refspecs(ref_map, rs);
607 else
608 ref_map = apply_negative_refspecs(ref_map, &remote->fetch);
609
610 ref_map = ref_remove_duplicates(ref_map);
611
612 for (rm = ref_map; rm; rm = rm->next) {
613 if (rm->peer_ref) {
614 const char *refname = rm->peer_ref->name;
615 struct refname_hash_entry *peer_item;
616 unsigned int hash = strhash(refname);
617
618 if (!existing_refs_populated) {
619 refname_hash_init(&existing_refs);
620 refs_for_each_ref(get_main_ref_store(the_repository),
621 add_one_refname,
622 &existing_refs);
623 existing_refs_populated = 1;
624 }
625
626 peer_item = hashmap_get_entry_from_hash(&existing_refs,
627 hash, refname,
628 struct refname_hash_entry, ent);
629 if (peer_item) {
630 struct object_id *old_oid = &peer_item->oid;
631 oidcpy(&rm->peer_ref->old_oid, old_oid);
632 }
633 }
634 }
635 if (existing_refs_populated)
636 hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
637
638 return ref_map;
639 }
640
641 static int s_update_ref(const char *action,
642 struct ref *ref,
643 struct ref_transaction *transaction,
644 int check_old)
645 {
646 char *msg;
647 char *rla = getenv("GIT_REFLOG_ACTION");
648 struct strbuf err = STRBUF_INIT;
649 int ret;
650
651 if (dry_run)
652 return 0;
653 if (!rla)
654 rla = default_rla.buf;
655 msg = xstrfmt("%s: %s", rla, action);
656
657 ret = ref_transaction_update(transaction, ref->name, &ref->new_oid,
658 check_old ? &ref->old_oid : NULL,
659 NULL, NULL, 0, msg, &err);
660
661 if (ret)
662 error("%s", err.buf);
663 strbuf_release(&err);
664 free(msg);
665 return ret;
666 }
667
668 static int refcol_width(const struct ref *ref_map, int compact_format)
669 {
670 const struct ref *ref;
671 int max, width = 10;
672
673 max = term_columns();
674 if (compact_format)
675 max = max * 2 / 3;
676
677 for (ref = ref_map; ref; ref = ref->next) {
678 int rlen, llen = 0, len;
679
680 if (ref->status == REF_STATUS_REJECT_SHALLOW ||
681 !ref->peer_ref ||
682 !strcmp(ref->name, "HEAD"))
683 continue;
684
685 /* uptodate lines are only shown on high verbosity level */
686 if (verbosity <= 0 && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
687 continue;
688
689 rlen = utf8_strwidth(prettify_refname(ref->name));
690 if (!compact_format)
691 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
692
693 /*
694 * rough estimation to see if the output line is too long and
695 * should not be counted (we can't do precise calculation
696 * anyway because we don't know if the error explanation part
697 * will be printed in update_local_ref)
698 */
699 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
700 if (len >= max)
701 continue;
702
703 if (width < rlen)
704 width = rlen;
705 }
706
707 return width;
708 }
709
710 static void display_state_init(struct display_state *display_state, struct ref *ref_map,
711 const char *raw_url, enum display_format format)
712 {
713 int i;
714
715 memset(display_state, 0, sizeof(*display_state));
716 strbuf_init(&display_state->buf, 0);
717 display_state->format = format;
718
719 if (raw_url)
720 display_state->url = transport_anonymize_url(raw_url);
721 else
722 display_state->url = xstrdup("foreign");
723
724 display_state->url_len = strlen(display_state->url);
725 for (i = display_state->url_len - 1; 0 <= i && display_state->url[i] == '/'; i--)
726 ;
727 display_state->url_len = i + 1;
728 if (4 < i && !strncmp(".git", display_state->url + i - 3, 4))
729 display_state->url_len = i - 3;
730
731 if (verbosity < 0)
732 return;
733
734 switch (display_state->format) {
735 case DISPLAY_FORMAT_FULL:
736 case DISPLAY_FORMAT_COMPACT:
737 display_state->refcol_width = refcol_width(ref_map,
738 display_state->format == DISPLAY_FORMAT_COMPACT);
739 break;
740 case DISPLAY_FORMAT_PORCELAIN:
741 /* We don't need to precompute anything here. */
742 break;
743 default:
744 BUG("unexpected display format %d", display_state->format);
745 }
746 }
747
748 static void display_state_release(struct display_state *display_state)
749 {
750 strbuf_release(&display_state->buf);
751 free(display_state->url);
752 }
753
754 static void print_remote_to_local(struct display_state *display_state,
755 const char *remote, const char *local)
756 {
757 strbuf_addf(&display_state->buf, "%-*s -> %s",
758 display_state->refcol_width, remote, local);
759 }
760
761 static int find_and_replace(struct strbuf *haystack,
762 const char *needle,
763 const char *placeholder)
764 {
765 const char *p = NULL;
766 int plen, nlen;
767
768 nlen = strlen(needle);
769 if (ends_with(haystack->buf, needle))
770 p = haystack->buf + haystack->len - nlen;
771 else
772 p = strstr(haystack->buf, needle);
773 if (!p)
774 return 0;
775
776 if (p > haystack->buf && p[-1] != '/')
777 return 0;
778
779 plen = strlen(p);
780 if (plen > nlen && p[nlen] != '/')
781 return 0;
782
783 strbuf_splice(haystack, p - haystack->buf, nlen,
784 placeholder, strlen(placeholder));
785 return 1;
786 }
787
788 static void print_compact(struct display_state *display_state,
789 const char *remote, const char *local)
790 {
791 struct strbuf r = STRBUF_INIT;
792 struct strbuf l = STRBUF_INIT;
793
794 if (!strcmp(remote, local)) {
795 strbuf_addf(&display_state->buf, "%-*s -> *", display_state->refcol_width, remote);
796 return;
797 }
798
799 strbuf_addstr(&r, remote);
800 strbuf_addstr(&l, local);
801
802 if (!find_and_replace(&r, local, "*"))
803 find_and_replace(&l, remote, "*");
804 print_remote_to_local(display_state, r.buf, l.buf);
805
806 strbuf_release(&r);
807 strbuf_release(&l);
808 }
809
810 static void display_ref_update(struct display_state *display_state, char code,
811 const char *summary, const char *error,
812 const char *remote, const char *local,
813 const struct object_id *old_oid,
814 const struct object_id *new_oid,
815 int summary_width)
816 {
817 FILE *f = stderr;
818
819 if (verbosity < 0)
820 return;
821
822 strbuf_reset(&display_state->buf);
823
824 switch (display_state->format) {
825 case DISPLAY_FORMAT_FULL:
826 case DISPLAY_FORMAT_COMPACT: {
827 int width;
828
829 if (!display_state->shown_url) {
830 strbuf_addf(&display_state->buf, _("From %.*s\n"),
831 display_state->url_len, display_state->url);
832 display_state->shown_url = 1;
833 }
834
835 width = (summary_width + strlen(summary) - gettext_width(summary));
836 remote = prettify_refname(remote);
837 local = prettify_refname(local);
838
839 strbuf_addf(&display_state->buf, " %c %-*s ", code, width, summary);
840
841 if (display_state->format != DISPLAY_FORMAT_COMPACT)
842 print_remote_to_local(display_state, remote, local);
843 else
844 print_compact(display_state, remote, local);
845
846 if (error)
847 strbuf_addf(&display_state->buf, " (%s)", error);
848
849 break;
850 }
851 case DISPLAY_FORMAT_PORCELAIN:
852 strbuf_addf(&display_state->buf, "%c %s %s %s", code,
853 oid_to_hex(old_oid), oid_to_hex(new_oid), local);
854 f = stdout;
855 break;
856 default:
857 BUG("unexpected display format %d", display_state->format);
858 };
859 strbuf_addch(&display_state->buf, '\n');
860
861 fputs(display_state->buf.buf, f);
862 }
863
864 struct ref_update_display_info {
865 bool failed;
866 char success_code;
867 char fail_code;
868 char *summary;
869 char *fail_detail;
870 char *success_detail;
871 char *ref;
872 char *remote;
873 struct object_id old_oid;
874 struct object_id new_oid;
875 };
876
877 struct ref_update_display_info_array {
878 struct ref_update_display_info *info;
879 size_t alloc, nr;
880 };
881
882 static struct ref_update_display_info *ref_update_display_info_append(
883 struct ref_update_display_info_array *array,
884 char success_code,
885 char fail_code,
886 const char *summary,
887 const char *success_detail,
888 const char *fail_detail,
889 const char *ref,
890 const char *remote,
891 const struct object_id *old_oid,
892 const struct object_id *new_oid)
893 {
894 struct ref_update_display_info *info;
895
896 ALLOC_GROW(array->info, array->nr + 1, array->alloc);
897 info = &array->info[array->nr++];
898
899 info->failed = false;
900 info->success_code = success_code;
901 info->fail_code = fail_code;
902 info->summary = xstrdup(summary);
903 info->success_detail = xstrdup_or_null(success_detail);
904 info->fail_detail = xstrdup_or_null(fail_detail);
905 info->remote = xstrdup(remote);
906 info->ref = xstrdup(ref);
907
908 oidcpy(&info->old_oid, old_oid);
909 oidcpy(&info->new_oid, new_oid);
910
911 return info;
912 }
913
914 static void ref_update_display_info_set_failed(struct ref_update_display_info *info)
915 {
916 info->failed = true;
917 }
918
919 static void ref_update_display_info_free(struct ref_update_display_info *info)
920 {
921 free(info->summary);
922 free(info->success_detail);
923 free(info->fail_detail);
924 free(info->remote);
925 free(info->ref);
926 }
927
928 static void ref_update_display_info_display(struct ref_update_display_info *info,
929 struct display_state *display_state,
930 int summary_width)
931 {
932 display_ref_update(display_state,
933 info->failed ? info->fail_code : info->success_code,
934 info->summary,
935 info->failed ? info->fail_detail : info->success_detail,
936 info->remote, info->ref, &info->old_oid,
937 &info->new_oid, summary_width);
938 }
939
940 static int update_local_ref(struct ref *ref,
941 struct ref_transaction *transaction,
942 const struct ref *remote_ref,
943 const struct fetch_config *config,
944 struct ref_update_display_info_array *display_array)
945 {
946 struct commit *current = NULL, *updated;
947 int fast_forward = 0;
948
949 if (!odb_has_object(the_repository->objects, &ref->new_oid,
950 ODB_HAS_OBJECT_RECHECK_PACKED | ODB_HAS_OBJECT_FETCH_PROMISOR))
951 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
952
953 if (oideq(&ref->old_oid, &ref->new_oid)) {
954 if (verbosity > 0)
955 ref_update_display_info_append(display_array, '=', '=',
956 _("[up to date]"), NULL,
957 NULL, ref->name,
958 remote_ref->name, &ref->old_oid,
959 &ref->new_oid);
960 return 0;
961 }
962
963 if (!update_head_ok &&
964 !is_null_oid(&ref->old_oid) &&
965 branch_checked_out(ref->name)) {
966 struct ref_update_display_info *info;
967 /*
968 * If this is the head, and it's not okay to update
969 * the head, and the old value of the head isn't empty...
970 */
971 info = ref_update_display_info_append(display_array, '!', '!',
972 _("[rejected]"), NULL,
973 _("can't fetch into checked-out branch"),
974 ref->name, remote_ref->name,
975 &ref->old_oid, &ref->new_oid);
976 ref_update_display_info_set_failed(info);
977 return 1;
978 }
979
980 if (!is_null_oid(&ref->old_oid) &&
981 starts_with(ref->name, "refs/tags/")) {
982 struct ref_update_display_info *info;
983
984 if (force || ref->force) {
985 int r;
986
987 r = s_update_ref("updating tag", ref, transaction, 0);
988
989 info = ref_update_display_info_append(display_array, 't', '!',
990 _("[tag update]"), NULL,
991 _("unable to update local ref"),
992 ref->name, remote_ref->name,
993 &ref->old_oid, &ref->new_oid);
994 if (r)
995 ref_update_display_info_set_failed(info);
996
997 return r;
998 } else {
999 info = ref_update_display_info_append(display_array, '!', '!',
1000 _("[rejected]"), NULL,
1001 _("would clobber existing tag"),
1002 ref->name, remote_ref->name,
1003 &ref->old_oid, &ref->new_oid);
1004 ref_update_display_info_set_failed(info);
1005 return 1;
1006 }
1007 }
1008
1009 current = lookup_commit_reference_gently(the_repository,
1010 &ref->old_oid, 1);
1011 updated = lookup_commit_reference_gently(the_repository,
1012 &ref->new_oid, 1);
1013 if (!current || !updated) {
1014 struct ref_update_display_info *info;
1015 const char *msg;
1016 const char *what;
1017 int r;
1018 /*
1019 * Nicely describe the new ref we're fetching.
1020 * Base this on the remote's ref name, as it's
1021 * more likely to follow a standard layout.
1022 */
1023 if (starts_with(remote_ref->name, "refs/tags/")) {
1024 msg = "storing tag";
1025 what = _("[new tag]");
1026 } else if (starts_with(remote_ref->name, "refs/heads/")) {
1027 msg = "storing head";
1028 what = _("[new branch]");
1029 } else {
1030 msg = "storing ref";
1031 what = _("[new ref]");
1032 }
1033
1034 r = s_update_ref(msg, ref, transaction, 0);
1035
1036 info = ref_update_display_info_append(display_array, '*', '!',
1037 what, NULL,
1038 _("unable to update local ref"),
1039 ref->name, remote_ref->name,
1040 &ref->old_oid, &ref->new_oid);
1041 if (r)
1042 ref_update_display_info_set_failed(info);
1043
1044 return r;
1045 }
1046
1047 if (config->show_forced_updates) {
1048 uint64_t t_before = getnanotime();
1049 fast_forward = repo_in_merge_bases(the_repository, current,
1050 updated);
1051 if (fast_forward < 0)
1052 die(NULL);
1053 forced_updates_ms += (getnanotime() - t_before) / 1000000;
1054 } else {
1055 fast_forward = 1;
1056 }
1057
1058 if (fast_forward) {
1059 struct ref_update_display_info *info;
1060 struct strbuf quickref = STRBUF_INIT;
1061 int r;
1062
1063 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
1064 strbuf_addstr(&quickref, "..");
1065 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
1066 r = s_update_ref("fast-forward", ref, transaction, 1);
1067
1068 info = ref_update_display_info_append(display_array, ' ', '!',
1069 quickref.buf, NULL,
1070 _("unable to update local ref"),
1071 ref->name, remote_ref->name,
1072 &ref->old_oid, &ref->new_oid);
1073 if (r)
1074 ref_update_display_info_set_failed(info);
1075
1076 strbuf_release(&quickref);
1077 return r;
1078 } else if (force || ref->force) {
1079 struct ref_update_display_info *info;
1080 struct strbuf quickref = STRBUF_INIT;
1081 int r;
1082
1083 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
1084 strbuf_addstr(&quickref, "...");
1085 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
1086 r = s_update_ref("forced-update", ref, transaction, 1);
1087
1088 info = ref_update_display_info_append(display_array, '+', '!',
1089 quickref.buf, _("forced update"),
1090 _("unable to update local ref"),
1091 ref->name, remote_ref->name,
1092 &ref->old_oid, &ref->new_oid);
1093
1094 if (r)
1095 ref_update_display_info_set_failed(info);
1096
1097 strbuf_release(&quickref);
1098 return r;
1099 } else {
1100 struct ref_update_display_info *info;
1101 info = ref_update_display_info_append(display_array, '!', '!',
1102 _("[rejected]"), NULL,
1103 _("non-fast-forward"),
1104 ref->name, remote_ref->name,
1105 &ref->old_oid, &ref->new_oid);
1106 ref_update_display_info_set_failed(info);
1107 return 1;
1108 }
1109 }
1110
1111 static const struct object_id *iterate_ref_map(void *cb_data)
1112 {
1113 struct ref **rm = cb_data;
1114 struct ref *ref = *rm;
1115
1116 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
1117 ref = ref->next;
1118 if (!ref)
1119 return NULL;
1120 *rm = ref->next;
1121 return &ref->old_oid;
1122 }
1123
1124 struct fetch_head {
1125 FILE *fp;
1126 struct strbuf buf;
1127 };
1128
1129 static int open_fetch_head(struct fetch_head *fetch_head)
1130 {
1131 const char *filename = git_path_fetch_head(the_repository);
1132
1133 if (write_fetch_head) {
1134 fetch_head->fp = fopen(filename, "a");
1135 if (!fetch_head->fp)
1136 return error_errno(_("cannot open '%s'"), filename);
1137 strbuf_init(&fetch_head->buf, 0);
1138 } else {
1139 fetch_head->fp = NULL;
1140 }
1141
1142 return 0;
1143 }
1144
1145 static void append_fetch_head(struct fetch_head *fetch_head,
1146 const struct object_id *old_oid,
1147 enum fetch_head_status fetch_head_status,
1148 const char *note,
1149 const char *url, size_t url_len)
1150 {
1151 char old_oid_hex[GIT_MAX_HEXSZ + 1];
1152 const char *merge_status_marker;
1153 size_t i;
1154
1155 if (!fetch_head->fp)
1156 return;
1157
1158 switch (fetch_head_status) {
1159 case FETCH_HEAD_NOT_FOR_MERGE:
1160 merge_status_marker = "not-for-merge";
1161 break;
1162 case FETCH_HEAD_MERGE:
1163 merge_status_marker = "";
1164 break;
1165 default:
1166 /* do not write anything to FETCH_HEAD */
1167 return;
1168 }
1169
1170 strbuf_addf(&fetch_head->buf, "%s\t%s\t%s",
1171 oid_to_hex_r(old_oid_hex, old_oid), merge_status_marker, note);
1172 for (i = 0; i < url_len; ++i)
1173 if ('\n' == url[i])
1174 strbuf_addstr(&fetch_head->buf, "\\n");
1175 else
1176 strbuf_addch(&fetch_head->buf, url[i]);
1177 strbuf_addch(&fetch_head->buf, '\n');
1178
1179 /*
1180 * When using an atomic fetch, we do not want to update FETCH_HEAD if
1181 * any of the reference updates fails. We thus have to write all
1182 * updates to a buffer first and only commit it as soon as all
1183 * references have been successfully updated.
1184 */
1185 if (!atomic_fetch) {
1186 strbuf_write(&fetch_head->buf, fetch_head->fp);
1187 strbuf_reset(&fetch_head->buf);
1188 }
1189 }
1190
1191 static void commit_fetch_head(struct fetch_head *fetch_head)
1192 {
1193 if (!fetch_head->fp || !atomic_fetch)
1194 return;
1195 strbuf_write(&fetch_head->buf, fetch_head->fp);
1196 }
1197
1198 static void close_fetch_head(struct fetch_head *fetch_head)
1199 {
1200 if (!fetch_head->fp)
1201 return;
1202
1203 fclose(fetch_head->fp);
1204 strbuf_release(&fetch_head->buf);
1205 }
1206
1207 static const char warn_show_forced_updates[] =
1208 N_("fetch normally indicates which branches had a forced update,\n"
1209 "but that check has been disabled; to re-enable, use '--show-forced-updates'\n"
1210 "flag or run 'git config fetch.showForcedUpdates true'");
1211 static const char warn_time_show_forced_updates[] =
1212 N_("it took %.2f seconds to check forced updates; you can use\n"
1213 "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
1214 "to avoid this check\n");
1215
1216 static int store_updated_refs(struct display_state *display_state,
1217 int connectivity_checked,
1218 struct ref_transaction *transaction, struct ref *ref_map,
1219 struct fetch_head *fetch_head,
1220 const struct fetch_config *config,
1221 struct ref_update_display_info_array *display_array)
1222 {
1223 int rc = 0;
1224 struct strbuf note = STRBUF_INIT;
1225 const char *what, *kind;
1226 struct ref *rm;
1227 int want_status;
1228
1229 if (!connectivity_checked) {
1230 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1231
1232 opt.exclude_hidden_refs_section = "fetch";
1233 rm = ref_map;
1234 if (check_connected(iterate_ref_map, &rm, &opt)) {
1235 rc = error(_("%s did not send all necessary objects"),
1236 display_state->url);
1237 goto abort;
1238 }
1239 }
1240
1241 /*
1242 * We do a pass for each fetch_head_status type in their enum order, so
1243 * merged entries are written before not-for-merge. That lets readers
1244 * use FETCH_HEAD as a refname to refer to the ref to be merged.
1245 */
1246 for (want_status = FETCH_HEAD_MERGE;
1247 want_status <= FETCH_HEAD_IGNORE;
1248 want_status++) {
1249 for (rm = ref_map; rm; rm = rm->next) {
1250 struct ref *ref = NULL;
1251
1252 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
1253 if (want_status == FETCH_HEAD_MERGE)
1254 warning(_("rejected %s because shallow roots are not allowed to be updated"),
1255 rm->peer_ref ? rm->peer_ref->name : rm->name);
1256 continue;
1257 }
1258
1259 /*
1260 * When writing FETCH_HEAD we need to determine whether
1261 * we already have the commit or not. If not, then the
1262 * reference is not for merge and needs to be written
1263 * to the reflog after other commits which we already
1264 * have. We're not interested in this property though
1265 * in case FETCH_HEAD is not to be updated, so we can
1266 * skip the classification in that case.
1267 */
1268 if (fetch_head->fp) {
1269 struct commit *commit = NULL;
1270
1271 /*
1272 * References in "refs/tags/" are often going to point
1273 * to annotated tags, which are not part of the
1274 * commit-graph. We thus only try to look up refs in
1275 * the graph which are not in that namespace to not
1276 * regress performance in repositories with many
1277 * annotated tags.
1278 */
1279 if (!starts_with(rm->name, "refs/tags/"))
1280 commit = lookup_commit_in_graph(the_repository, &rm->old_oid);
1281 if (!commit) {
1282 commit = lookup_commit_reference_gently(the_repository,
1283 &rm->old_oid,
1284 1);
1285 if (!commit)
1286 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
1287 }
1288 }
1289
1290 if (rm->fetch_head_status != want_status)
1291 continue;
1292
1293 if (rm->peer_ref) {
1294 ref = alloc_ref(rm->peer_ref->name);
1295 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
1296 oidcpy(&ref->new_oid, &rm->old_oid);
1297 ref->force = rm->peer_ref->force;
1298 }
1299
1300 if (config->recurse_submodules != RECURSE_SUBMODULES_OFF &&
1301 (!rm->peer_ref || !oideq(&ref->old_oid, &ref->new_oid))) {
1302 check_for_new_submodule_commits(&rm->old_oid);
1303 }
1304
1305 if (!strcmp(rm->name, "HEAD")) {
1306 kind = "";
1307 what = "";
1308 } else if (skip_prefix(rm->name, "refs/heads/", &what)) {
1309 kind = "branch";
1310 } else if (skip_prefix(rm->name, "refs/tags/", &what)) {
1311 kind = "tag";
1312 } else if (skip_prefix(rm->name, "refs/remotes/", &what)) {
1313 kind = "remote-tracking branch";
1314 } else {
1315 kind = "";
1316 what = rm->name;
1317 }
1318
1319 strbuf_reset(&note);
1320 if (*what) {
1321 if (*kind)
1322 strbuf_addf(&note, "%s ", kind);
1323 strbuf_addf(&note, "'%s' of ", what);
1324 }
1325
1326 append_fetch_head(fetch_head, &rm->old_oid,
1327 rm->fetch_head_status,
1328 note.buf, display_state->url,
1329 display_state->url_len);
1330
1331 if (ref) {
1332 rc |= update_local_ref(ref, transaction, rm,
1333 config, display_array);
1334 free(ref);
1335 } else if (write_fetch_head || dry_run) {
1336 /*
1337 * Display fetches written to FETCH_HEAD (or
1338 * would be written to FETCH_HEAD, if --dry-run
1339 * is set).
1340 */
1341
1342 ref_update_display_info_append(display_array, '*', '*',
1343 *kind ? kind : "branch",
1344 NULL, NULL, "FETCH_HEAD",
1345 rm->name, &rm->new_oid,
1346 &rm->old_oid);
1347 }
1348 }
1349 }
1350
1351 if (advice_enabled(ADVICE_FETCH_SHOW_FORCED_UPDATES)) {
1352 if (!config->show_forced_updates) {
1353 warning(_(warn_show_forced_updates));
1354 } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
1355 warning(_(warn_time_show_forced_updates),
1356 forced_updates_ms / 1000.0);
1357 }
1358 }
1359
1360 abort:
1361 strbuf_release(&note);
1362 return rc;
1363 }
1364
1365 /*
1366 * We would want to bypass the object transfer altogether if
1367 * everything we are going to fetch already exists and is connected
1368 * locally.
1369 */
1370 static int check_exist_and_connected(struct ref *ref_map)
1371 {
1372 struct ref *rm = ref_map;
1373 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1374 struct ref *r;
1375
1376 /*
1377 * If we are deepening a shallow clone we already have these
1378 * objects reachable. Running rev-list here will return with
1379 * a good (0) exit status and we'll bypass the fetch that we
1380 * really need to perform. Claiming failure now will ensure
1381 * we perform the network exchange to deepen our history.
1382 */
1383 if (deepen)
1384 return -1;
1385
1386 /*
1387 * Similarly, if we need to refetch, we always want to perform a full
1388 * fetch ignoring existing objects.
1389 */
1390 if (refetch)
1391 return -1;
1392
1393
1394 /*
1395 * check_connected() allows objects to merely be promised, but
1396 * we need all direct targets to exist.
1397 */
1398 for (r = rm; r; r = r->next) {
1399 if (!odb_has_object(the_repository->objects, &r->old_oid,
1400 ODB_HAS_OBJECT_RECHECK_PACKED))
1401 return -1;
1402 }
1403
1404 opt.quiet = 1;
1405 opt.exclude_hidden_refs_section = "fetch";
1406 return check_connected(iterate_ref_map, &rm, &opt);
1407 }
1408
1409 static int fetch_and_consume_refs(struct display_state *display_state,
1410 struct transport *transport,
1411 struct ref_transaction *transaction,
1412 struct ref *ref_map,
1413 struct fetch_head *fetch_head,
1414 const struct fetch_config *config,
1415 struct ref_update_display_info_array *display_array)
1416 {
1417 int connectivity_checked = 1;
1418 int ret;
1419
1420 /*
1421 * We don't need to perform a fetch in case we can already satisfy all
1422 * refs.
1423 */
1424 ret = check_exist_and_connected(ref_map);
1425 if (ret) {
1426 trace2_region_enter("fetch", "fetch_refs", the_repository);
1427 ret = transport_fetch_refs(transport, ref_map);
1428 trace2_region_leave("fetch", "fetch_refs", the_repository);
1429 if (ret)
1430 goto out;
1431 connectivity_checked = transport->smart_options ?
1432 transport->smart_options->connectivity_checked : 0;
1433 }
1434
1435 trace2_region_enter("fetch", "consume_refs", the_repository);
1436 ret = store_updated_refs(display_state, connectivity_checked,
1437 transaction, ref_map, fetch_head, config,
1438 display_array);
1439 trace2_region_leave("fetch", "consume_refs", the_repository);
1440
1441 out:
1442 transport_unlock_pack(transport, 0);
1443 return ret;
1444 }
1445
1446 static int prune_refs(struct display_state *display_state,
1447 struct refspec *rs,
1448 struct ref_transaction *transaction,
1449 struct ref *ref_map)
1450 {
1451 int result = 0;
1452 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
1453 struct strbuf err = STRBUF_INIT;
1454 struct string_list refnames = STRING_LIST_INIT_NODUP;
1455
1456 for (ref = stale_refs; ref; ref = ref->next)
1457 string_list_append(&refnames, ref->name);
1458
1459 if (!dry_run) {
1460 if (transaction) {
1461 for (ref = stale_refs; ref; ref = ref->next) {
1462 result = ref_transaction_delete(transaction, ref->name, NULL,
1463 NULL, 0, "fetch: prune", &err);
1464 if (result)
1465 goto cleanup;
1466 }
1467 } else {
1468 result = refs_delete_refs(get_main_ref_store(the_repository),
1469 "fetch: prune", &refnames,
1470 0);
1471 }
1472 }
1473
1474 if (verbosity >= 0) {
1475 int summary_width = transport_summary_width(stale_refs);
1476
1477 for (ref = stale_refs; ref; ref = ref->next) {
1478 display_ref_update(display_state, '-', _("[deleted]"), NULL,
1479 _("(none)"), ref->name,
1480 &ref->new_oid, &ref->old_oid,
1481 summary_width);
1482 }
1483 string_list_sort(&refnames);
1484 refs_warn_dangling_symrefs(get_main_ref_store(the_repository),
1485 stderr, " ", dry_run, &refnames);
1486 }
1487
1488 cleanup:
1489 string_list_clear(&refnames, 0);
1490 strbuf_release(&err);
1491 free_refs(stale_refs);
1492 return result;
1493 }
1494
1495 static void check_not_current_branch(struct ref *ref_map)
1496 {
1497 const char *path;
1498 for (; ref_map; ref_map = ref_map->next)
1499 if (ref_map->peer_ref &&
1500 starts_with(ref_map->peer_ref->name, "refs/heads/") &&
1501 (path = branch_checked_out(ref_map->peer_ref->name)))
1502 die(_("refusing to fetch into branch '%s' "
1503 "checked out at '%s'"),
1504 ref_map->peer_ref->name, path);
1505 }
1506
1507 static int truncate_fetch_head(void)
1508 {
1509 const char *filename = git_path_fetch_head(the_repository);
1510 FILE *fp = fopen_for_writing(filename);
1511
1512 if (!fp)
1513 return error_errno(_("cannot open '%s'"), filename);
1514 fclose(fp);
1515 return 0;
1516 }
1517
1518 static void set_option(struct transport *transport, const char *name, const char *value)
1519 {
1520 int r = transport_set_option(transport, name, value);
1521 if (r < 0)
1522 die(_("option \"%s\" value \"%s\" is not valid for %s"),
1523 name, value, transport->url);
1524 if (r > 0)
1525 warning(_("option \"%s\" is ignored for %s"),
1526 name, transport->url);
1527 }
1528
1529
1530 static int add_oid(const struct reference *ref, void *cb_data)
1531 {
1532 struct oid_array *oids = cb_data;
1533
1534 oid_array_append(oids, ref->oid);
1535 return 0;
1536 }
1537
1538 static void add_negotiation_tips(struct string_list *input_list,
1539 struct oid_array **output_list,
1540 const char *argname)
1541 {
1542 struct oid_array *oids = xcalloc(1, sizeof(*oids));
1543 int i;
1544
1545 for (i = 0; i < input_list->nr; i++) {
1546 const char *s = input_list->items[i].string;
1547 struct refs_for_each_ref_options opts = {
1548 .pattern = s,
1549 };
1550 int old_nr;
1551 if (!has_glob_specials(s)) {
1552 struct object_id oid;
1553
1554 /* Ignore missing reference. */
1555 if (repo_get_oid(the_repository, s, &oid))
1556 continue;
1557 /* Fail on missing object pointed by ref. */
1558 if (!odb_has_object(the_repository->objects, &oid, 0))
1559 die(_("the object %s does not exist"), s);
1560
1561 oid_array_append(oids, &oid);
1562 continue;
1563 }
1564 old_nr = oids->nr;
1565 refs_for_each_ref_ext(get_main_ref_store(the_repository),
1566 add_oid, oids, &opts);
1567 if (old_nr == oids->nr)
1568 warning(_("ignoring %s=%s because it does not match any refs"),
1569 argname, s);
1570 }
1571 *output_list = oids;
1572 }
1573
1574 static struct transport *prepare_transport(struct remote *remote, int deepen,
1575 struct list_objects_filter_options *filter_options)
1576 {
1577 struct transport *transport;
1578
1579 transport = transport_get(remote, NULL);
1580 transport_set_verbosity(transport, verbosity, progress);
1581 transport->family = family;
1582 if (upload_pack)
1583 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1584 if (keep)
1585 set_option(transport, TRANS_OPT_KEEP, "yes");
1586 if (depth)
1587 set_option(transport, TRANS_OPT_DEPTH, depth);
1588 if (deepen && deepen_since)
1589 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
1590 if (deepen && deepen_not.nr)
1591 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1592 (const char *)&deepen_not);
1593 if (deepen_relative)
1594 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
1595 if (update_shallow)
1596 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
1597 if (refetch)
1598 set_option(transport, TRANS_OPT_REFETCH, "yes");
1599 if (filter_options->choice) {
1600 const char *spec =
1601 expand_list_objects_filter_spec(filter_options);
1602 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec);
1603 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1604 }
1605 if (negotiation_restrict.nr) {
1606 if (transport->smart_options)
1607 add_negotiation_tips(&negotiation_restrict,
1608 &transport->smart_options->negotiation_restrict_tips,
1609 "--negotiation-restrict");
1610 else
1611 warning(_("ignoring %s because the protocol does not support it"),
1612 "--negotiation-restrict");
1613 } else if (remote->negotiation_restrict.nr) {
1614 struct string_list_item *item;
1615 for_each_string_list_item(item, &remote->negotiation_restrict)
1616 string_list_append(&negotiation_restrict, item->string);
1617 if (transport->smart_options)
1618 add_negotiation_tips(&negotiation_restrict,
1619 &transport->smart_options->negotiation_restrict_tips,
1620 "--negotiation-restrict");
1621 else {
1622 struct strbuf config_name = STRBUF_INIT;
1623 strbuf_addf(&config_name, "remote.%s.negotiationRestrict", remote->name);
1624 warning(_("ignoring %s because the protocol does not support it"),
1625 config_name.buf);
1626 strbuf_release(&config_name);
1627 }
1628 }
1629 if (negotiation_include.nr) {
1630 if (transport->smart_options)
1631 add_negotiation_tips(&negotiation_include,
1632 &transport->smart_options->negotiation_include_tips,
1633 "--negotiation-include");
1634 else
1635 warning(_("ignoring %s because the protocol does not support it"),
1636 "--negotiation-include");
1637 } else if (remote->negotiation_include.nr) {
1638 if (transport->smart_options) {
1639 add_negotiation_tips(&remote->negotiation_include,
1640 &transport->smart_options->negotiation_include_tips,
1641 "--negotiation-include");
1642 } else {
1643 struct strbuf config_name = STRBUF_INIT;
1644 strbuf_addf(&config_name, "remote.%s.negotiationInclude", remote->name);
1645 warning(_("ignoring %s because the protocol does not support it"),
1646 config_name.buf);
1647 strbuf_release(&config_name);
1648 }
1649 }
1650 return transport;
1651 }
1652
1653 static int backfill_tags(struct display_state *display_state,
1654 struct transport *transport,
1655 struct ref_transaction *transaction,
1656 struct ref *ref_map,
1657 struct fetch_head *fetch_head,
1658 const struct fetch_config *config,
1659 struct ref_update_display_info_array *display_array,
1660 struct list_objects_filter_options *filter_options)
1661 {
1662 int retcode, cannot_reuse;
1663
1664 /*
1665 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1666 * when remote helper is used (setting it to an empty string
1667 * is not unsetting). We could extend the remote helper
1668 * protocol for that, but for now, just force a new connection
1669 * without deepen-since. Similar story for deepen-not.
1670 */
1671 cannot_reuse = transport->cannot_reuse ||
1672 deepen_since || deepen_not.nr;
1673 if (cannot_reuse) {
1674 gsecondary = prepare_transport(transport->remote, 0, filter_options);
1675 transport = gsecondary;
1676 }
1677
1678 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1679 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
1680 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
1681 retcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map,
1682 fetch_head, config, display_array);
1683
1684 if (gsecondary) {
1685 transport_disconnect(gsecondary);
1686 gsecondary = NULL;
1687 }
1688
1689 return retcode;
1690 }
1691
1692 static const char *strip_refshead(const char *name){
1693 skip_prefix(name, "refs/heads/", &name);
1694 return name;
1695 }
1696
1697 static void set_head_advice_msg(const char *remote, const char *head_name)
1698 {
1699 const char message_advice_set_head[] =
1700 N_("Run 'git remote set-head %s %s' to follow the change, or set\n"
1701 "'remote.%s.followRemoteHEAD' configuration option to a different value\n"
1702 "if you do not want to see this message. Specifically running\n"
1703 "'git config set remote.%s.followRemoteHEAD warn-if-not-branch-%s'\n"
1704 "will disable the warning until the remote changes HEAD to something else.");
1705
1706 advise_if_enabled(ADVICE_FETCH_SET_HEAD_WARN, _(message_advice_set_head),
1707 remote, head_name, remote, remote, head_name);
1708 }
1709
1710 static void report_set_head(const char *remote, const char *head_name,
1711 struct strbuf *buf_prev, int updateres) {
1712 struct strbuf buf_prefix = STRBUF_INIT;
1713 const char *prev_head = NULL;
1714
1715 strbuf_addf(&buf_prefix, "refs/remotes/%s/", remote);
1716 skip_prefix(buf_prev->buf, buf_prefix.buf, &prev_head);
1717
1718 if (prev_head && strcmp(prev_head, head_name)) {
1719 printf("'HEAD' at '%s' is '%s', but we have '%s' locally.\n",
1720 remote, head_name, prev_head);
1721 set_head_advice_msg(remote, head_name);
1722 }
1723 else if (updateres && buf_prev->len) {
1724 printf("'HEAD' at '%s' is '%s', "
1725 "but we have a detached HEAD pointing to '%s' locally.\n",
1726 remote, head_name, buf_prev->buf);
1727 set_head_advice_msg(remote, head_name);
1728 }
1729 strbuf_release(&buf_prefix);
1730 }
1731
1732 static int set_head(const struct ref *remote_refs, struct remote *remote)
1733 {
1734 int result = 0, create_only, baremirror, was_detached;
1735 struct strbuf b_head = STRBUF_INIT, b_remote_head = STRBUF_INIT,
1736 b_local_head = STRBUF_INIT;
1737 int follow_remote_head = remote->follow_remote_head;
1738 const char *no_warn_branch = remote->no_warn_branch;
1739 char *head_name = NULL;
1740 struct ref *ref, *matches;
1741 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
1742 struct refspec_item refspec = {
1743 .force = 0,
1744 .pattern = 1,
1745 .src = (char *) "refs/heads/*",
1746 .dst = (char *) "refs/heads/*",
1747 };
1748 struct string_list heads = STRING_LIST_INIT_DUP;
1749 struct ref_store *refs = get_main_ref_store(the_repository);
1750
1751 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
1752 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
1753 fetch_map, REMOTE_GUESS_HEAD_ALL);
1754 for (ref = matches; ref; ref = ref->next) {
1755 string_list_append(&heads, strip_refshead(ref->name));
1756 }
1757
1758 if (!heads.nr)
1759 result = 1;
1760 else if (heads.nr > 1)
1761 result = 1;
1762 else
1763 head_name = xstrdup(heads.items[0].string);
1764
1765 if (!head_name)
1766 goto cleanup;
1767 baremirror = is_bare_repository() && remote->mirror;
1768 create_only = follow_remote_head == FOLLOW_REMOTE_ALWAYS ? 0 : !baremirror;
1769 if (baremirror) {
1770 strbuf_addstr(&b_head, "HEAD");
1771 strbuf_addf(&b_remote_head, "refs/heads/%s", head_name);
1772 } else {
1773 strbuf_addf(&b_head, "refs/remotes/%s/HEAD", remote->name);
1774 strbuf_addf(&b_remote_head, "refs/remotes/%s/%s", remote->name, head_name);
1775 }
1776 /* make sure it's valid */
1777 if (!baremirror && !refs_ref_exists(refs, b_remote_head.buf)) {
1778 result = 1;
1779 goto cleanup;
1780 }
1781 was_detached = refs_update_symref_extended(refs, b_head.buf, b_remote_head.buf,
1782 "fetch", &b_local_head, create_only);
1783 if (was_detached == -1) {
1784 result = 1;
1785 goto cleanup;
1786 }
1787 if (verbosity >= 0 &&
1788 follow_remote_head == FOLLOW_REMOTE_WARN &&
1789 (!no_warn_branch || strcmp(no_warn_branch, head_name)))
1790 report_set_head(remote->name, head_name, &b_local_head, was_detached);
1791
1792 cleanup:
1793 free(head_name);
1794 free_refs(fetch_map);
1795 free_refs(matches);
1796 string_list_clear(&heads, 0);
1797 strbuf_release(&b_head);
1798 strbuf_release(&b_local_head);
1799 strbuf_release(&b_remote_head);
1800 return result;
1801 }
1802
1803 struct ref_rejection_data {
1804 int *retcode;
1805 bool conflict_msg_shown;
1806 bool case_sensitive_msg_shown;
1807 const char *remote_name;
1808 struct strmap *rejected_refs;
1809 };
1810
1811 static void ref_transaction_rejection_handler(const char *refname,
1812 const struct object_id *old_oid UNUSED,
1813 const struct object_id *new_oid UNUSED,
1814 const char *old_target UNUSED,
1815 const char *new_target UNUSED,
1816 enum ref_transaction_error err,
1817 const char *details,
1818 void *cb_data)
1819 {
1820 struct ref_rejection_data *data = cb_data;
1821
1822 if (err == REF_TRANSACTION_ERROR_CASE_CONFLICT && ignore_case &&
1823 !data->case_sensitive_msg_shown) {
1824 error(_("You're on a case-insensitive filesystem, and the remote you are\n"
1825 "trying to fetch from has references that only differ in casing. It\n"
1826 "is impossible to store such references with the 'files' backend. You\n"
1827 "can either accept this as-is, in which case you won't be able to\n"
1828 "store all remote references on disk. Or you can alternatively\n"
1829 "migrate your repository to use the 'reftable' backend with the\n"
1830 "following command:\n\n git refs migrate --ref-format=reftable\n\n"
1831 "Please keep in mind that not all implementations of Git support this\n"
1832 "new format yet. So if you use tools other than Git to access this\n"
1833 "repository it may not be an option to migrate to reftables.\n"));
1834 data->case_sensitive_msg_shown = true;
1835 } else if (err == REF_TRANSACTION_ERROR_NAME_CONFLICT &&
1836 !data->conflict_msg_shown) {
1837 error(_("some local refs could not be updated; try running\n"
1838 " 'git remote prune %s' to remove any old, conflicting "
1839 "branches"), data->remote_name);
1840 data->conflict_msg_shown = true;
1841 } else {
1842 if (details)
1843 error("%s", details);
1844 else
1845 error(_("fetching ref %s failed: %s"),
1846 refname, ref_transaction_error_msg(err));
1847 }
1848
1849 strmap_put(data->rejected_refs, refname, NULL);
1850 *data->retcode = 1;
1851 }
1852
1853 /*
1854 * Commit the reference transaction. If it isn't an atomic transaction, handle
1855 * rejected updates as part of using batched updates.
1856 */
1857 static int commit_ref_transaction(struct ref_transaction **transaction,
1858 bool is_atomic, const char *remote_name,
1859 struct strmap *rejected_refs,
1860 struct strbuf *err)
1861 {
1862 int retcode = ref_transaction_commit(*transaction, err);
1863 if (retcode)
1864 goto out;
1865
1866 if (!is_atomic) {
1867 struct ref_rejection_data data = {
1868 .conflict_msg_shown = 0,
1869 .remote_name = remote_name,
1870 .retcode = &retcode,
1871 .rejected_refs = rejected_refs,
1872 };
1873
1874 ref_transaction_for_each_rejected_update(*transaction,
1875 ref_transaction_rejection_handler,
1876 &data);
1877 }
1878
1879 out:
1880 ref_transaction_free(*transaction);
1881 *transaction = NULL;
1882 return retcode;
1883 }
1884
1885 static int do_fetch(struct transport *transport,
1886 struct refspec *rs,
1887 const struct fetch_config *config,
1888 struct list_objects_filter_options *filter_options)
1889 {
1890 struct ref_transaction *transaction = NULL;
1891 struct ref *ref_map = NULL;
1892 struct display_state display_state = { 0 };
1893 int autotags = (transport->remote->fetch_tags == 1);
1894 int retcode = 0;
1895 const struct ref *remote_refs;
1896 struct transport_ls_refs_options transport_ls_refs_options =
1897 TRANSPORT_LS_REFS_OPTIONS_INIT;
1898 struct fetch_head fetch_head = { 0 };
1899 struct strbuf err = STRBUF_INIT;
1900 int do_set_head = 0;
1901 struct ref_update_display_info_array display_array = { 0 };
1902 struct strmap rejected_refs = STRMAP_INIT;
1903 int summary_width = 0;
1904
1905 if (tags == TAGS_DEFAULT) {
1906 if (transport->remote->fetch_tags == 2)
1907 tags = TAGS_SET;
1908 if (transport->remote->fetch_tags == -1)
1909 tags = TAGS_UNSET;
1910 }
1911
1912 /* if not appending, truncate FETCH_HEAD */
1913 if (!append && write_fetch_head) {
1914 retcode = truncate_fetch_head();
1915 if (retcode)
1916 goto cleanup;
1917 }
1918
1919 if (rs->nr) {
1920 refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
1921 } else {
1922 struct branch *branch = branch_get(NULL);
1923
1924 if (transport->remote->fetch.nr) {
1925 refspec_ref_prefixes(&transport->remote->fetch,
1926 &transport_ls_refs_options.ref_prefixes);
1927 if (transport->remote->follow_remote_head != FOLLOW_REMOTE_NEVER)
1928 do_set_head = 1;
1929 }
1930 if (branch && branch_has_merge_config(branch) &&
1931 !strcmp(branch->remote_name, transport->remote->name)) {
1932 int i;
1933 for (i = 0; i < branch->merge_nr; i++) {
1934 strvec_push(&transport_ls_refs_options.ref_prefixes,
1935 branch->merge[i]->src);
1936 }
1937 }
1938
1939 /*
1940 * If there are no refs specified to fetch, then we just
1941 * fetch HEAD; mention that to narrow the advertisement.
1942 */
1943 if (!transport_ls_refs_options.ref_prefixes.nr)
1944 strvec_push(&transport_ls_refs_options.ref_prefixes,
1945 "HEAD");
1946 }
1947
1948 if (tags == TAGS_SET || tags == TAGS_DEFAULT)
1949 strvec_push(&transport_ls_refs_options.ref_prefixes,
1950 "refs/tags/");
1951
1952 if (do_set_head)
1953 strvec_push(&transport_ls_refs_options.ref_prefixes,
1954 "HEAD");
1955
1956 /*
1957 * Only initiate ref listing if we have at least one ref we want to
1958 * know about.
1959 */
1960 if (transport_ls_refs_options.ref_prefixes.nr) {
1961 trace2_region_enter("fetch", "remote_refs", the_repository);
1962 remote_refs = transport_get_remote_refs(transport,
1963 &transport_ls_refs_options);
1964 trace2_region_leave("fetch", "remote_refs", the_repository);
1965 } else
1966 remote_refs = NULL;
1967
1968 transport_ls_refs_options_release(&transport_ls_refs_options);
1969
1970 ref_map = get_ref_map(transport->remote, remote_refs, rs,
1971 tags, &autotags);
1972 if (!update_head_ok)
1973 check_not_current_branch(ref_map);
1974
1975 retcode = open_fetch_head(&fetch_head);
1976 if (retcode)
1977 goto cleanup;
1978
1979 display_state_init(&display_state, ref_map, transport->url,
1980 config->display_format);
1981
1982 if (atomic_fetch) {
1983 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
1984 0, &err);
1985 if (!transaction) {
1986 retcode = -1;
1987 goto cleanup;
1988 }
1989 }
1990
1991 if (tags == TAGS_DEFAULT && autotags)
1992 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
1993 if (prune) {
1994 /*
1995 * We only prune based on refspecs specified
1996 * explicitly (via command line or configuration); we
1997 * don't care whether --tags was specified.
1998 */
1999 if (rs->nr) {
2000 retcode = prune_refs(&display_state, rs, transaction, ref_map);
2001 } else {
2002 retcode = prune_refs(&display_state, &transport->remote->fetch,
2003 transaction, ref_map);
2004 }
2005 if (retcode != 0)
2006 retcode = 1;
2007 }
2008
2009 /*
2010 * If not atomic, we can still use batched updates, which would be much
2011 * more performant. We don't initiate the transaction before pruning,
2012 * since pruning must be an independent step, to avoid F/D conflicts.
2013 *
2014 * TODO: if reference transactions gain logical conflict resolution, we
2015 * can delete and create refs (with F/D conflicts) in the same transaction
2016 * and this can be moved above the 'prune_refs()' block.
2017 */
2018 if (!transaction) {
2019 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
2020 REF_TRANSACTION_ALLOW_FAILURE, &err);
2021 if (!transaction) {
2022 retcode = -1;
2023 goto cleanup;
2024 }
2025 }
2026
2027 if (fetch_and_consume_refs(&display_state, transport, transaction, ref_map,
2028 &fetch_head, config, &display_array)) {
2029 retcode = 1;
2030 goto cleanup;
2031 }
2032
2033 /*
2034 * If neither --no-tags nor --tags was specified, do automated tag
2035 * following.
2036 */
2037 if (tags == TAGS_DEFAULT && autotags) {
2038 struct ref *tags_ref_map = NULL, **tail = &tags_ref_map;
2039
2040 find_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);
2041 if (tags_ref_map) {
2042 /*
2043 * If backfilling of tags fails then we want to tell
2044 * the user so, but we have to continue regardless to
2045 * populate upstream information of the references we
2046 * have already fetched above. The exception though is
2047 * when `--atomic` is passed: in that case we'll abort
2048 * the transaction and don't commit anything.
2049 */
2050 if (backfill_tags(&display_state, transport, transaction, tags_ref_map,
2051 &fetch_head, config, &display_array, filter_options))
2052 retcode = 1;
2053 }
2054
2055 free_refs(tags_ref_map);
2056 }
2057
2058 if (retcode)
2059 goto cleanup;
2060
2061 if (verbosity >= 0)
2062 summary_width = transport_summary_width(ref_map);
2063
2064 retcode = commit_ref_transaction(&transaction, atomic_fetch,
2065 transport->remote->name,
2066 &rejected_refs, &err);
2067 /*
2068 * With '--atomic', bail out if the transaction fails. Without '--atomic',
2069 * continue to fetch head and perform other post-fetch operations.
2070 */
2071 if (retcode && atomic_fetch)
2072 goto cleanup;
2073
2074 commit_fetch_head(&fetch_head);
2075
2076 if (set_upstream) {
2077 struct branch *branch = branch_get("HEAD");
2078 struct ref *rm;
2079 struct ref *source_ref = NULL;
2080
2081 /*
2082 * We're setting the upstream configuration for the
2083 * current branch. The relevant upstream is the
2084 * fetched branch that is meant to be merged with the
2085 * current one, i.e. the one fetched to FETCH_HEAD.
2086 *
2087 * When there are several such branches, consider the
2088 * request ambiguous and err on the safe side by doing
2089 * nothing and just emit a warning.
2090 */
2091 for (rm = ref_map; rm; rm = rm->next) {
2092 if (!rm->peer_ref) {
2093 if (source_ref) {
2094 warning(_("multiple branches detected, incompatible with --set-upstream"));
2095 goto cleanup;
2096 } else {
2097 source_ref = rm;
2098 }
2099 }
2100 }
2101 if (source_ref) {
2102 if (!branch) {
2103 const char *shortname = source_ref->name;
2104 skip_prefix(shortname, "refs/heads/", &shortname);
2105
2106 warning(_("could not set upstream of HEAD to '%s' from '%s' when "
2107 "it does not point to any branch."),
2108 shortname, transport->remote->name);
2109 goto cleanup;
2110 }
2111
2112 if (!strcmp(source_ref->name, "HEAD") ||
2113 starts_with(source_ref->name, "refs/heads/"))
2114 install_branch_config(0,
2115 branch->name,
2116 transport->remote->name,
2117 source_ref->name);
2118 else if (starts_with(source_ref->name, "refs/remotes/"))
2119 warning(_("not setting upstream for a remote remote-tracking branch"));
2120 else if (starts_with(source_ref->name, "refs/tags/"))
2121 warning(_("not setting upstream for a remote tag"));
2122 else
2123 warning(_("unknown branch type"));
2124 } else {
2125 warning(_("no source branch found;\n"
2126 "you need to specify exactly one branch with the --set-upstream option"));
2127 }
2128 }
2129 if (do_set_head) {
2130 /*
2131 * Way too many cases where this can go wrong so let's just
2132 * ignore errors and fail silently for now.
2133 */
2134 set_head(remote_refs, transport->remote);
2135 }
2136
2137 cleanup:
2138 /*
2139 * When using batched updates, we want to commit the non-rejected
2140 * updates and also handle the rejections.
2141 */
2142 if (retcode && !atomic_fetch && transaction)
2143 commit_ref_transaction(&transaction, false,
2144 transport->remote->name,
2145 &rejected_refs, &err);
2146
2147 for (size_t i = 0; i < display_array.nr; i++) {
2148 struct ref_update_display_info *info = &display_array.info[i];
2149
2150 if (!info->failed && strmap_contains(&rejected_refs, info->ref))
2151 ref_update_display_info_set_failed(info);
2152 ref_update_display_info_display(info, &display_state, summary_width);
2153 ref_update_display_info_free(info);
2154 }
2155
2156 if (retcode) {
2157 if (err.len) {
2158 error("%s", err.buf);
2159 strbuf_reset(&err);
2160 }
2161 if (transaction && ref_transaction_abort(transaction, &err) &&
2162 err.len)
2163 error("%s", err.buf);
2164 transaction = NULL;
2165 }
2166
2167 if (transaction)
2168 ref_transaction_free(transaction);
2169
2170 free(display_array.info);
2171 strmap_clear(&rejected_refs, 0);
2172 display_state_release(&display_state);
2173 close_fetch_head(&fetch_head);
2174 strbuf_release(&err);
2175 free_refs(ref_map);
2176 return retcode;
2177 }
2178
2179 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
2180 {
2181 struct string_list *list = priv;
2182 if (!remote->skip_default_update)
2183 string_list_append(list, remote->name);
2184 return 0;
2185 }
2186
2187 static void add_options_to_argv(struct strvec *argv,
2188 const struct fetch_config *config)
2189 {
2190 if (dry_run)
2191 strvec_push(argv, "--dry-run");
2192 if (prune != -1)
2193 strvec_push(argv, prune ? "--prune" : "--no-prune");
2194 if (prune_tags != -1)
2195 strvec_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
2196 if (update_head_ok)
2197 strvec_push(argv, "--update-head-ok");
2198 if (force)
2199 strvec_push(argv, "--force");
2200 if (keep)
2201 strvec_push(argv, "--keep");
2202 if (config->recurse_submodules == RECURSE_SUBMODULES_ON)
2203 strvec_push(argv, "--recurse-submodules");
2204 else if (config->recurse_submodules == RECURSE_SUBMODULES_OFF)
2205 strvec_push(argv, "--no-recurse-submodules");
2206 else if (config->recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
2207 strvec_push(argv, "--recurse-submodules=on-demand");
2208 if (tags == TAGS_SET)
2209 strvec_push(argv, "--tags");
2210 else if (tags == TAGS_UNSET)
2211 strvec_push(argv, "--no-tags");
2212 if (verbosity >= 2)
2213 strvec_push(argv, "-v");
2214 if (verbosity >= 1)
2215 strvec_push(argv, "-v");
2216 else if (verbosity < 0)
2217 strvec_push(argv, "-q");
2218 if (family == TRANSPORT_FAMILY_IPV4)
2219 strvec_push(argv, "--ipv4");
2220 else if (family == TRANSPORT_FAMILY_IPV6)
2221 strvec_push(argv, "--ipv6");
2222 if (!write_fetch_head)
2223 strvec_push(argv, "--no-write-fetch-head");
2224 if (config->display_format == DISPLAY_FORMAT_PORCELAIN)
2225 strvec_pushf(argv, "--porcelain");
2226 }
2227
2228 /* Fetch multiple remotes in parallel */
2229
2230 struct parallel_fetch_state {
2231 const char **argv;
2232 struct string_list *remotes;
2233 int next, result;
2234 const struct fetch_config *config;
2235 };
2236
2237 static int fetch_next_remote(struct child_process *cp,
2238 struct strbuf *out UNUSED,
2239 void *cb, void **task_cb)
2240 {
2241 struct parallel_fetch_state *state = cb;
2242 char *remote;
2243
2244 if (state->next < 0 || state->next >= state->remotes->nr)
2245 return 0;
2246
2247 remote = state->remotes->items[state->next++].string;
2248 *task_cb = remote;
2249
2250 strvec_pushv(&cp->args, state->argv);
2251 strvec_push(&cp->args, remote);
2252 cp->git_cmd = 1;
2253
2254 if (verbosity >= 0 && state->config->display_format != DISPLAY_FORMAT_PORCELAIN)
2255 printf(_("Fetching %s\n"), remote);
2256
2257 return 1;
2258 }
2259
2260 static int fetch_failed_to_start(struct strbuf *out UNUSED,
2261 void *cb, void *task_cb)
2262 {
2263 struct parallel_fetch_state *state = cb;
2264 const char *remote = task_cb;
2265
2266 state->result = error(_("could not fetch %s"), remote);
2267
2268 return 0;
2269 }
2270
2271 static int fetch_finished(int result, struct strbuf *out,
2272 void *cb, void *task_cb)
2273 {
2274 struct parallel_fetch_state *state = cb;
2275 const char *remote = task_cb;
2276
2277 if (result) {
2278 strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"),
2279 remote, result);
2280 state->result = -1;
2281 }
2282
2283 return 0;
2284 }
2285
2286 static int fetch_multiple(struct string_list *list, int max_children,
2287 const struct fetch_config *config)
2288 {
2289 int i, result = 0;
2290 struct strvec argv = STRVEC_INIT;
2291
2292 if (!append && write_fetch_head) {
2293 int errcode = truncate_fetch_head();
2294 if (errcode)
2295 return errcode;
2296 }
2297
2298 /*
2299 * Cancel out the fetch.bundleURI config when running subprocesses,
2300 * to avoid fetching from the same bundle list multiple times.
2301 */
2302 strvec_pushl(&argv, "-c", "fetch.bundleURI=",
2303 "fetch", "--append", "--no-auto-gc",
2304 "--no-write-commit-graph", NULL);
2305 for (i = 0; i < server_options.nr; i++)
2306 strvec_pushf(&argv, "--server-option=%s", server_options.items[i].string);
2307 add_options_to_argv(&argv, config);
2308
2309 if (max_children != 1 && list->nr != 1) {
2310 struct parallel_fetch_state state = { argv.v, list, 0, 0, config };
2311 const struct run_process_parallel_opts opts = {
2312 .tr2_category = "fetch",
2313 .tr2_label = "parallel/fetch",
2314
2315 .processes = max_children,
2316
2317 .get_next_task = &fetch_next_remote,
2318 .start_failure = &fetch_failed_to_start,
2319 .task_finished = &fetch_finished,
2320 .data = &state,
2321 };
2322
2323 strvec_push(&argv, "--end-of-options");
2324
2325 run_processes_parallel(&opts);
2326 result = state.result;
2327 } else
2328 for (i = 0; i < list->nr; i++) {
2329 const char *name = list->items[i].string;
2330 struct child_process cmd = CHILD_PROCESS_INIT;
2331
2332 strvec_pushv(&cmd.args, argv.v);
2333 strvec_push(&cmd.args, name);
2334 if (verbosity >= 0 && config->display_format != DISPLAY_FORMAT_PORCELAIN)
2335 printf(_("Fetching %s\n"), name);
2336 cmd.git_cmd = 1;
2337 if (run_command(&cmd)) {
2338 error(_("could not fetch %s"), name);
2339 result = 1;
2340 }
2341 }
2342
2343 strvec_clear(&argv);
2344 return !!result;
2345 }
2346
2347 /*
2348 * Fetching from the promisor remote should use the given filter-spec
2349 * or inherit the default filter-spec from the config.
2350 */
2351 static inline void fetch_one_setup_partial(struct remote *remote,
2352 struct list_objects_filter_options *filter_options)
2353 {
2354 /*
2355 * Explicit --no-filter argument overrides everything, regardless
2356 * of any prior partial clones and fetches.
2357 */
2358 if (filter_options->no_filter)
2359 return;
2360
2361 /*
2362 * If no prior partial clone/fetch and the current fetch DID NOT
2363 * request a partial-fetch, do a normal fetch.
2364 */
2365 if (!repo_has_promisor_remote(the_repository) && !filter_options->choice)
2366 return;
2367
2368 /*
2369 * If this is a partial-fetch request, we enable partial on
2370 * this repo if not already enabled and remember the given
2371 * filter-spec as the default for subsequent fetches to this
2372 * remote if there is currently no default filter-spec.
2373 */
2374 if (filter_options->choice) {
2375 partial_clone_register(remote->name, filter_options);
2376 return;
2377 }
2378
2379 /*
2380 * Do a partial-fetch from the promisor remote using either the
2381 * explicitly given filter-spec or inherit the filter-spec from
2382 * the config.
2383 */
2384 if (!filter_options->choice)
2385 partial_clone_get_default_filter_spec(filter_options, remote->name);
2386 return;
2387 }
2388
2389 static int fetch_one(struct remote *remote, int argc, const char **argv,
2390 int prune_tags_ok, int use_stdin_refspecs,
2391 const struct fetch_config *config,
2392 struct list_objects_filter_options *filter_options)
2393 {
2394 struct refspec rs = REFSPEC_INIT_FETCH;
2395 int i;
2396 int exit_code;
2397 int maybe_prune_tags;
2398 int remote_via_config = remote_is_configured(remote, 0);
2399
2400 if (!remote)
2401 die(_("no remote repository specified; please specify either a URL or a\n"
2402 "remote name from which new revisions should be fetched"));
2403
2404 gtransport = prepare_transport(remote, 1, filter_options);
2405
2406 if (prune < 0) {
2407 /* no command line request */
2408 if (0 <= remote->prune)
2409 prune = remote->prune;
2410 else if (0 <= config->prune)
2411 prune = config->prune;
2412 else
2413 prune = PRUNE_BY_DEFAULT;
2414 }
2415
2416 if (prune_tags < 0) {
2417 /* no command line request */
2418 if (0 <= remote->prune_tags)
2419 prune_tags = remote->prune_tags;
2420 else if (0 <= config->prune_tags)
2421 prune_tags = config->prune_tags;
2422 else
2423 prune_tags = PRUNE_TAGS_BY_DEFAULT;
2424 }
2425
2426 maybe_prune_tags = prune_tags_ok && prune_tags;
2427 if (maybe_prune_tags && remote_via_config)
2428 refspec_append(&remote->fetch, TAG_REFSPEC);
2429
2430 if (maybe_prune_tags && (argc || !remote_via_config))
2431 refspec_append(&rs, TAG_REFSPEC);
2432
2433 for (i = 0; i < argc; i++) {
2434 if (!strcmp(argv[i], "tag")) {
2435 i++;
2436 if (i >= argc)
2437 die(_("you need to specify a tag name"));
2438
2439 refspec_appendf(&rs, "refs/tags/%s:refs/tags/%s",
2440 argv[i], argv[i]);
2441 } else {
2442 refspec_append(&rs, argv[i]);
2443 }
2444 }
2445
2446 if (use_stdin_refspecs) {
2447 struct strbuf line = STRBUF_INIT;
2448 while (strbuf_getline_lf(&line, stdin) != EOF)
2449 refspec_append(&rs, line.buf);
2450 strbuf_release(&line);
2451 }
2452
2453 if (server_options.nr)
2454 gtransport->server_options = &server_options;
2455
2456 sigchain_push_common(unlock_pack_on_signal);
2457 atexit(unlock_pack_atexit);
2458 sigchain_push(SIGPIPE, SIG_IGN);
2459 exit_code = do_fetch(gtransport, &rs, config, filter_options);
2460 sigchain_pop(SIGPIPE);
2461 refspec_clear(&rs);
2462 transport_disconnect(gtransport);
2463 gtransport = NULL;
2464 return exit_code;
2465 }
2466
2467 int cmd_fetch(int argc,
2468 const char **argv,
2469 const char *prefix,
2470 struct repository *repo UNUSED)
2471 {
2472 struct fetch_config config = {
2473 .display_format = DISPLAY_FORMAT_FULL,
2474 .prune = -1,
2475 .prune_tags = -1,
2476 .show_forced_updates = 1,
2477 .recurse_submodules = RECURSE_SUBMODULES_DEFAULT,
2478 .parallel = 1,
2479 .submodule_fetch_jobs = -1,
2480 };
2481 const char *submodule_prefix = "";
2482 const char *bundle_uri;
2483 struct string_list list = STRING_LIST_INIT_DUP;
2484 struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT;
2485 struct remote *remote = NULL;
2486 int all = -1, multiple = 0;
2487 int result = 0;
2488 int prune_tags_ok = 1;
2489 int enable_auto_gc = 1;
2490 int unshallow = 0;
2491 int max_jobs = -1;
2492 int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT;
2493 int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
2494 int fetch_write_commit_graph = -1;
2495 int stdin_refspecs = 0;
2496 int negotiate_only = 0;
2497 int porcelain = 0;
2498 int i;
2499
2500 struct option builtin_fetch_options[] = {
2501 OPT__VERBOSITY(&verbosity),
2502 OPT_BOOL(0, "all", &all,
2503 N_("fetch from all remotes")),
2504 OPT_BOOL(0, "set-upstream", &set_upstream,
2505 N_("set upstream for git pull/fetch")),
2506 OPT_BOOL('a', "append", &append,
2507 N_("append to .git/FETCH_HEAD instead of overwriting")),
2508 OPT_BOOL(0, "atomic", &atomic_fetch,
2509 N_("use atomic transaction to update references")),
2510 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
2511 N_("path to upload pack on remote end")),
2512 OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
2513 OPT_BOOL('m', "multiple", &multiple,
2514 N_("fetch from multiple remotes")),
2515 OPT_SET_INT('t', "tags", &tags,
2516 N_("fetch all tags and associated objects"), TAGS_SET),
2517 OPT_SET_INT('n', NULL, &tags,
2518 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
2519 OPT_INTEGER('j', "jobs", &max_jobs,
2520 N_("number of submodules fetched in parallel")),
2521 OPT_BOOL(0, "prefetch", &prefetch,
2522 N_("modify the refspec to place all refs within refs/prefetch/")),
2523 OPT_BOOL('p', "prune", &prune,
2524 N_("prune remote-tracking branches no longer on remote")),
2525 OPT_BOOL('P', "prune-tags", &prune_tags,
2526 N_("prune local tags no longer on remote and clobber changed tags")),
2527 OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules_cli, N_("on-demand"),
2528 N_("control recursive fetching of submodules"),
2529 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
2530 OPT_BOOL(0, "dry-run", &dry_run,
2531 N_("dry run")),
2532 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
2533 OPT_BOOL(0, "write-fetch-head", &write_fetch_head,
2534 N_("write fetched references to the FETCH_HEAD file")),
2535 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
2536 OPT_BOOL('u', "update-head-ok", &update_head_ok,
2537 N_("allow updating of HEAD ref")),
2538 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
2539 OPT_STRING(0, "depth", &depth, N_("depth"),
2540 N_("deepen history of shallow clone")),
2541 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
2542 N_("deepen history of shallow repository based on time")),
2543 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("ref"),
2544 N_("deepen history of shallow clone, excluding ref")),
2545 OPT_INTEGER(0, "deepen", &deepen_relative,
2546 N_("deepen history of shallow clone")),
2547 OPT_SET_INT_F(0, "unshallow", &unshallow,
2548 N_("convert to a complete repository"),
2549 1, PARSE_OPT_NONEG),
2550 OPT_SET_INT_F(0, "refetch", &refetch,
2551 N_("re-fetch without negotiating common commits"),
2552 1, PARSE_OPT_NONEG),
2553 {
2554 .type = OPTION_STRING,
2555 .long_name = "submodule-prefix",
2556 .value = &submodule_prefix,
2557 .argh = N_("dir"),
2558 .help = N_("prepend this to submodule path output"),
2559 .flags = PARSE_OPT_HIDDEN,
2560 },
2561 OPT_CALLBACK_F(0, "recurse-submodules-default",
2562 &recurse_submodules_default, N_("on-demand"),
2563 N_("default for recursive fetching of submodules "
2564 "(lower priority than config files)"),
2565 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules),
2566 OPT_BOOL(0, "update-shallow", &update_shallow,
2567 N_("accept refs that update .git/shallow")),
2568 OPT_CALLBACK_F(0, "refmap", &refmap, N_("refmap"),
2569 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg),
2570 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
2571 OPT_IPVERSION(&family),
2572 OPT_STRING_LIST(0, "negotiation-restrict", &negotiation_restrict, N_("revision"),
2573 N_("report that we have only objects reachable from this object")),
2574 OPT_ALIAS(0, "negotiation-tip", "negotiation-restrict"),
2575 OPT_STRING_LIST(0, "negotiation-include", &negotiation_include, N_("revision"),
2576 N_("ensure this ref is always sent as a negotiation have")),
2577 OPT_BOOL(0, "negotiate-only", &negotiate_only,
2578 N_("do not fetch a packfile; instead, print ancestors of negotiation tips")),
2579 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2580 OPT_BOOL(0, "auto-maintenance", &enable_auto_gc,
2581 N_("run 'maintenance --auto' after fetching")),
2582 OPT_BOOL(0, "auto-gc", &enable_auto_gc,
2583 N_("run 'maintenance --auto' after fetching")),
2584 OPT_BOOL(0, "show-forced-updates", &config.show_forced_updates,
2585 N_("check for forced-updates on all updated branches")),
2586 OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph,
2587 N_("write the commit-graph after fetching")),
2588 OPT_BOOL(0, "stdin", &stdin_refspecs,
2589 N_("accept refspecs from stdin")),
2590 OPT_END()
2591 };
2592
2593 filter_options.allow_auto_filter = 1;
2594
2595 packet_trace_identity("fetch");
2596
2597 /* Record the command line for the reflog */
2598 strbuf_addstr(&default_rla, "fetch");
2599 for (i = 1; i < argc; i++) {
2600 /* This handles non-URLs gracefully */
2601 char *anon = transport_anonymize_url(argv[i]);
2602
2603 strbuf_addf(&default_rla, " %s", anon);
2604 free(anon);
2605 }
2606
2607 repo_config(the_repository, git_fetch_config, &config);
2608 if (the_repository->gitdir) {
2609 prepare_repo_settings(the_repository);
2610 the_repository->settings.command_requires_full_index = 0;
2611 }
2612
2613 argc = parse_options(argc, argv, prefix,
2614 builtin_fetch_options, builtin_fetch_usage, 0);
2615
2616 if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT)
2617 config.recurse_submodules = recurse_submodules_cli;
2618
2619 if (negotiate_only) {
2620 switch (recurse_submodules_cli) {
2621 case RECURSE_SUBMODULES_OFF:
2622 case RECURSE_SUBMODULES_DEFAULT:
2623 /*
2624 * --negotiate-only should never recurse into
2625 * submodules. Skip it by setting recurse_submodules to
2626 * RECURSE_SUBMODULES_OFF.
2627 */
2628 config.recurse_submodules = RECURSE_SUBMODULES_OFF;
2629 break;
2630
2631 default:
2632 die(_("options '%s' and '%s' cannot be used together"),
2633 "--negotiate-only", "--recurse-submodules");
2634 }
2635 }
2636
2637 if (config.recurse_submodules != RECURSE_SUBMODULES_OFF) {
2638 int *sfjc = config.submodule_fetch_jobs == -1
2639 ? &config.submodule_fetch_jobs : NULL;
2640 int *rs = config.recurse_submodules == RECURSE_SUBMODULES_DEFAULT
2641 ? &config.recurse_submodules : NULL;
2642
2643 fetch_config_from_gitmodules(sfjc, rs);
2644 }
2645
2646
2647 if (porcelain) {
2648 switch (recurse_submodules_cli) {
2649 case RECURSE_SUBMODULES_OFF:
2650 case RECURSE_SUBMODULES_DEFAULT:
2651 /*
2652 * Reference updates in submodules would be ambiguous
2653 * in porcelain mode, so we reject this combination.
2654 */
2655 config.recurse_submodules = RECURSE_SUBMODULES_OFF;
2656 break;
2657
2658 default:
2659 die(_("options '%s' and '%s' cannot be used together"),
2660 "--porcelain", "--recurse-submodules");
2661 }
2662
2663 config.display_format = DISPLAY_FORMAT_PORCELAIN;
2664 }
2665
2666 if (deepen_relative) {
2667 if (deepen_relative < 0)
2668 die(_("negative depth in --deepen is not supported"));
2669 if (depth)
2670 die(_("options '%s' and '%s' cannot be used together"), "--deepen", "--depth");
2671 depth = xstrfmt("%d", deepen_relative);
2672 }
2673 if (unshallow) {
2674 if (depth)
2675 die(_("options '%s' and '%s' cannot be used together"), "--depth", "--unshallow");
2676 else if (!is_repository_shallow(the_repository))
2677 die(_("--unshallow on a complete repository does not make sense"));
2678 else
2679 depth = xstrfmt("%d", INFINITE_DEPTH);
2680 }
2681
2682 /* no need to be strict, transport_set_option() will validate it again */
2683 if (depth && atoi(depth) < 1)
2684 die(_("depth %s is not a positive number"), depth);
2685 if (depth || deepen_since || deepen_not.nr)
2686 deepen = 1;
2687
2688 /* FETCH_HEAD never gets updated in --dry-run mode */
2689 if (dry_run)
2690 write_fetch_head = 0;
2691
2692 if (!max_jobs)
2693 max_jobs = online_cpus();
2694
2695 if (!repo_config_get_string_tmp(the_repository, "fetch.bundleuri", &bundle_uri) &&
2696 fetch_bundle_uri(the_repository, bundle_uri, NULL))
2697 warning(_("failed to fetch bundles from '%s'"), bundle_uri);
2698
2699 if (all < 0) {
2700 /*
2701 * no --[no-]all given;
2702 * only use config option if no remote was explicitly specified
2703 */
2704 all = (!argc) ? config.all : 0;
2705 }
2706
2707 if (all) {
2708 if (argc == 1)
2709 die(_("fetch --all does not take a repository argument"));
2710 else if (argc > 1)
2711 die(_("fetch --all does not make sense with refspecs"));
2712
2713 (void) for_each_remote(get_one_remote_for_fetch, &list);
2714
2715 /* do not do fetch_multiple() of one */
2716 if (list.nr == 1)
2717 remote = remote_get(list.items[0].string);
2718 } else if (argc == 0) {
2719 /* No arguments -- use default remote */
2720 remote = remote_get(NULL);
2721 } else if (multiple) {
2722 /* All arguments are assumed to be remotes or groups */
2723 for (i = 0; i < argc; i++)
2724 if (!add_remote_or_group(argv[i], &list))
2725 die(_("no such remote or remote group: %s"),
2726 argv[i]);
2727 } else {
2728 /* Single remote or group */
2729 (void) add_remote_or_group(argv[0], &list);
2730 if (list.nr > 1) {
2731 /* More than one remote */
2732 if (argc > 1)
2733 die(_("fetching a group and specifying refspecs does not make sense"));
2734 } else {
2735 /* Zero or one remotes */
2736 remote = remote_get(argv[0]);
2737 prune_tags_ok = (argc == 1);
2738 argc--;
2739 argv++;
2740 }
2741 }
2742 string_list_remove_duplicates(&list, 0);
2743
2744 if (negotiate_only) {
2745 struct oidset acked_commits = OIDSET_INIT;
2746 struct oidset_iter iter;
2747 const struct object_id *oid;
2748
2749 trace2_region_enter("fetch", "negotiate-only", the_repository);
2750 if (!remote)
2751 die(_("must supply remote when using --negotiate-only"));
2752 gtransport = prepare_transport(remote, 1, &filter_options);
2753
2754 if (!gtransport->smart_options) {
2755 warning(_("protocol does not support --negotiate-only, exiting"));
2756 result = 1;
2757 trace2_region_leave("fetch", "negotiate-only", the_repository);
2758 goto cleanup;
2759 }
2760 if (!gtransport->smart_options->negotiation_restrict_tips)
2761 die(_("%s needs one or more %s"), "--negotiate-only",
2762 "--negotiation-restrict=*");
2763
2764 gtransport->smart_options->acked_commits = &acked_commits;
2765
2766 if (server_options.nr)
2767 gtransport->server_options = &server_options;
2768 result = transport_fetch_refs(gtransport, NULL);
2769 gtransport->smart_options->acked_commits = NULL;
2770
2771 oidset_iter_init(&acked_commits, &iter);
2772 while ((oid = oidset_iter_next(&iter)))
2773 printf("%s\n", oid_to_hex(oid));
2774 oidset_clear(&acked_commits);
2775 trace2_region_leave("fetch", "negotiate-only", the_repository);
2776 } else if (remote) {
2777 if (filter_options.choice || repo_has_promisor_remote(the_repository)) {
2778 trace2_region_enter("fetch", "setup-partial", the_repository);
2779 fetch_one_setup_partial(remote, &filter_options);
2780 trace2_region_leave("fetch", "setup-partial", the_repository);
2781 }
2782 trace2_region_enter("fetch", "fetch-one", the_repository);
2783 result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs,
2784 &config, &filter_options);
2785 trace2_region_leave("fetch", "fetch-one", the_repository);
2786 } else {
2787 int max_children = max_jobs;
2788
2789 if (filter_options.choice)
2790 die(_("--filter can only be used with the remote "
2791 "configured in extensions.partialclone"));
2792
2793 if (atomic_fetch)
2794 die(_("--atomic can only be used when fetching "
2795 "from one remote"));
2796
2797 if (stdin_refspecs)
2798 die(_("--stdin can only be used when fetching "
2799 "from one remote"));
2800
2801 if (max_children < 0)
2802 max_children = config.parallel;
2803
2804 /* TODO should this also die if we have a previous partial-clone? */
2805 trace2_region_enter("fetch", "fetch-multiple", the_repository);
2806 result = fetch_multiple(&list, max_children, &config);
2807 trace2_region_leave("fetch", "fetch-multiple", the_repository);
2808 }
2809
2810 /*
2811 * This is only needed after fetch_one(), which does not fetch
2812 * submodules by itself.
2813 *
2814 * When we fetch from multiple remotes, fetch_multiple() has
2815 * already updated submodules to grab commits necessary for
2816 * the fetched history from each remote, so there is no need
2817 * to fetch submodules from here.
2818 */
2819 if (!result && remote && (config.recurse_submodules != RECURSE_SUBMODULES_OFF)) {
2820 struct strvec options = STRVEC_INIT;
2821 int max_children = max_jobs;
2822
2823 if (max_children < 0)
2824 max_children = config.submodule_fetch_jobs;
2825 if (max_children < 0)
2826 max_children = config.parallel;
2827
2828 add_options_to_argv(&options, &config);
2829 trace2_region_enter_printf("fetch", "recurse-submodule", the_repository, "%s", submodule_prefix);
2830 result = fetch_submodules(the_repository,
2831 &options,
2832 submodule_prefix,
2833 config.recurse_submodules,
2834 recurse_submodules_default,
2835 verbosity < 0,
2836 max_children);
2837 trace2_region_leave_printf("fetch", "recurse-submodule", the_repository, "%s", submodule_prefix);
2838 strvec_clear(&options);
2839 }
2840
2841 /*
2842 * Skip irrelevant tasks because we know objects were not
2843 * fetched.
2844 *
2845 * NEEDSWORK: as a future optimization, we can return early
2846 * whenever objects were not fetched e.g. if we already have all
2847 * of them.
2848 */
2849 if (negotiate_only)
2850 goto cleanup;
2851
2852 prepare_repo_settings(the_repository);
2853 if (fetch_write_commit_graph > 0 ||
2854 (fetch_write_commit_graph < 0 &&
2855 the_repository->settings.fetch_write_commit_graph)) {
2856 int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT;
2857
2858 if (progress)
2859 commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS;
2860
2861 trace2_region_enter("fetch", "write-commit-graph", the_repository);
2862 write_commit_graph_reachable(the_repository->objects->sources,
2863 commit_graph_flags,
2864 NULL);
2865 trace2_region_leave("fetch", "write-commit-graph", the_repository);
2866 }
2867
2868 if (enable_auto_gc) {
2869 if (refetch) {
2870 /*
2871 * Hint auto-maintenance strongly to encourage repacking,
2872 * but respect config settings disabling it.
2873 */
2874 int opt_val;
2875 if (repo_config_get_int(the_repository, "gc.autopacklimit", &opt_val))
2876 opt_val = -1;
2877 if (opt_val != 0)
2878 git_config_push_parameter("gc.autoPackLimit=1");
2879
2880 if (repo_config_get_int(the_repository, "maintenance.incremental-repack.auto", &opt_val))
2881 opt_val = -1;
2882 if (opt_val != 0)
2883 git_config_push_parameter("maintenance.incremental-repack.auto=-1");
2884 }
2885 run_auto_maintenance(the_repository, verbosity < 0);
2886 }
2887
2888 cleanup:
2889 string_list_clear(&list, 0);
2890 list_objects_filter_release(&filter_options);
2891 return result;
2892 }