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