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