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