]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fetch.c
Merge branch 'ds/fetch-config-parse-microfix'
[thirdparty/git.git] / builtin / fetch.c
CommitLineData
b888d61c
DB
1/*
2 * "git fetch"
3 */
bc5c5ec0 4#include "builtin.h"
6c6ddf92 5#include "advice.h"
b2141fc1 6#include "config.h"
f394e093 7#include "gettext.h"
32a8f510 8#include "environment.h"
41771fa4 9#include "hex.h"
e724197f 10#include "repository.h"
b888d61c 11#include "refs.h"
ec0cb496 12#include "refspec.h"
dabab1d6 13#include "object-name.h"
a034e910 14#include "object-store-ll.h"
b7e2d8bc 15#include "oidset.h"
6f2d7430 16#include "oid-array.h"
b888d61c 17#include "commit.h"
c455c87c 18#include "string-list.h"
b888d61c
DB
19#include "remote.h"
20#include "transport.h"
4191c356 21#include "run-command.h"
83201998 22#include "parse-options.h"
4a16d072 23#include "sigchain.h"
027771fc 24#include "submodule-config.h"
7dce19d3 25#include "submodule.h"
f96400cb 26#include "connected.h"
dbbcd44f 27#include "strvec.h"
6bc91f23 28#include "utf8.h"
ca4eed70 29#include "pager.h"
c339932b 30#include "path.h"
b388633c 31#include "pkt-line.h"
acb0c572 32#include "list-objects-filter-options.h"
64043556 33#include "commit-reach.h"
24bc1a12 34#include "branch.h"
b14ed5ad 35#include "promisor-remote.h"
50f26bd0 36#include "commit-graph.h"
120ad2b0 37#include "shallow.h"
74ea5c95
EN
38#include "trace.h"
39#include "trace2.h"
7f0cc04f 40#include "bundle-uri.h"
b888d61c 41
377444b4
DS
42#define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
43
83201998 44static const char * const builtin_fetch_usage[] = {
719acedb
NTND
45 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
46 N_("git fetch [<options>] <group>"),
47 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
48 N_("git fetch --all [<options>]"),
83201998
KH
49 NULL
50};
b888d61c 51
83201998
KH
52enum {
53 TAGS_UNSET = 0,
54 TAGS_DEFAULT = 1,
55 TAGS_SET = 2
56};
57
50957937 58enum display_format {
50957937
PS
59 DISPLAY_FORMAT_FULL,
60 DISPLAY_FORMAT_COMPACT,
dd781e38 61 DISPLAY_FORMAT_PORCELAIN,
50957937
PS
62};
63
ce9636d6 64struct display_state {
d6606e02
PS
65 struct strbuf buf;
66
ce9636d6 67 int refcol_width;
50957937 68 enum display_format format;
c4ef5edb
PS
69
70 char *url;
71 int url_len, shown_url;
ce9636d6
PS
72};
73
377444b4 74static uint64_t forced_updates_ms = 0;
2e03115d 75static int prefetch = 0;
737c5a9c
MS
76static int prune = -1; /* unspecified */
77#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
78
97716d21
ÆAB
79static int prune_tags = -1; /* unspecified */
80#define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
81
cdc034a0 82static int append, dry_run, force, keep, update_head_ok;
887952b8 83static int write_fetch_head = 1;
3c7bab06 84static int verbosity, deepen_relative, set_upstream, refetch;
8c69832d 85static int progress = -1;
cdc034a0 86static int tags = TAGS_DEFAULT, update_shallow, deepen;
c7b190da 87static int atomic_fetch;
c915f11e 88static enum transport_family family;
4191c356 89static const char *depth;
508ea882 90static const char *deepen_since;
83201998 91static const char *upload_pack;
a45a2600 92static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
2d324efa 93static struct strbuf default_rla = STRBUF_INIT;
af234459 94static struct transport *gtransport;
b26ed430 95static struct transport *gsecondary;
e4cffacc 96static struct refspec refmap = REFSPEC_INIT_FETCH;
2a01bded 97static struct list_objects_filter_options filter_options = LIST_OBJECTS_FILTER_INIT;
5e3548ef 98static struct string_list server_options = STRING_LIST_INIT_DUP;
3390e42a 99static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
e4022ed2 100
58afbe88
PS
101struct fetch_config {
102 enum display_format display_format;
39487a15 103 int all;
b779a25e 104 int prune;
2b472cfe 105 int prune_tags;
ba28b2ca 106 int show_forced_updates;
56e8bb4f 107 int recurse_submodules;
ac197cc0 108 int parallel;
f7e063f3 109 int submodule_fetch_jobs;
58afbe88 110};
e4022ed2 111
a4e7e317
GC
112static int git_fetch_config(const char *k, const char *v,
113 const struct config_context *ctx, void *cb)
737c5a9c 114{
58afbe88
PS
115 struct fetch_config *fetch_config = cb;
116
39487a15
TB
117 if (!strcmp(k, "fetch.all")) {
118 fetch_config->all = git_config_bool(k, v);
119 return 0;
120 }
121
737c5a9c 122 if (!strcmp(k, "fetch.prune")) {
b779a25e 123 fetch_config->prune = git_config_bool(k, v);
737c5a9c
MS
124 return 0;
125 }
58f4203e 126
97716d21 127 if (!strcmp(k, "fetch.prunetags")) {
2b472cfe 128 fetch_config->prune_tags = git_config_bool(k, v);
97716d21
ÆAB
129 return 0;
130 }
131
cdbd70c4 132 if (!strcmp(k, "fetch.showforcedupdates")) {
ba28b2ca 133 fetch_config->show_forced_updates = git_config_bool(k, v);
cdbd70c4
DS
134 return 0;
135 }
136
58f4203e
SB
137 if (!strcmp(k, "submodule.recurse")) {
138 int r = git_config_bool(k, v) ?
139 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
56e8bb4f 140 fetch_config->recurse_submodules = r;
a816ccd6 141 return 0;
58f4203e
SB
142 }
143
f20e7c1e 144 if (!strcmp(k, "submodule.fetchjobs")) {
8868b1eb 145 fetch_config->submodule_fetch_jobs = parse_submodule_fetchjobs(k, v, ctx->kvi);
f20e7c1e 146 return 0;
8fa29159 147 } else if (!strcmp(k, "fetch.recursesubmodules")) {
56e8bb4f 148 fetch_config->recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
8fa29159 149 return 0;
f20e7c1e
BW
150 }
151
d54dea77 152 if (!strcmp(k, "fetch.parallel")) {
8868b1eb 153 fetch_config->parallel = git_config_int(k, v, ctx->kvi);
ac197cc0 154 if (fetch_config->parallel < 0)
d54dea77 155 die(_("fetch.parallel cannot be negative"));
ac197cc0
PS
156 if (!fetch_config->parallel)
157 fetch_config->parallel = online_cpus();
d54dea77
JS
158 return 0;
159 }
160
58afbe88
PS
161 if (!strcmp(k, "fetch.output")) {
162 if (!v)
163 return config_error_nonbool(k);
164 else if (!strcasecmp(v, "full"))
165 fetch_config->display_format = DISPLAY_FORMAT_FULL;
166 else if (!strcasecmp(v, "compact"))
167 fetch_config->display_format = DISPLAY_FORMAT_COMPACT;
168 else
169 die(_("invalid value for '%s': '%s'"),
170 "fetch.output", v);
171 }
172
a4e7e317 173 return git_default_config(k, v, ctx, cb);
737c5a9c
MS
174}
175
c5558f80
JH
176static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
177{
517fe807
JK
178 BUG_ON_OPT_NEG(unset);
179
c5558f80
JH
180 /*
181 * "git fetch --refmap='' origin foo"
182 * can be used to tell the command not to store anywhere
183 */
66e33092 184 refspec_append(opt->value, arg);
e4cffacc 185
c5558f80
JH
186 return 0;
187}
188
58d4d7f1 189static void unlock_pack(unsigned int flags)
e4022ed2 190{
af234459 191 if (gtransport)
58d4d7f1 192 transport_unlock_pack(gtransport, flags);
b26ed430 193 if (gsecondary)
58d4d7f1
PS
194 transport_unlock_pack(gsecondary, flags);
195}
196
197static void unlock_pack_atexit(void)
198{
199 unlock_pack(0);
e4022ed2
SP
200}
201
202static void unlock_pack_on_signal(int signo)
203{
58d4d7f1 204 unlock_pack(TRANSPORT_UNLOCK_PACK_IN_SIGNAL_HANDLER);
4a16d072 205 sigchain_pop(signo);
e4022ed2
SP
206 raise(signo);
207}
b888d61c 208
85682c19 209static void add_merge_config(struct ref **head,
4577370e 210 const struct ref *remote_refs,
85682c19
SP
211 struct branch *branch,
212 struct ref ***tail)
b888d61c 213{
85682c19 214 int i;
b888d61c 215
85682c19
SP
216 for (i = 0; i < branch->merge_nr; i++) {
217 struct ref *rm, **old_tail = *tail;
0ad4a5ff 218 struct refspec_item refspec;
85682c19
SP
219
220 for (rm = *head; rm; rm = rm->next) {
221 if (branch_merge_matches(branch, i, rm->name)) {
900f2814 222 rm->fetch_head_status = FETCH_HEAD_MERGE;
85682c19
SP
223 break;
224 }
b888d61c 225 }
85682c19
SP
226 if (rm)
227 continue;
228
9ad7c5ae 229 /*
8b3f3f84 230 * Not fetched to a remote-tracking branch? We need to fetch
85682c19 231 * it anyway to allow this branch's "branch.$name.merge"
05207a28 232 * to be honored by 'git pull', but we do not have to
9ad7c5ae
JH
233 * fail if branch.$name.merge is misconfigured to point
234 * at a nonexisting branch. If we were indeed called by
05207a28 235 * 'git pull', it will notice the misconfiguration because
9ad7c5ae
JH
236 * there is no entry in the resulting FETCH_HEAD marked
237 * for merging.
85682c19 238 */
8da61a2a 239 memset(&refspec, 0, sizeof(refspec));
85682c19 240 refspec.src = branch->merge[i]->src;
9ad7c5ae 241 get_fetch_map(remote_refs, &refspec, tail, 1);
85682c19 242 for (rm = *old_tail; rm; rm = rm->next)
900f2814 243 rm->fetch_head_status = FETCH_HEAD_MERGE;
b888d61c
DB
244 }
245}
246
b7e2d8bc 247static void create_fetch_oidset(struct ref **head, struct oidset *out)
a0fbb5a3
MH
248{
249 struct ref *rm = *head;
250 while (rm) {
b7e2d8bc 251 oidset_insert(out, &rm->old_oid);
a0fbb5a3
MH
252 rm = rm->next;
253 }
a0fbb5a3
MH
254}
255
e198b3a7 256struct refname_hash_entry {
e2b5038d 257 struct hashmap_entry ent;
e198b3a7 258 struct object_id oid;
f80d9223 259 int ignore;
e198b3a7
JH
260 char refname[FLEX_ARRAY];
261};
262
5cf88fd8 263static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data UNUSED,
939af16e
EW
264 const struct hashmap_entry *eptr,
265 const struct hashmap_entry *entry_or_key,
e198b3a7
JH
266 const void *keydata)
267{
939af16e 268 const struct refname_hash_entry *e1, *e2;
e198b3a7 269
939af16e
EW
270 e1 = container_of(eptr, const struct refname_hash_entry, ent);
271 e2 = container_of(entry_or_key, const struct refname_hash_entry, ent);
e198b3a7
JH
272 return strcmp(e1->refname, keydata ? keydata : e2->refname);
273}
274
275static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
276 const char *refname,
277 const struct object_id *oid)
278{
279 struct refname_hash_entry *ent;
280 size_t len = strlen(refname);
281
282 FLEX_ALLOC_MEM(ent, refname, refname, len);
d22245a2 283 hashmap_entry_init(&ent->ent, strhash(refname));
e198b3a7 284 oidcpy(&ent->oid, oid);
b94e5c1d 285 hashmap_add(map, &ent->ent);
e198b3a7
JH
286 return ent;
287}
288
289static int add_one_refname(const char *refname,
290 const struct object_id *oid,
5cf88fd8 291 int flag UNUSED, void *cbdata)
e198b3a7
JH
292{
293 struct hashmap *refname_map = cbdata;
294
295 (void) refname_hash_add(refname_map, refname, oid);
296 return 0;
297}
298
299static void refname_hash_init(struct hashmap *map)
300{
301 hashmap_init(map, refname_hash_entry_cmp, NULL, 0);
302}
303
304static int refname_hash_exists(struct hashmap *map, const char *refname)
305{
306 return !!hashmap_get_from_hash(map, strhash(refname), refname);
307}
308
a8363b57
FC
309static void clear_item(struct refname_hash_entry *item)
310{
f80d9223 311 item->ignore = 1;
a8363b57
FC
312}
313
b3a80466
PS
314
315static void add_already_queued_tags(const char *refname,
ccf759cd 316 const struct object_id *old_oid UNUSED,
b3a80466
PS
317 const struct object_id *new_oid,
318 void *cb_data)
319{
320 struct hashmap *queued_tags = cb_data;
321 if (starts_with(refname, "refs/tags/") && new_oid)
322 (void) refname_hash_add(queued_tags, refname, new_oid);
323}
324
6d1700d5 325static void find_non_local_tags(const struct ref *refs,
b3a80466 326 struct ref_transaction *transaction,
6d1700d5
BW
327 struct ref **head,
328 struct ref ***tail)
a0fbb5a3 329{
e198b3a7
JH
330 struct hashmap existing_refs;
331 struct hashmap remote_refs;
b7e2d8bc 332 struct oidset fetch_oids = OIDSET_INIT;
e198b3a7
JH
333 struct string_list remote_refs_list = STRING_LIST_INIT_NODUP;
334 struct string_list_item *remote_ref_item;
a0fbb5a3 335 const struct ref *ref;
e198b3a7 336 struct refname_hash_entry *item = NULL;
3e96c668 337 const int quick_flags = OBJECT_INFO_QUICK | OBJECT_INFO_SKIP_FETCH_OBJECT;
e198b3a7
JH
338
339 refname_hash_init(&existing_refs);
340 refname_hash_init(&remote_refs);
b7e2d8bc 341 create_fetch_oidset(head, &fetch_oids);
a0fbb5a3 342
e198b3a7 343 for_each_ref(add_one_refname, &existing_refs);
b3a80466
PS
344
345 /*
346 * If we already have a transaction, then we need to filter out all
347 * tags which have already been queued up.
348 */
349 if (transaction)
350 ref_transaction_for_each_queued_update(transaction,
351 add_already_queued_tags,
352 &existing_refs);
353
6d1700d5 354 for (ref = refs; ref; ref = ref->next) {
ad704485 355 if (!starts_with(ref->name, "refs/tags/"))
a0fbb5a3
MH
356 continue;
357
358 /*
359 * The peeled ref always follows the matching base
360 * ref, so if we see a peeled ref that we don't want
361 * to fetch then we can mark the ref entry in the list
362 * as one to ignore by setting util to NULL.
363 */
ad704485 364 if (ends_with(ref->name, "^{}")) {
5827a035 365 if (item &&
bc726bd0 366 !repo_has_object_file_with_flags(the_repository, &ref->old_oid, quick_flags) &&
b7e2d8bc 367 !oidset_contains(&fetch_oids, &ref->old_oid) &&
bc726bd0 368 !repo_has_object_file_with_flags(the_repository, &item->oid, quick_flags) &&
b7e2d8bc 369 !oidset_contains(&fetch_oids, &item->oid))
a8363b57 370 clear_item(item);
a0fbb5a3
MH
371 item = NULL;
372 continue;
373 }
374
375 /*
376 * If item is non-NULL here, then we previously saw a
377 * ref not followed by a peeled reference, so we need
378 * to check if it is a lightweight tag that we want to
379 * fetch.
380 */
5827a035 381 if (item &&
bc726bd0 382 !repo_has_object_file_with_flags(the_repository, &item->oid, quick_flags) &&
b7e2d8bc 383 !oidset_contains(&fetch_oids, &item->oid))
a8363b57 384 clear_item(item);
a0fbb5a3
MH
385
386 item = NULL;
387
388 /* skip duplicates and refs that we already have */
e198b3a7
JH
389 if (refname_hash_exists(&remote_refs, ref->name) ||
390 refname_hash_exists(&existing_refs, ref->name))
a0fbb5a3
MH
391 continue;
392
e198b3a7
JH
393 item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
394 string_list_insert(&remote_refs_list, ref->name);
a0fbb5a3 395 }
6da1a258 396 hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
a0fbb5a3
MH
397
398 /*
399 * We may have a final lightweight tag that needs to be
400 * checked to see if it needs fetching.
401 */
5827a035 402 if (item &&
bc726bd0 403 !repo_has_object_file_with_flags(the_repository, &item->oid, quick_flags) &&
b7e2d8bc 404 !oidset_contains(&fetch_oids, &item->oid))
a8363b57 405 clear_item(item);
a0fbb5a3
MH
406
407 /*
e198b3a7 408 * For all the tags in the remote_refs_list,
a0fbb5a3
MH
409 * add them to the list of refs to be fetched
410 */
e198b3a7
JH
411 for_each_string_list_item(remote_ref_item, &remote_refs_list) {
412 const char *refname = remote_ref_item->string;
9528b80b 413 struct ref *rm;
f23a4651 414 unsigned int hash = strhash(refname);
e198b3a7 415
f23a4651
EW
416 item = hashmap_get_entry_from_hash(&remote_refs, hash, refname,
417 struct refname_hash_entry, ent);
e198b3a7
JH
418 if (!item)
419 BUG("unseen remote ref?");
420
a0fbb5a3 421 /* Unless we have already decided to ignore this item... */
f80d9223 422 if (item->ignore)
9528b80b
FC
423 continue;
424
425 rm = alloc_ref(item->refname);
426 rm->peer_ref = alloc_ref(item->refname);
427 oidcpy(&rm->old_oid, &item->oid);
428 **tail = rm;
429 *tail = &rm->next;
a0fbb5a3 430 }
6da1a258 431 hashmap_clear_and_free(&remote_refs, struct refname_hash_entry, ent);
e198b3a7 432 string_list_clear(&remote_refs_list, 0);
b7e2d8bc 433 oidset_clear(&fetch_oids);
a0fbb5a3 434}
767f176a 435
2e03115d
DS
436static void filter_prefetch_refspec(struct refspec *rs)
437{
438 int i;
439
440 if (!prefetch)
441 return;
442
443 for (i = 0; i < rs->nr; i++) {
444 struct strbuf new_dst = STRBUF_INIT;
445 char *old_dst;
446 const char *sub = NULL;
447
448 if (rs->items[i].negative)
449 continue;
450 if (!rs->items[i].dst ||
451 (rs->items[i].src &&
87bd7fbb
RS
452 starts_with(rs->items[i].src,
453 ref_namespace[NAMESPACE_TAGS].ref))) {
2e03115d
DS
454 int j;
455
456 free(rs->items[i].src);
457 free(rs->items[i].dst);
458
459 for (j = i + 1; j < rs->nr; j++) {
460 rs->items[j - 1] = rs->items[j];
461 rs->raw[j - 1] = rs->raw[j];
462 }
463 rs->nr--;
464 i--;
465 continue;
466 }
467
468 old_dst = rs->items[i].dst;
992f25d7 469 strbuf_addstr(&new_dst, ref_namespace[NAMESPACE_PREFETCH].ref);
2e03115d
DS
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
6d1700d5
BW
487static struct ref *get_ref_map(struct remote *remote,
488 const struct ref *remote_refs,
65d96c8b 489 struct refspec *rs,
f137a45e 490 int tags, int *autotags)
b888d61c
DB
491{
492 int i;
493 struct ref *rm;
494 struct ref *ref_map = NULL;
495 struct ref **tail = &ref_map;
496
c5a84e92
MH
497 /* opportunistically-updated references: */
498 struct ref *orefs = NULL, **oref_tail = &orefs;
b888d61c 499
e198b3a7 500 struct hashmap existing_refs;
abcb7eeb 501 int existing_refs_populated = 0;
f2690487 502
2e03115d
DS
503 filter_prefetch_refspec(rs);
504 if (remote)
505 filter_prefetch_refspec(&remote->fetch);
506
65d96c8b 507 if (rs->nr) {
c5558f80 508 struct refspec *fetch_refspec;
c5558f80 509
65d96c8b
BW
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])
b888d61c
DB
513 *autotags = 1;
514 }
0281c930 515 /* Merge everything on the command line (but not --tags) */
b888d61c 516 for (rm = ref_map; rm; rm = rm->next)
900f2814 517 rm->fetch_head_status = FETCH_HEAD_MERGE;
f2690487
JK
518
519 /*
0281c930
MH
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.
f2690487 538 */
65d96c8b
BW
539 if (refmap.nr)
540 fetch_refspec = &refmap;
541 else
6d1700d5 542 fetch_refspec = &remote->fetch;
c5558f80 543
65d96c8b
BW
544 for (i = 0; i < fetch_refspec->nr; i++)
545 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
e4cffacc 546 } else if (refmap.nr) {
66996bea 547 die("--refmap option is only meaningful with command-line refspec(s)");
b888d61c
DB
548 } else {
549 /* Use the defaults */
85682c19
SP
550 struct branch *branch = branch_get(NULL);
551 int has_merge = branch_has_merge_config(branch);
3ee1757b 552 if (remote &&
e5349abf 553 (remote->fetch.nr ||
f31dbdc7 554 /* Note: has_merge implies non-NULL branch->remote_name */
3ee1757b 555 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
e5349abf
BW
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])
b888d61c 560 *autotags = 1;
85682c19 561 if (!i && !has_merge && ref_map &&
e5349abf 562 !remote->fetch.items[0].pattern)
900f2814 563 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
b888d61c 564 }
da0204df
JS
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.
f31dbdc7
BC
569 *
570 * Note: has_merge implies non-NULL branch->remote_name
da0204df 571 */
9ad7c5ae
JH
572 if (has_merge &&
573 !strcmp(branch->remote_name, remote->name))
85682c19 574 add_merge_config(&ref_map, remote_refs, branch, &tail);
2e03115d 575 } else if (!prefetch) {
b888d61c 576 ref_map = get_remote_ref(remote_refs, "HEAD");
9ad7c5ae 577 if (!ref_map)
66996bea 578 die(_("couldn't find remote ref HEAD"));
900f2814 579 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
5aaf7f2a 580 tail = &ref_map->next;
b888d61c
DB
581 }
582 }
0281c930 583
c5a84e92
MH
584 if (tags == TAGS_SET)
585 /* also fetch all tags */
586 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
587 else if (tags == TAGS_DEFAULT && *autotags)
b3a80466 588 find_non_local_tags(remote_refs, NULL, &ref_map, &tail);
0281c930 589
c5a84e92
MH
590 /* Now append any refs to be updated opportunistically: */
591 *tail = orefs;
592 for (rm = orefs; rm; rm = rm->next) {
593 rm->fetch_head_status = FETCH_HEAD_IGNORE;
594 tail = &rm->next;
595 }
596
c0192df6
JK
597 /*
598 * apply negative refspecs first, before we remove duplicates. This is
599 * necessary as negative refspecs might remove an otherwise conflicting
600 * duplicate.
601 */
602 if (rs->nr)
603 ref_map = apply_negative_refspecs(ref_map, rs);
604 else
605 ref_map = apply_negative_refspecs(ref_map, &remote->fetch);
606
14b8ced3
BW
607 ref_map = ref_remove_duplicates(ref_map);
608
14b8ced3
BW
609 for (rm = ref_map; rm; rm = rm->next) {
610 if (rm->peer_ref) {
e198b3a7
JH
611 const char *refname = rm->peer_ref->name;
612 struct refname_hash_entry *peer_item;
f23a4651 613 unsigned int hash = strhash(refname);
e198b3a7 614
abcb7eeb
JT
615 if (!existing_refs_populated) {
616 refname_hash_init(&existing_refs);
617 for_each_ref(add_one_refname, &existing_refs);
618 existing_refs_populated = 1;
619 }
620
f23a4651
EW
621 peer_item = hashmap_get_entry_from_hash(&existing_refs,
622 hash, refname,
623 struct refname_hash_entry, ent);
14b8ced3 624 if (peer_item) {
e198b3a7 625 struct object_id *old_oid = &peer_item->oid;
14b8ced3
BW
626 oidcpy(&rm->peer_ref->old_oid, old_oid);
627 }
628 }
629 }
abcb7eeb 630 if (existing_refs_populated)
6da1a258 631 hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
14b8ced3
BW
632
633 return ref_map;
b888d61c
DB
634}
635
fa250759
JK
636#define STORE_REF_ERROR_OTHER 1
637#define STORE_REF_ERROR_DF_CONFLICT 2
638
b888d61c
DB
639static int s_update_ref(const char *action,
640 struct ref *ref,
d4c8db8f 641 struct ref_transaction *transaction,
b888d61c
DB
642 int check_old)
643{
1412f762 644 char *msg;
b888d61c 645 char *rla = getenv("GIT_REFLOG_ACTION");
d4c8db8f 646 struct ref_transaction *our_transaction = NULL;
cd94f765 647 struct strbuf err = STRBUF_INIT;
c45889f1 648 int ret;
b888d61c 649
28a15401
JS
650 if (dry_run)
651 return 0;
b888d61c 652 if (!rla)
2d324efa 653 rla = default_rla.buf;
1412f762 654 msg = xstrfmt("%s: %s", rla, action);
cd94f765 655
d4c8db8f
PS
656 /*
657 * If no transaction was passed to us, we manage the transaction
658 * ourselves. Otherwise, we trust the caller to handle the transaction
659 * lifecycle.
660 */
c45889f1 661 if (!transaction) {
d4c8db8f
PS
662 transaction = our_transaction = ref_transaction_begin(&err);
663 if (!transaction) {
664 ret = STORE_REF_ERROR_OTHER;
665 goto out;
666 }
c45889f1
PS
667 }
668
669 ret = ref_transaction_update(transaction, ref->name, &ref->new_oid,
670 check_old ? &ref->old_oid : NULL,
671 0, msg, &err);
cd94f765 672 if (ret) {
c45889f1
PS
673 ret = STORE_REF_ERROR_OTHER;
674 goto out;
cd94f765
RS
675 }
676
d4c8db8f
PS
677 if (our_transaction) {
678 switch (ref_transaction_commit(our_transaction, &err)) {
679 case 0:
680 break;
681 case TRANSACTION_NAME_CONFLICT:
682 ret = STORE_REF_ERROR_DF_CONFLICT;
683 goto out;
684 default:
685 ret = STORE_REF_ERROR_OTHER;
686 goto out;
687 }
c45889f1
PS
688 }
689
690out:
d4c8db8f 691 ref_transaction_free(our_transaction);
c45889f1
PS
692 if (ret)
693 error("%s", err.buf);
cd94f765 694 strbuf_release(&err);
1412f762 695 free(msg);
c45889f1 696 return ret;
b888d61c
DB
697}
698
9539638a 699static int refcol_width(const struct ref *ref_map, int compact_format)
6bc91f23 700{
9539638a
PS
701 const struct ref *ref;
702 int max, width = 10;
6bc91f23 703
9539638a
PS
704 max = term_columns();
705 if (compact_format)
706 max = max * 2 / 3;
6bc91f23 707
9539638a
PS
708 for (ref = ref_map; ref; ref = ref->next) {
709 int rlen, llen = 0, len;
bc437d10 710
9539638a
PS
711 if (ref->status == REF_STATUS_REJECT_SHALLOW ||
712 !ref->peer_ref ||
713 !strcmp(ref->name, "HEAD"))
714 continue;
6bc91f23 715
9539638a
PS
716 /* uptodate lines are only shown on high verbosity level */
717 if (verbosity <= 0 && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
718 continue;
719
720 rlen = utf8_strwidth(prettify_refname(ref->name));
721 if (!compact_format)
722 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
723
724 /*
725 * rough estimation to see if the output line is too long and
726 * should not be counted (we can't do precise calculation
727 * anyway because we don't know if the error explanation part
728 * will be printed in update_local_ref)
729 */
730 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
731 if (len >= max)
732 continue;
733
734 if (width < rlen)
735 width = rlen;
bc437d10 736 }
6bc91f23 737
9539638a 738 return width;
6bc91f23
NTND
739}
740
c4ef5edb 741static void display_state_init(struct display_state *display_state, struct ref *ref_map,
58afbe88 742 const char *raw_url, enum display_format format)
6bc91f23 743{
c4ef5edb 744 int i;
bc437d10 745
ce9636d6 746 memset(display_state, 0, sizeof(*display_state));
d6606e02 747 strbuf_init(&display_state->buf, 0);
58afbe88 748 display_state->format = format;
d6606e02 749
c4ef5edb
PS
750 if (raw_url)
751 display_state->url = transport_anonymize_url(raw_url);
752 else
753 display_state->url = xstrdup("foreign");
754
755 display_state->url_len = strlen(display_state->url);
756 for (i = display_state->url_len - 1; display_state->url[i] == '/' && 0 <= i; i--)
757 ;
758 display_state->url_len = i + 1;
759 if (4 < i && !strncmp(".git", display_state->url + i - 3, 4))
760 display_state->url_len = i - 3;
bc437d10 761
f6bb64df
PS
762 if (verbosity < 0)
763 return;
764
50957937
PS
765 switch (display_state->format) {
766 case DISPLAY_FORMAT_FULL:
767 case DISPLAY_FORMAT_COMPACT:
768 display_state->refcol_width = refcol_width(ref_map,
769 display_state->format == DISPLAY_FORMAT_COMPACT);
770 break;
dd781e38
PS
771 case DISPLAY_FORMAT_PORCELAIN:
772 /* We don't need to precompute anything here. */
773 break;
50957937
PS
774 default:
775 BUG("unexpected display format %d", display_state->format);
6bc91f23
NTND
776 }
777}
165f3902 778
c4ef5edb
PS
779static void display_state_release(struct display_state *display_state)
780{
d6606e02 781 strbuf_release(&display_state->buf);
c4ef5edb
PS
782 free(display_state->url);
783}
784
ce9636d6 785static void print_remote_to_local(struct display_state *display_state,
bc437d10
NTND
786 const char *remote, const char *local)
787{
d6606e02
PS
788 strbuf_addf(&display_state->buf, "%-*s -> %s",
789 display_state->refcol_width, remote, local);
bc437d10
NTND
790}
791
792static int find_and_replace(struct strbuf *haystack,
793 const char *needle,
794 const char *placeholder)
795{
dc40b24d 796 const char *p = NULL;
bc437d10
NTND
797 int plen, nlen;
798
dc40b24d
NTND
799 nlen = strlen(needle);
800 if (ends_with(haystack->buf, needle))
801 p = haystack->buf + haystack->len - nlen;
802 else
803 p = strstr(haystack->buf, needle);
bc437d10
NTND
804 if (!p)
805 return 0;
806
807 if (p > haystack->buf && p[-1] != '/')
808 return 0;
809
810 plen = strlen(p);
bc437d10
NTND
811 if (plen > nlen && p[nlen] != '/')
812 return 0;
813
814 strbuf_splice(haystack, p - haystack->buf, nlen,
815 placeholder, strlen(placeholder));
816 return 1;
817}
818
d6606e02 819static void print_compact(struct display_state *display_state,
bc437d10
NTND
820 const char *remote, const char *local)
821{
822 struct strbuf r = STRBUF_INIT;
823 struct strbuf l = STRBUF_INIT;
824
825 if (!strcmp(remote, local)) {
d6606e02 826 strbuf_addf(&display_state->buf, "%-*s -> *", display_state->refcol_width, remote);
bc437d10
NTND
827 return;
828 }
829
830 strbuf_addstr(&r, remote);
831 strbuf_addstr(&l, local);
832
833 if (!find_and_replace(&r, local, "*"))
834 find_and_replace(&l, remote, "*");
d6606e02 835 print_remote_to_local(display_state, r.buf, l.buf);
bc437d10
NTND
836
837 strbuf_release(&r);
838 strbuf_release(&l);
839}
840
d6606e02
PS
841static void display_ref_update(struct display_state *display_state, char code,
842 const char *summary, const char *error,
843 const char *remote, const char *local,
dd781e38
PS
844 const struct object_id *old_oid,
845 const struct object_id *new_oid,
d6606e02 846 int summary_width)
d0b39a03 847{
dd781e38 848 FILE *f = stderr;
f6bb64df
PS
849
850 if (verbosity < 0)
851 return;
852
d6606e02
PS
853 strbuf_reset(&display_state->buf);
854
50957937
PS
855 switch (display_state->format) {
856 case DISPLAY_FORMAT_FULL:
857 case DISPLAY_FORMAT_COMPACT: {
858 int width;
c4ef5edb 859
50957937
PS
860 if (!display_state->shown_url) {
861 strbuf_addf(&display_state->buf, _("From %.*s\n"),
862 display_state->url_len, display_state->url);
863 display_state->shown_url = 1;
864 }
901f3d40 865
50957937
PS
866 width = (summary_width + strlen(summary) - gettext_width(summary));
867 remote = prettify_refname(remote);
868 local = prettify_refname(local);
869
870 strbuf_addf(&display_state->buf, " %c %-*s ", code, width, summary);
871
872 if (display_state->format != DISPLAY_FORMAT_COMPACT)
873 print_remote_to_local(display_state, remote, local);
874 else
875 print_compact(display_state, remote, local);
876
877 if (error)
878 strbuf_addf(&display_state->buf, " (%s)", error);
879
880 break;
881 }
dd781e38
PS
882 case DISPLAY_FORMAT_PORCELAIN:
883 strbuf_addf(&display_state->buf, "%c %s %s %s", code,
884 oid_to_hex(old_oid), oid_to_hex(new_oid), local);
885 f = stdout;
886 break;
50957937
PS
887 default:
888 BUG("unexpected display format %d", display_state->format);
889 };
d6606e02
PS
890 strbuf_addch(&display_state->buf, '\n');
891
dd781e38 892 fputs(display_state->buf.buf, f);
d0b39a03 893}
165f3902 894
b888d61c 895static int update_local_ref(struct ref *ref,
d4c8db8f 896 struct ref_transaction *transaction,
ce9636d6 897 struct display_state *display_state,
1c31764d 898 const struct ref *remote_ref,
ba28b2ca
PS
899 int summary_width,
900 const struct fetch_config *config)
b888d61c 901{
b888d61c 902 struct commit *current = NULL, *updated;
377444b4 903 int fast_forward = 0;
b888d61c 904
47c61004 905 if (!repo_has_object_file(the_repository, &ref->new_oid))
f4e54d02 906 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
b888d61c 907
4a7e27e9 908 if (oideq(&ref->old_oid, &ref->new_oid)) {
7f87aff2 909 if (verbosity > 0)
d6606e02 910 display_ref_update(display_state, '=', _("[up to date]"), NULL,
dd781e38
PS
911 remote_ref->name, ref->name,
912 &ref->old_oid, &ref->new_oid, summary_width);
b888d61c
DB
913 return 0;
914 }
915
8bc1f39f 916 if (!update_head_ok &&
12d47e3b
DS
917 !is_null_oid(&ref->old_oid) &&
918 branch_checked_out(ref->name)) {
b888d61c
DB
919 /*
920 * If this is the head, and it's not okay to update
921 * the head, and the old value of the head isn't empty...
922 */
d6606e02
PS
923 display_ref_update(display_state, '!', _("[rejected]"),
924 _("can't fetch into checked-out branch"),
dd781e38
PS
925 remote_ref->name, ref->name,
926 &ref->old_oid, &ref->new_oid, summary_width);
b888d61c
DB
927 return 1;
928 }
929
f4e54d02 930 if (!is_null_oid(&ref->old_oid) &&
59556548 931 starts_with(ref->name, "refs/tags/")) {
0bc8d71b
ÆAB
932 if (force || ref->force) {
933 int r;
d4c8db8f 934 r = s_update_ref("updating tag", ref, transaction, 0);
d6606e02
PS
935 display_ref_update(display_state, r ? '!' : 't', _("[tag update]"),
936 r ? _("unable to update local ref") : NULL,
dd781e38
PS
937 remote_ref->name, ref->name,
938 &ref->old_oid, &ref->new_oid, summary_width);
0bc8d71b
ÆAB
939 return r;
940 } else {
d6606e02
PS
941 display_ref_update(display_state, '!', _("[rejected]"),
942 _("would clobber existing tag"),
dd781e38
PS
943 remote_ref->name, ref->name,
944 &ref->old_oid, &ref->new_oid, summary_width);
0bc8d71b
ÆAB
945 return 1;
946 }
b888d61c
DB
947 }
948
21e1ee8f
SB
949 current = lookup_commit_reference_gently(the_repository,
950 &ref->old_oid, 1);
951 updated = lookup_commit_reference_gently(the_repository,
952 &ref->new_oid, 1);
b888d61c 953 if (!current || !updated) {
165f3902
NP
954 const char *msg;
955 const char *what;
6315472e 956 int r;
0997adaa
MB
957 /*
958 * Nicely describe the new ref we're fetching.
959 * Base this on the remote's ref name, as it's
960 * more likely to follow a standard layout.
961 */
6bc7a37e 962 if (starts_with(remote_ref->name, "refs/tags/")) {
b888d61c 963 msg = "storing tag";
f7b3742a 964 what = _("[new tag]");
6bc7a37e 965 } else if (starts_with(remote_ref->name, "refs/heads/")) {
b888d61c 966 msg = "storing head";
f7b3742a 967 what = _("[new branch]");
0997adaa
MB
968 } else {
969 msg = "storing ref";
970 what = _("[new ref]");
165f3902
NP
971 }
972
d4c8db8f 973 r = s_update_ref(msg, ref, transaction, 0);
d6606e02
PS
974 display_ref_update(display_state, r ? '!' : '*', what,
975 r ? _("unable to update local ref") : NULL,
dd781e38
PS
976 remote_ref->name, ref->name,
977 &ref->old_oid, &ref->new_oid, summary_width);
6315472e 978 return r;
b888d61c
DB
979 }
980
ba28b2ca 981 if (config->show_forced_updates) {
377444b4 982 uint64_t t_before = getnanotime();
cb338c23
ÆAB
983 fast_forward = repo_in_merge_bases(the_repository, current,
984 updated);
24876ebf
JS
985 if (fast_forward < 0)
986 exit(128);
377444b4
DS
987 forced_updates_ms += (getnanotime() - t_before) / 1000000;
988 } else {
989 fast_forward = 1;
990 }
991
992 if (fast_forward) {
bd22d4ff 993 struct strbuf quickref = STRBUF_INIT;
6315472e 994 int r;
cdbd70c4 995
30e677e0 996 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
bd22d4ff 997 strbuf_addstr(&quickref, "..");
30e677e0 998 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
d4c8db8f 999 r = s_update_ref("fast-forward", ref, transaction, 1);
d6606e02
PS
1000 display_ref_update(display_state, r ? '!' : ' ', quickref.buf,
1001 r ? _("unable to update local ref") : NULL,
dd781e38
PS
1002 remote_ref->name, ref->name,
1003 &ref->old_oid, &ref->new_oid, summary_width);
bd22d4ff 1004 strbuf_release(&quickref);
6315472e 1005 return r;
165f3902 1006 } else if (force || ref->force) {
bd22d4ff 1007 struct strbuf quickref = STRBUF_INIT;
6315472e 1008 int r;
30e677e0 1009 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
bd22d4ff 1010 strbuf_addstr(&quickref, "...");
30e677e0 1011 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
d4c8db8f 1012 r = s_update_ref("forced-update", ref, transaction, 1);
d6606e02
PS
1013 display_ref_update(display_state, r ? '!' : '+', quickref.buf,
1014 r ? _("unable to update local ref") : _("forced update"),
dd781e38
PS
1015 remote_ref->name, ref->name,
1016 &ref->old_oid, &ref->new_oid, summary_width);
bd22d4ff 1017 strbuf_release(&quickref);
6315472e 1018 return r;
165f3902 1019 } else {
d6606e02 1020 display_ref_update(display_state, '!', _("[rejected]"), _("non-fast-forward"),
dd781e38
PS
1021 remote_ref->name, ref->name,
1022 &ref->old_oid, &ref->new_oid, summary_width);
b888d61c
DB
1023 return 1;
1024 }
b888d61c
DB
1025}
1026
9fec7b21 1027static const struct object_id *iterate_ref_map(void *cb_data)
6b67e0dc 1028{
f0e278b1
JH
1029 struct ref **rm = cb_data;
1030 struct ref *ref = *rm;
6b67e0dc 1031
4820a33b
NTND
1032 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
1033 ref = ref->next;
f0e278b1 1034 if (!ref)
9fec7b21 1035 return NULL;
f0e278b1 1036 *rm = ref->next;
9fec7b21 1037 return &ref->old_oid;
6b67e0dc
JH
1038}
1039
58a646a3
PS
1040struct fetch_head {
1041 FILE *fp;
929d0445 1042 struct strbuf buf;
58a646a3
PS
1043};
1044
1045static int open_fetch_head(struct fetch_head *fetch_head)
1046{
1047 const char *filename = git_path_fetch_head(the_repository);
1048
1049 if (write_fetch_head) {
1050 fetch_head->fp = fopen(filename, "a");
1051 if (!fetch_head->fp)
c4904377 1052 return error_errno(_("cannot open '%s'"), filename);
929d0445 1053 strbuf_init(&fetch_head->buf, 0);
58a646a3
PS
1054 } else {
1055 fetch_head->fp = NULL;
1056 }
1057
1058 return 0;
1059}
1060
1061static void append_fetch_head(struct fetch_head *fetch_head,
1062 const struct object_id *old_oid,
1063 enum fetch_head_status fetch_head_status,
1064 const char *note,
1065 const char *url, size_t url_len)
1066{
1067 char old_oid_hex[GIT_MAX_HEXSZ + 1];
1068 const char *merge_status_marker;
1069 size_t i;
1070
1071 if (!fetch_head->fp)
1072 return;
1073
1074 switch (fetch_head_status) {
1075 case FETCH_HEAD_NOT_FOR_MERGE:
1076 merge_status_marker = "not-for-merge";
1077 break;
1078 case FETCH_HEAD_MERGE:
1079 merge_status_marker = "";
1080 break;
1081 default:
1082 /* do not write anything to FETCH_HEAD */
1083 return;
1084 }
1085
929d0445
PS
1086 strbuf_addf(&fetch_head->buf, "%s\t%s\t%s",
1087 oid_to_hex_r(old_oid_hex, old_oid), merge_status_marker, note);
58a646a3
PS
1088 for (i = 0; i < url_len; ++i)
1089 if ('\n' == url[i])
929d0445 1090 strbuf_addstr(&fetch_head->buf, "\\n");
58a646a3 1091 else
929d0445
PS
1092 strbuf_addch(&fetch_head->buf, url[i]);
1093 strbuf_addch(&fetch_head->buf, '\n');
1094
c7b190da
PS
1095 /*
1096 * When using an atomic fetch, we do not want to update FETCH_HEAD if
1097 * any of the reference updates fails. We thus have to write all
1098 * updates to a buffer first and only commit it as soon as all
1099 * references have been successfully updated.
1100 */
1101 if (!atomic_fetch) {
1102 strbuf_write(&fetch_head->buf, fetch_head->fp);
1103 strbuf_reset(&fetch_head->buf);
1104 }
58a646a3
PS
1105}
1106
1107static void commit_fetch_head(struct fetch_head *fetch_head)
1108{
c7b190da
PS
1109 if (!fetch_head->fp || !atomic_fetch)
1110 return;
1111 strbuf_write(&fetch_head->buf, fetch_head->fp);
58a646a3
PS
1112}
1113
1114static void close_fetch_head(struct fetch_head *fetch_head)
1115{
1116 if (!fetch_head->fp)
1117 return;
1118
1119 fclose(fetch_head->fp);
929d0445 1120 strbuf_release(&fetch_head->buf);
58a646a3
PS
1121}
1122
182f59da 1123static const char warn_show_forced_updates[] =
66996bea
AK
1124N_("fetch normally indicates which branches had a forced update,\n"
1125 "but that check has been disabled; to re-enable, use '--show-forced-updates'\n"
1126 "flag or run 'git config fetch.showForcedUpdates true'");
182f59da 1127static const char warn_time_show_forced_updates[] =
66996bea 1128N_("it took %.2f seconds to check forced updates; you can use\n"
182f59da 1129 "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
66996bea 1130 "to avoid this check\n");
182f59da 1131
ce9636d6 1132static int store_updated_refs(struct display_state *display_state,
c4ef5edb 1133 const char *remote_name,
b3a80466
PS
1134 int connectivity_checked,
1135 struct ref_transaction *transaction, struct ref *ref_map,
ba28b2ca
PS
1136 struct fetch_head *fetch_head,
1137 const struct fetch_config *config)
b888d61c 1138{
c4ef5edb 1139 int rc = 0;
4f40f6cb 1140 struct strbuf note = STRBUF_INIT;
b888d61c
DB
1141 const char *what, *kind;
1142 struct ref *rm;
900f2814 1143 int want_status;
b18aaaa5 1144 int summary_width = 0;
b888d61c 1145
b18aaaa5
PS
1146 if (verbosity >= 0)
1147 summary_width = transport_summary_width(ref_map);
1148
cf1e7c07 1149 if (!connectivity_checked) {
2df1aa23
JT
1150 struct check_connected_options opt = CHECK_CONNECTED_INIT;
1151
c6ce27ab 1152 opt.exclude_hidden_refs_section = "fetch";
cf1e7c07 1153 rm = ref_map;
2df1aa23 1154 if (check_connected(iterate_ref_map, &rm, &opt)) {
c4ef5edb
PS
1155 rc = error(_("%s did not send all necessary objects\n"),
1156 display_state->url);
cf1e7c07
JT
1157 goto abort;
1158 }
9516a598 1159 }
6b67e0dc 1160
96890f4c 1161 /*
900f2814
JK
1162 * We do a pass for each fetch_head_status type in their enum order, so
1163 * merged entries are written before not-for-merge. That lets readers
1164 * use FETCH_HEAD as a refname to refer to the ref to be merged.
96890f4c 1165 */
900f2814
JK
1166 for (want_status = FETCH_HEAD_MERGE;
1167 want_status <= FETCH_HEAD_IGNORE;
1168 want_status++) {
96890f4c
JH
1169 for (rm = ref_map; rm; rm = rm->next) {
1170 struct ref *ref = NULL;
1171
4820a33b
NTND
1172 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
1173 if (want_status == FETCH_HEAD_MERGE)
09667e95 1174 warning(_("rejected %s because shallow roots are not allowed to be updated"),
4820a33b
NTND
1175 rm->peer_ref ? rm->peer_ref->name : rm->name);
1176 continue;
1177 }
1178
fe7df03a 1179 /*
8e55634b
PS
1180 * When writing FETCH_HEAD we need to determine whether
1181 * we already have the commit or not. If not, then the
1182 * reference is not for merge and needs to be written
1183 * to the reflog after other commits which we already
1184 * have. We're not interested in this property though
1185 * in case FETCH_HEAD is not to be updated, so we can
1186 * skip the classification in that case.
fe7df03a 1187 */
8e55634b
PS
1188 if (fetch_head->fp) {
1189 struct commit *commit = NULL;
1190
1191 /*
1192 * References in "refs/tags/" are often going to point
1193 * to annotated tags, which are not part of the
1194 * commit-graph. We thus only try to look up refs in
1195 * the graph which are not in that namespace to not
1196 * regress performance in repositories with many
1197 * annotated tags.
1198 */
1199 if (!starts_with(rm->name, "refs/tags/"))
1200 commit = lookup_commit_in_graph(the_repository, &rm->old_oid);
1201 if (!commit) {
1202 commit = lookup_commit_reference_gently(the_repository,
1203 &rm->old_oid,
1204 1);
1205 if (!commit)
1206 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
1207 }
fe7df03a 1208 }
96890f4c 1209
900f2814 1210 if (rm->fetch_head_status != want_status)
96890f4c
JH
1211 continue;
1212
1213 if (rm->peer_ref) {
6f687c21 1214 ref = alloc_ref(rm->peer_ref->name);
f4e54d02 1215 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
1216 oidcpy(&ref->new_oid, &rm->old_oid);
96890f4c
JH
1217 ref->force = rm->peer_ref->force;
1218 }
b888d61c 1219
56e8bb4f 1220 if (config->recurse_submodules != RECURSE_SUBMODULES_OFF &&
7ea0c2f4 1221 (!rm->peer_ref || !oideq(&ref->old_oid, &ref->new_oid))) {
be76c212 1222 check_for_new_submodule_commits(&rm->old_oid);
7ea0c2f4 1223 }
b888d61c 1224
96890f4c
JH
1225 if (!strcmp(rm->name, "HEAD")) {
1226 kind = "";
1227 what = "";
1c31764d 1228 } else if (skip_prefix(rm->name, "refs/heads/", &what)) {
96890f4c 1229 kind = "branch";
1c31764d 1230 } else if (skip_prefix(rm->name, "refs/tags/", &what)) {
96890f4c 1231 kind = "tag";
1c31764d 1232 } else if (skip_prefix(rm->name, "refs/remotes/", &what)) {
96890f4c 1233 kind = "remote-tracking branch";
1c31764d 1234 } else {
96890f4c
JH
1235 kind = "";
1236 what = rm->name;
1237 }
b888d61c 1238
96890f4c
JH
1239 strbuf_reset(&note);
1240 if (*what) {
1241 if (*kind)
1242 strbuf_addf(&note, "%s ", kind);
1243 strbuf_addf(&note, "'%s' of ", what);
1244 }
58a646a3 1245
2983cec0 1246 append_fetch_head(fetch_head, &rm->old_oid,
58a646a3 1247 rm->fetch_head_status,
c4ef5edb
PS
1248 note.buf, display_state->url,
1249 display_state->url_len);
96890f4c 1250
96890f4c 1251 if (ref) {
1c31764d 1252 rc |= update_local_ref(ref, transaction, display_state,
ba28b2ca 1253 rm, summary_width, config);
96890f4c 1254 free(ref);
db3c293e
JT
1255 } else if (write_fetch_head || dry_run) {
1256 /*
1257 * Display fetches written to FETCH_HEAD (or
1258 * would be written to FETCH_HEAD, if --dry-run
1259 * is set).
1260 */
d6606e02
PS
1261 display_ref_update(display_state, '*',
1262 *kind ? kind : "branch", NULL,
1c31764d 1263 rm->name,
dd781e38
PS
1264 "FETCH_HEAD",
1265 &rm->new_oid, &rm->old_oid,
1266 summary_width);
165f3902
NP
1267 }
1268 }
b888d61c 1269 }
9516a598 1270
fa250759 1271 if (rc & STORE_REF_ERROR_DF_CONFLICT)
bd4a51fc 1272 error(_("some local refs could not be updated; try running\n"
f3cb169b 1273 " 'git remote prune %s' to remove any old, conflicting "
bd4a51fc 1274 "branches"), remote_name);
9516a598 1275
ed9bff08 1276 if (advice_enabled(ADVICE_FETCH_SHOW_FORCED_UPDATES)) {
ba28b2ca 1277 if (!config->show_forced_updates) {
182f59da 1278 warning(_(warn_show_forced_updates));
377444b4 1279 } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
182f59da 1280 warning(_(warn_time_show_forced_updates),
377444b4 1281 forced_updates_ms / 1000.0);
377444b4
DS
1282 }
1283 }
1284
9516a598 1285 abort:
5914f2d0 1286 strbuf_release(&note);
efb98b44 1287 return rc;
b888d61c
DB
1288}
1289
4191c356
SP
1290/*
1291 * We would want to bypass the object transfer altogether if
d9eb0205 1292 * everything we are going to fetch already exists and is connected
4191c356 1293 * locally.
4191c356 1294 */
35f9e3e5 1295static int check_exist_and_connected(struct ref *ref_map)
4191c356 1296{
f0e278b1 1297 struct ref *rm = ref_map;
7043c707 1298 struct check_connected_options opt = CHECK_CONNECTED_INIT;
35f9e3e5 1299 struct ref *r;
f0e278b1 1300
4191c356
SP
1301 /*
1302 * If we are deepening a shallow clone we already have these
1303 * objects reachable. Running rev-list here will return with
1304 * a good (0) exit status and we'll bypass the fetch that we
1305 * really need to perform. Claiming failure now will ensure
1306 * we perform the network exchange to deepen our history.
1307 */
508ea882 1308 if (deepen)
4191c356 1309 return -1;
35f9e3e5 1310
3c7bab06
RC
1311 /*
1312 * Similarly, if we need to refetch, we always want to perform a full
1313 * fetch ignoring existing objects.
1314 */
1315 if (refetch)
1316 return -1;
1317
1318
35f9e3e5
JT
1319 /*
1320 * check_connected() allows objects to merely be promised, but
1321 * we need all direct targets to exist.
1322 */
1323 for (r = rm; r; r = r->next) {
bc726bd0
ÆAB
1324 if (!repo_has_object_file_with_flags(the_repository, &r->old_oid,
1325 OBJECT_INFO_SKIP_FETCH_OBJECT))
35f9e3e5
JT
1326 return -1;
1327 }
1328
7043c707 1329 opt.quiet = 1;
c6ce27ab 1330 opt.exclude_hidden_refs_section = "fetch";
7043c707 1331 return check_connected(iterate_ref_map, &rm, &opt);
4191c356
SP
1332}
1333
ce9636d6
PS
1334static int fetch_and_consume_refs(struct display_state *display_state,
1335 struct transport *transport,
b3a80466 1336 struct ref_transaction *transaction,
8bc1f39f 1337 struct ref *ref_map,
ba28b2ca
PS
1338 struct fetch_head *fetch_head,
1339 const struct fetch_config *config)
b888d61c 1340{
caff8b73 1341 int connectivity_checked = 1;
284b2ce8
PS
1342 int ret;
1343
1344 /*
1345 * We don't need to perform a fetch in case we can already satisfy all
1346 * refs.
1347 */
1348 ret = check_exist_and_connected(ref_map);
5fc31180
JS
1349 if (ret) {
1350 trace2_region_enter("fetch", "fetch_refs", the_repository);
e2842b39 1351 ret = transport_fetch_refs(transport, ref_map);
5fc31180 1352 trace2_region_leave("fetch", "fetch_refs", the_repository);
284b2ce8
PS
1353 if (ret)
1354 goto out;
caff8b73
PS
1355 connectivity_checked = transport->smart_options ?
1356 transport->smart_options->connectivity_checked : 0;
5fc31180 1357 }
05c44226 1358
5fc31180 1359 trace2_region_enter("fetch", "consume_refs", the_repository);
c4ef5edb 1360 ret = store_updated_refs(display_state, transport->remote->name,
b3a80466 1361 connectivity_checked, transaction, ref_map,
ba28b2ca 1362 fetch_head, config);
5fc31180 1363 trace2_region_leave("fetch", "consume_refs", the_repository);
1c7d1ab6
PS
1364
1365out:
58d4d7f1 1366 transport_unlock_pack(transport, 0);
b888d61c
DB
1367 return ret;
1368}
1369
ce9636d6
PS
1370static int prune_refs(struct display_state *display_state,
1371 struct refspec *rs,
583bc419 1372 struct ref_transaction *transaction,
c4ef5edb 1373 struct ref *ref_map)
f360d844 1374{
c4ef5edb 1375 int result = 0;
a2ac50cb 1376 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
583bc419 1377 struct strbuf err = STRBUF_INIT;
f360d844 1378 const char *dangling_msg = dry_run
18986d53
NTND
1379 ? _(" (%s will become dangling)")
1380 : _(" (%s has become dangling)");
f360d844 1381
a087b432 1382 if (!dry_run) {
583bc419
PS
1383 if (transaction) {
1384 for (ref = stale_refs; ref; ref = ref->next) {
1385 result = ref_transaction_delete(transaction, ref->name, NULL, 0,
1386 "fetch: prune", &err);
1387 if (result)
1388 goto cleanup;
1389 }
1390 } else {
1391 struct string_list refnames = STRING_LIST_INIT_NODUP;
a087b432 1392
583bc419
PS
1393 for (ref = stale_refs; ref; ref = ref->next)
1394 string_list_append(&refnames, ref->name);
a087b432 1395
583bc419
PS
1396 result = delete_refs("fetch: prune", &refnames, 0);
1397 string_list_clear(&refnames, 0);
1398 }
a087b432
MH
1399 }
1400
1401 if (verbosity >= 0) {
b18aaaa5
PS
1402 int summary_width = transport_summary_width(stale_refs);
1403
a087b432 1404 for (ref = stale_refs; ref; ref = ref->next) {
d6606e02
PS
1405 display_ref_update(display_state, '-', _("[deleted]"), NULL,
1406 _("(none)"), ref->name,
dd781e38 1407 &ref->new_oid, &ref->old_oid,
d6606e02 1408 summary_width);
f360d844
JS
1409 warn_dangling_symref(stderr, dangling_msg, ref->name);
1410 }
1411 }
a087b432 1412
583bc419
PS
1413cleanup:
1414 strbuf_release(&err);
f360d844
JS
1415 free_refs(stale_refs);
1416 return result;
1417}
1418
12d47e3b 1419static void check_not_current_branch(struct ref *ref_map)
8ee5d731 1420{
12d47e3b 1421 const char *path;
8ee5d731 1422 for (; ref_map; ref_map = ref_map->next)
8bc1f39f 1423 if (ref_map->peer_ref &&
f7400da8 1424 starts_with(ref_map->peer_ref->name, "refs/heads/") &&
12d47e3b 1425 (path = branch_checked_out(ref_map->peer_ref->name)))
8bc1f39f
AK
1426 die(_("refusing to fetch into branch '%s' "
1427 "checked out at '%s'"),
12d47e3b 1428 ref_map->peer_ref->name, path);
8ee5d731
JS
1429}
1430
e6cc5104
JH
1431static int truncate_fetch_head(void)
1432{
102de880 1433 const char *filename = git_path_fetch_head(the_repository);
ea56518d 1434 FILE *fp = fopen_for_writing(filename);
e6cc5104
JH
1435
1436 if (!fp)
c4904377 1437 return error_errno(_("cannot open '%s'"), filename);
e6cc5104
JH
1438 fclose(fp);
1439 return 0;
1440}
1441
db5723c6
JH
1442static void set_option(struct transport *transport, const char *name, const char *value)
1443{
1444 int r = transport_set_option(transport, name, value);
1445 if (r < 0)
66996bea 1446 die(_("option \"%s\" value \"%s\" is not valid for %s"),
db5723c6
JH
1447 name, value, transport->url);
1448 if (r > 0)
66996bea 1449 warning(_("option \"%s\" is ignored for %s\n"),
db5723c6
JH
1450 name, transport->url);
1451}
1452
3390e42a 1453
5cf88fd8 1454static int add_oid(const char *refname UNUSED,
63e14ee2 1455 const struct object_id *oid,
5cf88fd8 1456 int flags UNUSED, void *cb_data)
3390e42a
JT
1457{
1458 struct oid_array *oids = cb_data;
1459
1460 oid_array_append(oids, oid);
1461 return 0;
1462}
1463
1464static void add_negotiation_tips(struct git_transport_options *smart_options)
1465{
1466 struct oid_array *oids = xcalloc(1, sizeof(*oids));
1467 int i;
1468
1469 for (i = 0; i < negotiation_tip.nr; i++) {
1470 const char *s = negotiation_tip.items[i].string;
1471 int old_nr;
1472 if (!has_glob_specials(s)) {
1473 struct object_id oid;
d850b7a5 1474 if (repo_get_oid(the_repository, s, &oid))
82823118
JT
1475 die(_("%s is not a valid object"), s);
1476 if (!has_object(the_repository, &oid, 0))
1477 die(_("the object %s does not exist"), s);
3390e42a
JT
1478 oid_array_append(oids, &oid);
1479 continue;
1480 }
1481 old_nr = oids->nr;
1482 for_each_glob_ref(add_oid, s, oids);
1483 if (old_nr == oids->nr)
66996bea 1484 warning("ignoring --negotiation-tip=%s because it does not match any refs",
3390e42a
JT
1485 s);
1486 }
1487 smart_options->negotiation_tips = oids;
1488}
1489
508ea882 1490static struct transport *prepare_transport(struct remote *remote, int deepen)
db5723c6
JH
1491{
1492 struct transport *transport;
87c2d9d3 1493
db5723c6
JH
1494 transport = transport_get(remote, NULL);
1495 transport_set_verbosity(transport, verbosity, progress);
c915f11e 1496 transport->family = family;
db5723c6
JH
1497 if (upload_pack)
1498 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1499 if (keep)
1500 set_option(transport, TRANS_OPT_KEEP, "yes");
1501 if (depth)
1502 set_option(transport, TRANS_OPT_DEPTH, depth);
508ea882
NTND
1503 if (deepen && deepen_since)
1504 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
a45a2600
NTND
1505 if (deepen && deepen_not.nr)
1506 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1507 (const char *)&deepen_not);
cccf74e2
NTND
1508 if (deepen_relative)
1509 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
48d25cae
NTND
1510 if (update_shallow)
1511 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
3c7bab06
RC
1512 if (refetch)
1513 set_option(transport, TRANS_OPT_REFETCH, "yes");
acb0c572 1514 if (filter_options.choice) {
cf9ceb5a
MD
1515 const char *spec =
1516 expand_list_objects_filter_spec(&filter_options);
1517 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec);
acb0c572
JH
1518 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1519 }
3390e42a
JT
1520 if (negotiation_tip.nr) {
1521 if (transport->smart_options)
1522 add_negotiation_tips(transport->smart_options);
1523 else
66996bea 1524 warning("ignoring --negotiation-tip because the protocol does not support it");
3390e42a 1525 }
db5723c6
JH
1526 return transport;
1527}
1528
ce9636d6
PS
1529static int backfill_tags(struct display_state *display_state,
1530 struct transport *transport,
b3a80466 1531 struct ref_transaction *transaction,
62091b4c 1532 struct ref *ref_map,
ba28b2ca
PS
1533 struct fetch_head *fetch_head,
1534 const struct fetch_config *config)
069d5032 1535{
62091b4c 1536 int retcode, cannot_reuse;
508ea882
NTND
1537
1538 /*
1539 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1540 * when remote helper is used (setting it to an empty string
1541 * is not unsetting). We could extend the remote helper
1542 * protocol for that, but for now, just force a new connection
a45a2600 1543 * without deepen-since. Similar story for deepen-not.
508ea882 1544 */
a45a2600
NTND
1545 cannot_reuse = transport->cannot_reuse ||
1546 deepen_since || deepen_not.nr;
508ea882
NTND
1547 if (cannot_reuse) {
1548 gsecondary = prepare_transport(transport->remote, 0);
b26ed430
JH
1549 transport = gsecondary;
1550 }
1551
069d5032
JH
1552 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1553 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
cccf74e2 1554 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
ba28b2ca
PS
1555 retcode = fetch_and_consume_refs(display_state, transport, transaction, ref_map,
1556 fetch_head, config);
b26ed430
JH
1557
1558 if (gsecondary) {
1559 transport_disconnect(gsecondary);
1560 gsecondary = NULL;
1561 }
62091b4c
PS
1562
1563 return retcode;
069d5032
JH
1564}
1565
b888d61c 1566static int do_fetch(struct transport *transport,
58afbe88 1567 struct refspec *rs,
d1adf85b 1568 const struct fetch_config *config)
b888d61c 1569{
b3a80466 1570 struct ref_transaction *transaction = NULL;
efbade06 1571 struct ref *ref_map = NULL;
c4ef5edb 1572 struct display_state display_state = { 0 };
b888d61c 1573 int autotags = (transport->remote->fetch_tags == 1);
5b87d8d3 1574 int retcode = 0;
6d1700d5 1575 const struct ref *remote_refs;
39835409
JT
1576 struct transport_ls_refs_options transport_ls_refs_options =
1577 TRANSPORT_LS_REFS_OPTIONS_INIT;
e70a3030 1578 int must_list_refs = 1;
2983cec0 1579 struct fetch_head fetch_head = { 0 };
b3a80466 1580 struct strbuf err = STRBUF_INIT;
b1a01e1c 1581
ed368546
DJ
1582 if (tags == TAGS_DEFAULT) {
1583 if (transport->remote->fetch_tags == 2)
1584 tags = TAGS_SET;
1585 if (transport->remote->fetch_tags == -1)
1586 tags = TAGS_UNSET;
1587 }
b888d61c 1588
b888d61c 1589 /* if not appending, truncate FETCH_HEAD */
887952b8 1590 if (!append && write_fetch_head) {
5b87d8d3
MH
1591 retcode = truncate_fetch_head();
1592 if (retcode)
1593 goto cleanup;
d6617c7c 1594 }
b888d61c 1595
e70a3030
JT
1596 if (rs->nr) {
1597 int i;
1598
39835409 1599 refspec_ref_prefixes(rs, &transport_ls_refs_options.ref_prefixes);
e70a3030
JT
1600
1601 /*
1602 * We can avoid listing refs if all of them are exact
1603 * OIDs
1604 */
1605 must_list_refs = 0;
1606 for (i = 0; i < rs->nr; i++) {
1607 if (!rs->items[i].exact_sha1) {
1608 must_list_refs = 1;
1609 break;
1610 }
1611 }
49ca2fba
JK
1612 } else {
1613 struct branch *branch = branch_get(NULL);
1614
1615 if (transport->remote->fetch.nr)
1616 refspec_ref_prefixes(&transport->remote->fetch,
1617 &transport_ls_refs_options.ref_prefixes);
1618 if (branch_has_merge_config(branch) &&
1619 !strcmp(branch->remote_name, transport->remote->name)) {
1620 int i;
1621 for (i = 0; i < branch->merge_nr; i++) {
1622 strvec_push(&transport_ls_refs_options.ref_prefixes,
1623 branch->merge[i]->src);
1624 }
1625 }
1626 }
b888d61c 1627
e70a3030
JT
1628 if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
1629 must_list_refs = 1;
39835409
JT
1630 if (transport_ls_refs_options.ref_prefixes.nr)
1631 strvec_push(&transport_ls_refs_options.ref_prefixes,
1632 "refs/tags/");
b888d61c
DB
1633 }
1634
5fc31180
JS
1635 if (must_list_refs) {
1636 trace2_region_enter("fetch", "remote_refs", the_repository);
39835409
JT
1637 remote_refs = transport_get_remote_refs(transport,
1638 &transport_ls_refs_options);
5fc31180
JS
1639 trace2_region_leave("fetch", "remote_refs", the_repository);
1640 } else
e70a3030
JT
1641 remote_refs = NULL;
1642
f36d4f83 1643 transport_ls_refs_options_release(&transport_ls_refs_options);
6d1700d5
BW
1644
1645 ref_map = get_ref_map(transport->remote, remote_refs, rs,
1646 tags, &autotags);
8ee5d731 1647 if (!update_head_ok)
12d47e3b 1648 check_not_current_branch(ref_map);
b888d61c 1649
2983cec0
PS
1650 retcode = open_fetch_head(&fetch_head);
1651 if (retcode)
1652 goto cleanup;
1653
d1adf85b
PS
1654 display_state_init(&display_state, ref_map, transport->url,
1655 config->display_format);
ce9636d6 1656
b3a80466
PS
1657 if (atomic_fetch) {
1658 transaction = ref_transaction_begin(&err);
1659 if (!transaction) {
18ce4891 1660 retcode = -1;
b3a80466
PS
1661 goto cleanup;
1662 }
1663 }
1664
41fa7d2e
SP
1665 if (tags == TAGS_DEFAULT && autotags)
1666 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
ed43de6e 1667 if (prune) {
0838bf47
MH
1668 /*
1669 * We only prune based on refspecs specified
1670 * explicitly (via command line or configuration); we
1671 * don't care whether --tags was specified.
1672 */
65a1301f 1673 if (rs->nr) {
c4ef5edb 1674 retcode = prune_refs(&display_state, rs, transaction, ref_map);
e8c1e6c7 1675 } else {
ce9636d6 1676 retcode = prune_refs(&display_state, &transport->remote->fetch,
c4ef5edb 1677 transaction, ref_map);
e8c1e6c7 1678 }
c9e04d90
TG
1679 if (retcode != 0)
1680 retcode = 1;
ed43de6e 1681 }
2983cec0 1682
ba28b2ca
PS
1683 if (fetch_and_consume_refs(&display_state, transport, transaction, ref_map,
1684 &fetch_head, config)) {
10a6cc88
TM
1685 retcode = 1;
1686 goto cleanup;
1687 }
24bc1a12 1688
efbade06
PS
1689 /*
1690 * If neither --no-tags nor --tags was specified, do automated tag
1691 * following.
1692 */
1693 if (tags == TAGS_DEFAULT && autotags) {
1694 struct ref *tags_ref_map = NULL, **tail = &tags_ref_map;
1695
b3a80466 1696 find_non_local_tags(remote_refs, transaction, &tags_ref_map, &tail);
62091b4c
PS
1697 if (tags_ref_map) {
1698 /*
1699 * If backfilling of tags fails then we want to tell
1700 * the user so, but we have to continue regardless to
1701 * populate upstream information of the references we
b3a80466
PS
1702 * have already fetched above. The exception though is
1703 * when `--atomic` is passed: in that case we'll abort
1704 * the transaction and don't commit anything.
62091b4c 1705 */
ce9636d6 1706 if (backfill_tags(&display_state, transport, transaction, tags_ref_map,
ba28b2ca 1707 &fetch_head, config))
62091b4c
PS
1708 retcode = 1;
1709 }
efbade06
PS
1710
1711 free_refs(tags_ref_map);
1712 }
1713
b3a80466
PS
1714 if (transaction) {
1715 if (retcode)
1716 goto cleanup;
1717
1718 retcode = ref_transaction_commit(transaction, &err);
1719 if (retcode) {
b3a80466
PS
1720 ref_transaction_free(transaction);
1721 transaction = NULL;
1722 goto cleanup;
1723 }
1724 }
1725
2983cec0
PS
1726 commit_fetch_head(&fetch_head);
1727
24bc1a12
CB
1728 if (set_upstream) {
1729 struct branch *branch = branch_get("HEAD");
1730 struct ref *rm;
1731 struct ref *source_ref = NULL;
1732
1733 /*
1734 * We're setting the upstream configuration for the
15beaaa3 1735 * current branch. The relevant upstream is the
24bc1a12
CB
1736 * fetched branch that is meant to be merged with the
1737 * current one, i.e. the one fetched to FETCH_HEAD.
1738 *
1739 * When there are several such branches, consider the
1740 * request ambiguous and err on the safe side by doing
1741 * nothing and just emit a warning.
1742 */
1743 for (rm = ref_map; rm; rm = rm->next) {
1744 if (!rm->peer_ref) {
1745 if (source_ref) {
391c7e40 1746 warning(_("multiple branches detected, incompatible with --set-upstream"));
efbade06 1747 goto cleanup;
24bc1a12
CB
1748 } else {
1749 source_ref = rm;
1750 }
1751 }
1752 }
1753 if (source_ref) {
17baeaf8
ÆAB
1754 if (!branch) {
1755 const char *shortname = source_ref->name;
1756 skip_prefix(shortname, "refs/heads/", &shortname);
1757
1758 warning(_("could not set upstream of HEAD to '%s' from '%s' when "
1759 "it does not point to any branch."),
1760 shortname, transport->remote->name);
efbade06 1761 goto cleanup;
17baeaf8
ÆAB
1762 }
1763
24bc1a12
CB
1764 if (!strcmp(source_ref->name, "HEAD") ||
1765 starts_with(source_ref->name, "refs/heads/"))
1766 install_branch_config(0,
1767 branch->name,
1768 transport->remote->name,
1769 source_ref->name);
1770 else if (starts_with(source_ref->name, "refs/remotes/"))
1771 warning(_("not setting upstream for a remote remote-tracking branch"));
1772 else if (starts_with(source_ref->name, "refs/tags/"))
1773 warning(_("not setting upstream for a remote tag"));
1774 else
1775 warning(_("unknown branch type"));
1776 } else {
66996bea
AK
1777 warning(_("no source branch found;\n"
1778 "you need to specify exactly one branch with the --set-upstream option"));
24bc1a12
CB
1779 }
1780 }
b888d61c 1781
8bc1f39f 1782cleanup:
18ce4891
JX
1783 if (retcode) {
1784 if (err.len) {
1785 error("%s", err.buf);
1786 strbuf_reset(&err);
1787 }
1788 if (transaction && ref_transaction_abort(transaction, &err) &&
1789 err.len)
1790 error("%s", err.buf);
b888d61c
DB
1791 }
1792
c4ef5edb 1793 display_state_release(&display_state);
2983cec0 1794 close_fetch_head(&fetch_head);
b3a80466 1795 strbuf_release(&err);
efbade06 1796 free_refs(ref_map);
5b87d8d3 1797 return retcode;
b888d61c
DB
1798}
1799
9c4a036b
BG
1800static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1801{
1802 struct string_list *list = priv;
7cc91a2f 1803 if (!remote->skip_default_update)
1d2f80fa 1804 string_list_append(list, remote->name);
9c4a036b
BG
1805 return 0;
1806}
1807
1808struct remote_group_data {
1809 const char *name;
1810 struct string_list *list;
1811};
1812
a4e7e317
GC
1813static int get_remote_group(const char *key, const char *value,
1814 const struct config_context *ctx UNUSED,
1815 void *priv)
9c4a036b
BG
1816{
1817 struct remote_group_data *g = priv;
1818
bc598c32 1819 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
9c4a036b 1820 /* split list by white space */
9c4a036b 1821 while (*value) {
5f65499f
MH
1822 size_t wordlen = strcspn(value, " \t\n");
1823
e286542d 1824 if (wordlen >= 1)
b7410f61 1825 string_list_append_nodup(g->list,
e286542d
MH
1826 xstrndup(value, wordlen));
1827 value += wordlen + (value[wordlen] != '\0');
9c4a036b
BG
1828 }
1829 }
1830
1831 return 0;
1832}
1833
1834static int add_remote_or_group(const char *name, struct string_list *list)
1835{
1836 int prev_nr = list->nr;
66dbfd55
GV
1837 struct remote_group_data g;
1838 g.name = name; g.list = list;
9c4a036b
BG
1839
1840 git_config(get_remote_group, &g);
1841 if (list->nr == prev_nr) {
674468b3 1842 struct remote *remote = remote_get(name);
e459b073 1843 if (!remote_is_configured(remote, 0))
9c4a036b 1844 return 0;
1d2f80fa 1845 string_list_append(list, remote->name);
9c4a036b
BG
1846 }
1847 return 1;
1848}
1849
dd781e38 1850static void add_options_to_argv(struct strvec *argv,
d1adf85b 1851 const struct fetch_config *config)
9c4a036b 1852{
28a15401 1853 if (dry_run)
22f9b7f3 1854 strvec_push(argv, "--dry-run");
90765fa3 1855 if (prune != -1)
22f9b7f3 1856 strvec_push(argv, prune ? "--prune" : "--no-prune");
97716d21 1857 if (prune_tags != -1)
22f9b7f3 1858 strvec_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
bba5322a 1859 if (update_head_ok)
22f9b7f3 1860 strvec_push(argv, "--update-head-ok");
bba5322a 1861 if (force)
22f9b7f3 1862 strvec_push(argv, "--force");
bba5322a 1863 if (keep)
22f9b7f3 1864 strvec_push(argv, "--keep");
56e8bb4f 1865 if (config->recurse_submodules == RECURSE_SUBMODULES_ON)
22f9b7f3 1866 strvec_push(argv, "--recurse-submodules");
56e8bb4f 1867 else if (config->recurse_submodules == RECURSE_SUBMODULES_OFF)
5667141e 1868 strvec_push(argv, "--no-recurse-submodules");
56e8bb4f 1869 else if (config->recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
22f9b7f3 1870 strvec_push(argv, "--recurse-submodules=on-demand");
85566460 1871 if (tags == TAGS_SET)
22f9b7f3 1872 strvec_push(argv, "--tags");
85566460 1873 else if (tags == TAGS_UNSET)
22f9b7f3 1874 strvec_push(argv, "--no-tags");
9c4a036b 1875 if (verbosity >= 2)
22f9b7f3 1876 strvec_push(argv, "-v");
9c4a036b 1877 if (verbosity >= 1)
22f9b7f3 1878 strvec_push(argv, "-v");
9c4a036b 1879 else if (verbosity < 0)
22f9b7f3 1880 strvec_push(argv, "-q");
4e735c13 1881 if (family == TRANSPORT_FAMILY_IPV4)
6854689e 1882 strvec_push(argv, "--ipv4");
4e735c13 1883 else if (family == TRANSPORT_FAMILY_IPV6)
6854689e 1884 strvec_push(argv, "--ipv6");
15184ae9
EW
1885 if (!write_fetch_head)
1886 strvec_push(argv, "--no-write-fetch-head");
d1adf85b 1887 if (config->display_format == DISPLAY_FORMAT_PORCELAIN)
dd781e38 1888 strvec_pushf(argv, "--porcelain");
7dce19d3
JL
1889}
1890
d54dea77
JS
1891/* Fetch multiple remotes in parallel */
1892
1893struct parallel_fetch_state {
1894 const char **argv;
1895 struct string_list *remotes;
1896 int next, result;
d1adf85b 1897 const struct fetch_config *config;
d54dea77
JS
1898};
1899
a5c76b36
JK
1900static int fetch_next_remote(struct child_process *cp,
1901 struct strbuf *out UNUSED,
d54dea77
JS
1902 void *cb, void **task_cb)
1903{
1904 struct parallel_fetch_state *state = cb;
1905 char *remote;
1906
1907 if (state->next < 0 || state->next >= state->remotes->nr)
1908 return 0;
1909
1910 remote = state->remotes->items[state->next++].string;
1911 *task_cb = remote;
1912
22f9b7f3
JK
1913 strvec_pushv(&cp->args, state->argv);
1914 strvec_push(&cp->args, remote);
d54dea77
JS
1915 cp->git_cmd = 1;
1916
d1adf85b 1917 if (verbosity >= 0 && state->config->display_format != DISPLAY_FORMAT_PORCELAIN)
d54dea77
JS
1918 printf(_("Fetching %s\n"), remote);
1919
1920 return 1;
1921}
1922
a5c76b36
JK
1923static int fetch_failed_to_start(struct strbuf *out UNUSED,
1924 void *cb, void *task_cb)
d54dea77
JS
1925{
1926 struct parallel_fetch_state *state = cb;
1927 const char *remote = task_cb;
1928
66996bea 1929 state->result = error(_("could not fetch %s"), remote);
d54dea77
JS
1930
1931 return 0;
1932}
1933
1934static int fetch_finished(int result, struct strbuf *out,
1935 void *cb, void *task_cb)
1936{
1937 struct parallel_fetch_state *state = cb;
1938 const char *remote = task_cb;
1939
1940 if (result) {
1941 strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"),
1942 remote, result);
1943 state->result = -1;
1944 }
1945
1946 return 0;
1947}
1948
dd781e38 1949static int fetch_multiple(struct string_list *list, int max_children,
d1adf85b 1950 const struct fetch_config *config)
7dce19d3
JL
1951{
1952 int i, result = 0;
22f9b7f3 1953 struct strvec argv = STRVEC_INIT;
9c4a036b 1954
887952b8 1955 if (!append && write_fetch_head) {
e6cc5104
JH
1956 int errcode = truncate_fetch_head();
1957 if (errcode)
1958 return errcode;
1959 }
1960
25bccb4b
DS
1961 /*
1962 * Cancel out the fetch.bundleURI config when running subprocesses,
1963 * to avoid fetching from the same bundle list multiple times.
1964 */
1965 strvec_pushl(&argv, "-c", "fetch.bundleURI=",
1966 "fetch", "--append", "--no-auto-gc",
f6d8942b 1967 "--no-write-commit-graph", NULL);
d1adf85b 1968 add_options_to_argv(&argv, config);
85556d4e 1969
d54dea77 1970 if (max_children != 1 && list->nr != 1) {
d1adf85b 1971 struct parallel_fetch_state state = { argv.v, list, 0, 0, config };
36d69bf7
ÆAB
1972 const struct run_process_parallel_opts opts = {
1973 .tr2_category = "fetch",
1974 .tr2_label = "parallel/fetch",
1975
1976 .processes = max_children,
1977
1978 .get_next_task = &fetch_next_remote,
1979 .start_failure = &fetch_failed_to_start,
1980 .task_finished = &fetch_finished,
1981 .data = &state,
1982 };
d54dea77 1983
22f9b7f3 1984 strvec_push(&argv, "--end-of-options");
7dd5762d 1985
36d69bf7 1986 run_processes_parallel(&opts);
7dd5762d 1987 result = state.result;
d54dea77
JS
1988 } else
1989 for (i = 0; i < list->nr; i++) {
1990 const char *name = list->items[i].string;
ddbb47fd
RS
1991 struct child_process cmd = CHILD_PROCESS_INIT;
1992
1993 strvec_pushv(&cmd.args, argv.v);
1994 strvec_push(&cmd.args, name);
d1adf85b 1995 if (verbosity >= 0 && config->display_format != DISPLAY_FORMAT_PORCELAIN)
d54dea77 1996 printf(_("Fetching %s\n"), name);
ddbb47fd
RS
1997 cmd.git_cmd = 1;
1998 if (run_command(&cmd)) {
66996bea 1999 error(_("could not fetch %s"), name);
d54dea77
JS
2000 result = 1;
2001 }
9c4a036b 2002 }
9c4a036b 2003
22f9b7f3 2004 strvec_clear(&argv);
d54dea77 2005 return !!result;
9c4a036b
BG
2006}
2007
aa57b871
JH
2008/*
2009 * Fetching from the promisor remote should use the given filter-spec
2010 * or inherit the default filter-spec from the config.
2011 */
2012static inline void fetch_one_setup_partial(struct remote *remote)
2013{
2014 /*
2015 * Explicit --no-filter argument overrides everything, regardless
2016 * of any prior partial clones and fetches.
2017 */
2018 if (filter_options.no_filter)
2019 return;
2020
2021 /*
2022 * If no prior partial clone/fetch and the current fetch DID NOT
2023 * request a partial-fetch, do a normal fetch.
2024 */
a5183d76 2025 if (!repo_has_promisor_remote(the_repository) && !filter_options.choice)
aa57b871
JH
2026 return;
2027
2028 /*
5e461393
CC
2029 * If this is a partial-fetch request, we enable partial on
2030 * this repo if not already enabled and remember the given
2031 * filter-spec as the default for subsequent fetches to this
23547c40 2032 * remote if there is currently no default filter-spec.
aa57b871 2033 */
5e461393 2034 if (filter_options.choice) {
aa57b871
JH
2035 partial_clone_register(remote->name, &filter_options);
2036 return;
2037 }
2038
aa57b871
JH
2039 /*
2040 * Do a partial-fetch from the promisor remote using either the
2041 * explicitly given filter-spec or inherit the filter-spec from
2042 * the config.
2043 */
2044 if (!filter_options.choice)
fa3d1b63 2045 partial_clone_get_default_filter_spec(&filter_options, remote->name);
aa57b871
JH
2046 return;
2047}
2048
2b713c27 2049static int fetch_one(struct remote *remote, int argc, const char **argv,
58afbe88 2050 int prune_tags_ok, int use_stdin_refspecs,
d1adf85b 2051 const struct fetch_config *config)
b888d61c 2052{
d7c8e307
BW
2053 struct refspec rs = REFSPEC_INIT_FETCH;
2054 int i;
7b7f39ea 2055 int exit_code;
6317972c
ÆAB
2056 int maybe_prune_tags;
2057 int remote_via_config = remote_is_configured(remote, 0);
b888d61c 2058
fa685bdf 2059 if (!remote)
66996bea
AK
2060 die(_("no remote repository specified; please specify either a URL or a\n"
2061 "remote name from which new revisions should be fetched"));
fa685bdf 2062
508ea882 2063 gtransport = prepare_transport(remote, 1);
737c5a9c
MS
2064
2065 if (prune < 0) {
2066 /* no command line request */
07118832
ÆAB
2067 if (0 <= remote->prune)
2068 prune = remote->prune;
b779a25e
PS
2069 else if (0 <= config->prune)
2070 prune = config->prune;
737c5a9c
MS
2071 else
2072 prune = PRUNE_BY_DEFAULT;
2073 }
2074
97716d21
ÆAB
2075 if (prune_tags < 0) {
2076 /* no command line request */
2077 if (0 <= remote->prune_tags)
2078 prune_tags = remote->prune_tags;
2b472cfe
PS
2079 else if (0 <= config->prune_tags)
2080 prune_tags = config->prune_tags;
97716d21
ÆAB
2081 else
2082 prune_tags = PRUNE_TAGS_BY_DEFAULT;
2083 }
2084
6317972c
ÆAB
2085 maybe_prune_tags = prune_tags_ok && prune_tags;
2086 if (maybe_prune_tags && remote_via_config)
95303500 2087 refspec_append(&remote->fetch, TAG_REFSPEC);
97716d21 2088
d7c8e307
BW
2089 if (maybe_prune_tags && (argc || !remote_via_config))
2090 refspec_append(&rs, TAG_REFSPEC);
6317972c 2091
d7c8e307
BW
2092 for (i = 0; i < argc; i++) {
2093 if (!strcmp(argv[i], "tag")) {
d7c8e307
BW
2094 i++;
2095 if (i >= argc)
66996bea 2096 die(_("you need to specify a tag name"));
d7c8e307 2097
1af8b8c0
RS
2098 refspec_appendf(&rs, "refs/tags/%s:refs/tags/%s",
2099 argv[i], argv[i]);
d7c8e307
BW
2100 } else {
2101 refspec_append(&rs, argv[i]);
b888d61c 2102 }
b888d61c
DB
2103 }
2104
2b713c27
JT
2105 if (use_stdin_refspecs) {
2106 struct strbuf line = STRBUF_INIT;
2107 while (strbuf_getline_lf(&line, stdin) != EOF)
2108 refspec_append(&rs, line.buf);
2109 strbuf_release(&line);
2110 }
2111
5e3548ef
BW
2112 if (server_options.nr)
2113 gtransport->server_options = &server_options;
2114
57b235a4 2115 sigchain_push_common(unlock_pack_on_signal);
58d4d7f1 2116 atexit(unlock_pack_atexit);
14358894 2117 sigchain_push(SIGPIPE, SIG_IGN);
d1adf85b 2118 exit_code = do_fetch(gtransport, &rs, config);
14358894 2119 sigchain_pop(SIGPIPE);
d7c8e307 2120 refspec_clear(&rs);
af234459
JH
2121 transport_disconnect(gtransport);
2122 gtransport = NULL;
7b7f39ea 2123 return exit_code;
b888d61c 2124}
9c4a036b
BG
2125
2126int cmd_fetch(int argc, const char **argv, const char *prefix)
2127{
58afbe88
PS
2128 struct fetch_config config = {
2129 .display_format = DISPLAY_FORMAT_FULL,
b779a25e 2130 .prune = -1,
2b472cfe 2131 .prune_tags = -1,
ba28b2ca 2132 .show_forced_updates = 1,
56e8bb4f 2133 .recurse_submodules = RECURSE_SUBMODULES_DEFAULT,
ac197cc0 2134 .parallel = 1,
f7e063f3 2135 .submodule_fetch_jobs = -1,
58afbe88 2136 };
cdc034a0 2137 const char *submodule_prefix = "";
7f0cc04f 2138 const char *bundle_uri;
b7410f61 2139 struct string_list list = STRING_LIST_INIT_DUP;
a1743343 2140 struct remote *remote = NULL;
39487a15 2141 int all = -1, multiple = 0;
9c4a036b 2142 int result = 0;
c1a7902f 2143 int prune_tags_ok = 1;
cdc034a0
PS
2144 int enable_auto_gc = 1;
2145 int unshallow = 0;
2146 int max_jobs = -1;
2147 int recurse_submodules_cli = RECURSE_SUBMODULES_DEFAULT;
2148 int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
2149 int fetch_write_commit_graph = -1;
2150 int stdin_refspecs = 0;
2151 int negotiate_only = 0;
dd781e38 2152 int porcelain = 0;
cdc034a0
PS
2153 int i;
2154
2155 struct option builtin_fetch_options[] = {
2156 OPT__VERBOSITY(&verbosity),
2157 OPT_BOOL(0, "all", &all,
2158 N_("fetch from all remotes")),
2159 OPT_BOOL(0, "set-upstream", &set_upstream,
2160 N_("set upstream for git pull/fetch")),
2161 OPT_BOOL('a', "append", &append,
2162 N_("append to .git/FETCH_HEAD instead of overwriting")),
2163 OPT_BOOL(0, "atomic", &atomic_fetch,
2164 N_("use atomic transaction to update references")),
2165 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
2166 N_("path to upload pack on remote end")),
2167 OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
2168 OPT_BOOL('m', "multiple", &multiple,
2169 N_("fetch from multiple remotes")),
2170 OPT_SET_INT('t', "tags", &tags,
2171 N_("fetch all tags and associated objects"), TAGS_SET),
2172 OPT_SET_INT('n', NULL, &tags,
2173 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
2174 OPT_INTEGER('j', "jobs", &max_jobs,
2175 N_("number of submodules fetched in parallel")),
2176 OPT_BOOL(0, "prefetch", &prefetch,
2177 N_("modify the refspec to place all refs within refs/prefetch/")),
2178 OPT_BOOL('p', "prune", &prune,
2179 N_("prune remote-tracking branches no longer on remote")),
2180 OPT_BOOL('P', "prune-tags", &prune_tags,
2181 N_("prune local tags no longer on remote and clobber changed tags")),
2182 OPT_CALLBACK_F(0, "recurse-submodules", &recurse_submodules_cli, N_("on-demand"),
2183 N_("control recursive fetching of submodules"),
2184 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules),
2185 OPT_BOOL(0, "dry-run", &dry_run,
2186 N_("dry run")),
dd781e38 2187 OPT_BOOL(0, "porcelain", &porcelain, N_("machine-readable output")),
cdc034a0
PS
2188 OPT_BOOL(0, "write-fetch-head", &write_fetch_head,
2189 N_("write fetched references to the FETCH_HEAD file")),
2190 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
2191 OPT_BOOL('u', "update-head-ok", &update_head_ok,
2192 N_("allow updating of HEAD ref")),
2193 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
2194 OPT_STRING(0, "depth", &depth, N_("depth"),
2195 N_("deepen history of shallow clone")),
2196 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
2197 N_("deepen history of shallow repository based on time")),
2198 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
2199 N_("deepen history of shallow clone, excluding rev")),
2200 OPT_INTEGER(0, "deepen", &deepen_relative,
2201 N_("deepen history of shallow clone")),
2202 OPT_SET_INT_F(0, "unshallow", &unshallow,
2203 N_("convert to a complete repository"),
2204 1, PARSE_OPT_NONEG),
2205 OPT_SET_INT_F(0, "refetch", &refetch,
2206 N_("re-fetch without negotiating common commits"),
2207 1, PARSE_OPT_NONEG),
2208 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
2209 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
2210 OPT_CALLBACK_F(0, "recurse-submodules-default",
2211 &recurse_submodules_default, N_("on-demand"),
2212 N_("default for recursive fetching of submodules "
2213 "(lower priority than config files)"),
2214 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules),
2215 OPT_BOOL(0, "update-shallow", &update_shallow,
2216 N_("accept refs that update .git/shallow")),
66e33092 2217 OPT_CALLBACK_F(0, "refmap", &refmap, N_("refmap"),
cdc034a0
PS
2218 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg),
2219 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
ae2c912c 2220 OPT_IPVERSION(&family),
cdc034a0
PS
2221 OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
2222 N_("report that we have only objects reachable from this object")),
2223 OPT_BOOL(0, "negotiate-only", &negotiate_only,
2224 N_("do not fetch a packfile; instead, print ancestors of negotiation tips")),
2225 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
2226 OPT_BOOL(0, "auto-maintenance", &enable_auto_gc,
2227 N_("run 'maintenance --auto' after fetching")),
2228 OPT_BOOL(0, "auto-gc", &enable_auto_gc,
2229 N_("run 'maintenance --auto' after fetching")),
ba28b2ca 2230 OPT_BOOL(0, "show-forced-updates", &config.show_forced_updates,
cdc034a0
PS
2231 N_("check for forced-updates on all updated branches")),
2232 OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph,
2233 N_("write the commit-graph after fetching")),
2234 OPT_BOOL(0, "stdin", &stdin_refspecs,
2235 N_("accept refspecs from stdin")),
2236 OPT_END()
2237 };
9c4a036b 2238
bbc30f99
JK
2239 packet_trace_identity("fetch");
2240
9c4a036b
BG
2241 /* Record the command line for the reflog */
2242 strbuf_addstr(&default_rla, "fetch");
46da295a
JS
2243 for (i = 1; i < argc; i++) {
2244 /* This handles non-URLs gracefully */
2245 char *anon = transport_anonymize_url(argv[i]);
2246
2247 strbuf_addf(&default_rla, " %s", anon);
2248 free(anon);
2249 }
9c4a036b 2250
58afbe88 2251 git_config(git_fetch_config, &config);
059fda19
JS
2252 if (the_repository->gitdir) {
2253 prepare_repo_settings(the_repository);
2254 the_repository->settings.command_requires_full_index = 0;
2255 }
737c5a9c 2256
9c4a036b
BG
2257 argc = parse_options(argc, argv, prefix,
2258 builtin_fetch_options, builtin_fetch_usage, 0);
386c076a
GC
2259
2260 if (recurse_submodules_cli != RECURSE_SUBMODULES_DEFAULT)
56e8bb4f 2261 config.recurse_submodules = recurse_submodules_cli;
386c076a
GC
2262
2263 if (negotiate_only) {
2264 switch (recurse_submodules_cli) {
2265 case RECURSE_SUBMODULES_OFF:
2266 case RECURSE_SUBMODULES_DEFAULT:
2267 /*
2268 * --negotiate-only should never recurse into
2269 * submodules. Skip it by setting recurse_submodules to
2270 * RECURSE_SUBMODULES_OFF.
2271 */
56e8bb4f 2272 config.recurse_submodules = RECURSE_SUBMODULES_OFF;
386c076a
GC
2273 break;
2274
2275 default:
de4eaae6
JH
2276 die(_("options '%s' and '%s' cannot be used together"),
2277 "--negotiate-only", "--recurse-submodules");
386c076a
GC
2278 }
2279 }
2280
56e8bb4f 2281 if (config.recurse_submodules != RECURSE_SUBMODULES_OFF) {
f7e063f3
PS
2282 int *sfjc = config.submodule_fetch_jobs == -1
2283 ? &config.submodule_fetch_jobs : NULL;
56e8bb4f
PS
2284 int *rs = config.recurse_submodules == RECURSE_SUBMODULES_DEFAULT
2285 ? &config.recurse_submodules : NULL;
e5b94213
JT
2286
2287 fetch_config_from_gitmodules(sfjc, rs);
2288 }
9c4a036b 2289
dd781e38
PS
2290
2291 if (porcelain) {
2292 switch (recurse_submodules_cli) {
2293 case RECURSE_SUBMODULES_OFF:
2294 case RECURSE_SUBMODULES_DEFAULT:
2295 /*
2296 * Reference updates in submodules would be ambiguous
2297 * in porcelain mode, so we reject this combination.
2298 */
56e8bb4f 2299 config.recurse_submodules = RECURSE_SUBMODULES_OFF;
dd781e38
PS
2300 break;
2301
2302 default:
2303 die(_("options '%s' and '%s' cannot be used together"),
2304 "--porcelain", "--recurse-submodules");
2305 }
2306
2307 config.display_format = DISPLAY_FORMAT_PORCELAIN;
2308 }
2309
eff40457 2310 if (negotiate_only && !negotiation_tip.nr)
2826ffad 2311 die(_("--negotiate-only needs one or more --negotiation-tip=*"));
eff40457 2312
cccf74e2
NTND
2313 if (deepen_relative) {
2314 if (deepen_relative < 0)
66996bea 2315 die(_("negative depth in --deepen is not supported"));
cccf74e2 2316 if (depth)
43ea635c 2317 die(_("options '%s' and '%s' cannot be used together"), "--deepen", "--depth");
cccf74e2
NTND
2318 depth = xstrfmt("%d", deepen_relative);
2319 }
4dcb167f
NTND
2320 if (unshallow) {
2321 if (depth)
43ea635c 2322 die(_("options '%s' and '%s' cannot be used together"), "--depth", "--unshallow");
c8813487 2323 else if (!is_repository_shallow(the_repository))
4dcb167f 2324 die(_("--unshallow on a complete repository does not make sense"));
2805bb59
JK
2325 else
2326 depth = xstrfmt("%d", INFINITE_DEPTH);
4dcb167f
NTND
2327 }
2328
5594bcad
NTND
2329 /* no need to be strict, transport_set_option() will validate it again */
2330 if (depth && atoi(depth) < 1)
2331 die(_("depth %s is not a positive number"), depth);
a45a2600 2332 if (depth || deepen_since || deepen_not.nr)
508ea882 2333 deepen = 1;
5594bcad 2334
887952b8
JH
2335 /* FETCH_HEAD never gets updated in --dry-run mode */
2336 if (dry_run)
2337 write_fetch_head = 0;
2338
c39952b9
MA
2339 if (!max_jobs)
2340 max_jobs = online_cpus();
2341
7f0cc04f
DS
2342 if (!git_config_get_string_tmp("fetch.bundleuri", &bundle_uri) &&
2343 fetch_bundle_uri(the_repository, bundle_uri, NULL))
2344 warning(_("failed to fetch bundles from '%s'"), bundle_uri);
2345
39487a15
TB
2346 if (all < 0) {
2347 /*
2348 * no --[no-]all given;
2349 * only use config option if no remote was explicitly specified
2350 */
2351 all = (!argc) ? config.all : 0;
2352 }
2353
9c4a036b
BG
2354 if (all) {
2355 if (argc == 1)
bd4a51fc 2356 die(_("fetch --all does not take a repository argument"));
9c4a036b 2357 else if (argc > 1)
bd4a51fc 2358 die(_("fetch --all does not make sense with refspecs"));
39487a15 2359
9c4a036b 2360 (void) for_each_remote(get_one_remote_for_fetch, &list);
0353c688
JH
2361
2362 /* do not do fetch_multiple() of one */
2363 if (list.nr == 1)
2364 remote = remote_get(list.items[0].string);
9c4a036b
BG
2365 } else if (argc == 0) {
2366 /* No arguments -- use default remote */
2367 remote = remote_get(NULL);
16679e37
BG
2368 } else if (multiple) {
2369 /* All arguments are assumed to be remotes or groups */
2370 for (i = 0; i < argc; i++)
2371 if (!add_remote_or_group(argv[i], &list))
66996bea
AK
2372 die(_("no such remote or remote group: %s"),
2373 argv[i]);
9c4a036b
BG
2374 } else {
2375 /* Single remote or group */
2376 (void) add_remote_or_group(argv[0], &list);
2377 if (list.nr > 1) {
2378 /* More than one remote */
2379 if (argc > 1)
66996bea 2380 die(_("fetching a group and specifying refspecs does not make sense"));
9c4a036b
BG
2381 } else {
2382 /* Zero or one remotes */
2383 remote = remote_get(argv[0]);
c1a7902f 2384 prune_tags_ok = (argc == 1);
a1743343
JT
2385 argc--;
2386 argv++;
9c4a036b
BG
2387 }
2388 }
06a668cb 2389 string_list_remove_duplicates(&list, 0);
9c4a036b 2390
9c1e657a
JT
2391 if (negotiate_only) {
2392 struct oidset acked_commits = OIDSET_INIT;
2393 struct oidset_iter iter;
2394 const struct object_id *oid;
2395
2396 if (!remote)
2397 die(_("must supply remote when using --negotiate-only"));
2398 gtransport = prepare_transport(remote, 1);
2399 if (gtransport->smart_options) {
2400 gtransport->smart_options->acked_commits = &acked_commits;
2401 } else {
66996bea 2402 warning(_("protocol does not support --negotiate-only, exiting"));
bec587d4
GC
2403 result = 1;
2404 goto cleanup;
9c1e657a
JT
2405 }
2406 if (server_options.nr)
2407 gtransport->server_options = &server_options;
2408 result = transport_fetch_refs(gtransport, NULL);
2409
2410 oidset_iter_init(&acked_commits, &iter);
2411 while ((oid = oidset_iter_next(&iter)))
2412 printf("%s\n", oid_to_hex(oid));
2413 oidset_clear(&acked_commits);
2414 } else if (remote) {
a5183d76 2415 if (filter_options.choice || repo_has_promisor_remote(the_repository))
aa57b871 2416 fetch_one_setup_partial(remote);
58afbe88 2417 result = fetch_one(remote, argc, argv, prune_tags_ok, stdin_refspecs,
d1adf85b 2418 &config);
acb0c572 2419 } else {
d54dea77
JS
2420 int max_children = max_jobs;
2421
acb0c572 2422 if (filter_options.choice)
e0137875
CC
2423 die(_("--filter can only be used with the remote "
2424 "configured in extensions.partialclone"));
d54dea77 2425
c7b190da
PS
2426 if (atomic_fetch)
2427 die(_("--atomic can only be used when fetching "
2428 "from one remote"));
2429
2b713c27
JT
2430 if (stdin_refspecs)
2431 die(_("--stdin can only be used when fetching "
2432 "from one remote"));
2433
d54dea77 2434 if (max_children < 0)
ac197cc0 2435 max_children = config.parallel;
d54dea77 2436
aa57b871 2437 /* TODO should this also die if we have a previous partial-clone? */
d1adf85b 2438 result = fetch_multiple(&list, max_children, &config);
acb0c572 2439 }
a1743343 2440
0353c688
JH
2441 /*
2442 * This is only needed after fetch_one(), which does not fetch
2443 * submodules by itself.
2444 *
2445 * When we fetch from multiple remotes, fetch_multiple() has
2446 * already updated submodules to grab commits necessary for
2447 * the fetched history from each remote, so there is no need
2448 * to fetch submodules from here.
2449 */
56e8bb4f 2450 if (!result && remote && (config.recurse_submodules != RECURSE_SUBMODULES_OFF)) {
22f9b7f3 2451 struct strvec options = STRVEC_INIT;
d54dea77
JS
2452 int max_children = max_jobs;
2453
2454 if (max_children < 0)
f7e063f3 2455 max_children = config.submodule_fetch_jobs;
d54dea77 2456 if (max_children < 0)
ac197cc0 2457 max_children = config.parallel;
85556d4e 2458
d1adf85b 2459 add_options_to_argv(&options, &config);
b90d9f76
GC
2460 result = fetch_submodules(the_repository,
2461 &options,
2462 submodule_prefix,
56e8bb4f 2463 config.recurse_submodules,
b90d9f76
GC
2464 recurse_submodules_default,
2465 verbosity < 0,
2466 max_children);
22f9b7f3 2467 strvec_clear(&options);
7dce19d3
JL
2468 }
2469
135a12bc
GC
2470 /*
2471 * Skip irrelevant tasks because we know objects were not
2472 * fetched.
2473 *
2474 * NEEDSWORK: as a future optimization, we can return early
2475 * whenever objects were not fetched e.g. if we already have all
2476 * of them.
2477 */
2478 if (negotiate_only)
2479 goto cleanup;
9c4a036b 2480
50f26bd0 2481 prepare_repo_settings(the_repository);
c14e6e79
JS
2482 if (fetch_write_commit_graph > 0 ||
2483 (fetch_write_commit_graph < 0 &&
2484 the_repository->settings.fetch_write_commit_graph)) {
5a535094 2485 int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT;
50f26bd0
DS
2486
2487 if (progress)
5a535094 2488 commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS;
50f26bd0 2489
0bd52e27 2490 write_commit_graph_reachable(the_repository->objects->odb,
50f26bd0 2491 commit_graph_flags,
63020f17 2492 NULL);
50f26bd0
DS
2493 }
2494
7390f05a
RC
2495 if (enable_auto_gc) {
2496 if (refetch) {
2497 /*
2498 * Hint auto-maintenance strongly to encourage repacking,
2499 * but respect config settings disabling it.
2500 */
2501 int opt_val;
2502 if (git_config_get_int("gc.autopacklimit", &opt_val))
2503 opt_val = -1;
2504 if (opt_val != 0)
2505 git_config_push_parameter("gc.autoPackLimit=1");
2506
2507 if (git_config_get_int("maintenance.incremental-repack.auto", &opt_val))
2508 opt_val = -1;
2509 if (opt_val != 0)
2510 git_config_push_parameter("maintenance.incremental-repack.auto=-1");
2511 }
a95ce124 2512 run_auto_maintenance(verbosity < 0);
7390f05a 2513 }
131b8fcb 2514
bec587d4
GC
2515 cleanup:
2516 string_list_clear(&list, 0);
9c4a036b
BG
2517 return result;
2518}