6 #include "repository.h"
9 #include "object-store.h"
12 #include "string-list.h"
14 #include "transport.h"
15 #include "run-command.h"
16 #include "parse-options.h"
18 #include "submodule-config.h"
19 #include "submodule.h"
20 #include "connected.h"
21 #include "argv-array.h"
24 #include "list-objects-filter-options.h"
26 static const char * const builtin_fetch_usage
[] = {
27 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
28 N_("git fetch [<options>] <group>"),
29 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
30 N_("git fetch --all [<options>]"),
40 static int fetch_prune_config
= -1; /* unspecified */
41 static int prune
= -1; /* unspecified */
42 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
44 static int fetch_prune_tags_config
= -1; /* unspecified */
45 static int prune_tags
= -1; /* unspecified */
46 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
48 static int all
, append
, dry_run
, force
, keep
, multiple
, update_head_ok
, verbosity
, deepen_relative
;
49 static int progress
= -1;
50 static int tags
= TAGS_DEFAULT
, unshallow
, update_shallow
, deepen
;
51 static int max_children
= 1;
52 static enum transport_family family
;
53 static const char *depth
;
54 static const char *deepen_since
;
55 static const char *upload_pack
;
56 static struct string_list deepen_not
= STRING_LIST_INIT_NODUP
;
57 static struct strbuf default_rla
= STRBUF_INIT
;
58 static struct transport
*gtransport
;
59 static struct transport
*gsecondary
;
60 static const char *submodule_prefix
= "";
61 static int recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
62 static int recurse_submodules_default
= RECURSE_SUBMODULES_ON_DEMAND
;
63 static int shown_url
= 0;
64 static struct refspec refmap
= REFSPEC_INIT_FETCH
;
65 static struct list_objects_filter_options filter_options
;
66 static struct string_list server_options
= STRING_LIST_INIT_DUP
;
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
);
75 if (!strcmp(k
, "fetch.prunetags")) {
76 fetch_prune_tags_config
= git_config_bool(k
, v
);
80 if (!strcmp(k
, "submodule.recurse")) {
81 int r
= git_config_bool(k
, v
) ?
82 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
83 recurse_submodules
= r
;
86 if (!strcmp(k
, "submodule.fetchjobs")) {
87 max_children
= parse_submodule_fetchjobs(k
, v
);
89 } else if (!strcmp(k
, "fetch.recursesubmodules")) {
90 recurse_submodules
= parse_fetch_recurse_submodules_arg(k
, v
);
94 return git_default_config(k
, v
, cb
);
97 static int parse_refmap_arg(const struct option
*opt
, const char *arg
, int unset
)
100 * "git fetch --refmap='' origin foo"
101 * can be used to tell the command not to store anywhere
103 refspec_append(&refmap
, arg
);
108 static struct option builtin_fetch_options
[] = {
109 OPT__VERBOSITY(&verbosity
),
110 OPT_BOOL(0, "all", &all
,
111 N_("fetch from all remotes")),
112 OPT_BOOL('a', "append", &append
,
113 N_("append to .git/FETCH_HEAD instead of overwriting")),
114 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
115 N_("path to upload pack on remote end")),
116 OPT__FORCE(&force
, N_("force overwrite of local reference"), 0),
117 OPT_BOOL('m', "multiple", &multiple
,
118 N_("fetch from multiple remotes")),
119 OPT_SET_INT('t', "tags", &tags
,
120 N_("fetch all tags and associated objects"), TAGS_SET
),
121 OPT_SET_INT('n', NULL
, &tags
,
122 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
123 OPT_INTEGER('j', "jobs", &max_children
,
124 N_("number of submodules fetched in parallel")),
125 OPT_BOOL('p', "prune", &prune
,
126 N_("prune remote-tracking branches no longer on remote")),
127 OPT_BOOL('P', "prune-tags", &prune_tags
,
128 N_("prune local tags no longer on remote and clobber changed tags")),
129 { OPTION_CALLBACK
, 0, "recurse-submodules", &recurse_submodules
, N_("on-demand"),
130 N_("control recursive fetching of submodules"),
131 PARSE_OPT_OPTARG
, option_fetch_parse_recurse_submodules
},
132 OPT_BOOL(0, "dry-run", &dry_run
,
134 OPT_BOOL('k', "keep", &keep
, N_("keep downloaded pack")),
135 OPT_BOOL('u', "update-head-ok", &update_head_ok
,
136 N_("allow updating of HEAD ref")),
137 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
138 OPT_STRING(0, "depth", &depth
, N_("depth"),
139 N_("deepen history of shallow clone")),
140 OPT_STRING(0, "shallow-since", &deepen_since
, N_("time"),
141 N_("deepen history of shallow repository based on time")),
142 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not
, N_("revision"),
143 N_("deepen history of shallow clone, excluding rev")),
144 OPT_INTEGER(0, "deepen", &deepen_relative
,
145 N_("deepen history of shallow clone")),
146 OPT_SET_INT_F(0, "unshallow", &unshallow
,
147 N_("convert to a complete repository"),
149 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
150 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
151 { OPTION_CALLBACK
, 0, "recurse-submodules-default",
152 &recurse_submodules_default
, N_("on-demand"),
153 N_("default for recursive fetching of submodules "
154 "(lower priority than config files)"),
155 PARSE_OPT_HIDDEN
, option_fetch_parse_recurse_submodules
},
156 OPT_BOOL(0, "update-shallow", &update_shallow
,
157 N_("accept refs that update .git/shallow")),
158 { OPTION_CALLBACK
, 0, "refmap", NULL
, N_("refmap"),
159 N_("specify fetch refmap"), PARSE_OPT_NONEG
, parse_refmap_arg
},
160 OPT_STRING_LIST('o', "server-option", &server_options
, N_("server-specific"), N_("option to transmit")),
161 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
162 TRANSPORT_FAMILY_IPV4
),
163 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
164 TRANSPORT_FAMILY_IPV6
),
165 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
169 static void unlock_pack(void)
172 transport_unlock_pack(gtransport
);
174 transport_unlock_pack(gsecondary
);
177 static void unlock_pack_on_signal(int signo
)
184 static void add_merge_config(struct ref
**head
,
185 const struct ref
*remote_refs
,
186 struct branch
*branch
,
191 for (i
= 0; i
< branch
->merge_nr
; i
++) {
192 struct ref
*rm
, **old_tail
= *tail
;
193 struct refspec_item refspec
;
195 for (rm
= *head
; rm
; rm
= rm
->next
) {
196 if (branch_merge_matches(branch
, i
, rm
->name
)) {
197 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
205 * Not fetched to a remote-tracking branch? We need to fetch
206 * it anyway to allow this branch's "branch.$name.merge"
207 * to be honored by 'git pull', but we do not have to
208 * fail if branch.$name.merge is misconfigured to point
209 * at a nonexisting branch. If we were indeed called by
210 * 'git pull', it will notice the misconfiguration because
211 * there is no entry in the resulting FETCH_HEAD marked
214 memset(&refspec
, 0, sizeof(refspec
));
215 refspec
.src
= branch
->merge
[i
]->src
;
216 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
217 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
218 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
222 static int add_existing(const char *refname
, const struct object_id
*oid
,
223 int flag
, void *cbdata
)
225 struct string_list
*list
= (struct string_list
*)cbdata
;
226 struct string_list_item
*item
= string_list_insert(list
, refname
);
227 struct object_id
*old_oid
= xmalloc(sizeof(*old_oid
));
229 oidcpy(old_oid
, oid
);
230 item
->util
= old_oid
;
234 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
236 struct ref
*rm
= *head
;
238 if (!hashcmp(rm
->old_oid
.hash
, sha1
))
245 static void find_non_local_tags(const struct ref
*refs
,
249 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
250 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
251 const struct ref
*ref
;
252 struct string_list_item
*item
= NULL
;
254 for_each_ref(add_existing
, &existing_refs
);
255 for (ref
= refs
; ref
; ref
= ref
->next
) {
256 if (!starts_with(ref
->name
, "refs/tags/"))
260 * The peeled ref always follows the matching base
261 * ref, so if we see a peeled ref that we don't want
262 * to fetch then we can mark the ref entry in the list
263 * as one to ignore by setting util to NULL.
265 if (ends_with(ref
->name
, "^{}")) {
267 !has_object_file_with_flags(&ref
->old_oid
,
268 OBJECT_INFO_QUICK
) &&
269 !will_fetch(head
, ref
->old_oid
.hash
) &&
270 !has_sha1_file_with_flags(item
->util
,
271 OBJECT_INFO_QUICK
) &&
272 !will_fetch(head
, item
->util
))
279 * If item is non-NULL here, then we previously saw a
280 * ref not followed by a peeled reference, so we need
281 * to check if it is a lightweight tag that we want to
285 !has_sha1_file_with_flags(item
->util
, OBJECT_INFO_QUICK
) &&
286 !will_fetch(head
, item
->util
))
291 /* skip duplicates and refs that we already have */
292 if (string_list_has_string(&remote_refs
, ref
->name
) ||
293 string_list_has_string(&existing_refs
, ref
->name
))
296 item
= string_list_insert(&remote_refs
, ref
->name
);
297 item
->util
= (void *)&ref
->old_oid
;
299 string_list_clear(&existing_refs
, 1);
302 * We may have a final lightweight tag that needs to be
303 * checked to see if it needs fetching.
306 !has_sha1_file_with_flags(item
->util
, OBJECT_INFO_QUICK
) &&
307 !will_fetch(head
, item
->util
))
311 * For all the tags in the remote_refs string list,
312 * add them to the list of refs to be fetched
314 for_each_string_list_item(item
, &remote_refs
) {
315 /* Unless we have already decided to ignore this item... */
318 struct ref
*rm
= alloc_ref(item
->string
);
319 rm
->peer_ref
= alloc_ref(item
->string
);
320 oidcpy(&rm
->old_oid
, item
->util
);
326 string_list_clear(&remote_refs
, 0);
329 static struct ref
*get_ref_map(struct remote
*remote
,
330 const struct ref
*remote_refs
,
332 int tags
, int *autotags
)
336 struct ref
*ref_map
= NULL
;
337 struct ref
**tail
= &ref_map
;
339 /* opportunistically-updated references: */
340 struct ref
*orefs
= NULL
, **oref_tail
= &orefs
;
342 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
345 struct refspec
*fetch_refspec
;
347 for (i
= 0; i
< rs
->nr
; i
++) {
348 get_fetch_map(remote_refs
, &rs
->items
[i
], &tail
, 0);
349 if (rs
->items
[i
].dst
&& rs
->items
[i
].dst
[0])
352 /* Merge everything on the command line (but not --tags) */
353 for (rm
= ref_map
; rm
; rm
= rm
->next
)
354 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
357 * For any refs that we happen to be fetching via
358 * command-line arguments, the destination ref might
359 * have been missing or have been different than the
360 * remote-tracking ref that would be derived from the
361 * configured refspec. In these cases, we want to
362 * take the opportunity to update their configured
363 * remote-tracking reference. However, we do not want
364 * to mention these entries in FETCH_HEAD at all, as
365 * they would simply be duplicates of existing
366 * entries, so we set them FETCH_HEAD_IGNORE below.
368 * We compute these entries now, based only on the
369 * refspecs specified on the command line. But we add
370 * them to the list following the refspecs resulting
371 * from the tags option so that one of the latter,
372 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
373 * by ref_remove_duplicates() in favor of one of these
374 * opportunistic entries with FETCH_HEAD_IGNORE.
377 fetch_refspec
= &refmap
;
379 fetch_refspec
= &remote
->fetch
;
381 for (i
= 0; i
< fetch_refspec
->nr
; i
++)
382 get_fetch_map(ref_map
, &fetch_refspec
->items
[i
], &oref_tail
, 1);
383 } else if (refmap
.nr
) {
384 die("--refmap option is only meaningful with command-line refspec(s).");
386 /* Use the defaults */
387 struct branch
*branch
= branch_get(NULL
);
388 int has_merge
= branch_has_merge_config(branch
);
391 /* Note: has_merge implies non-NULL branch->remote_name */
392 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
393 for (i
= 0; i
< remote
->fetch
.nr
; i
++) {
394 get_fetch_map(remote_refs
, &remote
->fetch
.items
[i
], &tail
, 0);
395 if (remote
->fetch
.items
[i
].dst
&&
396 remote
->fetch
.items
[i
].dst
[0])
398 if (!i
&& !has_merge
&& ref_map
&&
399 !remote
->fetch
.items
[0].pattern
)
400 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
403 * if the remote we're fetching from is the same
404 * as given in branch.<name>.remote, we add the
405 * ref given in branch.<name>.merge, too.
407 * Note: has_merge implies non-NULL branch->remote_name
410 !strcmp(branch
->remote_name
, remote
->name
))
411 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
413 ref_map
= get_remote_ref(remote_refs
, "HEAD");
415 die(_("Couldn't find remote ref HEAD"));
416 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
417 tail
= &ref_map
->next
;
421 if (tags
== TAGS_SET
)
422 /* also fetch all tags */
423 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
424 else if (tags
== TAGS_DEFAULT
&& *autotags
)
425 find_non_local_tags(remote_refs
, &ref_map
, &tail
);
427 /* Now append any refs to be updated opportunistically: */
429 for (rm
= orefs
; rm
; rm
= rm
->next
) {
430 rm
->fetch_head_status
= FETCH_HEAD_IGNORE
;
434 ref_map
= ref_remove_duplicates(ref_map
);
436 for_each_ref(add_existing
, &existing_refs
);
437 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
439 struct string_list_item
*peer_item
=
440 string_list_lookup(&existing_refs
,
443 struct object_id
*old_oid
= peer_item
->util
;
444 oidcpy(&rm
->peer_ref
->old_oid
, old_oid
);
448 string_list_clear(&existing_refs
, 1);
453 #define STORE_REF_ERROR_OTHER 1
454 #define STORE_REF_ERROR_DF_CONFLICT 2
456 static int s_update_ref(const char *action
,
461 char *rla
= getenv("GIT_REFLOG_ACTION");
462 struct ref_transaction
*transaction
;
463 struct strbuf err
= STRBUF_INIT
;
464 int ret
, df_conflict
= 0;
469 rla
= default_rla
.buf
;
470 msg
= xstrfmt("%s: %s", rla
, action
);
472 transaction
= ref_transaction_begin(&err
);
474 ref_transaction_update(transaction
, ref
->name
,
476 check_old
? &ref
->old_oid
: NULL
,
480 ret
= ref_transaction_commit(transaction
, &err
);
482 df_conflict
= (ret
== TRANSACTION_NAME_CONFLICT
);
486 ref_transaction_free(transaction
);
487 strbuf_release(&err
);
491 ref_transaction_free(transaction
);
492 error("%s", err
.buf
);
493 strbuf_release(&err
);
495 return df_conflict
? STORE_REF_ERROR_DF_CONFLICT
496 : STORE_REF_ERROR_OTHER
;
499 static int refcol_width
= 10;
500 static int compact_format
;
502 static void adjust_refcol_width(const struct ref
*ref
)
504 int max
, rlen
, llen
, len
;
506 /* uptodate lines are only shown on high verbosity level */
507 if (!verbosity
&& !oidcmp(&ref
->peer_ref
->old_oid
, &ref
->old_oid
))
510 max
= term_columns();
511 rlen
= utf8_strwidth(prettify_refname(ref
->name
));
513 llen
= utf8_strwidth(prettify_refname(ref
->peer_ref
->name
));
516 * rough estimation to see if the output line is too long and
517 * should not be counted (we can't do precise calculation
518 * anyway because we don't know if the error explanation part
519 * will be printed in update_local_ref)
521 if (compact_format
) {
525 len
= 21 /* flag and summary */ + rlen
+ 4 /* -> */ + llen
;
530 * Not precise calculation for compact mode because '*' can
531 * appear on the left hand side of '->' and shrink the column
534 if (refcol_width
< rlen
)
538 static void prepare_format_display(struct ref
*ref_map
)
541 const char *format
= "full";
543 git_config_get_string_const("fetch.output", &format
);
544 if (!strcasecmp(format
, "full"))
546 else if (!strcasecmp(format
, "compact"))
549 die(_("configuration fetch.output contains invalid value %s"),
552 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
553 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
||
555 !strcmp(rm
->name
, "HEAD"))
558 adjust_refcol_width(rm
);
562 static void print_remote_to_local(struct strbuf
*display
,
563 const char *remote
, const char *local
)
565 strbuf_addf(display
, "%-*s -> %s", refcol_width
, remote
, local
);
568 static int find_and_replace(struct strbuf
*haystack
,
570 const char *placeholder
)
572 const char *p
= strstr(haystack
->buf
, needle
);
578 if (p
> haystack
->buf
&& p
[-1] != '/')
582 nlen
= strlen(needle
);
583 if (plen
> nlen
&& p
[nlen
] != '/')
586 strbuf_splice(haystack
, p
- haystack
->buf
, nlen
,
587 placeholder
, strlen(placeholder
));
591 static void print_compact(struct strbuf
*display
,
592 const char *remote
, const char *local
)
594 struct strbuf r
= STRBUF_INIT
;
595 struct strbuf l
= STRBUF_INIT
;
597 if (!strcmp(remote
, local
)) {
598 strbuf_addf(display
, "%-*s -> *", refcol_width
, remote
);
602 strbuf_addstr(&r
, remote
);
603 strbuf_addstr(&l
, local
);
605 if (!find_and_replace(&r
, local
, "*"))
606 find_and_replace(&l
, remote
, "*");
607 print_remote_to_local(display
, r
.buf
, l
.buf
);
613 static void format_display(struct strbuf
*display
, char code
,
614 const char *summary
, const char *error
,
615 const char *remote
, const char *local
,
618 int width
= (summary_width
+ strlen(summary
) - gettext_width(summary
));
620 strbuf_addf(display
, "%c %-*s ", code
, width
, summary
);
622 print_remote_to_local(display
, remote
, local
);
624 print_compact(display
, remote
, local
);
626 strbuf_addf(display
, " (%s)", error
);
629 static int update_local_ref(struct ref
*ref
,
631 const struct ref
*remote_ref
,
632 struct strbuf
*display
,
635 struct commit
*current
= NULL
, *updated
;
636 enum object_type type
;
637 struct branch
*current_branch
= branch_get(NULL
);
638 const char *pretty_ref
= prettify_refname(ref
->name
);
640 type
= oid_object_info(the_repository
, &ref
->new_oid
, NULL
);
642 die(_("object %s not found"), oid_to_hex(&ref
->new_oid
));
644 if (!oidcmp(&ref
->old_oid
, &ref
->new_oid
)) {
646 format_display(display
, '=', _("[up to date]"), NULL
,
647 remote
, pretty_ref
, summary_width
);
651 if (current_branch
&&
652 !strcmp(ref
->name
, current_branch
->name
) &&
653 !(update_head_ok
|| is_bare_repository()) &&
654 !is_null_oid(&ref
->old_oid
)) {
656 * If this is the head, and it's not okay to update
657 * the head, and the old value of the head isn't empty...
659 format_display(display
, '!', _("[rejected]"),
660 _("can't fetch in current branch"),
661 remote
, pretty_ref
, summary_width
);
665 if (!is_null_oid(&ref
->old_oid
) &&
666 starts_with(ref
->name
, "refs/tags/")) {
667 if (force
|| ref
->force
) {
669 r
= s_update_ref("updating tag", ref
, 0);
670 format_display(display
, r
? '!' : 't', _("[tag update]"),
671 r
? _("unable to update local ref") : NULL
,
672 remote
, pretty_ref
, summary_width
);
675 format_display(display
, '!', _("[rejected]"), _("would clobber existing tag"),
676 remote
, pretty_ref
, summary_width
);
681 current
= lookup_commit_reference_gently(&ref
->old_oid
, 1);
682 updated
= lookup_commit_reference_gently(&ref
->new_oid
, 1);
683 if (!current
|| !updated
) {
688 * Nicely describe the new ref we're fetching.
689 * Base this on the remote's ref name, as it's
690 * more likely to follow a standard layout.
692 const char *name
= remote_ref
? remote_ref
->name
: "";
693 if (starts_with(name
, "refs/tags/")) {
695 what
= _("[new tag]");
696 } else if (starts_with(name
, "refs/heads/")) {
697 msg
= "storing head";
698 what
= _("[new branch]");
701 what
= _("[new ref]");
704 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
705 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
706 check_for_new_submodule_commits(&ref
->new_oid
);
707 r
= s_update_ref(msg
, ref
, 0);
708 format_display(display
, r
? '!' : '*', what
,
709 r
? _("unable to update local ref") : NULL
,
710 remote
, pretty_ref
, summary_width
);
714 if (in_merge_bases(current
, updated
)) {
715 struct strbuf quickref
= STRBUF_INIT
;
717 strbuf_add_unique_abbrev(&quickref
, ¤t
->object
.oid
, DEFAULT_ABBREV
);
718 strbuf_addstr(&quickref
, "..");
719 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
, DEFAULT_ABBREV
);
720 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
721 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
722 check_for_new_submodule_commits(&ref
->new_oid
);
723 r
= s_update_ref("fast-forward", ref
, 1);
724 format_display(display
, r
? '!' : ' ', quickref
.buf
,
725 r
? _("unable to update local ref") : NULL
,
726 remote
, pretty_ref
, summary_width
);
727 strbuf_release(&quickref
);
729 } else if (force
|| ref
->force
) {
730 struct strbuf quickref
= STRBUF_INIT
;
732 strbuf_add_unique_abbrev(&quickref
, ¤t
->object
.oid
, DEFAULT_ABBREV
);
733 strbuf_addstr(&quickref
, "...");
734 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
, DEFAULT_ABBREV
);
735 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
736 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
737 check_for_new_submodule_commits(&ref
->new_oid
);
738 r
= s_update_ref("forced-update", ref
, 1);
739 format_display(display
, r
? '!' : '+', quickref
.buf
,
740 r
? _("unable to update local ref") : _("forced update"),
741 remote
, pretty_ref
, summary_width
);
742 strbuf_release(&quickref
);
745 format_display(display
, '!', _("[rejected]"), _("non-fast-forward"),
746 remote
, pretty_ref
, summary_width
);
751 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
753 struct ref
**rm
= cb_data
;
754 struct ref
*ref
= *rm
;
756 while (ref
&& ref
->status
== REF_STATUS_REJECT_SHALLOW
)
759 return -1; /* end of the list */
761 oidcpy(oid
, &ref
->old_oid
);
765 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
766 int connectivity_checked
, struct ref
*ref_map
)
769 struct commit
*commit
;
770 int url_len
, i
, rc
= 0;
771 struct strbuf note
= STRBUF_INIT
;
772 const char *what
, *kind
;
775 const char *filename
= dry_run
? "/dev/null" : git_path_fetch_head(the_repository
);
777 int summary_width
= transport_summary_width(ref_map
);
779 fp
= fopen(filename
, "a");
781 return error_errno(_("cannot open %s"), filename
);
784 url
= transport_anonymize_url(raw_url
);
786 url
= xstrdup("foreign");
788 if (!connectivity_checked
) {
790 if (check_connected(iterate_ref_map
, &rm
, NULL
)) {
791 rc
= error(_("%s did not send all necessary objects\n"), url
);
796 prepare_format_display(ref_map
);
799 * We do a pass for each fetch_head_status type in their enum order, so
800 * merged entries are written before not-for-merge. That lets readers
801 * use FETCH_HEAD as a refname to refer to the ref to be merged.
803 for (want_status
= FETCH_HEAD_MERGE
;
804 want_status
<= FETCH_HEAD_IGNORE
;
806 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
807 struct ref
*ref
= NULL
;
808 const char *merge_status_marker
= "";
810 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
) {
811 if (want_status
== FETCH_HEAD_MERGE
)
812 warning(_("reject %s because shallow roots are not allowed to be updated"),
813 rm
->peer_ref
? rm
->peer_ref
->name
: rm
->name
);
817 commit
= lookup_commit_reference_gently(&rm
->old_oid
,
820 rm
->fetch_head_status
= FETCH_HEAD_NOT_FOR_MERGE
;
822 if (rm
->fetch_head_status
!= want_status
)
826 ref
= alloc_ref(rm
->peer_ref
->name
);
827 oidcpy(&ref
->old_oid
, &rm
->peer_ref
->old_oid
);
828 oidcpy(&ref
->new_oid
, &rm
->old_oid
);
829 ref
->force
= rm
->peer_ref
->force
;
833 if (!strcmp(rm
->name
, "HEAD")) {
837 else if (starts_with(rm
->name
, "refs/heads/")) {
839 what
= rm
->name
+ 11;
841 else if (starts_with(rm
->name
, "refs/tags/")) {
843 what
= rm
->name
+ 10;
845 else if (starts_with(rm
->name
, "refs/remotes/")) {
846 kind
= "remote-tracking branch";
847 what
= rm
->name
+ 13;
854 url_len
= strlen(url
);
855 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
858 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
864 strbuf_addf(¬e
, "%s ", kind
);
865 strbuf_addf(¬e
, "'%s' of ", what
);
867 switch (rm
->fetch_head_status
) {
868 case FETCH_HEAD_NOT_FOR_MERGE
:
869 merge_status_marker
= "not-for-merge";
871 case FETCH_HEAD_MERGE
:
872 fprintf(fp
, "%s\t%s\t%s",
873 oid_to_hex(&rm
->old_oid
),
876 for (i
= 0; i
< url_len
; ++i
)
884 /* do not write anything to FETCH_HEAD */
890 rc
|= update_local_ref(ref
, what
, rm
, ¬e
,
894 format_display(¬e
, '*',
895 *kind
? kind
: "branch", NULL
,
896 *what
? what
: "HEAD",
897 "FETCH_HEAD", summary_width
);
899 if (verbosity
>= 0 && !shown_url
) {
900 fprintf(stderr
, _("From %.*s\n"),
905 fprintf(stderr
, " %s\n", note
.buf
);
910 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
911 error(_("some local refs could not be updated; try running\n"
912 " 'git remote prune %s' to remove any old, conflicting "
913 "branches"), remote_name
);
916 strbuf_release(¬e
);
923 * We would want to bypass the object transfer altogether if
924 * everything we are going to fetch already exists and is connected
927 static int quickfetch(struct ref
*ref_map
)
929 struct ref
*rm
= ref_map
;
930 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
933 * If we are deepening a shallow clone we already have these
934 * objects reachable. Running rev-list here will return with
935 * a good (0) exit status and we'll bypass the fetch that we
936 * really need to perform. Claiming failure now will ensure
937 * we perform the network exchange to deepen our history.
942 return check_connected(iterate_ref_map
, &rm
, &opt
);
945 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
,
946 struct ref
**updated_remote_refs
)
948 int ret
= quickfetch(ref_map
);
950 ret
= transport_fetch_refs(transport
, ref_map
,
951 updated_remote_refs
);
954 * Keep the new pack's ".keep" file around to allow the caller
955 * time to update refs to reference the new objects.
958 transport_unlock_pack(transport
);
962 /* Update local refs based on the ref values fetched from a remote */
963 static int consume_refs(struct transport
*transport
, struct ref
*ref_map
)
965 int connectivity_checked
= transport
->smart_options
966 ? transport
->smart_options
->connectivity_checked
: 0;
967 int ret
= store_updated_refs(transport
->url
,
968 transport
->remote
->name
,
969 connectivity_checked
,
971 transport_unlock_pack(transport
);
975 static int prune_refs(struct refspec
*rs
, struct ref
*ref_map
,
978 int url_len
, i
, result
= 0;
979 struct ref
*ref
, *stale_refs
= get_stale_heads(rs
, ref_map
);
981 int summary_width
= transport_summary_width(stale_refs
);
982 const char *dangling_msg
= dry_run
983 ? _(" (%s will become dangling)")
984 : _(" (%s has become dangling)");
987 url
= transport_anonymize_url(raw_url
);
989 url
= xstrdup("foreign");
991 url_len
= strlen(url
);
992 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
996 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
1000 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
1002 for (ref
= stale_refs
; ref
; ref
= ref
->next
)
1003 string_list_append(&refnames
, ref
->name
);
1005 result
= delete_refs("fetch: prune", &refnames
, 0);
1006 string_list_clear(&refnames
, 0);
1009 if (verbosity
>= 0) {
1010 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
1011 struct strbuf sb
= STRBUF_INIT
;
1013 fprintf(stderr
, _("From %.*s\n"), url_len
, url
);
1016 format_display(&sb
, '-', _("[deleted]"), NULL
,
1017 _("(none)"), prettify_refname(ref
->name
),
1019 fprintf(stderr
, " %s\n",sb
.buf
);
1020 strbuf_release(&sb
);
1021 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
1026 free_refs(stale_refs
);
1030 static void check_not_current_branch(struct ref
*ref_map
)
1032 struct branch
*current_branch
= branch_get(NULL
);
1034 if (is_bare_repository() || !current_branch
)
1037 for (; ref_map
; ref_map
= ref_map
->next
)
1038 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
1039 ref_map
->peer_ref
->name
))
1040 die(_("Refusing to fetch into current branch %s "
1041 "of non-bare repository"), current_branch
->refname
);
1044 static int truncate_fetch_head(void)
1046 const char *filename
= git_path_fetch_head(the_repository
);
1047 FILE *fp
= fopen_for_writing(filename
);
1050 return error_errno(_("cannot open %s"), filename
);
1055 static void set_option(struct transport
*transport
, const char *name
, const char *value
)
1057 int r
= transport_set_option(transport
, name
, value
);
1059 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1060 name
, value
, transport
->url
);
1062 warning(_("Option \"%s\" is ignored for %s\n"),
1063 name
, transport
->url
);
1066 static struct transport
*prepare_transport(struct remote
*remote
, int deepen
)
1068 struct transport
*transport
;
1069 transport
= transport_get(remote
, NULL
);
1070 transport_set_verbosity(transport
, verbosity
, progress
);
1071 transport
->family
= family
;
1073 set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
1075 set_option(transport
, TRANS_OPT_KEEP
, "yes");
1077 set_option(transport
, TRANS_OPT_DEPTH
, depth
);
1078 if (deepen
&& deepen_since
)
1079 set_option(transport
, TRANS_OPT_DEEPEN_SINCE
, deepen_since
);
1080 if (deepen
&& deepen_not
.nr
)
1081 set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1082 (const char *)&deepen_not
);
1083 if (deepen_relative
)
1084 set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, "yes");
1086 set_option(transport
, TRANS_OPT_UPDATE_SHALLOW
, "yes");
1087 if (filter_options
.choice
) {
1088 set_option(transport
, TRANS_OPT_LIST_OBJECTS_FILTER
,
1089 filter_options
.filter_spec
);
1090 set_option(transport
, TRANS_OPT_FROM_PROMISOR
, "1");
1095 static void backfill_tags(struct transport
*transport
, struct ref
*ref_map
)
1100 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1101 * when remote helper is used (setting it to an empty string
1102 * is not unsetting). We could extend the remote helper
1103 * protocol for that, but for now, just force a new connection
1104 * without deepen-since. Similar story for deepen-not.
1106 cannot_reuse
= transport
->cannot_reuse
||
1107 deepen_since
|| deepen_not
.nr
;
1109 gsecondary
= prepare_transport(transport
->remote
, 0);
1110 transport
= gsecondary
;
1113 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
1114 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
1115 transport_set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, NULL
);
1116 if (!fetch_refs(transport
, ref_map
, NULL
))
1117 consume_refs(transport
, ref_map
);
1120 transport_disconnect(gsecondary
);
1125 static int do_fetch(struct transport
*transport
,
1128 struct ref
*ref_map
;
1129 int autotags
= (transport
->remote
->fetch_tags
== 1);
1131 const struct ref
*remote_refs
;
1132 struct ref
*updated_remote_refs
= NULL
;
1133 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
1135 if (tags
== TAGS_DEFAULT
) {
1136 if (transport
->remote
->fetch_tags
== 2)
1138 if (transport
->remote
->fetch_tags
== -1)
1142 /* if not appending, truncate FETCH_HEAD */
1143 if (!append
&& !dry_run
) {
1144 retcode
= truncate_fetch_head();
1150 refspec_ref_prefixes(rs
, &ref_prefixes
);
1151 else if (transport
->remote
&& transport
->remote
->fetch
.nr
)
1152 refspec_ref_prefixes(&transport
->remote
->fetch
, &ref_prefixes
);
1154 if (ref_prefixes
.argc
&&
1155 (tags
== TAGS_SET
|| (tags
== TAGS_DEFAULT
&& !rs
->nr
))) {
1156 argv_array_push(&ref_prefixes
, "refs/tags/");
1159 remote_refs
= transport_get_remote_refs(transport
, &ref_prefixes
);
1160 argv_array_clear(&ref_prefixes
);
1162 ref_map
= get_ref_map(transport
->remote
, remote_refs
, rs
,
1164 if (!update_head_ok
)
1165 check_not_current_branch(ref_map
);
1167 if (tags
== TAGS_DEFAULT
&& autotags
)
1168 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1171 * We only prune based on refspecs specified
1172 * explicitly (via command line or configuration); we
1173 * don't care whether --tags was specified.
1176 prune_refs(rs
, ref_map
, transport
->url
);
1178 prune_refs(&transport
->remote
->fetch
,
1184 if (fetch_refs(transport
, ref_map
, &updated_remote_refs
)) {
1189 if (updated_remote_refs
) {
1191 * Regenerate ref_map using the updated remote refs. This is
1192 * to account for additional information which may be provided
1193 * by the transport (e.g. shallow info).
1196 ref_map
= get_ref_map(transport
->remote
, updated_remote_refs
, rs
,
1198 free_refs(updated_remote_refs
);
1200 if (consume_refs(transport
, ref_map
)) {
1207 /* if neither --no-tags nor --tags was specified, do automated tag
1209 if (tags
== TAGS_DEFAULT
&& autotags
) {
1210 struct ref
**tail
= &ref_map
;
1212 find_non_local_tags(remote_refs
, &ref_map
, &tail
);
1214 backfill_tags(transport
, ref_map
);
1222 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
1224 struct string_list
*list
= priv
;
1225 if (!remote
->skip_default_update
)
1226 string_list_append(list
, remote
->name
);
1230 struct remote_group_data
{
1232 struct string_list
*list
;
1235 static int get_remote_group(const char *key
, const char *value
, void *priv
)
1237 struct remote_group_data
*g
= priv
;
1239 if (skip_prefix(key
, "remotes.", &key
) && !strcmp(key
, g
->name
)) {
1240 /* split list by white space */
1242 size_t wordlen
= strcspn(value
, " \t\n");
1245 string_list_append_nodup(g
->list
,
1246 xstrndup(value
, wordlen
));
1247 value
+= wordlen
+ (value
[wordlen
] != '\0');
1254 static int add_remote_or_group(const char *name
, struct string_list
*list
)
1256 int prev_nr
= list
->nr
;
1257 struct remote_group_data g
;
1258 g
.name
= name
; g
.list
= list
;
1260 git_config(get_remote_group
, &g
);
1261 if (list
->nr
== prev_nr
) {
1262 struct remote
*remote
= remote_get(name
);
1263 if (!remote_is_configured(remote
, 0))
1265 string_list_append(list
, remote
->name
);
1270 static void add_options_to_argv(struct argv_array
*argv
)
1273 argv_array_push(argv
, "--dry-run");
1275 argv_array_push(argv
, prune
? "--prune" : "--no-prune");
1276 if (prune_tags
!= -1)
1277 argv_array_push(argv
, prune_tags
? "--prune-tags" : "--no-prune-tags");
1279 argv_array_push(argv
, "--update-head-ok");
1281 argv_array_push(argv
, "--force");
1283 argv_array_push(argv
, "--keep");
1284 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
1285 argv_array_push(argv
, "--recurse-submodules");
1286 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
1287 argv_array_push(argv
, "--recurse-submodules=on-demand");
1288 if (tags
== TAGS_SET
)
1289 argv_array_push(argv
, "--tags");
1290 else if (tags
== TAGS_UNSET
)
1291 argv_array_push(argv
, "--no-tags");
1293 argv_array_push(argv
, "-v");
1295 argv_array_push(argv
, "-v");
1296 else if (verbosity
< 0)
1297 argv_array_push(argv
, "-q");
1301 static int fetch_multiple(struct string_list
*list
)
1304 struct argv_array argv
= ARGV_ARRAY_INIT
;
1306 if (!append
&& !dry_run
) {
1307 int errcode
= truncate_fetch_head();
1312 argv_array_pushl(&argv
, "fetch", "--append", NULL
);
1313 add_options_to_argv(&argv
);
1315 for (i
= 0; i
< list
->nr
; i
++) {
1316 const char *name
= list
->items
[i
].string
;
1317 argv_array_push(&argv
, name
);
1319 printf(_("Fetching %s\n"), name
);
1320 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
1321 error(_("Could not fetch %s"), name
);
1324 argv_array_pop(&argv
);
1327 argv_array_clear(&argv
);
1332 * Fetching from the promisor remote should use the given filter-spec
1333 * or inherit the default filter-spec from the config.
1335 static inline void fetch_one_setup_partial(struct remote
*remote
)
1338 * Explicit --no-filter argument overrides everything, regardless
1339 * of any prior partial clones and fetches.
1341 if (filter_options
.no_filter
)
1345 * If no prior partial clone/fetch and the current fetch DID NOT
1346 * request a partial-fetch, do a normal fetch.
1348 if (!repository_format_partial_clone
&& !filter_options
.choice
)
1352 * If this is the FIRST partial-fetch request, we enable partial
1353 * on this repo and remember the given filter-spec as the default
1354 * for subsequent fetches to this remote.
1356 if (!repository_format_partial_clone
&& filter_options
.choice
) {
1357 partial_clone_register(remote
->name
, &filter_options
);
1362 * We are currently limited to only ONE promisor remote and only
1363 * allow partial-fetches from the promisor remote.
1365 if (strcmp(remote
->name
, repository_format_partial_clone
)) {
1366 if (filter_options
.choice
)
1367 die(_("--filter can only be used with the remote configured in core.partialClone"));
1372 * Do a partial-fetch from the promisor remote using either the
1373 * explicitly given filter-spec or inherit the filter-spec from
1376 if (!filter_options
.choice
)
1377 partial_clone_get_default_filter_spec(&filter_options
);
1381 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
, int prune_tags_ok
)
1383 struct refspec rs
= REFSPEC_INIT_FETCH
;
1386 int maybe_prune_tags
;
1387 int remote_via_config
= remote_is_configured(remote
, 0);
1390 die(_("No remote repository specified. Please, specify either a URL or a\n"
1391 "remote name from which new revisions should be fetched."));
1393 gtransport
= prepare_transport(remote
, 1);
1396 /* no command line request */
1397 if (0 <= remote
->prune
)
1398 prune
= remote
->prune
;
1399 else if (0 <= fetch_prune_config
)
1400 prune
= fetch_prune_config
;
1402 prune
= PRUNE_BY_DEFAULT
;
1405 if (prune_tags
< 0) {
1406 /* no command line request */
1407 if (0 <= remote
->prune_tags
)
1408 prune_tags
= remote
->prune_tags
;
1409 else if (0 <= fetch_prune_tags_config
)
1410 prune_tags
= fetch_prune_tags_config
;
1412 prune_tags
= PRUNE_TAGS_BY_DEFAULT
;
1415 maybe_prune_tags
= prune_tags_ok
&& prune_tags
;
1416 if (maybe_prune_tags
&& remote_via_config
)
1417 refspec_append(&remote
->fetch
, TAG_REFSPEC
);
1419 if (maybe_prune_tags
&& (argc
|| !remote_via_config
))
1420 refspec_append(&rs
, TAG_REFSPEC
);
1422 for (i
= 0; i
< argc
; i
++) {
1423 if (!strcmp(argv
[i
], "tag")) {
1427 die(_("You need to specify a tag name."));
1429 tag
= xstrfmt("refs/tags/%s:refs/tags/%s",
1431 refspec_append(&rs
, tag
);
1434 refspec_append(&rs
, argv
[i
]);
1438 if (server_options
.nr
)
1439 gtransport
->server_options
= &server_options
;
1441 sigchain_push_common(unlock_pack_on_signal
);
1442 atexit(unlock_pack
);
1443 exit_code
= do_fetch(gtransport
, &rs
);
1445 transport_disconnect(gtransport
);
1450 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
1453 struct string_list list
= STRING_LIST_INIT_DUP
;
1454 struct remote
*remote
= NULL
;
1456 int prune_tags_ok
= 1;
1457 struct argv_array argv_gc_auto
= ARGV_ARRAY_INIT
;
1459 packet_trace_identity("fetch");
1461 fetch_if_missing
= 0;
1463 /* Record the command line for the reflog */
1464 strbuf_addstr(&default_rla
, "fetch");
1465 for (i
= 1; i
< argc
; i
++)
1466 strbuf_addf(&default_rla
, " %s", argv
[i
]);
1468 fetch_config_from_gitmodules(&max_children
, &recurse_submodules
);
1469 git_config(git_fetch_config
, NULL
);
1471 argc
= parse_options(argc
, argv
, prefix
,
1472 builtin_fetch_options
, builtin_fetch_usage
, 0);
1474 if (deepen_relative
) {
1475 if (deepen_relative
< 0)
1476 die(_("Negative depth in --deepen is not supported"));
1478 die(_("--deepen and --depth are mutually exclusive"));
1479 depth
= xstrfmt("%d", deepen_relative
);
1483 die(_("--depth and --unshallow cannot be used together"));
1484 else if (!is_repository_shallow(the_repository
))
1485 die(_("--unshallow on a complete repository does not make sense"));
1487 depth
= xstrfmt("%d", INFINITE_DEPTH
);
1490 /* no need to be strict, transport_set_option() will validate it again */
1491 if (depth
&& atoi(depth
) < 1)
1492 die(_("depth %s is not a positive number"), depth
);
1493 if (depth
|| deepen_since
|| deepen_not
.nr
)
1496 if (filter_options
.choice
&& !repository_format_partial_clone
)
1497 die("--filter can only be used when extensions.partialClone is set");
1501 die(_("fetch --all does not take a repository argument"));
1503 die(_("fetch --all does not make sense with refspecs"));
1504 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1505 } else if (argc
== 0) {
1506 /* No arguments -- use default remote */
1507 remote
= remote_get(NULL
);
1508 } else if (multiple
) {
1509 /* All arguments are assumed to be remotes or groups */
1510 for (i
= 0; i
< argc
; i
++)
1511 if (!add_remote_or_group(argv
[i
], &list
))
1512 die(_("No such remote or remote group: %s"), argv
[i
]);
1514 /* Single remote or group */
1515 (void) add_remote_or_group(argv
[0], &list
);
1517 /* More than one remote */
1519 die(_("Fetching a group and specifying refspecs does not make sense"));
1521 /* Zero or one remotes */
1522 remote
= remote_get(argv
[0]);
1523 prune_tags_ok
= (argc
== 1);
1530 if (filter_options
.choice
|| repository_format_partial_clone
)
1531 fetch_one_setup_partial(remote
);
1532 result
= fetch_one(remote
, argc
, argv
, prune_tags_ok
);
1534 if (filter_options
.choice
)
1535 die(_("--filter can only be used with the remote configured in core.partialClone"));
1536 /* TODO should this also die if we have a previous partial-clone? */
1537 result
= fetch_multiple(&list
);
1540 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1541 struct argv_array options
= ARGV_ARRAY_INIT
;
1543 add_options_to_argv(&options
);
1544 result
= fetch_populated_submodules(the_repository
,
1548 recurse_submodules_default
,
1551 argv_array_clear(&options
);
1554 string_list_clear(&list
, 0);
1556 close_all_packs(the_repository
->objects
);
1558 argv_array_pushl(&argv_gc_auto
, "gc", "--auto", NULL
);
1560 argv_array_push(&argv_gc_auto
, "--quiet");
1561 run_command_v_opt(argv_gc_auto
.argv
, RUN_GIT_CMD
);
1562 argv_array_clear(&argv_gc_auto
);