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