6 #include "repository.h"
10 #include "string-list.h"
12 #include "transport.h"
13 #include "run-command.h"
14 #include "parse-options.h"
16 #include "submodule-config.h"
17 #include "submodule.h"
18 #include "connected.h"
19 #include "argv-array.h"
22 #include "list-objects-filter-options.h"
24 static const char * const builtin_fetch_usage
[] = {
25 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
26 N_("git fetch [<options>] <group>"),
27 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
28 N_("git fetch --all [<options>]"),
38 static int fetch_prune_config
= -1; /* unspecified */
39 static int prune
= -1; /* unspecified */
40 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
42 static int all
, append
, dry_run
, force
, keep
, multiple
, update_head_ok
, verbosity
, deepen_relative
;
43 static int progress
= -1;
44 static int tags
= TAGS_DEFAULT
, unshallow
, update_shallow
, deepen
;
45 static int max_children
= 1;
46 static enum transport_family family
;
47 static const char *depth
;
48 static const char *deepen_since
;
49 static const char *upload_pack
;
50 static struct string_list deepen_not
= STRING_LIST_INIT_NODUP
;
51 static struct strbuf default_rla
= STRBUF_INIT
;
52 static struct transport
*gtransport
;
53 static struct transport
*gsecondary
;
54 static const char *submodule_prefix
= "";
55 static int recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
56 static int recurse_submodules_default
= RECURSE_SUBMODULES_ON_DEMAND
;
57 static int shown_url
= 0;
58 static int refmap_alloc
, refmap_nr
;
59 static const char **refmap_array
;
60 static struct list_objects_filter_options filter_options
;
62 static int git_fetch_config(const char *k
, const char *v
, void *cb
)
64 if (!strcmp(k
, "fetch.prune")) {
65 fetch_prune_config
= git_config_bool(k
, v
);
69 if (!strcmp(k
, "submodule.recurse")) {
70 int r
= git_config_bool(k
, v
) ?
71 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
72 recurse_submodules
= r
;
75 if (!strcmp(k
, "submodule.fetchjobs")) {
76 max_children
= parse_submodule_fetchjobs(k
, v
);
78 } else if (!strcmp(k
, "fetch.recursesubmodules")) {
79 recurse_submodules
= parse_fetch_recurse_submodules_arg(k
, v
);
83 return git_default_config(k
, v
, cb
);
86 static int gitmodules_fetch_config(const char *var
, const char *value
, void *cb
)
88 if (!strcmp(var
, "submodule.fetchjobs")) {
89 max_children
= parse_submodule_fetchjobs(var
, value
);
91 } else if (!strcmp(var
, "fetch.recursesubmodules")) {
92 recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
99 static int parse_refmap_arg(const struct option
*opt
, const char *arg
, int unset
)
101 ALLOC_GROW(refmap_array
, refmap_nr
+ 1, refmap_alloc
);
104 * "git fetch --refmap='' origin foo"
105 * can be used to tell the command not to store anywhere
108 refmap_array
[refmap_nr
++] = arg
;
112 static struct option builtin_fetch_options
[] = {
113 OPT__VERBOSITY(&verbosity
),
114 OPT_BOOL(0, "all", &all
,
115 N_("fetch from all remotes")),
116 OPT_BOOL('a', "append", &append
,
117 N_("append to .git/FETCH_HEAD instead of overwriting")),
118 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
119 N_("path to upload pack on remote end")),
120 OPT__FORCE(&force
, N_("force overwrite of local branch")),
121 OPT_BOOL('m', "multiple", &multiple
,
122 N_("fetch from multiple remotes")),
123 OPT_SET_INT('t', "tags", &tags
,
124 N_("fetch all tags and associated objects"), TAGS_SET
),
125 OPT_SET_INT('n', NULL
, &tags
,
126 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
127 OPT_INTEGER('j', "jobs", &max_children
,
128 N_("number of submodules fetched in parallel")),
129 OPT_BOOL('p', "prune", &prune
,
130 N_("prune remote-tracking branches no longer on remote")),
131 { OPTION_CALLBACK
, 0, "recurse-submodules", &recurse_submodules
, N_("on-demand"),
132 N_("control recursive fetching of submodules"),
133 PARSE_OPT_OPTARG
, option_fetch_parse_recurse_submodules
},
134 OPT_BOOL(0, "dry-run", &dry_run
,
136 OPT_BOOL('k', "keep", &keep
, N_("keep downloaded pack")),
137 OPT_BOOL('u', "update-head-ok", &update_head_ok
,
138 N_("allow updating of HEAD ref")),
139 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
140 OPT_STRING(0, "depth", &depth
, N_("depth"),
141 N_("deepen history of shallow clone")),
142 OPT_STRING(0, "shallow-since", &deepen_since
, N_("time"),
143 N_("deepen history of shallow repository based on time")),
144 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not
, N_("revision"),
145 N_("deepen history of shallow clone, excluding rev")),
146 OPT_INTEGER(0, "deepen", &deepen_relative
,
147 N_("deepen history of shallow clone")),
148 { OPTION_SET_INT
, 0, "unshallow", &unshallow
, NULL
,
149 N_("convert to a complete repository"),
150 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, NULL
, 1 },
151 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
152 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
153 { OPTION_CALLBACK
, 0, "recurse-submodules-default",
154 &recurse_submodules_default
, N_("on-demand"),
155 N_("default for recursive fetching of submodules "
156 "(lower priority than config files)"),
157 PARSE_OPT_HIDDEN
, option_fetch_parse_recurse_submodules
},
158 OPT_BOOL(0, "update-shallow", &update_shallow
,
159 N_("accept refs that update .git/shallow")),
160 { OPTION_CALLBACK
, 0, "refmap", NULL
, N_("refmap"),
161 N_("specify fetch refmap"), PARSE_OPT_NONEG
, parse_refmap_arg
},
162 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
163 TRANSPORT_FAMILY_IPV4
),
164 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
165 TRANSPORT_FAMILY_IPV6
),
166 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
170 static void unlock_pack(void)
173 transport_unlock_pack(gtransport
);
175 transport_unlock_pack(gsecondary
);
178 static void unlock_pack_on_signal(int signo
)
185 static void add_merge_config(struct ref
**head
,
186 const struct ref
*remote_refs
,
187 struct branch
*branch
,
192 for (i
= 0; i
< branch
->merge_nr
; i
++) {
193 struct ref
*rm
, **old_tail
= *tail
;
194 struct refspec refspec
;
196 for (rm
= *head
; rm
; rm
= rm
->next
) {
197 if (branch_merge_matches(branch
, i
, rm
->name
)) {
198 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
206 * Not fetched to a remote-tracking branch? We need to fetch
207 * it anyway to allow this branch's "branch.$name.merge"
208 * to be honored by 'git pull', but we do not have to
209 * fail if branch.$name.merge is misconfigured to point
210 * at a nonexisting branch. If we were indeed called by
211 * 'git pull', it will notice the misconfiguration because
212 * there is no entry in the resulting FETCH_HEAD marked
215 memset(&refspec
, 0, sizeof(refspec
));
216 refspec
.src
= branch
->merge
[i
]->src
;
217 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
218 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
219 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
223 static int add_existing(const char *refname
, const struct object_id
*oid
,
224 int flag
, void *cbdata
)
226 struct string_list
*list
= (struct string_list
*)cbdata
;
227 struct string_list_item
*item
= string_list_insert(list
, refname
);
228 struct object_id
*old_oid
= xmalloc(sizeof(*old_oid
));
230 oidcpy(old_oid
, oid
);
231 item
->util
= old_oid
;
235 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
237 struct ref
*rm
= *head
;
239 if (!hashcmp(rm
->old_oid
.hash
, sha1
))
246 static void find_non_local_tags(struct transport
*transport
,
250 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
251 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
252 const struct ref
*ref
;
253 struct string_list_item
*item
= NULL
;
255 for_each_ref(add_existing
, &existing_refs
);
256 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
257 if (!starts_with(ref
->name
, "refs/tags/"))
261 * The peeled ref always follows the matching base
262 * ref, so if we see a peeled ref that we don't want
263 * to fetch then we can mark the ref entry in the list
264 * as one to ignore by setting util to NULL.
266 if (ends_with(ref
->name
, "^{}")) {
268 !has_object_file_with_flags(&ref
->old_oid
,
269 OBJECT_INFO_QUICK
) &&
270 !will_fetch(head
, ref
->old_oid
.hash
) &&
271 !has_sha1_file_with_flags(item
->util
,
272 OBJECT_INFO_QUICK
) &&
273 !will_fetch(head
, item
->util
))
280 * If item is non-NULL here, then we previously saw a
281 * ref not followed by a peeled reference, so we need
282 * to check if it is a lightweight tag that we want to
286 !has_sha1_file_with_flags(item
->util
, OBJECT_INFO_QUICK
) &&
287 !will_fetch(head
, item
->util
))
292 /* skip duplicates and refs that we already have */
293 if (string_list_has_string(&remote_refs
, ref
->name
) ||
294 string_list_has_string(&existing_refs
, ref
->name
))
297 item
= string_list_insert(&remote_refs
, ref
->name
);
298 item
->util
= (void *)&ref
->old_oid
;
300 string_list_clear(&existing_refs
, 1);
303 * We may have a final lightweight tag that needs to be
304 * checked to see if it needs fetching.
307 !has_sha1_file_with_flags(item
->util
, OBJECT_INFO_QUICK
) &&
308 !will_fetch(head
, item
->util
))
312 * For all the tags in the remote_refs string list,
313 * add them to the list of refs to be fetched
315 for_each_string_list_item(item
, &remote_refs
) {
316 /* Unless we have already decided to ignore this item... */
319 struct ref
*rm
= alloc_ref(item
->string
);
320 rm
->peer_ref
= alloc_ref(item
->string
);
321 oidcpy(&rm
->old_oid
, item
->util
);
327 string_list_clear(&remote_refs
, 0);
330 static struct ref
*get_ref_map(struct transport
*transport
,
331 struct refspec
*refspecs
, int refspec_count
,
332 int tags
, int *autotags
)
336 struct ref
*ref_map
= NULL
;
337 struct ref
**tail
= &ref_map
;
339 /* opportunistically-updated references: */
340 struct ref
*orefs
= NULL
, **oref_tail
= &orefs
;
342 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
345 struct refspec
*fetch_refspec
;
346 int fetch_refspec_nr
;
348 for (i
= 0; i
< refspec_count
; i
++) {
349 get_fetch_map(remote_refs
, &refspecs
[i
], &tail
, 0);
350 if (refspecs
[i
].dst
&& refspecs
[i
].dst
[0])
353 /* Merge everything on the command line (but not --tags) */
354 for (rm
= ref_map
; rm
; rm
= rm
->next
)
355 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
358 * For any refs that we happen to be fetching via
359 * command-line arguments, the destination ref might
360 * have been missing or have been different than the
361 * remote-tracking ref that would be derived from the
362 * configured refspec. In these cases, we want to
363 * take the opportunity to update their configured
364 * remote-tracking reference. However, we do not want
365 * to mention these entries in FETCH_HEAD at all, as
366 * they would simply be duplicates of existing
367 * entries, so we set them FETCH_HEAD_IGNORE below.
369 * We compute these entries now, based only on the
370 * refspecs specified on the command line. But we add
371 * them to the list following the refspecs resulting
372 * from the tags option so that one of the latter,
373 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
374 * by ref_remove_duplicates() in favor of one of these
375 * opportunistic entries with FETCH_HEAD_IGNORE.
378 fetch_refspec
= parse_fetch_refspec(refmap_nr
, refmap_array
);
379 fetch_refspec_nr
= refmap_nr
;
381 fetch_refspec
= transport
->remote
->fetch
;
382 fetch_refspec_nr
= transport
->remote
->fetch_refspec_nr
;
385 for (i
= 0; i
< fetch_refspec_nr
; i
++)
386 get_fetch_map(ref_map
, &fetch_refspec
[i
], &oref_tail
, 1);
387 } else if (refmap_array
) {
388 die("--refmap option is only meaningful with command-line refspec(s).");
390 /* Use the defaults */
391 struct remote
*remote
= transport
->remote
;
392 struct branch
*branch
= branch_get(NULL
);
393 int has_merge
= branch_has_merge_config(branch
);
395 (remote
->fetch_refspec_nr
||
396 /* Note: has_merge implies non-NULL branch->remote_name */
397 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
398 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
399 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
400 if (remote
->fetch
[i
].dst
&&
401 remote
->fetch
[i
].dst
[0])
403 if (!i
&& !has_merge
&& ref_map
&&
404 !remote
->fetch
[0].pattern
)
405 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
408 * if the remote we're fetching from is the same
409 * as given in branch.<name>.remote, we add the
410 * ref given in branch.<name>.merge, too.
412 * Note: has_merge implies non-NULL branch->remote_name
415 !strcmp(branch
->remote_name
, remote
->name
))
416 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
418 ref_map
= get_remote_ref(remote_refs
, "HEAD");
420 die(_("Couldn't find remote ref HEAD"));
421 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
422 tail
= &ref_map
->next
;
426 if (tags
== TAGS_SET
)
427 /* also fetch all tags */
428 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
429 else if (tags
== TAGS_DEFAULT
&& *autotags
)
430 find_non_local_tags(transport
, &ref_map
, &tail
);
432 /* Now append any refs to be updated opportunistically: */
434 for (rm
= orefs
; rm
; rm
= rm
->next
) {
435 rm
->fetch_head_status
= FETCH_HEAD_IGNORE
;
439 return ref_remove_duplicates(ref_map
);
442 #define STORE_REF_ERROR_OTHER 1
443 #define STORE_REF_ERROR_DF_CONFLICT 2
445 static int s_update_ref(const char *action
,
450 char *rla
= getenv("GIT_REFLOG_ACTION");
451 struct ref_transaction
*transaction
;
452 struct strbuf err
= STRBUF_INIT
;
453 int ret
, df_conflict
= 0;
458 rla
= default_rla
.buf
;
459 msg
= xstrfmt("%s: %s", rla
, action
);
461 transaction
= ref_transaction_begin(&err
);
463 ref_transaction_update(transaction
, ref
->name
,
465 check_old
? &ref
->old_oid
: NULL
,
469 ret
= ref_transaction_commit(transaction
, &err
);
471 df_conflict
= (ret
== TRANSACTION_NAME_CONFLICT
);
475 ref_transaction_free(transaction
);
476 strbuf_release(&err
);
480 ref_transaction_free(transaction
);
481 error("%s", err
.buf
);
482 strbuf_release(&err
);
484 return df_conflict
? STORE_REF_ERROR_DF_CONFLICT
485 : STORE_REF_ERROR_OTHER
;
488 static int refcol_width
= 10;
489 static int compact_format
;
491 static void adjust_refcol_width(const struct ref
*ref
)
493 int max
, rlen
, llen
, len
;
495 /* uptodate lines are only shown on high verbosity level */
496 if (!verbosity
&& !oidcmp(&ref
->peer_ref
->old_oid
, &ref
->old_oid
))
499 max
= term_columns();
500 rlen
= utf8_strwidth(prettify_refname(ref
->name
));
502 llen
= utf8_strwidth(prettify_refname(ref
->peer_ref
->name
));
505 * rough estimation to see if the output line is too long and
506 * should not be counted (we can't do precise calculation
507 * anyway because we don't know if the error explanation part
508 * will be printed in update_local_ref)
510 if (compact_format
) {
514 len
= 21 /* flag and summary */ + rlen
+ 4 /* -> */ + llen
;
519 * Not precise calculation for compact mode because '*' can
520 * appear on the left hand side of '->' and shrink the column
523 if (refcol_width
< rlen
)
527 static void prepare_format_display(struct ref
*ref_map
)
530 const char *format
= "full";
532 git_config_get_string_const("fetch.output", &format
);
533 if (!strcasecmp(format
, "full"))
535 else if (!strcasecmp(format
, "compact"))
538 die(_("configuration fetch.output contains invalid value %s"),
541 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
542 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
||
544 !strcmp(rm
->name
, "HEAD"))
547 adjust_refcol_width(rm
);
551 static void print_remote_to_local(struct strbuf
*display
,
552 const char *remote
, const char *local
)
554 strbuf_addf(display
, "%-*s -> %s", refcol_width
, remote
, local
);
557 static int find_and_replace(struct strbuf
*haystack
,
559 const char *placeholder
)
561 const char *p
= strstr(haystack
->buf
, needle
);
567 if (p
> haystack
->buf
&& p
[-1] != '/')
571 nlen
= strlen(needle
);
572 if (plen
> nlen
&& p
[nlen
] != '/')
575 strbuf_splice(haystack
, p
- haystack
->buf
, nlen
,
576 placeholder
, strlen(placeholder
));
580 static void print_compact(struct strbuf
*display
,
581 const char *remote
, const char *local
)
583 struct strbuf r
= STRBUF_INIT
;
584 struct strbuf l
= STRBUF_INIT
;
586 if (!strcmp(remote
, local
)) {
587 strbuf_addf(display
, "%-*s -> *", refcol_width
, remote
);
591 strbuf_addstr(&r
, remote
);
592 strbuf_addstr(&l
, local
);
594 if (!find_and_replace(&r
, local
, "*"))
595 find_and_replace(&l
, remote
, "*");
596 print_remote_to_local(display
, r
.buf
, l
.buf
);
602 static void format_display(struct strbuf
*display
, char code
,
603 const char *summary
, const char *error
,
604 const char *remote
, const char *local
,
607 int width
= (summary_width
+ strlen(summary
) - gettext_width(summary
));
609 strbuf_addf(display
, "%c %-*s ", code
, width
, summary
);
611 print_remote_to_local(display
, remote
, local
);
613 print_compact(display
, remote
, local
);
615 strbuf_addf(display
, " (%s)", error
);
618 static int update_local_ref(struct ref
*ref
,
620 const struct ref
*remote_ref
,
621 struct strbuf
*display
,
624 struct commit
*current
= NULL
, *updated
;
625 enum object_type type
;
626 struct branch
*current_branch
= branch_get(NULL
);
627 const char *pretty_ref
= prettify_refname(ref
->name
);
629 type
= sha1_object_info(ref
->new_oid
.hash
, NULL
);
631 die(_("object %s not found"), oid_to_hex(&ref
->new_oid
));
633 if (!oidcmp(&ref
->old_oid
, &ref
->new_oid
)) {
635 format_display(display
, '=', _("[up to date]"), NULL
,
636 remote
, pretty_ref
, summary_width
);
640 if (current_branch
&&
641 !strcmp(ref
->name
, current_branch
->name
) &&
642 !(update_head_ok
|| is_bare_repository()) &&
643 !is_null_oid(&ref
->old_oid
)) {
645 * If this is the head, and it's not okay to update
646 * the head, and the old value of the head isn't empty...
648 format_display(display
, '!', _("[rejected]"),
649 _("can't fetch in current branch"),
650 remote
, pretty_ref
, summary_width
);
654 if (!is_null_oid(&ref
->old_oid
) &&
655 starts_with(ref
->name
, "refs/tags/")) {
657 r
= s_update_ref("updating tag", ref
, 0);
658 format_display(display
, r
? '!' : 't', _("[tag update]"),
659 r
? _("unable to update local ref") : NULL
,
660 remote
, pretty_ref
, summary_width
);
664 current
= lookup_commit_reference_gently(&ref
->old_oid
, 1);
665 updated
= lookup_commit_reference_gently(&ref
->new_oid
, 1);
666 if (!current
|| !updated
) {
671 * Nicely describe the new ref we're fetching.
672 * Base this on the remote's ref name, as it's
673 * more likely to follow a standard layout.
675 const char *name
= remote_ref
? remote_ref
->name
: "";
676 if (starts_with(name
, "refs/tags/")) {
678 what
= _("[new tag]");
679 } else if (starts_with(name
, "refs/heads/")) {
680 msg
= "storing head";
681 what
= _("[new branch]");
684 what
= _("[new ref]");
687 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
688 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
689 check_for_new_submodule_commits(&ref
->new_oid
);
690 r
= s_update_ref(msg
, ref
, 0);
691 format_display(display
, r
? '!' : '*', what
,
692 r
? _("unable to update local ref") : NULL
,
693 remote
, pretty_ref
, summary_width
);
697 if (in_merge_bases(current
, updated
)) {
698 struct strbuf quickref
= STRBUF_INIT
;
700 strbuf_add_unique_abbrev(&quickref
, current
->object
.oid
.hash
, DEFAULT_ABBREV
);
701 strbuf_addstr(&quickref
, "..");
702 strbuf_add_unique_abbrev(&quickref
, ref
->new_oid
.hash
, DEFAULT_ABBREV
);
703 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
704 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
705 check_for_new_submodule_commits(&ref
->new_oid
);
706 r
= s_update_ref("fast-forward", ref
, 1);
707 format_display(display
, r
? '!' : ' ', quickref
.buf
,
708 r
? _("unable to update local ref") : NULL
,
709 remote
, pretty_ref
, summary_width
);
710 strbuf_release(&quickref
);
712 } else if (force
|| ref
->force
) {
713 struct strbuf quickref
= STRBUF_INIT
;
715 strbuf_add_unique_abbrev(&quickref
, current
->object
.oid
.hash
, DEFAULT_ABBREV
);
716 strbuf_addstr(&quickref
, "...");
717 strbuf_add_unique_abbrev(&quickref
, ref
->new_oid
.hash
, DEFAULT_ABBREV
);
718 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
719 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
720 check_for_new_submodule_commits(&ref
->new_oid
);
721 r
= s_update_ref("forced-update", ref
, 1);
722 format_display(display
, r
? '!' : '+', quickref
.buf
,
723 r
? _("unable to update local ref") : _("forced update"),
724 remote
, pretty_ref
, summary_width
);
725 strbuf_release(&quickref
);
728 format_display(display
, '!', _("[rejected]"), _("non-fast-forward"),
729 remote
, pretty_ref
, summary_width
);
734 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
736 struct ref
**rm
= cb_data
;
737 struct ref
*ref
= *rm
;
739 while (ref
&& ref
->status
== REF_STATUS_REJECT_SHALLOW
)
742 return -1; /* end of the list */
744 oidcpy(oid
, &ref
->old_oid
);
748 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
752 struct commit
*commit
;
753 int url_len
, i
, rc
= 0;
754 struct strbuf note
= STRBUF_INIT
;
755 const char *what
, *kind
;
758 const char *filename
= dry_run
? "/dev/null" : git_path_fetch_head();
760 int summary_width
= transport_summary_width(ref_map
);
762 fp
= fopen(filename
, "a");
764 return error_errno(_("cannot open %s"), filename
);
767 url
= transport_anonymize_url(raw_url
);
769 url
= xstrdup("foreign");
772 if (check_connected(iterate_ref_map
, &rm
, NULL
)) {
773 rc
= error(_("%s did not send all necessary objects\n"), url
);
777 prepare_format_display(ref_map
);
780 * We do a pass for each fetch_head_status type in their enum order, so
781 * merged entries are written before not-for-merge. That lets readers
782 * use FETCH_HEAD as a refname to refer to the ref to be merged.
784 for (want_status
= FETCH_HEAD_MERGE
;
785 want_status
<= FETCH_HEAD_IGNORE
;
787 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
788 struct ref
*ref
= NULL
;
789 const char *merge_status_marker
= "";
791 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
) {
792 if (want_status
== FETCH_HEAD_MERGE
)
793 warning(_("reject %s because shallow roots are not allowed to be updated"),
794 rm
->peer_ref
? rm
->peer_ref
->name
: rm
->name
);
798 commit
= lookup_commit_reference_gently(&rm
->old_oid
,
801 rm
->fetch_head_status
= FETCH_HEAD_NOT_FOR_MERGE
;
803 if (rm
->fetch_head_status
!= want_status
)
807 ref
= alloc_ref(rm
->peer_ref
->name
);
808 oidcpy(&ref
->old_oid
, &rm
->peer_ref
->old_oid
);
809 oidcpy(&ref
->new_oid
, &rm
->old_oid
);
810 ref
->force
= rm
->peer_ref
->force
;
814 if (!strcmp(rm
->name
, "HEAD")) {
818 else if (starts_with(rm
->name
, "refs/heads/")) {
820 what
= rm
->name
+ 11;
822 else if (starts_with(rm
->name
, "refs/tags/")) {
824 what
= rm
->name
+ 10;
826 else if (starts_with(rm
->name
, "refs/remotes/")) {
827 kind
= "remote-tracking branch";
828 what
= rm
->name
+ 13;
835 url_len
= strlen(url
);
836 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
839 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
845 strbuf_addf(¬e
, "%s ", kind
);
846 strbuf_addf(¬e
, "'%s' of ", what
);
848 switch (rm
->fetch_head_status
) {
849 case FETCH_HEAD_NOT_FOR_MERGE
:
850 merge_status_marker
= "not-for-merge";
852 case FETCH_HEAD_MERGE
:
853 fprintf(fp
, "%s\t%s\t%s",
854 oid_to_hex(&rm
->old_oid
),
857 for (i
= 0; i
< url_len
; ++i
)
865 /* do not write anything to FETCH_HEAD */
871 rc
|= update_local_ref(ref
, what
, rm
, ¬e
,
875 format_display(¬e
, '*',
876 *kind
? kind
: "branch", NULL
,
877 *what
? what
: "HEAD",
878 "FETCH_HEAD", summary_width
);
880 if (verbosity
>= 0 && !shown_url
) {
881 fprintf(stderr
, _("From %.*s\n"),
886 fprintf(stderr
, " %s\n", note
.buf
);
891 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
892 error(_("some local refs could not be updated; try running\n"
893 " 'git remote prune %s' to remove any old, conflicting "
894 "branches"), remote_name
);
897 strbuf_release(¬e
);
904 * We would want to bypass the object transfer altogether if
905 * everything we are going to fetch already exists and is connected
908 static int quickfetch(struct ref
*ref_map
)
910 struct ref
*rm
= ref_map
;
911 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
914 * If we are deepening a shallow clone we already have these
915 * objects reachable. Running rev-list here will return with
916 * a good (0) exit status and we'll bypass the fetch that we
917 * really need to perform. Claiming failure now will ensure
918 * we perform the network exchange to deepen our history.
923 return check_connected(iterate_ref_map
, &rm
, &opt
);
926 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
928 int ret
= quickfetch(ref_map
);
930 ret
= transport_fetch_refs(transport
, ref_map
);
932 ret
|= store_updated_refs(transport
->url
,
933 transport
->remote
->name
,
935 transport_unlock_pack(transport
);
939 static int prune_refs(struct refspec
*refs
, int ref_count
, struct ref
*ref_map
,
942 int url_len
, i
, result
= 0;
943 struct ref
*ref
, *stale_refs
= get_stale_heads(refs
, ref_count
, ref_map
);
945 int summary_width
= transport_summary_width(stale_refs
);
946 const char *dangling_msg
= dry_run
947 ? _(" (%s will become dangling)")
948 : _(" (%s has become dangling)");
951 url
= transport_anonymize_url(raw_url
);
953 url
= xstrdup("foreign");
955 url_len
= strlen(url
);
956 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
960 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
964 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
966 for (ref
= stale_refs
; ref
; ref
= ref
->next
)
967 string_list_append(&refnames
, ref
->name
);
969 result
= delete_refs("fetch: prune", &refnames
, 0);
970 string_list_clear(&refnames
, 0);
973 if (verbosity
>= 0) {
974 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
975 struct strbuf sb
= STRBUF_INIT
;
977 fprintf(stderr
, _("From %.*s\n"), url_len
, url
);
980 format_display(&sb
, '-', _("[deleted]"), NULL
,
981 _("(none)"), prettify_refname(ref
->name
),
983 fprintf(stderr
, " %s\n",sb
.buf
);
985 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
990 free_refs(stale_refs
);
994 static void check_not_current_branch(struct ref
*ref_map
)
996 struct branch
*current_branch
= branch_get(NULL
);
998 if (is_bare_repository() || !current_branch
)
1001 for (; ref_map
; ref_map
= ref_map
->next
)
1002 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
1003 ref_map
->peer_ref
->name
))
1004 die(_("Refusing to fetch into current branch %s "
1005 "of non-bare repository"), current_branch
->refname
);
1008 static int truncate_fetch_head(void)
1010 const char *filename
= git_path_fetch_head();
1011 FILE *fp
= fopen_for_writing(filename
);
1014 return error_errno(_("cannot open %s"), filename
);
1019 static void set_option(struct transport
*transport
, const char *name
, const char *value
)
1021 int r
= transport_set_option(transport
, name
, value
);
1023 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1024 name
, value
, transport
->url
);
1026 warning(_("Option \"%s\" is ignored for %s\n"),
1027 name
, transport
->url
);
1030 static struct transport
*prepare_transport(struct remote
*remote
, int deepen
)
1032 struct transport
*transport
;
1033 transport
= transport_get(remote
, NULL
);
1034 transport_set_verbosity(transport
, verbosity
, progress
);
1035 transport
->family
= family
;
1037 set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
1039 set_option(transport
, TRANS_OPT_KEEP
, "yes");
1041 set_option(transport
, TRANS_OPT_DEPTH
, depth
);
1042 if (deepen
&& deepen_since
)
1043 set_option(transport
, TRANS_OPT_DEEPEN_SINCE
, deepen_since
);
1044 if (deepen
&& deepen_not
.nr
)
1045 set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1046 (const char *)&deepen_not
);
1047 if (deepen_relative
)
1048 set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, "yes");
1050 set_option(transport
, TRANS_OPT_UPDATE_SHALLOW
, "yes");
1051 if (filter_options
.choice
) {
1052 set_option(transport
, TRANS_OPT_LIST_OBJECTS_FILTER
,
1053 filter_options
.filter_spec
);
1054 set_option(transport
, TRANS_OPT_FROM_PROMISOR
, "1");
1059 static void backfill_tags(struct transport
*transport
, struct ref
*ref_map
)
1064 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1065 * when remote helper is used (setting it to an empty string
1066 * is not unsetting). We could extend the remote helper
1067 * protocol for that, but for now, just force a new connection
1068 * without deepen-since. Similar story for deepen-not.
1070 cannot_reuse
= transport
->cannot_reuse
||
1071 deepen_since
|| deepen_not
.nr
;
1073 gsecondary
= prepare_transport(transport
->remote
, 0);
1074 transport
= gsecondary
;
1077 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
1078 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
1079 transport_set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, NULL
);
1080 fetch_refs(transport
, ref_map
);
1083 transport_disconnect(gsecondary
);
1088 static int do_fetch(struct transport
*transport
,
1089 struct refspec
*refs
, int ref_count
)
1091 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
1092 struct ref
*ref_map
;
1094 int autotags
= (transport
->remote
->fetch_tags
== 1);
1097 for_each_ref(add_existing
, &existing_refs
);
1099 if (tags
== TAGS_DEFAULT
) {
1100 if (transport
->remote
->fetch_tags
== 2)
1102 if (transport
->remote
->fetch_tags
== -1)
1106 /* if not appending, truncate FETCH_HEAD */
1107 if (!append
&& !dry_run
) {
1108 retcode
= truncate_fetch_head();
1113 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
1114 if (!update_head_ok
)
1115 check_not_current_branch(ref_map
);
1117 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
1119 struct string_list_item
*peer_item
=
1120 string_list_lookup(&existing_refs
,
1121 rm
->peer_ref
->name
);
1123 struct object_id
*old_oid
= peer_item
->util
;
1124 oidcpy(&rm
->peer_ref
->old_oid
, old_oid
);
1129 if (tags
== TAGS_DEFAULT
&& autotags
)
1130 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1133 * We only prune based on refspecs specified
1134 * explicitly (via command line or configuration); we
1135 * don't care whether --tags was specified.
1138 prune_refs(refs
, ref_count
, ref_map
, transport
->url
);
1140 prune_refs(transport
->remote
->fetch
,
1141 transport
->remote
->fetch_refspec_nr
,
1146 if (fetch_refs(transport
, ref_map
)) {
1153 /* if neither --no-tags nor --tags was specified, do automated tag
1155 if (tags
== TAGS_DEFAULT
&& autotags
) {
1156 struct ref
**tail
= &ref_map
;
1158 find_non_local_tags(transport
, &ref_map
, &tail
);
1160 backfill_tags(transport
, ref_map
);
1165 string_list_clear(&existing_refs
, 1);
1169 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
1171 struct string_list
*list
= priv
;
1172 if (!remote
->skip_default_update
)
1173 string_list_append(list
, remote
->name
);
1177 struct remote_group_data
{
1179 struct string_list
*list
;
1182 static int get_remote_group(const char *key
, const char *value
, void *priv
)
1184 struct remote_group_data
*g
= priv
;
1186 if (skip_prefix(key
, "remotes.", &key
) && !strcmp(key
, g
->name
)) {
1187 /* split list by white space */
1189 size_t wordlen
= strcspn(value
, " \t\n");
1192 string_list_append_nodup(g
->list
,
1193 xstrndup(value
, wordlen
));
1194 value
+= wordlen
+ (value
[wordlen
] != '\0');
1201 static int add_remote_or_group(const char *name
, struct string_list
*list
)
1203 int prev_nr
= list
->nr
;
1204 struct remote_group_data g
;
1205 g
.name
= name
; g
.list
= list
;
1207 git_config(get_remote_group
, &g
);
1208 if (list
->nr
== prev_nr
) {
1209 struct remote
*remote
= remote_get(name
);
1210 if (!remote_is_configured(remote
, 0))
1212 string_list_append(list
, remote
->name
);
1217 static void add_options_to_argv(struct argv_array
*argv
)
1220 argv_array_push(argv
, "--dry-run");
1222 argv_array_push(argv
, prune
? "--prune" : "--no-prune");
1224 argv_array_push(argv
, "--update-head-ok");
1226 argv_array_push(argv
, "--force");
1228 argv_array_push(argv
, "--keep");
1229 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
1230 argv_array_push(argv
, "--recurse-submodules");
1231 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
1232 argv_array_push(argv
, "--recurse-submodules=on-demand");
1233 if (tags
== TAGS_SET
)
1234 argv_array_push(argv
, "--tags");
1235 else if (tags
== TAGS_UNSET
)
1236 argv_array_push(argv
, "--no-tags");
1238 argv_array_push(argv
, "-v");
1240 argv_array_push(argv
, "-v");
1241 else if (verbosity
< 0)
1242 argv_array_push(argv
, "-q");
1246 static int fetch_multiple(struct string_list
*list
)
1249 struct argv_array argv
= ARGV_ARRAY_INIT
;
1251 if (!append
&& !dry_run
) {
1252 int errcode
= truncate_fetch_head();
1257 argv_array_pushl(&argv
, "fetch", "--append", NULL
);
1258 add_options_to_argv(&argv
);
1260 for (i
= 0; i
< list
->nr
; i
++) {
1261 const char *name
= list
->items
[i
].string
;
1262 argv_array_push(&argv
, name
);
1264 printf(_("Fetching %s\n"), name
);
1265 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
1266 error(_("Could not fetch %s"), name
);
1269 argv_array_pop(&argv
);
1272 argv_array_clear(&argv
);
1277 * Fetching from the promisor remote should use the given filter-spec
1278 * or inherit the default filter-spec from the config.
1280 static inline void fetch_one_setup_partial(struct remote
*remote
)
1283 * Explicit --no-filter argument overrides everything, regardless
1284 * of any prior partial clones and fetches.
1286 if (filter_options
.no_filter
)
1290 * If no prior partial clone/fetch and the current fetch DID NOT
1291 * request a partial-fetch, do a normal fetch.
1293 if (!repository_format_partial_clone
&& !filter_options
.choice
)
1297 * If this is the FIRST partial-fetch request, we enable partial
1298 * on this repo and remember the given filter-spec as the default
1299 * for subsequent fetches to this remote.
1301 if (!repository_format_partial_clone
&& filter_options
.choice
) {
1302 partial_clone_register(remote
->name
, &filter_options
);
1307 * We are currently limited to only ONE promisor remote and only
1308 * allow partial-fetches from the promisor remote.
1310 if (strcmp(remote
->name
, repository_format_partial_clone
)) {
1311 if (filter_options
.choice
)
1312 die(_("--filter can only be used with the remote configured in core.partialClone"));
1317 * Do a partial-fetch from the promisor remote using either the
1318 * explicitly given filter-spec or inherit the filter-spec from
1321 if (!filter_options
.choice
)
1322 partial_clone_get_default_filter_spec(&filter_options
);
1326 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
1328 static const char **refs
= NULL
;
1329 struct refspec
*refspec
;
1334 die(_("No remote repository specified. Please, specify either a URL or a\n"
1335 "remote name from which new revisions should be fetched."));
1337 gtransport
= prepare_transport(remote
, 1);
1340 /* no command line request */
1341 if (0 <= gtransport
->remote
->prune
)
1342 prune
= gtransport
->remote
->prune
;
1343 else if (0 <= fetch_prune_config
)
1344 prune
= fetch_prune_config
;
1346 prune
= PRUNE_BY_DEFAULT
;
1352 refs
= xcalloc(st_add(argc
, 1), sizeof(const char *));
1353 for (i
= 0; i
< argc
; i
++) {
1354 if (!strcmp(argv
[i
], "tag")) {
1357 die(_("You need to specify a tag name."));
1358 refs
[j
++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1361 refs
[j
++] = argv
[i
];
1367 sigchain_push_common(unlock_pack_on_signal
);
1368 atexit(unlock_pack
);
1369 refspec
= parse_fetch_refspec(ref_nr
, refs
);
1370 exit_code
= do_fetch(gtransport
, refspec
, ref_nr
);
1371 free_refspec(ref_nr
, refspec
);
1372 transport_disconnect(gtransport
);
1377 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
1380 struct string_list list
= STRING_LIST_INIT_DUP
;
1381 struct remote
*remote
= NULL
;
1383 struct argv_array argv_gc_auto
= ARGV_ARRAY_INIT
;
1385 packet_trace_identity("fetch");
1387 fetch_if_missing
= 0;
1389 /* Record the command line for the reflog */
1390 strbuf_addstr(&default_rla
, "fetch");
1391 for (i
= 1; i
< argc
; i
++)
1392 strbuf_addf(&default_rla
, " %s", argv
[i
]);
1394 config_from_gitmodules(gitmodules_fetch_config
, NULL
);
1395 git_config(git_fetch_config
, NULL
);
1397 argc
= parse_options(argc
, argv
, prefix
,
1398 builtin_fetch_options
, builtin_fetch_usage
, 0);
1400 if (deepen_relative
) {
1401 if (deepen_relative
< 0)
1402 die(_("Negative depth in --deepen is not supported"));
1404 die(_("--deepen and --depth are mutually exclusive"));
1405 depth
= xstrfmt("%d", deepen_relative
);
1409 die(_("--depth and --unshallow cannot be used together"));
1410 else if (!is_repository_shallow())
1411 die(_("--unshallow on a complete repository does not make sense"));
1413 depth
= xstrfmt("%d", INFINITE_DEPTH
);
1416 /* no need to be strict, transport_set_option() will validate it again */
1417 if (depth
&& atoi(depth
) < 1)
1418 die(_("depth %s is not a positive number"), depth
);
1419 if (depth
|| deepen_since
|| deepen_not
.nr
)
1422 if (filter_options
.choice
&& !repository_format_partial_clone
)
1423 die("--filter can only be used when extensions.partialClone is set");
1427 die(_("fetch --all does not take a repository argument"));
1429 die(_("fetch --all does not make sense with refspecs"));
1430 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1431 } else if (argc
== 0) {
1432 /* No arguments -- use default remote */
1433 remote
= remote_get(NULL
);
1434 } else if (multiple
) {
1435 /* All arguments are assumed to be remotes or groups */
1436 for (i
= 0; i
< argc
; i
++)
1437 if (!add_remote_or_group(argv
[i
], &list
))
1438 die(_("No such remote or remote group: %s"), argv
[i
]);
1440 /* Single remote or group */
1441 (void) add_remote_or_group(argv
[0], &list
);
1443 /* More than one remote */
1445 die(_("Fetching a group and specifying refspecs does not make sense"));
1447 /* Zero or one remotes */
1448 remote
= remote_get(argv
[0]);
1455 if (filter_options
.choice
|| repository_format_partial_clone
)
1456 fetch_one_setup_partial(remote
);
1457 result
= fetch_one(remote
, argc
, argv
);
1459 if (filter_options
.choice
)
1460 die(_("--filter can only be used with the remote configured in core.partialClone"));
1461 /* TODO should this also die if we have a previous partial-clone? */
1462 result
= fetch_multiple(&list
);
1465 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1466 struct argv_array options
= ARGV_ARRAY_INIT
;
1468 add_options_to_argv(&options
);
1469 result
= fetch_populated_submodules(the_repository
,
1473 recurse_submodules_default
,
1476 argv_array_clear(&options
);
1479 string_list_clear(&list
, 0);
1483 argv_array_pushl(&argv_gc_auto
, "gc", "--auto", NULL
);
1485 argv_array_push(&argv_gc_auto
, "--quiet");
1486 run_command_v_opt(argv_gc_auto
.argv
, RUN_GIT_CMD
);
1487 argv_array_clear(&argv_gc_auto
);