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