]> git.ipfire.org Git - thirdparty/git.git/blame - builtin/fetch.c
fetch: delay fetch_if_missing=0 until after config
[thirdparty/git.git] / builtin / fetch.c
CommitLineData
b888d61c
DB
1/*
2 * "git fetch"
3 */
4#include "cache.h"
b2141fc1 5#include "config.h"
e724197f 6#include "repository.h"
b888d61c 7#include "refs.h"
ec0cb496 8#include "refspec.h"
cbd53a21 9#include "object-store.h"
b7e2d8bc 10#include "oidset.h"
b888d61c
DB
11#include "commit.h"
12#include "builtin.h"
c455c87c 13#include "string-list.h"
b888d61c
DB
14#include "remote.h"
15#include "transport.h"
4191c356 16#include "run-command.h"
83201998 17#include "parse-options.h"
4a16d072 18#include "sigchain.h"
027771fc 19#include "submodule-config.h"
7dce19d3 20#include "submodule.h"
f96400cb 21#include "connected.h"
85556d4e 22#include "argv-array.h"
6bc91f23 23#include "utf8.h"
3836d88a 24#include "packfile.h"
acb0c572 25#include "list-objects-filter-options.h"
64043556 26#include "commit-reach.h"
24bc1a12 27#include "branch.h"
b14ed5ad 28#include "promisor-remote.h"
50f26bd0 29#include "commit-graph.h"
b888d61c 30
377444b4
DS
31#define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
32
83201998 33static const char * const builtin_fetch_usage[] = {
719acedb
NTND
34 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
35 N_("git fetch [<options>] <group>"),
36 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
37 N_("git fetch --all [<options>]"),
83201998
KH
38 NULL
39};
b888d61c 40
83201998
KH
41enum {
42 TAGS_UNSET = 0,
43 TAGS_DEFAULT = 1,
44 TAGS_SET = 2
45};
46
737c5a9c 47static int fetch_prune_config = -1; /* unspecified */
cdbd70c4 48static int fetch_show_forced_updates = 1;
377444b4 49static uint64_t forced_updates_ms = 0;
737c5a9c
MS
50static int prune = -1; /* unspecified */
51#define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
52
97716d21
ÆAB
53static int fetch_prune_tags_config = -1; /* unspecified */
54static int prune_tags = -1; /* unspecified */
55#define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
56
24bc1a12
CB
57static int all, append, dry_run, force, keep, multiple, update_head_ok;
58static int verbosity, deepen_relative, set_upstream;
8c69832d 59static int progress = -1;
c3d6b703 60static int enable_auto_gc = 1;
508ea882 61static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
d54dea77
JS
62static int max_jobs = -1, submodule_fetch_jobs_config = -1;
63static int fetch_parallel_config = 1;
c915f11e 64static enum transport_family family;
4191c356 65static const char *depth;
508ea882 66static const char *deepen_since;
83201998 67static const char *upload_pack;
a45a2600 68static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
2d324efa 69static struct strbuf default_rla = STRBUF_INIT;
af234459 70static struct transport *gtransport;
b26ed430 71static struct transport *gsecondary;
7dce19d3 72static const char *submodule_prefix = "";
8c69832d 73static int recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
e8906a90 74static int recurse_submodules_default = RECURSE_SUBMODULES_ON_DEMAND;
4b3b33a7 75static int shown_url = 0;
e4cffacc 76static struct refspec refmap = REFSPEC_INIT_FETCH;
acb0c572 77static struct list_objects_filter_options filter_options;
5e3548ef 78static struct string_list server_options = STRING_LIST_INIT_DUP;
3390e42a 79static struct string_list negotiation_tip = STRING_LIST_INIT_NODUP;
e4022ed2 80
737c5a9c
MS
81static int git_fetch_config(const char *k, const char *v, void *cb)
82{
83 if (!strcmp(k, "fetch.prune")) {
84 fetch_prune_config = git_config_bool(k, v);
85 return 0;
86 }
58f4203e 87
97716d21
ÆAB
88 if (!strcmp(k, "fetch.prunetags")) {
89 fetch_prune_tags_config = git_config_bool(k, v);
90 return 0;
91 }
92
cdbd70c4
DS
93 if (!strcmp(k, "fetch.showforcedupdates")) {
94 fetch_show_forced_updates = git_config_bool(k, v);
95 return 0;
96 }
97
58f4203e
SB
98 if (!strcmp(k, "submodule.recurse")) {
99 int r = git_config_bool(k, v) ?
100 RECURSE_SUBMODULES_ON : RECURSE_SUBMODULES_OFF;
101 recurse_submodules = r;
102 }
103
f20e7c1e 104 if (!strcmp(k, "submodule.fetchjobs")) {
d54dea77 105 submodule_fetch_jobs_config = parse_submodule_fetchjobs(k, v);
f20e7c1e 106 return 0;
8fa29159
BW
107 } else if (!strcmp(k, "fetch.recursesubmodules")) {
108 recurse_submodules = parse_fetch_recurse_submodules_arg(k, v);
109 return 0;
f20e7c1e
BW
110 }
111
d54dea77
JS
112 if (!strcmp(k, "fetch.parallel")) {
113 fetch_parallel_config = git_config_int(k, v);
114 if (fetch_parallel_config < 0)
115 die(_("fetch.parallel cannot be negative"));
116 return 0;
117 }
118
72549dfd 119 return git_default_config(k, v, cb);
737c5a9c
MS
120}
121
c5558f80
JH
122static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
123{
517fe807
JK
124 BUG_ON_OPT_NEG(unset);
125
c5558f80
JH
126 /*
127 * "git fetch --refmap='' origin foo"
128 * can be used to tell the command not to store anywhere
129 */
e4cffacc
BW
130 refspec_append(&refmap, arg);
131
c5558f80
JH
132 return 0;
133}
134
83201998 135static struct option builtin_fetch_options[] = {
7f87aff2 136 OPT__VERBOSITY(&verbosity),
d5d09d47
SB
137 OPT_BOOL(0, "all", &all,
138 N_("fetch from all remotes")),
24bc1a12
CB
139 OPT_BOOL(0, "set-upstream", &set_upstream,
140 N_("set upstream for git pull/fetch")),
d5d09d47
SB
141 OPT_BOOL('a', "append", &append,
142 N_("append to .git/FETCH_HEAD instead of overwriting")),
719acedb
NTND
143 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
144 N_("path to upload pack on remote end")),
8cd4b7c1 145 OPT__FORCE(&force, N_("force overwrite of local reference"), 0),
d5d09d47
SB
146 OPT_BOOL('m', "multiple", &multiple,
147 N_("fetch from multiple remotes")),
83201998 148 OPT_SET_INT('t', "tags", &tags,
719acedb 149 N_("fetch all tags and associated objects"), TAGS_SET),
e7951290 150 OPT_SET_INT('n', NULL, &tags,
719acedb 151 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
d54dea77 152 OPT_INTEGER('j', "jobs", &max_jobs,
62104ba1 153 N_("number of submodules fetched in parallel")),
d5d09d47
SB
154 OPT_BOOL('p', "prune", &prune,
155 N_("prune remote-tracking branches no longer on remote")),
97716d21
ÆAB
156 OPT_BOOL('P', "prune-tags", &prune_tags,
157 N_("prune local tags no longer on remote and clobber changed tags")),
886dc154 158 { OPTION_CALLBACK, 0, "recurse-submodules", &recurse_submodules, N_("on-demand"),
719acedb 159 N_("control recursive fetching of submodules"),
886dc154 160 PARSE_OPT_OPTARG, option_fetch_parse_recurse_submodules },
d5d09d47
SB
161 OPT_BOOL(0, "dry-run", &dry_run,
162 N_("dry run")),
163 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
164 OPT_BOOL('u', "update-head-ok", &update_head_ok,
719acedb
NTND
165 N_("allow updating of HEAD ref")),
166 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
167 OPT_STRING(0, "depth", &depth, N_("depth"),
168 N_("deepen history of shallow clone")),
508ea882
NTND
169 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
170 N_("deepen history of shallow repository based on time")),
a45a2600 171 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
6d873865 172 N_("deepen history of shallow clone, excluding rev")),
cccf74e2
NTND
173 OPT_INTEGER(0, "deepen", &deepen_relative,
174 N_("deepen history of shallow clone")),
3e4a67b4
NTND
175 OPT_SET_INT_F(0, "unshallow", &unshallow,
176 N_("convert to a complete repository"),
177 1, PARSE_OPT_NONEG),
719acedb
NTND
178 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
179 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
8c69832d
SB
180 { OPTION_CALLBACK, 0, "recurse-submodules-default",
181 &recurse_submodules_default, N_("on-demand"),
182 N_("default for recursive fetching of submodules "
183 "(lower priority than config files)"),
184 PARSE_OPT_HIDDEN, option_fetch_parse_recurse_submodules },
48d25cae
NTND
185 OPT_BOOL(0, "update-shallow", &update_shallow,
186 N_("accept refs that update .git/shallow")),
c5558f80
JH
187 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
188 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
5e3548ef 189 OPT_STRING_LIST('o', "server-option", &server_options, N_("server-specific"), N_("option to transmit")),
c915f11e
EW
190 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
191 TRANSPORT_FAMILY_IPV4),
192 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
193 TRANSPORT_FAMILY_IPV6),
3390e42a
JT
194 OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip, N_("revision"),
195 N_("report that we have only objects reachable from this object")),
acb0c572 196 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
c3d6b703
NTND
197 OPT_BOOL(0, "auto-gc", &enable_auto_gc,
198 N_("run 'gc --auto' after fetching")),
cdbd70c4
DS
199 OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates,
200 N_("check for forced-updates on all updated branches")),
83201998
KH
201 OPT_END()
202};
203
e4022ed2
SP
204static void unlock_pack(void)
205{
af234459
JH
206 if (gtransport)
207 transport_unlock_pack(gtransport);
b26ed430
JH
208 if (gsecondary)
209 transport_unlock_pack(gsecondary);
e4022ed2
SP
210}
211
212static void unlock_pack_on_signal(int signo)
213{
214 unlock_pack();
4a16d072 215 sigchain_pop(signo);
e4022ed2
SP
216 raise(signo);
217}
b888d61c 218
85682c19 219static void add_merge_config(struct ref **head,
4577370e 220 const struct ref *remote_refs,
85682c19
SP
221 struct branch *branch,
222 struct ref ***tail)
b888d61c 223{
85682c19 224 int i;
b888d61c 225
85682c19
SP
226 for (i = 0; i < branch->merge_nr; i++) {
227 struct ref *rm, **old_tail = *tail;
0ad4a5ff 228 struct refspec_item refspec;
85682c19
SP
229
230 for (rm = *head; rm; rm = rm->next) {
231 if (branch_merge_matches(branch, i, rm->name)) {
900f2814 232 rm->fetch_head_status = FETCH_HEAD_MERGE;
85682c19
SP
233 break;
234 }
b888d61c 235 }
85682c19
SP
236 if (rm)
237 continue;
238
9ad7c5ae 239 /*
8b3f3f84 240 * Not fetched to a remote-tracking branch? We need to fetch
85682c19 241 * it anyway to allow this branch's "branch.$name.merge"
05207a28 242 * to be honored by 'git pull', but we do not have to
9ad7c5ae
JH
243 * fail if branch.$name.merge is misconfigured to point
244 * at a nonexisting branch. If we were indeed called by
05207a28 245 * 'git pull', it will notice the misconfiguration because
9ad7c5ae
JH
246 * there is no entry in the resulting FETCH_HEAD marked
247 * for merging.
85682c19 248 */
8da61a2a 249 memset(&refspec, 0, sizeof(refspec));
85682c19 250 refspec.src = branch->merge[i]->src;
9ad7c5ae 251 get_fetch_map(remote_refs, &refspec, tail, 1);
85682c19 252 for (rm = *old_tail; rm; rm = rm->next)
900f2814 253 rm->fetch_head_status = FETCH_HEAD_MERGE;
b888d61c
DB
254 }
255}
256
b7e2d8bc 257static void create_fetch_oidset(struct ref **head, struct oidset *out)
a0fbb5a3
MH
258{
259 struct ref *rm = *head;
260 while (rm) {
b7e2d8bc 261 oidset_insert(out, &rm->old_oid);
a0fbb5a3
MH
262 rm = rm->next;
263 }
a0fbb5a3
MH
264}
265
e198b3a7 266struct refname_hash_entry {
e2b5038d 267 struct hashmap_entry ent;
e198b3a7 268 struct object_id oid;
f80d9223 269 int ignore;
e198b3a7
JH
270 char refname[FLEX_ARRAY];
271};
272
273static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data,
939af16e
EW
274 const struct hashmap_entry *eptr,
275 const struct hashmap_entry *entry_or_key,
e198b3a7
JH
276 const void *keydata)
277{
939af16e 278 const struct refname_hash_entry *e1, *e2;
e198b3a7 279
939af16e
EW
280 e1 = container_of(eptr, const struct refname_hash_entry, ent);
281 e2 = container_of(entry_or_key, const struct refname_hash_entry, ent);
e198b3a7
JH
282 return strcmp(e1->refname, keydata ? keydata : e2->refname);
283}
284
285static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
286 const char *refname,
287 const struct object_id *oid)
288{
289 struct refname_hash_entry *ent;
290 size_t len = strlen(refname);
291
292 FLEX_ALLOC_MEM(ent, refname, refname, len);
d22245a2 293 hashmap_entry_init(&ent->ent, strhash(refname));
e198b3a7 294 oidcpy(&ent->oid, oid);
b94e5c1d 295 hashmap_add(map, &ent->ent);
e198b3a7
JH
296 return ent;
297}
298
299static int add_one_refname(const char *refname,
300 const struct object_id *oid,
301 int flag, void *cbdata)
302{
303 struct hashmap *refname_map = cbdata;
304
305 (void) refname_hash_add(refname_map, refname, oid);
306 return 0;
307}
308
309static void refname_hash_init(struct hashmap *map)
310{
311 hashmap_init(map, refname_hash_entry_cmp, NULL, 0);
312}
313
314static int refname_hash_exists(struct hashmap *map, const char *refname)
315{
316 return !!hashmap_get_from_hash(map, strhash(refname), refname);
317}
318
a8363b57
FC
319static void clear_item(struct refname_hash_entry *item)
320{
f80d9223 321 item->ignore = 1;
a8363b57
FC
322}
323
6d1700d5
BW
324static void find_non_local_tags(const struct ref *refs,
325 struct ref **head,
326 struct ref ***tail)
a0fbb5a3 327{
e198b3a7
JH
328 struct hashmap existing_refs;
329 struct hashmap remote_refs;
b7e2d8bc 330 struct oidset fetch_oids = OIDSET_INIT;
e198b3a7
JH
331 struct string_list remote_refs_list = STRING_LIST_INIT_NODUP;
332 struct string_list_item *remote_ref_item;
a0fbb5a3 333 const struct ref *ref;
e198b3a7
JH
334 struct refname_hash_entry *item = NULL;
335
336 refname_hash_init(&existing_refs);
337 refname_hash_init(&remote_refs);
b7e2d8bc 338 create_fetch_oidset(head, &fetch_oids);
a0fbb5a3 339
e198b3a7 340 for_each_ref(add_one_refname, &existing_refs);
6d1700d5 341 for (ref = refs; ref; ref = ref->next) {
ad704485 342 if (!starts_with(ref->name, "refs/tags/"))
a0fbb5a3
MH
343 continue;
344
345 /*
346 * The peeled ref always follows the matching base
347 * ref, so if we see a peeled ref that we don't want
348 * to fetch then we can mark the ref entry in the list
349 * as one to ignore by setting util to NULL.
350 */
ad704485 351 if (ends_with(ref->name, "^{}")) {
5827a035 352 if (item &&
e83e71c5
JT
353 !has_object_file_with_flags(&ref->old_oid,
354 OBJECT_INFO_QUICK) &&
b7e2d8bc 355 !oidset_contains(&fetch_oids, &ref->old_oid) &&
98374a07 356 !has_object_file_with_flags(&item->oid, OBJECT_INFO_QUICK) &&
b7e2d8bc 357 !oidset_contains(&fetch_oids, &item->oid))
a8363b57 358 clear_item(item);
a0fbb5a3
MH
359 item = NULL;
360 continue;
361 }
362
363 /*
364 * If item is non-NULL here, then we previously saw a
365 * ref not followed by a peeled reference, so we need
366 * to check if it is a lightweight tag that we want to
367 * fetch.
368 */
5827a035 369 if (item &&
98374a07 370 !has_object_file_with_flags(&item->oid, OBJECT_INFO_QUICK) &&
b7e2d8bc 371 !oidset_contains(&fetch_oids, &item->oid))
a8363b57 372 clear_item(item);
a0fbb5a3
MH
373
374 item = NULL;
375
376 /* skip duplicates and refs that we already have */
e198b3a7
JH
377 if (refname_hash_exists(&remote_refs, ref->name) ||
378 refname_hash_exists(&existing_refs, ref->name))
a0fbb5a3
MH
379 continue;
380
e198b3a7
JH
381 item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
382 string_list_insert(&remote_refs_list, ref->name);
a0fbb5a3 383 }
c8e424c9 384 hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
a0fbb5a3
MH
385
386 /*
387 * We may have a final lightweight tag that needs to be
388 * checked to see if it needs fetching.
389 */
5827a035 390 if (item &&
98374a07 391 !has_object_file_with_flags(&item->oid, OBJECT_INFO_QUICK) &&
b7e2d8bc 392 !oidset_contains(&fetch_oids, &item->oid))
a8363b57 393 clear_item(item);
a0fbb5a3
MH
394
395 /*
e198b3a7 396 * For all the tags in the remote_refs_list,
a0fbb5a3
MH
397 * add them to the list of refs to be fetched
398 */
e198b3a7
JH
399 for_each_string_list_item(remote_ref_item, &remote_refs_list) {
400 const char *refname = remote_ref_item->string;
9528b80b 401 struct ref *rm;
f23a4651 402 unsigned int hash = strhash(refname);
e198b3a7 403
f23a4651
EW
404 item = hashmap_get_entry_from_hash(&remote_refs, hash, refname,
405 struct refname_hash_entry, ent);
e198b3a7
JH
406 if (!item)
407 BUG("unseen remote ref?");
408
a0fbb5a3 409 /* Unless we have already decided to ignore this item... */
f80d9223 410 if (item->ignore)
9528b80b
FC
411 continue;
412
413 rm = alloc_ref(item->refname);
414 rm->peer_ref = alloc_ref(item->refname);
415 oidcpy(&rm->old_oid, &item->oid);
416 **tail = rm;
417 *tail = &rm->next;
a0fbb5a3 418 }
c8e424c9 419 hashmap_free_entries(&remote_refs, struct refname_hash_entry, ent);
e198b3a7 420 string_list_clear(&remote_refs_list, 0);
b7e2d8bc 421 oidset_clear(&fetch_oids);
a0fbb5a3 422}
767f176a 423
6d1700d5
BW
424static struct ref *get_ref_map(struct remote *remote,
425 const struct ref *remote_refs,
65d96c8b 426 struct refspec *rs,
f137a45e 427 int tags, int *autotags)
b888d61c
DB
428{
429 int i;
430 struct ref *rm;
431 struct ref *ref_map = NULL;
432 struct ref **tail = &ref_map;
433
c5a84e92
MH
434 /* opportunistically-updated references: */
435 struct ref *orefs = NULL, **oref_tail = &orefs;
b888d61c 436
e198b3a7 437 struct hashmap existing_refs;
f2690487 438
65d96c8b 439 if (rs->nr) {
c5558f80 440 struct refspec *fetch_refspec;
c5558f80 441
65d96c8b
BW
442 for (i = 0; i < rs->nr; i++) {
443 get_fetch_map(remote_refs, &rs->items[i], &tail, 0);
444 if (rs->items[i].dst && rs->items[i].dst[0])
b888d61c
DB
445 *autotags = 1;
446 }
0281c930 447 /* Merge everything on the command line (but not --tags) */
b888d61c 448 for (rm = ref_map; rm; rm = rm->next)
900f2814 449 rm->fetch_head_status = FETCH_HEAD_MERGE;
f2690487
JK
450
451 /*
0281c930
MH
452 * For any refs that we happen to be fetching via
453 * command-line arguments, the destination ref might
454 * have been missing or have been different than the
455 * remote-tracking ref that would be derived from the
456 * configured refspec. In these cases, we want to
457 * take the opportunity to update their configured
458 * remote-tracking reference. However, we do not want
459 * to mention these entries in FETCH_HEAD at all, as
460 * they would simply be duplicates of existing
461 * entries, so we set them FETCH_HEAD_IGNORE below.
462 *
463 * We compute these entries now, based only on the
464 * refspecs specified on the command line. But we add
465 * them to the list following the refspecs resulting
466 * from the tags option so that one of the latter,
467 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
468 * by ref_remove_duplicates() in favor of one of these
469 * opportunistic entries with FETCH_HEAD_IGNORE.
f2690487 470 */
65d96c8b
BW
471 if (refmap.nr)
472 fetch_refspec = &refmap;
473 else
6d1700d5 474 fetch_refspec = &remote->fetch;
c5558f80 475
65d96c8b
BW
476 for (i = 0; i < fetch_refspec->nr; i++)
477 get_fetch_map(ref_map, &fetch_refspec->items[i], &oref_tail, 1);
e4cffacc 478 } else if (refmap.nr) {
c5558f80 479 die("--refmap option is only meaningful with command-line refspec(s).");
b888d61c
DB
480 } else {
481 /* Use the defaults */
85682c19
SP
482 struct branch *branch = branch_get(NULL);
483 int has_merge = branch_has_merge_config(branch);
3ee1757b 484 if (remote &&
e5349abf 485 (remote->fetch.nr ||
f31dbdc7 486 /* Note: has_merge implies non-NULL branch->remote_name */
3ee1757b 487 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
e5349abf
BW
488 for (i = 0; i < remote->fetch.nr; i++) {
489 get_fetch_map(remote_refs, &remote->fetch.items[i], &tail, 0);
490 if (remote->fetch.items[i].dst &&
491 remote->fetch.items[i].dst[0])
b888d61c 492 *autotags = 1;
85682c19 493 if (!i && !has_merge && ref_map &&
e5349abf 494 !remote->fetch.items[0].pattern)
900f2814 495 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
b888d61c 496 }
da0204df
JS
497 /*
498 * if the remote we're fetching from is the same
499 * as given in branch.<name>.remote, we add the
500 * ref given in branch.<name>.merge, too.
f31dbdc7
BC
501 *
502 * Note: has_merge implies non-NULL branch->remote_name
da0204df 503 */
9ad7c5ae
JH
504 if (has_merge &&
505 !strcmp(branch->remote_name, remote->name))
85682c19 506 add_merge_config(&ref_map, remote_refs, branch, &tail);
b888d61c
DB
507 } else {
508 ref_map = get_remote_ref(remote_refs, "HEAD");
9ad7c5ae 509 if (!ref_map)
bd4a51fc 510 die(_("Couldn't find remote ref HEAD"));
900f2814 511 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
5aaf7f2a 512 tail = &ref_map->next;
b888d61c
DB
513 }
514 }
0281c930 515
c5a84e92
MH
516 if (tags == TAGS_SET)
517 /* also fetch all tags */
518 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
519 else if (tags == TAGS_DEFAULT && *autotags)
6d1700d5 520 find_non_local_tags(remote_refs, &ref_map, &tail);
0281c930 521
c5a84e92
MH
522 /* Now append any refs to be updated opportunistically: */
523 *tail = orefs;
524 for (rm = orefs; rm; rm = rm->next) {
525 rm->fetch_head_status = FETCH_HEAD_IGNORE;
526 tail = &rm->next;
527 }
528
14b8ced3
BW
529 ref_map = ref_remove_duplicates(ref_map);
530
e198b3a7
JH
531 refname_hash_init(&existing_refs);
532 for_each_ref(add_one_refname, &existing_refs);
533
14b8ced3
BW
534 for (rm = ref_map; rm; rm = rm->next) {
535 if (rm->peer_ref) {
e198b3a7
JH
536 const char *refname = rm->peer_ref->name;
537 struct refname_hash_entry *peer_item;
f23a4651 538 unsigned int hash = strhash(refname);
e198b3a7 539
f23a4651
EW
540 peer_item = hashmap_get_entry_from_hash(&existing_refs,
541 hash, refname,
542 struct refname_hash_entry, ent);
14b8ced3 543 if (peer_item) {
e198b3a7 544 struct object_id *old_oid = &peer_item->oid;
14b8ced3
BW
545 oidcpy(&rm->peer_ref->old_oid, old_oid);
546 }
547 }
548 }
c8e424c9 549 hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
14b8ced3
BW
550
551 return ref_map;
b888d61c
DB
552}
553
fa250759
JK
554#define STORE_REF_ERROR_OTHER 1
555#define STORE_REF_ERROR_DF_CONFLICT 2
556
b888d61c
DB
557static int s_update_ref(const char *action,
558 struct ref *ref,
559 int check_old)
560{
1412f762 561 char *msg;
b888d61c 562 char *rla = getenv("GIT_REFLOG_ACTION");
cd94f765
RS
563 struct ref_transaction *transaction;
564 struct strbuf err = STRBUF_INIT;
565 int ret, df_conflict = 0;
b888d61c 566
28a15401
JS
567 if (dry_run)
568 return 0;
b888d61c 569 if (!rla)
2d324efa 570 rla = default_rla.buf;
1412f762 571 msg = xstrfmt("%s: %s", rla, action);
cd94f765
RS
572
573 transaction = ref_transaction_begin(&err);
574 if (!transaction ||
1d147bdf 575 ref_transaction_update(transaction, ref->name,
89f3bbdd 576 &ref->new_oid,
577 check_old ? &ref->old_oid : NULL,
1d147bdf 578 0, msg, &err))
cd94f765
RS
579 goto fail;
580
581 ret = ref_transaction_commit(transaction, &err);
582 if (ret) {
583 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
584 goto fail;
585 }
586
587 ref_transaction_free(transaction);
588 strbuf_release(&err);
1412f762 589 free(msg);
b888d61c 590 return 0;
cd94f765
RS
591fail:
592 ref_transaction_free(transaction);
593 error("%s", err.buf);
594 strbuf_release(&err);
1412f762 595 free(msg);
cd94f765
RS
596 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
597 : STORE_REF_ERROR_OTHER;
b888d61c
DB
598}
599
6bc91f23 600static int refcol_width = 10;
bc437d10 601static int compact_format;
6bc91f23
NTND
602
603static void adjust_refcol_width(const struct ref *ref)
604{
605 int max, rlen, llen, len;
606
607 /* uptodate lines are only shown on high verbosity level */
4a7e27e9 608 if (!verbosity && oideq(&ref->peer_ref->old_oid, &ref->old_oid))
6bc91f23
NTND
609 return;
610
611 max = term_columns();
612 rlen = utf8_strwidth(prettify_refname(ref->name));
bc437d10 613
6bc91f23
NTND
614 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
615
616 /*
617 * rough estimation to see if the output line is too long and
618 * should not be counted (we can't do precise calculation
619 * anyway because we don't know if the error explanation part
620 * will be printed in update_local_ref)
621 */
bc437d10
NTND
622 if (compact_format) {
623 llen = 0;
624 max = max * 2 / 3;
625 }
6bc91f23
NTND
626 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
627 if (len >= max)
628 return;
629
bc437d10
NTND
630 /*
631 * Not precise calculation for compact mode because '*' can
632 * appear on the left hand side of '->' and shrink the column
633 * back.
634 */
6bc91f23
NTND
635 if (refcol_width < rlen)
636 refcol_width = rlen;
637}
638
639static void prepare_format_display(struct ref *ref_map)
640{
641 struct ref *rm;
bc437d10
NTND
642 const char *format = "full";
643
644 git_config_get_string_const("fetch.output", &format);
645 if (!strcasecmp(format, "full"))
646 compact_format = 0;
647 else if (!strcasecmp(format, "compact"))
648 compact_format = 1;
649 else
650 die(_("configuration fetch.output contains invalid value %s"),
651 format);
6bc91f23
NTND
652
653 for (rm = ref_map; rm; rm = rm->next) {
654 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
655 !rm->peer_ref ||
656 !strcmp(rm->name, "HEAD"))
657 continue;
658
659 adjust_refcol_width(rm);
660 }
661}
165f3902 662
bc437d10
NTND
663static void print_remote_to_local(struct strbuf *display,
664 const char *remote, const char *local)
665{
666 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
667}
668
669static int find_and_replace(struct strbuf *haystack,
670 const char *needle,
671 const char *placeholder)
672{
dc40b24d 673 const char *p = NULL;
bc437d10
NTND
674 int plen, nlen;
675
dc40b24d
NTND
676 nlen = strlen(needle);
677 if (ends_with(haystack->buf, needle))
678 p = haystack->buf + haystack->len - nlen;
679 else
680 p = strstr(haystack->buf, needle);
bc437d10
NTND
681 if (!p)
682 return 0;
683
684 if (p > haystack->buf && p[-1] != '/')
685 return 0;
686
687 plen = strlen(p);
bc437d10
NTND
688 if (plen > nlen && p[nlen] != '/')
689 return 0;
690
691 strbuf_splice(haystack, p - haystack->buf, nlen,
692 placeholder, strlen(placeholder));
693 return 1;
694}
695
696static void print_compact(struct strbuf *display,
697 const char *remote, const char *local)
698{
699 struct strbuf r = STRBUF_INIT;
700 struct strbuf l = STRBUF_INIT;
701
702 if (!strcmp(remote, local)) {
703 strbuf_addf(display, "%-*s -> *", refcol_width, remote);
704 return;
705 }
706
707 strbuf_addstr(&r, remote);
708 strbuf_addstr(&l, local);
709
710 if (!find_and_replace(&r, local, "*"))
711 find_and_replace(&l, remote, "*");
712 print_remote_to_local(display, r.buf, l.buf);
713
714 strbuf_release(&r);
715 strbuf_release(&l);
716}
717
d0b39a03
NTND
718static void format_display(struct strbuf *display, char code,
719 const char *summary, const char *error,
901f3d40
JH
720 const char *remote, const char *local,
721 int summary_width)
d0b39a03 722{
901f3d40
JH
723 int width = (summary_width + strlen(summary) - gettext_width(summary));
724
725 strbuf_addf(display, "%c %-*s ", code, width, summary);
bc437d10
NTND
726 if (!compact_format)
727 print_remote_to_local(display, remote, local);
728 else
729 print_compact(display, remote, local);
d0b39a03
NTND
730 if (error)
731 strbuf_addf(display, " (%s)", error);
732}
165f3902 733
b888d61c 734static int update_local_ref(struct ref *ref,
165f3902 735 const char *remote,
6da618d5 736 const struct ref *remote_ref,
901f3d40
JH
737 struct strbuf *display,
738 int summary_width)
b888d61c 739{
b888d61c
DB
740 struct commit *current = NULL, *updated;
741 enum object_type type;
742 struct branch *current_branch = branch_get(NULL);
4577e483 743 const char *pretty_ref = prettify_refname(ref->name);
377444b4 744 int fast_forward = 0;
b888d61c 745
0df8e965 746 type = oid_object_info(the_repository, &ref->new_oid, NULL);
b888d61c 747 if (type < 0)
f4e54d02 748 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
b888d61c 749
4a7e27e9 750 if (oideq(&ref->old_oid, &ref->new_oid)) {
7f87aff2 751 if (verbosity > 0)
d0b39a03 752 format_display(display, '=', _("[up to date]"), NULL,
901f3d40 753 remote, pretty_ref, summary_width);
b888d61c
DB
754 return 0;
755 }
756
b3abdd9d
SP
757 if (current_branch &&
758 !strcmp(ref->name, current_branch->name) &&
b888d61c 759 !(update_head_ok || is_bare_repository()) &&
f4e54d02 760 !is_null_oid(&ref->old_oid)) {
b888d61c
DB
761 /*
762 * If this is the head, and it's not okay to update
763 * the head, and the old value of the head isn't empty...
764 */
d0b39a03
NTND
765 format_display(display, '!', _("[rejected]"),
766 _("can't fetch in current branch"),
901f3d40 767 remote, pretty_ref, summary_width);
b888d61c
DB
768 return 1;
769 }
770
f4e54d02 771 if (!is_null_oid(&ref->old_oid) &&
59556548 772 starts_with(ref->name, "refs/tags/")) {
0bc8d71b
ÆAB
773 if (force || ref->force) {
774 int r;
775 r = s_update_ref("updating tag", ref, 0);
776 format_display(display, r ? '!' : 't', _("[tag update]"),
777 r ? _("unable to update local ref") : NULL,
778 remote, pretty_ref, summary_width);
779 return r;
780 } else {
781 format_display(display, '!', _("[rejected]"), _("would clobber existing tag"),
782 remote, pretty_ref, summary_width);
783 return 1;
784 }
b888d61c
DB
785 }
786
21e1ee8f
SB
787 current = lookup_commit_reference_gently(the_repository,
788 &ref->old_oid, 1);
789 updated = lookup_commit_reference_gently(the_repository,
790 &ref->new_oid, 1);
b888d61c 791 if (!current || !updated) {
165f3902
NP
792 const char *msg;
793 const char *what;
6315472e 794 int r;
0997adaa
MB
795 /*
796 * Nicely describe the new ref we're fetching.
797 * Base this on the remote's ref name, as it's
798 * more likely to follow a standard layout.
799 */
800 const char *name = remote_ref ? remote_ref->name : "";
59556548 801 if (starts_with(name, "refs/tags/")) {
b888d61c 802 msg = "storing tag";
f7b3742a 803 what = _("[new tag]");
59556548 804 } else if (starts_with(name, "refs/heads/")) {
b888d61c 805 msg = "storing head";
f7b3742a 806 what = _("[new branch]");
0997adaa
MB
807 } else {
808 msg = "storing ref";
809 what = _("[new ref]");
165f3902
NP
810 }
811
6315472e 812 r = s_update_ref(msg, ref, 0);
d0b39a03
NTND
813 format_display(display, r ? '!' : '*', what,
814 r ? _("unable to update local ref") : NULL,
901f3d40 815 remote, pretty_ref, summary_width);
6315472e 816 return r;
b888d61c
DB
817 }
818
377444b4
DS
819 if (fetch_show_forced_updates) {
820 uint64_t t_before = getnanotime();
821 fast_forward = in_merge_bases(current, updated);
822 forced_updates_ms += (getnanotime() - t_before) / 1000000;
823 } else {
824 fast_forward = 1;
825 }
826
827 if (fast_forward) {
bd22d4ff 828 struct strbuf quickref = STRBUF_INIT;
6315472e 829 int r;
cdbd70c4 830
30e677e0 831 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
bd22d4ff 832 strbuf_addstr(&quickref, "..");
30e677e0 833 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
a75d7b54 834 r = s_update_ref("fast-forward", ref, 1);
d0b39a03
NTND
835 format_display(display, r ? '!' : ' ', quickref.buf,
836 r ? _("unable to update local ref") : NULL,
901f3d40 837 remote, pretty_ref, summary_width);
bd22d4ff 838 strbuf_release(&quickref);
6315472e 839 return r;
165f3902 840 } else if (force || ref->force) {
bd22d4ff 841 struct strbuf quickref = STRBUF_INIT;
6315472e 842 int r;
30e677e0 843 strbuf_add_unique_abbrev(&quickref, &current->object.oid, DEFAULT_ABBREV);
bd22d4ff 844 strbuf_addstr(&quickref, "...");
30e677e0 845 strbuf_add_unique_abbrev(&quickref, &ref->new_oid, DEFAULT_ABBREV);
6315472e 846 r = s_update_ref("forced-update", ref, 1);
d0b39a03
NTND
847 format_display(display, r ? '!' : '+', quickref.buf,
848 r ? _("unable to update local ref") : _("forced update"),
901f3d40 849 remote, pretty_ref, summary_width);
bd22d4ff 850 strbuf_release(&quickref);
6315472e 851 return r;
165f3902 852 } else {
d0b39a03 853 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
901f3d40 854 remote, pretty_ref, summary_width);
b888d61c
DB
855 return 1;
856 }
b888d61c
DB
857}
858
6ccac9ee 859static int iterate_ref_map(void *cb_data, struct object_id *oid)
6b67e0dc 860{
f0e278b1
JH
861 struct ref **rm = cb_data;
862 struct ref *ref = *rm;
6b67e0dc 863
4820a33b
NTND
864 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
865 ref = ref->next;
f0e278b1
JH
866 if (!ref)
867 return -1; /* end of the list */
868 *rm = ref->next;
6ccac9ee 869 oidcpy(oid, &ref->old_oid);
f0e278b1 870 return 0;
6b67e0dc
JH
871}
872
182f59da
JNA
873static const char warn_show_forced_updates[] =
874N_("Fetch normally indicates which branches had a forced update,\n"
875 "but that check has been disabled. To re-enable, use '--show-forced-updates'\n"
876 "flag or run 'git config fetch.showForcedUpdates true'.");
877static const char warn_time_show_forced_updates[] =
878N_("It took %.2f seconds to check forced updates. You can use\n"
879 "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
880 " to avoid this check.\n");
881
47abd85b 882static int store_updated_refs(const char *raw_url, const char *remote_name,
cf1e7c07 883 int connectivity_checked, struct ref *ref_map)
b888d61c
DB
884{
885 FILE *fp;
886 struct commit *commit;
4b3b33a7 887 int url_len, i, rc = 0;
5914f2d0 888 struct strbuf note = STRBUF_INIT;
b888d61c
DB
889 const char *what, *kind;
890 struct ref *rm;
dcf69262 891 char *url;
102de880 892 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head(the_repository);
900f2814 893 int want_status;
11fd66de 894 int summary_width = transport_summary_width(ref_map);
b888d61c 895
d6617c7c
AGR
896 fp = fopen(filename, "a");
897 if (!fp)
6da31d7f 898 return error_errno(_("cannot open %s"), filename);
47abd85b 899
fb0cc87e
DB
900 if (raw_url)
901 url = transport_anonymize_url(raw_url);
902 else
903 url = xstrdup("foreign");
6b67e0dc 904
cf1e7c07
JT
905 if (!connectivity_checked) {
906 rm = ref_map;
907 if (check_connected(iterate_ref_map, &rm, NULL)) {
908 rc = error(_("%s did not send all necessary objects\n"), url);
909 goto abort;
910 }
9516a598 911 }
6b67e0dc 912
6bc91f23
NTND
913 prepare_format_display(ref_map);
914
96890f4c 915 /*
900f2814
JK
916 * We do a pass for each fetch_head_status type in their enum order, so
917 * merged entries are written before not-for-merge. That lets readers
918 * use FETCH_HEAD as a refname to refer to the ref to be merged.
96890f4c 919 */
900f2814
JK
920 for (want_status = FETCH_HEAD_MERGE;
921 want_status <= FETCH_HEAD_IGNORE;
922 want_status++) {
96890f4c
JH
923 for (rm = ref_map; rm; rm = rm->next) {
924 struct ref *ref = NULL;
900f2814 925 const char *merge_status_marker = "";
96890f4c 926
4820a33b
NTND
927 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
928 if (want_status == FETCH_HEAD_MERGE)
929 warning(_("reject %s because shallow roots are not allowed to be updated"),
930 rm->peer_ref ? rm->peer_ref->name : rm->name);
931 continue;
932 }
933
21e1ee8f
SB
934 commit = lookup_commit_reference_gently(the_repository,
935 &rm->old_oid,
bc83266a 936 1);
96890f4c 937 if (!commit)
900f2814 938 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
96890f4c 939
900f2814 940 if (rm->fetch_head_status != want_status)
96890f4c
JH
941 continue;
942
943 if (rm->peer_ref) {
6f687c21 944 ref = alloc_ref(rm->peer_ref->name);
f4e54d02 945 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
946 oidcpy(&ref->new_oid, &rm->old_oid);
96890f4c
JH
947 ref->force = rm->peer_ref->force;
948 }
b888d61c 949
be76c212
SB
950 if (recurse_submodules != RECURSE_SUBMODULES_OFF)
951 check_for_new_submodule_commits(&rm->old_oid);
b888d61c 952
96890f4c
JH
953 if (!strcmp(rm->name, "HEAD")) {
954 kind = "";
955 what = "";
956 }
59556548 957 else if (starts_with(rm->name, "refs/heads/")) {
96890f4c
JH
958 kind = "branch";
959 what = rm->name + 11;
960 }
59556548 961 else if (starts_with(rm->name, "refs/tags/")) {
96890f4c
JH
962 kind = "tag";
963 what = rm->name + 10;
964 }
59556548 965 else if (starts_with(rm->name, "refs/remotes/")) {
96890f4c
JH
966 kind = "remote-tracking branch";
967 what = rm->name + 13;
968 }
969 else {
970 kind = "";
971 what = rm->name;
972 }
b888d61c 973
96890f4c
JH
974 url_len = strlen(url);
975 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
976 ;
977 url_len = i + 1;
978 if (4 < i && !strncmp(".git", url + i - 3, 4))
979 url_len = i - 3;
980
981 strbuf_reset(&note);
982 if (*what) {
983 if (*kind)
984 strbuf_addf(&note, "%s ", kind);
985 strbuf_addf(&note, "'%s' of ", what);
986 }
900f2814
JK
987 switch (rm->fetch_head_status) {
988 case FETCH_HEAD_NOT_FOR_MERGE:
989 merge_status_marker = "not-for-merge";
990 /* fall-through */
991 case FETCH_HEAD_MERGE:
992 fprintf(fp, "%s\t%s\t%s",
f4e54d02 993 oid_to_hex(&rm->old_oid),
900f2814
JK
994 merge_status_marker,
995 note.buf);
996 for (i = 0; i < url_len; ++i)
997 if ('\n' == url[i])
998 fputs("\\n", fp);
999 else
1000 fputc(url[i], fp);
1001 fputc('\n', fp);
1002 break;
1003 default:
1004 /* do not write anything to FETCH_HEAD */
1005 break;
1006 }
96890f4c
JH
1007
1008 strbuf_reset(&note);
1009 if (ref) {
901f3d40
JH
1010 rc |= update_local_ref(ref, what, rm, &note,
1011 summary_width);
96890f4c
JH
1012 free(ref);
1013 } else
d0b39a03
NTND
1014 format_display(&note, '*',
1015 *kind ? kind : "branch", NULL,
1016 *what ? what : "HEAD",
901f3d40 1017 "FETCH_HEAD", summary_width);
96890f4c
JH
1018 if (note.len) {
1019 if (verbosity >= 0 && !shown_url) {
1020 fprintf(stderr, _("From %.*s\n"),
1021 url_len, url);
1022 shown_url = 1;
1023 }
1024 if (verbosity >= 0)
1025 fprintf(stderr, " %s\n", note.buf);
165f3902
NP
1026 }
1027 }
b888d61c 1028 }
9516a598 1029
fa250759 1030 if (rc & STORE_REF_ERROR_DF_CONFLICT)
bd4a51fc 1031 error(_("some local refs could not be updated; try running\n"
f3cb169b 1032 " 'git remote prune %s' to remove any old, conflicting "
bd4a51fc 1033 "branches"), remote_name);
9516a598 1034
377444b4
DS
1035 if (advice_fetch_show_forced_updates) {
1036 if (!fetch_show_forced_updates) {
182f59da 1037 warning(_(warn_show_forced_updates));
377444b4 1038 } else if (forced_updates_ms > FORCED_UPDATES_DELAY_WARNING_IN_MS) {
182f59da 1039 warning(_(warn_time_show_forced_updates),
377444b4 1040 forced_updates_ms / 1000.0);
377444b4
DS
1041 }
1042 }
1043
9516a598 1044 abort:
5914f2d0 1045 strbuf_release(&note);
9516a598
TRC
1046 free(url);
1047 fclose(fp);
efb98b44 1048 return rc;
b888d61c
DB
1049}
1050
4191c356
SP
1051/*
1052 * We would want to bypass the object transfer altogether if
d9eb0205 1053 * everything we are going to fetch already exists and is connected
4191c356 1054 * locally.
4191c356 1055 */
35f9e3e5 1056static int check_exist_and_connected(struct ref *ref_map)
4191c356 1057{
f0e278b1 1058 struct ref *rm = ref_map;
7043c707 1059 struct check_connected_options opt = CHECK_CONNECTED_INIT;
35f9e3e5 1060 struct ref *r;
f0e278b1 1061
4191c356
SP
1062 /*
1063 * If we are deepening a shallow clone we already have these
1064 * objects reachable. Running rev-list here will return with
1065 * a good (0) exit status and we'll bypass the fetch that we
1066 * really need to perform. Claiming failure now will ensure
1067 * we perform the network exchange to deepen our history.
1068 */
508ea882 1069 if (deepen)
4191c356 1070 return -1;
35f9e3e5
JT
1071
1072 /*
1073 * check_connected() allows objects to merely be promised, but
1074 * we need all direct targets to exist.
1075 */
1076 for (r = rm; r; r = r->next) {
1077 if (!has_object_file(&r->old_oid))
1078 return -1;
1079 }
1080
7043c707
JK
1081 opt.quiet = 1;
1082 return check_connected(iterate_ref_map, &rm, &opt);
4191c356
SP
1083}
1084
e2842b39 1085static int fetch_refs(struct transport *transport, struct ref *ref_map)
b888d61c 1086{
35f9e3e5 1087 int ret = check_exist_and_connected(ref_map);
5fc31180
JS
1088 if (ret) {
1089 trace2_region_enter("fetch", "fetch_refs", the_repository);
e2842b39 1090 ret = transport_fetch_refs(transport, ref_map);
5fc31180
JS
1091 trace2_region_leave("fetch", "fetch_refs", the_repository);
1092 }
b888d61c 1093 if (!ret)
05c44226
BW
1094 /*
1095 * Keep the new pack's ".keep" file around to allow the caller
1096 * time to update refs to reference the new objects.
1097 */
1098 return 0;
1099 transport_unlock_pack(transport);
1100 return ret;
1101}
1102
1103/* Update local refs based on the ref values fetched from a remote */
1104static int consume_refs(struct transport *transport, struct ref *ref_map)
1105{
cf1e7c07
JT
1106 int connectivity_checked = transport->smart_options
1107 ? transport->smart_options->connectivity_checked : 0;
5fc31180
JS
1108 int ret;
1109 trace2_region_enter("fetch", "consume_refs", the_repository);
1110 ret = store_updated_refs(transport->url,
1111 transport->remote->name,
1112 connectivity_checked,
1113 ref_map);
1788c39c 1114 transport_unlock_pack(transport);
5fc31180 1115 trace2_region_leave("fetch", "consume_refs", the_repository);
b888d61c
DB
1116 return ret;
1117}
1118
def11e71
BW
1119static int prune_refs(struct refspec *rs, struct ref *ref_map,
1120 const char *raw_url)
f360d844 1121{
4b3b33a7 1122 int url_len, i, result = 0;
a2ac50cb 1123 struct ref *ref, *stale_refs = get_stale_heads(rs, ref_map);
4b3b33a7 1124 char *url;
11fd66de 1125 int summary_width = transport_summary_width(stale_refs);
f360d844 1126 const char *dangling_msg = dry_run
18986d53
NTND
1127 ? _(" (%s will become dangling)")
1128 : _(" (%s has become dangling)");
f360d844 1129
4b3b33a7
TM
1130 if (raw_url)
1131 url = transport_anonymize_url(raw_url);
1132 else
1133 url = xstrdup("foreign");
1134
1135 url_len = strlen(url);
1136 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
1137 ;
1138
1139 url_len = i + 1;
1140 if (4 < i && !strncmp(".git", url + i - 3, 4))
1141 url_len = i - 3;
1142
a087b432
MH
1143 if (!dry_run) {
1144 struct string_list refnames = STRING_LIST_INIT_NODUP;
1145
1146 for (ref = stale_refs; ref; ref = ref->next)
1147 string_list_append(&refnames, ref->name);
1148
64da4199 1149 result = delete_refs("fetch: prune", &refnames, 0);
a087b432
MH
1150 string_list_clear(&refnames, 0);
1151 }
1152
1153 if (verbosity >= 0) {
1154 for (ref = stale_refs; ref; ref = ref->next) {
d0b39a03 1155 struct strbuf sb = STRBUF_INIT;
a087b432
MH
1156 if (!shown_url) {
1157 fprintf(stderr, _("From %.*s\n"), url_len, url);
1158 shown_url = 1;
1159 }
2cb040ba 1160 format_display(&sb, '-', _("[deleted]"), NULL,
901f3d40
JH
1161 _("(none)"), prettify_refname(ref->name),
1162 summary_width);
d0b39a03
NTND
1163 fprintf(stderr, " %s\n",sb.buf);
1164 strbuf_release(&sb);
f360d844
JS
1165 warn_dangling_symref(stderr, dangling_msg, ref->name);
1166 }
1167 }
a087b432 1168
4b3b33a7 1169 free(url);
f360d844
JS
1170 free_refs(stale_refs);
1171 return result;
1172}
1173
8ee5d731
JS
1174static void check_not_current_branch(struct ref *ref_map)
1175{
1176 struct branch *current_branch = branch_get(NULL);
1177
1178 if (is_bare_repository() || !current_branch)
1179 return;
1180
1181 for (; ref_map; ref_map = ref_map->next)
1182 if (ref_map->peer_ref && !strcmp(current_branch->refname,
1183 ref_map->peer_ref->name))
bd4a51fc
ÆAB
1184 die(_("Refusing to fetch into current branch %s "
1185 "of non-bare repository"), current_branch->refname);
8ee5d731
JS
1186}
1187
e6cc5104
JH
1188static int truncate_fetch_head(void)
1189{
102de880 1190 const char *filename = git_path_fetch_head(the_repository);
ea56518d 1191 FILE *fp = fopen_for_writing(filename);
e6cc5104
JH
1192
1193 if (!fp)
6da31d7f 1194 return error_errno(_("cannot open %s"), filename);
e6cc5104
JH
1195 fclose(fp);
1196 return 0;
1197}
1198
db5723c6
JH
1199static void set_option(struct transport *transport, const char *name, const char *value)
1200{
1201 int r = transport_set_option(transport, name, value);
1202 if (r < 0)
1203 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1204 name, value, transport->url);
1205 if (r > 0)
1206 warning(_("Option \"%s\" is ignored for %s\n"),
1207 name, transport->url);
1208}
1209
3390e42a
JT
1210
1211static int add_oid(const char *refname, const struct object_id *oid, int flags,
1212 void *cb_data)
1213{
1214 struct oid_array *oids = cb_data;
1215
1216 oid_array_append(oids, oid);
1217 return 0;
1218}
1219
1220static void add_negotiation_tips(struct git_transport_options *smart_options)
1221{
1222 struct oid_array *oids = xcalloc(1, sizeof(*oids));
1223 int i;
1224
1225 for (i = 0; i < negotiation_tip.nr; i++) {
1226 const char *s = negotiation_tip.items[i].string;
1227 int old_nr;
1228 if (!has_glob_specials(s)) {
1229 struct object_id oid;
1230 if (get_oid(s, &oid))
1231 die("%s is not a valid object", s);
1232 oid_array_append(oids, &oid);
1233 continue;
1234 }
1235 old_nr = oids->nr;
1236 for_each_glob_ref(add_oid, s, oids);
1237 if (old_nr == oids->nr)
1238 warning("Ignoring --negotiation-tip=%s because it does not match any refs",
1239 s);
1240 }
1241 smart_options->negotiation_tips = oids;
1242}
1243
508ea882 1244static struct transport *prepare_transport(struct remote *remote, int deepen)
db5723c6
JH
1245{
1246 struct transport *transport;
87c2d9d3 1247
db5723c6
JH
1248 transport = transport_get(remote, NULL);
1249 transport_set_verbosity(transport, verbosity, progress);
c915f11e 1250 transport->family = family;
db5723c6
JH
1251 if (upload_pack)
1252 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
1253 if (keep)
1254 set_option(transport, TRANS_OPT_KEEP, "yes");
1255 if (depth)
1256 set_option(transport, TRANS_OPT_DEPTH, depth);
508ea882
NTND
1257 if (deepen && deepen_since)
1258 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
a45a2600
NTND
1259 if (deepen && deepen_not.nr)
1260 set_option(transport, TRANS_OPT_DEEPEN_NOT,
1261 (const char *)&deepen_not);
cccf74e2
NTND
1262 if (deepen_relative)
1263 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
48d25cae
NTND
1264 if (update_shallow)
1265 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
acb0c572 1266 if (filter_options.choice) {
cf9ceb5a
MD
1267 const char *spec =
1268 expand_list_objects_filter_spec(&filter_options);
1269 set_option(transport, TRANS_OPT_LIST_OBJECTS_FILTER, spec);
acb0c572
JH
1270 set_option(transport, TRANS_OPT_FROM_PROMISOR, "1");
1271 }
3390e42a
JT
1272 if (negotiation_tip.nr) {
1273 if (transport->smart_options)
1274 add_negotiation_tips(transport->smart_options);
1275 else
1276 warning("Ignoring --negotiation-tip because the protocol does not support it.");
1277 }
db5723c6
JH
1278 return transport;
1279}
1280
069d5032
JH
1281static void backfill_tags(struct transport *transport, struct ref *ref_map)
1282{
508ea882
NTND
1283 int cannot_reuse;
1284
1285 /*
1286 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1287 * when remote helper is used (setting it to an empty string
1288 * is not unsetting). We could extend the remote helper
1289 * protocol for that, but for now, just force a new connection
a45a2600 1290 * without deepen-since. Similar story for deepen-not.
508ea882 1291 */
a45a2600
NTND
1292 cannot_reuse = transport->cannot_reuse ||
1293 deepen_since || deepen_not.nr;
508ea882
NTND
1294 if (cannot_reuse) {
1295 gsecondary = prepare_transport(transport->remote, 0);
b26ed430
JH
1296 transport = gsecondary;
1297 }
1298
069d5032
JH
1299 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
1300 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
cccf74e2 1301 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
e2842b39 1302 if (!fetch_refs(transport, ref_map))
05c44226 1303 consume_refs(transport, ref_map);
b26ed430
JH
1304
1305 if (gsecondary) {
1306 transport_disconnect(gsecondary);
1307 gsecondary = NULL;
1308 }
069d5032
JH
1309}
1310
b888d61c 1311static int do_fetch(struct transport *transport,
65a1301f 1312 struct refspec *rs)
b888d61c 1313{
7f98428d 1314 struct ref *ref_map;
b888d61c 1315 int autotags = (transport->remote->fetch_tags == 1);
5b87d8d3 1316 int retcode = 0;
6d1700d5
BW
1317 const struct ref *remote_refs;
1318 struct argv_array ref_prefixes = ARGV_ARRAY_INIT;
e70a3030 1319 int must_list_refs = 1;
b1a01e1c 1320
ed368546
DJ
1321 if (tags == TAGS_DEFAULT) {
1322 if (transport->remote->fetch_tags == 2)
1323 tags = TAGS_SET;
1324 if (transport->remote->fetch_tags == -1)
1325 tags = TAGS_UNSET;
1326 }
b888d61c 1327
b888d61c 1328 /* if not appending, truncate FETCH_HEAD */
28a15401 1329 if (!append && !dry_run) {
5b87d8d3
MH
1330 retcode = truncate_fetch_head();
1331 if (retcode)
1332 goto cleanup;
d6617c7c 1333 }
b888d61c 1334
e70a3030
JT
1335 if (rs->nr) {
1336 int i;
1337
6d1700d5 1338 refspec_ref_prefixes(rs, &ref_prefixes);
e70a3030
JT
1339
1340 /*
1341 * We can avoid listing refs if all of them are exact
1342 * OIDs
1343 */
1344 must_list_refs = 0;
1345 for (i = 0; i < rs->nr; i++) {
1346 if (!rs->items[i].exact_sha1) {
1347 must_list_refs = 1;
1348 break;
1349 }
1350 }
1351 } else if (transport->remote && transport->remote->fetch.nr)
6d1700d5 1352 refspec_ref_prefixes(&transport->remote->fetch, &ref_prefixes);
b888d61c 1353
e70a3030
JT
1354 if (tags == TAGS_SET || tags == TAGS_DEFAULT) {
1355 must_list_refs = 1;
1356 if (ref_prefixes.argc)
1357 argv_array_push(&ref_prefixes, "refs/tags/");
b888d61c
DB
1358 }
1359
5fc31180
JS
1360 if (must_list_refs) {
1361 trace2_region_enter("fetch", "remote_refs", the_repository);
e70a3030 1362 remote_refs = transport_get_remote_refs(transport, &ref_prefixes);
5fc31180
JS
1363 trace2_region_leave("fetch", "remote_refs", the_repository);
1364 } else
e70a3030
JT
1365 remote_refs = NULL;
1366
6d1700d5
BW
1367 argv_array_clear(&ref_prefixes);
1368
1369 ref_map = get_ref_map(transport->remote, remote_refs, rs,
1370 tags, &autotags);
8ee5d731
JS
1371 if (!update_head_ok)
1372 check_not_current_branch(ref_map);
b888d61c 1373
41fa7d2e
SP
1374 if (tags == TAGS_DEFAULT && autotags)
1375 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
ed43de6e 1376 if (prune) {
0838bf47
MH
1377 /*
1378 * We only prune based on refspecs specified
1379 * explicitly (via command line or configuration); we
1380 * don't care whether --tags was specified.
1381 */
65a1301f 1382 if (rs->nr) {
def11e71 1383 prune_refs(rs, ref_map, transport->url);
e8c1e6c7 1384 } else {
def11e71 1385 prune_refs(&transport->remote->fetch,
4b3b33a7
TM
1386 ref_map,
1387 transport->url);
e8c1e6c7 1388 }
ed43de6e 1389 }
e2842b39 1390 if (fetch_refs(transport, ref_map) || consume_refs(transport, ref_map)) {
10a6cc88
TM
1391 free_refs(ref_map);
1392 retcode = 1;
1393 goto cleanup;
1394 }
24bc1a12
CB
1395
1396 if (set_upstream) {
1397 struct branch *branch = branch_get("HEAD");
1398 struct ref *rm;
1399 struct ref *source_ref = NULL;
1400
1401 /*
1402 * We're setting the upstream configuration for the
1403 * current branch. The relevent upstream is the
1404 * fetched branch that is meant to be merged with the
1405 * current one, i.e. the one fetched to FETCH_HEAD.
1406 *
1407 * When there are several such branches, consider the
1408 * request ambiguous and err on the safe side by doing
1409 * nothing and just emit a warning.
1410 */
1411 for (rm = ref_map; rm; rm = rm->next) {
1412 if (!rm->peer_ref) {
1413 if (source_ref) {
1414 warning(_("multiple branch detected, incompatible with --set-upstream"));
1415 goto skip;
1416 } else {
1417 source_ref = rm;
1418 }
1419 }
1420 }
1421 if (source_ref) {
1422 if (!strcmp(source_ref->name, "HEAD") ||
1423 starts_with(source_ref->name, "refs/heads/"))
1424 install_branch_config(0,
1425 branch->name,
1426 transport->remote->name,
1427 source_ref->name);
1428 else if (starts_with(source_ref->name, "refs/remotes/"))
1429 warning(_("not setting upstream for a remote remote-tracking branch"));
1430 else if (starts_with(source_ref->name, "refs/tags/"))
1431 warning(_("not setting upstream for a remote tag"));
1432 else
1433 warning(_("unknown branch type"));
1434 } else {
1435 warning(_("no source branch found.\n"
1436 "you need to specify exactly one branch with the --set-upstream option."));
1437 }
1438 }
1439 skip:
7f98428d 1440 free_refs(ref_map);
b888d61c
DB
1441
1442 /* if neither --no-tags nor --tags was specified, do automated tag
1443 * following ... */
83201998 1444 if (tags == TAGS_DEFAULT && autotags) {
c50b2b47
SP
1445 struct ref **tail = &ref_map;
1446 ref_map = NULL;
6d1700d5 1447 find_non_local_tags(remote_refs, &ref_map, &tail);
069d5032
JH
1448 if (ref_map)
1449 backfill_tags(transport, ref_map);
b888d61c
DB
1450 free_refs(ref_map);
1451 }
1452
5b87d8d3 1453 cleanup:
5b87d8d3 1454 return retcode;
b888d61c
DB
1455}
1456
9c4a036b
BG
1457static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1458{
1459 struct string_list *list = priv;
7cc91a2f 1460 if (!remote->skip_default_update)
1d2f80fa 1461 string_list_append(list, remote->name);
9c4a036b
BG
1462 return 0;
1463}
1464
1465struct remote_group_data {
1466 const char *name;
1467 struct string_list *list;
1468};
1469
1470static int get_remote_group(const char *key, const char *value, void *priv)
1471{
1472 struct remote_group_data *g = priv;
1473
bc598c32 1474 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
9c4a036b 1475 /* split list by white space */
9c4a036b 1476 while (*value) {
5f65499f
MH
1477 size_t wordlen = strcspn(value, " \t\n");
1478
e286542d 1479 if (wordlen >= 1)
b7410f61 1480 string_list_append_nodup(g->list,
e286542d
MH
1481 xstrndup(value, wordlen));
1482 value += wordlen + (value[wordlen] != '\0');
9c4a036b
BG
1483 }
1484 }
1485
1486 return 0;
1487}
1488
1489static int add_remote_or_group(const char *name, struct string_list *list)
1490{
1491 int prev_nr = list->nr;
66dbfd55
GV
1492 struct remote_group_data g;
1493 g.name = name; g.list = list;
9c4a036b
BG
1494
1495 git_config(get_remote_group, &g);
1496 if (list->nr == prev_nr) {
674468b3 1497 struct remote *remote = remote_get(name);
e459b073 1498 if (!remote_is_configured(remote, 0))
9c4a036b 1499 return 0;
1d2f80fa 1500 string_list_append(list, remote->name);
9c4a036b
BG
1501 }
1502 return 1;
1503}
1504
85556d4e 1505static void add_options_to_argv(struct argv_array *argv)
9c4a036b 1506{
28a15401 1507 if (dry_run)
85556d4e 1508 argv_array_push(argv, "--dry-run");
90765fa3
MH
1509 if (prune != -1)
1510 argv_array_push(argv, prune ? "--prune" : "--no-prune");
97716d21
ÆAB
1511 if (prune_tags != -1)
1512 argv_array_push(argv, prune_tags ? "--prune-tags" : "--no-prune-tags");
bba5322a 1513 if (update_head_ok)
85556d4e 1514 argv_array_push(argv, "--update-head-ok");
bba5322a 1515 if (force)
85556d4e 1516 argv_array_push(argv, "--force");
bba5322a 1517 if (keep)
85556d4e 1518 argv_array_push(argv, "--keep");
be254a0e 1519 if (recurse_submodules == RECURSE_SUBMODULES_ON)
85556d4e 1520 argv_array_push(argv, "--recurse-submodules");
8f0700dd 1521 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
85556d4e 1522 argv_array_push(argv, "--recurse-submodules=on-demand");
85566460
DJ
1523 if (tags == TAGS_SET)
1524 argv_array_push(argv, "--tags");
1525 else if (tags == TAGS_UNSET)
1526 argv_array_push(argv, "--no-tags");
9c4a036b 1527 if (verbosity >= 2)
85556d4e 1528 argv_array_push(argv, "-v");
9c4a036b 1529 if (verbosity >= 1)
85556d4e 1530 argv_array_push(argv, "-v");
9c4a036b 1531 else if (verbosity < 0)
85556d4e 1532 argv_array_push(argv, "-q");
7dce19d3
JL
1533
1534}
1535
d54dea77
JS
1536/* Fetch multiple remotes in parallel */
1537
1538struct parallel_fetch_state {
1539 const char **argv;
1540 struct string_list *remotes;
1541 int next, result;
1542};
1543
1544static int fetch_next_remote(struct child_process *cp, struct strbuf *out,
1545 void *cb, void **task_cb)
1546{
1547 struct parallel_fetch_state *state = cb;
1548 char *remote;
1549
1550 if (state->next < 0 || state->next >= state->remotes->nr)
1551 return 0;
1552
1553 remote = state->remotes->items[state->next++].string;
1554 *task_cb = remote;
1555
1556 argv_array_pushv(&cp->args, state->argv);
1557 argv_array_push(&cp->args, remote);
1558 cp->git_cmd = 1;
1559
1560 if (verbosity >= 0)
1561 printf(_("Fetching %s\n"), remote);
1562
1563 return 1;
1564}
1565
1566static int fetch_failed_to_start(struct strbuf *out, void *cb, void *task_cb)
1567{
1568 struct parallel_fetch_state *state = cb;
1569 const char *remote = task_cb;
1570
1571 state->result = error(_("Could not fetch %s"), remote);
1572
1573 return 0;
1574}
1575
1576static int fetch_finished(int result, struct strbuf *out,
1577 void *cb, void *task_cb)
1578{
1579 struct parallel_fetch_state *state = cb;
1580 const char *remote = task_cb;
1581
1582 if (result) {
1583 strbuf_addf(out, _("could not fetch '%s' (exit code: %d)\n"),
1584 remote, result);
1585 state->result = -1;
1586 }
1587
1588 return 0;
1589}
1590
1591static int fetch_multiple(struct string_list *list, int max_children)
7dce19d3
JL
1592{
1593 int i, result = 0;
85556d4e 1594 struct argv_array argv = ARGV_ARRAY_INIT;
9c4a036b 1595
e6cc5104
JH
1596 if (!append && !dry_run) {
1597 int errcode = truncate_fetch_head();
1598 if (errcode)
1599 return errcode;
1600 }
1601
c3d6b703 1602 argv_array_pushl(&argv, "fetch", "--append", "--no-auto-gc", NULL);
85556d4e
JK
1603 add_options_to_argv(&argv);
1604
d54dea77
JS
1605 if (max_children != 1 && list->nr != 1) {
1606 struct parallel_fetch_state state = { argv.argv, list, 0, 0 };
1607
1608 argv_array_push(&argv, "--end-of-options");
1609 result = run_processes_parallel_tr2(max_children,
1610 &fetch_next_remote,
1611 &fetch_failed_to_start,
1612 &fetch_finished,
1613 &state,
1614 "fetch", "parallel/fetch");
1615
1616 if (!result)
1617 result = state.result;
1618 } else
1619 for (i = 0; i < list->nr; i++) {
1620 const char *name = list->items[i].string;
1621 argv_array_push(&argv, name);
1622 if (verbosity >= 0)
1623 printf(_("Fetching %s\n"), name);
1624 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1625 error(_("Could not fetch %s"), name);
1626 result = 1;
1627 }
1628 argv_array_pop(&argv);
9c4a036b 1629 }
9c4a036b 1630
85556d4e 1631 argv_array_clear(&argv);
d54dea77 1632 return !!result;
9c4a036b
BG
1633}
1634
aa57b871
JH
1635/*
1636 * Fetching from the promisor remote should use the given filter-spec
1637 * or inherit the default filter-spec from the config.
1638 */
1639static inline void fetch_one_setup_partial(struct remote *remote)
1640{
1641 /*
1642 * Explicit --no-filter argument overrides everything, regardless
1643 * of any prior partial clones and fetches.
1644 */
1645 if (filter_options.no_filter)
1646 return;
1647
1648 /*
1649 * If no prior partial clone/fetch and the current fetch DID NOT
1650 * request a partial-fetch, do a normal fetch.
1651 */
b14ed5ad 1652 if (!has_promisor_remote() && !filter_options.choice)
aa57b871
JH
1653 return;
1654
1655 /*
5e461393
CC
1656 * If this is a partial-fetch request, we enable partial on
1657 * this repo if not already enabled and remember the given
1658 * filter-spec as the default for subsequent fetches to this
1659 * remote.
aa57b871 1660 */
5e461393 1661 if (filter_options.choice) {
aa57b871
JH
1662 partial_clone_register(remote->name, &filter_options);
1663 return;
1664 }
1665
aa57b871
JH
1666 /*
1667 * Do a partial-fetch from the promisor remote using either the
1668 * explicitly given filter-spec or inherit the filter-spec from
1669 * the config.
1670 */
1671 if (!filter_options.choice)
fa3d1b63 1672 partial_clone_get_default_filter_spec(&filter_options, remote->name);
aa57b871
JH
1673 return;
1674}
1675
97716d21 1676static int fetch_one(struct remote *remote, int argc, const char **argv, int prune_tags_ok)
b888d61c 1677{
d7c8e307
BW
1678 struct refspec rs = REFSPEC_INIT_FETCH;
1679 int i;
7b7f39ea 1680 int exit_code;
6317972c
ÆAB
1681 int maybe_prune_tags;
1682 int remote_via_config = remote_is_configured(remote, 0);
b888d61c 1683
fa685bdf 1684 if (!remote)
bd4a51fc
ÆAB
1685 die(_("No remote repository specified. Please, specify either a URL or a\n"
1686 "remote name from which new revisions should be fetched."));
fa685bdf 1687
508ea882 1688 gtransport = prepare_transport(remote, 1);
737c5a9c
MS
1689
1690 if (prune < 0) {
1691 /* no command line request */
07118832
ÆAB
1692 if (0 <= remote->prune)
1693 prune = remote->prune;
737c5a9c
MS
1694 else if (0 <= fetch_prune_config)
1695 prune = fetch_prune_config;
1696 else
1697 prune = PRUNE_BY_DEFAULT;
1698 }
1699
97716d21
ÆAB
1700 if (prune_tags < 0) {
1701 /* no command line request */
1702 if (0 <= remote->prune_tags)
1703 prune_tags = remote->prune_tags;
1704 else if (0 <= fetch_prune_tags_config)
1705 prune_tags = fetch_prune_tags_config;
1706 else
1707 prune_tags = PRUNE_TAGS_BY_DEFAULT;
1708 }
1709
6317972c
ÆAB
1710 maybe_prune_tags = prune_tags_ok && prune_tags;
1711 if (maybe_prune_tags && remote_via_config)
95303500 1712 refspec_append(&remote->fetch, TAG_REFSPEC);
97716d21 1713
d7c8e307
BW
1714 if (maybe_prune_tags && (argc || !remote_via_config))
1715 refspec_append(&rs, TAG_REFSPEC);
6317972c 1716
d7c8e307
BW
1717 for (i = 0; i < argc; i++) {
1718 if (!strcmp(argv[i], "tag")) {
1719 char *tag;
1720 i++;
1721 if (i >= argc)
1722 die(_("You need to specify a tag name."));
1723
1724 tag = xstrfmt("refs/tags/%s:refs/tags/%s",
1725 argv[i], argv[i]);
1726 refspec_append(&rs, tag);
1727 free(tag);
1728 } else {
1729 refspec_append(&rs, argv[i]);
b888d61c 1730 }
b888d61c
DB
1731 }
1732
5e3548ef
BW
1733 if (server_options.nr)
1734 gtransport->server_options = &server_options;
1735
57b235a4 1736 sigchain_push_common(unlock_pack_on_signal);
e4022ed2 1737 atexit(unlock_pack);
14358894 1738 sigchain_push(SIGPIPE, SIG_IGN);
65a1301f 1739 exit_code = do_fetch(gtransport, &rs);
14358894 1740 sigchain_pop(SIGPIPE);
d7c8e307 1741 refspec_clear(&rs);
af234459
JH
1742 transport_disconnect(gtransport);
1743 gtransport = NULL;
7b7f39ea 1744 return exit_code;
b888d61c 1745}
9c4a036b
BG
1746
1747int cmd_fetch(int argc, const char **argv, const char *prefix)
1748{
1749 int i;
b7410f61 1750 struct string_list list = STRING_LIST_INIT_DUP;
a1743343 1751 struct remote *remote = NULL;
9c4a036b 1752 int result = 0;
c1a7902f 1753 int prune_tags_ok = 1;
1991006c 1754 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
9c4a036b 1755
bbc30f99
JK
1756 packet_trace_identity("fetch");
1757
9c4a036b
BG
1758 /* Record the command line for the reflog */
1759 strbuf_addstr(&default_rla, "fetch");
1760 for (i = 1; i < argc; i++)
1761 strbuf_addf(&default_rla, " %s", argv[i]);
1762
d54dea77
JS
1763 fetch_config_from_gitmodules(&submodule_fetch_jobs_config,
1764 &recurse_submodules);
737c5a9c
MS
1765 git_config(git_fetch_config, NULL);
1766
9c4a036b
BG
1767 argc = parse_options(argc, argv, prefix,
1768 builtin_fetch_options, builtin_fetch_usage, 0);
1769
cccf74e2
NTND
1770 if (deepen_relative) {
1771 if (deepen_relative < 0)
1772 die(_("Negative depth in --deepen is not supported"));
1773 if (depth)
1774 die(_("--deepen and --depth are mutually exclusive"));
1775 depth = xstrfmt("%d", deepen_relative);
1776 }
4dcb167f
NTND
1777 if (unshallow) {
1778 if (depth)
1779 die(_("--depth and --unshallow cannot be used together"));
c8813487 1780 else if (!is_repository_shallow(the_repository))
4dcb167f 1781 die(_("--unshallow on a complete repository does not make sense"));
2805bb59
JK
1782 else
1783 depth = xstrfmt("%d", INFINITE_DEPTH);
4dcb167f
NTND
1784 }
1785
5594bcad
NTND
1786 /* no need to be strict, transport_set_option() will validate it again */
1787 if (depth && atoi(depth) < 1)
1788 die(_("depth %s is not a positive number"), depth);
a45a2600 1789 if (depth || deepen_since || deepen_not.nr)
508ea882 1790 deepen = 1;
5594bcad 1791
b14ed5ad 1792 if (filter_options.choice && !has_promisor_remote())
acb0c572
JH
1793 die("--filter can only be used when extensions.partialClone is set");
1794
9c4a036b
BG
1795 if (all) {
1796 if (argc == 1)
bd4a51fc 1797 die(_("fetch --all does not take a repository argument"));
9c4a036b 1798 else if (argc > 1)
bd4a51fc 1799 die(_("fetch --all does not make sense with refspecs"));
9c4a036b 1800 (void) for_each_remote(get_one_remote_for_fetch, &list);
9c4a036b
BG
1801 } else if (argc == 0) {
1802 /* No arguments -- use default remote */
1803 remote = remote_get(NULL);
16679e37
BG
1804 } else if (multiple) {
1805 /* All arguments are assumed to be remotes or groups */
1806 for (i = 0; i < argc; i++)
1807 if (!add_remote_or_group(argv[i], &list))
bd4a51fc 1808 die(_("No such remote or remote group: %s"), argv[i]);
9c4a036b
BG
1809 } else {
1810 /* Single remote or group */
1811 (void) add_remote_or_group(argv[0], &list);
1812 if (list.nr > 1) {
1813 /* More than one remote */
1814 if (argc > 1)
bd4a51fc 1815 die(_("Fetching a group and specifying refspecs does not make sense"));
9c4a036b
BG
1816 } else {
1817 /* Zero or one remotes */
1818 remote = remote_get(argv[0]);
c1a7902f 1819 prune_tags_ok = (argc == 1);
a1743343
JT
1820 argc--;
1821 argv++;
9c4a036b
BG
1822 }
1823 }
1824
c7aadccb
JT
1825 fetch_if_missing = 0;
1826
acb0c572 1827 if (remote) {
b14ed5ad 1828 if (filter_options.choice || has_promisor_remote())
aa57b871 1829 fetch_one_setup_partial(remote);
c1a7902f 1830 result = fetch_one(remote, argc, argv, prune_tags_ok);
acb0c572 1831 } else {
d54dea77
JS
1832 int max_children = max_jobs;
1833
acb0c572 1834 if (filter_options.choice)
e0137875
CC
1835 die(_("--filter can only be used with the remote "
1836 "configured in extensions.partialclone"));
d54dea77
JS
1837
1838 if (max_children < 0)
1839 max_children = fetch_parallel_config;
1840
aa57b871 1841 /* TODO should this also die if we have a previous partial-clone? */
d54dea77 1842 result = fetch_multiple(&list, max_children);
acb0c572 1843 }
a1743343 1844
be254a0e 1845 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
85556d4e 1846 struct argv_array options = ARGV_ARRAY_INIT;
d54dea77
JS
1847 int max_children = max_jobs;
1848
1849 if (max_children < 0)
1850 max_children = submodule_fetch_jobs_config;
1851 if (max_children < 0)
1852 max_children = fetch_parallel_config;
85556d4e
JK
1853
1854 add_options_to_argv(&options);
e724197f
BW
1855 result = fetch_populated_submodules(the_repository,
1856 &options,
7dce19d3 1857 submodule_prefix,
8f0700dd 1858 recurse_submodules,
8fa29159 1859 recurse_submodules_default,
62104ba1
SB
1860 verbosity < 0,
1861 max_children);
85556d4e 1862 argv_array_clear(&options);
7dce19d3
JL
1863 }
1864
9c4a036b
BG
1865 string_list_clear(&list, 0);
1866
50f26bd0
DS
1867 prepare_repo_settings(the_repository);
1868 if (the_repository->settings.fetch_write_commit_graph) {
5a535094 1869 int commit_graph_flags = COMMIT_GRAPH_WRITE_SPLIT;
50f26bd0
DS
1870 struct split_commit_graph_opts split_opts;
1871 memset(&split_opts, 0, sizeof(struct split_commit_graph_opts));
1872
1873 if (progress)
5a535094 1874 commit_graph_flags |= COMMIT_GRAPH_WRITE_PROGRESS;
50f26bd0
DS
1875
1876 write_commit_graph_reachable(get_object_directory(),
1877 commit_graph_flags,
1878 &split_opts);
1879 }
1880
2d511cfc 1881 close_object_store(the_repository->objects);
0898c962 1882
c3d6b703
NTND
1883 if (enable_auto_gc) {
1884 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1885 if (verbosity < 0)
1886 argv_array_push(&argv_gc_auto, "--quiet");
1887 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1888 argv_array_clear(&argv_gc_auto);
1889 }
131b8fcb 1890
9c4a036b
BG
1891 return result;
1892}