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