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