6 #include "repository.h"
11 #include "string-list.h"
13 #include "transport.h"
14 #include "run-command.h"
15 #include "parse-options.h"
17 #include "submodule-config.h"
18 #include "submodule.h"
19 #include "connected.h"
20 #include "argv-array.h"
23 #include "list-objects-filter-options.h"
25 static const char * const builtin_fetch_usage
[] = {
26 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
27 N_("git fetch [<options>] <group>"),
28 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
29 N_("git fetch --all [<options>]"),
39 static int fetch_prune_config
= -1; /* unspecified */
40 static int prune
= -1; /* unspecified */
41 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
43 static int fetch_prune_tags_config
= -1; /* unspecified */
44 static int prune_tags
= -1; /* unspecified */
45 #define PRUNE_TAGS_BY_DEFAULT 0 /* do we prune tags by default? */
47 static int all
, append
, dry_run
, force
, keep
, multiple
, update_head_ok
, verbosity
, deepen_relative
;
48 static int progress
= -1;
49 static int tags
= TAGS_DEFAULT
, unshallow
, update_shallow
, deepen
;
50 static int max_children
= 1;
51 static enum transport_family family
;
52 static const char *depth
;
53 static const char *deepen_since
;
54 static const char *upload_pack
;
55 static struct string_list deepen_not
= STRING_LIST_INIT_NODUP
;
56 static struct strbuf default_rla
= STRBUF_INIT
;
57 static struct transport
*gtransport
;
58 static struct transport
*gsecondary
;
59 static const char *submodule_prefix
= "";
60 static int recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
61 static int recurse_submodules_default
= RECURSE_SUBMODULES_ON_DEMAND
;
62 static int shown_url
= 0;
63 static int refmap_alloc
, refmap_nr
;
64 static const char **refmap_array
;
65 static struct list_objects_filter_options filter_options
;
67 static int git_fetch_config(const char *k
, const char *v
, void *cb
)
69 if (!strcmp(k
, "fetch.prune")) {
70 fetch_prune_config
= git_config_bool(k
, v
);
74 if (!strcmp(k
, "fetch.prunetags")) {
75 fetch_prune_tags_config
= git_config_bool(k
, v
);
79 if (!strcmp(k
, "submodule.recurse")) {
80 int r
= git_config_bool(k
, v
) ?
81 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
82 recurse_submodules
= r
;
85 if (!strcmp(k
, "submodule.fetchjobs")) {
86 max_children
= parse_submodule_fetchjobs(k
, v
);
88 } else if (!strcmp(k
, "fetch.recursesubmodules")) {
89 recurse_submodules
= parse_fetch_recurse_submodules_arg(k
, v
);
93 return git_default_config(k
, v
, cb
);
96 static int gitmodules_fetch_config(const char *var
, const char *value
, void *cb
)
98 if (!strcmp(var
, "submodule.fetchjobs")) {
99 max_children
= parse_submodule_fetchjobs(var
, value
);
101 } else if (!strcmp(var
, "fetch.recursesubmodules")) {
102 recurse_submodules
= parse_fetch_recurse_submodules_arg(var
, value
);
109 static int parse_refmap_arg(const struct option
*opt
, const char *arg
, int unset
)
111 ALLOC_GROW(refmap_array
, refmap_nr
+ 1, refmap_alloc
);
114 * "git fetch --refmap='' origin foo"
115 * can be used to tell the command not to store anywhere
118 refmap_array
[refmap_nr
++] = arg
;
122 static struct option builtin_fetch_options
[] = {
123 OPT__VERBOSITY(&verbosity
),
124 OPT_BOOL(0, "all", &all
,
125 N_("fetch from all remotes")),
126 OPT_BOOL('a', "append", &append
,
127 N_("append to .git/FETCH_HEAD instead of overwriting")),
128 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
129 N_("path to upload pack on remote end")),
130 OPT__FORCE(&force
, N_("force overwrite of local branch"), 0),
131 OPT_BOOL('m', "multiple", &multiple
,
132 N_("fetch from multiple remotes")),
133 OPT_SET_INT('t', "tags", &tags
,
134 N_("fetch all tags and associated objects"), TAGS_SET
),
135 OPT_SET_INT('n', NULL
, &tags
,
136 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
137 OPT_INTEGER('j', "jobs", &max_children
,
138 N_("number of submodules fetched in parallel")),
139 OPT_BOOL('p', "prune", &prune
,
140 N_("prune remote-tracking branches no longer on remote")),
141 OPT_BOOL('P', "prune-tags", &prune_tags
,
142 N_("prune local tags no longer on remote and clobber changed tags")),
143 { OPTION_CALLBACK
, 0, "recurse-submodules", &recurse_submodules
, N_("on-demand"),
144 N_("control recursive fetching of submodules"),
145 PARSE_OPT_OPTARG
, option_fetch_parse_recurse_submodules
},
146 OPT_BOOL(0, "dry-run", &dry_run
,
148 OPT_BOOL('k', "keep", &keep
, N_("keep downloaded pack")),
149 OPT_BOOL('u', "update-head-ok", &update_head_ok
,
150 N_("allow updating of HEAD ref")),
151 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
152 OPT_STRING(0, "depth", &depth
, N_("depth"),
153 N_("deepen history of shallow clone")),
154 OPT_STRING(0, "shallow-since", &deepen_since
, N_("time"),
155 N_("deepen history of shallow repository based on time")),
156 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not
, N_("revision"),
157 N_("deepen history of shallow clone, excluding rev")),
158 OPT_INTEGER(0, "deepen", &deepen_relative
,
159 N_("deepen history of shallow clone")),
160 { OPTION_SET_INT
, 0, "unshallow", &unshallow
, NULL
,
161 N_("convert to a complete repository"),
162 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, NULL
, 1 },
163 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
164 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
165 { OPTION_CALLBACK
, 0, "recurse-submodules-default",
166 &recurse_submodules_default
, N_("on-demand"),
167 N_("default for recursive fetching of submodules "
168 "(lower priority than config files)"),
169 PARSE_OPT_HIDDEN
, option_fetch_parse_recurse_submodules
},
170 OPT_BOOL(0, "update-shallow", &update_shallow
,
171 N_("accept refs that update .git/shallow")),
172 { OPTION_CALLBACK
, 0, "refmap", NULL
, N_("refmap"),
173 N_("specify fetch refmap"), PARSE_OPT_NONEG
, parse_refmap_arg
},
174 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
175 TRANSPORT_FAMILY_IPV4
),
176 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
177 TRANSPORT_FAMILY_IPV6
),
178 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options
),
182 static void unlock_pack(void)
185 transport_unlock_pack(gtransport
);
187 transport_unlock_pack(gsecondary
);
190 static void unlock_pack_on_signal(int signo
)
197 static void add_merge_config(struct ref
**head
,
198 const struct ref
*remote_refs
,
199 struct branch
*branch
,
204 for (i
= 0; i
< branch
->merge_nr
; i
++) {
205 struct ref
*rm
, **old_tail
= *tail
;
206 struct refspec_item refspec
;
208 for (rm
= *head
; rm
; rm
= rm
->next
) {
209 if (branch_merge_matches(branch
, i
, rm
->name
)) {
210 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
218 * Not fetched to a remote-tracking branch? We need to fetch
219 * it anyway to allow this branch's "branch.$name.merge"
220 * to be honored by 'git pull', but we do not have to
221 * fail if branch.$name.merge is misconfigured to point
222 * at a nonexisting branch. If we were indeed called by
223 * 'git pull', it will notice the misconfiguration because
224 * there is no entry in the resulting FETCH_HEAD marked
227 memset(&refspec
, 0, sizeof(refspec
));
228 refspec
.src
= branch
->merge
[i
]->src
;
229 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
230 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
231 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
235 static int add_existing(const char *refname
, const struct object_id
*oid
,
236 int flag
, void *cbdata
)
238 struct string_list
*list
= (struct string_list
*)cbdata
;
239 struct string_list_item
*item
= string_list_insert(list
, refname
);
240 struct object_id
*old_oid
= xmalloc(sizeof(*old_oid
));
242 oidcpy(old_oid
, oid
);
243 item
->util
= old_oid
;
247 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
249 struct ref
*rm
= *head
;
251 if (!hashcmp(rm
->old_oid
.hash
, sha1
))
258 static void find_non_local_tags(struct transport
*transport
,
262 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
263 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
264 const struct ref
*ref
;
265 struct string_list_item
*item
= NULL
;
267 for_each_ref(add_existing
, &existing_refs
);
268 for (ref
= transport_get_remote_refs(transport
, NULL
); ref
; ref
= ref
->next
) {
269 if (!starts_with(ref
->name
, "refs/tags/"))
273 * The peeled ref always follows the matching base
274 * ref, so if we see a peeled ref that we don't want
275 * to fetch then we can mark the ref entry in the list
276 * as one to ignore by setting util to NULL.
278 if (ends_with(ref
->name
, "^{}")) {
280 !has_object_file_with_flags(&ref
->old_oid
,
281 OBJECT_INFO_QUICK
) &&
282 !will_fetch(head
, ref
->old_oid
.hash
) &&
283 !has_sha1_file_with_flags(item
->util
,
284 OBJECT_INFO_QUICK
) &&
285 !will_fetch(head
, item
->util
))
292 * If item is non-NULL here, then we previously saw a
293 * ref not followed by a peeled reference, so we need
294 * to check if it is a lightweight tag that we want to
298 !has_sha1_file_with_flags(item
->util
, OBJECT_INFO_QUICK
) &&
299 !will_fetch(head
, item
->util
))
304 /* skip duplicates and refs that we already have */
305 if (string_list_has_string(&remote_refs
, ref
->name
) ||
306 string_list_has_string(&existing_refs
, ref
->name
))
309 item
= string_list_insert(&remote_refs
, ref
->name
);
310 item
->util
= (void *)&ref
->old_oid
;
312 string_list_clear(&existing_refs
, 1);
315 * We may have a final lightweight tag that needs to be
316 * checked to see if it needs fetching.
319 !has_sha1_file_with_flags(item
->util
, OBJECT_INFO_QUICK
) &&
320 !will_fetch(head
, item
->util
))
324 * For all the tags in the remote_refs string list,
325 * add them to the list of refs to be fetched
327 for_each_string_list_item(item
, &remote_refs
) {
328 /* Unless we have already decided to ignore this item... */
331 struct ref
*rm
= alloc_ref(item
->string
);
332 rm
->peer_ref
= alloc_ref(item
->string
);
333 oidcpy(&rm
->old_oid
, item
->util
);
339 string_list_clear(&remote_refs
, 0);
342 static struct ref
*get_ref_map(struct transport
*transport
,
343 struct refspec_item
*refspecs
, int refspec_count
,
344 int tags
, int *autotags
)
348 struct ref
*ref_map
= NULL
;
349 struct ref
**tail
= &ref_map
;
350 struct argv_array ref_prefixes
= ARGV_ARRAY_INIT
;
352 /* opportunistically-updated references: */
353 struct ref
*orefs
= NULL
, **oref_tail
= &orefs
;
355 const struct ref
*remote_refs
;
357 for (i
= 0; i
< refspec_count
; i
++) {
358 if (!refspecs
[i
].exact_sha1
) {
359 const char *glob
= strchr(refspecs
[i
].src
, '*');
361 argv_array_pushf(&ref_prefixes
, "%.*s",
362 (int)(glob
- refspecs
[i
].src
),
365 expand_ref_prefix(&ref_prefixes
, refspecs
[i
].src
);
369 remote_refs
= transport_get_remote_refs(transport
, &ref_prefixes
);
371 argv_array_clear(&ref_prefixes
);
374 struct refspec_item
*fetch_refspec
;
375 int fetch_refspec_nr
;
377 for (i
= 0; i
< refspec_count
; i
++) {
378 get_fetch_map(remote_refs
, &refspecs
[i
], &tail
, 0);
379 if (refspecs
[i
].dst
&& refspecs
[i
].dst
[0])
382 /* Merge everything on the command line (but not --tags) */
383 for (rm
= ref_map
; rm
; rm
= rm
->next
)
384 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
387 * For any refs that we happen to be fetching via
388 * command-line arguments, the destination ref might
389 * have been missing or have been different than the
390 * remote-tracking ref that would be derived from the
391 * configured refspec. In these cases, we want to
392 * take the opportunity to update their configured
393 * remote-tracking reference. However, we do not want
394 * to mention these entries in FETCH_HEAD at all, as
395 * they would simply be duplicates of existing
396 * entries, so we set them FETCH_HEAD_IGNORE below.
398 * We compute these entries now, based only on the
399 * refspecs specified on the command line. But we add
400 * them to the list following the refspecs resulting
401 * from the tags option so that one of the latter,
402 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
403 * by ref_remove_duplicates() in favor of one of these
404 * opportunistic entries with FETCH_HEAD_IGNORE.
407 fetch_refspec
= parse_fetch_refspec(refmap_nr
, refmap_array
);
408 fetch_refspec_nr
= refmap_nr
;
410 fetch_refspec
= transport
->remote
->fetch
.items
;
411 fetch_refspec_nr
= transport
->remote
->fetch
.nr
;
414 for (i
= 0; i
< fetch_refspec_nr
; i
++)
415 get_fetch_map(ref_map
, &fetch_refspec
[i
], &oref_tail
, 1);
416 } else if (refmap_array
) {
417 die("--refmap option is only meaningful with command-line refspec(s).");
419 /* Use the defaults */
420 struct remote
*remote
= transport
->remote
;
421 struct branch
*branch
= branch_get(NULL
);
422 int has_merge
= branch_has_merge_config(branch
);
425 /* Note: has_merge implies non-NULL branch->remote_name */
426 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
427 for (i
= 0; i
< remote
->fetch
.nr
; i
++) {
428 get_fetch_map(remote_refs
, &remote
->fetch
.items
[i
], &tail
, 0);
429 if (remote
->fetch
.items
[i
].dst
&&
430 remote
->fetch
.items
[i
].dst
[0])
432 if (!i
&& !has_merge
&& ref_map
&&
433 !remote
->fetch
.items
[0].pattern
)
434 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
437 * if the remote we're fetching from is the same
438 * as given in branch.<name>.remote, we add the
439 * ref given in branch.<name>.merge, too.
441 * Note: has_merge implies non-NULL branch->remote_name
444 !strcmp(branch
->remote_name
, remote
->name
))
445 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
447 ref_map
= get_remote_ref(remote_refs
, "HEAD");
449 die(_("Couldn't find remote ref HEAD"));
450 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
451 tail
= &ref_map
->next
;
455 if (tags
== TAGS_SET
)
456 /* also fetch all tags */
457 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
458 else if (tags
== TAGS_DEFAULT
&& *autotags
)
459 find_non_local_tags(transport
, &ref_map
, &tail
);
461 /* Now append any refs to be updated opportunistically: */
463 for (rm
= orefs
; rm
; rm
= rm
->next
) {
464 rm
->fetch_head_status
= FETCH_HEAD_IGNORE
;
468 return ref_remove_duplicates(ref_map
);
471 #define STORE_REF_ERROR_OTHER 1
472 #define STORE_REF_ERROR_DF_CONFLICT 2
474 static int s_update_ref(const char *action
,
479 char *rla
= getenv("GIT_REFLOG_ACTION");
480 struct ref_transaction
*transaction
;
481 struct strbuf err
= STRBUF_INIT
;
482 int ret
, df_conflict
= 0;
487 rla
= default_rla
.buf
;
488 msg
= xstrfmt("%s: %s", rla
, action
);
490 transaction
= ref_transaction_begin(&err
);
492 ref_transaction_update(transaction
, ref
->name
,
494 check_old ?
&ref
->old_oid
: NULL
,
498 ret
= ref_transaction_commit(transaction
, &err
);
500 df_conflict
= (ret
== TRANSACTION_NAME_CONFLICT
);
504 ref_transaction_free(transaction
);
505 strbuf_release(&err
);
509 ref_transaction_free(transaction
);
510 error("%s", err
.buf
);
511 strbuf_release(&err
);
513 return df_conflict ? STORE_REF_ERROR_DF_CONFLICT
514 : STORE_REF_ERROR_OTHER
;
517 static int refcol_width
= 10;
518 static int compact_format
;
520 static void adjust_refcol_width(const struct ref
*ref
)
522 int max
, rlen
, llen
, len
;
524 /* uptodate lines are only shown on high verbosity level */
525 if (!verbosity
&& !oidcmp(&ref
->peer_ref
->old_oid
, &ref
->old_oid
))
528 max
= term_columns();
529 rlen
= utf8_strwidth(prettify_refname(ref
->name
));
531 llen
= utf8_strwidth(prettify_refname(ref
->peer_ref
->name
));
534 * rough estimation to see if the output line is too long and
535 * should not be counted (we can't do precise calculation
536 * anyway because we don't know if the error explanation part
537 * will be printed in update_local_ref)
539 if (compact_format
) {
543 len
= 21 /* flag and summary */ + rlen
+ 4 /* -> */ + llen
;
548 * Not precise calculation for compact mode because '*' can
549 * appear on the left hand side of '->' and shrink the column
552 if (refcol_width
< rlen
)
556 static void prepare_format_display(struct ref
*ref_map
)
559 const char *format
= "full";
561 git_config_get_string_const("fetch.output", &format
);
562 if (!strcasecmp(format
, "full"))
564 else if (!strcasecmp(format
, "compact"))
567 die(_("configuration fetch.output contains invalid value %s"),
570 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
571 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
||
573 !strcmp(rm
->name
, "HEAD"))
576 adjust_refcol_width(rm
);
580 static void print_remote_to_local(struct strbuf
*display
,
581 const char *remote
, const char *local
)
583 strbuf_addf(display
, "%-*s -> %s", refcol_width
, remote
, local
);
586 static int find_and_replace(struct strbuf
*haystack
,
588 const char *placeholder
)
590 const char *p
= strstr(haystack
->buf
, needle
);
596 if (p
> haystack
->buf
&& p
[-1] != '/')
600 nlen
= strlen(needle
);
601 if (plen
> nlen
&& p
[nlen
] != '/')
604 strbuf_splice(haystack
, p
- haystack
->buf
, nlen
,
605 placeholder
, strlen(placeholder
));
609 static void print_compact(struct strbuf
*display
,
610 const char *remote
, const char *local
)
612 struct strbuf r
= STRBUF_INIT
;
613 struct strbuf l
= STRBUF_INIT
;
615 if (!strcmp(remote
, local
)) {
616 strbuf_addf(display
, "%-*s -> *", refcol_width
, remote
);
620 strbuf_addstr(&r
, remote
);
621 strbuf_addstr(&l
, local
);
623 if (!find_and_replace(&r
, local
, "*"))
624 find_and_replace(&l
, remote
, "*");
625 print_remote_to_local(display
, r
.buf
, l
.buf
);
631 static void format_display(struct strbuf
*display
, char code
,
632 const char *summary
, const char *error
,
633 const char *remote
, const char *local
,
636 int width
= (summary_width
+ strlen(summary
) - gettext_width(summary
));
638 strbuf_addf(display
, "%c %-*s ", code
, width
, summary
);
640 print_remote_to_local(display
, remote
, local
);
642 print_compact(display
, remote
, local
);
644 strbuf_addf(display
, " (%s)", error
);
647 static int update_local_ref(struct ref
*ref
,
649 const struct ref
*remote_ref
,
650 struct strbuf
*display
,
653 struct commit
*current
= NULL
, *updated
;
654 enum object_type type
;
655 struct branch
*current_branch
= branch_get(NULL
);
656 const char *pretty_ref
= prettify_refname(ref
->name
);
658 type
= oid_object_info(&ref
->new_oid
, NULL
);
660 die(_("object %s not found"), oid_to_hex(&ref
->new_oid
));
662 if (!oidcmp(&ref
->old_oid
, &ref
->new_oid
)) {
664 format_display(display
, '=', _("[up to date]"), NULL
,
665 remote
, pretty_ref
, summary_width
);
669 if (current_branch
&&
670 !strcmp(ref
->name
, current_branch
->name
) &&
671 !(update_head_ok
|| is_bare_repository()) &&
672 !is_null_oid(&ref
->old_oid
)) {
674 * If this is the head, and it's not okay to update
675 * the head, and the old value of the head isn't empty...
677 format_display(display
, '!', _("[rejected]"),
678 _("can't fetch in current branch"),
679 remote
, pretty_ref
, summary_width
);
683 if (!is_null_oid(&ref
->old_oid
) &&
684 starts_with(ref
->name
, "refs/tags/")) {
686 r
= s_update_ref("updating tag", ref
, 0);
687 format_display(display
, r ?
'!' : 't', _("[tag update]"),
688 r ?
_("unable to update local ref") : NULL
,
689 remote
, pretty_ref
, summary_width
);
693 current
= lookup_commit_reference_gently(&ref
->old_oid
, 1);
694 updated
= lookup_commit_reference_gently(&ref
->new_oid
, 1);
695 if (!current
|| !updated
) {
700 * Nicely describe the new ref we're fetching.
701 * Base this on the remote's ref name, as it's
702 * more likely to follow a standard layout.
704 const char *name
= remote_ref ? remote_ref
->name
: "";
705 if (starts_with(name
, "refs/tags/")) {
707 what
= _("[new tag]");
708 } else if (starts_with(name
, "refs/heads/")) {
709 msg
= "storing head";
710 what
= _("[new branch]");
713 what
= _("[new ref]");
716 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
717 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
718 check_for_new_submodule_commits(&ref
->new_oid
);
719 r
= s_update_ref(msg
, ref
, 0);
720 format_display(display
, r ?
'!' : '*', what
,
721 r ?
_("unable to update local ref") : NULL
,
722 remote
, pretty_ref
, summary_width
);
726 if (in_merge_bases(current
, updated
)) {
727 struct strbuf quickref
= STRBUF_INIT
;
729 strbuf_add_unique_abbrev(&quickref
, ¤t
->object
.oid
, DEFAULT_ABBREV
);
730 strbuf_addstr(&quickref
, "..");
731 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
, DEFAULT_ABBREV
);
732 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
733 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
734 check_for_new_submodule_commits(&ref
->new_oid
);
735 r
= s_update_ref("fast-forward", ref
, 1);
736 format_display(display
, r ?
'!' : ' ', quickref
.buf
,
737 r ?
_("unable to update local ref") : NULL
,
738 remote
, pretty_ref
, summary_width
);
739 strbuf_release(&quickref
);
741 } else if (force
|| ref
->force
) {
742 struct strbuf quickref
= STRBUF_INIT
;
744 strbuf_add_unique_abbrev(&quickref
, ¤t
->object
.oid
, DEFAULT_ABBREV
);
745 strbuf_addstr(&quickref
, "...");
746 strbuf_add_unique_abbrev(&quickref
, &ref
->new_oid
, DEFAULT_ABBREV
);
747 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
748 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
749 check_for_new_submodule_commits(&ref
->new_oid
);
750 r
= s_update_ref("forced-update", ref
, 1);
751 format_display(display
, r ?
'!' : '+', quickref
.buf
,
752 r ?
_("unable to update local ref") : _("forced update"),
753 remote
, pretty_ref
, summary_width
);
754 strbuf_release(&quickref
);
757 format_display(display
, '!', _("[rejected]"), _("non-fast-forward"),
758 remote
, pretty_ref
, summary_width
);
763 static int iterate_ref_map(void *cb_data
, struct object_id
*oid
)
765 struct ref
**rm
= cb_data
;
766 struct ref
*ref
= *rm
;
768 while (ref
&& ref
->status
== REF_STATUS_REJECT_SHALLOW
)
771 return -1; /* end of the list */
773 oidcpy(oid
, &ref
->old_oid
);
777 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
781 struct commit
*commit
;
782 int url_len
, i
, rc
= 0;
783 struct strbuf note
= STRBUF_INIT
;
784 const char *what
, *kind
;
787 const char *filename
= dry_run ?
"/dev/null" : git_path_fetch_head();
789 int summary_width
= transport_summary_width(ref_map
);
791 fp
= fopen(filename
, "a");
793 return error_errno(_("cannot open %s"), filename
);
796 url
= transport_anonymize_url(raw_url
);
798 url
= xstrdup("foreign");
801 if (check_connected(iterate_ref_map
, &rm
, NULL
)) {
802 rc
= error(_("%s did not send all necessary objects\n"), url
);
806 prepare_format_display(ref_map
);
809 * We do a pass for each fetch_head_status type in their enum order, so
810 * merged entries are written before not-for-merge. That lets readers
811 * use FETCH_HEAD as a refname to refer to the ref to be merged.
813 for (want_status
= FETCH_HEAD_MERGE
;
814 want_status
<= FETCH_HEAD_IGNORE
;
816 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
817 struct ref
*ref
= NULL
;
818 const char *merge_status_marker
= "";
820 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
) {
821 if (want_status
== FETCH_HEAD_MERGE
)
822 warning(_("reject %s because shallow roots are not allowed to be updated"),
823 rm
->peer_ref ? rm
->peer_ref
->name
: rm
->name
);
827 commit
= lookup_commit_reference_gently(&rm
->old_oid
,
830 rm
->fetch_head_status
= FETCH_HEAD_NOT_FOR_MERGE
;
832 if (rm
->fetch_head_status
!= want_status
)
836 ref
= alloc_ref(rm
->peer_ref
->name
);
837 oidcpy(&ref
->old_oid
, &rm
->peer_ref
->old_oid
);
838 oidcpy(&ref
->new_oid
, &rm
->old_oid
);
839 ref
->force
= rm
->peer_ref
->force
;
843 if (!strcmp(rm
->name
, "HEAD")) {
847 else if (starts_with(rm
->name
, "refs/heads/")) {
849 what
= rm
->name
+ 11;
851 else if (starts_with(rm
->name
, "refs/tags/")) {
853 what
= rm
->name
+ 10;
855 else if (starts_with(rm
->name
, "refs/remotes/")) {
856 kind
= "remote-tracking branch";
857 what
= rm
->name
+ 13;
864 url_len
= strlen(url
);
865 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
868 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
874 strbuf_addf(¬e
, "%s ", kind
);
875 strbuf_addf(¬e
, "'%s' of ", what
);
877 switch (rm
->fetch_head_status
) {
878 case FETCH_HEAD_NOT_FOR_MERGE
:
879 merge_status_marker
= "not-for-merge";
881 case FETCH_HEAD_MERGE
:
882 fprintf(fp
, "%s\t%s\t%s",
883 oid_to_hex(&rm
->old_oid
),
886 for (i
= 0; i
< url_len
; ++i
)
894 /* do not write anything to FETCH_HEAD */
900 rc
|= update_local_ref(ref
, what
, rm
, ¬e
,
904 format_display(¬e
, '*',
905 *kind ? kind
: "branch", NULL
,
906 *what ? what
: "HEAD",
907 "FETCH_HEAD", summary_width
);
909 if (verbosity
>= 0 && !shown_url
) {
910 fprintf(stderr
, _("From %.*s\n"),
915 fprintf(stderr
, " %s\n", note
.buf
);
920 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
921 error(_("some local refs could not be updated; try running\n"
922 " 'git remote prune %s' to remove any old, conflicting "
923 "branches"), remote_name
);
926 strbuf_release(¬e
);
933 * We would want to bypass the object transfer altogether if
934 * everything we are going to fetch already exists and is connected
937 static int quickfetch(struct ref
*ref_map
)
939 struct ref
*rm
= ref_map
;
940 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
943 * If we are deepening a shallow clone we already have these
944 * objects reachable. Running rev-list here will return with
945 * a good (0) exit status and we'll bypass the fetch that we
946 * really need to perform. Claiming failure now will ensure
947 * we perform the network exchange to deepen our history.
952 return check_connected(iterate_ref_map
, &rm
, &opt
);
955 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
957 int ret
= quickfetch(ref_map
);
959 ret
= transport_fetch_refs(transport
, ref_map
);
961 ret
|= store_updated_refs(transport
->url
,
962 transport
->remote
->name
,
964 transport_unlock_pack(transport
);
968 static int prune_refs(struct refspec_item
*refs
, int ref_count
, struct ref
*ref_map
,
971 int url_len
, i
, result
= 0;
972 struct ref
*ref
, *stale_refs
= get_stale_heads(refs
, ref_count
, ref_map
);
974 int summary_width
= transport_summary_width(stale_refs
);
975 const char *dangling_msg
= dry_run
976 ?
_(" (%s will become dangling)")
977 : _(" (%s has become dangling)");
980 url
= transport_anonymize_url(raw_url
);
982 url
= xstrdup("foreign");
984 url_len
= strlen(url
);
985 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
989 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
993 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
995 for (ref
= stale_refs
; ref
; ref
= ref
->next
)
996 string_list_append(&refnames
, ref
->name
);
998 result
= delete_refs("fetch: prune", &refnames
, 0);
999 string_list_clear(&refnames
, 0);
1002 if (verbosity
>= 0) {
1003 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
1004 struct strbuf sb
= STRBUF_INIT
;
1006 fprintf(stderr
, _("From %.*s\n"), url_len
, url
);
1009 format_display(&sb
, '-', _("[deleted]"), NULL
,
1010 _("(none)"), prettify_refname(ref
->name
),
1012 fprintf(stderr
, " %s\n",sb
.buf
);
1013 strbuf_release(&sb
);
1014 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
1019 free_refs(stale_refs
);
1023 static void check_not_current_branch(struct ref
*ref_map
)
1025 struct branch
*current_branch
= branch_get(NULL
);
1027 if (is_bare_repository() || !current_branch
)
1030 for (; ref_map
; ref_map
= ref_map
->next
)
1031 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
1032 ref_map
->peer_ref
->name
))
1033 die(_("Refusing to fetch into current branch %s "
1034 "of non-bare repository"), current_branch
->refname
);
1037 static int truncate_fetch_head(void)
1039 const char *filename
= git_path_fetch_head();
1040 FILE *fp
= fopen_for_writing(filename
);
1043 return error_errno(_("cannot open %s"), filename
);
1048 static void set_option(struct transport
*transport
, const char *name
, const char *value
)
1050 int r
= transport_set_option(transport
, name
, value
);
1052 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1053 name
, value
, transport
->url
);
1055 warning(_("Option \"%s\" is ignored for %s\n"),
1056 name
, transport
->url
);
1059 static struct transport
*prepare_transport(struct remote
*remote
, int deepen
)
1061 struct transport
*transport
;
1062 transport
= transport_get(remote
, NULL
);
1063 transport_set_verbosity(transport
, verbosity
, progress
);
1064 transport
->family
= family
;
1066 set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
1068 set_option(transport
, TRANS_OPT_KEEP
, "yes");
1070 set_option(transport
, TRANS_OPT_DEPTH
, depth
);
1071 if (deepen
&& deepen_since
)
1072 set_option(transport
, TRANS_OPT_DEEPEN_SINCE
, deepen_since
);
1073 if (deepen
&& deepen_not
.nr
)
1074 set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1075 (const char *)&deepen_not
);
1076 if (deepen_relative
)
1077 set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, "yes");
1079 set_option(transport
, TRANS_OPT_UPDATE_SHALLOW
, "yes");
1080 if (filter_options
.choice
) {
1081 set_option(transport
, TRANS_OPT_LIST_OBJECTS_FILTER
,
1082 filter_options
.filter_spec
);
1083 set_option(transport
, TRANS_OPT_FROM_PROMISOR
, "1");
1088 static void backfill_tags(struct transport
*transport
, struct ref
*ref_map
)
1093 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1094 * when remote helper is used (setting it to an empty string
1095 * is not unsetting). We could extend the remote helper
1096 * protocol for that, but for now, just force a new connection
1097 * without deepen-since. Similar story for deepen-not.
1099 cannot_reuse
= transport
->cannot_reuse
||
1100 deepen_since
|| deepen_not
.nr
;
1102 gsecondary
= prepare_transport(transport
->remote
, 0);
1103 transport
= gsecondary
;
1106 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
1107 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
1108 transport_set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, NULL
);
1109 fetch_refs(transport
, ref_map
);
1112 transport_disconnect(gsecondary
);
1117 static int do_fetch(struct transport
*transport
,
1118 struct refspec_item
*refs
, int ref_count
)
1120 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
1121 struct ref
*ref_map
;
1123 int autotags
= (transport
->remote
->fetch_tags
== 1);
1126 for_each_ref(add_existing
, &existing_refs
);
1128 if (tags
== TAGS_DEFAULT
) {
1129 if (transport
->remote
->fetch_tags
== 2)
1131 if (transport
->remote
->fetch_tags
== -1)
1135 /* if not appending, truncate FETCH_HEAD */
1136 if (!append
&& !dry_run
) {
1137 retcode
= truncate_fetch_head();
1142 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
1143 if (!update_head_ok
)
1144 check_not_current_branch(ref_map
);
1146 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
1148 struct string_list_item
*peer_item
=
1149 string_list_lookup(&existing_refs
,
1150 rm
->peer_ref
->name
);
1152 struct object_id
*old_oid
= peer_item
->util
;
1153 oidcpy(&rm
->peer_ref
->old_oid
, old_oid
);
1158 if (tags
== TAGS_DEFAULT
&& autotags
)
1159 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1162 * We only prune based on refspecs specified
1163 * explicitly (via command line or configuration); we
1164 * don't care whether --tags was specified.
1167 prune_refs(refs
, ref_count
, ref_map
, transport
->url
);
1169 prune_refs(transport
->remote
->fetch
.items
,
1170 transport
->remote
->fetch
.nr
,
1175 if (fetch_refs(transport
, ref_map
)) {
1182 /* if neither --no-tags nor --tags was specified, do automated tag
1184 if (tags
== TAGS_DEFAULT
&& autotags
) {
1185 struct ref
**tail
= &ref_map
;
1187 find_non_local_tags(transport
, &ref_map
, &tail
);
1189 backfill_tags(transport
, ref_map
);
1194 string_list_clear(&existing_refs
, 1);
1198 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
1200 struct string_list
*list
= priv
;
1201 if (!remote
->skip_default_update
)
1202 string_list_append(list
, remote
->name
);
1206 struct remote_group_data
{
1208 struct string_list
*list
;
1211 static int get_remote_group(const char *key
, const char *value
, void *priv
)
1213 struct remote_group_data
*g
= priv
;
1215 if (skip_prefix(key
, "remotes.", &key
) && !strcmp(key
, g
->name
)) {
1216 /* split list by white space */
1218 size_t wordlen
= strcspn(value
, " \t\n");
1221 string_list_append_nodup(g
->list
,
1222 xstrndup(value
, wordlen
));
1223 value
+= wordlen
+ (value
[wordlen
] != '\0');
1230 static int add_remote_or_group(const char *name
, struct string_list
*list
)
1232 int prev_nr
= list
->nr
;
1233 struct remote_group_data g
;
1234 g
.name
= name
; g
.list
= list
;
1236 git_config(get_remote_group
, &g
);
1237 if (list
->nr
== prev_nr
) {
1238 struct remote
*remote
= remote_get(name
);
1239 if (!remote_is_configured(remote
, 0))
1241 string_list_append(list
, remote
->name
);
1246 static void add_options_to_argv(struct argv_array
*argv
)
1249 argv_array_push(argv
, "--dry-run");
1251 argv_array_push(argv
, prune ?
"--prune" : "--no-prune");
1252 if (prune_tags
!= -1)
1253 argv_array_push(argv
, prune_tags ?
"--prune-tags" : "--no-prune-tags");
1255 argv_array_push(argv
, "--update-head-ok");
1257 argv_array_push(argv
, "--force");
1259 argv_array_push(argv
, "--keep");
1260 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
1261 argv_array_push(argv
, "--recurse-submodules");
1262 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
1263 argv_array_push(argv
, "--recurse-submodules=on-demand");
1264 if (tags
== TAGS_SET
)
1265 argv_array_push(argv
, "--tags");
1266 else if (tags
== TAGS_UNSET
)
1267 argv_array_push(argv
, "--no-tags");
1269 argv_array_push(argv
, "-v");
1271 argv_array_push(argv
, "-v");
1272 else if (verbosity
< 0)
1273 argv_array_push(argv
, "-q");
1277 static int fetch_multiple(struct string_list
*list
)
1280 struct argv_array argv
= ARGV_ARRAY_INIT
;
1282 if (!append
&& !dry_run
) {
1283 int errcode
= truncate_fetch_head();
1288 argv_array_pushl(&argv
, "fetch", "--append", NULL
);
1289 add_options_to_argv(&argv
);
1291 for (i
= 0; i
< list
->nr
; i
++) {
1292 const char *name
= list
->items
[i
].string
;
1293 argv_array_push(&argv
, name
);
1295 printf(_("Fetching %s\n"), name
);
1296 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
1297 error(_("Could not fetch %s"), name
);
1300 argv_array_pop(&argv
);
1303 argv_array_clear(&argv
);
1308 * Fetching from the promisor remote should use the given filter-spec
1309 * or inherit the default filter-spec from the config.
1311 static inline void fetch_one_setup_partial(struct remote
*remote
)
1314 * Explicit --no-filter argument overrides everything, regardless
1315 * of any prior partial clones and fetches.
1317 if (filter_options
.no_filter
)
1321 * If no prior partial clone/fetch and the current fetch DID NOT
1322 * request a partial-fetch, do a normal fetch.
1324 if (!repository_format_partial_clone
&& !filter_options
.choice
)
1328 * If this is the FIRST partial-fetch request, we enable partial
1329 * on this repo and remember the given filter-spec as the default
1330 * for subsequent fetches to this remote.
1332 if (!repository_format_partial_clone
&& filter_options
.choice
) {
1333 partial_clone_register(remote
->name
, &filter_options
);
1338 * We are currently limited to only ONE promisor remote and only
1339 * allow partial-fetches from the promisor remote.
1341 if (strcmp(remote
->name
, repository_format_partial_clone
)) {
1342 if (filter_options
.choice
)
1343 die(_("--filter can only be used with the remote configured in core.partialClone"));
1348 * Do a partial-fetch from the promisor remote using either the
1349 * explicitly given filter-spec or inherit the filter-spec from
1352 if (!filter_options
.choice
)
1353 partial_clone_get_default_filter_spec(&filter_options
);
1357 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
, int prune_tags_ok
)
1359 static const char **refs
= NULL
;
1360 struct refspec_item
*refspec
;
1364 int maybe_prune_tags
;
1365 int remote_via_config
= remote_is_configured(remote
, 0);
1368 die(_("No remote repository specified. Please, specify either a URL or a\n"
1369 "remote name from which new revisions should be fetched."));
1371 gtransport
= prepare_transport(remote
, 1);
1374 /* no command line request */
1375 if (0 <= remote
->prune
)
1376 prune
= remote
->prune
;
1377 else if (0 <= fetch_prune_config
)
1378 prune
= fetch_prune_config
;
1380 prune
= PRUNE_BY_DEFAULT
;
1383 if (prune_tags
< 0) {
1384 /* no command line request */
1385 if (0 <= remote
->prune_tags
)
1386 prune_tags
= remote
->prune_tags
;
1387 else if (0 <= fetch_prune_tags_config
)
1388 prune_tags
= fetch_prune_tags_config
;
1390 prune_tags
= PRUNE_TAGS_BY_DEFAULT
;
1393 maybe_prune_tags
= prune_tags_ok
&& prune_tags
;
1394 if (maybe_prune_tags
&& remote_via_config
)
1395 add_prune_tags_to_fetch_refspec(remote
);
1397 if (argc
> 0 || (maybe_prune_tags
&& !remote_via_config
)) {
1398 size_t nr_alloc
= st_add3(argc
, maybe_prune_tags
, 1);
1399 refs
= xcalloc(nr_alloc
, sizeof(const char *));
1400 if (maybe_prune_tags
) {
1401 refs
[j
++] = xstrdup("refs/tags/*:refs/tags/*");
1408 for (i
= 0; i
< argc
; i
++) {
1409 if (!strcmp(argv
[i
], "tag")) {
1412 die(_("You need to specify a tag name."));
1413 refs
[j
++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1416 refs
[j
++] = argv
[i
];
1421 sigchain_push_common(unlock_pack_on_signal
);
1422 atexit(unlock_pack
);
1423 refspec
= parse_fetch_refspec(ref_nr
, refs
);
1424 exit_code
= do_fetch(gtransport
, refspec
, ref_nr
);
1425 free_refspec(ref_nr
, refspec
);
1426 transport_disconnect(gtransport
);
1431 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
1434 struct string_list list
= STRING_LIST_INIT_DUP
;
1435 struct remote
*remote
= NULL
;
1437 int prune_tags_ok
= 1;
1438 struct argv_array argv_gc_auto
= ARGV_ARRAY_INIT
;
1440 packet_trace_identity("fetch");
1442 fetch_if_missing
= 0;
1444 /* Record the command line for the reflog */
1445 strbuf_addstr(&default_rla
, "fetch");
1446 for (i
= 1; i
< argc
; i
++)
1447 strbuf_addf(&default_rla
, " %s", argv
[i
]);
1449 config_from_gitmodules(gitmodules_fetch_config
, NULL
);
1450 git_config(git_fetch_config
, NULL
);
1452 argc
= parse_options(argc
, argv
, prefix
,
1453 builtin_fetch_options
, builtin_fetch_usage
, 0);
1455 if (deepen_relative
) {
1456 if (deepen_relative
< 0)
1457 die(_("Negative depth in --deepen is not supported"));
1459 die(_("--deepen and --depth are mutually exclusive"));
1460 depth
= xstrfmt("%d", deepen_relative
);
1464 die(_("--depth and --unshallow cannot be used together"));
1465 else if (!is_repository_shallow())
1466 die(_("--unshallow on a complete repository does not make sense"));
1468 depth
= xstrfmt("%d", INFINITE_DEPTH
);
1471 /* no need to be strict, transport_set_option() will validate it again */
1472 if (depth
&& atoi(depth
) < 1)
1473 die(_("depth %s is not a positive number"), depth
);
1474 if (depth
|| deepen_since
|| deepen_not
.nr
)
1477 if (filter_options
.choice
&& !repository_format_partial_clone
)
1478 die("--filter can only be used when extensions.partialClone is set");
1482 die(_("fetch --all does not take a repository argument"));
1484 die(_("fetch --all does not make sense with refspecs"));
1485 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1486 } else if (argc
== 0) {
1487 /* No arguments -- use default remote */
1488 remote
= remote_get(NULL
);
1489 } else if (multiple
) {
1490 /* All arguments are assumed to be remotes or groups */
1491 for (i
= 0; i
< argc
; i
++)
1492 if (!add_remote_or_group(argv
[i
], &list
))
1493 die(_("No such remote or remote group: %s"), argv
[i
]);
1495 /* Single remote or group */
1496 (void) add_remote_or_group(argv
[0], &list
);
1498 /* More than one remote */
1500 die(_("Fetching a group and specifying refspecs does not make sense"));
1502 /* Zero or one remotes */
1503 remote
= remote_get(argv
[0]);
1504 prune_tags_ok
= (argc
== 1);
1511 if (filter_options
.choice
|| repository_format_partial_clone
)
1512 fetch_one_setup_partial(remote
);
1513 result
= fetch_one(remote
, argc
, argv
, prune_tags_ok
);
1515 if (filter_options
.choice
)
1516 die(_("--filter can only be used with the remote configured in core.partialClone"));
1517 /* TODO should this also die if we have a previous partial-clone? */
1518 result
= fetch_multiple(&list
);
1521 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1522 struct argv_array options
= ARGV_ARRAY_INIT
;
1524 add_options_to_argv(&options
);
1525 result
= fetch_populated_submodules(the_repository
,
1529 recurse_submodules_default
,
1532 argv_array_clear(&options
);
1535 string_list_clear(&list
, 0);
1537 close_all_packs(the_repository
->objects
);
1539 argv_array_pushl(&argv_gc_auto
, "gc", "--auto", NULL
);
1541 argv_array_push(&argv_gc_auto
, "--quiet");
1542 run_command_v_opt(argv_gc_auto
.argv
, RUN_GIT_CMD
);
1543 argv_array_clear(&argv_gc_auto
);