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