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"
25 #include "commit-reach.h"
27 #include "promisor-remote.h"
28 #include "commit-graph.h"
30 #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
32 static const char * const builtin_fetch_usage
[] = {
33 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
34 N_("git fetch [<options>] <group>"),
35 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
36 N_("git fetch --all [<options>]"),
46 static int fetch_prune_config
= -1; /* unspecified */
47 static int fetch_show_forced_updates
= 1;
48 static uint64_t forced_updates_ms
= 0;
49 static int prune
= -1; /* unspecified */
50 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
52 static int fetch_prune_tags_config
= -1; /* unspecified */
53 static int prune_tags
= -1; /* unspecified */
54 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
56 static int all
, append
, dry_run
, force
, keep
, multiple
, update_head_ok
;
57 static int verbosity
, deepen_relative
, set_upstream
;
58 static int progress
= -1;
59 static int enable_auto_gc
= 1;
60 static int tags
= TAGS_DEFAULT
, unshallow
, update_shallow
, deepen
;
61 static int max_children
= 1;
62 static enum transport_family family
;
63 static const char *depth
;
64 static const char *deepen_since
;
65 static const char *upload_pack
;
66 static struct string_list deepen_not
= STRING_LIST_INIT_NODUP
;
67 static struct strbuf default_rla
= STRBUF_INIT
;
68 static struct transport
*gtransport
;
69 static struct transport
*gsecondary
;
70 static const char *submodule_prefix
= "";
71 static int recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
72 static int recurse_submodules_default
= RECURSE_SUBMODULES_ON_DEMAND
;
73 static int shown_url
= 0;
74 static struct refspec refmap
= REFSPEC_INIT_FETCH
;
75 static struct list_objects_filter_options filter_options
;
76 static struct string_list server_options
= STRING_LIST_INIT_DUP
;
77 static struct string_list negotiation_tip
= STRING_LIST_INIT_NODUP
;
79 static int git_fetch_config(const char *k
, const char *v
, void *cb
)
81 if (!strcmp(k
, "fetch.prune")) {
82 fetch_prune_config
= git_config_bool(k
, v
);
86 if (!strcmp(k
, "fetch.prunetags")) {
87 fetch_prune_tags_config
= git_config_bool(k
, v
);
91 if (!strcmp(k
, "fetch.showforcedupdates")) {
92 fetch_show_forced_updates
= git_config_bool(k
, v
);
96 if (!strcmp(k
, "submodule.recurse")) {
97 int r
= git_config_bool(k
, v
) ?
98 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
99 recurse_submodules
= r
;
102 if (!strcmp(k
, "submodule.fetchjobs")) {
103 max_children
= parse_submodule_fetchjobs(k
, v
);
105 } else if (!strcmp(k
, "fetch.recursesubmodules")) {
106 recurse_submodules
= parse_fetch_recurse_submodules_arg(k
, v
);
110 return git_default_config(k
, v
, cb
);
113 static int parse_refmap_arg(const struct option
*opt
, const char *arg
, int unset
)
115 BUG_ON_OPT_NEG(unset
);
118 * "git fetch --refmap='' origin foo"
119 * can be used to tell the command not to store anywhere
121 refspec_append(&refmap
, arg
);
126 static struct option builtin_fetch_options
[] = {
127 OPT__VERBOSITY(&verbosity
),
128 OPT_BOOL(0, "all", &all
,
129 N_("fetch from all remotes")),
130 OPT_BOOL(0, "set-upstream", &set_upstream
,
131 N_("set upstream for git pull/fetch")),
132 OPT_BOOL('a', "append", &append
,
133 N_("append to .git/FETCH_HEAD instead of overwriting")),
134 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
135 N_("path to upload pack on remote end")),
136 OPT__FORCE(&force
, N_("force overwrite of local reference"), 0),
137 OPT_BOOL('m', "multiple", &multiple
,
138 N_("fetch from multiple remotes")),
139 OPT_SET_INT('t', "tags", &tags
,
140 N_("fetch all tags and associated objects"), TAGS_SET
),
141 OPT_SET_INT('n', NULL
, &tags
,
142 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
143 OPT_INTEGER('j', "jobs", &max_children
,
144 N_("number of submodules fetched in parallel")),
145 OPT_BOOL('p', "prune", &prune
,
146 N_("prune remote-tracking branches no longer on remote")),
147 OPT_BOOL('P', "prune-tags", &prune_tags
,
148 N_("prune local tags no longer on remote and clobber changed tags")),
149 { OPTION_CALLBACK
, 0, "recurse-submodules", &recurse_submodules
, N_("on-demand"),
150 N_("control recursive fetching of submodules"),
151 PARSE_OPT_OPTARG
, option_fetch_parse_recurse_submodules
},
152 OPT_BOOL(0, "dry-run", &dry_run
,
154 OPT_BOOL('k', "keep", &keep
, N_("keep downloaded pack")),
155 OPT_BOOL('u', "update-head-ok", &update_head_ok
,
156 N_("allow updating of HEAD ref")),
157 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
158 OPT_STRING(0, "depth", &depth
, N_("depth"),
159 N_("deepen history of shallow clone")),
160 OPT_STRING(0, "shallow-since", &deepen_since
, N_("time"),
161 N_("deepen history of shallow repository based on time")),
162 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not
, N_("revision"),
163 N_("deepen history of shallow clone, excluding rev")),
164 OPT_INTEGER(0, "deepen", &deepen_relative
,
165 N_("deepen history of shallow clone")),
166 OPT_SET_INT_F(0, "unshallow", &unshallow
,
167 N_("convert to a complete repository"),
169 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
170 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
171 { OPTION_CALLBACK
, 0, "recurse-submodules-default",
172 &recurse_submodules_default
, N_("on-demand"),
173 N_("default for recursive fetching of submodules "
174 "(lower priority than config files)"),
175 PARSE_OPT_HIDDEN
, option_fetch_parse_recurse_submodules
},
176 OPT_BOOL(0, "update-shallow", &update_shallow
,
177 N_("accept refs that update .git/shallow")),
178 { OPTION_CALLBACK
, 0, "refmap", NULL
, N_("refmap"),
179 N_("specify fetch refmap"), PARSE_OPT_NONEG
, parse_refmap_arg
},
180 OPT_STRING_LIST('o', "server-option", &server_options
, N_("server-specific"), N_("option to transmit")),
181 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
182 TRANSPORT_FAMILY_IPV4
),
183 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
184 TRANSPORT_FAMILY_IPV6
),
185 OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip
, N_("revision"),
186 N_("report that we have only objects reachable from this object")),
187 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
188 OPT_BOOL(0, "auto-gc", &enable_auto_gc
,
189 N_("run 'gc --auto' after fetching")),
190 OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates
,
191 N_("check for forced-updates on all updated branches")),
195 static void unlock_pack(void)
198 transport_unlock_pack(gtransport
);
200 transport_unlock_pack(gsecondary
);
203 static void unlock_pack_on_signal(int signo
)
210 static void add_merge_config(struct ref
**head
,
211 const struct ref
*remote_refs
,
212 struct branch
*branch
,
217 for (i
= 0; i
< branch
->merge_nr
; i
++) {
218 struct ref
*rm
, **old_tail
= *tail
;
219 struct refspec_item refspec
;
221 for (rm
= *head
; rm
; rm
= rm
->next
) {
222 if (branch_merge_matches(branch
, i
, rm
->name
)) {
223 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
231 * Not fetched to a remote-tracking branch? We need to fetch
232 * it anyway to allow this branch's "branch.$name.merge"
233 * to be honored by 'git pull', but we do not have to
234 * fail if branch.$name.merge is misconfigured to point
235 * at a nonexisting branch. If we were indeed called by
236 * 'git pull', it will notice the misconfiguration because
237 * there is no entry in the resulting FETCH_HEAD marked
240 memset(&refspec
, 0, sizeof(refspec
));
241 refspec
.src
= branch
->merge
[i
]->src
;
242 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
243 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
244 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
248 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
250 struct ref
*rm
= *head
;
252 if (hasheq(rm
->old_oid
.hash
, sha1
))
259 struct refname_hash_entry
{
260 struct hashmap_entry ent
; /* must be the first member */
261 struct object_id oid
;
263 char refname
[FLEX_ARRAY
];
266 static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data
,
271 const struct refname_hash_entry
*e1
= e1_
;
272 const struct refname_hash_entry
*e2
= e2_
;
274 return strcmp(e1
->refname
, keydata
? keydata
: e2
->refname
);
277 static struct refname_hash_entry
*refname_hash_add(struct hashmap
*map
,
279 const struct object_id
*oid
)
281 struct refname_hash_entry
*ent
;
282 size_t len
= strlen(refname
);
284 FLEX_ALLOC_MEM(ent
, refname
, refname
, len
);
285 hashmap_entry_init(ent
, strhash(refname
));
286 oidcpy(&ent
->oid
, oid
);
287 hashmap_add(map
, ent
);
291 static int add_one_refname(const char *refname
,
292 const struct object_id
*oid
,
293 int flag
, void *cbdata
)
295 struct hashmap
*refname_map
= cbdata
;
297 (void) refname_hash_add(refname_map
, refname
, oid
);
301 static void refname_hash_init(struct hashmap
*map
)
303 hashmap_init(map
, refname_hash_entry_cmp
, NULL
, 0);
306 static int refname_hash_exists(struct hashmap
*map
, const char *refname
)
308 return !!hashmap_get_from_hash(map
, strhash(refname
), refname
);
311 static void clear_item(struct refname_hash_entry
*item
)
316 static void find_non_local_tags(const struct ref
*refs
,
320 struct hashmap existing_refs
;
321 struct hashmap remote_refs
;
322 struct string_list remote_refs_list
= STRING_LIST_INIT_NODUP
;
323 struct string_list_item
*remote_ref_item
;
324 const struct ref
*ref
;
325 struct refname_hash_entry
*item
= NULL
;
327 refname_hash_init(&existing_refs
);
328 refname_hash_init(&remote_refs
);
330 for_each_ref(add_one_refname
, &existing_refs
);
331 for (ref
= refs
; ref
; ref
= ref
->next
) {
332 if (!starts_with(ref
->name
, "refs/tags/"))
336 * The peeled ref always follows the matching base
337 * ref, so if we see a peeled ref that we don't want
338 * to fetch then we can mark the ref entry in the list
339 * as one to ignore by setting util to NULL.
341 if (ends_with(ref
->name
, "^{}")) {
343 !has_object_file_with_flags(&ref
->old_oid
,
344 OBJECT_INFO_QUICK
) &&
345 !will_fetch(head
, ref
->old_oid
.hash
) &&
346 !has_object_file_with_flags(&item
->oid
, OBJECT_INFO_QUICK
) &&
347 !will_fetch(head
, item
->oid
.hash
))
354 * If item is non-NULL here, then we previously saw a
355 * ref not followed by a peeled reference, so we need
356 * to check if it is a lightweight tag that we want to
360 !has_object_file_with_flags(&item
->oid
, OBJECT_INFO_QUICK
) &&
361 !will_fetch(head
, item
->oid
.hash
))
366 /* skip duplicates and refs that we already have */
367 if (refname_hash_exists(&remote_refs
, ref
->name
) ||
368 refname_hash_exists(&existing_refs
, ref
->name
))
371 item
= refname_hash_add(&remote_refs
, ref
->name
, &ref
->old_oid
);
372 string_list_insert(&remote_refs_list
, ref
->name
);
374 hashmap_free(&existing_refs
, 1);
377 * We may have a final lightweight tag that needs to be
378 * checked to see if it needs fetching.
381 !has_object_file_with_flags(&item
->oid
, OBJECT_INFO_QUICK
) &&
382 !will_fetch(head
, item
->oid
.hash
))
386 * For all the tags in the remote_refs_list,
387 * add them to the list of refs to be fetched
389 for_each_string_list_item(remote_ref_item
, &remote_refs_list
) {
390 const char *refname
= remote_ref_item
->string
;
393 item
= hashmap_get_from_hash(&remote_refs
, strhash(refname
), refname
);
395 BUG("unseen remote ref?");
397 /* Unless we have already decided to ignore this item... */
401 rm
= alloc_ref(item
->refname
);
402 rm
->peer_ref
= alloc_ref(item
->refname
);
403 oidcpy(&rm
->old_oid
, &item
->oid
);
407 hashmap_free(&remote_refs
, 1);
408 string_list_clear(&remote_refs_list
, 0);
411 static struct ref
*get_ref_map(struct remote
*remote
,
412 const struct ref
*remote_refs
,
414 int tags
, int *autotags
)
418 struct ref
*ref_map
= NULL
;
419 struct ref
**tail
= &ref_map
;
421 /* opportunistically-updated references: */
422 struct ref
*orefs
= NULL
, **oref_tail
= &orefs
;
424 struct hashmap existing_refs
;
427 struct refspec
*fetch_refspec
;
429 for (i
= 0; i
< rs
->nr
; i
++) {
430 get_fetch_map(remote_refs
, &rs
->items
[i
], &tail
, 0);
431 if (rs
->items
[i
].dst
&& rs
->items
[i
].dst
[0])
434 /* Merge everything on the command line (but not --tags) */
435 for (rm
= ref_map
; rm
; rm
= rm
->next
)
436 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
439 * For any refs that we happen to be fetching via
440 * command-line arguments, the destination ref might
441 * have been missing or have been different than the
442 * remote-tracking ref that would be derived from the
443 * configured refspec. In these cases, we want to
444 * take the opportunity to update their configured
445 * remote-tracking reference. However, we do not want
446 * to mention these entries in FETCH_HEAD at all, as
447 * they would simply be duplicates of existing
448 * entries, so we set them FETCH_HEAD_IGNORE below.
450 * We compute these entries now, based only on the
451 * refspecs specified on the command line. But we add
452 * them to the list following the refspecs resulting
453 * from the tags option so that one of the latter,
454 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
455 * by ref_remove_duplicates() in favor of one of these
456 * opportunistic entries with FETCH_HEAD_IGNORE.
459 fetch_refspec
= &refmap
;
461 fetch_refspec
= &remote
->fetch
;
463 for (i
= 0; i
< fetch_refspec
->nr
; i
++)
464 get_fetch_map(ref_map
, &fetch_refspec
->items
[i
], &oref_tail
, 1);
465 } else if (refmap
.nr
) {
466 die("--refmap option is only meaningful with command-line refspec(s).");
468 /* Use the defaults */
469 struct branch
*branch
= branch_get(NULL
);
470 int has_merge
= branch_has_merge_config(branch
);
473 /* Note: has_merge implies non-NULL branch->remote_name */
474 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
475 for (i
= 0; i
< remote
->fetch
.nr
; i
++) {
476 get_fetch_map(remote_refs
, &remote
->fetch
.items
[i
], &tail
, 0);
477 if (remote
->fetch
.items
[i
].dst
&&
478 remote
->fetch
.items
[i
].dst
[0])
480 if (!i
&& !has_merge
&& ref_map
&&
481 !remote
->fetch
.items
[0].pattern
)
482 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
485 * if the remote we're fetching from is the same
486 * as given in branch.<name>.remote, we add the
487 * ref given in branch.<name>.merge, too.
489 * Note: has_merge implies non-NULL branch->remote_name
492 !strcmp(branch
->remote_name
, remote
->name
))
493 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
495 ref_map
= get_remote_ref(remote_refs
, "HEAD");
497 die(_("Couldn't find remote ref HEAD"));
498 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
499 tail
= &ref_map
->next
;
503 if (tags
== TAGS_SET
)
504 /* also fetch all tags */
505 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
506 else if (tags
== TAGS_DEFAULT
&& *autotags
)
507 find_non_local_tags(remote_refs
, &ref_map
, &tail
);
509 /* Now append any refs to be updated opportunistically: */
511 for (rm
= orefs
; rm
; rm
= rm
->next
) {
512 rm
->fetch_head_status
= FETCH_HEAD_IGNORE
;
516 ref_map
= ref_remove_duplicates(ref_map
);
518 refname_hash_init(&existing_refs
);
519 for_each_ref(add_one_refname
, &existing_refs
);
521 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
523 const char *refname
= rm
->peer_ref
->name
;
524 struct refname_hash_entry
*peer_item
;
526 peer_item
= hashmap_get_from_hash(&existing_refs
,
530 struct object_id
*old_oid
= &peer_item
->oid
;
531 oidcpy(&rm
->peer_ref
->old_oid
, old_oid
);
535 hashmap_free(&existing_refs
, 1);
540 #define STORE_REF_ERROR_OTHER 1
541 #define STORE_REF_ERROR_DF_CONFLICT 2
543 static int s_update_ref(const char *action
,
548 char *rla
= getenv("GIT_REFLOG_ACTION");
549 struct ref_transaction
*transaction
;
550 struct strbuf err
= STRBUF_INIT
;
551 int ret
, df_conflict
= 0;
556 rla
= default_rla
.buf
;
557 msg
= xstrfmt("%s: %s", rla
, action
);
559 transaction
= ref_transaction_begin(&err
);
561 ref_transaction_update(transaction
, ref
->name
,
563 check_old
? &ref
->old_oid
: NULL
,
567 ret
= ref_transaction_commit(transaction
, &err
);
569 df_conflict
= (ret
== TRANSACTION_NAME_CONFLICT
);
573 ref_transaction_free(transaction
);
574 strbuf_release(&err
);
578 ref_transaction_free(transaction
);
579 error("%s", err
.buf
);
580 strbuf_release(&err
);
582 return df_conflict
? STORE_REF_ERROR_DF_CONFLICT
583 : STORE_REF_ERROR_OTHER
;
586 static int refcol_width
= 10;
587 static int compact_format
;
589 static void adjust_refcol_width(const struct ref
*ref
)
591 int max
, rlen
, llen
, len
;
593 /* uptodate lines are only shown on high verbosity level */
594 if (!verbosity
&& oideq(&ref
->peer_ref
->old_oid
, &ref
->old_oid
))
597 max
= term_columns();
598 rlen
= utf8_strwidth(prettify_refname(ref
->name
));
600 llen
= utf8_strwidth(prettify_refname(ref
->peer_ref
->name
));
603 * rough estimation to see if the output line is too long and
604 * should not be counted (we can't do precise calculation
605 * anyway because we don't know if the error explanation part
606 * will be printed in update_local_ref)
608 if (compact_format
) {
612 len
= 21 /* flag and summary */ + rlen
+ 4 /* -> */ + llen
;
617 * Not precise calculation for compact mode because '*' can
618 * appear on the left hand side of '->' and shrink the column
621 if (refcol_width
< rlen
)
625 static void prepare_format_display(struct ref
*ref_map
)
628 const char *format
= "full";
630 git_config_get_string_const("fetch.output", &format
);
631 if (!strcasecmp(format
, "full"))
633 else if (!strcasecmp(format
, "compact"))
636 die(_("configuration fetch.output contains invalid value %s"),
639 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
640 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
||
642 !strcmp(rm
->name
, "HEAD"))
645 adjust_refcol_width(rm
);
649 static void print_remote_to_local(struct strbuf
*display
,
650 const char *remote
, const char *local
)
652 strbuf_addf(display
, "%-*s -> %s", refcol_width
, remote
, local
);
655 static int find_and_replace(struct strbuf
*haystack
,
657 const char *placeholder
)
659 const char *p
= NULL
;
662 nlen
= strlen(needle
);
663 if (ends_with(haystack
->buf
, needle
))
664 p
= haystack
->buf
+ haystack
->len
- nlen
;
666 p
= strstr(haystack
->buf
, needle
);
670 if (p
> haystack
->buf
&& p
[-1] != '/')
674 if (plen
> nlen
&& p
[nlen
] != '/')
677 strbuf_splice(haystack
, p
- haystack
->buf
, nlen
,
678 placeholder
, strlen(placeholder
));
682 static void print_compact(struct strbuf
*display
,
683 const char *remote
, const char *local
)
685 struct strbuf r
= STRBUF_INIT
;
686 struct strbuf l
= STRBUF_INIT
;
688 if (!strcmp(remote
, local
)) {
689 strbuf_addf(display
, "%-*s -> *", refcol_width
, remote
);
693 strbuf_addstr(&r
, remote
);
694 strbuf_addstr(&l
, local
);
696 if (!find_and_replace(&r
, local
, "*"))
697 find_and_replace(&l
, remote
, "*");
698 print_remote_to_local(display
, r
.buf
, l
.buf
);
704 static void format_display(struct strbuf
*display
, char code
,
705 const char *summary
, const char *error
,
706 const char *remote
, const char *local
,
709 int width
= (summary_width
+ strlen(summary
) - gettext_width(summary
));
711 strbuf_addf(display
, "%c %-*s ", code
, width
, summary
);
713 print_remote_to_local(display
, remote
, local
);
715 print_compact(display
, remote
, local
);
717 strbuf_addf(display
, " (%s)", error
);
720 static int update_local_ref(struct ref
*ref
,
722 const struct ref
*remote_ref
,
723 struct strbuf
*display
,
726 struct commit
*current
= NULL
, *updated
;
727 enum object_type type
;
728 struct branch
*current_branch
= branch_get(NULL
);
729 const char *pretty_ref
= prettify_refname(ref
->name
);
730 int fast_forward
= 0;
732 type
= oid_object_info(the_repository
, &ref
->new_oid
, NULL
);
734 die(_("object %s not found"), oid_to_hex(&ref
->new_oid
));
736 if (oideq(&ref
->old_oid
, &ref
->new_oid
)) {
738 format_display(display
, '=', _("[up to date]"), NULL
,
739 remote
, pretty_ref
, summary_width
);
743 if (current_branch
&&
744 !strcmp(ref
->name
, current_branch
->name
) &&
745 !(update_head_ok
|| is_bare_repository()) &&
746 !is_null_oid(&ref
->old_oid
)) {
748 * If this is the head, and it's not okay to update
749 * the head, and the old value of the head isn't empty...
751 format_display(display
, '!', _("[rejected]"),
752 _("can't fetch in current branch"),
753 remote
, pretty_ref
, summary_width
);
757 if (!is_null_oid(&ref
->old_oid
) &&
758 starts_with(ref
->name
, "refs/tags/")) {
759 if (force
|| ref
->force
) {
761 r
= s_update_ref("updating tag", ref
, 0);
762 format_display(display
, r
? '!' : 't', _("[tag update]"),
763 r
? _("unable to update local ref") : NULL
,
764 remote
, pretty_ref
, summary_width
);
767 format_display(display
, '!', _("[rejected]"), _("would clobber existing tag"),
768 remote
, pretty_ref
, summary_width
);
773 current
= lookup_commit_reference_gently(the_repository
,
775 updated
= lookup_commit_reference_gently(the_repository
,
777 if (!current
|| !updated
) {
782 * Nicely describe the new ref we're fetching.
783 * Base this on the remote's ref name, as it's
784 * more likely to follow a standard layout.
786 const char *name
= remote_ref
? remote_ref
->name
: "";
787 if (starts_with(name
, "refs/tags/")) {
789 what
= _("[new tag]");
790 } else if (starts_with(name
, "refs/heads/")) {
791 msg
= "storing head";
792 what
= _("[new branch]");
795 what
= _("[new ref]");
798 r
= s_update_ref(msg
, ref
, 0);
799 format_display(display
, r
? '!' : '*', what
,
800 r
? _("unable to update local ref") : NULL
,
801 remote
, pretty_ref
, summary_width
);
805 if (fetch_show_forced_updates
) {
806 uint64_t t_before
= getnanotime();
807 fast_forward
= in_merge_bases(current
, updated
);
808 forced_updates_ms
+= (getnanotime() - t_before
) / 1000000;
814 struct strbuf quickref
= STRBUF_INIT
;
817 strbuf_add_unique_abbrev(&quickref
, ¤t
->object
.oid
, DEFAULT_ABBREV
);
818 strbuf_addstr(&quickref
, "..");
819 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
, DEFAULT_ABBREV
);
820 r
= s_update_ref("fast-forward", ref
, 1);
821 format_display(display
, r
? '!' : ' ', quickref
.buf
,
822 r
? _("unable to update local ref") : NULL
,
823 remote
, pretty_ref
, summary_width
);
824 strbuf_release(&quickref
);
826 } else if (force
|| ref
->force
) {
827 struct strbuf quickref
= STRBUF_INIT
;
829 strbuf_add_unique_abbrev(&quickref
, ¤t
->object
.oid
, DEFAULT_ABBREV
);
830 strbuf_addstr(&quickref
, "...");
831 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
, DEFAULT_ABBREV
);
832 r
= s_update_ref("forced-update", ref
, 1);
833 format_display(display
, r
? '!' : '+', quickref
.buf
,
834 r
? _("unable to update local ref") : _("forced update"),
835 remote
, pretty_ref
, summary_width
);
836 strbuf_release(&quickref
);
839 format_display(display
, '!', _("[rejected]"), _("non-fast-forward"),
840 remote
, pretty_ref
, summary_width
);
845 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
847 struct ref
**rm
= cb_data
;
848 struct ref
*ref
= *rm
;
850 while (ref
&& ref
->status
== REF_STATUS_REJECT_SHALLOW
)
853 return -1; /* end of the list */
855 oidcpy(oid
, &ref
->old_oid
);
859 static const char warn_show_forced_updates
[] =
860 N_("Fetch normally indicates which branches had a forced update,\n"
861 "but that check has been disabled. To re-enable, use '--show-forced-updates'\n"
862 "flag or run 'git config fetch.showForcedUpdates true'.");
863 static const char warn_time_show_forced_updates
[] =
864 N_("It took %.2f seconds to check forced updates. You can use\n"
865 "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
866 " to avoid this check.\n");
868 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
869 int connectivity_checked
, struct ref
*ref_map
)
872 struct commit
*commit
;
873 int url_len
, i
, rc
= 0;
874 struct strbuf note
= STRBUF_INIT
;
875 const char *what
, *kind
;
878 const char *filename
= dry_run
? "/dev/null" : git_path_fetch_head(the_repository
);
880 int summary_width
= transport_summary_width(ref_map
);
882 fp
= fopen(filename
, "a");
884 return error_errno(_("cannot open %s"), filename
);
887 url
= transport_anonymize_url(raw_url
);
889 url
= xstrdup("foreign");
891 if (!connectivity_checked
) {
893 if (check_connected(iterate_ref_map
, &rm
, NULL
)) {
894 rc
= error(_("%s did not send all necessary objects\n"), url
);
899 prepare_format_display(ref_map
);
902 * We do a pass for each fetch_head_status type in their enum order, so
903 * merged entries are written before not-for-merge. That lets readers
904 * use FETCH_HEAD as a refname to refer to the ref to be merged.
906 for (want_status
= FETCH_HEAD_MERGE
;
907 want_status
<= FETCH_HEAD_IGNORE
;
909 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
910 struct ref
*ref
= NULL
;
911 const char *merge_status_marker
= "";
913 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
) {
914 if (want_status
== FETCH_HEAD_MERGE
)
915 warning(_("reject %s because shallow roots are not allowed to be updated"),
916 rm
->peer_ref
? rm
->peer_ref
->name
: rm
->name
);
920 commit
= lookup_commit_reference_gently(the_repository
,
924 rm
->fetch_head_status
= FETCH_HEAD_NOT_FOR_MERGE
;
926 if (rm
->fetch_head_status
!= want_status
)
930 ref
= alloc_ref(rm
->peer_ref
->name
);
931 oidcpy(&ref
->old_oid
, &rm
->peer_ref
->old_oid
);
932 oidcpy(&ref
->new_oid
, &rm
->old_oid
);
933 ref
->force
= rm
->peer_ref
->force
;
936 if (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)
937 check_for_new_submodule_commits(&rm
->old_oid
);
939 if (!strcmp(rm
->name
, "HEAD")) {
943 else if (starts_with(rm
->name
, "refs/heads/")) {
945 what
= rm
->name
+ 11;
947 else if (starts_with(rm
->name
, "refs/tags/")) {
949 what
= rm
->name
+ 10;
951 else if (starts_with(rm
->name
, "refs/remotes/")) {
952 kind
= "remote-tracking branch";
953 what
= rm
->name
+ 13;
960 url_len
= strlen(url
);
961 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
964 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
970 strbuf_addf(¬e
, "%s ", kind
);
971 strbuf_addf(¬e
, "'%s' of ", what
);
973 switch (rm
->fetch_head_status
) {
974 case FETCH_HEAD_NOT_FOR_MERGE
:
975 merge_status_marker
= "not-for-merge";
977 case FETCH_HEAD_MERGE
:
978 fprintf(fp
, "%s\t%s\t%s",
979 oid_to_hex(&rm
->old_oid
),
982 for (i
= 0; i
< url_len
; ++i
)
990 /* do not write anything to FETCH_HEAD */
996 rc
|= update_local_ref(ref
, what
, rm
, ¬e
,
1000 format_display(¬e
, '*',
1001 *kind
? kind
: "branch", NULL
,
1002 *what
? what
: "HEAD",
1003 "FETCH_HEAD", summary_width
);
1005 if (verbosity
>= 0 && !shown_url
) {
1006 fprintf(stderr
, _("From %.*s\n"),
1011 fprintf(stderr
, " %s\n", note
.buf
);
1016 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
1017 error(_("some local refs could not be updated; try running\n"
1018 " 'git remote prune %s' to remove any old, conflicting "
1019 "branches"), remote_name
);
1021 if (advice_fetch_show_forced_updates
) {
1022 if (!fetch_show_forced_updates
) {
1023 warning(_(warn_show_forced_updates
));
1024 } else if (forced_updates_ms
> FORCED_UPDATES_DELAY_WARNING_IN_MS
) {
1025 warning(_(warn_time_show_forced_updates
),
1026 forced_updates_ms
/ 1000.0);
1031 strbuf_release(¬e
);
1038 * We would want to bypass the object transfer altogether if
1039 * everything we are going to fetch already exists and is connected
1042 static int check_exist_and_connected(struct ref
*ref_map
)
1044 struct ref
*rm
= ref_map
;
1045 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
1049 * If we are deepening a shallow clone we already have these
1050 * objects reachable. Running rev-list here will return with
1051 * a good (0) exit status and we'll bypass the fetch that we
1052 * really need to perform. Claiming failure now will ensure
1053 * we perform the network exchange to deepen our history.
1059 * check_connected() allows objects to merely be promised, but
1060 * we need all direct targets to exist.
1062 for (r
= rm
; r
; r
= r
->next
) {
1063 if (!has_object_file(&r
->old_oid
))
1068 return check_connected(iterate_ref_map
, &rm
, &opt
);
1071 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
1073 int ret
= check_exist_and_connected(ref_map
);
1075 ret
= transport_fetch_refs(transport
, ref_map
);
1078 * Keep the new pack's ".keep" file around to allow the caller
1079 * time to update refs to reference the new objects.
1082 transport_unlock_pack(transport
);
1086 /* Update local refs based on the ref values fetched from a remote */
1087 static int consume_refs(struct transport
*transport
, struct ref
*ref_map
)
1089 int connectivity_checked
= transport
->smart_options
1090 ? transport
->smart_options
->connectivity_checked
: 0;
1091 int ret
= store_updated_refs(transport
->url
,
1092 transport
->remote
->name
,
1093 connectivity_checked
,
1095 transport_unlock_pack(transport
);
1099 static int prune_refs(struct refspec
*rs
, struct ref
*ref_map
,
1100 const char *raw_url
)
1102 int url_len
, i
, result
= 0;
1103 struct ref
*ref
, *stale_refs
= get_stale_heads(rs
, ref_map
);
1105 int summary_width
= transport_summary_width(stale_refs
);
1106 const char *dangling_msg
= dry_run
1107 ? _(" (%s will become dangling)")
1108 : _(" (%s has become dangling)");
1111 url
= transport_anonymize_url(raw_url
);
1113 url
= xstrdup("foreign");
1115 url_len
= strlen(url
);
1116 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
1120 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
1124 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
1126 for (ref
= stale_refs
; ref
; ref
= ref
->next
)
1127 string_list_append(&refnames
, ref
->name
);
1129 result
= delete_refs("fetch: prune", &refnames
, 0);
1130 string_list_clear(&refnames
, 0);
1133 if (verbosity
>= 0) {
1134 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
1135 struct strbuf sb
= STRBUF_INIT
;
1137 fprintf(stderr
, _("From %.*s\n"), url_len
, url
);
1140 format_display(&sb
, '-', _("[deleted]"), NULL
,
1141 _("(none)"), prettify_refname(ref
->name
),
1143 fprintf(stderr
, " %s\n",sb
.buf
);
1144 strbuf_release(&sb
);
1145 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
1150 free_refs(stale_refs
);
1154 static void check_not_current_branch(struct ref
*ref_map
)
1156 struct branch
*current_branch
= branch_get(NULL
);
1158 if (is_bare_repository() || !current_branch
)
1161 for (; ref_map
; ref_map
= ref_map
->next
)
1162 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
1163 ref_map
->peer_ref
->name
))
1164 die(_("Refusing to fetch into current branch %s "
1165 "of non-bare repository"), current_branch
->refname
);
1168 static int truncate_fetch_head(void)
1170 const char *filename
= git_path_fetch_head(the_repository
);
1171 FILE *fp
= fopen_for_writing(filename
);
1174 return error_errno(_("cannot open %s"), filename
);
1179 static void set_option(struct transport
*transport
, const char *name
, const char *value
)
1181 int r
= transport_set_option(transport
, name
, value
);
1183 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1184 name
, value
, transport
->url
);
1186 warning(_("Option \"%s\" is ignored for %s\n"),
1187 name
, transport
->url
);
1191 static int add_oid(const char *refname
, const struct object_id
*oid
, int flags
,
1194 struct oid_array
*oids
= cb_data
;
1196 oid_array_append(oids
, oid
);
1200 static void add_negotiation_tips(struct git_transport_options
*smart_options
)
1202 struct oid_array
*oids
= xcalloc(1, sizeof(*oids
));
1205 for (i
= 0; i
< negotiation_tip
.nr
; i
++) {
1206 const char *s
= negotiation_tip
.items
[i
].string
;
1208 if (!has_glob_specials(s
)) {
1209 struct object_id oid
;
1210 if (get_oid(s
, &oid
))
1211 die("%s is not a valid object", s
);
1212 oid_array_append(oids
, &oid
);
1216 for_each_glob_ref(add_oid
, s
, oids
);
1217 if (old_nr
== oids
->nr
)
1218 warning("Ignoring --negotiation-tip=%s because it does not match any refs",
1221 smart_options
->negotiation_tips
= oids
;
1224 static struct transport
*prepare_transport(struct remote
*remote
, int deepen
)
1226 struct transport
*transport
;
1228 transport
= transport_get(remote
, NULL
);
1229 transport_set_verbosity(transport
, verbosity
, progress
);
1230 transport
->family
= family
;
1232 set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
1234 set_option(transport
, TRANS_OPT_KEEP
, "yes");
1236 set_option(transport
, TRANS_OPT_DEPTH
, depth
);
1237 if (deepen
&& deepen_since
)
1238 set_option(transport
, TRANS_OPT_DEEPEN_SINCE
, deepen_since
);
1239 if (deepen
&& deepen_not
.nr
)
1240 set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1241 (const char *)&deepen_not
);
1242 if (deepen_relative
)
1243 set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, "yes");
1245 set_option(transport
, TRANS_OPT_UPDATE_SHALLOW
, "yes");
1246 if (filter_options
.choice
) {
1248 expand_list_objects_filter_spec(&filter_options
);
1249 set_option(transport
, TRANS_OPT_LIST_OBJECTS_FILTER
, spec
);
1250 set_option(transport
, TRANS_OPT_FROM_PROMISOR
, "1");
1252 if (negotiation_tip
.nr
) {
1253 if (transport
->smart_options
)
1254 add_negotiation_tips(transport
->smart_options
);
1256 warning("Ignoring --negotiation-tip because the protocol does not support it.");
1261 static void backfill_tags(struct transport
*transport
, struct ref
*ref_map
)
1266 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1267 * when remote helper is used (setting it to an empty string
1268 * is not unsetting). We could extend the remote helper
1269 * protocol for that, but for now, just force a new connection
1270 * without deepen-since. Similar story for deepen-not.
1272 cannot_reuse
= transport
->cannot_reuse
||
1273 deepen_since
|| deepen_not
.nr
;
1275 gsecondary
= prepare_transport(transport
->remote
, 0);
1276 transport
= gsecondary
;
1279 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
1280 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
1281 transport_set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, NULL
);
1282 if (!fetch_refs(transport
, ref_map
))
1283 consume_refs(transport
, ref_map
);
1286 transport_disconnect(gsecondary
);
1291 static int do_fetch(struct transport
*transport
,
1294 struct ref
*ref_map
;
1295 int autotags
= (transport
->remote
->fetch_tags
== 1);
1297 const struct ref
*remote_refs
;
1298 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
1299 int must_list_refs
= 1;
1301 if (tags
== TAGS_DEFAULT
) {
1302 if (transport
->remote
->fetch_tags
== 2)
1304 if (transport
->remote
->fetch_tags
== -1)
1308 /* if not appending, truncate FETCH_HEAD */
1309 if (!append
&& !dry_run
) {
1310 retcode
= truncate_fetch_head();
1318 refspec_ref_prefixes(rs
, &ref_prefixes
);
1321 * We can avoid listing refs if all of them are exact
1325 for (i
= 0; i
< rs
->nr
; i
++) {
1326 if (!rs
->items
[i
].exact_sha1
) {
1331 } else if (transport
->remote
&& transport
->remote
->fetch
.nr
)
1332 refspec_ref_prefixes(&transport
->remote
->fetch
, &ref_prefixes
);
1334 if (tags
== TAGS_SET
|| tags
== TAGS_DEFAULT
) {
1336 if (ref_prefixes
.argc
)
1337 argv_array_push(&ref_prefixes
, "refs/tags/");
1341 remote_refs
= transport_get_remote_refs(transport
, &ref_prefixes
);
1345 argv_array_clear(&ref_prefixes
);
1347 ref_map
= get_ref_map(transport
->remote
, remote_refs
, rs
,
1349 if (!update_head_ok
)
1350 check_not_current_branch(ref_map
);
1352 if (tags
== TAGS_DEFAULT
&& autotags
)
1353 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1356 * We only prune based on refspecs specified
1357 * explicitly (via command line or configuration); we
1358 * don't care whether --tags was specified.
1361 prune_refs(rs
, ref_map
, transport
->url
);
1363 prune_refs(&transport
->remote
->fetch
,
1368 if (fetch_refs(transport
, ref_map
) || consume_refs(transport
, ref_map
)) {
1375 struct branch
*branch
= branch_get("HEAD");
1377 struct ref
*source_ref
= NULL
;
1380 * We're setting the upstream configuration for the
1381 * current branch. The relevent upstream is the
1382 * fetched branch that is meant to be merged with the
1383 * current one, i.e. the one fetched to FETCH_HEAD.
1385 * When there are several such branches, consider the
1386 * request ambiguous and err on the safe side by doing
1387 * nothing and just emit a warning.
1389 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
1390 if (!rm
->peer_ref
) {
1392 warning(_("multiple branch detected, incompatible with --set-upstream"));
1400 if (!strcmp(source_ref
->name
, "HEAD") ||
1401 starts_with(source_ref
->name
, "refs/heads/"))
1402 install_branch_config(0,
1404 transport
->remote
->name
,
1406 else if (starts_with(source_ref
->name
, "refs/remotes/"))
1407 warning(_("not setting upstream for a remote remote-tracking branch"));
1408 else if (starts_with(source_ref
->name
, "refs/tags/"))
1409 warning(_("not setting upstream for a remote tag"));
1411 warning(_("unknown branch type"));
1413 warning(_("no source branch found.\n"
1414 "you need to specify exactly one branch with the --set-upstream option."));
1420 /* if neither --no-tags nor --tags was specified, do automated tag
1422 if (tags
== TAGS_DEFAULT
&& autotags
) {
1423 struct ref
**tail
= &ref_map
;
1425 find_non_local_tags(remote_refs
, &ref_map
, &tail
);
1427 backfill_tags(transport
, ref_map
);
1435 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
1437 struct string_list
*list
= priv
;
1438 if (!remote
->skip_default_update
)
1439 string_list_append(list
, remote
->name
);
1443 struct remote_group_data
{
1445 struct string_list
*list
;
1448 static int get_remote_group(const char *key
, const char *value
, void *priv
)
1450 struct remote_group_data
*g
= priv
;
1452 if (skip_prefix(key
, "remotes.", &key
) && !strcmp(key
, g
->name
)) {
1453 /* split list by white space */
1455 size_t wordlen
= strcspn(value
, " \t\n");
1458 string_list_append_nodup(g
->list
,
1459 xstrndup(value
, wordlen
));
1460 value
+= wordlen
+ (value
[wordlen
] != '\0');
1467 static int add_remote_or_group(const char *name
, struct string_list
*list
)
1469 int prev_nr
= list
->nr
;
1470 struct remote_group_data g
;
1471 g
.name
= name
; g
.list
= list
;
1473 git_config(get_remote_group
, &g
);
1474 if (list
->nr
== prev_nr
) {
1475 struct remote
*remote
= remote_get(name
);
1476 if (!remote_is_configured(remote
, 0))
1478 string_list_append(list
, remote
->name
);
1483 static void add_options_to_argv(struct argv_array
*argv
)
1486 argv_array_push(argv
, "--dry-run");
1488 argv_array_push(argv
, prune
? "--prune" : "--no-prune");
1489 if (prune_tags
!= -1)
1490 argv_array_push(argv
, prune_tags
? "--prune-tags" : "--no-prune-tags");
1492 argv_array_push(argv
, "--update-head-ok");
1494 argv_array_push(argv
, "--force");
1496 argv_array_push(argv
, "--keep");
1497 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
1498 argv_array_push(argv
, "--recurse-submodules");
1499 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
1500 argv_array_push(argv
, "--recurse-submodules=on-demand");
1501 if (tags
== TAGS_SET
)
1502 argv_array_push(argv
, "--tags");
1503 else if (tags
== TAGS_UNSET
)
1504 argv_array_push(argv
, "--no-tags");
1506 argv_array_push(argv
, "-v");
1508 argv_array_push(argv
, "-v");
1509 else if (verbosity
< 0)
1510 argv_array_push(argv
, "-q");
1514 static int fetch_multiple(struct string_list
*list
)
1517 struct argv_array argv
= ARGV_ARRAY_INIT
;
1519 if (!append
&& !dry_run
) {
1520 int errcode
= truncate_fetch_head();
1525 argv_array_pushl(&argv
, "fetch", "--append", "--no-auto-gc", NULL
);
1526 add_options_to_argv(&argv
);
1528 for (i
= 0; i
< list
->nr
; i
++) {
1529 const char *name
= list
->items
[i
].string
;
1530 argv_array_push(&argv
, name
);
1532 printf(_("Fetching %s\n"), name
);
1533 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
1534 error(_("Could not fetch %s"), name
);
1537 argv_array_pop(&argv
);
1540 argv_array_clear(&argv
);
1545 * Fetching from the promisor remote should use the given filter-spec
1546 * or inherit the default filter-spec from the config.
1548 static inline void fetch_one_setup_partial(struct remote
*remote
)
1551 * Explicit --no-filter argument overrides everything, regardless
1552 * of any prior partial clones and fetches.
1554 if (filter_options
.no_filter
)
1558 * If no prior partial clone/fetch and the current fetch DID NOT
1559 * request a partial-fetch, do a normal fetch.
1561 if (!has_promisor_remote() && !filter_options
.choice
)
1565 * If this is a partial-fetch request, we enable partial on
1566 * this repo if not already enabled and remember the given
1567 * filter-spec as the default for subsequent fetches to this
1570 if (filter_options
.choice
) {
1571 partial_clone_register(remote
->name
, &filter_options
);
1576 * Do a partial-fetch from the promisor remote using either the
1577 * explicitly given filter-spec or inherit the filter-spec from
1580 if (!filter_options
.choice
)
1581 partial_clone_get_default_filter_spec(&filter_options
, remote
->name
);
1585 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
, int prune_tags_ok
)
1587 struct refspec rs
= REFSPEC_INIT_FETCH
;
1590 int maybe_prune_tags
;
1591 int remote_via_config
= remote_is_configured(remote
, 0);
1594 die(_("No remote repository specified. Please, specify either a URL or a\n"
1595 "remote name from which new revisions should be fetched."));
1597 gtransport
= prepare_transport(remote
, 1);
1600 /* no command line request */
1601 if (0 <= remote
->prune
)
1602 prune
= remote
->prune
;
1603 else if (0 <= fetch_prune_config
)
1604 prune
= fetch_prune_config
;
1606 prune
= PRUNE_BY_DEFAULT
;
1609 if (prune_tags
< 0) {
1610 /* no command line request */
1611 if (0 <= remote
->prune_tags
)
1612 prune_tags
= remote
->prune_tags
;
1613 else if (0 <= fetch_prune_tags_config
)
1614 prune_tags
= fetch_prune_tags_config
;
1616 prune_tags
= PRUNE_TAGS_BY_DEFAULT
;
1619 maybe_prune_tags
= prune_tags_ok
&& prune_tags
;
1620 if (maybe_prune_tags
&& remote_via_config
)
1621 refspec_append(&remote
->fetch
, TAG_REFSPEC
);
1623 if (maybe_prune_tags
&& (argc
|| !remote_via_config
))
1624 refspec_append(&rs
, TAG_REFSPEC
);
1626 for (i
= 0; i
< argc
; i
++) {
1627 if (!strcmp(argv
[i
], "tag")) {
1631 die(_("You need to specify a tag name."));
1633 tag
= xstrfmt("refs/tags/%s:refs/tags/%s",
1635 refspec_append(&rs
, tag
);
1638 refspec_append(&rs
, argv
[i
]);
1642 if (server_options
.nr
)
1643 gtransport
->server_options
= &server_options
;
1645 sigchain_push_common(unlock_pack_on_signal
);
1646 atexit(unlock_pack
);
1647 sigchain_push(SIGPIPE
, SIG_IGN
);
1648 exit_code
= do_fetch(gtransport
, &rs
);
1649 sigchain_pop(SIGPIPE
);
1651 transport_disconnect(gtransport
);
1656 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
1659 struct string_list list
= STRING_LIST_INIT_DUP
;
1660 struct remote
*remote
= NULL
;
1662 int prune_tags_ok
= 1;
1663 struct argv_array argv_gc_auto
= ARGV_ARRAY_INIT
;
1665 packet_trace_identity("fetch");
1667 fetch_if_missing
= 0;
1669 /* Record the command line for the reflog */
1670 strbuf_addstr(&default_rla
, "fetch");
1671 for (i
= 1; i
< argc
; i
++)
1672 strbuf_addf(&default_rla
, " %s", argv
[i
]);
1674 fetch_config_from_gitmodules(&max_children
, &recurse_submodules
);
1675 git_config(git_fetch_config
, NULL
);
1677 argc
= parse_options(argc
, argv
, prefix
,
1678 builtin_fetch_options
, builtin_fetch_usage
, 0);
1680 if (deepen_relative
) {
1681 if (deepen_relative
< 0)
1682 die(_("Negative depth in --deepen is not supported"));
1684 die(_("--deepen and --depth are mutually exclusive"));
1685 depth
= xstrfmt("%d", deepen_relative
);
1689 die(_("--depth and --unshallow cannot be used together"));
1690 else if (!is_repository_shallow(the_repository
))
1691 die(_("--unshallow on a complete repository does not make sense"));
1693 depth
= xstrfmt("%d", INFINITE_DEPTH
);
1696 /* no need to be strict, transport_set_option() will validate it again */
1697 if (depth
&& atoi(depth
) < 1)
1698 die(_("depth %s is not a positive number"), depth
);
1699 if (depth
|| deepen_since
|| deepen_not
.nr
)
1702 if (filter_options
.choice
&& !has_promisor_remote())
1703 die("--filter can only be used when extensions.partialClone is set");
1707 die(_("fetch --all does not take a repository argument"));
1709 die(_("fetch --all does not make sense with refspecs"));
1710 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1711 } else if (argc
== 0) {
1712 /* No arguments -- use default remote */
1713 remote
= remote_get(NULL
);
1714 } else if (multiple
) {
1715 /* All arguments are assumed to be remotes or groups */
1716 for (i
= 0; i
< argc
; i
++)
1717 if (!add_remote_or_group(argv
[i
], &list
))
1718 die(_("No such remote or remote group: %s"), argv
[i
]);
1720 /* Single remote or group */
1721 (void) add_remote_or_group(argv
[0], &list
);
1723 /* More than one remote */
1725 die(_("Fetching a group and specifying refspecs does not make sense"));
1727 /* Zero or one remotes */
1728 remote
= remote_get(argv
[0]);
1729 prune_tags_ok
= (argc
== 1);
1736 if (filter_options
.choice
|| has_promisor_remote())
1737 fetch_one_setup_partial(remote
);
1738 result
= fetch_one(remote
, argc
, argv
, prune_tags_ok
);
1740 if (filter_options
.choice
)
1741 die(_("--filter can only be used with the remote "
1742 "configured in extensions.partialclone"));
1743 /* TODO should this also die if we have a previous partial-clone? */
1744 result
= fetch_multiple(&list
);
1747 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1748 struct argv_array options
= ARGV_ARRAY_INIT
;
1750 add_options_to_argv(&options
);
1751 result
= fetch_populated_submodules(the_repository
,
1755 recurse_submodules_default
,
1758 argv_array_clear(&options
);
1761 string_list_clear(&list
, 0);
1763 prepare_repo_settings(the_repository
);
1764 if (the_repository
->settings
.fetch_write_commit_graph
) {
1765 int commit_graph_flags
= COMMIT_GRAPH_WRITE_SPLIT
;
1766 struct split_commit_graph_opts split_opts
;
1767 memset(&split_opts
, 0, sizeof(struct split_commit_graph_opts
));
1770 commit_graph_flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
1772 write_commit_graph_reachable(get_object_directory(),
1777 close_object_store(the_repository
->objects
);
1779 if (enable_auto_gc
) {
1780 argv_array_pushl(&argv_gc_auto
, "gc", "--auto", NULL
);
1782 argv_array_push(&argv_gc_auto
, "--quiet");
1783 run_command_v_opt(argv_gc_auto
.argv
, RUN_GIT_CMD
);
1784 argv_array_clear(&argv_gc_auto
);