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