6 #include "repository.h"
11 #include "string-list.h"
13 #include "transport.h"
14 #include "run-command.h"
15 #include "parse-options.h"
17 #include "submodule-config.h"
18 #include "submodule.h"
19 #include "connected.h"
20 #include "argv-array.h"
23 #include "list-objects-filter-options.h"
25 static const char * const builtin_fetch_usage
[] = {
26 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
27 N_("git fetch [<options>] <group>"),
28 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
29 N_("git fetch --all [<options>]"),
39 static int fetch_prune_config
= -1; /* unspecified */
40 static int prune
= -1; /* unspecified */
41 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
43 static int fetch_prune_tags_config
= -1; /* unspecified */
44 static int prune_tags
= -1; /* unspecified */
45 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
47 static int all
, append
, dry_run
, force
, keep
, multiple
, update_head_ok
, verbosity
, deepen_relative
;
48 static int progress
= -1;
49 static int tags
= TAGS_DEFAULT
, unshallow
, update_shallow
, deepen
;
50 static int max_children
= 1;
51 static enum transport_family family
;
52 static const char *depth
;
53 static const char *deepen_since
;
54 static const char *upload_pack
;
55 static struct string_list deepen_not
= STRING_LIST_INIT_NODUP
;
56 static struct strbuf default_rla
= STRBUF_INIT
;
57 static struct transport
*gtransport
;
58 static struct transport
*gsecondary
;
59 static const char *submodule_prefix
= "";
60 static int recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
61 static int recurse_submodules_default
= RECURSE_SUBMODULES_ON_DEMAND
;
62 static int shown_url
= 0;
63 static struct refspec refmap
= REFSPEC_INIT_FETCH
;
64 static struct list_objects_filter_options filter_options
;
65 static struct string_list server_options
= STRING_LIST_INIT_DUP
;
67 static int git_fetch_config(const char *k
, const char *v
, void *cb
)
69 if (!strcmp(k
, "fetch.prune")) {
70 fetch_prune_config
= git_config_bool(k
, v
);
74 if (!strcmp(k
, "fetch.prunetags")) {
75 fetch_prune_tags_config
= git_config_bool(k
, v
);
79 if (!strcmp(k
, "submodule.recurse")) {
80 int r
= git_config_bool(k
, v
) ?
81 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
82 recurse_submodules
= r
;
85 if (!strcmp(k
, "submodule.fetchjobs")) {
86 max_children
= parse_submodule_fetchjobs(k
, v
);
88 } else if (!strcmp(k
, "fetch.recursesubmodules")) {
89 recurse_submodules
= parse_fetch_recurse_submodules_arg(k
, v
);
93 return git_default_config(k
, v
, cb
);
96 static int gitmodules_fetch_config(const char *var
, const char *value
, void *cb
)
98 if (!strcmp(var
, "submodule.fetchjobs")) {
99 max_children
= parse_submodule_fetchjobs(var
, value
);
101 } else if (!strcmp(var
, "fetch.recursesubmodules")) {
102 recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
109 static int parse_refmap_arg(const struct option
*opt
, const char *arg
, int unset
)
112 * "git fetch --refmap='' origin foo"
113 * can be used to tell the command not to store anywhere
115 refspec_append(&refmap
, arg
);
120 static struct option builtin_fetch_options
[] = {
121 OPT__VERBOSITY(&verbosity
),
122 OPT_BOOL(0, "all", &all
,
123 N_("fetch from all remotes")),
124 OPT_BOOL('a', "append", &append
,
125 N_("append to .git/FETCH_HEAD instead of overwriting")),
126 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
127 N_("path to upload pack on remote end")),
128 OPT__FORCE(&force
, N_("force overwrite of local branch"), 0),
129 OPT_BOOL('m', "multiple", &multiple
,
130 N_("fetch from multiple remotes")),
131 OPT_SET_INT('t', "tags", &tags
,
132 N_("fetch all tags and associated objects"), TAGS_SET
),
133 OPT_SET_INT('n', NULL
, &tags
,
134 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
135 OPT_INTEGER('j', "jobs", &max_children
,
136 N_("number of submodules fetched in parallel")),
137 OPT_BOOL('p', "prune", &prune
,
138 N_("prune remote-tracking branches no longer on remote")),
139 OPT_BOOL('P', "prune-tags", &prune_tags
,
140 N_("prune local tags no longer on remote and clobber changed tags")),
141 { OPTION_CALLBACK
, 0, "recurse-submodules", &recurse_submodules
, N_("on-demand"),
142 N_("control recursive fetching of submodules"),
143 PARSE_OPT_OPTARG
, option_fetch_parse_recurse_submodules
},
144 OPT_BOOL(0, "dry-run", &dry_run
,
146 OPT_BOOL('k', "keep", &keep
, N_("keep downloaded pack")),
147 OPT_BOOL('u', "update-head-ok", &update_head_ok
,
148 N_("allow updating of HEAD ref")),
149 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
150 OPT_STRING(0, "depth", &depth
, N_("depth"),
151 N_("deepen history of shallow clone")),
152 OPT_STRING(0, "shallow-since", &deepen_since
, N_("time"),
153 N_("deepen history of shallow repository based on time")),
154 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not
, N_("revision"),
155 N_("deepen history of shallow clone, excluding rev")),
156 OPT_INTEGER(0, "deepen", &deepen_relative
,
157 N_("deepen history of shallow clone")),
158 OPT_SET_INT_F(0, "unshallow", &unshallow
,
159 N_("convert to a complete repository"),
161 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
162 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
163 { OPTION_CALLBACK
, 0, "recurse-submodules-default",
164 &recurse_submodules_default
, N_("on-demand"),
165 N_("default for recursive fetching of submodules "
166 "(lower priority than config files)"),
167 PARSE_OPT_HIDDEN
, option_fetch_parse_recurse_submodules
},
168 OPT_BOOL(0, "update-shallow", &update_shallow
,
169 N_("accept refs that update .git/shallow")),
170 { OPTION_CALLBACK
, 0, "refmap", NULL
, N_("refmap"),
171 N_("specify fetch refmap"), PARSE_OPT_NONEG
, parse_refmap_arg
},
172 OPT_STRING_LIST('o', "server-option", &server_options
, N_("server-specific"), N_("option to transmit")),
173 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
174 TRANSPORT_FAMILY_IPV4
),
175 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
176 TRANSPORT_FAMILY_IPV6
),
177 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
181 static void unlock_pack(void)
184 transport_unlock_pack(gtransport
);
186 transport_unlock_pack(gsecondary
);
189 static void unlock_pack_on_signal(int signo
)
196 static void add_merge_config(struct ref
**head
,
197 const struct ref
*remote_refs
,
198 struct branch
*branch
,
203 for (i
= 0; i
< branch
->merge_nr
; i
++) {
204 struct ref
*rm
, **old_tail
= *tail
;
205 struct refspec_item refspec
;
207 for (rm
= *head
; rm
; rm
= rm
->next
) {
208 if (branch_merge_matches(branch
, i
, rm
->name
)) {
209 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
217 * Not fetched to a remote-tracking branch? We need to fetch
218 * it anyway to allow this branch's "branch.$name.merge"
219 * to be honored by 'git pull', but we do not have to
220 * fail if branch.$name.merge is misconfigured to point
221 * at a nonexisting branch. If we were indeed called by
222 * 'git pull', it will notice the misconfiguration because
223 * there is no entry in the resulting FETCH_HEAD marked
226 memset(&refspec
, 0, sizeof(refspec
));
227 refspec
.src
= branch
->merge
[i
]->src
;
228 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
229 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
230 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
234 static int add_existing(const char *refname
, const struct object_id
*oid
,
235 int flag
, void *cbdata
)
237 struct string_list
*list
= (struct string_list
*)cbdata
;
238 struct string_list_item
*item
= string_list_insert(list
, refname
);
239 struct object_id
*old_oid
= xmalloc(sizeof(*old_oid
));
241 oidcpy(old_oid
, oid
);
242 item
->util
= old_oid
;
246 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
248 struct ref
*rm
= *head
;
250 if (!hashcmp(rm
->old_oid
.hash
, sha1
))
257 static void find_non_local_tags(const struct ref
*refs
,
261 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
262 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
263 const struct ref
*ref
;
264 struct string_list_item
*item
= NULL
;
266 for_each_ref(add_existing
, &existing_refs
);
267 for (ref
= refs
; ref
; ref
= ref
->next
) {
268 if (!starts_with(ref
->name
, "refs/tags/"))
272 * The peeled ref always follows the matching base
273 * ref, so if we see a peeled ref that we don't want
274 * to fetch then we can mark the ref entry in the list
275 * as one to ignore by setting util to NULL.
277 if (ends_with(ref
->name
, "^{}")) {
279 !has_object_file_with_flags(&ref
->old_oid
,
280 OBJECT_INFO_QUICK
) &&
281 !will_fetch(head
, ref
->old_oid
.hash
) &&
282 !has_sha1_file_with_flags(item
->util
,
283 OBJECT_INFO_QUICK
) &&
284 !will_fetch(head
, item
->util
))
291 * If item is non-NULL here, then we previously saw a
292 * ref not followed by a peeled reference, so we need
293 * to check if it is a lightweight tag that we want to
297 !has_sha1_file_with_flags(item
->util
, OBJECT_INFO_QUICK
) &&
298 !will_fetch(head
, item
->util
))
303 /* skip duplicates and refs that we already have */
304 if (string_list_has_string(&remote_refs
, ref
->name
) ||
305 string_list_has_string(&existing_refs
, ref
->name
))
308 item
= string_list_insert(&remote_refs
, ref
->name
);
309 item
->util
= (void *)&ref
->old_oid
;
311 string_list_clear(&existing_refs
, 1);
314 * We may have a final lightweight tag that needs to be
315 * checked to see if it needs fetching.
318 !has_sha1_file_with_flags(item
->util
, OBJECT_INFO_QUICK
) &&
319 !will_fetch(head
, item
->util
))
323 * For all the tags in the remote_refs string list,
324 * add them to the list of refs to be fetched
326 for_each_string_list_item(item
, &remote_refs
) {
327 /* Unless we have already decided to ignore this item... */
330 struct ref
*rm
= alloc_ref(item
->string
);
331 rm
->peer_ref
= alloc_ref(item
->string
);
332 oidcpy(&rm
->old_oid
, item
->util
);
338 string_list_clear(&remote_refs
, 0);
341 static struct ref
*get_ref_map(struct remote
*remote
,
342 const struct ref
*remote_refs
,
344 int tags
, int *autotags
)
348 struct ref
*ref_map
= NULL
;
349 struct ref
**tail
= &ref_map
;
351 /* opportunistically-updated references: */
352 struct ref
*orefs
= NULL
, **oref_tail
= &orefs
;
354 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
357 struct refspec
*fetch_refspec
;
359 for (i
= 0; i
< rs
->nr
; i
++) {
360 get_fetch_map(remote_refs
, &rs
->items
[i
], &tail
, 0);
361 if (rs
->items
[i
].dst
&& rs
->items
[i
].dst
[0])
364 /* Merge everything on the command line (but not --tags) */
365 for (rm
= ref_map
; rm
; rm
= rm
->next
)
366 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
369 * For any refs that we happen to be fetching via
370 * command-line arguments, the destination ref might
371 * have been missing or have been different than the
372 * remote-tracking ref that would be derived from the
373 * configured refspec. In these cases, we want to
374 * take the opportunity to update their configured
375 * remote-tracking reference. However, we do not want
376 * to mention these entries in FETCH_HEAD at all, as
377 * they would simply be duplicates of existing
378 * entries, so we set them FETCH_HEAD_IGNORE below.
380 * We compute these entries now, based only on the
381 * refspecs specified on the command line. But we add
382 * them to the list following the refspecs resulting
383 * from the tags option so that one of the latter,
384 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
385 * by ref_remove_duplicates() in favor of one of these
386 * opportunistic entries with FETCH_HEAD_IGNORE.
389 fetch_refspec
= &refmap
;
391 fetch_refspec
= &remote
->fetch
;
393 for (i
= 0; i
< fetch_refspec
->nr
; i
++)
394 get_fetch_map(ref_map
, &fetch_refspec
->items
[i
], &oref_tail
, 1);
395 } else if (refmap
.nr
) {
396 die("--refmap option is only meaningful with command-line refspec(s).");
398 /* Use the defaults */
399 struct branch
*branch
= branch_get(NULL
);
400 int has_merge
= branch_has_merge_config(branch
);
403 /* Note: has_merge implies non-NULL branch->remote_name */
404 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
405 for (i
= 0; i
< remote
->fetch
.nr
; i
++) {
406 get_fetch_map(remote_refs
, &remote
->fetch
.items
[i
], &tail
, 0);
407 if (remote
->fetch
.items
[i
].dst
&&
408 remote
->fetch
.items
[i
].dst
[0])
410 if (!i
&& !has_merge
&& ref_map
&&
411 !remote
->fetch
.items
[0].pattern
)
412 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
415 * if the remote we're fetching from is the same
416 * as given in branch.<name>.remote, we add the
417 * ref given in branch.<name>.merge, too.
419 * Note: has_merge implies non-NULL branch->remote_name
422 !strcmp(branch
->remote_name
, remote
->name
))
423 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
425 ref_map
= get_remote_ref(remote_refs
, "HEAD");
427 die(_("Couldn't find remote ref HEAD"));
428 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
429 tail
= &ref_map
->next
;
433 if (tags
== TAGS_SET
)
434 /* also fetch all tags */
435 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
436 else if (tags
== TAGS_DEFAULT
&& *autotags
)
437 find_non_local_tags(remote_refs
, &ref_map
, &tail
);
439 /* Now append any refs to be updated opportunistically: */
441 for (rm
= orefs
; rm
; rm
= rm
->next
) {
442 rm
->fetch_head_status
= FETCH_HEAD_IGNORE
;
446 ref_map
= ref_remove_duplicates(ref_map
);
448 for_each_ref(add_existing
, &existing_refs
);
449 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
451 struct string_list_item
*peer_item
=
452 string_list_lookup(&existing_refs
,
455 struct object_id
*old_oid
= peer_item
->util
;
456 oidcpy(&rm
->peer_ref
->old_oid
, old_oid
);
460 string_list_clear(&existing_refs
, 1);
465 #define STORE_REF_ERROR_OTHER 1
466 #define STORE_REF_ERROR_DF_CONFLICT 2
468 static int s_update_ref(const char *action
,
473 char *rla
= getenv("GIT_REFLOG_ACTION");
474 struct ref_transaction
*transaction
;
475 struct strbuf err
= STRBUF_INIT
;
476 int ret
, df_conflict
= 0;
481 rla
= default_rla
.buf
;
482 msg
= xstrfmt("%s: %s", rla
, action
);
484 transaction
= ref_transaction_begin(&err
);
486 ref_transaction_update(transaction
, ref
->name
,
488 check_old
? &ref
->old_oid
: NULL
,
492 ret
= ref_transaction_commit(transaction
, &err
);
494 df_conflict
= (ret
== TRANSACTION_NAME_CONFLICT
);
498 ref_transaction_free(transaction
);
499 strbuf_release(&err
);
503 ref_transaction_free(transaction
);
504 error("%s", err
.buf
);
505 strbuf_release(&err
);
507 return df_conflict
? STORE_REF_ERROR_DF_CONFLICT
508 : STORE_REF_ERROR_OTHER
;
511 static int refcol_width
= 10;
512 static int compact_format
;
514 static void adjust_refcol_width(const struct ref
*ref
)
516 int max
, rlen
, llen
, len
;
518 /* uptodate lines are only shown on high verbosity level */
519 if (!verbosity
&& !oidcmp(&ref
->peer_ref
->old_oid
, &ref
->old_oid
))
522 max
= term_columns();
523 rlen
= utf8_strwidth(prettify_refname(ref
->name
));
525 llen
= utf8_strwidth(prettify_refname(ref
->peer_ref
->name
));
528 * rough estimation to see if the output line is too long and
529 * should not be counted (we can't do precise calculation
530 * anyway because we don't know if the error explanation part
531 * will be printed in update_local_ref)
533 if (compact_format
) {
537 len
= 21 /* flag and summary */ + rlen
+ 4 /* -> */ + llen
;
542 * Not precise calculation for compact mode because '*' can
543 * appear on the left hand side of '->' and shrink the column
546 if (refcol_width
< rlen
)
550 static void prepare_format_display(struct ref
*ref_map
)
553 const char *format
= "full";
555 git_config_get_string_const("fetch.output", &format
);
556 if (!strcasecmp(format
, "full"))
558 else if (!strcasecmp(format
, "compact"))
561 die(_("configuration fetch.output contains invalid value %s"),
564 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
565 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
||
567 !strcmp(rm
->name
, "HEAD"))
570 adjust_refcol_width(rm
);
574 static void print_remote_to_local(struct strbuf
*display
,
575 const char *remote
, const char *local
)
577 strbuf_addf(display
, "%-*s -> %s", refcol_width
, remote
, local
);
580 static int find_and_replace(struct strbuf
*haystack
,
582 const char *placeholder
)
584 const char *p
= strstr(haystack
->buf
, needle
);
590 if (p
> haystack
->buf
&& p
[-1] != '/')
594 nlen
= strlen(needle
);
595 if (plen
> nlen
&& p
[nlen
] != '/')
598 strbuf_splice(haystack
, p
- haystack
->buf
, nlen
,
599 placeholder
, strlen(placeholder
));
603 static void print_compact(struct strbuf
*display
,
604 const char *remote
, const char *local
)
606 struct strbuf r
= STRBUF_INIT
;
607 struct strbuf l
= STRBUF_INIT
;
609 if (!strcmp(remote
, local
)) {
610 strbuf_addf(display
, "%-*s -> *", refcol_width
, remote
);
614 strbuf_addstr(&r
, remote
);
615 strbuf_addstr(&l
, local
);
617 if (!find_and_replace(&r
, local
, "*"))
618 find_and_replace(&l
, remote
, "*");
619 print_remote_to_local(display
, r
.buf
, l
.buf
);
625 static void format_display(struct strbuf
*display
, char code
,
626 const char *summary
, const char *error
,
627 const char *remote
, const char *local
,
630 int width
= (summary_width
+ strlen(summary
) - gettext_width(summary
));
632 strbuf_addf(display
, "%c %-*s ", code
, width
, summary
);
634 print_remote_to_local(display
, remote
, local
);
636 print_compact(display
, remote
, local
);
638 strbuf_addf(display
, " (%s)", error
);
641 static int update_local_ref(struct ref
*ref
,
643 const struct ref
*remote_ref
,
644 struct strbuf
*display
,
647 struct commit
*current
= NULL
, *updated
;
648 enum object_type type
;
649 struct branch
*current_branch
= branch_get(NULL
);
650 const char *pretty_ref
= prettify_refname(ref
->name
);
652 type
= oid_object_info(the_repository
, &ref
->new_oid
, NULL
);
654 die(_("object %s not found"), oid_to_hex(&ref
->new_oid
));
656 if (!oidcmp(&ref
->old_oid
, &ref
->new_oid
)) {
658 format_display(display
, '=', _("[up to date]"), NULL
,
659 remote
, pretty_ref
, summary_width
);
663 if (current_branch
&&
664 !strcmp(ref
->name
, current_branch
->name
) &&
665 !(update_head_ok
|| is_bare_repository()) &&
666 !is_null_oid(&ref
->old_oid
)) {
668 * If this is the head, and it's not okay to update
669 * the head, and the old value of the head isn't empty...
671 format_display(display
, '!', _("[rejected]"),
672 _("can't fetch in current branch"),
673 remote
, pretty_ref
, summary_width
);
677 if (!is_null_oid(&ref
->old_oid
) &&
678 starts_with(ref
->name
, "refs/tags/")) {
680 r
= s_update_ref("updating tag", ref
, 0);
681 format_display(display
, r
? '!' : 't', _("[tag update]"),
682 r
? _("unable to update local ref") : NULL
,
683 remote
, pretty_ref
, summary_width
);
687 current
= lookup_commit_reference_gently(&ref
->old_oid
, 1);
688 updated
= lookup_commit_reference_gently(&ref
->new_oid
, 1);
689 if (!current
|| !updated
) {
694 * Nicely describe the new ref we're fetching.
695 * Base this on the remote's ref name, as it's
696 * more likely to follow a standard layout.
698 const char *name
= remote_ref
? remote_ref
->name
: "";
699 if (starts_with(name
, "refs/tags/")) {
701 what
= _("[new tag]");
702 } else if (starts_with(name
, "refs/heads/")) {
703 msg
= "storing head";
704 what
= _("[new branch]");
707 what
= _("[new ref]");
710 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
711 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
712 check_for_new_submodule_commits(&ref
->new_oid
);
713 r
= s_update_ref(msg
, ref
, 0);
714 format_display(display
, r
? '!' : '*', what
,
715 r
? _("unable to update local ref") : NULL
,
716 remote
, pretty_ref
, summary_width
);
720 if (in_merge_bases(current
, updated
)) {
721 struct strbuf quickref
= STRBUF_INIT
;
723 strbuf_add_unique_abbrev(&quickref
, ¤t
->object
.oid
, DEFAULT_ABBREV
);
724 strbuf_addstr(&quickref
, "..");
725 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
, DEFAULT_ABBREV
);
726 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
727 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
728 check_for_new_submodule_commits(&ref
->new_oid
);
729 r
= s_update_ref("fast-forward", ref
, 1);
730 format_display(display
, r
? '!' : ' ', quickref
.buf
,
731 r
? _("unable to update local ref") : NULL
,
732 remote
, pretty_ref
, summary_width
);
733 strbuf_release(&quickref
);
735 } else if (force
|| ref
->force
) {
736 struct strbuf quickref
= STRBUF_INIT
;
738 strbuf_add_unique_abbrev(&quickref
, ¤t
->object
.oid
, DEFAULT_ABBREV
);
739 strbuf_addstr(&quickref
, "...");
740 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
, DEFAULT_ABBREV
);
741 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
742 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
743 check_for_new_submodule_commits(&ref
->new_oid
);
744 r
= s_update_ref("forced-update", ref
, 1);
745 format_display(display
, r
? '!' : '+', quickref
.buf
,
746 r
? _("unable to update local ref") : _("forced update"),
747 remote
, pretty_ref
, summary_width
);
748 strbuf_release(&quickref
);
751 format_display(display
, '!', _("[rejected]"), _("non-fast-forward"),
752 remote
, pretty_ref
, summary_width
);
757 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
759 struct ref
**rm
= cb_data
;
760 struct ref
*ref
= *rm
;
762 while (ref
&& ref
->status
== REF_STATUS_REJECT_SHALLOW
)
765 return -1; /* end of the list */
767 oidcpy(oid
, &ref
->old_oid
);
771 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
775 struct commit
*commit
;
776 int url_len
, i
, rc
= 0;
777 struct strbuf note
= STRBUF_INIT
;
778 const char *what
, *kind
;
781 const char *filename
= dry_run
? "/dev/null" : git_path_fetch_head();
783 int summary_width
= transport_summary_width(ref_map
);
785 fp
= fopen(filename
, "a");
787 return error_errno(_("cannot open %s"), filename
);
790 url
= transport_anonymize_url(raw_url
);
792 url
= xstrdup("foreign");
795 if (check_connected(iterate_ref_map
, &rm
, NULL
)) {
796 rc
= error(_("%s did not send all necessary objects\n"), url
);
800 prepare_format_display(ref_map
);
803 * We do a pass for each fetch_head_status type in their enum order, so
804 * merged entries are written before not-for-merge. That lets readers
805 * use FETCH_HEAD as a refname to refer to the ref to be merged.
807 for (want_status
= FETCH_HEAD_MERGE
;
808 want_status
<= FETCH_HEAD_IGNORE
;
810 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
811 struct ref
*ref
= NULL
;
812 const char *merge_status_marker
= "";
814 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
) {
815 if (want_status
== FETCH_HEAD_MERGE
)
816 warning(_("reject %s because shallow roots are not allowed to be updated"),
817 rm
->peer_ref
? rm
->peer_ref
->name
: rm
->name
);
821 commit
= lookup_commit_reference_gently(&rm
->old_oid
,
824 rm
->fetch_head_status
= FETCH_HEAD_NOT_FOR_MERGE
;
826 if (rm
->fetch_head_status
!= want_status
)
830 ref
= alloc_ref(rm
->peer_ref
->name
);
831 oidcpy(&ref
->old_oid
, &rm
->peer_ref
->old_oid
);
832 oidcpy(&ref
->new_oid
, &rm
->old_oid
);
833 ref
->force
= rm
->peer_ref
->force
;
837 if (!strcmp(rm
->name
, "HEAD")) {
841 else if (starts_with(rm
->name
, "refs/heads/")) {
843 what
= rm
->name
+ 11;
845 else if (starts_with(rm
->name
, "refs/tags/")) {
847 what
= rm
->name
+ 10;
849 else if (starts_with(rm
->name
, "refs/remotes/")) {
850 kind
= "remote-tracking branch";
851 what
= rm
->name
+ 13;
858 url_len
= strlen(url
);
859 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
862 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
868 strbuf_addf(¬e
, "%s ", kind
);
869 strbuf_addf(¬e
, "'%s' of ", what
);
871 switch (rm
->fetch_head_status
) {
872 case FETCH_HEAD_NOT_FOR_MERGE
:
873 merge_status_marker
= "not-for-merge";
875 case FETCH_HEAD_MERGE
:
876 fprintf(fp
, "%s\t%s\t%s",
877 oid_to_hex(&rm
->old_oid
),
880 for (i
= 0; i
< url_len
; ++i
)
888 /* do not write anything to FETCH_HEAD */
894 rc
|= update_local_ref(ref
, what
, rm
, ¬e
,
898 format_display(¬e
, '*',
899 *kind
? kind
: "branch", NULL
,
900 *what
? what
: "HEAD",
901 "FETCH_HEAD", summary_width
);
903 if (verbosity
>= 0 && !shown_url
) {
904 fprintf(stderr
, _("From %.*s\n"),
909 fprintf(stderr
, " %s\n", note
.buf
);
914 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
915 error(_("some local refs could not be updated; try running\n"
916 " 'git remote prune %s' to remove any old, conflicting "
917 "branches"), remote_name
);
920 strbuf_release(¬e
);
927 * We would want to bypass the object transfer altogether if
928 * everything we are going to fetch already exists and is connected
931 static int quickfetch(struct ref
*ref_map
)
933 struct ref
*rm
= ref_map
;
934 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
937 * If we are deepening a shallow clone we already have these
938 * objects reachable. Running rev-list here will return with
939 * a good (0) exit status and we'll bypass the fetch that we
940 * really need to perform. Claiming failure now will ensure
941 * we perform the network exchange to deepen our history.
946 return check_connected(iterate_ref_map
, &rm
, &opt
);
949 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
,
950 struct ref
**updated_remote_refs
)
952 int ret
= quickfetch(ref_map
);
954 ret
= transport_fetch_refs(transport
, ref_map
,
955 updated_remote_refs
);
958 * Keep the new pack's ".keep" file around to allow the caller
959 * time to update refs to reference the new objects.
962 transport_unlock_pack(transport
);
966 /* Update local refs based on the ref values fetched from a remote */
967 static int consume_refs(struct transport
*transport
, struct ref
*ref_map
)
969 int ret
= store_updated_refs(transport
->url
,
970 transport
->remote
->name
,
972 transport_unlock_pack(transport
);
976 static int prune_refs(struct refspec
*rs
, struct ref
*ref_map
,
979 int url_len
, i
, result
= 0;
980 struct ref
*ref
, *stale_refs
= get_stale_heads(rs
, ref_map
);
982 int summary_width
= transport_summary_width(stale_refs
);
983 const char *dangling_msg
= dry_run
984 ? _(" (%s will become dangling)")
985 : _(" (%s has become dangling)");
988 url
= transport_anonymize_url(raw_url
);
990 url
= xstrdup("foreign");
992 url_len
= strlen(url
);
993 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
997 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
1001 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
1003 for (ref
= stale_refs
; ref
; ref
= ref
->next
)
1004 string_list_append(&refnames
, ref
->name
);
1006 result
= delete_refs("fetch: prune", &refnames
, 0);
1007 string_list_clear(&refnames
, 0);
1010 if (verbosity
>= 0) {
1011 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
1012 struct strbuf sb
= STRBUF_INIT
;
1014 fprintf(stderr
, _("From %.*s\n"), url_len
, url
);
1017 format_display(&sb
, '-', _("[deleted]"), NULL
,
1018 _("(none)"), prettify_refname(ref
->name
),
1020 fprintf(stderr
, " %s\n",sb
.buf
);
1021 strbuf_release(&sb
);
1022 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
1027 free_refs(stale_refs
);
1031 static void check_not_current_branch(struct ref
*ref_map
)
1033 struct branch
*current_branch
= branch_get(NULL
);
1035 if (is_bare_repository() || !current_branch
)
1038 for (; ref_map
; ref_map
= ref_map
->next
)
1039 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
1040 ref_map
->peer_ref
->name
))
1041 die(_("Refusing to fetch into current branch %s "
1042 "of non-bare repository"), current_branch
->refname
);
1045 static int truncate_fetch_head(void)
1047 const char *filename
= git_path_fetch_head();
1048 FILE *fp
= fopen_for_writing(filename
);
1051 return error_errno(_("cannot open %s"), filename
);
1056 static void set_option(struct transport
*transport
, const char *name
, const char *value
)
1058 int r
= transport_set_option(transport
, name
, value
);
1060 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1061 name
, value
, transport
->url
);
1063 warning(_("Option \"%s\" is ignored for %s\n"),
1064 name
, transport
->url
);
1067 static struct transport
*prepare_transport(struct remote
*remote
, int deepen
)
1069 struct transport
*transport
;
1070 transport
= transport_get(remote
, NULL
);
1071 transport_set_verbosity(transport
, verbosity
, progress
);
1072 transport
->family
= family
;
1074 set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
1076 set_option(transport
, TRANS_OPT_KEEP
, "yes");
1078 set_option(transport
, TRANS_OPT_DEPTH
, depth
);
1079 if (deepen
&& deepen_since
)
1080 set_option(transport
, TRANS_OPT_DEEPEN_SINCE
, deepen_since
);
1081 if (deepen
&& deepen_not
.nr
)
1082 set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1083 (const char *)&deepen_not
);
1084 if (deepen_relative
)
1085 set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, "yes");
1087 set_option(transport
, TRANS_OPT_UPDATE_SHALLOW
, "yes");
1088 if (filter_options
.choice
) {
1089 set_option(transport
, TRANS_OPT_LIST_OBJECTS_FILTER
,
1090 filter_options
.filter_spec
);
1091 set_option(transport
, TRANS_OPT_FROM_PROMISOR
, "1");
1096 static void backfill_tags(struct transport
*transport
, struct ref
*ref_map
)
1101 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1102 * when remote helper is used (setting it to an empty string
1103 * is not unsetting). We could extend the remote helper
1104 * protocol for that, but for now, just force a new connection
1105 * without deepen-since. Similar story for deepen-not.
1107 cannot_reuse
= transport
->cannot_reuse
||
1108 deepen_since
|| deepen_not
.nr
;
1110 gsecondary
= prepare_transport(transport
->remote
, 0);
1111 transport
= gsecondary
;
1114 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
1115 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
1116 transport_set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, NULL
);
1117 if (!fetch_refs(transport
, ref_map
, NULL
))
1118 consume_refs(transport
, ref_map
);
1121 transport_disconnect(gsecondary
);
1126 static int do_fetch(struct transport
*transport
,
1129 struct ref
*ref_map
;
1130 int autotags
= (transport
->remote
->fetch_tags
== 1);
1132 const struct ref
*remote_refs
;
1133 struct ref
*updated_remote_refs
= NULL
;
1134 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
1136 if (tags
== TAGS_DEFAULT
) {
1137 if (transport
->remote
->fetch_tags
== 2)
1139 if (transport
->remote
->fetch_tags
== -1)
1143 /* if not appending, truncate FETCH_HEAD */
1144 if (!append
&& !dry_run
) {
1145 retcode
= truncate_fetch_head();
1151 refspec_ref_prefixes(rs
, &ref_prefixes
);
1152 else if (transport
->remote
&& transport
->remote
->fetch
.nr
)
1153 refspec_ref_prefixes(&transport
->remote
->fetch
, &ref_prefixes
);
1155 if (ref_prefixes
.argc
&&
1156 (tags
== TAGS_SET
|| (tags
== TAGS_DEFAULT
&& !rs
->nr
))) {
1157 argv_array_push(&ref_prefixes
, "refs/tags/");
1160 remote_refs
= transport_get_remote_refs(transport
, &ref_prefixes
);
1161 argv_array_clear(&ref_prefixes
);
1163 ref_map
= get_ref_map(transport
->remote
, remote_refs
, rs
,
1165 if (!update_head_ok
)
1166 check_not_current_branch(ref_map
);
1168 if (tags
== TAGS_DEFAULT
&& autotags
)
1169 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1172 * We only prune based on refspecs specified
1173 * explicitly (via command line or configuration); we
1174 * don't care whether --tags was specified.
1177 prune_refs(rs
, ref_map
, transport
->url
);
1179 prune_refs(&transport
->remote
->fetch
,
1185 if (fetch_refs(transport
, ref_map
, &updated_remote_refs
)) {
1190 if (updated_remote_refs
) {
1192 * Regenerate ref_map using the updated remote refs. This is
1193 * to account for additional information which may be provided
1194 * by the transport (e.g. shallow info).
1197 ref_map
= get_ref_map(transport
->remote
, updated_remote_refs
, rs
,
1199 free_refs(updated_remote_refs
);
1201 if (consume_refs(transport
, ref_map
)) {
1208 /* if neither --no-tags nor --tags was specified, do automated tag
1210 if (tags
== TAGS_DEFAULT
&& autotags
) {
1211 struct ref
**tail
= &ref_map
;
1213 find_non_local_tags(remote_refs
, &ref_map
, &tail
);
1215 backfill_tags(transport
, ref_map
);
1223 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
1225 struct string_list
*list
= priv
;
1226 if (!remote
->skip_default_update
)
1227 string_list_append(list
, remote
->name
);
1231 struct remote_group_data
{
1233 struct string_list
*list
;
1236 static int get_remote_group(const char *key
, const char *value
, void *priv
)
1238 struct remote_group_data
*g
= priv
;
1240 if (skip_prefix(key
, "remotes.", &key
) && !strcmp(key
, g
->name
)) {
1241 /* split list by white space */
1243 size_t wordlen
= strcspn(value
, " \t\n");
1246 string_list_append_nodup(g
->list
,
1247 xstrndup(value
, wordlen
));
1248 value
+= wordlen
+ (value
[wordlen
] != '\0');
1255 static int add_remote_or_group(const char *name
, struct string_list
*list
)
1257 int prev_nr
= list
->nr
;
1258 struct remote_group_data g
;
1259 g
.name
= name
; g
.list
= list
;
1261 git_config(get_remote_group
, &g
);
1262 if (list
->nr
== prev_nr
) {
1263 struct remote
*remote
= remote_get(name
);
1264 if (!remote_is_configured(remote
, 0))
1266 string_list_append(list
, remote
->name
);
1271 static void add_options_to_argv(struct argv_array
*argv
)
1274 argv_array_push(argv
, "--dry-run");
1276 argv_array_push(argv
, prune
? "--prune" : "--no-prune");
1277 if (prune_tags
!= -1)
1278 argv_array_push(argv
, prune_tags
? "--prune-tags" : "--no-prune-tags");
1280 argv_array_push(argv
, "--update-head-ok");
1282 argv_array_push(argv
, "--force");
1284 argv_array_push(argv
, "--keep");
1285 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
1286 argv_array_push(argv
, "--recurse-submodules");
1287 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
1288 argv_array_push(argv
, "--recurse-submodules=on-demand");
1289 if (tags
== TAGS_SET
)
1290 argv_array_push(argv
, "--tags");
1291 else if (tags
== TAGS_UNSET
)
1292 argv_array_push(argv
, "--no-tags");
1294 argv_array_push(argv
, "-v");
1296 argv_array_push(argv
, "-v");
1297 else if (verbosity
< 0)
1298 argv_array_push(argv
, "-q");
1302 static int fetch_multiple(struct string_list
*list
)
1305 struct argv_array argv
= ARGV_ARRAY_INIT
;
1307 if (!append
&& !dry_run
) {
1308 int errcode
= truncate_fetch_head();
1313 argv_array_pushl(&argv
, "fetch", "--append", NULL
);
1314 add_options_to_argv(&argv
);
1316 for (i
= 0; i
< list
->nr
; i
++) {
1317 const char *name
= list
->items
[i
].string
;
1318 argv_array_push(&argv
, name
);
1320 printf(_("Fetching %s\n"), name
);
1321 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
1322 error(_("Could not fetch %s"), name
);
1325 argv_array_pop(&argv
);
1328 argv_array_clear(&argv
);
1333 * Fetching from the promisor remote should use the given filter-spec
1334 * or inherit the default filter-spec from the config.
1336 static inline void fetch_one_setup_partial(struct remote
*remote
)
1339 * Explicit --no-filter argument overrides everything, regardless
1340 * of any prior partial clones and fetches.
1342 if (filter_options
.no_filter
)
1346 * If no prior partial clone/fetch and the current fetch DID NOT
1347 * request a partial-fetch, do a normal fetch.
1349 if (!repository_format_partial_clone
&& !filter_options
.choice
)
1353 * If this is the FIRST partial-fetch request, we enable partial
1354 * on this repo and remember the given filter-spec as the default
1355 * for subsequent fetches to this remote.
1357 if (!repository_format_partial_clone
&& filter_options
.choice
) {
1358 partial_clone_register(remote
->name
, &filter_options
);
1363 * We are currently limited to only ONE promisor remote and only
1364 * allow partial-fetches from the promisor remote.
1366 if (strcmp(remote
->name
, repository_format_partial_clone
)) {
1367 if (filter_options
.choice
)
1368 die(_("--filter can only be used with the remote configured in core.partialClone"));
1373 * Do a partial-fetch from the promisor remote using either the
1374 * explicitly given filter-spec or inherit the filter-spec from
1377 if (!filter_options
.choice
)
1378 partial_clone_get_default_filter_spec(&filter_options
);
1382 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
, int prune_tags_ok
)
1384 struct refspec rs
= REFSPEC_INIT_FETCH
;
1387 int maybe_prune_tags
;
1388 int remote_via_config
= remote_is_configured(remote
, 0);
1391 die(_("No remote repository specified. Please, specify either a URL or a\n"
1392 "remote name from which new revisions should be fetched."));
1394 gtransport
= prepare_transport(remote
, 1);
1397 /* no command line request */
1398 if (0 <= remote
->prune
)
1399 prune
= remote
->prune
;
1400 else if (0 <= fetch_prune_config
)
1401 prune
= fetch_prune_config
;
1403 prune
= PRUNE_BY_DEFAULT
;
1406 if (prune_tags
< 0) {
1407 /* no command line request */
1408 if (0 <= remote
->prune_tags
)
1409 prune_tags
= remote
->prune_tags
;
1410 else if (0 <= fetch_prune_tags_config
)
1411 prune_tags
= fetch_prune_tags_config
;
1413 prune_tags
= PRUNE_TAGS_BY_DEFAULT
;
1416 maybe_prune_tags
= prune_tags_ok
&& prune_tags
;
1417 if (maybe_prune_tags
&& remote_via_config
)
1418 refspec_append(&remote
->fetch
, TAG_REFSPEC
);
1420 if (maybe_prune_tags
&& (argc
|| !remote_via_config
))
1421 refspec_append(&rs
, TAG_REFSPEC
);
1423 for (i
= 0; i
< argc
; i
++) {
1424 if (!strcmp(argv
[i
], "tag")) {
1428 die(_("You need to specify a tag name."));
1430 tag
= xstrfmt("refs/tags/%s:refs/tags/%s",
1432 refspec_append(&rs
, tag
);
1435 refspec_append(&rs
, argv
[i
]);
1439 if (server_options
.nr
)
1440 gtransport
->server_options
= &server_options
;
1442 sigchain_push_common(unlock_pack_on_signal
);
1443 atexit(unlock_pack
);
1444 exit_code
= do_fetch(gtransport
, &rs
);
1446 transport_disconnect(gtransport
);
1451 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
1454 struct string_list list
= STRING_LIST_INIT_DUP
;
1455 struct remote
*remote
= NULL
;
1457 int prune_tags_ok
= 1;
1458 struct argv_array argv_gc_auto
= ARGV_ARRAY_INIT
;
1460 packet_trace_identity("fetch");
1462 fetch_if_missing
= 0;
1464 /* Record the command line for the reflog */
1465 strbuf_addstr(&default_rla
, "fetch");
1466 for (i
= 1; i
< argc
; i
++)
1467 strbuf_addf(&default_rla
, " %s", argv
[i
]);
1469 config_from_gitmodules(gitmodules_fetch_config
, NULL
);
1470 git_config(git_fetch_config
, NULL
);
1472 argc
= parse_options(argc
, argv
, prefix
,
1473 builtin_fetch_options
, builtin_fetch_usage
, 0);
1475 if (deepen_relative
) {
1476 if (deepen_relative
< 0)
1477 die(_("Negative depth in --deepen is not supported"));
1479 die(_("--deepen and --depth are mutually exclusive"));
1480 depth
= xstrfmt("%d", deepen_relative
);
1484 die(_("--depth and --unshallow cannot be used together"));
1485 else if (!is_repository_shallow())
1486 die(_("--unshallow on a complete repository does not make sense"));
1488 depth
= xstrfmt("%d", INFINITE_DEPTH
);
1491 /* no need to be strict, transport_set_option() will validate it again */
1492 if (depth
&& atoi(depth
) < 1)
1493 die(_("depth %s is not a positive number"), depth
);
1494 if (depth
|| deepen_since
|| deepen_not
.nr
)
1497 if (filter_options
.choice
&& !repository_format_partial_clone
)
1498 die("--filter can only be used when extensions.partialClone is set");
1502 die(_("fetch --all does not take a repository argument"));
1504 die(_("fetch --all does not make sense with refspecs"));
1505 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1506 } else if (argc
== 0) {
1507 /* No arguments -- use default remote */
1508 remote
= remote_get(NULL
);
1509 } else if (multiple
) {
1510 /* All arguments are assumed to be remotes or groups */
1511 for (i
= 0; i
< argc
; i
++)
1512 if (!add_remote_or_group(argv
[i
], &list
))
1513 die(_("No such remote or remote group: %s"), argv
[i
]);
1515 /* Single remote or group */
1516 (void) add_remote_or_group(argv
[0], &list
);
1518 /* More than one remote */
1520 die(_("Fetching a group and specifying refspecs does not make sense"));
1522 /* Zero or one remotes */
1523 remote
= remote_get(argv
[0]);
1524 prune_tags_ok
= (argc
== 1);
1531 if (filter_options
.choice
|| repository_format_partial_clone
)
1532 fetch_one_setup_partial(remote
);
1533 result
= fetch_one(remote
, argc
, argv
, prune_tags_ok
);
1535 if (filter_options
.choice
)
1536 die(_("--filter can only be used with the remote configured in core.partialClone"));
1537 /* TODO should this also die if we have a previous partial-clone? */
1538 result
= fetch_multiple(&list
);
1541 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1542 struct argv_array options
= ARGV_ARRAY_INIT
;
1544 add_options_to_argv(&options
);
1545 result
= fetch_populated_submodules(the_repository
,
1549 recurse_submodules_default
,
1552 argv_array_clear(&options
);
1555 string_list_clear(&list
, 0);
1557 close_all_packs(the_repository
->objects
);
1559 argv_array_pushl(&argv_gc_auto
, "gc", "--auto", NULL
);
1561 argv_array_push(&argv_gc_auto
, "--quiet");
1562 run_command_v_opt(argv_gc_auto
.argv
, RUN_GIT_CMD
);
1563 argv_array_clear(&argv_gc_auto
);