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