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, deepen_relative;
38 static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
39 static int tags = TAGS_DEFAULT, unshallow, update_shallow, deepen;
40 static int max_children = 1;
41 static const char *depth;
42 static const char *deepen_since;
43 static const char *upload_pack;
44 static struct string_list deepen_not = STRING_LIST_INIT_NODUP;
45 static struct strbuf default_rla = STRBUF_INIT;
46 static struct transport *gtransport;
47 static struct transport *gsecondary;
48 static const char *submodule_prefix = "";
49 static const char *recurse_submodules_default;
50 static int shown_url = 0;
51 static int refmap_alloc, refmap_nr;
52 static const char **refmap_array;
54 static int option_parse_recurse_submodules(const struct option *opt,
55 const char *arg, int unset)
58 recurse_submodules = RECURSE_SUBMODULES_OFF;
61 recurse_submodules = parse_fetch_recurse_submodules_arg(opt->long_name, arg);
63 recurse_submodules = RECURSE_SUBMODULES_ON;
68 static int git_fetch_config(const char *k, const char *v, void *cb)
70 if (!strcmp(k, "fetch.prune")) {
71 fetch_prune_config = git_config_bool(k, v);
74 return git_default_config(k, v, cb);
77 static int parse_refmap_arg(const struct option *opt, const char *arg, int unset)
79 ALLOC_GROW(refmap_array, refmap_nr + 1, refmap_alloc);
82 * "git fetch --refmap='' origin foo"
83 * can be used to tell the command not to store anywhere
86 refmap_array[refmap_nr++] = arg;
90 static struct option builtin_fetch_options[] = {
91 OPT__VERBOSITY(&verbosity),
92 OPT_BOOL(0, "all", &all,
93 N_("fetch from all remotes")),
94 OPT_BOOL('a', "append", &append,
95 N_("append to .git/FETCH_HEAD instead of overwriting")),
96 OPT_STRING(0, "upload-pack", &upload_pack, N_("path"),
97 N_("path to upload pack on remote end")),
98 OPT__FORCE(&force, N_("force overwrite of local branch")),
99 OPT_BOOL('m', "multiple", &multiple,
100 N_("fetch from multiple remotes")),
101 OPT_SET_INT('t', "tags", &tags,
102 N_("fetch all tags and associated objects"), TAGS_SET),
103 OPT_SET_INT('n', NULL, &tags,
104 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET),
105 OPT_INTEGER('j', "jobs", &max_children,
106 N_("number of submodules fetched in parallel")),
107 OPT_BOOL('p', "prune", &prune,
108 N_("prune remote-tracking branches no longer on remote")),
109 { OPTION_CALLBACK, 0, "recurse-submodules", NULL, N_("on-demand"),
110 N_("control recursive fetching of submodules"),
111 PARSE_OPT_OPTARG, option_parse_recurse_submodules },
112 OPT_BOOL(0, "dry-run", &dry_run,
114 OPT_BOOL('k', "keep", &keep, N_("keep downloaded pack")),
115 OPT_BOOL('u', "update-head-ok", &update_head_ok,
116 N_("allow updating of HEAD ref")),
117 OPT_BOOL(0, "progress", &progress, N_("force progress reporting")),
118 OPT_STRING(0, "depth", &depth, N_("depth"),
119 N_("deepen history of shallow clone")),
120 OPT_STRING(0, "shallow-since", &deepen_since, N_("time"),
121 N_("deepen history of shallow repository based on time")),
122 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not, N_("revision"),
123 N_("deepen history of shallow clone by excluding rev")),
124 OPT_INTEGER(0, "deepen", &deepen_relative,
125 N_("deepen history of shallow clone")),
126 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
127 N_("convert to a complete repository"),
128 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
129 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
130 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
131 { OPTION_STRING, 0, "recurse-submodules-default",
132 &recurse_submodules_default, NULL,
133 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
134 OPT_BOOL(0, "update-shallow", &update_shallow,
135 N_("accept refs that update .git/shallow")),
136 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
137 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
141 static void unlock_pack(void)
144 transport_unlock_pack(gtransport);
146 transport_unlock_pack(gsecondary);
149 static void unlock_pack_on_signal(int signo)
156 static void add_merge_config(struct ref **head,
157 const struct ref *remote_refs,
158 struct branch *branch,
163 for (i = 0; i < branch->merge_nr; i++) {
164 struct ref *rm, **old_tail = *tail;
165 struct refspec refspec;
167 for (rm = *head; rm; rm = rm->next) {
168 if (branch_merge_matches(branch, i, rm->name)) {
169 rm->fetch_head_status = FETCH_HEAD_MERGE;
177 * Not fetched to a remote-tracking branch? We need to fetch
178 * it anyway to allow this branch's "branch.$name.merge"
179 * to be honored by 'git pull', but we do not have to
180 * fail if branch.$name.merge is misconfigured to point
181 * at a nonexisting branch. If we were indeed called by
182 * 'git pull', it will notice the misconfiguration because
183 * there is no entry in the resulting FETCH_HEAD marked
186 memset(&refspec, 0, sizeof(refspec));
187 refspec.src = branch->merge[i]->src;
188 get_fetch_map(remote_refs, &refspec, tail, 1);
189 for (rm = *old_tail; rm; rm = rm->next)
190 rm->fetch_head_status = FETCH_HEAD_MERGE;
194 static int add_existing(const char *refname, const struct object_id *oid,
195 int flag, void *cbdata)
197 struct string_list *list = (struct string_list *)cbdata;
198 struct string_list_item *item = string_list_insert(list, refname);
199 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
201 oidcpy(old_oid, oid);
202 item->util = old_oid;
206 static int will_fetch(struct ref **head, const unsigned char *sha1)
208 struct ref *rm = *head;
210 if (!hashcmp(rm->old_oid.hash, sha1))
217 static void find_non_local_tags(struct transport *transport,
221 struct string_list existing_refs = STRING_LIST_INIT_DUP;
222 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
223 const struct ref *ref;
224 struct string_list_item *item = NULL;
226 for_each_ref(add_existing, &existing_refs);
227 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
228 if (!starts_with(ref->name, "refs/tags/"))
232 * The peeled ref always follows the matching base
233 * ref, so if we see a peeled ref that we don't want
234 * to fetch then we can mark the ref entry in the list
235 * as one to ignore by setting util to NULL.
237 if (ends_with(ref->name, "^{}")) {
238 if (item && !has_object_file(&ref->old_oid) &&
239 !will_fetch(head, ref->old_oid.hash) &&
240 !has_sha1_file(item->util) &&
241 !will_fetch(head, item->util))
248 * If item is non-NULL here, then we previously saw a
249 * ref not followed by a peeled reference, so we need
250 * to check if it is a lightweight tag that we want to
253 if (item && !has_sha1_file(item->util) &&
254 !will_fetch(head, item->util))
259 /* skip duplicates and refs that we already have */
260 if (string_list_has_string(&remote_refs, ref->name) ||
261 string_list_has_string(&existing_refs, ref->name))
264 item = string_list_insert(&remote_refs, ref->name);
265 item->util = (void *)&ref->old_oid;
267 string_list_clear(&existing_refs, 1);
270 * We may have a final lightweight tag that needs to be
271 * checked to see if it needs fetching.
273 if (item && !has_sha1_file(item->util) &&
274 !will_fetch(head, item->util))
278 * For all the tags in the remote_refs string list,
279 * add them to the list of refs to be fetched
281 for_each_string_list_item(item, &remote_refs) {
282 /* Unless we have already decided to ignore this item... */
285 struct ref *rm = alloc_ref(item->string);
286 rm->peer_ref = alloc_ref(item->string);
287 oidcpy(&rm->old_oid, item->util);
293 string_list_clear(&remote_refs, 0);
296 static struct ref *get_ref_map(struct transport *transport,
297 struct refspec *refspecs, int refspec_count,
298 int tags, int *autotags)
302 struct ref *ref_map = NULL;
303 struct ref **tail = &ref_map;
305 /* opportunistically-updated references: */
306 struct ref *orefs = NULL, **oref_tail = &orefs;
308 const struct ref *remote_refs = transport_get_remote_refs(transport);
311 struct refspec *fetch_refspec;
312 int fetch_refspec_nr;
314 for (i = 0; i < refspec_count; i++) {
315 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
316 if (refspecs[i].dst && refspecs[i].dst[0])
319 /* Merge everything on the command line (but not --tags) */
320 for (rm = ref_map; rm; rm = rm->next)
321 rm->fetch_head_status = FETCH_HEAD_MERGE;
324 * For any refs that we happen to be fetching via
325 * command-line arguments, the destination ref might
326 * have been missing or have been different than the
327 * remote-tracking ref that would be derived from the
328 * configured refspec. In these cases, we want to
329 * take the opportunity to update their configured
330 * remote-tracking reference. However, we do not want
331 * to mention these entries in FETCH_HEAD at all, as
332 * they would simply be duplicates of existing
333 * entries, so we set them FETCH_HEAD_IGNORE below.
335 * We compute these entries now, based only on the
336 * refspecs specified on the command line. But we add
337 * them to the list following the refspecs resulting
338 * from the tags option so that one of the latter,
339 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
340 * by ref_remove_duplicates() in favor of one of these
341 * opportunistic entries with FETCH_HEAD_IGNORE.
344 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
345 fetch_refspec_nr = refmap_nr;
347 fetch_refspec = transport->remote->fetch;
348 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
351 for (i = 0; i < fetch_refspec_nr; i++)
352 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
354 if (tags == TAGS_SET)
355 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
356 } else if (refmap_array) {
357 die("--refmap option is only meaningful with command-line refspec(s).");
359 /* Use the defaults */
360 struct remote *remote = transport->remote;
361 struct branch *branch = branch_get(NULL);
362 int has_merge = branch_has_merge_config(branch);
364 (remote->fetch_refspec_nr ||
365 /* Note: has_merge implies non-NULL branch->remote_name */
366 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
367 for (i = 0; i < remote->fetch_refspec_nr; i++) {
368 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
369 if (remote->fetch[i].dst &&
370 remote->fetch[i].dst[0])
372 if (!i && !has_merge && ref_map &&
373 !remote->fetch[0].pattern)
374 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
377 * if the remote we're fetching from is the same
378 * as given in branch.<name>.remote, we add the
379 * ref given in branch.<name>.merge, too.
381 * Note: has_merge implies non-NULL branch->remote_name
384 !strcmp(branch->remote_name, remote->name))
385 add_merge_config(&ref_map, remote_refs, branch, &tail);
387 ref_map = get_remote_ref(remote_refs, "HEAD");
389 die(_("Couldn't find remote ref HEAD"));
390 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
391 tail = &ref_map->next;
395 if (tags == TAGS_SET)
396 /* also fetch all tags */
397 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
398 else if (tags == TAGS_DEFAULT && *autotags)
399 find_non_local_tags(transport, &ref_map, &tail);
401 /* Now append any refs to be updated opportunistically: */
403 for (rm = orefs; rm; rm = rm->next) {
404 rm->fetch_head_status = FETCH_HEAD_IGNORE;
408 return ref_remove_duplicates(ref_map);
411 #define STORE_REF_ERROR_OTHER 1
412 #define STORE_REF_ERROR_DF_CONFLICT 2
414 static int s_update_ref(const char *action,
419 char *rla = getenv("GIT_REFLOG_ACTION");
420 struct ref_transaction *transaction;
421 struct strbuf err = STRBUF_INIT;
422 int ret, df_conflict = 0;
427 rla = default_rla.buf;
428 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
430 transaction = ref_transaction_begin(&err);
432 ref_transaction_update(transaction, ref->name,
434 check_old ? ref->old_oid.hash : NULL,
438 ret = ref_transaction_commit(transaction, &err);
440 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
444 ref_transaction_free(transaction);
445 strbuf_release(&err);
448 ref_transaction_free(transaction);
449 error("%s", err.buf);
450 strbuf_release(&err);
451 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
452 : STORE_REF_ERROR_OTHER;
455 #define REFCOL_WIDTH 10
457 static int update_local_ref(struct ref *ref,
459 const struct ref *remote_ref,
460 struct strbuf *display)
462 struct commit *current = NULL, *updated;
463 enum object_type type;
464 struct branch *current_branch = branch_get(NULL);
465 const char *pretty_ref = prettify_refname(ref->name);
467 type = sha1_object_info(ref->new_oid.hash, NULL);
469 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
471 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
473 strbuf_addf(display, "= %-*s %-*s -> %s",
474 TRANSPORT_SUMMARY(_("[up to date]")),
475 REFCOL_WIDTH, remote, pretty_ref);
479 if (current_branch &&
480 !strcmp(ref->name, current_branch->name) &&
481 !(update_head_ok || is_bare_repository()) &&
482 !is_null_oid(&ref->old_oid)) {
484 * If this is the head, and it's not okay to update
485 * the head, and the old value of the head isn't empty...
488 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
489 TRANSPORT_SUMMARY(_("[rejected]")),
490 REFCOL_WIDTH, remote, pretty_ref);
494 if (!is_null_oid(&ref->old_oid) &&
495 starts_with(ref->name, "refs/tags/")) {
497 r = s_update_ref("updating tag", ref, 0);
498 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
500 TRANSPORT_SUMMARY(_("[tag update]")),
501 REFCOL_WIDTH, remote, pretty_ref,
502 r ? _(" (unable to update local ref)") : "");
506 current = lookup_commit_reference_gently(ref->old_oid.hash, 1);
507 updated = lookup_commit_reference_gently(ref->new_oid.hash, 1);
508 if (!current || !updated) {
513 * Nicely describe the new ref we're fetching.
514 * Base this on the remote's ref name, as it's
515 * more likely to follow a standard layout.
517 const char *name = remote_ref ? remote_ref->name : "";
518 if (starts_with(name, "refs/tags/")) {
520 what = _("[new tag]");
521 } else if (starts_with(name, "refs/heads/")) {
522 msg = "storing head";
523 what = _("[new branch]");
526 what = _("[new ref]");
529 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
530 (recurse_submodules != RECURSE_SUBMODULES_ON))
531 check_for_new_submodule_commits(ref->new_oid.hash);
532 r = s_update_ref(msg, ref, 0);
533 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
535 TRANSPORT_SUMMARY(what),
536 REFCOL_WIDTH, remote, pretty_ref,
537 r ? _(" (unable to update local ref)") : "");
541 if (in_merge_bases(current, updated)) {
542 struct strbuf quickref = STRBUF_INIT;
544 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
545 strbuf_addstr(&quickref, "..");
546 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
547 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
548 (recurse_submodules != RECURSE_SUBMODULES_ON))
549 check_for_new_submodule_commits(ref->new_oid.hash);
550 r = s_update_ref("fast-forward", ref, 1);
551 strbuf_addf(display, "%c %-*s %-*s -> %s%s",
553 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
554 REFCOL_WIDTH, remote, pretty_ref,
555 r ? _(" (unable to update local ref)") : "");
556 strbuf_release(&quickref);
558 } else if (force || ref->force) {
559 struct strbuf quickref = STRBUF_INIT;
561 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
562 strbuf_addstr(&quickref, "...");
563 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
564 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
565 (recurse_submodules != RECURSE_SUBMODULES_ON))
566 check_for_new_submodule_commits(ref->new_oid.hash);
567 r = s_update_ref("forced-update", ref, 1);
568 strbuf_addf(display, "%c %-*s %-*s -> %s (%s)",
570 TRANSPORT_SUMMARY_WIDTH, quickref.buf,
571 REFCOL_WIDTH, remote, pretty_ref,
572 r ? _("unable to update local ref") : _("forced update"));
573 strbuf_release(&quickref);
576 strbuf_addf(display, "! %-*s %-*s -> %s %s",
577 TRANSPORT_SUMMARY(_("[rejected]")),
578 REFCOL_WIDTH, remote, pretty_ref,
579 _("(non-fast-forward)"));
584 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
586 struct ref **rm = cb_data;
587 struct ref *ref = *rm;
589 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
592 return -1; /* end of the list */
594 hashcpy(sha1, ref->old_oid.hash);
598 static int store_updated_refs(const char *raw_url, const char *remote_name,
602 struct commit *commit;
603 int url_len, i, rc = 0;
604 struct strbuf note = STRBUF_INIT;
605 const char *what, *kind;
608 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
611 fp = fopen(filename, "a");
613 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
616 url = transport_anonymize_url(raw_url);
618 url = xstrdup("foreign");
621 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
622 rc = error(_("%s did not send all necessary objects\n"), url);
627 * We do a pass for each fetch_head_status type in their enum order, so
628 * merged entries are written before not-for-merge. That lets readers
629 * use FETCH_HEAD as a refname to refer to the ref to be merged.
631 for (want_status = FETCH_HEAD_MERGE;
632 want_status <= FETCH_HEAD_IGNORE;
634 for (rm = ref_map; rm; rm = rm->next) {
635 struct ref *ref = NULL;
636 const char *merge_status_marker = "";
638 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
639 if (want_status == FETCH_HEAD_MERGE)
640 warning(_("reject %s because shallow roots are not allowed to be updated"),
641 rm->peer_ref ? rm->peer_ref->name : rm->name);
645 commit = lookup_commit_reference_gently(rm->old_oid.hash, 1);
647 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
649 if (rm->fetch_head_status != want_status)
653 ref = alloc_ref(rm->peer_ref->name);
654 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
655 oidcpy(&ref->new_oid, &rm->old_oid);
656 ref->force = rm->peer_ref->force;
660 if (!strcmp(rm->name, "HEAD")) {
664 else if (starts_with(rm->name, "refs/heads/")) {
666 what = rm->name + 11;
668 else if (starts_with(rm->name, "refs/tags/")) {
670 what = rm->name + 10;
672 else if (starts_with(rm->name, "refs/remotes/")) {
673 kind = "remote-tracking branch";
674 what = rm->name + 13;
681 url_len = strlen(url);
682 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
685 if (4 < i && !strncmp(".git", url + i - 3, 4))
691 strbuf_addf(¬e, "%s ", kind);
692 strbuf_addf(¬e, "'%s' of ", what);
694 switch (rm->fetch_head_status) {
695 case FETCH_HEAD_NOT_FOR_MERGE:
696 merge_status_marker = "not-for-merge";
698 case FETCH_HEAD_MERGE:
699 fprintf(fp, "%s\t%s\t%s",
700 oid_to_hex(&rm->old_oid),
703 for (i = 0; i < url_len; ++i)
711 /* do not write anything to FETCH_HEAD */
717 rc |= update_local_ref(ref, what, rm, ¬e);
720 strbuf_addf(¬e, "* %-*s %-*s -> FETCH_HEAD",
721 TRANSPORT_SUMMARY_WIDTH,
722 *kind ? kind : "branch",
724 *what ? what : "HEAD");
726 if (verbosity >= 0 && !shown_url) {
727 fprintf(stderr, _("From %.*s\n"),
732 fprintf(stderr, " %s\n", note.buf);
737 if (rc & STORE_REF_ERROR_DF_CONFLICT)
738 error(_("some local refs could not be updated; try running\n"
739 " 'git remote prune %s' to remove any old, conflicting "
740 "branches"), remote_name);
743 strbuf_release(¬e);
750 * We would want to bypass the object transfer altogether if
751 * everything we are going to fetch already exists and is connected
754 static int quickfetch(struct ref *ref_map)
756 struct ref *rm = ref_map;
759 * If we are deepening a shallow clone we already have these
760 * objects reachable. Running rev-list here will return with
761 * a good (0) exit status and we'll bypass the fetch that we
762 * really need to perform. Claiming failure now will ensure
763 * we perform the network exchange to deepen our history.
767 return check_everything_connected(iterate_ref_map, 1, &rm);
770 static int fetch_refs(struct transport *transport, struct ref *ref_map)
772 int ret = quickfetch(ref_map);
774 ret = transport_fetch_refs(transport, ref_map);
776 ret |= store_updated_refs(transport->url,
777 transport->remote->name,
779 transport_unlock_pack(transport);
783 static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
786 int url_len, i, result = 0;
787 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
789 const char *dangling_msg = dry_run
790 ? _(" (%s will become dangling)")
791 : _(" (%s has become dangling)");
794 url = transport_anonymize_url(raw_url);
796 url = xstrdup("foreign");
798 url_len = strlen(url);
799 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
803 if (4 < i && !strncmp(".git", url + i - 3, 4))
807 struct string_list refnames = STRING_LIST_INIT_NODUP;
809 for (ref = stale_refs; ref; ref = ref->next)
810 string_list_append(&refnames, ref->name);
812 result = delete_refs(&refnames);
813 string_list_clear(&refnames, 0);
816 if (verbosity >= 0) {
817 for (ref = stale_refs; ref; ref = ref->next) {
819 fprintf(stderr, _("From %.*s\n"), url_len, url);
822 fprintf(stderr, " x %-*s %-*s -> %s\n",
823 TRANSPORT_SUMMARY(_("[deleted]")),
824 REFCOL_WIDTH, _("(none)"), prettify_refname(ref->name));
825 warn_dangling_symref(stderr, dangling_msg, ref->name);
830 free_refs(stale_refs);
834 static void check_not_current_branch(struct ref *ref_map)
836 struct branch *current_branch = branch_get(NULL);
838 if (is_bare_repository() || !current_branch)
841 for (; ref_map; ref_map = ref_map->next)
842 if (ref_map->peer_ref && !strcmp(current_branch->refname,
843 ref_map->peer_ref->name))
844 die(_("Refusing to fetch into current branch %s "
845 "of non-bare repository"), current_branch->refname);
848 static int truncate_fetch_head(void)
850 const char *filename = git_path_fetch_head();
851 FILE *fp = fopen_for_writing(filename);
854 return error(_("cannot open %s: %s\n"), filename, strerror(errno));
859 static void set_option(struct transport *transport, const char *name, const char *value)
861 int r = transport_set_option(transport, name, value);
863 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
864 name, value, transport->url);
866 warning(_("Option \"%s\" is ignored for %s\n"),
867 name, transport->url);
870 static struct transport *prepare_transport(struct remote *remote, int deepen)
872 struct transport *transport;
873 transport = transport_get(remote, NULL);
874 transport_set_verbosity(transport, verbosity, progress);
876 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
878 set_option(transport, TRANS_OPT_KEEP, "yes");
880 set_option(transport, TRANS_OPT_DEPTH, depth);
881 if (deepen && deepen_since)
882 set_option(transport, TRANS_OPT_DEEPEN_SINCE, deepen_since);
883 if (deepen && deepen_not.nr)
884 set_option(transport, TRANS_OPT_DEEPEN_NOT,
885 (const char *)&deepen_not);
887 set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, "yes");
889 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
893 static void backfill_tags(struct transport *transport, struct ref *ref_map)
898 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
899 * when remote helper is used (setting it to an empty string
900 * is not unsetting). We could extend the remote helper
901 * protocol for that, but for now, just force a new connection
902 * without deepen-since. Similar story for deepen-not.
904 cannot_reuse = transport->cannot_reuse ||
905 deepen_since || deepen_not.nr;
907 gsecondary = prepare_transport(transport->remote, 0);
908 transport = gsecondary;
911 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
912 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
913 transport_set_option(transport, TRANS_OPT_DEEPEN_RELATIVE, NULL);
914 fetch_refs(transport, ref_map);
917 transport_disconnect(gsecondary);
922 static int do_fetch(struct transport *transport,
923 struct refspec *refs, int ref_count)
925 struct string_list existing_refs = STRING_LIST_INIT_DUP;
928 int autotags = (transport->remote->fetch_tags == 1);
931 for_each_ref(add_existing, &existing_refs);
933 if (tags == TAGS_DEFAULT) {
934 if (transport->remote->fetch_tags == 2)
936 if (transport->remote->fetch_tags == -1)
940 if (!transport->get_refs_list || !transport->fetch)
941 die(_("Don't know how to fetch from %s"), transport->url);
943 /* if not appending, truncate FETCH_HEAD */
944 if (!append && !dry_run) {
945 retcode = truncate_fetch_head();
950 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
952 check_not_current_branch(ref_map);
954 for (rm = ref_map; rm; rm = rm->next) {
956 struct string_list_item *peer_item =
957 string_list_lookup(&existing_refs,
960 struct object_id *old_oid = peer_item->util;
961 oidcpy(&rm->peer_ref->old_oid, old_oid);
966 if (tags == TAGS_DEFAULT && autotags)
967 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
970 * We only prune based on refspecs specified
971 * explicitly (via command line or configuration); we
972 * don't care whether --tags was specified.
975 prune_refs(refs, ref_count, ref_map, transport->url);
977 prune_refs(transport->remote->fetch,
978 transport->remote->fetch_refspec_nr,
983 if (fetch_refs(transport, ref_map)) {
990 /* if neither --no-tags nor --tags was specified, do automated tag
992 if (tags == TAGS_DEFAULT && autotags) {
993 struct ref **tail = &ref_map;
995 find_non_local_tags(transport, &ref_map, &tail);
997 backfill_tags(transport, ref_map);
1002 string_list_clear(&existing_refs, 1);
1006 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1008 struct string_list *list = priv;
1009 if (!remote->skip_default_update)
1010 string_list_append(list, remote->name);
1014 struct remote_group_data {
1016 struct string_list *list;
1019 static int get_remote_group(const char *key, const char *value, void *priv)
1021 struct remote_group_data *g = priv;
1023 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1024 /* split list by white space */
1026 size_t wordlen = strcspn(value, " \t\n");
1029 string_list_append(g->list,
1030 xstrndup(value, wordlen));
1031 value += wordlen + (value[wordlen] != '\0');
1038 static int add_remote_or_group(const char *name, struct string_list *list)
1040 int prev_nr = list->nr;
1041 struct remote_group_data g;
1042 g.name = name; g.list = list;
1044 git_config(get_remote_group, &g);
1045 if (list->nr == prev_nr) {
1046 struct remote *remote;
1047 if (!remote_is_configured(name))
1049 remote = remote_get(name);
1050 string_list_append(list, remote->name);
1055 static void add_options_to_argv(struct argv_array *argv)
1058 argv_array_push(argv, "--dry-run");
1060 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1062 argv_array_push(argv, "--update-head-ok");
1064 argv_array_push(argv, "--force");
1066 argv_array_push(argv, "--keep");
1067 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1068 argv_array_push(argv, "--recurse-submodules");
1069 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1070 argv_array_push(argv, "--recurse-submodules=on-demand");
1071 if (tags == TAGS_SET)
1072 argv_array_push(argv, "--tags");
1073 else if (tags == TAGS_UNSET)
1074 argv_array_push(argv, "--no-tags");
1076 argv_array_push(argv, "-v");
1078 argv_array_push(argv, "-v");
1079 else if (verbosity < 0)
1080 argv_array_push(argv, "-q");
1084 static int fetch_multiple(struct string_list *list)
1087 struct argv_array argv = ARGV_ARRAY_INIT;
1089 if (!append && !dry_run) {
1090 int errcode = truncate_fetch_head();
1095 argv_array_pushl(&argv, "fetch", "--append", NULL);
1096 add_options_to_argv(&argv);
1098 for (i = 0; i < list->nr; i++) {
1099 const char *name = list->items[i].string;
1100 argv_array_push(&argv, name);
1102 printf(_("Fetching %s\n"), name);
1103 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1104 error(_("Could not fetch %s"), name);
1107 argv_array_pop(&argv);
1110 argv_array_clear(&argv);
1114 static int fetch_one(struct remote *remote, int argc, const char **argv)
1116 static const char **refs = NULL;
1117 struct refspec *refspec;
1122 die(_("No remote repository specified. Please, specify either a URL or a\n"
1123 "remote name from which new revisions should be fetched."));
1125 gtransport = prepare_transport(remote, 1);
1128 /* no command line request */
1129 if (0 <= gtransport->remote->prune)
1130 prune = gtransport->remote->prune;
1131 else if (0 <= fetch_prune_config)
1132 prune = fetch_prune_config;
1134 prune = PRUNE_BY_DEFAULT;
1140 refs = xcalloc(argc + 1, sizeof(const char *));
1141 for (i = 0; i < argc; i++) {
1142 if (!strcmp(argv[i], "tag")) {
1145 die(_("You need to specify a tag name."));
1146 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1149 refs[j++] = argv[i];
1155 sigchain_push_common(unlock_pack_on_signal);
1156 atexit(unlock_pack);
1157 refspec = parse_fetch_refspec(ref_nr, refs);
1158 exit_code = do_fetch(gtransport, refspec, ref_nr);
1159 free_refspec(ref_nr, refspec);
1160 transport_disconnect(gtransport);
1165 int cmd_fetch(int argc, const char **argv, const char *prefix)
1168 struct string_list list = STRING_LIST_INIT_NODUP;
1169 struct remote *remote;
1171 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
1173 packet_trace_identity("fetch");
1175 /* Record the command line for the reflog */
1176 strbuf_addstr(&default_rla, "fetch");
1177 for (i = 1; i < argc; i++)
1178 strbuf_addf(&default_rla, " %s", argv[i]);
1180 git_config(git_fetch_config, NULL);
1182 argc = parse_options(argc, argv, prefix,
1183 builtin_fetch_options, builtin_fetch_usage, 0);
1185 if (deepen_relative) {
1186 if (deepen_relative < 0)
1187 die(_("Negative depth in --deepen is not supported"));
1189 die(_("--deepen and --depth are mutually exclusive"));
1190 depth = xstrfmt("%d", deepen_relative);
1194 die(_("--depth and --unshallow cannot be used together"));
1195 else if (!is_repository_shallow())
1196 die(_("--unshallow on a complete repository does not make sense"));
1198 depth = xstrfmt("%d", INFINITE_DEPTH);
1201 /* no need to be strict, transport_set_option() will validate it again */
1202 if (depth && atoi(depth) < 1)
1203 die(_("depth %s is not a positive number"), depth);
1204 if (depth || deepen_since || deepen_not.nr)
1207 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1208 if (recurse_submodules_default) {
1209 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1210 set_config_fetch_recurse_submodules(arg);
1212 gitmodules_config();
1213 git_config(submodule_config, NULL);
1218 die(_("fetch --all does not take a repository argument"));
1220 die(_("fetch --all does not make sense with refspecs"));
1221 (void) for_each_remote(get_one_remote_for_fetch, &list);
1222 result = fetch_multiple(&list);
1223 } else if (argc == 0) {
1224 /* No arguments -- use default remote */
1225 remote = remote_get(NULL);
1226 result = fetch_one(remote, argc, argv);
1227 } else if (multiple) {
1228 /* All arguments are assumed to be remotes or groups */
1229 for (i = 0; i < argc; i++)
1230 if (!add_remote_or_group(argv[i], &list))
1231 die(_("No such remote or remote group: %s"), argv[i]);
1232 result = fetch_multiple(&list);
1234 /* Single remote or group */
1235 (void) add_remote_or_group(argv[0], &list);
1237 /* More than one remote */
1239 die(_("Fetching a group and specifying refspecs does not make sense"));
1240 result = fetch_multiple(&list);
1242 /* Zero or one remotes */
1243 remote = remote_get(argv[0]);
1244 result = fetch_one(remote, argc-1, argv+1);
1248 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1249 struct argv_array options = ARGV_ARRAY_INIT;
1251 add_options_to_argv(&options);
1252 result = fetch_populated_submodules(&options,
1257 argv_array_clear(&options);
1260 /* All names were strdup()ed or strndup()ed */
1261 list.strdup_strings = 1;
1262 string_list_clear(&list, 0);
1266 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1268 argv_array_push(&argv_gc_auto, "--quiet");
1269 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1270 argv_array_clear(&argv_gc_auto);