9 #include "string-list.h"
11 #include "transport.h"
12 #include "run-command.h"
13 #include "parse-options.h"
15 #include "submodule-config.h"
16 #include "submodule.h"
17 #include "connected.h"
18 #include "argv-array.h"
21 static const char * const builtin_fetch_usage
[] = {
22 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
23 N_("git fetch [<options>] <group>"),
24 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
25 N_("git fetch --all [<options>]"),
35 static int fetch_prune_config
= -1; /* unspecified */
36 static int prune
= -1; /* unspecified */
37 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
39 static int all
, append
, dry_run
, force
, keep
, multiple
, update_head_ok
, verbosity
, deepen_relative
;
40 static int progress
= -1, recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
41 static int tags
= TAGS_DEFAULT
, unshallow
, update_shallow
, deepen
;
42 static int max_children
= -1;
43 static enum transport_family family
;
44 static const char *depth
;
45 static const char *deepen_since
;
46 static const char *upload_pack
;
47 static struct string_list deepen_not
= STRING_LIST_INIT_NODUP
;
48 static struct strbuf default_rla
= STRBUF_INIT
;
49 static struct transport
*gtransport
;
50 static struct transport
*gsecondary
;
51 static const char *submodule_prefix
= "";
52 static const char *recurse_submodules_default
;
53 static int shown_url
= 0;
54 static int refmap_alloc
, refmap_nr
;
55 static const char **refmap_array
;
57 static int option_parse_recurse_submodules(const struct option
*opt
,
58 const char *arg
, int unset
)
61 recurse_submodules
= RECURSE_SUBMODULES_OFF
;
64 recurse_submodules
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
66 recurse_submodules
= RECURSE_SUBMODULES_ON
;
71 static int git_fetch_config(const char *k
, const char *v
, void *cb
)
73 if (!strcmp(k
, "fetch.prune")) {
74 fetch_prune_config
= git_config_bool(k
, v
);
78 if (!strcmp(k
, "submodule.recurse")) {
79 int r
= git_config_bool(k
, v
) ?
80 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
81 recurse_submodules
= r
;
84 return git_default_config(k
, v
, cb
);
87 static int parse_refmap_arg(const struct option
*opt
, const char *arg
, int unset
)
89 ALLOC_GROW(refmap_array
, refmap_nr
+ 1, refmap_alloc
);
92 * "git fetch --refmap='' origin foo"
93 * can be used to tell the command not to store anywhere
96 refmap_array
[refmap_nr
++] = arg
;
100 static struct option builtin_fetch_options
[] = {
101 OPT__VERBOSITY(&verbosity
),
102 OPT_BOOL(0, "all", &all
,
103 N_("fetch from all remotes")),
104 OPT_BOOL('a', "append", &append
,
105 N_("append to .git/FETCH_HEAD instead of overwriting")),
106 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
107 N_("path to upload pack on remote end")),
108 OPT__FORCE(&force
, N_("force overwrite of local branch")),
109 OPT_BOOL('m', "multiple", &multiple
,
110 N_("fetch from multiple remotes")),
111 OPT_SET_INT('t', "tags", &tags
,
112 N_("fetch all tags and associated objects"), TAGS_SET
),
113 OPT_SET_INT('n', NULL
, &tags
,
114 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
115 OPT_INTEGER('j', "jobs", &max_children
,
116 N_("number of submodules fetched in parallel")),
117 OPT_BOOL('p', "prune", &prune
,
118 N_("prune remote-tracking branches no longer on remote")),
119 { OPTION_CALLBACK
, 0, "recurse-submodules", NULL
, N_("on-demand"),
120 N_("control recursive fetching of submodules"),
121 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
122 OPT_BOOL(0, "dry-run", &dry_run
,
124 OPT_BOOL('k', "keep", &keep
, N_("keep downloaded pack")),
125 OPT_BOOL('u', "update-head-ok", &update_head_ok
,
126 N_("allow updating of HEAD ref")),
127 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
128 OPT_STRING(0, "depth", &depth
, N_("depth"),
129 N_("deepen history of shallow clone")),
130 OPT_STRING(0, "shallow-since", &deepen_since
, N_("time"),
131 N_("deepen history of shallow repository based on time")),
132 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not
, N_("revision"),
133 N_("deepen history of shallow clone, excluding rev")),
134 OPT_INTEGER(0, "deepen", &deepen_relative
,
135 N_("deepen history of shallow clone")),
136 { OPTION_SET_INT
, 0, "unshallow", &unshallow
, NULL
,
137 N_("convert to a complete repository"),
138 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, NULL
, 1 },
139 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
140 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
141 { OPTION_STRING
, 0, "recurse-submodules-default",
142 &recurse_submodules_default
, NULL
,
143 N_("default mode for recursion"), PARSE_OPT_HIDDEN
},
144 OPT_BOOL(0, "update-shallow", &update_shallow
,
145 N_("accept refs that update .git/shallow")),
146 { OPTION_CALLBACK
, 0, "refmap", NULL
, N_("refmap"),
147 N_("specify fetch refmap"), PARSE_OPT_NONEG
, parse_refmap_arg
},
148 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
149 TRANSPORT_FAMILY_IPV4
),
150 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
151 TRANSPORT_FAMILY_IPV6
),
155 static void unlock_pack(void)
158 transport_unlock_pack(gtransport
);
160 transport_unlock_pack(gsecondary
);
163 static void unlock_pack_on_signal(int signo
)
170 static void add_merge_config(struct ref
**head
,
171 const struct ref
*remote_refs
,
172 struct branch
*branch
,
177 for (i
= 0; i
< branch
->merge_nr
; i
++) {
178 struct ref
*rm
, **old_tail
= *tail
;
179 struct refspec refspec
;
181 for (rm
= *head
; rm
; rm
= rm
->next
) {
182 if (branch_merge_matches(branch
, i
, rm
->name
)) {
183 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
191 * Not fetched to a remote-tracking branch? We need to fetch
192 * it anyway to allow this branch's "branch.$name.merge"
193 * to be honored by 'git pull', but we do not have to
194 * fail if branch.$name.merge is misconfigured to point
195 * at a nonexisting branch. If we were indeed called by
196 * 'git pull', it will notice the misconfiguration because
197 * there is no entry in the resulting FETCH_HEAD marked
200 memset(&refspec
, 0, sizeof(refspec
));
201 refspec
.src
= branch
->merge
[i
]->src
;
202 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
203 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
204 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
208 static int add_existing(const char *refname
, const struct object_id
*oid
,
209 int flag
, void *cbdata
)
211 struct string_list
*list
= (struct string_list
*)cbdata
;
212 struct string_list_item
*item
= string_list_insert(list
, refname
);
213 struct object_id
*old_oid
= xmalloc(sizeof(*old_oid
));
215 oidcpy(old_oid
, oid
);
216 item
->util
= old_oid
;
220 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
222 struct ref
*rm
= *head
;
224 if (!hashcmp(rm
->old_oid
.hash
, sha1
))
231 static void find_non_local_tags(struct transport
*transport
,
235 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
236 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
237 const struct ref
*ref
;
238 struct string_list_item
*item
= NULL
;
240 for_each_ref(add_existing
, &existing_refs
);
241 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
242 if (!starts_with(ref
->name
, "refs/tags/"))
246 * The peeled ref always follows the matching base
247 * ref, so if we see a peeled ref that we don't want
248 * to fetch then we can mark the ref entry in the list
249 * as one to ignore by setting util to NULL.
251 if (ends_with(ref
->name
, "^{}")) {
253 !has_object_file_with_flags(&ref
->old_oid
, HAS_SHA1_QUICK
) &&
254 !will_fetch(head
, ref
->old_oid
.hash
) &&
255 !has_sha1_file_with_flags(item
->util
, HAS_SHA1_QUICK
) &&
256 !will_fetch(head
, item
->util
))
263 * If item is non-NULL here, then we previously saw a
264 * ref not followed by a peeled reference, so we need
265 * to check if it is a lightweight tag that we want to
269 !has_sha1_file_with_flags(item
->util
, HAS_SHA1_QUICK
) &&
270 !will_fetch(head
, item
->util
))
275 /* skip duplicates and refs that we already have */
276 if (string_list_has_string(&remote_refs
, ref
->name
) ||
277 string_list_has_string(&existing_refs
, ref
->name
))
280 item
= string_list_insert(&remote_refs
, ref
->name
);
281 item
->util
= (void *)&ref
->old_oid
;
283 string_list_clear(&existing_refs
, 1);
286 * We may have a final lightweight tag that needs to be
287 * checked to see if it needs fetching.
290 !has_sha1_file_with_flags(item
->util
, HAS_SHA1_QUICK
) &&
291 !will_fetch(head
, item
->util
))
295 * For all the tags in the remote_refs string list,
296 * add them to the list of refs to be fetched
298 for_each_string_list_item(item
, &remote_refs
) {
299 /* Unless we have already decided to ignore this item... */
302 struct ref
*rm
= alloc_ref(item
->string
);
303 rm
->peer_ref
= alloc_ref(item
->string
);
304 oidcpy(&rm
->old_oid
, item
->util
);
310 string_list_clear(&remote_refs
, 0);
313 static struct ref
*get_ref_map(struct transport
*transport
,
314 struct refspec
*refspecs
, int refspec_count
,
315 int tags
, int *autotags
)
319 struct ref
*ref_map
= NULL
;
320 struct ref
**tail
= &ref_map
;
322 /* opportunistically-updated references: */
323 struct ref
*orefs
= NULL
, **oref_tail
= &orefs
;
325 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
328 struct refspec
*fetch_refspec
;
329 int fetch_refspec_nr
;
331 for (i
= 0; i
< refspec_count
; i
++) {
332 get_fetch_map(remote_refs
, &refspecs
[i
], &tail
, 0);
333 if (refspecs
[i
].dst
&& refspecs
[i
].dst
[0])
336 /* Merge everything on the command line (but not --tags) */
337 for (rm
= ref_map
; rm
; rm
= rm
->next
)
338 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
341 * For any refs that we happen to be fetching via
342 * command-line arguments, the destination ref might
343 * have been missing or have been different than the
344 * remote-tracking ref that would be derived from the
345 * configured refspec. In these cases, we want to
346 * take the opportunity to update their configured
347 * remote-tracking reference. However, we do not want
348 * to mention these entries in FETCH_HEAD at all, as
349 * they would simply be duplicates of existing
350 * entries, so we set them FETCH_HEAD_IGNORE below.
352 * We compute these entries now, based only on the
353 * refspecs specified on the command line. But we add
354 * them to the list following the refspecs resulting
355 * from the tags option so that one of the latter,
356 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
357 * by ref_remove_duplicates() in favor of one of these
358 * opportunistic entries with FETCH_HEAD_IGNORE.
361 fetch_refspec
= parse_fetch_refspec(refmap_nr
, refmap_array
);
362 fetch_refspec_nr
= refmap_nr
;
364 fetch_refspec
= transport
->remote
->fetch
;
365 fetch_refspec_nr
= transport
->remote
->fetch_refspec_nr
;
368 for (i
= 0; i
< fetch_refspec_nr
; i
++)
369 get_fetch_map(ref_map
, &fetch_refspec
[i
], &oref_tail
, 1);
370 } else if (refmap_array
) {
371 die("--refmap option is only meaningful with command-line refspec(s).");
373 /* Use the defaults */
374 struct remote
*remote
= transport
->remote
;
375 struct branch
*branch
= branch_get(NULL
);
376 int has_merge
= branch_has_merge_config(branch
);
378 (remote
->fetch_refspec_nr
||
379 /* Note: has_merge implies non-NULL branch->remote_name */
380 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
381 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
382 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
383 if (remote
->fetch
[i
].dst
&&
384 remote
->fetch
[i
].dst
[0])
386 if (!i
&& !has_merge
&& ref_map
&&
387 !remote
->fetch
[0].pattern
)
388 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
391 * if the remote we're fetching from is the same
392 * as given in branch.<name>.remote, we add the
393 * ref given in branch.<name>.merge, too.
395 * Note: has_merge implies non-NULL branch->remote_name
398 !strcmp(branch
->remote_name
, remote
->name
))
399 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
401 ref_map
= get_remote_ref(remote_refs
, "HEAD");
403 die(_("Couldn't find remote ref HEAD"));
404 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
405 tail
= &ref_map
->next
;
409 if (tags
== TAGS_SET
)
410 /* also fetch all tags */
411 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
412 else if (tags
== TAGS_DEFAULT
&& *autotags
)
413 find_non_local_tags(transport
, &ref_map
, &tail
);
415 /* Now append any refs to be updated opportunistically: */
417 for (rm
= orefs
; rm
; rm
= rm
->next
) {
418 rm
->fetch_head_status
= FETCH_HEAD_IGNORE
;
422 return ref_remove_duplicates(ref_map
);
425 #define STORE_REF_ERROR_OTHER 1
426 #define STORE_REF_ERROR_DF_CONFLICT 2
428 static int s_update_ref(const char *action
,
433 char *rla
= getenv("GIT_REFLOG_ACTION");
434 struct ref_transaction
*transaction
;
435 struct strbuf err
= STRBUF_INIT
;
436 int ret
, df_conflict
= 0;
441 rla
= default_rla
.buf
;
442 msg
= xstrfmt("%s: %s", rla
, action
);
444 transaction
= ref_transaction_begin(&err
);
446 ref_transaction_update(transaction
, ref
->name
,
448 check_old
? ref
->old_oid
.hash
: NULL
,
452 ret
= ref_transaction_commit(transaction
, &err
);
454 df_conflict
= (ret
== TRANSACTION_NAME_CONFLICT
);
458 ref_transaction_free(transaction
);
459 strbuf_release(&err
);
463 ref_transaction_free(transaction
);
464 error("%s", err
.buf
);
465 strbuf_release(&err
);
467 return df_conflict
? STORE_REF_ERROR_DF_CONFLICT
468 : STORE_REF_ERROR_OTHER
;
471 static int refcol_width
= 10;
472 static int compact_format
;
474 static void adjust_refcol_width(const struct ref
*ref
)
476 int max
, rlen
, llen
, len
;
478 /* uptodate lines are only shown on high verbosity level */
479 if (!verbosity
&& !oidcmp(&ref
->peer_ref
->old_oid
, &ref
->old_oid
))
482 max
= term_columns();
483 rlen
= utf8_strwidth(prettify_refname(ref
->name
));
485 llen
= utf8_strwidth(prettify_refname(ref
->peer_ref
->name
));
488 * rough estimation to see if the output line is too long and
489 * should not be counted (we can't do precise calculation
490 * anyway because we don't know if the error explanation part
491 * will be printed in update_local_ref)
493 if (compact_format
) {
497 len
= 21 /* flag and summary */ + rlen
+ 4 /* -> */ + llen
;
502 * Not precise calculation for compact mode because '*' can
503 * appear on the left hand side of '->' and shrink the column
506 if (refcol_width
< rlen
)
510 static void prepare_format_display(struct ref
*ref_map
)
513 const char *format
= "full";
515 git_config_get_string_const("fetch.output", &format
);
516 if (!strcasecmp(format
, "full"))
518 else if (!strcasecmp(format
, "compact"))
521 die(_("configuration fetch.output contains invalid value %s"),
524 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
525 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
||
527 !strcmp(rm
->name
, "HEAD"))
530 adjust_refcol_width(rm
);
534 static void print_remote_to_local(struct strbuf
*display
,
535 const char *remote
, const char *local
)
537 strbuf_addf(display
, "%-*s -> %s", refcol_width
, remote
, local
);
540 static int find_and_replace(struct strbuf
*haystack
,
542 const char *placeholder
)
544 const char *p
= strstr(haystack
->buf
, needle
);
550 if (p
> haystack
->buf
&& p
[-1] != '/')
554 nlen
= strlen(needle
);
555 if (plen
> nlen
&& p
[nlen
] != '/')
558 strbuf_splice(haystack
, p
- haystack
->buf
, nlen
,
559 placeholder
, strlen(placeholder
));
563 static void print_compact(struct strbuf
*display
,
564 const char *remote
, const char *local
)
566 struct strbuf r
= STRBUF_INIT
;
567 struct strbuf l
= STRBUF_INIT
;
569 if (!strcmp(remote
, local
)) {
570 strbuf_addf(display
, "%-*s -> *", refcol_width
, remote
);
574 strbuf_addstr(&r
, remote
);
575 strbuf_addstr(&l
, local
);
577 if (!find_and_replace(&r
, local
, "*"))
578 find_and_replace(&l
, remote
, "*");
579 print_remote_to_local(display
, r
.buf
, l
.buf
);
585 static void format_display(struct strbuf
*display
, char code
,
586 const char *summary
, const char *error
,
587 const char *remote
, const char *local
,
590 int width
= (summary_width
+ strlen(summary
) - gettext_width(summary
));
592 strbuf_addf(display
, "%c %-*s ", code
, width
, summary
);
594 print_remote_to_local(display
, remote
, local
);
596 print_compact(display
, remote
, local
);
598 strbuf_addf(display
, " (%s)", error
);
601 static int update_local_ref(struct ref
*ref
,
603 const struct ref
*remote_ref
,
604 struct strbuf
*display
,
607 struct commit
*current
= NULL
, *updated
;
608 enum object_type type
;
609 struct branch
*current_branch
= branch_get(NULL
);
610 const char *pretty_ref
= prettify_refname(ref
->name
);
612 type
= sha1_object_info(ref
->new_oid
.hash
, NULL
);
614 die(_("object %s not found"), oid_to_hex(&ref
->new_oid
));
616 if (!oidcmp(&ref
->old_oid
, &ref
->new_oid
)) {
618 format_display(display
, '=', _("[up to date]"), NULL
,
619 remote
, pretty_ref
, summary_width
);
623 if (current_branch
&&
624 !strcmp(ref
->name
, current_branch
->name
) &&
625 !(update_head_ok
|| is_bare_repository()) &&
626 !is_null_oid(&ref
->old_oid
)) {
628 * If this is the head, and it's not okay to update
629 * the head, and the old value of the head isn't empty...
631 format_display(display
, '!', _("[rejected]"),
632 _("can't fetch in current branch"),
633 remote
, pretty_ref
, summary_width
);
637 if (!is_null_oid(&ref
->old_oid
) &&
638 starts_with(ref
->name
, "refs/tags/")) {
640 r
= s_update_ref("updating tag", ref
, 0);
641 format_display(display
, r
? '!' : 't', _("[tag update]"),
642 r
? _("unable to update local ref") : NULL
,
643 remote
, pretty_ref
, summary_width
);
647 current
= lookup_commit_reference_gently(&ref
->old_oid
, 1);
648 updated
= lookup_commit_reference_gently(&ref
->new_oid
, 1);
649 if (!current
|| !updated
) {
654 * Nicely describe the new ref we're fetching.
655 * Base this on the remote's ref name, as it's
656 * more likely to follow a standard layout.
658 const char *name
= remote_ref
? remote_ref
->name
: "";
659 if (starts_with(name
, "refs/tags/")) {
661 what
= _("[new tag]");
662 } else if (starts_with(name
, "refs/heads/")) {
663 msg
= "storing head";
664 what
= _("[new branch]");
667 what
= _("[new ref]");
670 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
671 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
672 check_for_new_submodule_commits(&ref
->new_oid
);
673 r
= s_update_ref(msg
, ref
, 0);
674 format_display(display
, r
? '!' : '*', what
,
675 r
? _("unable to update local ref") : NULL
,
676 remote
, pretty_ref
, summary_width
);
680 if (in_merge_bases(current
, updated
)) {
681 struct strbuf quickref
= STRBUF_INIT
;
683 strbuf_add_unique_abbrev(&quickref
, current
->object
.oid
.hash
, DEFAULT_ABBREV
);
684 strbuf_addstr(&quickref
, "..");
685 strbuf_add_unique_abbrev(&quickref
, ref
->new_oid
.hash
, DEFAULT_ABBREV
);
686 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
687 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
688 check_for_new_submodule_commits(&ref
->new_oid
);
689 r
= s_update_ref("fast-forward", ref
, 1);
690 format_display(display
, r
? '!' : ' ', quickref
.buf
,
691 r
? _("unable to update local ref") : NULL
,
692 remote
, pretty_ref
, summary_width
);
693 strbuf_release(&quickref
);
695 } else if (force
|| ref
->force
) {
696 struct strbuf quickref
= STRBUF_INIT
;
698 strbuf_add_unique_abbrev(&quickref
, current
->object
.oid
.hash
, DEFAULT_ABBREV
);
699 strbuf_addstr(&quickref
, "...");
700 strbuf_add_unique_abbrev(&quickref
, ref
->new_oid
.hash
, DEFAULT_ABBREV
);
701 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
702 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
703 check_for_new_submodule_commits(&ref
->new_oid
);
704 r
= s_update_ref("forced-update", ref
, 1);
705 format_display(display
, r
? '!' : '+', quickref
.buf
,
706 r
? _("unable to update local ref") : _("forced update"),
707 remote
, pretty_ref
, summary_width
);
708 strbuf_release(&quickref
);
711 format_display(display
, '!', _("[rejected]"), _("non-fast-forward"),
712 remote
, pretty_ref
, summary_width
);
717 static int iterate_ref_map(void *cb_data
, unsigned char sha1
[20])
719 struct ref
**rm
= cb_data
;
720 struct ref
*ref
= *rm
;
722 while (ref
&& ref
->status
== REF_STATUS_REJECT_SHALLOW
)
725 return -1; /* end of the list */
727 hashcpy(sha1
, ref
->old_oid
.hash
);
731 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
735 struct commit
*commit
;
736 int url_len
, i
, rc
= 0;
737 struct strbuf note
= STRBUF_INIT
;
738 const char *what
, *kind
;
741 const char *filename
= dry_run
? "/dev/null" : git_path_fetch_head();
743 int summary_width
= transport_summary_width(ref_map
);
745 fp
= fopen(filename
, "a");
747 return error_errno(_("cannot open %s"), filename
);
750 url
= transport_anonymize_url(raw_url
);
752 url
= xstrdup("foreign");
755 if (check_connected(iterate_ref_map
, &rm
, NULL
)) {
756 rc
= error(_("%s did not send all necessary objects\n"), url
);
760 prepare_format_display(ref_map
);
763 * We do a pass for each fetch_head_status type in their enum order, so
764 * merged entries are written before not-for-merge. That lets readers
765 * use FETCH_HEAD as a refname to refer to the ref to be merged.
767 for (want_status
= FETCH_HEAD_MERGE
;
768 want_status
<= FETCH_HEAD_IGNORE
;
770 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
771 struct ref
*ref
= NULL
;
772 const char *merge_status_marker
= "";
774 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
) {
775 if (want_status
== FETCH_HEAD_MERGE
)
776 warning(_("reject %s because shallow roots are not allowed to be updated"),
777 rm
->peer_ref
? rm
->peer_ref
->name
: rm
->name
);
781 commit
= lookup_commit_reference_gently(&rm
->old_oid
,
784 rm
->fetch_head_status
= FETCH_HEAD_NOT_FOR_MERGE
;
786 if (rm
->fetch_head_status
!= want_status
)
790 ref
= alloc_ref(rm
->peer_ref
->name
);
791 oidcpy(&ref
->old_oid
, &rm
->peer_ref
->old_oid
);
792 oidcpy(&ref
->new_oid
, &rm
->old_oid
);
793 ref
->force
= rm
->peer_ref
->force
;
797 if (!strcmp(rm
->name
, "HEAD")) {
801 else if (starts_with(rm
->name
, "refs/heads/")) {
803 what
= rm
->name
+ 11;
805 else if (starts_with(rm
->name
, "refs/tags/")) {
807 what
= rm
->name
+ 10;
809 else if (starts_with(rm
->name
, "refs/remotes/")) {
810 kind
= "remote-tracking branch";
811 what
= rm
->name
+ 13;
818 url_len
= strlen(url
);
819 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
822 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
828 strbuf_addf(¬e
, "%s ", kind
);
829 strbuf_addf(¬e
, "'%s' of ", what
);
831 switch (rm
->fetch_head_status
) {
832 case FETCH_HEAD_NOT_FOR_MERGE
:
833 merge_status_marker
= "not-for-merge";
835 case FETCH_HEAD_MERGE
:
836 fprintf(fp
, "%s\t%s\t%s",
837 oid_to_hex(&rm
->old_oid
),
840 for (i
= 0; i
< url_len
; ++i
)
848 /* do not write anything to FETCH_HEAD */
854 rc
|= update_local_ref(ref
, what
, rm
, ¬e
,
858 format_display(¬e
, '*',
859 *kind
? kind
: "branch", NULL
,
860 *what
? what
: "HEAD",
861 "FETCH_HEAD", summary_width
);
863 if (verbosity
>= 0 && !shown_url
) {
864 fprintf(stderr
, _("From %.*s\n"),
869 fprintf(stderr
, " %s\n", note
.buf
);
874 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
875 error(_("some local refs could not be updated; try running\n"
876 " 'git remote prune %s' to remove any old, conflicting "
877 "branches"), remote_name
);
880 strbuf_release(¬e
);
887 * We would want to bypass the object transfer altogether if
888 * everything we are going to fetch already exists and is connected
891 static int quickfetch(struct ref
*ref_map
)
893 struct ref
*rm
= ref_map
;
894 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
897 * If we are deepening a shallow clone we already have these
898 * objects reachable. Running rev-list here will return with
899 * a good (0) exit status and we'll bypass the fetch that we
900 * really need to perform. Claiming failure now will ensure
901 * we perform the network exchange to deepen our history.
906 return check_connected(iterate_ref_map
, &rm
, &opt
);
909 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
911 int ret
= quickfetch(ref_map
);
913 ret
= transport_fetch_refs(transport
, ref_map
);
915 ret
|= store_updated_refs(transport
->url
,
916 transport
->remote
->name
,
918 transport_unlock_pack(transport
);
922 static int prune_refs(struct refspec
*refs
, int ref_count
, struct ref
*ref_map
,
925 int url_len
, i
, result
= 0;
926 struct ref
*ref
, *stale_refs
= get_stale_heads(refs
, ref_count
, ref_map
);
928 int summary_width
= transport_summary_width(stale_refs
);
929 const char *dangling_msg
= dry_run
930 ? _(" (%s will become dangling)")
931 : _(" (%s has become dangling)");
934 url
= transport_anonymize_url(raw_url
);
936 url
= xstrdup("foreign");
938 url_len
= strlen(url
);
939 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
943 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
947 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
949 for (ref
= stale_refs
; ref
; ref
= ref
->next
)
950 string_list_append(&refnames
, ref
->name
);
952 result
= delete_refs("fetch: prune", &refnames
, 0);
953 string_list_clear(&refnames
, 0);
956 if (verbosity
>= 0) {
957 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
958 struct strbuf sb
= STRBUF_INIT
;
960 fprintf(stderr
, _("From %.*s\n"), url_len
, url
);
963 format_display(&sb
, '-', _("[deleted]"), NULL
,
964 _("(none)"), prettify_refname(ref
->name
),
966 fprintf(stderr
, " %s\n",sb
.buf
);
968 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
973 free_refs(stale_refs
);
977 static void check_not_current_branch(struct ref
*ref_map
)
979 struct branch
*current_branch
= branch_get(NULL
);
981 if (is_bare_repository() || !current_branch
)
984 for (; ref_map
; ref_map
= ref_map
->next
)
985 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
986 ref_map
->peer_ref
->name
))
987 die(_("Refusing to fetch into current branch %s "
988 "of non-bare repository"), current_branch
->refname
);
991 static int truncate_fetch_head(void)
993 const char *filename
= git_path_fetch_head();
994 FILE *fp
= fopen_for_writing(filename
);
997 return error_errno(_("cannot open %s"), filename
);
1002 static void set_option(struct transport
*transport
, const char *name
, const char *value
)
1004 int r
= transport_set_option(transport
, name
, value
);
1006 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1007 name
, value
, transport
->url
);
1009 warning(_("Option \"%s\" is ignored for %s\n"),
1010 name
, transport
->url
);
1013 static struct transport
*prepare_transport(struct remote
*remote
, int deepen
)
1015 struct transport
*transport
;
1016 transport
= transport_get(remote
, NULL
);
1017 transport_set_verbosity(transport
, verbosity
, progress
);
1018 transport
->family
= family
;
1020 set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
1022 set_option(transport
, TRANS_OPT_KEEP
, "yes");
1024 set_option(transport
, TRANS_OPT_DEPTH
, depth
);
1025 if (deepen
&& deepen_since
)
1026 set_option(transport
, TRANS_OPT_DEEPEN_SINCE
, deepen_since
);
1027 if (deepen
&& deepen_not
.nr
)
1028 set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1029 (const char *)&deepen_not
);
1030 if (deepen_relative
)
1031 set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, "yes");
1033 set_option(transport
, TRANS_OPT_UPDATE_SHALLOW
, "yes");
1037 static void backfill_tags(struct transport
*transport
, struct ref
*ref_map
)
1042 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1043 * when remote helper is used (setting it to an empty string
1044 * is not unsetting). We could extend the remote helper
1045 * protocol for that, but for now, just force a new connection
1046 * without deepen-since. Similar story for deepen-not.
1048 cannot_reuse
= transport
->cannot_reuse
||
1049 deepen_since
|| deepen_not
.nr
;
1051 gsecondary
= prepare_transport(transport
->remote
, 0);
1052 transport
= gsecondary
;
1055 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
1056 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
1057 transport_set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, NULL
);
1058 fetch_refs(transport
, ref_map
);
1061 transport_disconnect(gsecondary
);
1066 static int do_fetch(struct transport
*transport
,
1067 struct refspec
*refs
, int ref_count
)
1069 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
1070 struct ref
*ref_map
;
1072 int autotags
= (transport
->remote
->fetch_tags
== 1);
1075 for_each_ref(add_existing
, &existing_refs
);
1077 if (tags
== TAGS_DEFAULT
) {
1078 if (transport
->remote
->fetch_tags
== 2)
1080 if (transport
->remote
->fetch_tags
== -1)
1084 if (!transport
->get_refs_list
|| !transport
->fetch
)
1085 die(_("Don't know how to fetch from %s"), transport
->url
);
1087 /* if not appending, truncate FETCH_HEAD */
1088 if (!append
&& !dry_run
) {
1089 retcode
= truncate_fetch_head();
1094 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
1095 if (!update_head_ok
)
1096 check_not_current_branch(ref_map
);
1098 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
1100 struct string_list_item
*peer_item
=
1101 string_list_lookup(&existing_refs
,
1102 rm
->peer_ref
->name
);
1104 struct object_id
*old_oid
= peer_item
->util
;
1105 oidcpy(&rm
->peer_ref
->old_oid
, old_oid
);
1110 if (tags
== TAGS_DEFAULT
&& autotags
)
1111 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1114 * We only prune based on refspecs specified
1115 * explicitly (via command line or configuration); we
1116 * don't care whether --tags was specified.
1119 prune_refs(refs
, ref_count
, ref_map
, transport
->url
);
1121 prune_refs(transport
->remote
->fetch
,
1122 transport
->remote
->fetch_refspec_nr
,
1127 if (fetch_refs(transport
, ref_map
)) {
1134 /* if neither --no-tags nor --tags was specified, do automated tag
1136 if (tags
== TAGS_DEFAULT
&& autotags
) {
1137 struct ref
**tail
= &ref_map
;
1139 find_non_local_tags(transport
, &ref_map
, &tail
);
1141 backfill_tags(transport
, ref_map
);
1146 string_list_clear(&existing_refs
, 1);
1150 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
1152 struct string_list
*list
= priv
;
1153 if (!remote
->skip_default_update
)
1154 string_list_append(list
, remote
->name
);
1158 struct remote_group_data
{
1160 struct string_list
*list
;
1163 static int get_remote_group(const char *key
, const char *value
, void *priv
)
1165 struct remote_group_data
*g
= priv
;
1167 if (skip_prefix(key
, "remotes.", &key
) && !strcmp(key
, g
->name
)) {
1168 /* split list by white space */
1170 size_t wordlen
= strcspn(value
, " \t\n");
1173 string_list_append_nodup(g
->list
,
1174 xstrndup(value
, wordlen
));
1175 value
+= wordlen
+ (value
[wordlen
] != '\0');
1182 static int add_remote_or_group(const char *name
, struct string_list
*list
)
1184 int prev_nr
= list
->nr
;
1185 struct remote_group_data g
;
1186 g
.name
= name
; g
.list
= list
;
1188 git_config(get_remote_group
, &g
);
1189 if (list
->nr
== prev_nr
) {
1190 struct remote
*remote
= remote_get(name
);
1191 if (!remote_is_configured(remote
, 0))
1193 string_list_append(list
, remote
->name
);
1198 static void add_options_to_argv(struct argv_array
*argv
)
1201 argv_array_push(argv
, "--dry-run");
1203 argv_array_push(argv
, prune
? "--prune" : "--no-prune");
1205 argv_array_push(argv
, "--update-head-ok");
1207 argv_array_push(argv
, "--force");
1209 argv_array_push(argv
, "--keep");
1210 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
1211 argv_array_push(argv
, "--recurse-submodules");
1212 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
1213 argv_array_push(argv
, "--recurse-submodules=on-demand");
1214 if (tags
== TAGS_SET
)
1215 argv_array_push(argv
, "--tags");
1216 else if (tags
== TAGS_UNSET
)
1217 argv_array_push(argv
, "--no-tags");
1219 argv_array_push(argv
, "-v");
1221 argv_array_push(argv
, "-v");
1222 else if (verbosity
< 0)
1223 argv_array_push(argv
, "-q");
1227 static int fetch_multiple(struct string_list
*list
)
1230 struct argv_array argv
= ARGV_ARRAY_INIT
;
1232 if (!append
&& !dry_run
) {
1233 int errcode
= truncate_fetch_head();
1238 argv_array_pushl(&argv
, "fetch", "--append", NULL
);
1239 add_options_to_argv(&argv
);
1241 for (i
= 0; i
< list
->nr
; i
++) {
1242 const char *name
= list
->items
[i
].string
;
1243 argv_array_push(&argv
, name
);
1245 printf(_("Fetching %s\n"), name
);
1246 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
1247 error(_("Could not fetch %s"), name
);
1250 argv_array_pop(&argv
);
1253 argv_array_clear(&argv
);
1257 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
1259 static const char **refs
= NULL
;
1260 struct refspec
*refspec
;
1265 die(_("No remote repository specified. Please, specify either a URL or a\n"
1266 "remote name from which new revisions should be fetched."));
1268 gtransport
= prepare_transport(remote
, 1);
1271 /* no command line request */
1272 if (0 <= gtransport
->remote
->prune
)
1273 prune
= gtransport
->remote
->prune
;
1274 else if (0 <= fetch_prune_config
)
1275 prune
= fetch_prune_config
;
1277 prune
= PRUNE_BY_DEFAULT
;
1283 refs
= xcalloc(st_add(argc
, 1), sizeof(const char *));
1284 for (i
= 0; i
< argc
; i
++) {
1285 if (!strcmp(argv
[i
], "tag")) {
1288 die(_("You need to specify a tag name."));
1289 refs
[j
++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1292 refs
[j
++] = argv
[i
];
1298 sigchain_push_common(unlock_pack_on_signal
);
1299 atexit(unlock_pack
);
1300 refspec
= parse_fetch_refspec(ref_nr
, refs
);
1301 exit_code
= do_fetch(gtransport
, refspec
, ref_nr
);
1302 free_refspec(ref_nr
, refspec
);
1303 transport_disconnect(gtransport
);
1308 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
1311 struct string_list list
= STRING_LIST_INIT_DUP
;
1312 struct remote
*remote
;
1314 struct argv_array argv_gc_auto
= ARGV_ARRAY_INIT
;
1316 packet_trace_identity("fetch");
1318 /* Record the command line for the reflog */
1319 strbuf_addstr(&default_rla
, "fetch");
1320 for (i
= 1; i
< argc
; i
++)
1321 strbuf_addf(&default_rla
, " %s", argv
[i
]);
1323 git_config(git_fetch_config
, NULL
);
1325 argc
= parse_options(argc
, argv
, prefix
,
1326 builtin_fetch_options
, builtin_fetch_usage
, 0);
1328 if (deepen_relative
) {
1329 if (deepen_relative
< 0)
1330 die(_("Negative depth in --deepen is not supported"));
1332 die(_("--deepen and --depth are mutually exclusive"));
1333 depth
= xstrfmt("%d", deepen_relative
);
1337 die(_("--depth and --unshallow cannot be used together"));
1338 else if (!is_repository_shallow())
1339 die(_("--unshallow on a complete repository does not make sense"));
1341 depth
= xstrfmt("%d", INFINITE_DEPTH
);
1344 /* no need to be strict, transport_set_option() will validate it again */
1345 if (depth
&& atoi(depth
) < 1)
1346 die(_("depth %s is not a positive number"), depth
);
1347 if (depth
|| deepen_since
|| deepen_not
.nr
)
1350 if (recurse_submodules
!= RECURSE_SUBMODULES_OFF
) {
1351 if (recurse_submodules_default
) {
1352 int arg
= parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default
);
1353 set_config_fetch_recurse_submodules(arg
);
1355 gitmodules_config();
1356 git_config(submodule_config
, NULL
);
1361 die(_("fetch --all does not take a repository argument"));
1363 die(_("fetch --all does not make sense with refspecs"));
1364 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1365 result
= fetch_multiple(&list
);
1366 } else if (argc
== 0) {
1367 /* No arguments -- use default remote */
1368 remote
= remote_get(NULL
);
1369 result
= fetch_one(remote
, argc
, argv
);
1370 } else if (multiple
) {
1371 /* All arguments are assumed to be remotes or groups */
1372 for (i
= 0; i
< argc
; i
++)
1373 if (!add_remote_or_group(argv
[i
], &list
))
1374 die(_("No such remote or remote group: %s"), argv
[i
]);
1375 result
= fetch_multiple(&list
);
1377 /* Single remote or group */
1378 (void) add_remote_or_group(argv
[0], &list
);
1380 /* More than one remote */
1382 die(_("Fetching a group and specifying refspecs does not make sense"));
1383 result
= fetch_multiple(&list
);
1385 /* Zero or one remotes */
1386 remote
= remote_get(argv
[0]);
1387 result
= fetch_one(remote
, argc
-1, argv
+1);
1391 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1392 struct argv_array options
= ARGV_ARRAY_INIT
;
1394 add_options_to_argv(&options
);
1395 result
= fetch_populated_submodules(&options
,
1400 argv_array_clear(&options
);
1403 string_list_clear(&list
, 0);
1407 argv_array_pushl(&argv_gc_auto
, "gc", "--auto", NULL
);
1409 argv_array_push(&argv_gc_auto
, "--quiet");
1410 run_command_v_opt(argv_gc_auto
.argv
, RUN_GIT_CMD
);
1411 argv_array_clear(&argv_gc_auto
);