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
*transport
;
40 static const char *submodule_prefix
= "";
41 static const char *recurse_submodules_default
;
43 static int option_parse_recurse_submodules(const struct option
*opt
,
44 const char *arg
, int unset
)
47 recurse_submodules
= RECURSE_SUBMODULES_OFF
;
50 recurse_submodules
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
52 recurse_submodules
= RECURSE_SUBMODULES_ON
;
57 static struct option builtin_fetch_options
[] = {
58 OPT__VERBOSITY(&verbosity
),
59 OPT_BOOLEAN(0, "all", &all
,
60 N_("fetch from all remotes")),
61 OPT_BOOLEAN('a', "append", &append
,
62 N_("append to .git/FETCH_HEAD instead of overwriting")),
63 OPT_STRING(0, "upload-pack", &upload_pack
, N_("path"),
64 N_("path to upload pack on remote end")),
65 OPT__FORCE(&force
, N_("force overwrite of local branch")),
66 OPT_BOOLEAN('m', "multiple", &multiple
,
67 N_("fetch from multiple remotes")),
68 OPT_SET_INT('t', "tags", &tags
,
69 N_("fetch all tags and associated objects"), TAGS_SET
),
70 OPT_SET_INT('n', NULL
, &tags
,
71 N_("do not fetch all tags (--no-tags)"), TAGS_UNSET
),
72 OPT_BOOLEAN('p', "prune", &prune
,
73 N_("prune remote-tracking branches no longer on remote")),
74 { OPTION_CALLBACK
, 0, "recurse-submodules", NULL
, N_("on-demand"),
75 N_("control recursive fetching of submodules"),
76 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
77 OPT_BOOLEAN(0, "dry-run", &dry_run
,
79 OPT_BOOLEAN('k', "keep", &keep
, N_("keep downloaded pack")),
80 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok
,
81 N_("allow updating of HEAD ref")),
82 OPT_BOOL(0, "progress", &progress
, N_("force progress reporting")),
83 OPT_STRING(0, "depth", &depth
, N_("depth"),
84 N_("deepen history of shallow clone")),
85 { OPTION_SET_INT
, 0, "unshallow", &unshallow
, NULL
,
86 N_("convert to a complete repository"),
87 PARSE_OPT_NONEG
| PARSE_OPT_NOARG
, NULL
, 1 },
88 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, N_("dir"),
89 N_("prepend this to submodule path output"), PARSE_OPT_HIDDEN
},
90 { OPTION_STRING
, 0, "recurse-submodules-default",
91 &recurse_submodules_default
, NULL
,
92 N_("default mode for recursion"), PARSE_OPT_HIDDEN
},
96 static void unlock_pack(void)
99 transport_unlock_pack(transport
);
102 static void unlock_pack_on_signal(int signo
)
109 static void add_merge_config(struct ref
**head
,
110 const struct ref
*remote_refs
,
111 struct branch
*branch
,
116 for (i
= 0; i
< branch
->merge_nr
; i
++) {
117 struct ref
*rm
, **old_tail
= *tail
;
118 struct refspec refspec
;
120 for (rm
= *head
; rm
; rm
= rm
->next
) {
121 if (branch_merge_matches(branch
, i
, rm
->name
)) {
130 * Not fetched to a remote-tracking branch? We need to fetch
131 * it anyway to allow this branch's "branch.$name.merge"
132 * to be honored by 'git pull', but we do not have to
133 * fail if branch.$name.merge is misconfigured to point
134 * at a nonexisting branch. If we were indeed called by
135 * 'git pull', it will notice the misconfiguration because
136 * there is no entry in the resulting FETCH_HEAD marked
139 memset(&refspec
, 0, sizeof(refspec
));
140 refspec
.src
= branch
->merge
[i
]->src
;
141 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
142 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
147 static void find_non_local_tags(struct transport
*transport
,
151 static struct ref
*get_ref_map(struct transport
*transport
,
152 struct refspec
*refs
, int ref_count
, int tags
,
157 struct ref
*ref_map
= NULL
;
158 struct ref
**tail
= &ref_map
;
160 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
162 if (ref_count
|| tags
== TAGS_SET
) {
163 for (i
= 0; i
< ref_count
; i
++) {
164 get_fetch_map(remote_refs
, &refs
[i
], &tail
, 0);
165 if (refs
[i
].dst
&& refs
[i
].dst
[0])
168 /* Merge everything on the command line, but not --tags */
169 for (rm
= ref_map
; rm
; rm
= rm
->next
)
171 if (tags
== TAGS_SET
)
172 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
174 /* Use the defaults */
175 struct remote
*remote
= transport
->remote
;
176 struct branch
*branch
= branch_get(NULL
);
177 int has_merge
= branch_has_merge_config(branch
);
179 (remote
->fetch_refspec_nr
||
180 /* Note: has_merge implies non-NULL branch->remote_name */
181 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
182 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
183 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
184 if (remote
->fetch
[i
].dst
&&
185 remote
->fetch
[i
].dst
[0])
187 if (!i
&& !has_merge
&& ref_map
&&
188 !remote
->fetch
[0].pattern
)
192 * if the remote we're fetching from is the same
193 * as given in branch.<name>.remote, we add the
194 * ref given in branch.<name>.merge, too.
196 * Note: has_merge implies non-NULL branch->remote_name
199 !strcmp(branch
->remote_name
, remote
->name
))
200 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
202 ref_map
= get_remote_ref(remote_refs
, "HEAD");
204 die(_("Couldn't find remote ref HEAD"));
206 tail
= &ref_map
->next
;
209 if (tags
== TAGS_DEFAULT
&& *autotags
)
210 find_non_local_tags(transport
, &ref_map
, &tail
);
211 ref_remove_duplicates(ref_map
);
216 #define STORE_REF_ERROR_OTHER 1
217 #define STORE_REF_ERROR_DF_CONFLICT 2
219 static int s_update_ref(const char *action
,
224 char *rla
= getenv("GIT_REFLOG_ACTION");
225 static struct ref_lock
*lock
;
230 rla
= default_rla
.buf
;
231 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
232 lock
= lock_any_ref_for_update(ref
->name
,
233 check_old
? ref
->old_sha1
: NULL
, 0);
235 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
236 STORE_REF_ERROR_OTHER
;
237 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
238 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
239 STORE_REF_ERROR_OTHER
;
243 #define REFCOL_WIDTH 10
245 static int update_local_ref(struct ref
*ref
,
247 const struct ref
*remote_ref
,
248 struct strbuf
*display
)
250 struct commit
*current
= NULL
, *updated
;
251 enum object_type type
;
252 struct branch
*current_branch
= branch_get(NULL
);
253 const char *pretty_ref
= prettify_refname(ref
->name
);
255 type
= sha1_object_info(ref
->new_sha1
, NULL
);
257 die(_("object %s not found"), sha1_to_hex(ref
->new_sha1
));
259 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
261 strbuf_addf(display
, "= %-*s %-*s -> %s",
262 TRANSPORT_SUMMARY(_("[up to date]")),
263 REFCOL_WIDTH
, remote
, pretty_ref
);
267 if (current_branch
&&
268 !strcmp(ref
->name
, current_branch
->name
) &&
269 !(update_head_ok
|| is_bare_repository()) &&
270 !is_null_sha1(ref
->old_sha1
)) {
272 * If this is the head, and it's not okay to update
273 * the head, and the old value of the head isn't empty...
276 _("! %-*s %-*s -> %s (can't fetch in current branch)"),
277 TRANSPORT_SUMMARY(_("[rejected]")),
278 REFCOL_WIDTH
, remote
, pretty_ref
);
282 if (!is_null_sha1(ref
->old_sha1
) &&
283 !prefixcmp(ref
->name
, "refs/tags/")) {
285 r
= s_update_ref("updating tag", ref
, 0);
286 strbuf_addf(display
, "%c %-*s %-*s -> %s%s",
288 TRANSPORT_SUMMARY(_("[tag update]")),
289 REFCOL_WIDTH
, remote
, pretty_ref
,
290 r
? _(" (unable to update local ref)") : "");
294 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
295 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
296 if (!current
|| !updated
) {
301 * Nicely describe the new ref we're fetching.
302 * Base this on the remote's ref name, as it's
303 * more likely to follow a standard layout.
305 const char *name
= remote_ref
? remote_ref
->name
: "";
306 if (!prefixcmp(name
, "refs/tags/")) {
308 what
= _("[new tag]");
309 } else if (!prefixcmp(name
, "refs/heads/")) {
310 msg
= "storing head";
311 what
= _("[new branch]");
314 what
= _("[new ref]");
317 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
318 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
319 check_for_new_submodule_commits(ref
->new_sha1
);
320 r
= s_update_ref(msg
, ref
, 0);
321 strbuf_addf(display
, "%c %-*s %-*s -> %s%s",
323 TRANSPORT_SUMMARY(what
),
324 REFCOL_WIDTH
, remote
, pretty_ref
,
325 r
? _(" (unable to update local ref)") : "");
329 if (in_merge_bases(current
, updated
)) {
332 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
333 strcat(quickref
, "..");
334 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
335 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
336 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
337 check_for_new_submodule_commits(ref
->new_sha1
);
338 r
= s_update_ref("fast-forward", ref
, 1);
339 strbuf_addf(display
, "%c %-*s %-*s -> %s%s",
341 TRANSPORT_SUMMARY_WIDTH
, quickref
,
342 REFCOL_WIDTH
, remote
, pretty_ref
,
343 r
? _(" (unable to update local ref)") : "");
345 } else if (force
|| ref
->force
) {
348 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
349 strcat(quickref
, "...");
350 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
351 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
352 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
353 check_for_new_submodule_commits(ref
->new_sha1
);
354 r
= s_update_ref("forced-update", ref
, 1);
355 strbuf_addf(display
, "%c %-*s %-*s -> %s (%s)",
357 TRANSPORT_SUMMARY_WIDTH
, quickref
,
358 REFCOL_WIDTH
, remote
, pretty_ref
,
359 r
? _("unable to update local ref") : _("forced update"));
362 strbuf_addf(display
, "! %-*s %-*s -> %s %s",
363 TRANSPORT_SUMMARY(_("[rejected]")),
364 REFCOL_WIDTH
, remote
, pretty_ref
,
365 _("(non-fast-forward)"));
370 static int iterate_ref_map(void *cb_data
, unsigned char sha1
[20])
372 struct ref
**rm
= cb_data
;
373 struct ref
*ref
= *rm
;
376 return -1; /* end of the list */
378 hashcpy(sha1
, ref
->old_sha1
);
382 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
386 struct commit
*commit
;
387 int url_len
, i
, shown_url
= 0, rc
= 0;
388 struct strbuf note
= STRBUF_INIT
;
389 const char *what
, *kind
;
391 char *url
, *filename
= dry_run
? "/dev/null" : git_path("FETCH_HEAD");
394 fp
= fopen(filename
, "a");
396 return error(_("cannot open %s: %s\n"), filename
, strerror(errno
));
399 url
= transport_anonymize_url(raw_url
);
401 url
= xstrdup("foreign");
404 if (check_everything_connected(iterate_ref_map
, 0, &rm
)) {
405 rc
= error(_("%s did not send all necessary objects\n"), url
);
410 * The first pass writes objects to be merged and then the
411 * second pass writes the rest, in order to allow using
412 * FETCH_HEAD as a refname to refer to the ref to be merged.
414 for (want_merge
= 1; 0 <= want_merge
; want_merge
--) {
415 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
416 struct ref
*ref
= NULL
;
418 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
422 if (rm
->merge
!= want_merge
)
426 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
427 strcpy(ref
->name
, rm
->peer_ref
->name
);
428 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
429 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
430 ref
->force
= rm
->peer_ref
->force
;
434 if (!strcmp(rm
->name
, "HEAD")) {
438 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
440 what
= rm
->name
+ 11;
442 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
444 what
= rm
->name
+ 10;
446 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
447 kind
= "remote-tracking branch";
448 what
= rm
->name
+ 13;
455 url_len
= strlen(url
);
456 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
459 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
465 strbuf_addf(¬e
, "%s ", kind
);
466 strbuf_addf(¬e
, "'%s' of ", what
);
468 fprintf(fp
, "%s\t%s\t%s",
469 sha1_to_hex(rm
->old_sha1
),
470 rm
->merge
? "" : "not-for-merge",
472 for (i
= 0; i
< url_len
; ++i
)
481 rc
|= update_local_ref(ref
, what
, rm
, ¬e
);
484 strbuf_addf(¬e
, "* %-*s %-*s -> FETCH_HEAD",
485 TRANSPORT_SUMMARY_WIDTH
,
486 *kind
? kind
: "branch",
488 *what
? what
: "HEAD");
490 if (verbosity
>= 0 && !shown_url
) {
491 fprintf(stderr
, _("From %.*s\n"),
496 fprintf(stderr
, " %s\n", note
.buf
);
501 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
502 error(_("some local refs could not be updated; try running\n"
503 " 'git remote prune %s' to remove any old, conflicting "
504 "branches"), remote_name
);
507 strbuf_release(¬e
);
514 * We would want to bypass the object transfer altogether if
515 * everything we are going to fetch already exists and is connected
518 static int quickfetch(struct ref
*ref_map
)
520 struct ref
*rm
= ref_map
;
523 * If we are deepening a shallow clone we already have these
524 * objects reachable. Running rev-list here will return with
525 * a good (0) exit status and we'll bypass the fetch that we
526 * really need to perform. Claiming failure now will ensure
527 * we perform the network exchange to deepen our history.
531 return check_everything_connected(iterate_ref_map
, 1, &rm
);
534 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
536 int ret
= quickfetch(ref_map
);
538 ret
= transport_fetch_refs(transport
, ref_map
);
540 ret
|= store_updated_refs(transport
->url
,
541 transport
->remote
->name
,
543 transport_unlock_pack(transport
);
547 static int prune_refs(struct refspec
*refs
, int ref_count
, struct ref
*ref_map
)
550 struct ref
*ref
, *stale_refs
= get_stale_heads(refs
, ref_count
, ref_map
);
551 const char *dangling_msg
= dry_run
552 ? _(" (%s will become dangling)")
553 : _(" (%s has become dangling)");
555 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
557 result
|= delete_ref(ref
->name
, NULL
, 0);
558 if (verbosity
>= 0) {
559 fprintf(stderr
, " x %-*s %-*s -> %s\n",
560 TRANSPORT_SUMMARY(_("[deleted]")),
561 REFCOL_WIDTH
, _("(none)"), prettify_refname(ref
->name
));
562 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
565 free_refs(stale_refs
);
569 static int add_existing(const char *refname
, const unsigned char *sha1
,
570 int flag
, void *cbdata
)
572 struct string_list
*list
= (struct string_list
*)cbdata
;
573 struct string_list_item
*item
= string_list_insert(list
, refname
);
574 item
->util
= xmalloc(20);
575 hashcpy(item
->util
, sha1
);
579 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
581 struct ref
*rm
= *head
;
583 if (!hashcmp(rm
->old_sha1
, sha1
))
590 static void find_non_local_tags(struct transport
*transport
,
594 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
595 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
596 const struct ref
*ref
;
597 struct string_list_item
*item
= NULL
;
599 for_each_ref(add_existing
, &existing_refs
);
600 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
601 if (prefixcmp(ref
->name
, "refs/tags/"))
605 * The peeled ref always follows the matching base
606 * ref, so if we see a peeled ref that we don't want
607 * to fetch then we can mark the ref entry in the list
608 * as one to ignore by setting util to NULL.
610 if (!suffixcmp(ref
->name
, "^{}")) {
611 if (item
&& !has_sha1_file(ref
->old_sha1
) &&
612 !will_fetch(head
, ref
->old_sha1
) &&
613 !has_sha1_file(item
->util
) &&
614 !will_fetch(head
, item
->util
))
621 * If item is non-NULL here, then we previously saw a
622 * ref not followed by a peeled reference, so we need
623 * to check if it is a lightweight tag that we want to
626 if (item
&& !has_sha1_file(item
->util
) &&
627 !will_fetch(head
, item
->util
))
632 /* skip duplicates and refs that we already have */
633 if (string_list_has_string(&remote_refs
, ref
->name
) ||
634 string_list_has_string(&existing_refs
, ref
->name
))
637 item
= string_list_insert(&remote_refs
, ref
->name
);
638 item
->util
= (void *)ref
->old_sha1
;
640 string_list_clear(&existing_refs
, 1);
643 * We may have a final lightweight tag that needs to be
644 * checked to see if it needs fetching.
646 if (item
&& !has_sha1_file(item
->util
) &&
647 !will_fetch(head
, item
->util
))
651 * For all the tags in the remote_refs string list,
652 * add them to the list of refs to be fetched
654 for_each_string_list_item(item
, &remote_refs
) {
655 /* Unless we have already decided to ignore this item... */
658 struct ref
*rm
= alloc_ref(item
->string
);
659 rm
->peer_ref
= alloc_ref(item
->string
);
660 hashcpy(rm
->old_sha1
, item
->util
);
666 string_list_clear(&remote_refs
, 0);
669 static void check_not_current_branch(struct ref
*ref_map
)
671 struct branch
*current_branch
= branch_get(NULL
);
673 if (is_bare_repository() || !current_branch
)
676 for (; ref_map
; ref_map
= ref_map
->next
)
677 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
678 ref_map
->peer_ref
->name
))
679 die(_("Refusing to fetch into current branch %s "
680 "of non-bare repository"), current_branch
->refname
);
683 static int truncate_fetch_head(void)
685 char *filename
= git_path("FETCH_HEAD");
686 FILE *fp
= fopen(filename
, "w");
689 return error(_("cannot open %s: %s\n"), filename
, strerror(errno
));
694 static int do_fetch(struct transport
*transport
,
695 struct refspec
*refs
, int ref_count
)
697 struct string_list existing_refs
= STRING_LIST_INIT_DUP
;
700 int autotags
= (transport
->remote
->fetch_tags
== 1);
703 for_each_ref(add_existing
, &existing_refs
);
705 if (tags
== TAGS_DEFAULT
) {
706 if (transport
->remote
->fetch_tags
== 2)
708 if (transport
->remote
->fetch_tags
== -1)
712 if (!transport
->get_refs_list
|| !transport
->fetch
)
713 die(_("Don't know how to fetch from %s"), transport
->url
);
715 /* if not appending, truncate FETCH_HEAD */
716 if (!append
&& !dry_run
) {
717 retcode
= truncate_fetch_head();
722 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
724 check_not_current_branch(ref_map
);
726 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
728 struct string_list_item
*peer_item
=
729 string_list_lookup(&existing_refs
,
732 hashcpy(rm
->peer_ref
->old_sha1
,
737 if (tags
== TAGS_DEFAULT
&& autotags
)
738 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
739 if (fetch_refs(transport
, ref_map
)) {
745 /* If --tags was specified, pretend the user gave us the canonical tags refspec */
746 if (tags
== TAGS_SET
) {
747 const char *tags_str
= "refs/tags/*:refs/tags/*";
748 struct refspec
*tags_refspec
, *refspec
;
750 /* Copy the refspec and add the tags to it */
751 refspec
= xcalloc(ref_count
+ 1, sizeof(struct refspec
));
752 tags_refspec
= parse_fetch_refspec(1, &tags_str
);
753 memcpy(refspec
, refs
, ref_count
* sizeof(struct refspec
));
754 memcpy(&refspec
[ref_count
], tags_refspec
, sizeof(struct refspec
));
757 prune_refs(refspec
, ref_count
, ref_map
);
760 /* The rest of the strings belong to fetch_one */
761 free_refspec(1, tags_refspec
);
763 } else if (ref_count
) {
764 prune_refs(refs
, ref_count
, ref_map
);
766 prune_refs(transport
->remote
->fetch
, transport
->remote
->fetch_refspec_nr
, ref_map
);
771 /* if neither --no-tags nor --tags was specified, do automated tag
773 if (tags
== TAGS_DEFAULT
&& autotags
) {
774 struct ref
**tail
= &ref_map
;
776 find_non_local_tags(transport
, &ref_map
, &tail
);
778 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
779 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
780 fetch_refs(transport
, ref_map
);
786 string_list_clear(&existing_refs
, 1);
790 static void set_option(const char *name
, const char *value
)
792 int r
= transport_set_option(transport
, name
, value
);
794 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
795 name
, value
, transport
->url
);
797 warning(_("Option \"%s\" is ignored for %s\n"),
798 name
, transport
->url
);
801 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
803 struct string_list
*list
= priv
;
804 if (!remote
->skip_default_update
)
805 string_list_append(list
, remote
->name
);
809 struct remote_group_data
{
811 struct string_list
*list
;
814 static int get_remote_group(const char *key
, const char *value
, void *priv
)
816 struct remote_group_data
*g
= priv
;
818 if (!prefixcmp(key
, "remotes.") &&
819 !strcmp(key
+ 8, g
->name
)) {
820 /* split list by white space */
821 int space
= strcspn(value
, " \t\n");
824 string_list_append(g
->list
,
825 xstrndup(value
, space
));
827 value
+= space
+ (value
[space
] != '\0');
828 space
= strcspn(value
, " \t\n");
835 static int add_remote_or_group(const char *name
, struct string_list
*list
)
837 int prev_nr
= list
->nr
;
838 struct remote_group_data g
;
839 g
.name
= name
; g
.list
= list
;
841 git_config(get_remote_group
, &g
);
842 if (list
->nr
== prev_nr
) {
843 struct remote
*remote
;
844 if (!remote_is_configured(name
))
846 remote
= remote_get(name
);
847 string_list_append(list
, remote
->name
);
852 static void add_options_to_argv(struct argv_array
*argv
)
855 argv_array_push(argv
, "--dry-run");
857 argv_array_push(argv
, "--prune");
859 argv_array_push(argv
, "--update-head-ok");
861 argv_array_push(argv
, "--force");
863 argv_array_push(argv
, "--keep");
864 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
865 argv_array_push(argv
, "--recurse-submodules");
866 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
867 argv_array_push(argv
, "--recurse-submodules=on-demand");
868 if (tags
== TAGS_SET
)
869 argv_array_push(argv
, "--tags");
870 else if (tags
== TAGS_UNSET
)
871 argv_array_push(argv
, "--no-tags");
873 argv_array_push(argv
, "-v");
875 argv_array_push(argv
, "-v");
876 else if (verbosity
< 0)
877 argv_array_push(argv
, "-q");
881 static int fetch_multiple(struct string_list
*list
)
884 struct argv_array argv
= ARGV_ARRAY_INIT
;
886 if (!append
&& !dry_run
) {
887 int errcode
= truncate_fetch_head();
892 argv_array_pushl(&argv
, "fetch", "--append", NULL
);
893 add_options_to_argv(&argv
);
895 for (i
= 0; i
< list
->nr
; i
++) {
896 const char *name
= list
->items
[i
].string
;
897 argv_array_push(&argv
, name
);
899 printf(_("Fetching %s\n"), name
);
900 if (run_command_v_opt(argv
.argv
, RUN_GIT_CMD
)) {
901 error(_("Could not fetch %s"), name
);
904 argv_array_pop(&argv
);
907 argv_array_clear(&argv
);
911 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
914 static const char **refs
= NULL
;
915 struct refspec
*refspec
;
920 die(_("No remote repository specified. Please, specify either a URL or a\n"
921 "remote name from which new revisions should be fetched."));
923 transport
= transport_get(remote
, NULL
);
924 transport_set_verbosity(transport
, verbosity
, progress
);
926 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
928 set_option(TRANS_OPT_KEEP
, "yes");
930 set_option(TRANS_OPT_DEPTH
, depth
);
934 refs
= xcalloc(argc
+ 1, sizeof(const char *));
935 for (i
= 0; i
< argc
; i
++) {
936 if (!strcmp(argv
[i
], "tag")) {
940 die(_("You need to specify a tag name."));
941 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
942 strcpy(ref
, "refs/tags/");
943 strcat(ref
, argv
[i
]);
944 strcat(ref
, ":refs/tags/");
945 strcat(ref
, argv
[i
]);
954 sigchain_push_common(unlock_pack_on_signal
);
956 refspec
= parse_fetch_refspec(ref_nr
, refs
);
957 exit_code
= do_fetch(transport
, refspec
, ref_nr
);
958 free_refspec(ref_nr
, refspec
);
959 transport_disconnect(transport
);
964 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
967 struct string_list list
= STRING_LIST_INIT_NODUP
;
968 struct remote
*remote
;
970 static const char *argv_gc_auto
[] = {
971 "gc", "--auto", NULL
,
974 packet_trace_identity("fetch");
976 /* Record the command line for the reflog */
977 strbuf_addstr(&default_rla
, "fetch");
978 for (i
= 1; i
< argc
; i
++)
979 strbuf_addf(&default_rla
, " %s", argv
[i
]);
981 argc
= parse_options(argc
, argv
, prefix
,
982 builtin_fetch_options
, builtin_fetch_usage
, 0);
986 die(_("--depth and --unshallow cannot be used together"));
987 else if (!is_repository_shallow())
988 die(_("--unshallow on a complete repository does not make sense"));
990 static char inf_depth
[12];
991 sprintf(inf_depth
, "%d", INFINITE_DEPTH
);
996 if (recurse_submodules
!= RECURSE_SUBMODULES_OFF
) {
997 if (recurse_submodules_default
) {
998 int arg
= parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default
);
999 set_config_fetch_recurse_submodules(arg
);
1001 gitmodules_config();
1002 git_config(submodule_config
, NULL
);
1007 die(_("fetch --all does not take a repository argument"));
1009 die(_("fetch --all does not make sense with refspecs"));
1010 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
1011 result
= fetch_multiple(&list
);
1012 } else if (argc
== 0) {
1013 /* No arguments -- use default remote */
1014 remote
= remote_get(NULL
);
1015 result
= fetch_one(remote
, argc
, argv
);
1016 } else if (multiple
) {
1017 /* All arguments are assumed to be remotes or groups */
1018 for (i
= 0; i
< argc
; i
++)
1019 if (!add_remote_or_group(argv
[i
], &list
))
1020 die(_("No such remote or remote group: %s"), argv
[i
]);
1021 result
= fetch_multiple(&list
);
1023 /* Single remote or group */
1024 (void) add_remote_or_group(argv
[0], &list
);
1026 /* More than one remote */
1028 die(_("Fetching a group and specifying refspecs does not make sense"));
1029 result
= fetch_multiple(&list
);
1031 /* Zero or one remotes */
1032 remote
= remote_get(argv
[0]);
1033 result
= fetch_one(remote
, argc
-1, argv
+1);
1037 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
1038 struct argv_array options
= ARGV_ARRAY_INIT
;
1040 add_options_to_argv(&options
);
1041 result
= fetch_populated_submodules(&options
,
1045 argv_array_clear(&options
);
1048 /* All names were strdup()ed or strndup()ed */
1049 list
.strdup_strings
= 1;
1050 string_list_clear(&list
, 0);
1052 run_command_v_opt(argv_gc_auto
, RUN_GIT_CMD
);