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