8 #include "string-list.h"
10 #include "transport.h"
11 #include "run-command.h"
12 #include "parse-options.h"
14 #include "transport.h"
15 #include "submodule.h"
16 #include "connected.h"
17 #include "argv-array.h"
19 static const char * const builtin_fetch_usage
[] = {
20 N_("git fetch [<options>] [<repository> [<refspec>...]]"),
21 N_("git fetch [<options>] <group>"),
22 N_("git fetch --multiple [<options>] [(<repository> | <group>)...]"),
23 N_("git fetch --all [<options>]"),
33 static int all
, append
, dry_run
, force
, keep
, multiple
, prune
, update_head_ok
, verbosity
;
34 static int progress
= -1, recurse_submodules
= RECURSE_SUBMODULES_DEFAULT
;
35 static int tags
= TAGS_DEFAULT
, unshallow
;
36 static const char *depth
;
37 static const char *upload_pack
;
38 static struct strbuf default_rla
= STRBUF_INIT
;
39 static struct transport
*gtransport
;
40 static struct transport
*gsecondary
;
41 static const char *submodule_prefix
= "";
42 static const char *recurse_submodules_default
;
44 static int option_parse_recurse_submodules(const struct option
*opt
,
45 const char *arg
, int unset
)
48 recurse_submodules
= RECURSE_SUBMODULES_OFF
;
51 recurse_submodules
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
53 recurse_submodules
= RECURSE_SUBMODULES_ON
;
58 static struct option builtin_fetch_options
[] = {
59 OPT__VERBOSITY(&verbosity
),
60 OPT_BOOLEAN(0, "all", &all
,
61 N_("fetch from all remotes")),
62 OPT_BOOLEAN('a', "append", &append
,
63 N_("append to .git/FETCH_HEAD instead of overwriting")),
64 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
65 N_("path to upload pack on remote end")),
66 OPT__FORCE(&force
, N_("force overwrite of local branch")),
67 OPT_BOOLEAN('m', "multiple", &multiple
,
68 N_("fetch from multiple remotes")),
69 OPT_SET_INT('t', "tags", &tags
,
70 N_("fetch all tags and associated objects"), TAGS_SET
),
71 OPT_SET_INT('n', NULL
, &tags
,
72 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
73 OPT_BOOLEAN('p', "prune", &prune
,
74 N_("prune remote-tracking branches no longer on remote")),
75 { OPTION_CALLBACK
, 0, "recurse-submodules", NULL
, N_("on-demand"),
76 N_("control recursive fetching of submodules"),
77 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
78 OPT_BOOLEAN(0, "dry-run", &dry_run
,
80 OPT_BOOLEAN('k', "keep", &keep
, N_("keep downloaded pack")),
81 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok
,
82 N_("allow updating of HEAD ref")),
83 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
84 OPT_STRING(0, "depth", &depth
, N_("depth"),
85 N_("deepen history of shallow clone")),
86 { OPTION_SET_INT
, 0, "unshallow", &unshallow
, NULL
,
87 N_("convert to a complete repository"),
88 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, NULL
, 1 },
89 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
90 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
91 { OPTION_STRING
, 0, "recurse-submodules-default",
92 &recurse_submodules_default
, NULL
,
93 N_("default mode for recursion"), PARSE_OPT_HIDDEN
},
97 static void unlock_pack(void)
100 transport_unlock_pack(gtransport
);
102 transport_unlock_pack(gsecondary
);
105 static void unlock_pack_on_signal(int signo
)
112 static void add_merge_config(struct ref
**head
,
113 const struct ref
*remote_refs
,
114 struct branch
*branch
,
119 for (i
= 0; i
< branch
->merge_nr
; i
++) {
120 struct ref
*rm
, **old_tail
= *tail
;
121 struct refspec refspec
;
123 for (rm
= *head
; rm
; rm
= rm
->next
) {
124 if (branch_merge_matches(branch
, i
, rm
->name
)) {
125 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
133 * Not fetched to a remote-tracking branch? We need to fetch
134 * it anyway to allow this branch's "branch.$name.merge"
135 * to be honored by 'git pull', but we do not have to
136 * fail if branch.$name.merge is misconfigured to point
137 * at a nonexisting branch. If we were indeed called by
138 * 'git pull', it will notice the misconfiguration because
139 * there is no entry in the resulting FETCH_HEAD marked
142 memset(&refspec
, 0, sizeof(refspec
));
143 refspec
.src
= branch
->merge
[i
]->src
;
144 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
145 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
146 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
150 static void find_non_local_tags(struct transport
*transport
,
154 static struct ref
*get_ref_map(struct transport
*transport
,
155 struct refspec
*refs
, int ref_count
, int tags
,
160 struct ref
*ref_map
= NULL
;
161 struct ref
**tail
= &ref_map
;
163 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
165 if (ref_count
|| tags
== TAGS_SET
) {
166 struct ref
**old_tail
;
168 for (i
= 0; i
< ref_count
; i
++) {
169 get_fetch_map(remote_refs
, &refs
[i
], &tail
, 0);
170 if (refs
[i
].dst
&& refs
[i
].dst
[0])
173 /* Merge everything on the command line, but not --tags */
174 for (rm
= ref_map
; rm
; rm
= rm
->next
)
175 rm
->fetch_head_status
= FETCH_HEAD_MERGE
;
176 if (tags
== TAGS_SET
)
177 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
180 * For any refs that we happen to be fetching via command-line
181 * arguments, take the opportunity to update their configured
182 * counterparts. However, we do not want to mention these
183 * entries in FETCH_HEAD at all, as they would simply be
184 * duplicates of existing entries.
187 for (i
= 0; i
< transport
->remote
->fetch_refspec_nr
; i
++)
188 get_fetch_map(ref_map
, &transport
->remote
->fetch
[i
],
190 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
191 rm
->fetch_head_status
= FETCH_HEAD_IGNORE
;
193 /* Use the defaults */
194 struct remote
*remote
= transport
->remote
;
195 struct branch
*branch
= branch_get(NULL
);
196 int has_merge
= branch_has_merge_config(branch
);
198 (remote
->fetch_refspec_nr
||
199 /* Note: has_merge implies non-NULL branch->remote_name */
200 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
201 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
202 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
203 if (remote
->fetch
[i
].dst
&&
204 remote
->fetch
[i
].dst
[0])
206 if (!i
&& !has_merge
&& ref_map
&&
207 !remote
->fetch
[0].pattern
)
208 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
211 * if the remote we're fetching from is the same
212 * as given in branch.<name>.remote, we add the
213 * ref given in branch.<name>.merge, too.
215 * Note: has_merge implies non-NULL branch->remote_name
218 !strcmp(branch
->remote_name
, remote
->name
))
219 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
221 ref_map
= get_remote_ref(remote_refs
, "HEAD");
223 die(_("Couldn't find remote ref HEAD"));
224 ref_map
->fetch_head_status
= FETCH_HEAD_MERGE
;
225 tail
= &ref_map
->next
;
228 if (tags
== TAGS_DEFAULT
&& *autotags
)
229 find_non_local_tags(transport
, &ref_map
, &tail
);
230 ref_remove_duplicates(ref_map
);
235 #define STORE_REF_ERROR_OTHER 1
236 #define STORE_REF_ERROR_DF_CONFLICT 2
238 static int s_update_ref(const char *action
,
243 char *rla
= getenv("GIT_REFLOG_ACTION");
244 static struct ref_lock
*lock
;
249 rla
= default_rla
.buf
;
250 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
251 lock
= lock_any_ref_for_update(ref
->name
,
252 check_old
? ref
->old_sha1
: NULL
, 0);
254 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
255 STORE_REF_ERROR_OTHER
;
256 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
257 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
258 STORE_REF_ERROR_OTHER
;
262 #define REFCOL_WIDTH 10
264 static int update_local_ref(struct ref
*ref
,
266 const struct ref
*remote_ref
,
267 struct strbuf
*display
)
269 struct commit
*current
= NULL
, *updated
;
270 enum object_type type
;
271 struct branch
*current_branch
= branch_get(NULL
);
272 const char *pretty_ref
= prettify_refname(ref
->name
);
274 type
= sha1_object_info(ref
->new_sha1
, NULL
);
276 die(_("object %s not found"), sha1_to_hex(ref
->new_sha1
));
278 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
280 strbuf_addf(display
, "= %-*s %-*s -> %s",
281 TRANSPORT_SUMMARY(_("[up to date]")),
282 REFCOL_WIDTH
, remote
, pretty_ref
);
286 if (current_branch
&&
287 !strcmp(ref
->name
, current_branch
->name
) &&
288 !(update_head_ok
|| is_bare_repository()) &&
289 !is_null_sha1(ref
->old_sha1
)) {
291 * If this is the head, and it's not okay to update
292 * the head, and the old value of the head isn't empty...
295 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
296 TRANSPORT_SUMMARY(_("[rejected]")),
297 REFCOL_WIDTH
, remote
, pretty_ref
);
301 if (!is_null_sha1(ref
->old_sha1
) &&
302 !prefixcmp(ref
->name
, "refs/tags/")) {
304 r
= s_update_ref("updating tag", ref
, 0);
305 strbuf_addf(display
, "%c %-*s %-*s -> %s%s",
307 TRANSPORT_SUMMARY(_("[tag update]")),
308 REFCOL_WIDTH
, remote
, pretty_ref
,
309 r
? _(" (unable to update local ref)") : "");
313 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
314 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
315 if (!current
|| !updated
) {
320 * Nicely describe the new ref we're fetching.
321 * Base this on the remote's ref name, as it's
322 * more likely to follow a standard layout.
324 const char *name
= remote_ref
? remote_ref
->name
: "";
325 if (!prefixcmp(name
, "refs/tags/")) {
327 what
= _("[new tag]");
328 } else if (!prefixcmp(name
, "refs/heads/")) {
329 msg
= "storing head";
330 what
= _("[new branch]");
333 what
= _("[new ref]");
336 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
337 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
338 check_for_new_submodule_commits(ref
->new_sha1
);
339 r
= s_update_ref(msg
, ref
, 0);
340 strbuf_addf(display
, "%c %-*s %-*s -> %s%s",
342 TRANSPORT_SUMMARY(what
),
343 REFCOL_WIDTH
, remote
, pretty_ref
,
344 r
? _(" (unable to update local ref)") : "");
348 if (in_merge_bases(current
, updated
)) {
351 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
352 strcat(quickref
, "..");
353 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
354 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
355 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
356 check_for_new_submodule_commits(ref
->new_sha1
);
357 r
= s_update_ref("fast-forward", ref
, 1);
358 strbuf_addf(display
, "%c %-*s %-*s -> %s%s",
360 TRANSPORT_SUMMARY_WIDTH
, quickref
,
361 REFCOL_WIDTH
, remote
, pretty_ref
,
362 r
? _(" (unable to update local ref)") : "");
364 } else if (force
|| ref
->force
) {
367 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
368 strcat(quickref
, "...");
369 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
370 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
371 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
372 check_for_new_submodule_commits(ref
->new_sha1
);
373 r
= s_update_ref("forced-update", ref
, 1);
374 strbuf_addf(display
, "%c %-*s %-*s -> %s (%s)",
376 TRANSPORT_SUMMARY_WIDTH
, quickref
,
377 REFCOL_WIDTH
, remote
, pretty_ref
,
378 r
? _("unable to update local ref") : _("forced update"));
381 strbuf_addf(display
, "! %-*s %-*s -> %s %s",
382 TRANSPORT_SUMMARY(_("[rejected]")),
383 REFCOL_WIDTH
, remote
, pretty_ref
,
384 _("(non-fast-forward)"));
389 static int iterate_ref_map(void *cb_data
, unsigned char sha1
[20])
391 struct ref
**rm
= cb_data
;
392 struct ref
*ref
= *rm
;
395 return -1; /* end of the list */
397 hashcpy(sha1
, ref
->old_sha1
);
401 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
405 struct commit
*commit
;
406 int url_len
, i
, shown_url
= 0, rc
= 0;
407 struct strbuf note
= STRBUF_INIT
;
408 const char *what
, *kind
;
410 char *url
, *filename
= dry_run
? "/dev/null" : git_path("FETCH_HEAD");
413 fp
= fopen(filename
, "a");
415 return error(_("cannot open %s: %s\n"), filename
, strerror(errno
));
418 url
= transport_anonymize_url(raw_url
);
420 url
= xstrdup("foreign");
423 if (check_everything_connected(iterate_ref_map
, 0, &rm
)) {
424 rc
= error(_("%s did not send all necessary objects\n"), url
);
429 * We do a pass for each fetch_head_status type in their enum order, so
430 * merged entries are written before not-for-merge. That lets readers
431 * use FETCH_HEAD as a refname to refer to the ref to be merged.
433 for (want_status
= FETCH_HEAD_MERGE
;
434 want_status
<= FETCH_HEAD_IGNORE
;
436 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
437 struct ref
*ref
= NULL
;
438 const char *merge_status_marker
= "";
440 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
442 rm
->fetch_head_status
= FETCH_HEAD_NOT_FOR_MERGE
;
444 if (rm
->fetch_head_status
!= want_status
)
448 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
449 strcpy(ref
->name
, rm
->peer_ref
->name
);
450 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
451 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
452 ref
->force
= rm
->peer_ref
->force
;
456 if (!strcmp(rm
->name
, "HEAD")) {
460 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
462 what
= rm
->name
+ 11;
464 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
466 what
= rm
->name
+ 10;
468 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
469 kind
= "remote-tracking branch";
470 what
= rm
->name
+ 13;
477 url_len
= strlen(url
);
478 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
481 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
487 strbuf_addf(¬e
, "%s ", kind
);
488 strbuf_addf(¬e
, "'%s' of ", what
);
490 switch (rm
->fetch_head_status
) {
491 case FETCH_HEAD_NOT_FOR_MERGE
:
492 merge_status_marker
= "not-for-merge";
494 case FETCH_HEAD_MERGE
:
495 fprintf(fp
, "%s\t%s\t%s",
496 sha1_to_hex(rm
->old_sha1
),
499 for (i
= 0; i
< url_len
; ++i
)
507 /* do not write anything to FETCH_HEAD */
513 rc
|= update_local_ref(ref
, what
, rm
, ¬e
);
516 strbuf_addf(¬e
, "* %-*s %-*s -> FETCH_HEAD",
517 TRANSPORT_SUMMARY_WIDTH
,
518 *kind
? kind
: "branch",
520 *what
? what
: "HEAD");
522 if (verbosity
>= 0 && !shown_url
) {
523 fprintf(stderr
, _("From %.*s\n"),
528 fprintf(stderr
, " %s\n", note
.buf
);
533 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
534 error(_("some local refs could not be updated; try running\n"
535 " 'git remote prune %s' to remove any old, conflicting "
536 "branches"), remote_name
);
539 strbuf_release(¬e
);
546 * We would want to bypass the object transfer altogether if
547 * everything we are going to fetch already exists and is connected
550 static int quickfetch(struct ref
*ref_map
)
552 struct ref
*rm
= ref_map
;
555 * If we are deepening a shallow clone we already have these
556 * objects reachable. Running rev-list here will return with
557 * a good (0) exit status and we'll bypass the fetch that we
558 * really need to perform. Claiming failure now will ensure
559 * we perform the network exchange to deepen our history.
563 return check_everything_connected(iterate_ref_map
, 1, &rm
);
566 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
568 int ret
= quickfetch(ref_map
);
570 ret
= transport_fetch_refs(transport
, ref_map
);
572 ret
|= store_updated_refs(transport
->url
,
573 transport
->remote
->name
,
575 transport_unlock_pack(transport
);
579 static int prune_refs(struct refspec
*refs
, int ref_count
, struct ref
*ref_map
)
582 struct ref
*ref
, *stale_refs
= get_stale_heads(refs
, ref_count
, ref_map
);
583 const char *dangling_msg
= dry_run
584 ? _(" (%s will become dangling)")
585 : _(" (%s has become dangling)");
587 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
589 result
|= delete_ref(ref
->name
, NULL
, 0);
590 if (verbosity
>= 0) {
591 fprintf(stderr
, " x %-*s %-*s -> %s\n",
592 TRANSPORT_SUMMARY(_("[deleted]")),
593 REFCOL_WIDTH
, _("(none)"), prettify_refname(ref
->name
));
594 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
597 free_refs(stale_refs
);
601 static int add_existing(const char *refname
, const unsigned char *sha1
,
602 int flag
, void *cbdata
)
604 struct string_list
*list
= (struct string_list
*)cbdata
;
605 struct string_list_item
*item
= string_list_insert(list
, refname
);
606 item
->util
= xmalloc(20);
607 hashcpy(item
->util
, sha1
);
611 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
613 struct ref
*rm
= *head
;
615 if (!hashcmp(rm
->old_sha1
, sha1
))
622 static void find_non_local_tags(struct transport
*transport
,
626 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
627 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
628 const struct ref
*ref
;
629 struct string_list_item
*item
= NULL
;
631 for_each_ref(add_existing
, &existing_refs
);
632 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
633 if (prefixcmp(ref
->name
, "refs/tags/"))
637 * The peeled ref always follows the matching base
638 * ref, so if we see a peeled ref that we don't want
639 * to fetch then we can mark the ref entry in the list
640 * as one to ignore by setting util to NULL.
642 if (!suffixcmp(ref
->name
, "^{}")) {
643 if (item
&& !has_sha1_file(ref
->old_sha1
) &&
644 !will_fetch(head
, ref
->old_sha1
) &&
645 !has_sha1_file(item
->util
) &&
646 !will_fetch(head
, item
->util
))
653 * If item is non-NULL here, then we previously saw a
654 * ref not followed by a peeled reference, so we need
655 * to check if it is a lightweight tag that we want to
658 if (item
&& !has_sha1_file(item
->util
) &&
659 !will_fetch(head
, item
->util
))
664 /* skip duplicates and refs that we already have */
665 if (string_list_has_string(&remote_refs
, ref
->name
) ||
666 string_list_has_string(&existing_refs
, ref
->name
))
669 item
= string_list_insert(&remote_refs
, ref
->name
);
670 item
->util
= (void *)ref
->old_sha1
;
672 string_list_clear(&existing_refs
, 1);
675 * We may have a final lightweight tag that needs to be
676 * checked to see if it needs fetching.
678 if (item
&& !has_sha1_file(item
->util
) &&
679 !will_fetch(head
, item
->util
))
683 * For all the tags in the remote_refs string list,
684 * add them to the list of refs to be fetched
686 for_each_string_list_item(item
, &remote_refs
) {
687 /* Unless we have already decided to ignore this item... */
690 struct ref
*rm
= alloc_ref(item
->string
);
691 rm
->peer_ref
= alloc_ref(item
->string
);
692 hashcpy(rm
->old_sha1
, item
->util
);
698 string_list_clear(&remote_refs
, 0);
701 static void check_not_current_branch(struct ref
*ref_map
)
703 struct branch
*current_branch
= branch_get(NULL
);
705 if (is_bare_repository() || !current_branch
)
708 for (; ref_map
; ref_map
= ref_map
->next
)
709 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
710 ref_map
->peer_ref
->name
))
711 die(_("Refusing to fetch into current branch %s "
712 "of non-bare repository"), current_branch
->refname
);
715 static int truncate_fetch_head(void)
717 char *filename
= git_path("FETCH_HEAD");
718 FILE *fp
= fopen(filename
, "w");
721 return error(_("cannot open %s: %s\n"), filename
, strerror(errno
));
726 static void set_option(struct transport
*transport
, const char *name
, const char *value
)
728 int r
= transport_set_option(transport
, name
, value
);
730 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
731 name
, value
, transport
->url
);
733 warning(_("Option \"%s\" is ignored for %s\n"),
734 name
, transport
->url
);
737 static struct transport
*prepare_transport(struct remote
*remote
)
739 struct transport
*transport
;
740 transport
= transport_get(remote
, NULL
);
741 transport_set_verbosity(transport
, verbosity
, progress
);
743 set_option(transport
, TRANS_OPT_UPLOADPACK
, upload_pack
);
745 set_option(transport
, TRANS_OPT_KEEP
, "yes");
747 set_option(transport
, TRANS_OPT_DEPTH
, depth
);
751 static void backfill_tags(struct transport
*transport
, struct ref
*ref_map
)
753 if (transport
->cannot_reuse
) {
754 gsecondary
= prepare_transport(transport
->remote
);
755 transport
= gsecondary
;
758 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
759 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
760 fetch_refs(transport
, ref_map
);
763 transport_disconnect(gsecondary
);
768 static int do_fetch(struct transport
*transport
,
769 struct refspec
*refs
, int ref_count
)
771 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
774 int autotags
= (transport
->remote
->fetch_tags
== 1);
777 for_each_ref(add_existing
, &existing_refs
);
779 if (tags
== TAGS_DEFAULT
) {
780 if (transport
->remote
->fetch_tags
== 2)
782 if (transport
->remote
->fetch_tags
== -1)
786 if (!transport
->get_refs_list
|| !transport
->fetch
)
787 die(_("Don't know how to fetch from %s"), transport
->url
);
789 /* if not appending, truncate FETCH_HEAD */
790 if (!append
&& !dry_run
) {
791 retcode
= truncate_fetch_head();
796 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
798 check_not_current_branch(ref_map
);
800 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
802 struct string_list_item
*peer_item
=
803 string_list_lookup(&existing_refs
,
806 hashcpy(rm
->peer_ref
->old_sha1
,
811 if (tags
== TAGS_DEFAULT
&& autotags
)
812 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
813 if (fetch_refs(transport
, ref_map
)) {
819 /* If --tags was specified, pretend the user gave us the canonical tags refspec */
820 if (tags
== TAGS_SET
) {
821 const char *tags_str
= "refs/tags/*:refs/tags/*";
822 struct refspec
*tags_refspec
, *refspec
;
824 /* Copy the refspec and add the tags to it */
825 refspec
= xcalloc(ref_count
+ 1, sizeof(struct refspec
));
826 tags_refspec
= parse_fetch_refspec(1, &tags_str
);
827 memcpy(refspec
, refs
, ref_count
* sizeof(struct refspec
));
828 memcpy(&refspec
[ref_count
], tags_refspec
, sizeof(struct refspec
));
831 prune_refs(refspec
, ref_count
, ref_map
);
834 /* The rest of the strings belong to fetch_one */
835 free_refspec(1, tags_refspec
);
837 } else if (ref_count
) {
838 prune_refs(refs
, ref_count
, ref_map
);
840 prune_refs(transport
->remote
->fetch
, transport
->remote
->fetch_refspec_nr
, ref_map
);
845 /* if neither --no-tags nor --tags was specified, do automated tag
847 if (tags
== TAGS_DEFAULT
&& autotags
) {
848 struct ref
**tail
= &ref_map
;
850 find_non_local_tags(transport
, &ref_map
, &tail
);
852 backfill_tags(transport
, ref_map
);
857 string_list_clear(&existing_refs
, 1);
861 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
863 struct string_list
*list
= priv
;
864 if (!remote
->skip_default_update
)
865 string_list_append(list
, remote
->name
);
869 struct remote_group_data
{
871 struct string_list
*list
;
874 static int get_remote_group(const char *key
, const char *value
, void *priv
)
876 struct remote_group_data
*g
= priv
;
878 if (!prefixcmp(key
, "remotes.") &&
879 !strcmp(key
+ 8, g
->name
)) {
880 /* split list by white space */
881 int space
= strcspn(value
, " \t\n");
884 string_list_append(g
->list
,
885 xstrndup(value
, space
));
887 value
+= space
+ (value
[space
] != '\0');
888 space
= strcspn(value
, " \t\n");
895 static int add_remote_or_group(const char *name
, struct string_list
*list
)
897 int prev_nr
= list
->nr
;
898 struct remote_group_data g
;
899 g
.name
= name
; g
.list
= list
;
901 git_config(get_remote_group
, &g
);
902 if (list
->nr
== prev_nr
) {
903 struct remote
*remote
;
904 if (!remote_is_configured(name
))
906 remote
= remote_get(name
);
907 string_list_append(list
, remote
->name
);
912 static void add_options_to_argv(struct argv_array
*argv
)
915 argv_array_push(argv
, "--dry-run");
917 argv_array_push(argv
, "--prune");
919 argv_array_push(argv
, "--update-head-ok");
921 argv_array_push(argv
, "--force");
923 argv_array_push(argv
, "--keep");
924 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
925 argv_array_push(argv
, "--recurse-submodules");
926 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
927 argv_array_push(argv
, "--recurse-submodules=on-demand");
928 if (tags
== TAGS_SET
)
929 argv_array_push(argv
, "--tags");
930 else if (tags
== TAGS_UNSET
)
931 argv_array_push(argv
, "--no-tags");
933 argv_array_push(argv
, "-v");
935 argv_array_push(argv
, "-v");
936 else if (verbosity
< 0)
937 argv_array_push(argv
, "-q");
941 static int fetch_multiple(struct string_list
*list
)
944 struct argv_array argv
= ARGV_ARRAY_INIT
;
946 if (!append
&& !dry_run
) {
947 int errcode
= truncate_fetch_head();
952 argv_array_pushl(&argv
, "fetch", "--append", NULL
);
953 add_options_to_argv(&argv
);
955 for (i
= 0; i
< list
->nr
; i
++) {
956 const char *name
= list
->items
[i
].string
;
957 argv_array_push(&argv
, name
);
959 printf(_("Fetching %s\n"), name
);
960 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
961 error(_("Could not fetch %s"), name
);
964 argv_array_pop(&argv
);
967 argv_array_clear(&argv
);
971 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
974 static const char **refs
= NULL
;
975 struct refspec
*refspec
;
980 die(_("No remote repository specified. Please, specify either a URL or a\n"
981 "remote name from which new revisions should be fetched."));
983 gtransport
= prepare_transport(remote
);
986 refs
= xcalloc(argc
+ 1, sizeof(const char *));
987 for (i
= 0; i
< argc
; i
++) {
988 if (!strcmp(argv
[i
], "tag")) {
992 die(_("You need to specify a tag name."));
993 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
994 strcpy(ref
, "refs/tags/");
995 strcat(ref
, argv
[i
]);
996 strcat(ref
, ":refs/tags/");
997 strcat(ref
, argv
[i
]);
1000 refs
[j
++] = argv
[i
];
1006 sigchain_push_common(unlock_pack_on_signal
);
1007 atexit(unlock_pack
);
1008 refspec
= parse_fetch_refspec(ref_nr
, refs
);
1009 exit_code
= do_fetch(gtransport
, refspec
, ref_nr
);
1010 free_refspec(ref_nr
, refspec
);
1011 transport_disconnect(gtransport
);
1016 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
1019 struct string_list list
= STRING_LIST_INIT_NODUP
;
1020 struct remote
*remote
;
1022 static const char *argv_gc_auto
[] = {
1023 "gc", "--auto", NULL
,
1026 packet_trace_identity("fetch");
1028 /* Record the command line for the reflog */
1029 strbuf_addstr(&default_rla
, "fetch");
1030 for (i
= 1; i
< argc
; i
++)
1031 strbuf_addf(&default_rla
, " %s", argv
[i
]);
1033 argc
= parse_options(argc
, argv
, prefix
,
1034 builtin_fetch_options
, builtin_fetch_usage
, 0);
1038 die(_("--depth and --unshallow cannot be used together"));
1039 else if (!is_repository_shallow())
1040 die(_("--unshallow on a complete repository does not make sense"));
1042 static char inf_depth
[12];
1043 sprintf(inf_depth
, "%d", INFINITE_DEPTH
);
1048 if (recurse_submodules
!= RECURSE_SUBMODULES_OFF
) {
1049 if (recurse_submodules_default
) {
1050 int arg
= parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default
);
1051 set_config_fetch_recurse_submodules(arg
);
1053 gitmodules_config();
1054 git_config(submodule_config
, NULL
);
1059 die(_("fetch --all does not take a repository argument"));
1061 die(_("fetch --all does not make sense with refspecs"));
1062 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1063 result
= fetch_multiple(&list
);
1064 } else if (argc
== 0) {
1065 /* No arguments -- use default remote */
1066 remote
= remote_get(NULL
);
1067 result
= fetch_one(remote
, argc
, argv
);
1068 } else if (multiple
) {
1069 /* All arguments are assumed to be remotes or groups */
1070 for (i
= 0; i
< argc
; i
++)
1071 if (!add_remote_or_group(argv
[i
], &list
))
1072 die(_("No such remote or remote group: %s"), argv
[i
]);
1073 result
= fetch_multiple(&list
);
1075 /* Single remote or group */
1076 (void) add_remote_or_group(argv
[0], &list
);
1078 /* More than one remote */
1080 die(_("Fetching a group and specifying refspecs does not make sense"));
1081 result
= fetch_multiple(&list
);
1083 /* Zero or one remotes */
1084 remote
= remote_get(argv
[0]);
1085 result
= fetch_one(remote
, argc
-1, argv
+1);
1089 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1090 struct argv_array options
= ARGV_ARRAY_INIT
;
1092 add_options_to_argv(&options
);
1093 result
= fetch_populated_submodules(&options
,
1097 argv_array_clear(&options
);
1100 /* All names were strdup()ed or strndup()ed */
1101 list
.strdup_strings
= 1;
1102 string_list_clear(&list
, 0);
1104 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);