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