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