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