6 #include "repository.h"
9 #include "object-store.h"
13 #include "string-list.h"
15 #include "transport.h"
16 #include "run-command.h"
17 #include "parse-options.h"
19 #include "submodule-config.h"
20 #include "submodule.h"
21 #include "connected.h"
22 #include "argv-array.h"
25 #include "list-objects-filter-options.h"
26 #include "commit-reach.h"
28 #include "promisor-remote.h"
29 #include "commit-graph.h"
31 #define FORCED_UPDATES_DELAY_WARNING_IN_MS (10 * 1000)
33 static const char * const builtin_fetch_usage
[] = {
34 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
35 N_("git fetch [<options>] <group>"),
36 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
37 N_("git fetch --all [<options>]"),
47 static int fetch_prune_config
= -1; /* unspecified */
48 static int fetch_show_forced_updates
= 1;
49 static uint64_t forced_updates_ms
= 0;
50 static int prune
= -1; /* unspecified */
51 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
53 static int fetch_prune_tags_config
= -1; /* unspecified */
54 static int prune_tags
= -1; /* unspecified */
55 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
57 static int all
, append
, dry_run
, force
, keep
, multiple
, update_head_ok
;
58 static int verbosity
, deepen_relative
, set_upstream
;
59 static int progress
= -1;
60 static int enable_auto_gc
= 1;
61 static int tags
= TAGS_DEFAULT
, unshallow
, update_shallow
, deepen
;
62 static int max_jobs
= -1, submodule_fetch_jobs_config
= -1;
63 static int fetch_parallel_config
= 1;
64 static enum transport_family family
;
65 static const char *depth
;
66 static const char *deepen_since
;
67 static const char *upload_pack
;
68 static struct string_list deepen_not
= STRING_LIST_INIT_NODUP
;
69 static struct strbuf default_rla
= STRBUF_INIT
;
70 static struct transport
*gtransport
;
71 static struct transport
*gsecondary
;
72 static const char *submodule_prefix
= "";
73 static int recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
74 static int recurse_submodules_default
= RECURSE_SUBMODULES_ON_DEMAND
;
75 static int shown_url
= 0;
76 static struct refspec refmap
= REFSPEC_INIT_FETCH
;
77 static struct list_objects_filter_options filter_options
;
78 static struct string_list server_options
= STRING_LIST_INIT_DUP
;
79 static struct string_list negotiation_tip
= STRING_LIST_INIT_NODUP
;
80 static int fetch_write_commit_graph
= -1;
82 static int git_fetch_config(const char *k
, const char *v
, void *cb
)
84 if (!strcmp(k
, "fetch.prune")) {
85 fetch_prune_config
= git_config_bool(k
, v
);
89 if (!strcmp(k
, "fetch.prunetags")) {
90 fetch_prune_tags_config
= git_config_bool(k
, v
);
94 if (!strcmp(k
, "fetch.showforcedupdates")) {
95 fetch_show_forced_updates
= git_config_bool(k
, v
);
99 if (!strcmp(k
, "submodule.recurse")) {
100 int r
= git_config_bool(k
, v
) ?
101 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
102 recurse_submodules
= r
;
105 if (!strcmp(k
, "submodule.fetchjobs")) {
106 submodule_fetch_jobs_config
= parse_submodule_fetchjobs(k
, v
);
108 } else if (!strcmp(k
, "fetch.recursesubmodules")) {
109 recurse_submodules
= parse_fetch_recurse_submodules_arg(k
, v
);
113 if (!strcmp(k
, "fetch.parallel")) {
114 fetch_parallel_config
= git_config_int(k
, v
);
115 if (fetch_parallel_config
< 0)
116 die(_("fetch.parallel cannot be negative"));
120 return git_default_config(k
, v
, cb
);
123 static int parse_refmap_arg(const struct option
*opt
, const char *arg
, int unset
)
125 BUG_ON_OPT_NEG(unset
);
128 * "git fetch --refmap='' origin foo"
129 * can be used to tell the command not to store anywhere
131 refspec_append(&refmap
, arg
);
136 static struct option builtin_fetch_options
[] = {
137 OPT__VERBOSITY(&verbosity
),
138 OPT_BOOL(0, "all", &all
,
139 N_("fetch from all remotes")),
140 OPT_BOOL(0, "set-upstream", &set_upstream
,
141 N_("set upstream for git pull/fetch")),
142 OPT_BOOL('a', "append", &append
,
143 N_("append to .git/FETCH_HEAD instead of overwriting")),
144 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
145 N_("path to upload pack on remote end")),
146 OPT__FORCE(&force
, N_("force overwrite of local reference"), 0),
147 OPT_BOOL('m', "multiple", &multiple
,
148 N_("fetch from multiple remotes")),
149 OPT_SET_INT('t', "tags", &tags
,
150 N_("fetch all tags and associated objects"), TAGS_SET
),
151 OPT_SET_INT('n', NULL
, &tags
,
152 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
153 OPT_INTEGER('j', "jobs", &max_jobs
,
154 N_("number of submodules fetched in parallel")),
155 OPT_BOOL('p', "prune", &prune
,
156 N_("prune remote-tracking branches no longer on remote")),
157 OPT_BOOL('P', "prune-tags", &prune_tags
,
158 N_("prune local tags no longer on remote and clobber changed tags")),
159 { OPTION_CALLBACK
, 0, "recurse-submodules", &recurse_submodules
, N_("on-demand"),
160 N_("control recursive fetching of submodules"),
161 PARSE_OPT_OPTARG
, option_fetch_parse_recurse_submodules
},
162 OPT_BOOL(0, "dry-run", &dry_run
,
164 OPT_BOOL('k', "keep", &keep
, N_("keep downloaded pack")),
165 OPT_BOOL('u', "update-head-ok", &update_head_ok
,
166 N_("allow updating of HEAD ref")),
167 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
168 OPT_STRING(0, "depth", &depth
, N_("depth"),
169 N_("deepen history of shallow clone")),
170 OPT_STRING(0, "shallow-since", &deepen_since
, N_("time"),
171 N_("deepen history of shallow repository based on time")),
172 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not
, N_("revision"),
173 N_("deepen history of shallow clone, excluding rev")),
174 OPT_INTEGER(0, "deepen", &deepen_relative
,
175 N_("deepen history of shallow clone")),
176 OPT_SET_INT_F(0, "unshallow", &unshallow
,
177 N_("convert to a complete repository"),
179 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
180 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
181 { OPTION_CALLBACK
, 0, "recurse-submodules-default",
182 &recurse_submodules_default
, N_("on-demand"),
183 N_("default for recursive fetching of submodules "
184 "(lower priority than config files)"),
185 PARSE_OPT_HIDDEN
, option_fetch_parse_recurse_submodules
},
186 OPT_BOOL(0, "update-shallow", &update_shallow
,
187 N_("accept refs that update .git/shallow")),
188 { OPTION_CALLBACK
, 0, "refmap", NULL
, N_("refmap"),
189 N_("specify fetch refmap"), PARSE_OPT_NONEG
, parse_refmap_arg
},
190 OPT_STRING_LIST('o', "server-option", &server_options
, N_("server-specific"), N_("option to transmit")),
191 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
192 TRANSPORT_FAMILY_IPV4
),
193 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
194 TRANSPORT_FAMILY_IPV6
),
195 OPT_STRING_LIST(0, "negotiation-tip", &negotiation_tip
, N_("revision"),
196 N_("report that we have only objects reachable from this object")),
197 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
198 OPT_BOOL(0, "auto-gc", &enable_auto_gc
,
199 N_("run 'gc --auto' after fetching")),
200 OPT_BOOL(0, "show-forced-updates", &fetch_show_forced_updates
,
201 N_("check for forced-updates on all updated branches")),
202 OPT_BOOL(0, "write-commit-graph", &fetch_write_commit_graph
,
203 N_("write the commit-graph after fetching")),
207 static void unlock_pack(void)
210 transport_unlock_pack(gtransport
);
212 transport_unlock_pack(gsecondary
);
215 static void unlock_pack_on_signal(int signo
)
222 static void add_merge_config(struct ref
**head
,
223 const struct ref
*remote_refs
,
224 struct branch
*branch
,
229 for (i
= 0; i
< branch
->merge_nr
; i
++) {
230 struct ref
*rm
, **old_tail
= *tail
;
231 struct refspec_item refspec
;
233 for (rm
= *head
; rm
; rm
= rm
->next
) {
234 if (branch_merge_matches(branch
, i
, rm
->name
)) {
235 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
243 * Not fetched to a remote-tracking branch? We need to fetch
244 * it anyway to allow this branch's "branch.$name.merge"
245 * to be honored by 'git pull', but we do not have to
246 * fail if branch.$name.merge is misconfigured to point
247 * at a nonexisting branch. If we were indeed called by
248 * 'git pull', it will notice the misconfiguration because
249 * there is no entry in the resulting FETCH_HEAD marked
252 memset(&refspec
, 0, sizeof(refspec
));
253 refspec
.src
= branch
->merge
[i
]->src
;
254 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
255 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
256 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
260 static void create_fetch_oidset(struct ref
**head
, struct oidset
*out
)
262 struct ref
*rm
= *head
;
264 oidset_insert(out
, &rm
->old_oid
);
269 struct refname_hash_entry
{
270 struct hashmap_entry ent
;
271 struct object_id oid
;
273 char refname
[FLEX_ARRAY
];
276 static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data
,
277 const struct hashmap_entry
*eptr
,
278 const struct hashmap_entry
*entry_or_key
,
281 const struct refname_hash_entry
*e1
, *e2
;
283 e1
= container_of(eptr
, const struct refname_hash_entry
, ent
);
284 e2
= container_of(entry_or_key
, const struct refname_hash_entry
, ent
);
285 return strcmp(e1
->refname
, keydata
? keydata
: e2
->refname
);
288 static struct refname_hash_entry
*refname_hash_add(struct hashmap
*map
,
290 const struct object_id
*oid
)
292 struct refname_hash_entry
*ent
;
293 size_t len
= strlen(refname
);
295 FLEX_ALLOC_MEM(ent
, refname
, refname
, len
);
296 hashmap_entry_init(&ent
->ent
, strhash(refname
));
297 oidcpy(&ent
->oid
, oid
);
298 hashmap_add(map
, &ent
->ent
);
302 static int add_one_refname(const char *refname
,
303 const struct object_id
*oid
,
304 int flag
, void *cbdata
)
306 struct hashmap
*refname_map
= cbdata
;
308 (void) refname_hash_add(refname_map
, refname
, oid
);
312 static void refname_hash_init(struct hashmap
*map
)
314 hashmap_init(map
, refname_hash_entry_cmp
, NULL
, 0);
317 static int refname_hash_exists(struct hashmap
*map
, const char *refname
)
319 return !!hashmap_get_from_hash(map
, strhash(refname
), refname
);
322 static void clear_item(struct refname_hash_entry
*item
)
327 static void find_non_local_tags(const struct ref
*refs
,
331 struct hashmap existing_refs
;
332 struct hashmap remote_refs
;
333 struct oidset fetch_oids
= OIDSET_INIT
;
334 struct string_list remote_refs_list
= STRING_LIST_INIT_NODUP
;
335 struct string_list_item
*remote_ref_item
;
336 const struct ref
*ref
;
337 struct refname_hash_entry
*item
= NULL
;
338 const int quick_flags
= OBJECT_INFO_QUICK
| OBJECT_INFO_SKIP_FETCH_OBJECT
;
340 refname_hash_init(&existing_refs
);
341 refname_hash_init(&remote_refs
);
342 create_fetch_oidset(head
, &fetch_oids
);
344 for_each_ref(add_one_refname
, &existing_refs
);
345 for (ref
= refs
; ref
; ref
= ref
->next
) {
346 if (!starts_with(ref
->name
, "refs/tags/"))
350 * The peeled ref always follows the matching base
351 * ref, so if we see a peeled ref that we don't want
352 * to fetch then we can mark the ref entry in the list
353 * as one to ignore by setting util to NULL.
355 if (ends_with(ref
->name
, "^{}")) {
357 !has_object_file_with_flags(&ref
->old_oid
, quick_flags
) &&
358 !oidset_contains(&fetch_oids
, &ref
->old_oid
) &&
359 !has_object_file_with_flags(&item
->oid
, quick_flags
) &&
360 !oidset_contains(&fetch_oids
, &item
->oid
))
367 * If item is non-NULL here, then we previously saw a
368 * ref not followed by a peeled reference, so we need
369 * to check if it is a lightweight tag that we want to
373 !has_object_file_with_flags(&item
->oid
, quick_flags
) &&
374 !oidset_contains(&fetch_oids
, &item
->oid
))
379 /* skip duplicates and refs that we already have */
380 if (refname_hash_exists(&remote_refs
, ref
->name
) ||
381 refname_hash_exists(&existing_refs
, ref
->name
))
384 item
= refname_hash_add(&remote_refs
, ref
->name
, &ref
->old_oid
);
385 string_list_insert(&remote_refs_list
, ref
->name
);
387 hashmap_free_entries(&existing_refs
, struct refname_hash_entry
, ent
);
390 * We may have a final lightweight tag that needs to be
391 * checked to see if it needs fetching.
394 !has_object_file_with_flags(&item
->oid
, quick_flags
) &&
395 !oidset_contains(&fetch_oids
, &item
->oid
))
399 * For all the tags in the remote_refs_list,
400 * add them to the list of refs to be fetched
402 for_each_string_list_item(remote_ref_item
, &remote_refs_list
) {
403 const char *refname
= remote_ref_item
->string
;
405 unsigned int hash
= strhash(refname
);
407 item
= hashmap_get_entry_from_hash(&remote_refs
, hash
, refname
,
408 struct refname_hash_entry
, ent
);
410 BUG("unseen remote ref?");
412 /* Unless we have already decided to ignore this item... */
416 rm
= alloc_ref(item
->refname
);
417 rm
->peer_ref
= alloc_ref(item
->refname
);
418 oidcpy(&rm
->old_oid
, &item
->oid
);
422 hashmap_free_entries(&remote_refs
, struct refname_hash_entry
, ent
);
423 string_list_clear(&remote_refs_list
, 0);
424 oidset_clear(&fetch_oids
);
427 static struct ref
*get_ref_map(struct remote
*remote
,
428 const struct ref
*remote_refs
,
430 int tags
, int *autotags
)
434 struct ref
*ref_map
= NULL
;
435 struct ref
**tail
= &ref_map
;
437 /* opportunistically-updated references: */
438 struct ref
*orefs
= NULL
, **oref_tail
= &orefs
;
440 struct hashmap existing_refs
;
443 struct refspec
*fetch_refspec
;
445 for (i
= 0; i
< rs
->nr
; i
++) {
446 get_fetch_map(remote_refs
, &rs
->items
[i
], &tail
, 0);
447 if (rs
->items
[i
].dst
&& rs
->items
[i
].dst
[0])
450 /* Merge everything on the command line (but not --tags) */
451 for (rm
= ref_map
; rm
; rm
= rm
->next
)
452 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
455 * For any refs that we happen to be fetching via
456 * command-line arguments, the destination ref might
457 * have been missing or have been different than the
458 * remote-tracking ref that would be derived from the
459 * configured refspec. In these cases, we want to
460 * take the opportunity to update their configured
461 * remote-tracking reference. However, we do not want
462 * to mention these entries in FETCH_HEAD at all, as
463 * they would simply be duplicates of existing
464 * entries, so we set them FETCH_HEAD_IGNORE below.
466 * We compute these entries now, based only on the
467 * refspecs specified on the command line. But we add
468 * them to the list following the refspecs resulting
469 * from the tags option so that one of the latter,
470 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
471 * by ref_remove_duplicates() in favor of one of these
472 * opportunistic entries with FETCH_HEAD_IGNORE.
475 fetch_refspec
= &refmap
;
477 fetch_refspec
= &remote
->fetch
;
479 for (i
= 0; i
< fetch_refspec
->nr
; i
++)
480 get_fetch_map(ref_map
, &fetch_refspec
->items
[i
], &oref_tail
, 1);
481 } else if (refmap
.nr
) {
482 die("--refmap option is only meaningful with command-line refspec(s).");
484 /* Use the defaults */
485 struct branch
*branch
= branch_get(NULL
);
486 int has_merge
= branch_has_merge_config(branch
);
489 /* Note: has_merge implies non-NULL branch->remote_name */
490 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
491 for (i
= 0; i
< remote
->fetch
.nr
; i
++) {
492 get_fetch_map(remote_refs
, &remote
->fetch
.items
[i
], &tail
, 0);
493 if (remote
->fetch
.items
[i
].dst
&&
494 remote
->fetch
.items
[i
].dst
[0])
496 if (!i
&& !has_merge
&& ref_map
&&
497 !remote
->fetch
.items
[0].pattern
)
498 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
501 * if the remote we're fetching from is the same
502 * as given in branch.<name>.remote, we add the
503 * ref given in branch.<name>.merge, too.
505 * Note: has_merge implies non-NULL branch->remote_name
508 !strcmp(branch
->remote_name
, remote
->name
))
509 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
511 ref_map
= get_remote_ref(remote_refs
, "HEAD");
513 die(_("Couldn't find remote ref HEAD"));
514 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
515 tail
= &ref_map
->next
;
519 if (tags
== TAGS_SET
)
520 /* also fetch all tags */
521 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
522 else if (tags
== TAGS_DEFAULT
&& *autotags
)
523 find_non_local_tags(remote_refs
, &ref_map
, &tail
);
525 /* Now append any refs to be updated opportunistically: */
527 for (rm
= orefs
; rm
; rm
= rm
->next
) {
528 rm
->fetch_head_status
= FETCH_HEAD_IGNORE
;
532 ref_map
= ref_remove_duplicates(ref_map
);
534 refname_hash_init(&existing_refs
);
535 for_each_ref(add_one_refname
, &existing_refs
);
537 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
539 const char *refname
= rm
->peer_ref
->name
;
540 struct refname_hash_entry
*peer_item
;
541 unsigned int hash
= strhash(refname
);
543 peer_item
= hashmap_get_entry_from_hash(&existing_refs
,
545 struct refname_hash_entry
, ent
);
547 struct object_id
*old_oid
= &peer_item
->oid
;
548 oidcpy(&rm
->peer_ref
->old_oid
, old_oid
);
552 hashmap_free_entries(&existing_refs
, struct refname_hash_entry
, ent
);
557 #define STORE_REF_ERROR_OTHER 1
558 #define STORE_REF_ERROR_DF_CONFLICT 2
560 static int s_update_ref(const char *action
,
565 char *rla
= getenv("GIT_REFLOG_ACTION");
566 struct ref_transaction
*transaction
;
567 struct strbuf err
= STRBUF_INIT
;
568 int ret
, df_conflict
= 0;
573 rla
= default_rla
.buf
;
574 msg
= xstrfmt("%s: %s", rla
, action
);
576 transaction
= ref_transaction_begin(&err
);
578 ref_transaction_update(transaction
, ref
->name
,
580 check_old
? &ref
->old_oid
: NULL
,
584 ret
= ref_transaction_commit(transaction
, &err
);
586 df_conflict
= (ret
== TRANSACTION_NAME_CONFLICT
);
590 ref_transaction_free(transaction
);
591 strbuf_release(&err
);
595 ref_transaction_free(transaction
);
596 error("%s", err
.buf
);
597 strbuf_release(&err
);
599 return df_conflict
? STORE_REF_ERROR_DF_CONFLICT
600 : STORE_REF_ERROR_OTHER
;
603 static int refcol_width
= 10;
604 static int compact_format
;
606 static void adjust_refcol_width(const struct ref
*ref
)
608 int max
, rlen
, llen
, len
;
610 /* uptodate lines are only shown on high verbosity level */
611 if (!verbosity
&& oideq(&ref
->peer_ref
->old_oid
, &ref
->old_oid
))
614 max
= term_columns();
615 rlen
= utf8_strwidth(prettify_refname(ref
->name
));
617 llen
= utf8_strwidth(prettify_refname(ref
->peer_ref
->name
));
620 * rough estimation to see if the output line is too long and
621 * should not be counted (we can't do precise calculation
622 * anyway because we don't know if the error explanation part
623 * will be printed in update_local_ref)
625 if (compact_format
) {
629 len
= 21 /* flag and summary */ + rlen
+ 4 /* -> */ + llen
;
634 * Not precise calculation for compact mode because '*' can
635 * appear on the left hand side of '->' and shrink the column
638 if (refcol_width
< rlen
)
642 static void prepare_format_display(struct ref
*ref_map
)
645 const char *format
= "full";
647 git_config_get_string_const("fetch.output", &format
);
648 if (!strcasecmp(format
, "full"))
650 else if (!strcasecmp(format
, "compact"))
653 die(_("configuration fetch.output contains invalid value %s"),
656 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
657 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
||
659 !strcmp(rm
->name
, "HEAD"))
662 adjust_refcol_width(rm
);
666 static void print_remote_to_local(struct strbuf
*display
,
667 const char *remote
, const char *local
)
669 strbuf_addf(display
, "%-*s -> %s", refcol_width
, remote
, local
);
672 static int find_and_replace(struct strbuf
*haystack
,
674 const char *placeholder
)
676 const char *p
= NULL
;
679 nlen
= strlen(needle
);
680 if (ends_with(haystack
->buf
, needle
))
681 p
= haystack
->buf
+ haystack
->len
- nlen
;
683 p
= strstr(haystack
->buf
, needle
);
687 if (p
> haystack
->buf
&& p
[-1] != '/')
691 if (plen
> nlen
&& p
[nlen
] != '/')
694 strbuf_splice(haystack
, p
- haystack
->buf
, nlen
,
695 placeholder
, strlen(placeholder
));
699 static void print_compact(struct strbuf
*display
,
700 const char *remote
, const char *local
)
702 struct strbuf r
= STRBUF_INIT
;
703 struct strbuf l
= STRBUF_INIT
;
705 if (!strcmp(remote
, local
)) {
706 strbuf_addf(display
, "%-*s -> *", refcol_width
, remote
);
710 strbuf_addstr(&r
, remote
);
711 strbuf_addstr(&l
, local
);
713 if (!find_and_replace(&r
, local
, "*"))
714 find_and_replace(&l
, remote
, "*");
715 print_remote_to_local(display
, r
.buf
, l
.buf
);
721 static void format_display(struct strbuf
*display
, char code
,
722 const char *summary
, const char *error
,
723 const char *remote
, const char *local
,
726 int width
= (summary_width
+ strlen(summary
) - gettext_width(summary
));
728 strbuf_addf(display
, "%c %-*s ", code
, width
, summary
);
730 print_remote_to_local(display
, remote
, local
);
732 print_compact(display
, remote
, local
);
734 strbuf_addf(display
, " (%s)", error
);
737 static int update_local_ref(struct ref
*ref
,
739 const struct ref
*remote_ref
,
740 struct strbuf
*display
,
743 struct commit
*current
= NULL
, *updated
;
744 enum object_type type
;
745 struct branch
*current_branch
= branch_get(NULL
);
746 const char *pretty_ref
= prettify_refname(ref
->name
);
747 int fast_forward
= 0;
749 type
= oid_object_info(the_repository
, &ref
->new_oid
, NULL
);
751 die(_("object %s not found"), oid_to_hex(&ref
->new_oid
));
753 if (oideq(&ref
->old_oid
, &ref
->new_oid
)) {
755 format_display(display
, '=', _("[up to date]"), NULL
,
756 remote
, pretty_ref
, summary_width
);
760 if (current_branch
&&
761 !strcmp(ref
->name
, current_branch
->name
) &&
762 !(update_head_ok
|| is_bare_repository()) &&
763 !is_null_oid(&ref
->old_oid
)) {
765 * If this is the head, and it's not okay to update
766 * the head, and the old value of the head isn't empty...
768 format_display(display
, '!', _("[rejected]"),
769 _("can't fetch in current branch"),
770 remote
, pretty_ref
, summary_width
);
774 if (!is_null_oid(&ref
->old_oid
) &&
775 starts_with(ref
->name
, "refs/tags/")) {
776 if (force
|| ref
->force
) {
778 r
= s_update_ref("updating tag", ref
, 0);
779 format_display(display
, r
? '!' : 't', _("[tag update]"),
780 r
? _("unable to update local ref") : NULL
,
781 remote
, pretty_ref
, summary_width
);
784 format_display(display
, '!', _("[rejected]"), _("would clobber existing tag"),
785 remote
, pretty_ref
, summary_width
);
790 current
= lookup_commit_reference_gently(the_repository
,
792 updated
= lookup_commit_reference_gently(the_repository
,
794 if (!current
|| !updated
) {
799 * Nicely describe the new ref we're fetching.
800 * Base this on the remote's ref name, as it's
801 * more likely to follow a standard layout.
803 const char *name
= remote_ref
? remote_ref
->name
: "";
804 if (starts_with(name
, "refs/tags/")) {
806 what
= _("[new tag]");
807 } else if (starts_with(name
, "refs/heads/")) {
808 msg
= "storing head";
809 what
= _("[new branch]");
812 what
= _("[new ref]");
815 r
= s_update_ref(msg
, ref
, 0);
816 format_display(display
, r
? '!' : '*', what
,
817 r
? _("unable to update local ref") : NULL
,
818 remote
, pretty_ref
, summary_width
);
822 if (fetch_show_forced_updates
) {
823 uint64_t t_before
= getnanotime();
824 fast_forward
= in_merge_bases(current
, updated
);
825 forced_updates_ms
+= (getnanotime() - t_before
) / 1000000;
831 struct strbuf quickref
= STRBUF_INIT
;
834 strbuf_add_unique_abbrev(&quickref
, ¤t
->object
.oid
, DEFAULT_ABBREV
);
835 strbuf_addstr(&quickref
, "..");
836 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
, DEFAULT_ABBREV
);
837 r
= s_update_ref("fast-forward", ref
, 1);
838 format_display(display
, r
? '!' : ' ', quickref
.buf
,
839 r
? _("unable to update local ref") : NULL
,
840 remote
, pretty_ref
, summary_width
);
841 strbuf_release(&quickref
);
843 } else if (force
|| ref
->force
) {
844 struct strbuf quickref
= STRBUF_INIT
;
846 strbuf_add_unique_abbrev(&quickref
, ¤t
->object
.oid
, DEFAULT_ABBREV
);
847 strbuf_addstr(&quickref
, "...");
848 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
, DEFAULT_ABBREV
);
849 r
= s_update_ref("forced-update", ref
, 1);
850 format_display(display
, r
? '!' : '+', quickref
.buf
,
851 r
? _("unable to update local ref") : _("forced update"),
852 remote
, pretty_ref
, summary_width
);
853 strbuf_release(&quickref
);
856 format_display(display
, '!', _("[rejected]"), _("non-fast-forward"),
857 remote
, pretty_ref
, summary_width
);
862 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
864 struct ref
**rm
= cb_data
;
865 struct ref
*ref
= *rm
;
867 while (ref
&& ref
->status
== REF_STATUS_REJECT_SHALLOW
)
870 return -1; /* end of the list */
872 oidcpy(oid
, &ref
->old_oid
);
876 static const char warn_show_forced_updates
[] =
877 N_("Fetch normally indicates which branches had a forced update,\n"
878 "but that check has been disabled. To re-enable, use '--show-forced-updates'\n"
879 "flag or run 'git config fetch.showForcedUpdates true'.");
880 static const char warn_time_show_forced_updates
[] =
881 N_("It took %.2f seconds to check forced updates. You can use\n"
882 "'--no-show-forced-updates' or run 'git config fetch.showForcedUpdates false'\n"
883 " to avoid this check.\n");
885 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
886 int connectivity_checked
, struct ref
*ref_map
)
889 struct commit
*commit
;
890 int url_len
, i
, rc
= 0;
891 struct strbuf note
= STRBUF_INIT
;
892 const char *what
, *kind
;
895 const char *filename
= dry_run
? "/dev/null" : git_path_fetch_head(the_repository
);
897 int summary_width
= transport_summary_width(ref_map
);
899 fp
= fopen(filename
, "a");
901 return error_errno(_("cannot open %s"), filename
);
904 url
= transport_anonymize_url(raw_url
);
906 url
= xstrdup("foreign");
908 if (!connectivity_checked
) {
909 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
912 if (check_connected(iterate_ref_map
, &rm
, &opt
)) {
913 rc
= error(_("%s did not send all necessary objects\n"), url
);
918 prepare_format_display(ref_map
);
921 * We do a pass for each fetch_head_status type in their enum order, so
922 * merged entries are written before not-for-merge. That lets readers
923 * use FETCH_HEAD as a refname to refer to the ref to be merged.
925 for (want_status
= FETCH_HEAD_MERGE
;
926 want_status
<= FETCH_HEAD_IGNORE
;
928 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
929 struct ref
*ref
= NULL
;
930 const char *merge_status_marker
= "";
932 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
) {
933 if (want_status
== FETCH_HEAD_MERGE
)
934 warning(_("reject %s because shallow roots are not allowed to be updated"),
935 rm
->peer_ref
? rm
->peer_ref
->name
: rm
->name
);
939 commit
= lookup_commit_reference_gently(the_repository
,
943 rm
->fetch_head_status
= FETCH_HEAD_NOT_FOR_MERGE
;
945 if (rm
->fetch_head_status
!= want_status
)
949 ref
= alloc_ref(rm
->peer_ref
->name
);
950 oidcpy(&ref
->old_oid
, &rm
->peer_ref
->old_oid
);
951 oidcpy(&ref
->new_oid
, &rm
->old_oid
);
952 ref
->force
= rm
->peer_ref
->force
;
955 if (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)
956 check_for_new_submodule_commits(&rm
->old_oid
);
958 if (!strcmp(rm
->name
, "HEAD")) {
962 else if (skip_prefix(rm
->name
, "refs/heads/", &what
))
964 else if (skip_prefix(rm
->name
, "refs/tags/", &what
))
966 else if (skip_prefix(rm
->name
, "refs/remotes/", &what
))
967 kind
= "remote-tracking branch";
973 url_len
= strlen(url
);
974 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
977 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
983 strbuf_addf(¬e
, "%s ", kind
);
984 strbuf_addf(¬e
, "'%s' of ", what
);
986 switch (rm
->fetch_head_status
) {
987 case FETCH_HEAD_NOT_FOR_MERGE
:
988 merge_status_marker
= "not-for-merge";
990 case FETCH_HEAD_MERGE
:
991 fprintf(fp
, "%s\t%s\t%s",
992 oid_to_hex(&rm
->old_oid
),
995 for (i
= 0; i
< url_len
; ++i
)
1003 /* do not write anything to FETCH_HEAD */
1007 strbuf_reset(¬e
);
1009 rc
|= update_local_ref(ref
, what
, rm
, ¬e
,
1013 format_display(¬e
, '*',
1014 *kind
? kind
: "branch", NULL
,
1015 *what
? what
: "HEAD",
1016 "FETCH_HEAD", summary_width
);
1018 if (verbosity
>= 0 && !shown_url
) {
1019 fprintf(stderr
, _("From %.*s\n"),
1024 fprintf(stderr
, " %s\n", note
.buf
);
1029 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
1030 error(_("some local refs could not be updated; try running\n"
1031 " 'git remote prune %s' to remove any old, conflicting "
1032 "branches"), remote_name
);
1034 if (advice_fetch_show_forced_updates
) {
1035 if (!fetch_show_forced_updates
) {
1036 warning(_(warn_show_forced_updates
));
1037 } else if (forced_updates_ms
> FORCED_UPDATES_DELAY_WARNING_IN_MS
) {
1038 warning(_(warn_time_show_forced_updates
),
1039 forced_updates_ms
/ 1000.0);
1044 strbuf_release(¬e
);
1051 * We would want to bypass the object transfer altogether if
1052 * everything we are going to fetch already exists and is connected
1055 static int check_exist_and_connected(struct ref
*ref_map
)
1057 struct ref
*rm
= ref_map
;
1058 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
1062 * If we are deepening a shallow clone we already have these
1063 * objects reachable. Running rev-list here will return with
1064 * a good (0) exit status and we'll bypass the fetch that we
1065 * really need to perform. Claiming failure now will ensure
1066 * we perform the network exchange to deepen our history.
1072 * check_connected() allows objects to merely be promised, but
1073 * we need all direct targets to exist.
1075 for (r
= rm
; r
; r
= r
->next
) {
1076 if (!has_object_file_with_flags(&r
->old_oid
,
1077 OBJECT_INFO_SKIP_FETCH_OBJECT
))
1082 return check_connected(iterate_ref_map
, &rm
, &opt
);
1085 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
1087 int ret
= check_exist_and_connected(ref_map
);
1089 trace2_region_enter("fetch", "fetch_refs", the_repository
);
1090 ret
= transport_fetch_refs(transport
, ref_map
);
1091 trace2_region_leave("fetch", "fetch_refs", the_repository
);
1095 * Keep the new pack's ".keep" file around to allow the caller
1096 * time to update refs to reference the new objects.
1099 transport_unlock_pack(transport
);
1103 /* Update local refs based on the ref values fetched from a remote */
1104 static int consume_refs(struct transport
*transport
, struct ref
*ref_map
)
1106 int connectivity_checked
= transport
->smart_options
1107 ? transport
->smart_options
->connectivity_checked
: 0;
1109 trace2_region_enter("fetch", "consume_refs", the_repository
);
1110 ret
= store_updated_refs(transport
->url
,
1111 transport
->remote
->name
,
1112 connectivity_checked
,
1114 transport_unlock_pack(transport
);
1115 trace2_region_leave("fetch", "consume_refs", the_repository
);
1119 static int prune_refs(struct refspec
*rs
, struct ref
*ref_map
,
1120 const char *raw_url
)
1122 int url_len
, i
, result
= 0;
1123 struct ref
*ref
, *stale_refs
= get_stale_heads(rs
, ref_map
);
1125 int summary_width
= transport_summary_width(stale_refs
);
1126 const char *dangling_msg
= dry_run
1127 ? _(" (%s will become dangling)")
1128 : _(" (%s has become dangling)");
1131 url
= transport_anonymize_url(raw_url
);
1133 url
= xstrdup("foreign");
1135 url_len
= strlen(url
);
1136 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
1140 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
1144 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
1146 for (ref
= stale_refs
; ref
; ref
= ref
->next
)
1147 string_list_append(&refnames
, ref
->name
);
1149 result
= delete_refs("fetch: prune", &refnames
, 0);
1150 string_list_clear(&refnames
, 0);
1153 if (verbosity
>= 0) {
1154 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
1155 struct strbuf sb
= STRBUF_INIT
;
1157 fprintf(stderr
, _("From %.*s\n"), url_len
, url
);
1160 format_display(&sb
, '-', _("[deleted]"), NULL
,
1161 _("(none)"), prettify_refname(ref
->name
),
1163 fprintf(stderr
, " %s\n",sb
.buf
);
1164 strbuf_release(&sb
);
1165 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
1170 free_refs(stale_refs
);
1174 static void check_not_current_branch(struct ref
*ref_map
)
1176 struct branch
*current_branch
= branch_get(NULL
);
1178 if (is_bare_repository() || !current_branch
)
1181 for (; ref_map
; ref_map
= ref_map
->next
)
1182 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
1183 ref_map
->peer_ref
->name
))
1184 die(_("Refusing to fetch into current branch %s "
1185 "of non-bare repository"), current_branch
->refname
);
1188 static int truncate_fetch_head(void)
1190 const char *filename
= git_path_fetch_head(the_repository
);
1191 FILE *fp
= fopen_for_writing(filename
);
1194 return error_errno(_("cannot open %s"), filename
);
1199 static void set_option(struct transport
*transport
, const char *name
, const char *value
)
1201 int r
= transport_set_option(transport
, name
, value
);
1203 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1204 name
, value
, transport
->url
);
1206 warning(_("Option \"%s\" is ignored for %s\n"),
1207 name
, transport
->url
);
1211 static int add_oid(const char *refname
, const struct object_id
*oid
, int flags
,
1214 struct oid_array
*oids
= cb_data
;
1216 oid_array_append(oids
, oid
);
1220 static void add_negotiation_tips(struct git_transport_options
*smart_options
)
1222 struct oid_array
*oids
= xcalloc(1, sizeof(*oids
));
1225 for (i
= 0; i
< negotiation_tip
.nr
; i
++) {
1226 const char *s
= negotiation_tip
.items
[i
].string
;
1228 if (!has_glob_specials(s
)) {
1229 struct object_id oid
;
1230 if (get_oid(s
, &oid
))
1231 die("%s is not a valid object", s
);
1232 oid_array_append(oids
, &oid
);
1236 for_each_glob_ref(add_oid
, s
, oids
);
1237 if (old_nr
== oids
->nr
)
1238 warning("Ignoring --negotiation-tip=%s because it does not match any refs",
1241 smart_options
->negotiation_tips
= oids
;
1244 static struct transport
*prepare_transport(struct remote
*remote
, int deepen
)
1246 struct transport
*transport
;
1248 transport
= transport_get(remote
, NULL
);
1249 transport_set_verbosity(transport
, verbosity
, progress
);
1250 transport
->family
= family
;
1252 set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
1254 set_option(transport
, TRANS_OPT_KEEP
, "yes");
1256 set_option(transport
, TRANS_OPT_DEPTH
, depth
);
1257 if (deepen
&& deepen_since
)
1258 set_option(transport
, TRANS_OPT_DEEPEN_SINCE
, deepen_since
);
1259 if (deepen
&& deepen_not
.nr
)
1260 set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1261 (const char *)&deepen_not
);
1262 if (deepen_relative
)
1263 set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, "yes");
1265 set_option(transport
, TRANS_OPT_UPDATE_SHALLOW
, "yes");
1266 if (filter_options
.choice
) {
1268 expand_list_objects_filter_spec(&filter_options
);
1269 set_option(transport
, TRANS_OPT_LIST_OBJECTS_FILTER
, spec
);
1270 set_option(transport
, TRANS_OPT_FROM_PROMISOR
, "1");
1272 if (negotiation_tip
.nr
) {
1273 if (transport
->smart_options
)
1274 add_negotiation_tips(transport
->smart_options
);
1276 warning("Ignoring --negotiation-tip because the protocol does not support it.");
1281 static void backfill_tags(struct transport
*transport
, struct ref
*ref_map
)
1286 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1287 * when remote helper is used (setting it to an empty string
1288 * is not unsetting). We could extend the remote helper
1289 * protocol for that, but for now, just force a new connection
1290 * without deepen-since. Similar story for deepen-not.
1292 cannot_reuse
= transport
->cannot_reuse
||
1293 deepen_since
|| deepen_not
.nr
;
1295 gsecondary
= prepare_transport(transport
->remote
, 0);
1296 transport
= gsecondary
;
1299 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
1300 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
1301 transport_set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, NULL
);
1302 if (!fetch_refs(transport
, ref_map
))
1303 consume_refs(transport
, ref_map
);
1306 transport_disconnect(gsecondary
);
1311 static int do_fetch(struct transport
*transport
,
1314 struct ref
*ref_map
;
1315 int autotags
= (transport
->remote
->fetch_tags
== 1);
1317 const struct ref
*remote_refs
;
1318 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
1319 int must_list_refs
= 1;
1321 if (tags
== TAGS_DEFAULT
) {
1322 if (transport
->remote
->fetch_tags
== 2)
1324 if (transport
->remote
->fetch_tags
== -1)
1328 /* if not appending, truncate FETCH_HEAD */
1329 if (!append
&& !dry_run
) {
1330 retcode
= truncate_fetch_head();
1338 refspec_ref_prefixes(rs
, &ref_prefixes
);
1341 * We can avoid listing refs if all of them are exact
1345 for (i
= 0; i
< rs
->nr
; i
++) {
1346 if (!rs
->items
[i
].exact_sha1
) {
1351 } else if (transport
->remote
&& transport
->remote
->fetch
.nr
)
1352 refspec_ref_prefixes(&transport
->remote
->fetch
, &ref_prefixes
);
1354 if (tags
== TAGS_SET
|| tags
== TAGS_DEFAULT
) {
1356 if (ref_prefixes
.argc
)
1357 argv_array_push(&ref_prefixes
, "refs/tags/");
1360 if (must_list_refs
) {
1361 trace2_region_enter("fetch", "remote_refs", the_repository
);
1362 remote_refs
= transport_get_remote_refs(transport
, &ref_prefixes
);
1363 trace2_region_leave("fetch", "remote_refs", the_repository
);
1367 argv_array_clear(&ref_prefixes
);
1369 ref_map
= get_ref_map(transport
->remote
, remote_refs
, rs
,
1371 if (!update_head_ok
)
1372 check_not_current_branch(ref_map
);
1374 if (tags
== TAGS_DEFAULT
&& autotags
)
1375 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1378 * We only prune based on refspecs specified
1379 * explicitly (via command line or configuration); we
1380 * don't care whether --tags was specified.
1383 prune_refs(rs
, ref_map
, transport
->url
);
1385 prune_refs(&transport
->remote
->fetch
,
1390 if (fetch_refs(transport
, ref_map
) || consume_refs(transport
, ref_map
)) {
1397 struct branch
*branch
= branch_get("HEAD");
1399 struct ref
*source_ref
= NULL
;
1402 * We're setting the upstream configuration for the
1403 * current branch. The relevant upstream is the
1404 * fetched branch that is meant to be merged with the
1405 * current one, i.e. the one fetched to FETCH_HEAD.
1407 * When there are several such branches, consider the
1408 * request ambiguous and err on the safe side by doing
1409 * nothing and just emit a warning.
1411 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
1412 if (!rm
->peer_ref
) {
1414 warning(_("multiple branches detected, incompatible with --set-upstream"));
1422 if (!strcmp(source_ref
->name
, "HEAD") ||
1423 starts_with(source_ref
->name
, "refs/heads/"))
1424 install_branch_config(0,
1426 transport
->remote
->name
,
1428 else if (starts_with(source_ref
->name
, "refs/remotes/"))
1429 warning(_("not setting upstream for a remote remote-tracking branch"));
1430 else if (starts_with(source_ref
->name
, "refs/tags/"))
1431 warning(_("not setting upstream for a remote tag"));
1433 warning(_("unknown branch type"));
1435 warning(_("no source branch found.\n"
1436 "you need to specify exactly one branch with the --set-upstream option."));
1442 /* if neither --no-tags nor --tags was specified, do automated tag
1444 if (tags
== TAGS_DEFAULT
&& autotags
) {
1445 struct ref
**tail
= &ref_map
;
1447 find_non_local_tags(remote_refs
, &ref_map
, &tail
);
1449 backfill_tags(transport
, ref_map
);
1457 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
1459 struct string_list
*list
= priv
;
1460 if (!remote
->skip_default_update
)
1461 string_list_append(list
, remote
->name
);
1465 struct remote_group_data
{
1467 struct string_list
*list
;
1470 static int get_remote_group(const char *key
, const char *value
, void *priv
)
1472 struct remote_group_data
*g
= priv
;
1474 if (skip_prefix(key
, "remotes.", &key
) && !strcmp(key
, g
->name
)) {
1475 /* split list by white space */
1477 size_t wordlen
= strcspn(value
, " \t\n");
1480 string_list_append_nodup(g
->list
,
1481 xstrndup(value
, wordlen
));
1482 value
+= wordlen
+ (value
[wordlen
] != '\0');
1489 static int add_remote_or_group(const char *name
, struct string_list
*list
)
1491 int prev_nr
= list
->nr
;
1492 struct remote_group_data g
;
1493 g
.name
= name
; g
.list
= list
;
1495 git_config(get_remote_group
, &g
);
1496 if (list
->nr
== prev_nr
) {
1497 struct remote
*remote
= remote_get(name
);
1498 if (!remote_is_configured(remote
, 0))
1500 string_list_append(list
, remote
->name
);
1505 static void add_options_to_argv(struct argv_array
*argv
)
1508 argv_array_push(argv
, "--dry-run");
1510 argv_array_push(argv
, prune
? "--prune" : "--no-prune");
1511 if (prune_tags
!= -1)
1512 argv_array_push(argv
, prune_tags
? "--prune-tags" : "--no-prune-tags");
1514 argv_array_push(argv
, "--update-head-ok");
1516 argv_array_push(argv
, "--force");
1518 argv_array_push(argv
, "--keep");
1519 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
1520 argv_array_push(argv
, "--recurse-submodules");
1521 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
1522 argv_array_push(argv
, "--recurse-submodules=on-demand");
1523 if (tags
== TAGS_SET
)
1524 argv_array_push(argv
, "--tags");
1525 else if (tags
== TAGS_UNSET
)
1526 argv_array_push(argv
, "--no-tags");
1528 argv_array_push(argv
, "-v");
1530 argv_array_push(argv
, "-v");
1531 else if (verbosity
< 0)
1532 argv_array_push(argv
, "-q");
1536 /* Fetch multiple remotes in parallel */
1538 struct parallel_fetch_state
{
1540 struct string_list
*remotes
;
1544 static int fetch_next_remote(struct child_process
*cp
, struct strbuf
*out
,
1545 void *cb
, void **task_cb
)
1547 struct parallel_fetch_state
*state
= cb
;
1550 if (state
->next
< 0 || state
->next
>= state
->remotes
->nr
)
1553 remote
= state
->remotes
->items
[state
->next
++].string
;
1556 argv_array_pushv(&cp
->args
, state
->argv
);
1557 argv_array_push(&cp
->args
, remote
);
1561 printf(_("Fetching %s\n"), remote
);
1566 static int fetch_failed_to_start(struct strbuf
*out
, void *cb
, void *task_cb
)
1568 struct parallel_fetch_state
*state
= cb
;
1569 const char *remote
= task_cb
;
1571 state
->result
= error(_("Could not fetch %s"), remote
);
1576 static int fetch_finished(int result
, struct strbuf
*out
,
1577 void *cb
, void *task_cb
)
1579 struct parallel_fetch_state
*state
= cb
;
1580 const char *remote
= task_cb
;
1583 strbuf_addf(out
, _("could not fetch '%s' (exit code: %d)\n"),
1591 static int fetch_multiple(struct string_list
*list
, int max_children
)
1594 struct argv_array argv
= ARGV_ARRAY_INIT
;
1596 if (!append
&& !dry_run
) {
1597 int errcode
= truncate_fetch_head();
1602 argv_array_pushl(&argv
, "fetch", "--append", "--no-auto-gc",
1603 "--no-write-commit-graph", NULL
);
1604 add_options_to_argv(&argv
);
1606 if (max_children
!= 1 && list
->nr
!= 1) {
1607 struct parallel_fetch_state state
= { argv
.argv
, list
, 0, 0 };
1609 argv_array_push(&argv
, "--end-of-options");
1610 result
= run_processes_parallel_tr2(max_children
,
1612 &fetch_failed_to_start
,
1615 "fetch", "parallel/fetch");
1618 result
= state
.result
;
1620 for (i
= 0; i
< list
->nr
; i
++) {
1621 const char *name
= list
->items
[i
].string
;
1622 argv_array_push(&argv
, name
);
1624 printf(_("Fetching %s\n"), name
);
1625 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
1626 error(_("Could not fetch %s"), name
);
1629 argv_array_pop(&argv
);
1632 argv_array_clear(&argv
);
1637 * Fetching from the promisor remote should use the given filter-spec
1638 * or inherit the default filter-spec from the config.
1640 static inline void fetch_one_setup_partial(struct remote
*remote
)
1643 * Explicit --no-filter argument overrides everything, regardless
1644 * of any prior partial clones and fetches.
1646 if (filter_options
.no_filter
)
1650 * If no prior partial clone/fetch and the current fetch DID NOT
1651 * request a partial-fetch, do a normal fetch.
1653 if (!has_promisor_remote() && !filter_options
.choice
)
1657 * If this is a partial-fetch request, we enable partial on
1658 * this repo if not already enabled and remember the given
1659 * filter-spec as the default for subsequent fetches to this
1662 if (filter_options
.choice
) {
1663 partial_clone_register(remote
->name
, &filter_options
);
1668 * Do a partial-fetch from the promisor remote using either the
1669 * explicitly given filter-spec or inherit the filter-spec from
1672 if (!filter_options
.choice
)
1673 partial_clone_get_default_filter_spec(&filter_options
, remote
->name
);
1677 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
, int prune_tags_ok
)
1679 struct refspec rs
= REFSPEC_INIT_FETCH
;
1682 int maybe_prune_tags
;
1683 int remote_via_config
= remote_is_configured(remote
, 0);
1686 die(_("No remote repository specified. Please, specify either a URL or a\n"
1687 "remote name from which new revisions should be fetched."));
1689 gtransport
= prepare_transport(remote
, 1);
1692 /* no command line request */
1693 if (0 <= remote
->prune
)
1694 prune
= remote
->prune
;
1695 else if (0 <= fetch_prune_config
)
1696 prune
= fetch_prune_config
;
1698 prune
= PRUNE_BY_DEFAULT
;
1701 if (prune_tags
< 0) {
1702 /* no command line request */
1703 if (0 <= remote
->prune_tags
)
1704 prune_tags
= remote
->prune_tags
;
1705 else if (0 <= fetch_prune_tags_config
)
1706 prune_tags
= fetch_prune_tags_config
;
1708 prune_tags
= PRUNE_TAGS_BY_DEFAULT
;
1711 maybe_prune_tags
= prune_tags_ok
&& prune_tags
;
1712 if (maybe_prune_tags
&& remote_via_config
)
1713 refspec_append(&remote
->fetch
, TAG_REFSPEC
);
1715 if (maybe_prune_tags
&& (argc
|| !remote_via_config
))
1716 refspec_append(&rs
, TAG_REFSPEC
);
1718 for (i
= 0; i
< argc
; i
++) {
1719 if (!strcmp(argv
[i
], "tag")) {
1723 die(_("You need to specify a tag name."));
1725 tag
= xstrfmt("refs/tags/%s:refs/tags/%s",
1727 refspec_append(&rs
, tag
);
1730 refspec_append(&rs
, argv
[i
]);
1734 if (server_options
.nr
)
1735 gtransport
->server_options
= &server_options
;
1737 sigchain_push_common(unlock_pack_on_signal
);
1738 atexit(unlock_pack
);
1739 sigchain_push(SIGPIPE
, SIG_IGN
);
1740 exit_code
= do_fetch(gtransport
, &rs
);
1741 sigchain_pop(SIGPIPE
);
1743 transport_disconnect(gtransport
);
1748 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
1751 struct string_list list
= STRING_LIST_INIT_DUP
;
1752 struct remote
*remote
= NULL
;
1754 int prune_tags_ok
= 1;
1755 struct argv_array argv_gc_auto
= ARGV_ARRAY_INIT
;
1757 packet_trace_identity("fetch");
1759 /* Record the command line for the reflog */
1760 strbuf_addstr(&default_rla
, "fetch");
1761 for (i
= 1; i
< argc
; i
++)
1762 strbuf_addf(&default_rla
, " %s", argv
[i
]);
1764 fetch_config_from_gitmodules(&submodule_fetch_jobs_config
,
1765 &recurse_submodules
);
1766 git_config(git_fetch_config
, NULL
);
1768 argc
= parse_options(argc
, argv
, prefix
,
1769 builtin_fetch_options
, builtin_fetch_usage
, 0);
1771 if (deepen_relative
) {
1772 if (deepen_relative
< 0)
1773 die(_("Negative depth in --deepen is not supported"));
1775 die(_("--deepen and --depth are mutually exclusive"));
1776 depth
= xstrfmt("%d", deepen_relative
);
1780 die(_("--depth and --unshallow cannot be used together"));
1781 else if (!is_repository_shallow(the_repository
))
1782 die(_("--unshallow on a complete repository does not make sense"));
1784 depth
= xstrfmt("%d", INFINITE_DEPTH
);
1787 /* no need to be strict, transport_set_option() will validate it again */
1788 if (depth
&& atoi(depth
) < 1)
1789 die(_("depth %s is not a positive number"), depth
);
1790 if (depth
|| deepen_since
|| deepen_not
.nr
)
1793 if (filter_options
.choice
&& !has_promisor_remote())
1794 die("--filter can only be used when extensions.partialClone is set");
1798 die(_("fetch --all does not take a repository argument"));
1800 die(_("fetch --all does not make sense with refspecs"));
1801 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1802 } else if (argc
== 0) {
1803 /* No arguments -- use default remote */
1804 remote
= remote_get(NULL
);
1805 } else if (multiple
) {
1806 /* All arguments are assumed to be remotes or groups */
1807 for (i
= 0; i
< argc
; i
++)
1808 if (!add_remote_or_group(argv
[i
], &list
))
1809 die(_("No such remote or remote group: %s"), argv
[i
]);
1811 /* Single remote or group */
1812 (void) add_remote_or_group(argv
[0], &list
);
1814 /* More than one remote */
1816 die(_("Fetching a group and specifying refspecs does not make sense"));
1818 /* Zero or one remotes */
1819 remote
= remote_get(argv
[0]);
1820 prune_tags_ok
= (argc
== 1);
1827 if (filter_options
.choice
|| has_promisor_remote())
1828 fetch_one_setup_partial(remote
);
1829 result
= fetch_one(remote
, argc
, argv
, prune_tags_ok
);
1831 int max_children
= max_jobs
;
1833 if (filter_options
.choice
)
1834 die(_("--filter can only be used with the remote "
1835 "configured in extensions.partialclone"));
1837 if (max_children
< 0)
1838 max_children
= fetch_parallel_config
;
1840 /* TODO should this also die if we have a previous partial-clone? */
1841 result
= fetch_multiple(&list
, max_children
);
1844 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1845 struct argv_array options
= ARGV_ARRAY_INIT
;
1846 int max_children
= max_jobs
;
1848 if (max_children
< 0)
1849 max_children
= submodule_fetch_jobs_config
;
1850 if (max_children
< 0)
1851 max_children
= fetch_parallel_config
;
1853 add_options_to_argv(&options
);
1854 result
= fetch_populated_submodules(the_repository
,
1858 recurse_submodules_default
,
1861 argv_array_clear(&options
);
1864 string_list_clear(&list
, 0);
1866 prepare_repo_settings(the_repository
);
1867 if (fetch_write_commit_graph
> 0 ||
1868 (fetch_write_commit_graph
< 0 &&
1869 the_repository
->settings
.fetch_write_commit_graph
)) {
1870 int commit_graph_flags
= COMMIT_GRAPH_WRITE_SPLIT
;
1873 commit_graph_flags
|= COMMIT_GRAPH_WRITE_PROGRESS
;
1875 write_commit_graph_reachable(the_repository
->objects
->odb
,
1880 close_object_store(the_repository
->objects
);
1882 if (enable_auto_gc
) {
1883 argv_array_pushl(&argv_gc_auto
, "gc", "--auto", NULL
);
1885 argv_array_push(&argv_gc_auto
, "--quiet");
1886 run_command_v_opt(argv_gc_auto
.argv
, RUN_GIT_CMD
);
1887 argv_array_clear(&argv_gc_auto
);