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"
20 static const char * const builtin_fetch_usage[] = {
21 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
22 N_("git fetch [<options>] <group>"),
23 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
24 N_("git fetch --all [<options>]"),
34 static int fetch_prune_config = -1; /* unspecified */
35 static int prune = -1; /* unspecified */
36 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
38 static int all, append, dry_run, force, keep, multiple, update_head_ok, verbosity;
39 static int progress = -1, recurse_submodules = RECURSE_SUBMODULES_DEFAULT;
40 static int tags = TAGS_DEFAULT, unshallow, update_shallow;
41 static int max_children = -1;
42 static enum transport_family family;
43 static const char *depth;
44 static const char *upload_pack;
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 { OPTION_SET_INT, 0, "unshallow", &unshallow, NULL,
121 N_("convert to a complete repository"),
122 PARSE_OPT_NONEG | PARSE_OPT_NOARG, NULL, 1 },
123 { OPTION_STRING, 0, "submodule-prefix", &submodule_prefix, N_("dir"),
124 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN },
125 { OPTION_STRING, 0, "recurse-submodules-default",
126 &recurse_submodules_default, NULL,
127 N_("default mode for recursion"), PARSE_OPT_HIDDEN },
128 OPT_BOOL(0, "update-shallow", &update_shallow,
129 N_("accept refs that update .git/shallow")),
130 { OPTION_CALLBACK, 0, "refmap", NULL, N_("refmap"),
131 N_("specify fetch refmap"), PARSE_OPT_NONEG, parse_refmap_arg },
132 OPT_SET_INT('4', "ipv4", &family, N_("use IPv4 addresses only"),
133 TRANSPORT_FAMILY_IPV4),
134 OPT_SET_INT('6', "ipv6", &family, N_("use IPv6 addresses only"),
135 TRANSPORT_FAMILY_IPV6),
139 static void unlock_pack(void)
142 transport_unlock_pack(gtransport);
144 transport_unlock_pack(gsecondary);
147 static void unlock_pack_on_signal(int signo)
154 static void add_merge_config(struct ref **head,
155 const struct ref *remote_refs,
156 struct branch *branch,
161 for (i = 0; i < branch->merge_nr; i++) {
162 struct ref *rm, **old_tail = *tail;
163 struct refspec refspec;
165 for (rm = *head; rm; rm = rm->next) {
166 if (branch_merge_matches(branch, i, rm->name)) {
167 rm->fetch_head_status = FETCH_HEAD_MERGE;
175 * Not fetched to a remote-tracking branch? We need to fetch
176 * it anyway to allow this branch's "branch.$name.merge"
177 * to be honored by 'git pull', but we do not have to
178 * fail if branch.$name.merge is misconfigured to point
179 * at a nonexisting branch. If we were indeed called by
180 * 'git pull', it will notice the misconfiguration because
181 * there is no entry in the resulting FETCH_HEAD marked
184 memset(&refspec, 0, sizeof(refspec));
185 refspec.src = branch->merge[i]->src;
186 get_fetch_map(remote_refs, &refspec, tail, 1);
187 for (rm = *old_tail; rm; rm = rm->next)
188 rm->fetch_head_status = FETCH_HEAD_MERGE;
192 static int add_existing(const char *refname, const struct object_id *oid,
193 int flag, void *cbdata)
195 struct string_list *list = (struct string_list *)cbdata;
196 struct string_list_item *item = string_list_insert(list, refname);
197 struct object_id *old_oid = xmalloc(sizeof(*old_oid));
199 oidcpy(old_oid, oid);
200 item->util = old_oid;
204 static int will_fetch(struct ref **head, const unsigned char *sha1)
206 struct ref *rm = *head;
208 if (!hashcmp(rm->old_oid.hash, sha1))
215 static void find_non_local_tags(struct transport *transport,
219 struct string_list existing_refs = STRING_LIST_INIT_DUP;
220 struct string_list remote_refs = STRING_LIST_INIT_NODUP;
221 const struct ref *ref;
222 struct string_list_item *item = NULL;
224 for_each_ref(add_existing, &existing_refs);
225 for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
226 if (!starts_with(ref->name, "refs/tags/"))
230 * The peeled ref always follows the matching base
231 * ref, so if we see a peeled ref that we don't want
232 * to fetch then we can mark the ref entry in the list
233 * as one to ignore by setting util to NULL.
235 if (ends_with(ref->name, "^{}")) {
236 if (item && !has_object_file(&ref->old_oid) &&
237 !will_fetch(head, ref->old_oid.hash) &&
238 !has_sha1_file(item->util) &&
239 !will_fetch(head, item->util))
246 * If item is non-NULL here, then we previously saw a
247 * ref not followed by a peeled reference, so we need
248 * to check if it is a lightweight tag that we want to
251 if (item && !has_sha1_file(item->util) &&
252 !will_fetch(head, item->util))
257 /* skip duplicates and refs that we already have */
258 if (string_list_has_string(&remote_refs, ref->name) ||
259 string_list_has_string(&existing_refs, ref->name))
262 item = string_list_insert(&remote_refs, ref->name);
263 item->util = (void *)&ref->old_oid;
265 string_list_clear(&existing_refs, 1);
268 * We may have a final lightweight tag that needs to be
269 * checked to see if it needs fetching.
271 if (item && !has_sha1_file(item->util) &&
272 !will_fetch(head, item->util))
276 * For all the tags in the remote_refs string list,
277 * add them to the list of refs to be fetched
279 for_each_string_list_item(item, &remote_refs) {
280 /* Unless we have already decided to ignore this item... */
283 struct ref *rm = alloc_ref(item->string);
284 rm->peer_ref = alloc_ref(item->string);
285 oidcpy(&rm->old_oid, item->util);
291 string_list_clear(&remote_refs, 0);
294 static struct ref *get_ref_map(struct transport *transport,
295 struct refspec *refspecs, int refspec_count,
296 int tags, int *autotags)
300 struct ref *ref_map = NULL;
301 struct ref **tail = &ref_map;
303 /* opportunistically-updated references: */
304 struct ref *orefs = NULL, **oref_tail = &orefs;
306 const struct ref *remote_refs = transport_get_remote_refs(transport);
309 struct refspec *fetch_refspec;
310 int fetch_refspec_nr;
312 for (i = 0; i < refspec_count; i++) {
313 get_fetch_map(remote_refs, &refspecs[i], &tail, 0);
314 if (refspecs[i].dst && refspecs[i].dst[0])
317 /* Merge everything on the command line (but not --tags) */
318 for (rm = ref_map; rm; rm = rm->next)
319 rm->fetch_head_status = FETCH_HEAD_MERGE;
322 * For any refs that we happen to be fetching via
323 * command-line arguments, the destination ref might
324 * have been missing or have been different than the
325 * remote-tracking ref that would be derived from the
326 * configured refspec. In these cases, we want to
327 * take the opportunity to update their configured
328 * remote-tracking reference. However, we do not want
329 * to mention these entries in FETCH_HEAD at all, as
330 * they would simply be duplicates of existing
331 * entries, so we set them FETCH_HEAD_IGNORE below.
333 * We compute these entries now, based only on the
334 * refspecs specified on the command line. But we add
335 * them to the list following the refspecs resulting
336 * from the tags option so that one of the latter,
337 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
338 * by ref_remove_duplicates() in favor of one of these
339 * opportunistic entries with FETCH_HEAD_IGNORE.
342 fetch_refspec = parse_fetch_refspec(refmap_nr, refmap_array);
343 fetch_refspec_nr = refmap_nr;
345 fetch_refspec = transport->remote->fetch;
346 fetch_refspec_nr = transport->remote->fetch_refspec_nr;
349 for (i = 0; i < fetch_refspec_nr; i++)
350 get_fetch_map(ref_map, &fetch_refspec[i], &oref_tail, 1);
352 if (tags == TAGS_SET)
353 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
354 } else if (refmap_array) {
355 die("--refmap option is only meaningful with command-line refspec(s).");
357 /* Use the defaults */
358 struct remote *remote = transport->remote;
359 struct branch *branch = branch_get(NULL);
360 int has_merge = branch_has_merge_config(branch);
362 (remote->fetch_refspec_nr ||
363 /* Note: has_merge implies non-NULL branch->remote_name */
364 (has_merge && !strcmp(branch->remote_name, remote->name)))) {
365 for (i = 0; i < remote->fetch_refspec_nr; i++) {
366 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
367 if (remote->fetch[i].dst &&
368 remote->fetch[i].dst[0])
370 if (!i && !has_merge && ref_map &&
371 !remote->fetch[0].pattern)
372 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
375 * if the remote we're fetching from is the same
376 * as given in branch.<name>.remote, we add the
377 * ref given in branch.<name>.merge, too.
379 * Note: has_merge implies non-NULL branch->remote_name
382 !strcmp(branch->remote_name, remote->name))
383 add_merge_config(&ref_map, remote_refs, branch, &tail);
385 ref_map = get_remote_ref(remote_refs, "HEAD");
387 die(_("Couldn't find remote ref HEAD"));
388 ref_map->fetch_head_status = FETCH_HEAD_MERGE;
389 tail = &ref_map->next;
393 if (tags == TAGS_SET)
394 /* also fetch all tags */
395 get_fetch_map(remote_refs, tag_refspec, &tail, 0);
396 else if (tags == TAGS_DEFAULT && *autotags)
397 find_non_local_tags(transport, &ref_map, &tail);
399 /* Now append any refs to be updated opportunistically: */
401 for (rm = orefs; rm; rm = rm->next) {
402 rm->fetch_head_status = FETCH_HEAD_IGNORE;
406 return ref_remove_duplicates(ref_map);
409 #define STORE_REF_ERROR_OTHER 1
410 #define STORE_REF_ERROR_DF_CONFLICT 2
412 static int s_update_ref(const char *action,
417 char *rla = getenv("GIT_REFLOG_ACTION");
418 struct ref_transaction *transaction;
419 struct strbuf err = STRBUF_INIT;
420 int ret, df_conflict = 0;
425 rla = default_rla.buf;
426 snprintf(msg, sizeof(msg), "%s: %s", rla, action);
428 transaction = ref_transaction_begin(&err);
430 ref_transaction_update(transaction, ref->name,
432 check_old ? ref->old_oid.hash : NULL,
436 ret = ref_transaction_commit(transaction, &err);
438 df_conflict = (ret == TRANSACTION_NAME_CONFLICT);
442 ref_transaction_free(transaction);
443 strbuf_release(&err);
446 ref_transaction_free(transaction);
447 error("%s", err.buf);
448 strbuf_release(&err);
449 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
450 : STORE_REF_ERROR_OTHER;
453 static int refcol_width = 10;
455 static void adjust_refcol_width(const struct ref *ref)
457 int max, rlen, llen, len;
459 /* uptodate lines are only shown on high verbosity level */
460 if (!verbosity && !oidcmp(&ref->peer_ref->old_oid, &ref->old_oid))
463 max = term_columns();
464 rlen = utf8_strwidth(prettify_refname(ref->name));
465 llen = utf8_strwidth(prettify_refname(ref->peer_ref->name));
468 * rough estimation to see if the output line is too long and
469 * should not be counted (we can't do precise calculation
470 * anyway because we don't know if the error explanation part
471 * will be printed in update_local_ref)
473 len = 21 /* flag and summary */ + rlen + 4 /* -> */ + llen;
477 if (refcol_width < rlen)
481 static void prepare_format_display(struct ref *ref_map)
485 for (rm = ref_map; rm; rm = rm->next) {
486 if (rm->status == REF_STATUS_REJECT_SHALLOW ||
488 !strcmp(rm->name, "HEAD"))
491 adjust_refcol_width(rm);
495 static void format_display(struct strbuf *display, char code,
496 const char *summary, const char *error,
497 const char *remote, const char *local)
499 strbuf_addf(display, "%c %-*s ", code, TRANSPORT_SUMMARY(summary));
500 strbuf_addf(display, "%-*s -> %s", refcol_width, remote, local);
502 strbuf_addf(display, " (%s)", error);
505 static int update_local_ref(struct ref *ref,
507 const struct ref *remote_ref,
508 struct strbuf *display)
510 struct commit *current = NULL, *updated;
511 enum object_type type;
512 struct branch *current_branch = branch_get(NULL);
513 const char *pretty_ref = prettify_refname(ref->name);
515 type = sha1_object_info(ref->new_oid.hash, NULL);
517 die(_("object %s not found"), oid_to_hex(&ref->new_oid));
519 if (!oidcmp(&ref->old_oid, &ref->new_oid)) {
521 format_display(display, '=', _("[up to date]"), NULL,
526 if (current_branch &&
527 !strcmp(ref->name, current_branch->name) &&
528 !(update_head_ok || is_bare_repository()) &&
529 !is_null_oid(&ref->old_oid)) {
531 * If this is the head, and it's not okay to update
532 * the head, and the old value of the head isn't empty...
534 format_display(display, '!', _("[rejected]"),
535 _("can't fetch in current branch"),
540 if (!is_null_oid(&ref->old_oid) &&
541 starts_with(ref->name, "refs/tags/")) {
543 r = s_update_ref("updating tag", ref, 0);
544 format_display(display, r ? '!' : 't', _("[tag update]"),
545 r ? _("unable to update local ref") : NULL,
550 current = lookup_commit_reference_gently(ref->old_oid.hash, 1);
551 updated = lookup_commit_reference_gently(ref->new_oid.hash, 1);
552 if (!current || !updated) {
557 * Nicely describe the new ref we're fetching.
558 * Base this on the remote's ref name, as it's
559 * more likely to follow a standard layout.
561 const char *name = remote_ref ? remote_ref->name : "";
562 if (starts_with(name, "refs/tags/")) {
564 what = _("[new tag]");
565 } else if (starts_with(name, "refs/heads/")) {
566 msg = "storing head";
567 what = _("[new branch]");
570 what = _("[new ref]");
573 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
574 (recurse_submodules != RECURSE_SUBMODULES_ON))
575 check_for_new_submodule_commits(ref->new_oid.hash);
576 r = s_update_ref(msg, ref, 0);
577 format_display(display, r ? '!' : '*', what,
578 r ? _("unable to update local ref") : NULL,
583 if (in_merge_bases(current, updated)) {
584 struct strbuf quickref = STRBUF_INIT;
586 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
587 strbuf_addstr(&quickref, "..");
588 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
589 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
590 (recurse_submodules != RECURSE_SUBMODULES_ON))
591 check_for_new_submodule_commits(ref->new_oid.hash);
592 r = s_update_ref("fast-forward", ref, 1);
593 format_display(display, r ? '!' : ' ', quickref.buf,
594 r ? _("unable to update local ref") : NULL,
596 strbuf_release(&quickref);
598 } else if (force || ref->force) {
599 struct strbuf quickref = STRBUF_INIT;
601 strbuf_add_unique_abbrev(&quickref, current->object.oid.hash, DEFAULT_ABBREV);
602 strbuf_addstr(&quickref, "...");
603 strbuf_add_unique_abbrev(&quickref, ref->new_oid.hash, DEFAULT_ABBREV);
604 if ((recurse_submodules != RECURSE_SUBMODULES_OFF) &&
605 (recurse_submodules != RECURSE_SUBMODULES_ON))
606 check_for_new_submodule_commits(ref->new_oid.hash);
607 r = s_update_ref("forced-update", ref, 1);
608 format_display(display, r ? '!' : '+', quickref.buf,
609 r ? _("unable to update local ref") : _("forced update"),
611 strbuf_release(&quickref);
614 format_display(display, '!', _("[rejected]"), _("non-fast-forward"),
620 static int iterate_ref_map(void *cb_data, unsigned char sha1[20])
622 struct ref **rm = cb_data;
623 struct ref *ref = *rm;
625 while (ref && ref->status == REF_STATUS_REJECT_SHALLOW)
628 return -1; /* end of the list */
630 hashcpy(sha1, ref->old_oid.hash);
634 static int store_updated_refs(const char *raw_url, const char *remote_name,
638 struct commit *commit;
639 int url_len, i, rc = 0;
640 struct strbuf note = STRBUF_INIT;
641 const char *what, *kind;
644 const char *filename = dry_run ? "/dev/null" : git_path_fetch_head();
647 fp = fopen(filename, "a");
649 return error_errno(_("cannot open %s"), filename);
652 url = transport_anonymize_url(raw_url);
654 url = xstrdup("foreign");
657 if (check_everything_connected(iterate_ref_map, 0, &rm)) {
658 rc = error(_("%s did not send all necessary objects\n"), url);
662 prepare_format_display(ref_map);
665 * We do a pass for each fetch_head_status type in their enum order, so
666 * merged entries are written before not-for-merge. That lets readers
667 * use FETCH_HEAD as a refname to refer to the ref to be merged.
669 for (want_status = FETCH_HEAD_MERGE;
670 want_status <= FETCH_HEAD_IGNORE;
672 for (rm = ref_map; rm; rm = rm->next) {
673 struct ref *ref = NULL;
674 const char *merge_status_marker = "";
676 if (rm->status == REF_STATUS_REJECT_SHALLOW) {
677 if (want_status == FETCH_HEAD_MERGE)
678 warning(_("reject %s because shallow roots are not allowed to be updated"),
679 rm->peer_ref ? rm->peer_ref->name : rm->name);
683 commit = lookup_commit_reference_gently(rm->old_oid.hash, 1);
685 rm->fetch_head_status = FETCH_HEAD_NOT_FOR_MERGE;
687 if (rm->fetch_head_status != want_status)
691 ref = alloc_ref(rm->peer_ref->name);
692 oidcpy(&ref->old_oid, &rm->peer_ref->old_oid);
693 oidcpy(&ref->new_oid, &rm->old_oid);
694 ref->force = rm->peer_ref->force;
698 if (!strcmp(rm->name, "HEAD")) {
702 else if (starts_with(rm->name, "refs/heads/")) {
704 what = rm->name + 11;
706 else if (starts_with(rm->name, "refs/tags/")) {
708 what = rm->name + 10;
710 else if (starts_with(rm->name, "refs/remotes/")) {
711 kind = "remote-tracking branch";
712 what = rm->name + 13;
719 url_len = strlen(url);
720 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
723 if (4 < i && !strncmp(".git", url + i - 3, 4))
729 strbuf_addf(¬e, "%s ", kind);
730 strbuf_addf(¬e, "'%s' of ", what);
732 switch (rm->fetch_head_status) {
733 case FETCH_HEAD_NOT_FOR_MERGE:
734 merge_status_marker = "not-for-merge";
736 case FETCH_HEAD_MERGE:
737 fprintf(fp, "%s\t%s\t%s",
738 oid_to_hex(&rm->old_oid),
741 for (i = 0; i < url_len; ++i)
749 /* do not write anything to FETCH_HEAD */
755 rc |= update_local_ref(ref, what, rm, ¬e);
758 format_display(¬e, '*',
759 *kind ? kind : "branch", NULL,
760 *what ? what : "HEAD",
763 if (verbosity >= 0 && !shown_url) {
764 fprintf(stderr, _("From %.*s\n"),
769 fprintf(stderr, " %s\n", note.buf);
774 if (rc & STORE_REF_ERROR_DF_CONFLICT)
775 error(_("some local refs could not be updated; try running\n"
776 " 'git remote prune %s' to remove any old, conflicting "
777 "branches"), remote_name);
780 strbuf_release(¬e);
787 * We would want to bypass the object transfer altogether if
788 * everything we are going to fetch already exists and is connected
791 static int quickfetch(struct ref *ref_map)
793 struct ref *rm = ref_map;
796 * If we are deepening a shallow clone we already have these
797 * objects reachable. Running rev-list here will return with
798 * a good (0) exit status and we'll bypass the fetch that we
799 * really need to perform. Claiming failure now will ensure
800 * we perform the network exchange to deepen our history.
804 return check_everything_connected(iterate_ref_map, 1, &rm);
807 static int fetch_refs(struct transport *transport, struct ref *ref_map)
809 int ret = quickfetch(ref_map);
811 ret = transport_fetch_refs(transport, ref_map);
813 ret |= store_updated_refs(transport->url,
814 transport->remote->name,
816 transport_unlock_pack(transport);
820 static int prune_refs(struct refspec *refs, int ref_count, struct ref *ref_map,
823 int url_len, i, result = 0;
824 struct ref *ref, *stale_refs = get_stale_heads(refs, ref_count, ref_map);
826 const char *dangling_msg = dry_run
827 ? _(" (%s will become dangling)")
828 : _(" (%s has become dangling)");
831 url = transport_anonymize_url(raw_url);
833 url = xstrdup("foreign");
835 url_len = strlen(url);
836 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
840 if (4 < i && !strncmp(".git", url + i - 3, 4))
844 struct string_list refnames = STRING_LIST_INIT_NODUP;
846 for (ref = stale_refs; ref; ref = ref->next)
847 string_list_append(&refnames, ref->name);
849 result = delete_refs(&refnames);
850 string_list_clear(&refnames, 0);
853 if (verbosity >= 0) {
854 for (ref = stale_refs; ref; ref = ref->next) {
855 struct strbuf sb = STRBUF_INIT;
857 fprintf(stderr, _("From %.*s\n"), url_len, url);
860 format_display(&sb, '-', _("[deleted]"), NULL,
861 _("(none)"), prettify_refname(ref->name));
862 fprintf(stderr, " %s\n",sb.buf);
864 warn_dangling_symref(stderr, dangling_msg, ref->name);
869 free_refs(stale_refs);
873 static void check_not_current_branch(struct ref *ref_map)
875 struct branch *current_branch = branch_get(NULL);
877 if (is_bare_repository() || !current_branch)
880 for (; ref_map; ref_map = ref_map->next)
881 if (ref_map->peer_ref && !strcmp(current_branch->refname,
882 ref_map->peer_ref->name))
883 die(_("Refusing to fetch into current branch %s "
884 "of non-bare repository"), current_branch->refname);
887 static int truncate_fetch_head(void)
889 const char *filename = git_path_fetch_head();
890 FILE *fp = fopen_for_writing(filename);
893 return error_errno(_("cannot open %s"), filename);
898 static void set_option(struct transport *transport, const char *name, const char *value)
900 int r = transport_set_option(transport, name, value);
902 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
903 name, value, transport->url);
905 warning(_("Option \"%s\" is ignored for %s\n"),
906 name, transport->url);
909 static struct transport *prepare_transport(struct remote *remote)
911 struct transport *transport;
912 transport = transport_get(remote, NULL);
913 transport_set_verbosity(transport, verbosity, progress);
914 transport->family = family;
916 set_option(transport, TRANS_OPT_UPLOADPACK, upload_pack);
918 set_option(transport, TRANS_OPT_KEEP, "yes");
920 set_option(transport, TRANS_OPT_DEPTH, depth);
922 set_option(transport, TRANS_OPT_UPDATE_SHALLOW, "yes");
926 static void backfill_tags(struct transport *transport, struct ref *ref_map)
928 if (transport->cannot_reuse) {
929 gsecondary = prepare_transport(transport->remote);
930 transport = gsecondary;
933 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
934 transport_set_option(transport, TRANS_OPT_DEPTH, "0");
935 fetch_refs(transport, ref_map);
938 transport_disconnect(gsecondary);
943 static int do_fetch(struct transport *transport,
944 struct refspec *refs, int ref_count)
946 struct string_list existing_refs = STRING_LIST_INIT_DUP;
949 int autotags = (transport->remote->fetch_tags == 1);
952 for_each_ref(add_existing, &existing_refs);
954 if (tags == TAGS_DEFAULT) {
955 if (transport->remote->fetch_tags == 2)
957 if (transport->remote->fetch_tags == -1)
961 if (!transport->get_refs_list || !transport->fetch)
962 die(_("Don't know how to fetch from %s"), transport->url);
964 /* if not appending, truncate FETCH_HEAD */
965 if (!append && !dry_run) {
966 retcode = truncate_fetch_head();
971 ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
973 check_not_current_branch(ref_map);
975 for (rm = ref_map; rm; rm = rm->next) {
977 struct string_list_item *peer_item =
978 string_list_lookup(&existing_refs,
981 struct object_id *old_oid = peer_item->util;
982 oidcpy(&rm->peer_ref->old_oid, old_oid);
987 if (tags == TAGS_DEFAULT && autotags)
988 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
991 * We only prune based on refspecs specified
992 * explicitly (via command line or configuration); we
993 * don't care whether --tags was specified.
996 prune_refs(refs, ref_count, ref_map, transport->url);
998 prune_refs(transport->remote->fetch,
999 transport->remote->fetch_refspec_nr,
1004 if (fetch_refs(transport, ref_map)) {
1011 /* if neither --no-tags nor --tags was specified, do automated tag
1013 if (tags == TAGS_DEFAULT && autotags) {
1014 struct ref **tail = &ref_map;
1016 find_non_local_tags(transport, &ref_map, &tail);
1018 backfill_tags(transport, ref_map);
1023 string_list_clear(&existing_refs, 1);
1027 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
1029 struct string_list *list = priv;
1030 if (!remote->skip_default_update)
1031 string_list_append(list, remote->name);
1035 struct remote_group_data {
1037 struct string_list *list;
1040 static int get_remote_group(const char *key, const char *value, void *priv)
1042 struct remote_group_data *g = priv;
1044 if (skip_prefix(key, "remotes.", &key) && !strcmp(key, g->name)) {
1045 /* split list by white space */
1047 size_t wordlen = strcspn(value, " \t\n");
1050 string_list_append(g->list,
1051 xstrndup(value, wordlen));
1052 value += wordlen + (value[wordlen] != '\0');
1059 static int add_remote_or_group(const char *name, struct string_list *list)
1061 int prev_nr = list->nr;
1062 struct remote_group_data g;
1063 g.name = name; g.list = list;
1065 git_config(get_remote_group, &g);
1066 if (list->nr == prev_nr) {
1067 struct remote *remote = remote_get(name);
1068 if (!remote_is_configured(remote))
1070 string_list_append(list, remote->name);
1075 static void add_options_to_argv(struct argv_array *argv)
1078 argv_array_push(argv, "--dry-run");
1080 argv_array_push(argv, prune ? "--prune" : "--no-prune");
1082 argv_array_push(argv, "--update-head-ok");
1084 argv_array_push(argv, "--force");
1086 argv_array_push(argv, "--keep");
1087 if (recurse_submodules == RECURSE_SUBMODULES_ON)
1088 argv_array_push(argv, "--recurse-submodules");
1089 else if (recurse_submodules == RECURSE_SUBMODULES_ON_DEMAND)
1090 argv_array_push(argv, "--recurse-submodules=on-demand");
1091 if (tags == TAGS_SET)
1092 argv_array_push(argv, "--tags");
1093 else if (tags == TAGS_UNSET)
1094 argv_array_push(argv, "--no-tags");
1096 argv_array_push(argv, "-v");
1098 argv_array_push(argv, "-v");
1099 else if (verbosity < 0)
1100 argv_array_push(argv, "-q");
1104 static int fetch_multiple(struct string_list *list)
1107 struct argv_array argv = ARGV_ARRAY_INIT;
1109 if (!append && !dry_run) {
1110 int errcode = truncate_fetch_head();
1115 argv_array_pushl(&argv, "fetch", "--append", NULL);
1116 add_options_to_argv(&argv);
1118 for (i = 0; i < list->nr; i++) {
1119 const char *name = list->items[i].string;
1120 argv_array_push(&argv, name);
1122 printf(_("Fetching %s\n"), name);
1123 if (run_command_v_opt(argv.argv, RUN_GIT_CMD)) {
1124 error(_("Could not fetch %s"), name);
1127 argv_array_pop(&argv);
1130 argv_array_clear(&argv);
1134 static int fetch_one(struct remote *remote, int argc, const char **argv)
1136 static const char **refs = NULL;
1137 struct refspec *refspec;
1142 die(_("No remote repository specified. Please, specify either a URL or a\n"
1143 "remote name from which new revisions should be fetched."));
1145 gtransport = prepare_transport(remote);
1148 /* no command line request */
1149 if (0 <= gtransport->remote->prune)
1150 prune = gtransport->remote->prune;
1151 else if (0 <= fetch_prune_config)
1152 prune = fetch_prune_config;
1154 prune = PRUNE_BY_DEFAULT;
1160 refs = xcalloc(st_add(argc, 1), sizeof(const char *));
1161 for (i = 0; i < argc; i++) {
1162 if (!strcmp(argv[i], "tag")) {
1165 die(_("You need to specify a tag name."));
1166 refs[j++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1169 refs[j++] = argv[i];
1175 sigchain_push_common(unlock_pack_on_signal);
1176 atexit(unlock_pack);
1177 refspec = parse_fetch_refspec(ref_nr, refs);
1178 exit_code = do_fetch(gtransport, refspec, ref_nr);
1179 free_refspec(ref_nr, refspec);
1180 transport_disconnect(gtransport);
1185 int cmd_fetch(int argc, const char **argv, const char *prefix)
1188 struct string_list list = STRING_LIST_INIT_NODUP;
1189 struct remote *remote;
1191 struct argv_array argv_gc_auto = ARGV_ARRAY_INIT;
1193 packet_trace_identity("fetch");
1195 /* Record the command line for the reflog */
1196 strbuf_addstr(&default_rla, "fetch");
1197 for (i = 1; i < argc; i++)
1198 strbuf_addf(&default_rla, " %s", argv[i]);
1200 git_config(git_fetch_config, NULL);
1202 argc = parse_options(argc, argv, prefix,
1203 builtin_fetch_options, builtin_fetch_usage, 0);
1207 die(_("--depth and --unshallow cannot be used together"));
1208 else if (!is_repository_shallow())
1209 die(_("--unshallow on a complete repository does not make sense"));
1211 depth = xstrfmt("%d", INFINITE_DEPTH);
1214 /* no need to be strict, transport_set_option() will validate it again */
1215 if (depth && atoi(depth) < 1)
1216 die(_("depth %s is not a positive number"), depth);
1218 if (recurse_submodules != RECURSE_SUBMODULES_OFF) {
1219 if (recurse_submodules_default) {
1220 int arg = parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default);
1221 set_config_fetch_recurse_submodules(arg);
1223 gitmodules_config();
1224 git_config(submodule_config, NULL);
1229 die(_("fetch --all does not take a repository argument"));
1231 die(_("fetch --all does not make sense with refspecs"));
1232 (void) for_each_remote(get_one_remote_for_fetch, &list);
1233 result = fetch_multiple(&list);
1234 } else if (argc == 0) {
1235 /* No arguments -- use default remote */
1236 remote = remote_get(NULL);
1237 result = fetch_one(remote, argc, argv);
1238 } else if (multiple) {
1239 /* All arguments are assumed to be remotes or groups */
1240 for (i = 0; i < argc; i++)
1241 if (!add_remote_or_group(argv[i], &list))
1242 die(_("No such remote or remote group: %s"), argv[i]);
1243 result = fetch_multiple(&list);
1245 /* Single remote or group */
1246 (void) add_remote_or_group(argv[0], &list);
1248 /* More than one remote */
1250 die(_("Fetching a group and specifying refspecs does not make sense"));
1251 result = fetch_multiple(&list);
1253 /* Zero or one remotes */
1254 remote = remote_get(argv[0]);
1255 result = fetch_one(remote, argc-1, argv+1);
1259 if (!result && (recurse_submodules != RECURSE_SUBMODULES_OFF)) {
1260 struct argv_array options = ARGV_ARRAY_INIT;
1262 add_options_to_argv(&options);
1263 result = fetch_populated_submodules(&options,
1268 argv_array_clear(&options);
1271 /* All names were strdup()ed or strndup()ed */
1272 list.strdup_strings = 1;
1273 string_list_clear(&list, 0);
1277 argv_array_pushl(&argv_gc_auto, "gc", "--auto", NULL);
1279 argv_array_push(&argv_gc_auto, "--quiet");
1280 run_command_v_opt(argv_gc_auto.argv, RUN_GIT_CMD);
1281 argv_array_clear(&argv_gc_auto);