9 #include "string-list.h"
11 #include "transport.h"
12 #include "run-command.h"
13 #include "parse-options.h"
15 #include "submodule-config.h"
16 #include "submodule.h"
17 #include "connected.h"
18 #include "argv-array.h"
21 static const char * const builtin_fetch_usage
[] = {
22 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
23 N_("git fetch [<options>] <group>"),
24 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
25 N_("git fetch --all [<options>]"),
35 static int fetch_prune_config
= -1; /* unspecified */
36 static int prune
= -1; /* unspecified */
37 #define PRUNE_BY_DEFAULT 0 /* do we prune by default? */
39 static int all
, append
, dry_run
, force
, keep
, multiple
, update_head_ok
, verbosity
, deepen_relative
;
40 static int progress
= -1, recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
41 static int tags
= TAGS_DEFAULT
, unshallow
, update_shallow
, deepen
;
42 static int max_children
= -1;
43 static enum transport_family family
;
44 static const char *depth
;
45 static const char *deepen_since
;
46 static const char *upload_pack
;
47 static struct string_list deepen_not
= STRING_LIST_INIT_NODUP
;
48 static struct strbuf default_rla
= STRBUF_INIT
;
49 static struct transport
*gtransport
;
50 static struct transport
*gsecondary
;
51 static const char *submodule_prefix
= "";
52 static const char *recurse_submodules_default
;
53 static int shown_url
= 0;
54 static int refmap_alloc
, refmap_nr
;
55 static const char **refmap_array
;
57 static int option_parse_recurse_submodules(const struct option
*opt
,
58 const char *arg
, int unset
)
61 recurse_submodules
= RECURSE_SUBMODULES_OFF
;
64 recurse_submodules
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
66 recurse_submodules
= RECURSE_SUBMODULES_ON
;
71 static int git_fetch_config(const char *k
, const char *v
, void *cb
)
73 if (!strcmp(k
, "fetch.prune")) {
74 fetch_prune_config
= git_config_bool(k
, v
);
78 if (!strcmp(k
, "submodule.recurse")) {
79 int r
= git_config_bool(k
, v
) ?
80 RECURSE_SUBMODULES_ON
: RECURSE_SUBMODULES_OFF
;
81 recurse_submodules
= r
;
84 return git_default_config(k
, v
, cb
);
87 static int parse_refmap_arg(const struct option
*opt
, const char *arg
, int unset
)
89 ALLOC_GROW(refmap_array
, refmap_nr
+ 1, refmap_alloc
);
92 * "git fetch --refmap='' origin foo"
93 * can be used to tell the command not to store anywhere
96 refmap_array
[refmap_nr
++] = arg
;
100 static struct option builtin_fetch_options
[] = {
101 OPT__VERBOSITY(&verbosity
),
102 OPT_BOOL(0, "all", &all
,
103 N_("fetch from all remotes")),
104 OPT_BOOL('a', "append", &append
,
105 N_("append to .git/FETCH_HEAD instead of overwriting")),
106 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
107 N_("path to upload pack on remote end")),
108 OPT__FORCE(&force
, N_("force overwrite of local branch")),
109 OPT_BOOL('m', "multiple", &multiple
,
110 N_("fetch from multiple remotes")),
111 OPT_SET_INT('t', "tags", &tags
,
112 N_("fetch all tags and associated objects"), TAGS_SET
),
113 OPT_SET_INT('n', NULL
, &tags
,
114 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
115 OPT_INTEGER('j', "jobs", &max_children
,
116 N_("number of submodules fetched in parallel")),
117 OPT_BOOL('p', "prune", &prune
,
118 N_("prune remote-tracking branches no longer on remote")),
119 { OPTION_CALLBACK
, 0, "recurse-submodules", NULL
, N_("on-demand"),
120 N_("control recursive fetching of submodules"),
121 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
122 OPT_BOOL(0, "dry-run", &dry_run
,
124 OPT_BOOL('k', "keep", &keep
, N_("keep downloaded pack")),
125 OPT_BOOL('u', "update-head-ok", &update_head_ok
,
126 N_("allow updating of HEAD ref")),
127 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
128 OPT_STRING(0, "depth", &depth
, N_("depth"),
129 N_("deepen history of shallow clone")),
130 OPT_STRING(0, "shallow-since", &deepen_since
, N_("time"),
131 N_("deepen history of shallow repository based on time")),
132 OPT_STRING_LIST(0, "shallow-exclude", &deepen_not
, N_("revision"),
133 N_("deepen history of shallow clone, excluding rev")),
134 OPT_INTEGER(0, "deepen", &deepen_relative
,
135 N_("deepen history of shallow clone")),
136 { OPTION_SET_INT
, 0, "unshallow", &unshallow
, NULL
,
137 N_("convert to a complete repository"),
138 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, NULL
, 1 },
139 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
140 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
141 { OPTION_STRING
, 0, "recurse-submodules-default",
142 &recurse_submodules_default
, NULL
,
143 N_("default mode for recursion"), PARSE_OPT_HIDDEN
},
144 OPT_BOOL(0, "update-shallow", &update_shallow
,
145 N_("accept refs that update .git/shallow")),
146 { OPTION_CALLBACK
, 0, "refmap", NULL
, N_("refmap"),
147 N_("specify fetch refmap"), PARSE_OPT_NONEG
, parse_refmap_arg
},
148 OPT_SET_INT('4', "ipv4", &family
, N_("use IPv4 addresses only"),
149 TRANSPORT_FAMILY_IPV4
),
150 OPT_SET_INT('6', "ipv6", &family
, N_("use IPv6 addresses only"),
151 TRANSPORT_FAMILY_IPV6
),
155 static void unlock_pack(void)
158 transport_unlock_pack(gtransport
);
160 transport_unlock_pack(gsecondary
);
163 static void unlock_pack_on_signal(int signo
)
170 static void add_merge_config(struct ref
**head
,
171 const struct ref
*remote_refs
,
172 struct branch
*branch
,
177 for (i
= 0; i
< branch
->merge_nr
; i
++) {
178 struct ref
*rm
, **old_tail
= *tail
;
179 struct refspec refspec
;
181 for (rm
= *head
; rm
; rm
= rm
->next
) {
182 if (branch_merge_matches(branch
, i
, rm
->name
)) {
183 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
191 * Not fetched to a remote-tracking branch? We need to fetch
192 * it anyway to allow this branch's "branch.$name.merge"
193 * to be honored by 'git pull', but we do not have to
194 * fail if branch.$name.merge is misconfigured to point
195 * at a nonexisting branch. If we were indeed called by
196 * 'git pull', it will notice the misconfiguration because
197 * there is no entry in the resulting FETCH_HEAD marked
200 memset(&refspec
, 0, sizeof(refspec
));
201 refspec
.src
= branch
->merge
[i
]->src
;
202 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
203 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
204 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
208 static int add_existing(const char *refname
, const struct object_id
*oid
,
209 int flag
, void *cbdata
)
211 struct string_list
*list
= (struct string_list
*)cbdata
;
212 struct string_list_item
*item
= string_list_insert(list
, refname
);
213 struct object_id
*old_oid
= xmalloc(sizeof(*old_oid
));
215 oidcpy(old_oid
, oid
);
216 item
->util
= old_oid
;
220 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
222 struct ref
*rm
= *head
;
224 if (!hashcmp(rm
->old_oid
.hash
, sha1
))
231 static void find_non_local_tags(struct transport
*transport
,
235 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
236 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
237 const struct ref
*ref
;
238 struct string_list_item
*item
= NULL
;
240 for_each_ref(add_existing
, &existing_refs
);
241 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
242 if (!starts_with(ref
->name
, "refs/tags/"))
246 * The peeled ref always follows the matching base
247 * ref, so if we see a peeled ref that we don't want
248 * to fetch then we can mark the ref entry in the list
249 * as one to ignore by setting util to NULL.
251 if (ends_with(ref
->name
, "^{}")) {
253 !has_object_file_with_flags(&ref
->old_oid
,
254 OBJECT_INFO_QUICK
) &&
255 !will_fetch(head
, ref
->old_oid
.hash
) &&
256 !has_sha1_file_with_flags(item
->util
,
257 OBJECT_INFO_QUICK
) &&
258 !will_fetch(head
, item
->util
))
265 * If item is non-NULL here, then we previously saw a
266 * ref not followed by a peeled reference, so we need
267 * to check if it is a lightweight tag that we want to
271 !has_sha1_file_with_flags(item
->util
, OBJECT_INFO_QUICK
) &&
272 !will_fetch(head
, item
->util
))
277 /* skip duplicates and refs that we already have */
278 if (string_list_has_string(&remote_refs
, ref
->name
) ||
279 string_list_has_string(&existing_refs
, ref
->name
))
282 item
= string_list_insert(&remote_refs
, ref
->name
);
283 item
->util
= (void *)&ref
->old_oid
;
285 string_list_clear(&existing_refs
, 1);
288 * We may have a final lightweight tag that needs to be
289 * checked to see if it needs fetching.
292 !has_sha1_file_with_flags(item
->util
, OBJECT_INFO_QUICK
) &&
293 !will_fetch(head
, item
->util
))
297 * For all the tags in the remote_refs string list,
298 * add them to the list of refs to be fetched
300 for_each_string_list_item(item
, &remote_refs
) {
301 /* Unless we have already decided to ignore this item... */
304 struct ref
*rm
= alloc_ref(item
->string
);
305 rm
->peer_ref
= alloc_ref(item
->string
);
306 oidcpy(&rm
->old_oid
, item
->util
);
312 string_list_clear(&remote_refs
, 0);
315 static struct ref
*get_ref_map(struct transport
*transport
,
316 struct refspec
*refspecs
, int refspec_count
,
317 int tags
, int *autotags
)
321 struct ref
*ref_map
= NULL
;
322 struct ref
**tail
= &ref_map
;
324 /* opportunistically-updated references: */
325 struct ref
*orefs
= NULL
, **oref_tail
= &orefs
;
327 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
330 struct refspec
*fetch_refspec
;
331 int fetch_refspec_nr
;
333 for (i
= 0; i
< refspec_count
; i
++) {
334 get_fetch_map(remote_refs
, &refspecs
[i
], &tail
, 0);
335 if (refspecs
[i
].dst
&& refspecs
[i
].dst
[0])
338 /* Merge everything on the command line (but not --tags) */
339 for (rm
= ref_map
; rm
; rm
= rm
->next
)
340 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
343 * For any refs that we happen to be fetching via
344 * command-line arguments, the destination ref might
345 * have been missing or have been different than the
346 * remote-tracking ref that would be derived from the
347 * configured refspec. In these cases, we want to
348 * take the opportunity to update their configured
349 * remote-tracking reference. However, we do not want
350 * to mention these entries in FETCH_HEAD at all, as
351 * they would simply be duplicates of existing
352 * entries, so we set them FETCH_HEAD_IGNORE below.
354 * We compute these entries now, based only on the
355 * refspecs specified on the command line. But we add
356 * them to the list following the refspecs resulting
357 * from the tags option so that one of the latter,
358 * which has FETCH_HEAD_NOT_FOR_MERGE, is not removed
359 * by ref_remove_duplicates() in favor of one of these
360 * opportunistic entries with FETCH_HEAD_IGNORE.
363 fetch_refspec
= parse_fetch_refspec(refmap_nr
, refmap_array
);
364 fetch_refspec_nr
= refmap_nr
;
366 fetch_refspec
= transport
->remote
->fetch
;
367 fetch_refspec_nr
= transport
->remote
->fetch_refspec_nr
;
370 for (i
= 0; i
< fetch_refspec_nr
; i
++)
371 get_fetch_map(ref_map
, &fetch_refspec
[i
], &oref_tail
, 1);
372 } else if (refmap_array
) {
373 die("--refmap option is only meaningful with command-line refspec(s).");
375 /* Use the defaults */
376 struct remote
*remote
= transport
->remote
;
377 struct branch
*branch
= branch_get(NULL
);
378 int has_merge
= branch_has_merge_config(branch
);
380 (remote
->fetch_refspec_nr
||
381 /* Note: has_merge implies non-NULL branch->remote_name */
382 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
383 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
384 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
385 if (remote
->fetch
[i
].dst
&&
386 remote
->fetch
[i
].dst
[0])
388 if (!i
&& !has_merge
&& ref_map
&&
389 !remote
->fetch
[0].pattern
)
390 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
393 * if the remote we're fetching from is the same
394 * as given in branch.<name>.remote, we add the
395 * ref given in branch.<name>.merge, too.
397 * Note: has_merge implies non-NULL branch->remote_name
400 !strcmp(branch
->remote_name
, remote
->name
))
401 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
403 ref_map
= get_remote_ref(remote_refs
, "HEAD");
405 die(_("Couldn't find remote ref HEAD"));
406 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
407 tail
= &ref_map
->next
;
411 if (tags
== TAGS_SET
)
412 /* also fetch all tags */
413 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
414 else if (tags
== TAGS_DEFAULT
&& *autotags
)
415 find_non_local_tags(transport
, &ref_map
, &tail
);
417 /* Now append any refs to be updated opportunistically: */
419 for (rm
= orefs
; rm
; rm
= rm
->next
) {
420 rm
->fetch_head_status
= FETCH_HEAD_IGNORE
;
424 return ref_remove_duplicates(ref_map
);
427 #define STORE_REF_ERROR_OTHER 1
428 #define STORE_REF_ERROR_DF_CONFLICT 2
430 static int s_update_ref(const char *action
,
435 char *rla
= getenv("GIT_REFLOG_ACTION");
436 struct ref_transaction
*transaction
;
437 struct strbuf err
= STRBUF_INIT
;
438 int ret
, df_conflict
= 0;
443 rla
= default_rla
.buf
;
444 msg
= xstrfmt("%s: %s", rla
, action
);
446 transaction
= ref_transaction_begin(&err
);
448 ref_transaction_update(transaction
, ref
->name
,
450 check_old
? ref
->old_oid
.hash
: NULL
,
454 ret
= ref_transaction_commit(transaction
, &err
);
456 df_conflict
= (ret
== TRANSACTION_NAME_CONFLICT
);
460 ref_transaction_free(transaction
);
461 strbuf_release(&err
);
465 ref_transaction_free(transaction
);
466 error("%s", err
.buf
);
467 strbuf_release(&err
);
469 return df_conflict
? STORE_REF_ERROR_DF_CONFLICT
470 : STORE_REF_ERROR_OTHER
;
473 static int refcol_width
= 10;
474 static int compact_format
;
476 static void adjust_refcol_width(const struct ref
*ref
)
478 int max
, rlen
, llen
, len
;
480 /* uptodate lines are only shown on high verbosity level */
481 if (!verbosity
&& !oidcmp(&ref
->peer_ref
->old_oid
, &ref
->old_oid
))
484 max
= term_columns();
485 rlen
= utf8_strwidth(prettify_refname(ref
->name
));
487 llen
= utf8_strwidth(prettify_refname(ref
->peer_ref
->name
));
490 * rough estimation to see if the output line is too long and
491 * should not be counted (we can't do precise calculation
492 * anyway because we don't know if the error explanation part
493 * will be printed in update_local_ref)
495 if (compact_format
) {
499 len
= 21 /* flag and summary */ + rlen
+ 4 /* -> */ + llen
;
504 * Not precise calculation for compact mode because '*' can
505 * appear on the left hand side of '->' and shrink the column
508 if (refcol_width
< rlen
)
512 static void prepare_format_display(struct ref
*ref_map
)
515 const char *format
= "full";
517 git_config_get_string_const("fetch.output", &format
);
518 if (!strcasecmp(format
, "full"))
520 else if (!strcasecmp(format
, "compact"))
523 die(_("configuration fetch.output contains invalid value %s"),
526 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
527 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
||
529 !strcmp(rm
->name
, "HEAD"))
532 adjust_refcol_width(rm
);
536 static void print_remote_to_local(struct strbuf
*display
,
537 const char *remote
, const char *local
)
539 strbuf_addf(display
, "%-*s -> %s", refcol_width
, remote
, local
);
542 static int find_and_replace(struct strbuf
*haystack
,
544 const char *placeholder
)
546 const char *p
= strstr(haystack
->buf
, needle
);
552 if (p
> haystack
->buf
&& p
[-1] != '/')
556 nlen
= strlen(needle
);
557 if (plen
> nlen
&& p
[nlen
] != '/')
560 strbuf_splice(haystack
, p
- haystack
->buf
, nlen
,
561 placeholder
, strlen(placeholder
));
565 static void print_compact(struct strbuf
*display
,
566 const char *remote
, const char *local
)
568 struct strbuf r
= STRBUF_INIT
;
569 struct strbuf l
= STRBUF_INIT
;
571 if (!strcmp(remote
, local
)) {
572 strbuf_addf(display
, "%-*s -> *", refcol_width
, remote
);
576 strbuf_addstr(&r
, remote
);
577 strbuf_addstr(&l
, local
);
579 if (!find_and_replace(&r
, local
, "*"))
580 find_and_replace(&l
, remote
, "*");
581 print_remote_to_local(display
, r
.buf
, l
.buf
);
587 static void format_display(struct strbuf
*display
, char code
,
588 const char *summary
, const char *error
,
589 const char *remote
, const char *local
,
592 int width
= (summary_width
+ strlen(summary
) - gettext_width(summary
));
594 strbuf_addf(display
, "%c %-*s ", code
, width
, summary
);
596 print_remote_to_local(display
, remote
, local
);
598 print_compact(display
, remote
, local
);
600 strbuf_addf(display
, " (%s)", error
);
603 static int update_local_ref(struct ref
*ref
,
605 const struct ref
*remote_ref
,
606 struct strbuf
*display
,
609 struct commit
*current
= NULL
, *updated
;
610 enum object_type type
;
611 struct branch
*current_branch
= branch_get(NULL
);
612 const char *pretty_ref
= prettify_refname(ref
->name
);
614 type
= sha1_object_info(ref
->new_oid
.hash
, NULL
);
616 die(_("object %s not found"), oid_to_hex(&ref
->new_oid
));
618 if (!oidcmp(&ref
->old_oid
, &ref
->new_oid
)) {
620 format_display(display
, '=', _("[up to date]"), NULL
,
621 remote
, pretty_ref
, summary_width
);
625 if (current_branch
&&
626 !strcmp(ref
->name
, current_branch
->name
) &&
627 !(update_head_ok
|| is_bare_repository()) &&
628 !is_null_oid(&ref
->old_oid
)) {
630 * If this is the head, and it's not okay to update
631 * the head, and the old value of the head isn't empty...
633 format_display(display
, '!', _("[rejected]"),
634 _("can't fetch in current branch"),
635 remote
, pretty_ref
, summary_width
);
639 if (!is_null_oid(&ref
->old_oid
) &&
640 starts_with(ref
->name
, "refs/tags/")) {
642 r
= s_update_ref("updating tag", ref
, 0);
643 format_display(display
, r
? '!' : 't', _("[tag update]"),
644 r
? _("unable to update local ref") : NULL
,
645 remote
, pretty_ref
, summary_width
);
649 current
= lookup_commit_reference_gently(&ref
->old_oid
, 1);
650 updated
= lookup_commit_reference_gently(&ref
->new_oid
, 1);
651 if (!current
|| !updated
) {
656 * Nicely describe the new ref we're fetching.
657 * Base this on the remote's ref name, as it's
658 * more likely to follow a standard layout.
660 const char *name
= remote_ref
? remote_ref
->name
: "";
661 if (starts_with(name
, "refs/tags/")) {
663 what
= _("[new tag]");
664 } else if (starts_with(name
, "refs/heads/")) {
665 msg
= "storing head";
666 what
= _("[new branch]");
669 what
= _("[new ref]");
672 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
673 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
674 check_for_new_submodule_commits(&ref
->new_oid
);
675 r
= s_update_ref(msg
, ref
, 0);
676 format_display(display
, r
? '!' : '*', what
,
677 r
? _("unable to update local ref") : NULL
,
678 remote
, pretty_ref
, summary_width
);
682 if (in_merge_bases(current
, updated
)) {
683 struct strbuf quickref
= STRBUF_INIT
;
685 strbuf_add_unique_abbrev(&quickref
, current
->object
.oid
.hash
, DEFAULT_ABBREV
);
686 strbuf_addstr(&quickref
, "..");
687 strbuf_add_unique_abbrev(&quickref
, ref
->new_oid
.hash
, DEFAULT_ABBREV
);
688 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
689 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
690 check_for_new_submodule_commits(&ref
->new_oid
);
691 r
= s_update_ref("fast-forward", ref
, 1);
692 format_display(display
, r
? '!' : ' ', quickref
.buf
,
693 r
? _("unable to update local ref") : NULL
,
694 remote
, pretty_ref
, summary_width
);
695 strbuf_release(&quickref
);
697 } else if (force
|| ref
->force
) {
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("forced-update", ref
, 1);
707 format_display(display
, r
? '!' : '+', quickref
.buf
,
708 r
? _("unable to update local ref") : _("forced update"),
709 remote
, pretty_ref
, summary_width
);
710 strbuf_release(&quickref
);
713 format_display(display
, '!', _("[rejected]"), _("non-fast-forward"),
714 remote
, pretty_ref
, summary_width
);
719 static int iterate_ref_map(void *cb_data
, unsigned char sha1
[20])
721 struct ref
**rm
= cb_data
;
722 struct ref
*ref
= *rm
;
724 while (ref
&& ref
->status
== REF_STATUS_REJECT_SHALLOW
)
727 return -1; /* end of the list */
729 hashcpy(sha1
, ref
->old_oid
.hash
);
733 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
737 struct commit
*commit
;
738 int url_len
, i
, rc
= 0;
739 struct strbuf note
= STRBUF_INIT
;
740 const char *what
, *kind
;
743 const char *filename
= dry_run
? "/dev/null" : git_path_fetch_head();
745 int summary_width
= transport_summary_width(ref_map
);
747 fp
= fopen(filename
, "a");
749 return error_errno(_("cannot open %s"), filename
);
752 url
= transport_anonymize_url(raw_url
);
754 url
= xstrdup("foreign");
757 if (check_connected(iterate_ref_map
, &rm
, NULL
)) {
758 rc
= error(_("%s did not send all necessary objects\n"), url
);
762 prepare_format_display(ref_map
);
765 * We do a pass for each fetch_head_status type in their enum order, so
766 * merged entries are written before not-for-merge. That lets readers
767 * use FETCH_HEAD as a refname to refer to the ref to be merged.
769 for (want_status
= FETCH_HEAD_MERGE
;
770 want_status
<= FETCH_HEAD_IGNORE
;
772 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
773 struct ref
*ref
= NULL
;
774 const char *merge_status_marker
= "";
776 if (rm
->status
== REF_STATUS_REJECT_SHALLOW
) {
777 if (want_status
== FETCH_HEAD_MERGE
)
778 warning(_("reject %s because shallow roots are not allowed to be updated"),
779 rm
->peer_ref
? rm
->peer_ref
->name
: rm
->name
);
783 commit
= lookup_commit_reference_gently(&rm
->old_oid
,
786 rm
->fetch_head_status
= FETCH_HEAD_NOT_FOR_MERGE
;
788 if (rm
->fetch_head_status
!= want_status
)
792 ref
= alloc_ref(rm
->peer_ref
->name
);
793 oidcpy(&ref
->old_oid
, &rm
->peer_ref
->old_oid
);
794 oidcpy(&ref
->new_oid
, &rm
->old_oid
);
795 ref
->force
= rm
->peer_ref
->force
;
799 if (!strcmp(rm
->name
, "HEAD")) {
803 else if (starts_with(rm
->name
, "refs/heads/")) {
805 what
= rm
->name
+ 11;
807 else if (starts_with(rm
->name
, "refs/tags/")) {
809 what
= rm
->name
+ 10;
811 else if (starts_with(rm
->name
, "refs/remotes/")) {
812 kind
= "remote-tracking branch";
813 what
= rm
->name
+ 13;
820 url_len
= strlen(url
);
821 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
824 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
830 strbuf_addf(¬e
, "%s ", kind
);
831 strbuf_addf(¬e
, "'%s' of ", what
);
833 switch (rm
->fetch_head_status
) {
834 case FETCH_HEAD_NOT_FOR_MERGE
:
835 merge_status_marker
= "not-for-merge";
837 case FETCH_HEAD_MERGE
:
838 fprintf(fp
, "%s\t%s\t%s",
839 oid_to_hex(&rm
->old_oid
),
842 for (i
= 0; i
< url_len
; ++i
)
850 /* do not write anything to FETCH_HEAD */
856 rc
|= update_local_ref(ref
, what
, rm
, ¬e
,
860 format_display(¬e
, '*',
861 *kind
? kind
: "branch", NULL
,
862 *what
? what
: "HEAD",
863 "FETCH_HEAD", summary_width
);
865 if (verbosity
>= 0 && !shown_url
) {
866 fprintf(stderr
, _("From %.*s\n"),
871 fprintf(stderr
, " %s\n", note
.buf
);
876 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
877 error(_("some local refs could not be updated; try running\n"
878 " 'git remote prune %s' to remove any old, conflicting "
879 "branches"), remote_name
);
882 strbuf_release(¬e
);
889 * We would want to bypass the object transfer altogether if
890 * everything we are going to fetch already exists and is connected
893 static int quickfetch(struct ref
*ref_map
)
895 struct ref
*rm
= ref_map
;
896 struct check_connected_options opt
= CHECK_CONNECTED_INIT
;
899 * If we are deepening a shallow clone we already have these
900 * objects reachable. Running rev-list here will return with
901 * a good (0) exit status and we'll bypass the fetch that we
902 * really need to perform. Claiming failure now will ensure
903 * we perform the network exchange to deepen our history.
908 return check_connected(iterate_ref_map
, &rm
, &opt
);
911 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
913 int ret
= quickfetch(ref_map
);
915 ret
= transport_fetch_refs(transport
, ref_map
);
917 ret
|= store_updated_refs(transport
->url
,
918 transport
->remote
->name
,
920 transport_unlock_pack(transport
);
924 static int prune_refs(struct refspec
*refs
, int ref_count
, struct ref
*ref_map
,
927 int url_len
, i
, result
= 0;
928 struct ref
*ref
, *stale_refs
= get_stale_heads(refs
, ref_count
, ref_map
);
930 int summary_width
= transport_summary_width(stale_refs
);
931 const char *dangling_msg
= dry_run
932 ? _(" (%s will become dangling)")
933 : _(" (%s has become dangling)");
936 url
= transport_anonymize_url(raw_url
);
938 url
= xstrdup("foreign");
940 url_len
= strlen(url
);
941 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
945 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
949 struct string_list refnames
= STRING_LIST_INIT_NODUP
;
951 for (ref
= stale_refs
; ref
; ref
= ref
->next
)
952 string_list_append(&refnames
, ref
->name
);
954 result
= delete_refs("fetch: prune", &refnames
, 0);
955 string_list_clear(&refnames
, 0);
958 if (verbosity
>= 0) {
959 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
960 struct strbuf sb
= STRBUF_INIT
;
962 fprintf(stderr
, _("From %.*s\n"), url_len
, url
);
965 format_display(&sb
, '-', _("[deleted]"), NULL
,
966 _("(none)"), prettify_refname(ref
->name
),
968 fprintf(stderr
, " %s\n",sb
.buf
);
970 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
975 free_refs(stale_refs
);
979 static void check_not_current_branch(struct ref
*ref_map
)
981 struct branch
*current_branch
= branch_get(NULL
);
983 if (is_bare_repository() || !current_branch
)
986 for (; ref_map
; ref_map
= ref_map
->next
)
987 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
988 ref_map
->peer_ref
->name
))
989 die(_("Refusing to fetch into current branch %s "
990 "of non-bare repository"), current_branch
->refname
);
993 static int truncate_fetch_head(void)
995 const char *filename
= git_path_fetch_head();
996 FILE *fp
= fopen_for_writing(filename
);
999 return error_errno(_("cannot open %s"), filename
);
1004 static void set_option(struct transport
*transport
, const char *name
, const char *value
)
1006 int r
= transport_set_option(transport
, name
, value
);
1008 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
1009 name
, value
, transport
->url
);
1011 warning(_("Option \"%s\" is ignored for %s\n"),
1012 name
, transport
->url
);
1015 static struct transport
*prepare_transport(struct remote
*remote
, int deepen
)
1017 struct transport
*transport
;
1018 transport
= transport_get(remote
, NULL
);
1019 transport_set_verbosity(transport
, verbosity
, progress
);
1020 transport
->family
= family
;
1022 set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
1024 set_option(transport
, TRANS_OPT_KEEP
, "yes");
1026 set_option(transport
, TRANS_OPT_DEPTH
, depth
);
1027 if (deepen
&& deepen_since
)
1028 set_option(transport
, TRANS_OPT_DEEPEN_SINCE
, deepen_since
);
1029 if (deepen
&& deepen_not
.nr
)
1030 set_option(transport
, TRANS_OPT_DEEPEN_NOT
,
1031 (const char *)&deepen_not
);
1032 if (deepen_relative
)
1033 set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, "yes");
1035 set_option(transport
, TRANS_OPT_UPDATE_SHALLOW
, "yes");
1039 static void backfill_tags(struct transport
*transport
, struct ref
*ref_map
)
1044 * Once we have set TRANS_OPT_DEEPEN_SINCE, we can't unset it
1045 * when remote helper is used (setting it to an empty string
1046 * is not unsetting). We could extend the remote helper
1047 * protocol for that, but for now, just force a new connection
1048 * without deepen-since. Similar story for deepen-not.
1050 cannot_reuse
= transport
->cannot_reuse
||
1051 deepen_since
|| deepen_not
.nr
;
1053 gsecondary
= prepare_transport(transport
->remote
, 0);
1054 transport
= gsecondary
;
1057 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
1058 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
1059 transport_set_option(transport
, TRANS_OPT_DEEPEN_RELATIVE
, NULL
);
1060 fetch_refs(transport
, ref_map
);
1063 transport_disconnect(gsecondary
);
1068 static int do_fetch(struct transport
*transport
,
1069 struct refspec
*refs
, int ref_count
)
1071 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
1072 struct ref
*ref_map
;
1074 int autotags
= (transport
->remote
->fetch_tags
== 1);
1077 for_each_ref(add_existing
, &existing_refs
);
1079 if (tags
== TAGS_DEFAULT
) {
1080 if (transport
->remote
->fetch_tags
== 2)
1082 if (transport
->remote
->fetch_tags
== -1)
1086 if (!transport
->get_refs_list
|| !transport
->fetch
)
1087 die(_("Don't know how to fetch from %s"), transport
->url
);
1089 /* if not appending, truncate FETCH_HEAD */
1090 if (!append
&& !dry_run
) {
1091 retcode
= truncate_fetch_head();
1096 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
1097 if (!update_head_ok
)
1098 check_not_current_branch(ref_map
);
1100 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
1102 struct string_list_item
*peer_item
=
1103 string_list_lookup(&existing_refs
,
1104 rm
->peer_ref
->name
);
1106 struct object_id
*old_oid
= peer_item
->util
;
1107 oidcpy(&rm
->peer_ref
->old_oid
, old_oid
);
1112 if (tags
== TAGS_DEFAULT
&& autotags
)
1113 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
1116 * We only prune based on refspecs specified
1117 * explicitly (via command line or configuration); we
1118 * don't care whether --tags was specified.
1121 prune_refs(refs
, ref_count
, ref_map
, transport
->url
);
1123 prune_refs(transport
->remote
->fetch
,
1124 transport
->remote
->fetch_refspec_nr
,
1129 if (fetch_refs(transport
, ref_map
)) {
1136 /* if neither --no-tags nor --tags was specified, do automated tag
1138 if (tags
== TAGS_DEFAULT
&& autotags
) {
1139 struct ref
**tail
= &ref_map
;
1141 find_non_local_tags(transport
, &ref_map
, &tail
);
1143 backfill_tags(transport
, ref_map
);
1148 string_list_clear(&existing_refs
, 1);
1152 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
1154 struct string_list
*list
= priv
;
1155 if (!remote
->skip_default_update
)
1156 string_list_append(list
, remote
->name
);
1160 struct remote_group_data
{
1162 struct string_list
*list
;
1165 static int get_remote_group(const char *key
, const char *value
, void *priv
)
1167 struct remote_group_data
*g
= priv
;
1169 if (skip_prefix(key
, "remotes.", &key
) && !strcmp(key
, g
->name
)) {
1170 /* split list by white space */
1172 size_t wordlen
= strcspn(value
, " \t\n");
1175 string_list_append_nodup(g
->list
,
1176 xstrndup(value
, wordlen
));
1177 value
+= wordlen
+ (value
[wordlen
] != '\0');
1184 static int add_remote_or_group(const char *name
, struct string_list
*list
)
1186 int prev_nr
= list
->nr
;
1187 struct remote_group_data g
;
1188 g
.name
= name
; g
.list
= list
;
1190 git_config(get_remote_group
, &g
);
1191 if (list
->nr
== prev_nr
) {
1192 struct remote
*remote
= remote_get(name
);
1193 if (!remote_is_configured(remote
, 0))
1195 string_list_append(list
, remote
->name
);
1200 static void add_options_to_argv(struct argv_array
*argv
)
1203 argv_array_push(argv
, "--dry-run");
1205 argv_array_push(argv
, prune
? "--prune" : "--no-prune");
1207 argv_array_push(argv
, "--update-head-ok");
1209 argv_array_push(argv
, "--force");
1211 argv_array_push(argv
, "--keep");
1212 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
1213 argv_array_push(argv
, "--recurse-submodules");
1214 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
1215 argv_array_push(argv
, "--recurse-submodules=on-demand");
1216 if (tags
== TAGS_SET
)
1217 argv_array_push(argv
, "--tags");
1218 else if (tags
== TAGS_UNSET
)
1219 argv_array_push(argv
, "--no-tags");
1221 argv_array_push(argv
, "-v");
1223 argv_array_push(argv
, "-v");
1224 else if (verbosity
< 0)
1225 argv_array_push(argv
, "-q");
1229 static int fetch_multiple(struct string_list
*list
)
1232 struct argv_array argv
= ARGV_ARRAY_INIT
;
1234 if (!append
&& !dry_run
) {
1235 int errcode
= truncate_fetch_head();
1240 argv_array_pushl(&argv
, "fetch", "--append", NULL
);
1241 add_options_to_argv(&argv
);
1243 for (i
= 0; i
< list
->nr
; i
++) {
1244 const char *name
= list
->items
[i
].string
;
1245 argv_array_push(&argv
, name
);
1247 printf(_("Fetching %s\n"), name
);
1248 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
1249 error(_("Could not fetch %s"), name
);
1252 argv_array_pop(&argv
);
1255 argv_array_clear(&argv
);
1259 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
1261 static const char **refs
= NULL
;
1262 struct refspec
*refspec
;
1267 die(_("No remote repository specified. Please, specify either a URL or a\n"
1268 "remote name from which new revisions should be fetched."));
1270 gtransport
= prepare_transport(remote
, 1);
1273 /* no command line request */
1274 if (0 <= gtransport
->remote
->prune
)
1275 prune
= gtransport
->remote
->prune
;
1276 else if (0 <= fetch_prune_config
)
1277 prune
= fetch_prune_config
;
1279 prune
= PRUNE_BY_DEFAULT
;
1285 refs
= xcalloc(st_add(argc
, 1), sizeof(const char *));
1286 for (i
= 0; i
< argc
; i
++) {
1287 if (!strcmp(argv
[i
], "tag")) {
1290 die(_("You need to specify a tag name."));
1291 refs
[j
++] = xstrfmt("refs/tags/%s:refs/tags/%s",
1294 refs
[j
++] = argv
[i
];
1300 sigchain_push_common(unlock_pack_on_signal
);
1301 atexit(unlock_pack
);
1302 refspec
= parse_fetch_refspec(ref_nr
, refs
);
1303 exit_code
= do_fetch(gtransport
, refspec
, ref_nr
);
1304 free_refspec(ref_nr
, refspec
);
1305 transport_disconnect(gtransport
);
1310 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
1313 struct string_list list
= STRING_LIST_INIT_DUP
;
1314 struct remote
*remote
;
1316 struct argv_array argv_gc_auto
= ARGV_ARRAY_INIT
;
1318 packet_trace_identity("fetch");
1320 /* Record the command line for the reflog */
1321 strbuf_addstr(&default_rla
, "fetch");
1322 for (i
= 1; i
< argc
; i
++)
1323 strbuf_addf(&default_rla
, " %s", argv
[i
]);
1325 git_config(git_fetch_config
, NULL
);
1327 argc
= parse_options(argc
, argv
, prefix
,
1328 builtin_fetch_options
, builtin_fetch_usage
, 0);
1330 if (deepen_relative
) {
1331 if (deepen_relative
< 0)
1332 die(_("Negative depth in --deepen is not supported"));
1334 die(_("--deepen and --depth are mutually exclusive"));
1335 depth
= xstrfmt("%d", deepen_relative
);
1339 die(_("--depth and --unshallow cannot be used together"));
1340 else if (!is_repository_shallow())
1341 die(_("--unshallow on a complete repository does not make sense"));
1343 depth
= xstrfmt("%d", INFINITE_DEPTH
);
1346 /* no need to be strict, transport_set_option() will validate it again */
1347 if (depth
&& atoi(depth
) < 1)
1348 die(_("depth %s is not a positive number"), depth
);
1349 if (depth
|| deepen_since
|| deepen_not
.nr
)
1352 if (recurse_submodules
!= RECURSE_SUBMODULES_OFF
) {
1353 if (recurse_submodules_default
) {
1354 int arg
= parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default
);
1355 set_config_fetch_recurse_submodules(arg
);
1357 gitmodules_config();
1358 git_config(submodule_config
, NULL
);
1363 die(_("fetch --all does not take a repository argument"));
1365 die(_("fetch --all does not make sense with refspecs"));
1366 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1367 result
= fetch_multiple(&list
);
1368 } else if (argc
== 0) {
1369 /* No arguments -- use default remote */
1370 remote
= remote_get(NULL
);
1371 result
= fetch_one(remote
, argc
, argv
);
1372 } else if (multiple
) {
1373 /* All arguments are assumed to be remotes or groups */
1374 for (i
= 0; i
< argc
; i
++)
1375 if (!add_remote_or_group(argv
[i
], &list
))
1376 die(_("No such remote or remote group: %s"), argv
[i
]);
1377 result
= fetch_multiple(&list
);
1379 /* Single remote or group */
1380 (void) add_remote_or_group(argv
[0], &list
);
1382 /* More than one remote */
1384 die(_("Fetching a group and specifying refspecs does not make sense"));
1385 result
= fetch_multiple(&list
);
1387 /* Zero or one remotes */
1388 remote
= remote_get(argv
[0]);
1389 result
= fetch_one(remote
, argc
-1, argv
+1);
1393 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1394 struct argv_array options
= ARGV_ARRAY_INIT
;
1396 add_options_to_argv(&options
);
1397 result
= fetch_populated_submodules(&options
,
1402 argv_array_clear(&options
);
1405 string_list_clear(&list
, 0);
1409 argv_array_pushl(&argv_gc_auto
, "gc", "--auto", NULL
);
1411 argv_array_push(&argv_gc_auto
, "--quiet");
1412 run_command_v_opt(argv_gc_auto
.argv
, RUN_GIT_CMD
);
1413 argv_array_clear(&argv_gc_auto
);