8 #include "string-list.h"
10 #include "transport.h"
11 #include "run-command.h"
12 #include "parse-options.h"
14 #include "submodule-config.h"
15 #include "submodule.h"
16 #include "connected.h"
17 #include "argv-array.h"
19 static const char * const builtin_fetch_usage[] = {
20 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
21 N_("git fetch [<options>] <group>"),
22 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
23 N_("git fetch --all [<options>]"),
33 static int fetch_prune_config = -1; /* unspecified */
34 static int prune = -1; /* unspecified */
35 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
37 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;
38 static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
39 static int tags = TAGS_DEFAULT, unshallow, update_shallow;
40 static int max_children = -1;
41 static enum transport_family family;
42 static const char *depth;
43 static const char *upload_pack;
44 static struct strbuf default_rla = STRBUF_INIT;
45 static struct transport *gtransport;
46 static struct transport *gsecondary;
47 static const char *submodule_prefix = "";
48 static const char *recurse_submodules_default;
49 static int shown_url = 0;
50 static int refmap_alloc, refmap_nr;
51 static const char **refmap_array;
53 static int option_parse_recurse_submodules(const struct option *opt,
54 const char *arg, int unset)
57 recurse_submodules = RECURSE_SUBMODULES_OFF;
60 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
62 recurse_submodules = RECURSE_SUBMODULES_ON;
67 static int git_fetch_config(const char *k, const char *v, void *cb)
69 if (!strcmp(k, "fetch.prune")) {
70 fetch_prune_config = git_config_bool(k, v);
73 return git_default_config(k, v, cb);
76 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
78 ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
81 * "git fetch --refmap='' origin foo"
82 * can be used to tell the command not to store anywhere
85 refmap_array[refmap_nr++] = arg;
89 static struct option builtin_fetch_options[] = {
90 OPT__VERBOSITY(&verbosity),
91 OPT_BOOL(0, "all", &all,
92 N_("fetch from all remotes")),
93 OPT_BOOL('a', "append", &append,
94 N_("append to .git/FETCH_HEAD instead of overwriting")),
95 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
96 N_("path to upload pack on remote end")),
97 OPT__FORCE(&force, N_("force overwrite of local branch")),
98 OPT_BOOL('m', "multiple", &multiple,
99 N_("fetch from multiple remotes")),
100 OPT_SET_INT('t', "tags", &tags,
101 N_("fetch all tags and associated objects"), TAGS_SET),
102 OPT_SET_INT('n', NULL, &tags,
103 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
104 OPT_INTEGER('j', "jobs", &max_children,
105 N_("number of submodules fetched in parallel")),
106 OPT_BOOL('p', "prune", &prune,
107 N_("prune remote-tracking branches no longer on remote")),
108 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
109 N_("control recursive fetching of submodules"),
110 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
111 OPT_BOOL(0, "dry-run", &dry_run,
113 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
114 OPT_BOOL('u', "update-head-ok", &update_head_ok,
115 N_("allow updating of HEAD ref")),
116 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
117 OPT_STRING(0, "depth", &depth, N_("depth"),
118 N_("deepen history of shallow clone")),
119 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
120 N_("convert to a complete repository"),
121 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
122 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
123 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
124 { OPTION_STRING, 0, "recurse-submodules-default",
125 &recurse_submodules_default, NULL,
126 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
127 OPT_BOOL(0, "update-shallow", &update_shallow,
128 N_("accept refs that update .git/shallow")),
129 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
130 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
131 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
132 TRANSPORT_FAMILY_IPV4),
133 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
134 TRANSPORT_FAMILY_IPV6),
138 static void unlock_pack(void)
141 transport_unlock_pack(gtransport);
143 transport_unlock_pack(gsecondary);
146 static void unlock_pack_on_signal(int signo)
153 static void add_merge_config(struct ref **head,
154 const struct ref *remote_refs,
155 struct branch *branch,
160 for (i = 0; i < branch->merge_nr; i++) {
161 struct ref *rm, **old_tail = *tail;
162 struct refspec refspec;
164 for (rm = *head; rm; rm = rm->next) {
165 if (branch_merge_matches(branch, i, rm->name)) {
166 rm->fetch_head_status = FETCH_HEAD_MERGE;
174 * Not fetched to a remote-tracking branch? We need to fetch
175 * it anyway to allow this branch's "branch.$name.merge"
176 * to be honored by 'git pull', but we do not have to
177 * fail if branch.$name.merge is misconfigured to point
178 * at a nonexisting branch. If we were indeed called by
179 * 'git pull', it will notice the misconfiguration because
180 * there is no entry in the resulting FETCH_HEAD marked
183 memset(&refspec, 0, sizeof(refspec));
184 refspec.src = branch->merge[i]->src;
185 get_fetch_map(remote_refs, &refspec, tail, 1);
186 for (rm = *old_tail; rm; rm = rm->next)
187 rm->fetch_head_status = FETCH_HEAD_MERGE;
191 static int add_existing(const char *refname, const struct object_id *oid,
192 int flag, void *cbdata)
194 struct string_list *list = (struct string_list *)cbdata;
195 struct string_list_item *item = string_list_insert(list, refname);
196 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
198 oidcpy(old_oid, oid);
199 item->util = old_oid;
203 static int will_fetch(struct ref **head, const unsigned char *sha1)
205 struct ref *rm = *head;
207 if (!hashcmp(rm->old_oid.hash, sha1))
214 static void find_non_local_tags(struct transport *transport,
218 struct string_list existing_refs = STRING_LIST_INIT_DUP;
219 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
220 const struct ref *ref;
221 struct string_list_item *item = NULL;
223 for_each_ref(add_existing, &existing_refs);
224 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
225 if (!starts_with(ref->name, "refs/tags/"))
229 * The peeled ref always follows the matching base
230 * ref, so if we see a peeled ref that we don't want
231 * to fetch then we can mark the ref entry in the list
232 * as one to ignore by setting util to NULL.
234 if (ends_with(ref->name, "^{}")) {
235 if (item && !has_object_file(&ref->old_oid) &&
236 !will_fetch(head, ref->old_oid.hash) &&
237 !has_sha1_file(item->util) &&
238 !will_fetch(head, item->util))
245 * If item is non-NULL here, then we previously saw a
246 * ref not followed by a peeled reference, so we need
247 * to check if it is a lightweight tag that we want to
250 if (item && !has_sha1_file(item->util) &&
251 !will_fetch(head, item->util))
256 /* skip duplicates and refs that we already have */
257 if (string_list_has_string(&remote_refs, ref->name) ||
258 string_list_has_string(&existing_refs, ref->name))
261 item = string_list_insert(&remote_refs, ref->name);
262 item->util = (void *)&ref->old_oid;
264 string_list_clear(&existing_refs, 1);
267 * We may have a final lightweight tag that needs to be
268 * checked to see if it needs fetching.
270 if (item && !has_sha1_file(item->util) &&
271 !will_fetch(head, item->util))
275 * For all the tags in the remote_refs string list,
276 * add them to the list of refs to be fetched
278 for_each_string_list_item(item, &remote_refs) {
279 /* Unless we have already decided to ignore this item... */
282 struct ref *rm = alloc_ref(item->string);
283 rm->peer_ref = alloc_ref(item->string);
284 oidcpy(&rm->old_oid, item->util);
290 string_list_clear(&remote_refs, 0);
293 static struct ref *get_ref_map(struct transport *transport,
294 struct refspec *refspecs, int refspec_count,
295 int tags, int *autotags)
299 struct ref *ref_map = NULL;
300 struct ref **tail = &ref_map;
302 /* opportunistically-updated references: */
303 struct ref *orefs = NULL, **oref_tail = &orefs;
305 const struct ref *remote_refs = transport_get_remote_refs(transport);
308 struct refspec *fetch_refspec;
309 int fetch_refspec_nr;
311 for (i = 0; i < refspec_count; i++) {
312 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
313 if (refspecs[i].dst && refspecs[i].dst[0])
316 /* Merge everything on the command line (but not --tags) */
317 for (rm = ref_map; rm; rm = rm->next)
318 rm->fetch_head_status = FETCH_HEAD_MERGE;
321 * For any refs that we happen to be fetching via
322 * command-line arguments, the destination ref might
323 * have been missing or have been different than the
324 * remote-tracking ref that would be derived from the
325 * configured refspec. In these cases, we want to
326 * take the opportunity to update their configured
327 * remote-tracking reference. However, we do not want
328 * to mention these entries in FETCH_HEAD at all, as
329 * they would simply be duplicates of existing
330 * entries, so we set them FETCH_HEAD_IGNORE below.
332 * We compute these entries now, based only on the
333 * refspecs specified on the command line. But we add
334 * them to the list following the refspecs resulting
335 * from the tags option so that one of the latter,
336 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
337 * by ref_remove_duplicates() in favor of one of these
338 * opportunistic entries with FETCH_HEAD_IGNORE.
341 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
342 fetch_refspec_nr = refmap_nr;
344 fetch_refspec = transport->remote->fetch;
345 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
348 for (i = 0; i < fetch_refspec_nr; i++)
349 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
351 if (tags == TAGS_SET)
352 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
353 } else if (refmap_array) {
354 die("--refmap option is only meaningful with command-line refspec(s).");
356 /* Use the defaults */
357 struct remote *remote = transport->remote;
358 struct branch *branch = branch_get(NULL);
359 int has_merge = branch_has_merge_config(branch);
361 (remote->fetch_refspec_nr ||
362 /* Note: has_merge implies non-NULL branch->remote_name */
363 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
364 for (i = 0; i < remote->fetch_refspec_nr; i++) {
365 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
366 if (remote->fetch[i].dst &&
367 remote->fetch[i].dst[0])
369 if (!i && !has_merge && ref_map &&
370 !remote->fetch[0].pattern)
371 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
374 * if the remote we're fetching from is the same
375 * as given in branch.<name>.remote, we add the
376 * ref given in branch.<name>.merge, too.
378 * Note: has_merge implies non-NULL branch->remote_name
381 !strcmp(branch->remote_name, remote->name))
382 add_merge_config(&ref_map, remote_refs, branch, &tail);
384 ref_map = get_remote_ref(remote_refs, "HEAD");
386 die(_("Couldn't find remote ref HEAD"));
387 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
388 tail = &ref_map->next;
392 if (tags == TAGS_SET)
393 /* also fetch all tags */
394 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
395 else if (tags == TAGS_DEFAULT && *autotags)
396 find_non_local_tags(transport, &ref_map, &tail);
398 /* Now append any refs to be updated opportunistically: */
400 for (rm = orefs; rm; rm = rm->next) {
401 rm->fetch_head_status = FETCH_HEAD_IGNORE;
405 return ref_remove_duplicates(ref_map);
408 #define STORE_REF_ERROR_OTHER 1
409 #define STORE_REF_ERROR_DF_CONFLICT 2
411 static int s_update_ref(const char *action,
416 char *rla = getenv("GIT_REFLOG_ACTION");
417 struct ref_transaction *transaction;
418 struct strbuf err = STRBUF_INIT;
419 int ret, df_conflict = 0;
424 rla = default_rla.buf;
425 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
427 transaction = ref_transaction_begin(&err);
429 ref_transaction_update(transaction, ref->name,
431 check_old ? ref->old_oid.hash : NULL,
435 ret = ref_transaction_commit(transaction, &err);
437 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
441 ref_transaction_free(transaction);
442 strbuf_release(&err);
445 ref_transaction_free(transaction);
446 error("%s", err.buf);
447 strbuf_release(&err);
448 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
449 : STORE_REF_ERROR_OTHER;
452 #define REFCOL_WIDTH 10
454 static int update_local_ref(struct ref *ref,
456 const struct ref *remote_ref,
457 struct strbuf *display)
459 struct commit *current = NULL, *updated;
460 enum object_type type;
461 struct branch *current_branch = branch_get(NULL);
462 const char *pretty_ref = prettify_refname(ref->name);
464 type = sha1_object_info(ref->new_oid.hash, NULL);
466 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
468 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
470 strbuf_addf(display, "= %-*s %-*s -> %s",
471 TRANSPORT_SUMMARY(_("[up to date]")),
472 REFCOL_WIDTH, remote, pretty_ref);
476 if (current_branch &&
477 !strcmp(ref->name, current_branch->name) &&
478 !(update_head_ok || is_bare_repository()) &&
479 !is_null_oid(&ref->old_oid)) {
481 * If this is the head, and it's not okay to update
482 * the head, and the old value of the head isn't empty...
485 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
486 TRANSPORT_SUMMARY(_("[rejected]")),
487 REFCOL_WIDTH, remote, pretty_ref);
491 if (!is_null_oid(&ref->old_oid) &&
492 starts_with(ref->name, "refs/tags/")) {
494 r = s_update_ref("updating tag", ref, 0);
495 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
497 TRANSPORT_SUMMARY(_("[tag update]")),
498 REFCOL_WIDTH, remote, pretty_ref,
499 r ? _(" (unable to update local ref)") : "");
503 current = lookup_commit_reference_gently(ref->old_oid.hash, 1);
504 updated = lookup_commit_reference_gently(ref->new_oid.hash, 1);
505 if (!current || !updated) {
510 * Nicely describe the new ref we're fetching.
511 * Base this on the remote's ref name, as it's
512 * more likely to follow a standard layout.
514 const char *name = remote_ref ? remote_ref->name : "";
515 if (starts_with(name, "refs/tags/")) {
517 what = _("[new tag]");
518 } else if (starts_with(name, "refs/heads/")) {
519 msg = "storing head";
520 what = _("[new branch]");
523 what = _("[new ref]");
526 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
527 (recurse_submodules != RECURSE_SUBMODULES_ON))
528 check_for_new_submodule_commits(ref->new_oid.hash);
529 r = s_update_ref(msg, ref, 0);
530 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
532 TRANSPORT_SUMMARY(what),
533 REFCOL_WIDTH, remote, pretty_ref,
534 r ? _(" (unable to update local ref)") : "");
538 if (in_merge_bases(current, updated)) {
539 struct strbuf quickref = STRBUF_INIT;
541 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
542 strbuf_addstr(&quickref, "..");
543 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
544 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
545 (recurse_submodules != RECURSE_SUBMODULES_ON))
546 check_for_new_submodule_commits(ref->new_oid.hash);
547 r = s_update_ref("fast-forward", ref, 1);
548 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
550 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
551 REFCOL_WIDTH, remote, pretty_ref,
552 r ? _(" (unable to update local ref)") : "");
553 strbuf_release(&quickref);
555 } else if (force || ref->force) {
556 struct strbuf quickref = STRBUF_INIT;
558 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
559 strbuf_addstr(&quickref, "...");
560 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
561 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
562 (recurse_submodules != RECURSE_SUBMODULES_ON))
563 check_for_new_submodule_commits(ref->new_oid.hash);
564 r = s_update_ref("forced-update", ref, 1);
565 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
567 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
568 REFCOL_WIDTH, remote, pretty_ref,
569 r ? _("unable to update local ref") : _("forced update"));
570 strbuf_release(&quickref);
573 strbuf_addf(display, "! %-*s %-*s -> %s %s",
574 TRANSPORT_SUMMARY(_("[rejected]")),
575 REFCOL_WIDTH, remote, pretty_ref,
576 _("(non-fast-forward)"));
581 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
583 struct ref **rm = cb_data;
584 struct ref *ref = *rm;
586 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
589 return -1; /* end of the list */
591 hashcpy(sha1, ref->old_oid.hash);
595 static int store_updated_refs(const char *raw_url, const char *remote_name,
599 struct commit *commit;
600 int url_len, i, rc = 0;
601 struct strbuf note = STRBUF_INIT;
602 const char *what, *kind;
605 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
608 fp = fopen(filename, "a");
610 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
613 url = transport_anonymize_url(raw_url);
615 url = xstrdup("foreign");
618 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
619 rc = error(_("%s did not send all necessary objects\n"), url);
624 * We do a pass for each fetch_head_status type in their enum order, so
625 * merged entries are written before not-for-merge. That lets readers
626 * use FETCH_HEAD as a refname to refer to the ref to be merged.
628 for (want_status = FETCH_HEAD_MERGE;
629 want_status <= FETCH_HEAD_IGNORE;
631 for (rm = ref_map; rm; rm = rm->next) {
632 struct ref *ref = NULL;
633 const char *merge_status_marker = "";
635 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
636 if (want_status == FETCH_HEAD_MERGE)
637 warning(_("reject %s because shallow roots are not allowed to be updated"),
638 rm->peer_ref ? rm->peer_ref->name : rm->name);
642 commit = lookup_commit_reference_gently(rm->old_oid.hash, 1);
644 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
646 if (rm->fetch_head_status != want_status)
650 ref = alloc_ref(rm->peer_ref->name);
651 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
652 oidcpy(&ref->new_oid, &rm->old_oid);
653 ref->force = rm->peer_ref->force;
657 if (!strcmp(rm->name, "HEAD")) {
661 else if (starts_with(rm->name, "refs/heads/")) {
663 what = rm->name + 11;
665 else if (starts_with(rm->name, "refs/tags/")) {
667 what = rm->name + 10;
669 else if (starts_with(rm->name, "refs/remotes/")) {
670 kind = "remote-tracking branch";
671 what = rm->name + 13;
678 url_len = strlen(url);
679 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
682 if (4 < i && !strncmp(".git", url + i - 3, 4))
688 strbuf_addf(¬e, "%s ", kind);
689 strbuf_addf(¬e, "'%s' of ", what);
691 switch (rm->fetch_head_status) {
692 case FETCH_HEAD_NOT_FOR_MERGE:
693 merge_status_marker = "not-for-merge";
695 case FETCH_HEAD_MERGE:
696 fprintf(fp, "%s\t%s\t%s",
697 oid_to_hex(&rm->old_oid),
700 for (i = 0; i < url_len; ++i)
708 /* do not write anything to FETCH_HEAD */
714 rc |= update_local_ref(ref, what, rm, ¬e);
717 strbuf_addf(¬e, "* %-*s %-*s -> FETCH_HEAD",
718 TRANSPORT_SUMMARY_WIDTH,
719 *kind ? kind : "branch",
721 *what ? what : "HEAD");
723 if (verbosity >= 0 && !shown_url) {
724 fprintf(stderr, _("From %.*s\n"),
729 fprintf(stderr, " %s\n", note.buf);
734 if (rc & STORE_REF_ERROR_DF_CONFLICT)
735 error(_("some local refs could not be updated; try running\n"
736 " 'git remote prune %s' to remove any old, conflicting "
737 "branches"), remote_name);
740 strbuf_release(¬e);
747 * We would want to bypass the object transfer altogether if
748 * everything we are going to fetch already exists and is connected
751 static int quickfetch(struct ref *ref_map)
753 struct ref *rm = ref_map;
756 * If we are deepening a shallow clone we already have these
757 * objects reachable. Running rev-list here will return with
758 * a good (0) exit status and we'll bypass the fetch that we
759 * really need to perform. Claiming failure now will ensure
760 * we perform the network exchange to deepen our history.
764 return check_everything_connected(iterate_ref_map, 1, &rm);
767 static int fetch_refs(struct transport *transport, struct ref *ref_map)
769 int ret = quickfetch(ref_map);
771 ret = transport_fetch_refs(transport, ref_map);
773 ret |= store_updated_refs(transport->url,
774 transport->remote->name,
776 transport_unlock_pack(transport);
780 static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
783 int url_len, i, result = 0;
784 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
786 const char *dangling_msg = dry_run
787 ? _(" (%s will become dangling)")
788 : _(" (%s has become dangling)");
791 url = transport_anonymize_url(raw_url);
793 url = xstrdup("foreign");
795 url_len = strlen(url);
796 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
800 if (4 < i && !strncmp(".git", url + i - 3, 4))
804 struct string_list refnames = STRING_LIST_INIT_NODUP;
806 for (ref = stale_refs; ref; ref = ref->next)
807 string_list_append(&refnames, ref->name);
809 result = delete_refs(&refnames);
810 string_list_clear(&refnames, 0);
813 if (verbosity >= 0) {
814 for (ref = stale_refs; ref; ref = ref->next) {
816 fprintf(stderr, _("From %.*s\n"), url_len, url);
819 fprintf(stderr, " x %-*s %-*s -> %s\n",
820 TRANSPORT_SUMMARY(_("[deleted]")),
821 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
822 warn_dangling_symref(stderr, dangling_msg, ref->name);
827 free_refs(stale_refs);
831 static void check_not_current_branch(struct ref *ref_map)
833 struct branch *current_branch = branch_get(NULL);
835 if (is_bare_repository() || !current_branch)
838 for (; ref_map; ref_map = ref_map->next)
839 if (ref_map->peer_ref && !strcmp(current_branch->refname,
840 ref_map->peer_ref->name))
841 die(_("Refusing to fetch into current branch %s "
842 "of non-bare repository"), current_branch->refname);
845 static int truncate_fetch_head(void)
847 const char *filename = git_path_fetch_head();
848 FILE *fp = fopen_for_writing(filename);
851 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
856 static void set_option(struct transport *transport, const char *name, const char *value)
858 int r = transport_set_option(transport, name, value);
860 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
861 name, value, transport->url);
863 warning(_("Option \"%s\" is ignored for %s\n"),
864 name, transport->url);
867 static struct transport *prepare_transport(struct remote *remote)
869 struct transport *transport;
870 transport = transport_get(remote, NULL);
871 transport_set_verbosity(transport, verbosity, progress);
872 transport->family = family;
874 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
876 set_option(transport, TRANS_OPT_KEEP, "yes");
878 set_option(transport, TRANS_OPT_DEPTH, depth);
880 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
884 static void backfill_tags(struct transport *transport, struct ref *ref_map)
886 if (transport->cannot_reuse) {
887 gsecondary = prepare_transport(transport->remote);
888 transport = gsecondary;
891 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
892 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
893 fetch_refs(transport, ref_map);
896 transport_disconnect(gsecondary);
901 static int do_fetch(struct transport *transport,
902 struct refspec *refs, int ref_count)
904 struct string_list existing_refs = STRING_LIST_INIT_DUP;
907 int autotags = (transport->remote->fetch_tags == 1);
910 for_each_ref(add_existing, &existing_refs);
912 if (tags == TAGS_DEFAULT) {
913 if (transport->remote->fetch_tags == 2)
915 if (transport->remote->fetch_tags == -1)
919 if (!transport->get_refs_list || !transport->fetch)
920 die(_("Don't know how to fetch from %s"), transport->url);
922 /* if not appending, truncate FETCH_HEAD */
923 if (!append && !dry_run) {
924 retcode = truncate_fetch_head();
929 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
931 check_not_current_branch(ref_map);
933 for (rm = ref_map; rm; rm = rm->next) {
935 struct string_list_item *peer_item =
936 string_list_lookup(&existing_refs,
939 struct object_id *old_oid = peer_item->util;
940 oidcpy(&rm->peer_ref->old_oid, old_oid);
945 if (tags == TAGS_DEFAULT && autotags)
946 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
949 * We only prune based on refspecs specified
950 * explicitly (via command line or configuration); we
951 * don't care whether --tags was specified.
954 prune_refs(refs, ref_count, ref_map, transport->url);
956 prune_refs(transport->remote->fetch,
957 transport->remote->fetch_refspec_nr,
962 if (fetch_refs(transport, ref_map)) {
969 /* if neither --no-tags nor --tags was specified, do automated tag
971 if (tags == TAGS_DEFAULT && autotags) {
972 struct ref **tail = &ref_map;
974 find_non_local_tags(transport, &ref_map, &tail);
976 backfill_tags(transport, ref_map);
981 string_list_clear(&existing_refs, 1);
985 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
987 struct string_list *list = priv;
988 if (!remote->skip_default_update)
989 string_list_append(list, remote->name);
993 struct remote_group_data {
995 struct string_list *list;
998 static int get_remote_group(const char *key, const char *value, void *priv)
1000 struct remote_group_data *g = priv;
1002 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1003 /* split list by white space */
1005 size_t wordlen = strcspn(value, " \t\n");
1008 string_list_append(g->list,
1009 xstrndup(value, wordlen));
1010 value += wordlen + (value[wordlen] != '\0');
1017 static int add_remote_or_group(const char *name, struct string_list *list)
1019 int prev_nr = list->nr;
1020 struct remote_group_data g;
1021 g.name = name; g.list = list;
1023 git_config(get_remote_group, &g);
1024 if (list->nr == prev_nr) {
1025 struct remote *remote = remote_get(name);
1026 if (!remote_is_configured(remote))
1028 string_list_append(list, remote->name);
1033 static void add_options_to_argv(struct argv_array *argv)
1036 argv_array_push(argv, "--dry-run");
1038 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1040 argv_array_push(argv, "--update-head-ok");
1042 argv_array_push(argv, "--force");
1044 argv_array_push(argv, "--keep");
1045 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1046 argv_array_push(argv, "--recurse-submodules");
1047 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1048 argv_array_push(argv, "--recurse-submodules=on-demand");
1049 if (tags == TAGS_SET)
1050 argv_array_push(argv, "--tags");
1051 else if (tags == TAGS_UNSET)
1052 argv_array_push(argv, "--no-tags");
1054 argv_array_push(argv, "-v");
1056 argv_array_push(argv, "-v");
1057 else if (verbosity < 0)
1058 argv_array_push(argv, "-q");
1062 static int fetch_multiple(struct string_list *list)
1065 struct argv_array argv = ARGV_ARRAY_INIT;
1067 if (!append && !dry_run) {
1068 int errcode = truncate_fetch_head();
1073 argv_array_pushl(&argv, "fetch", "--append", NULL);
1074 add_options_to_argv(&argv);
1076 for (i = 0; i < list->nr; i++) {
1077 const char *name = list->items[i].string;
1078 argv_array_push(&argv, name);
1080 printf(_("Fetching %s\n"), name);
1081 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1082 error(_("Could not fetch %s"), name);
1085 argv_array_pop(&argv);
1088 argv_array_clear(&argv);
1092 static int fetch_one(struct remote *remote, int argc, const char **argv)
1094 static const char **refs = NULL;
1095 struct refspec *refspec;
1100 die(_("No remote repository specified. Please, specify either a URL or a\n"
1101 "remote name from which new revisions should be fetched."));
1103 gtransport = prepare_transport(remote);
1106 /* no command line request */
1107 if (0 <= gtransport->remote->prune)
1108 prune = gtransport->remote->prune;
1109 else if (0 <= fetch_prune_config)
1110 prune = fetch_prune_config;
1112 prune = PRUNE_BY_DEFAULT;
1118 refs = xcalloc(st_add(argc, 1), sizeof(const char *));
1119 for (i = 0; i < argc; i++) {
1120 if (!strcmp(argv[i], "tag")) {
1123 die(_("You need to specify a tag name."));
1124 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1127 refs[j++] = argv[i];
1133 sigchain_push_common(unlock_pack_on_signal);
1134 atexit(unlock_pack);
1135 refspec = parse_fetch_refspec(ref_nr, refs);
1136 exit_code = do_fetch(gtransport, refspec, ref_nr);
1137 free_refspec(ref_nr, refspec);
1138 transport_disconnect(gtransport);
1143 int cmd_fetch(int argc, const char **argv, const char *prefix)
1146 struct string_list list = STRING_LIST_INIT_NODUP;
1147 struct remote *remote;
1149 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
1151 packet_trace_identity("fetch");
1153 /* Record the command line for the reflog */
1154 strbuf_addstr(&default_rla, "fetch");
1155 for (i = 1; i < argc; i++)
1156 strbuf_addf(&default_rla, " %s", argv[i]);
1158 git_config(git_fetch_config, NULL);
1160 argc = parse_options(argc, argv, prefix,
1161 builtin_fetch_options, builtin_fetch_usage, 0);
1165 die(_("--depth and --unshallow cannot be used together"));
1166 else if (!is_repository_shallow())
1167 die(_("--unshallow on a complete repository does not make sense"));
1169 depth = xstrfmt("%d", INFINITE_DEPTH);
1172 /* no need to be strict, transport_set_option() will validate it again */
1173 if (depth && atoi(depth) < 1)
1174 die(_("depth %s is not a positive number"), depth);
1176 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1177 if (recurse_submodules_default) {
1178 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1179 set_config_fetch_recurse_submodules(arg);
1181 gitmodules_config();
1182 git_config(submodule_config, NULL);
1187 die(_("fetch --all does not take a repository argument"));
1189 die(_("fetch --all does not make sense with refspecs"));
1190 (void) for_each_remote(get_one_remote_for_fetch, &list);
1191 result = fetch_multiple(&list);
1192 } else if (argc == 0) {
1193 /* No arguments -- use default remote */
1194 remote = remote_get(NULL);
1195 result = fetch_one(remote, argc, argv);
1196 } else if (multiple) {
1197 /* All arguments are assumed to be remotes or groups */
1198 for (i = 0; i < argc; i++)
1199 if (!add_remote_or_group(argv[i], &list))
1200 die(_("No such remote or remote group: %s"), argv[i]);
1201 result = fetch_multiple(&list);
1203 /* Single remote or group */
1204 (void) add_remote_or_group(argv[0], &list);
1206 /* More than one remote */
1208 die(_("Fetching a group and specifying refspecs does not make sense"));
1209 result = fetch_multiple(&list);
1211 /* Zero or one remotes */
1212 remote = remote_get(argv[0]);
1213 result = fetch_one(remote, argc-1, argv+1);
1217 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1218 struct argv_array options = ARGV_ARRAY_INIT;
1220 add_options_to_argv(&options);
1221 result = fetch_populated_submodules(&options,
1226 argv_array_clear(&options);
1229 /* All names were strdup()ed or strndup()ed */
1230 list.strdup_strings = 1;
1231 string_list_clear(&list, 0);
1235 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1237 argv_array_push(&argv_gc_auto, "--quiet");
1238 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1239 argv_array_clear(&argv_gc_auto);