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