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