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"
17 static const char * const builtin_fetch_usage
[] = {
18 "git fetch [<options>] [<repository> [<refspec>...]]",
19 "git fetch [<options>] <group>",
20 "git fetch --multiple [<options>] [<repository> | <group>]...",
21 "git fetch --all [<options>]",
31 static int all
, append
, dry_run
, force
, keep
, multiple
, prune
, update_head_ok
, verbosity
;
32 static int progress
, recurse_submodules
;
33 static int tags
= TAGS_DEFAULT
;
34 static const char *depth
;
35 static const char *upload_pack
;
36 static struct strbuf default_rla
= STRBUF_INIT
;
37 static struct transport
*transport
;
38 static const char *submodule_prefix
= "";
40 static struct option builtin_fetch_options
[] = {
41 OPT__VERBOSITY(&verbosity
),
42 OPT_BOOLEAN(0, "all", &all
,
43 "fetch from all remotes"),
44 OPT_BOOLEAN('a', "append", &append
,
45 "append to .git/FETCH_HEAD instead of overwriting"),
46 OPT_STRING(0, "upload-pack", &upload_pack
, "PATH",
47 "path to upload pack on remote end"),
48 OPT_BOOLEAN('f', "force", &force
,
49 "force overwrite of local branch"),
50 OPT_BOOLEAN('m', "multiple", &multiple
,
51 "fetch from multiple remotes"),
52 OPT_SET_INT('t', "tags", &tags
,
53 "fetch all tags and associated objects", TAGS_SET
),
54 OPT_SET_INT('n', NULL
, &tags
,
55 "do not fetch all tags (--no-tags)", TAGS_UNSET
),
56 OPT_BOOLEAN('p', "prune", &prune
,
57 "prune tracking branches no longer on remote"),
58 OPT_BOOLEAN(0, "recurse-submodules", &recurse_submodules
,
59 "control recursive fetching of submodules"),
60 OPT_BOOLEAN(0, "dry-run", &dry_run
,
62 OPT_BOOLEAN('k', "keep", &keep
, "keep downloaded pack"),
63 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok
,
64 "allow updating of HEAD ref"),
65 OPT_BOOLEAN(0, "progress", &progress
, "force progress reporting"),
66 OPT_STRING(0, "depth", &depth
, "DEPTH",
67 "deepen history of shallow clone"),
68 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, "DIR",
69 "prepend this to submodule path output", PARSE_OPT_HIDDEN
},
73 static void unlock_pack(void)
76 transport_unlock_pack(transport
);
79 static void unlock_pack_on_signal(int signo
)
86 static void add_merge_config(struct ref
**head
,
87 const struct ref
*remote_refs
,
88 struct branch
*branch
,
93 for (i
= 0; i
< branch
->merge_nr
; i
++) {
94 struct ref
*rm
, **old_tail
= *tail
;
95 struct refspec refspec
;
97 for (rm
= *head
; rm
; rm
= rm
->next
) {
98 if (branch_merge_matches(branch
, i
, rm
->name
)) {
107 * Not fetched to a tracking branch? We need to fetch
108 * it anyway to allow this branch's "branch.$name.merge"
109 * to be honored by 'git pull', but we do not have to
110 * fail if branch.$name.merge is misconfigured to point
111 * at a nonexisting branch. If we were indeed called by
112 * 'git pull', it will notice the misconfiguration because
113 * there is no entry in the resulting FETCH_HEAD marked
116 memset(&refspec
, 0, sizeof(refspec
));
117 refspec
.src
= branch
->merge
[i
]->src
;
118 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
119 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
124 static void find_non_local_tags(struct transport
*transport
,
128 static struct ref
*get_ref_map(struct transport
*transport
,
129 struct refspec
*refs
, int ref_count
, int tags
,
134 struct ref
*ref_map
= NULL
;
135 struct ref
**tail
= &ref_map
;
137 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
139 if (ref_count
|| tags
== TAGS_SET
) {
140 for (i
= 0; i
< ref_count
; i
++) {
141 get_fetch_map(remote_refs
, &refs
[i
], &tail
, 0);
142 if (refs
[i
].dst
&& refs
[i
].dst
[0])
145 /* Merge everything on the command line, but not --tags */
146 for (rm
= ref_map
; rm
; rm
= rm
->next
)
148 if (tags
== TAGS_SET
)
149 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
151 /* Use the defaults */
152 struct remote
*remote
= transport
->remote
;
153 struct branch
*branch
= branch_get(NULL
);
154 int has_merge
= branch_has_merge_config(branch
);
155 if (remote
&& (remote
->fetch_refspec_nr
|| has_merge
)) {
156 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
157 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
158 if (remote
->fetch
[i
].dst
&&
159 remote
->fetch
[i
].dst
[0])
161 if (!i
&& !has_merge
&& ref_map
&&
162 !remote
->fetch
[0].pattern
)
166 * if the remote we're fetching from is the same
167 * as given in branch.<name>.remote, we add the
168 * ref given in branch.<name>.merge, too.
171 !strcmp(branch
->remote_name
, remote
->name
))
172 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
174 ref_map
= get_remote_ref(remote_refs
, "HEAD");
176 die("Couldn't find remote ref HEAD");
178 tail
= &ref_map
->next
;
181 if (tags
== TAGS_DEFAULT
&& *autotags
)
182 find_non_local_tags(transport
, &ref_map
, &tail
);
183 ref_remove_duplicates(ref_map
);
188 #define STORE_REF_ERROR_OTHER 1
189 #define STORE_REF_ERROR_DF_CONFLICT 2
191 static int s_update_ref(const char *action
,
196 char *rla
= getenv("GIT_REFLOG_ACTION");
197 static struct ref_lock
*lock
;
202 rla
= default_rla
.buf
;
203 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
204 lock
= lock_any_ref_for_update(ref
->name
,
205 check_old
? ref
->old_sha1
: NULL
, 0);
207 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
208 STORE_REF_ERROR_OTHER
;
209 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
210 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
211 STORE_REF_ERROR_OTHER
;
215 #define REFCOL_WIDTH 10
217 static int update_local_ref(struct ref
*ref
,
221 struct commit
*current
= NULL
, *updated
;
222 enum object_type type
;
223 struct branch
*current_branch
= branch_get(NULL
);
224 const char *pretty_ref
= prettify_refname(ref
->name
);
227 type
= sha1_object_info(ref
->new_sha1
, NULL
);
229 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
231 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
233 sprintf(display
, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH
,
234 "[up to date]", REFCOL_WIDTH
, remote
,
239 if (current_branch
&&
240 !strcmp(ref
->name
, current_branch
->name
) &&
241 !(update_head_ok
|| is_bare_repository()) &&
242 !is_null_sha1(ref
->old_sha1
)) {
244 * If this is the head, and it's not okay to update
245 * the head, and the old value of the head isn't empty...
247 sprintf(display
, "! %-*s %-*s -> %s (can't fetch in current branch)",
248 TRANSPORT_SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
253 if (!is_null_sha1(ref
->old_sha1
) &&
254 !prefixcmp(ref
->name
, "refs/tags/")) {
256 r
= s_update_ref("updating tag", ref
, 0);
257 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '-',
258 TRANSPORT_SUMMARY_WIDTH
, "[tag update]", REFCOL_WIDTH
, remote
,
259 pretty_ref
, r
? " (unable to update local ref)" : "");
263 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
264 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
265 if (!current
|| !updated
) {
269 if (!strncmp(ref
->name
, "refs/tags/", 10)) {
274 msg
= "storing head";
275 what
= "[new branch]";
278 r
= s_update_ref(msg
, ref
, 0);
279 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '*',
280 TRANSPORT_SUMMARY_WIDTH
, what
, REFCOL_WIDTH
, remote
, pretty_ref
,
281 r
? " (unable to update local ref)" : "");
285 if (in_merge_bases(current
, &updated
, 1)) {
288 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
289 strcat(quickref
, "..");
290 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
291 r
= s_update_ref("fast-forward", ref
, 1);
292 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : ' ',
293 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
294 pretty_ref
, r
? " (unable to update local ref)" : "");
296 } else if (force
|| ref
->force
) {
299 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
300 strcat(quickref
, "...");
301 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
302 r
= s_update_ref("forced-update", ref
, 1);
303 sprintf(display
, "%c %-*s %-*s -> %s (%s)", r
? '!' : '+',
304 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
306 r
? "unable to update local ref" : "forced update");
309 sprintf(display
, "! %-*s %-*s -> %s (non-fast-forward)",
310 TRANSPORT_SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
316 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
320 struct commit
*commit
;
321 int url_len
, i
, note_len
, shown_url
= 0, rc
= 0;
323 const char *what
, *kind
;
325 char *url
, *filename
= dry_run
? "/dev/null" : git_path("FETCH_HEAD");
327 fp
= fopen(filename
, "a");
329 return error("cannot open %s: %s\n", filename
, strerror(errno
));
332 url
= transport_anonymize_url(raw_url
);
334 url
= xstrdup("foreign");
335 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
336 struct ref
*ref
= NULL
;
339 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
340 strcpy(ref
->name
, rm
->peer_ref
->name
);
341 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
342 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
343 ref
->force
= rm
->peer_ref
->force
;
346 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
350 if (!strcmp(rm
->name
, "HEAD")) {
354 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
356 what
= rm
->name
+ 11;
358 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
360 what
= rm
->name
+ 10;
362 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
363 kind
= "remote branch";
364 what
= rm
->name
+ 13;
371 url_len
= strlen(url
);
372 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
375 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
381 note_len
+= sprintf(note
+ note_len
, "%s ",
383 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
385 note
[note_len
] = '\0';
386 fprintf(fp
, "%s\t%s\t%s",
387 sha1_to_hex(commit
? commit
->object
.sha1
:
389 rm
->merge
? "" : "not-for-merge",
391 for (i
= 0; i
< url_len
; ++i
)
399 rc
|= update_local_ref(ref
, what
, note
);
402 sprintf(note
, "* %-*s %-*s -> FETCH_HEAD",
403 TRANSPORT_SUMMARY_WIDTH
, *kind
? kind
: "branch",
404 REFCOL_WIDTH
, *what
? what
: "HEAD");
406 if (verbosity
>= 0 && !shown_url
) {
407 fprintf(stderr
, "From %.*s\n",
412 fprintf(stderr
, " %s\n", note
);
417 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
418 error("some local refs could not be updated; try running\n"
419 " 'git remote prune %s' to remove any old, conflicting "
420 "branches", remote_name
);
425 * We would want to bypass the object transfer altogether if
426 * everything we are going to fetch already exists and is connected
429 * The refs we are going to fetch are in ref_map. If running
431 * $ git rev-list --objects --stdin --not --all
433 * (feeding all the refs in ref_map on its standard input)
434 * does not error out, that means everything reachable from the
435 * refs we are going to fetch exists and is connected to some of
438 static int quickfetch(struct ref
*ref_map
)
440 struct child_process revlist
;
443 const char *argv
[] = {"rev-list",
444 "--quiet", "--objects", "--stdin", "--not", "--all", NULL
};
447 * If we are deepening a shallow clone we already have these
448 * objects reachable. Running rev-list here will return with
449 * a good (0) exit status and we'll bypass the fetch that we
450 * really need to perform. Claiming failure now will ensure
451 * we perform the network exchange to deepen our history.
459 memset(&revlist
, 0, sizeof(revlist
));
462 revlist
.no_stdout
= 1;
463 revlist
.no_stderr
= 1;
466 err
= start_command(&revlist
);
468 error("could not run rev-list");
473 * If rev-list --stdin encounters an unknown commit, it terminates,
474 * which will cause SIGPIPE in the write loop below.
476 sigchain_push(SIGPIPE
, SIG_IGN
);
478 for (ref
= ref_map
; ref
; ref
= ref
->next
) {
479 if (write_in_full(revlist
.in
, sha1_to_hex(ref
->old_sha1
), 40) < 0 ||
480 write_str_in_full(revlist
.in
, "\n") < 0) {
481 if (errno
!= EPIPE
&& errno
!= EINVAL
)
482 error("failed write to rev-list: %s", strerror(errno
));
488 if (close(revlist
.in
)) {
489 error("failed to close rev-list's stdin: %s", strerror(errno
));
493 sigchain_pop(SIGPIPE
);
495 return finish_command(&revlist
) || err
;
498 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
500 int ret
= quickfetch(ref_map
);
502 ret
= transport_fetch_refs(transport
, ref_map
);
504 ret
|= store_updated_refs(transport
->url
,
505 transport
->remote
->name
,
507 transport_unlock_pack(transport
);
511 static int prune_refs(struct transport
*transport
, struct ref
*ref_map
)
514 struct ref
*ref
, *stale_refs
= get_stale_heads(transport
->remote
, ref_map
);
515 const char *dangling_msg
= dry_run
516 ? " (%s will become dangling)\n"
517 : " (%s has become dangling)\n";
519 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
521 result
|= delete_ref(ref
->name
, NULL
, 0);
522 if (verbosity
>= 0) {
523 fprintf(stderr
, " x %-*s %-*s -> %s\n",
524 TRANSPORT_SUMMARY_WIDTH
, "[deleted]",
525 REFCOL_WIDTH
, "(none)", prettify_refname(ref
->name
));
526 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
529 free_refs(stale_refs
);
533 static int add_existing(const char *refname
, const unsigned char *sha1
,
534 int flag
, void *cbdata
)
536 struct string_list
*list
= (struct string_list
*)cbdata
;
537 struct string_list_item
*item
= string_list_insert(list
, refname
);
538 item
->util
= (void *)sha1
;
542 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
544 struct ref
*rm
= *head
;
546 if (!hashcmp(rm
->old_sha1
, sha1
))
553 static void find_non_local_tags(struct transport
*transport
,
557 struct string_list existing_refs
= STRING_LIST_INIT_NODUP
;
558 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
559 const struct ref
*ref
;
560 struct string_list_item
*item
= NULL
;
562 for_each_ref(add_existing
, &existing_refs
);
563 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
564 if (prefixcmp(ref
->name
, "refs/tags"))
568 * The peeled ref always follows the matching base
569 * ref, so if we see a peeled ref that we don't want
570 * to fetch then we can mark the ref entry in the list
571 * as one to ignore by setting util to NULL.
573 if (!suffixcmp(ref
->name
, "^{}")) {
574 if (item
&& !has_sha1_file(ref
->old_sha1
) &&
575 !will_fetch(head
, ref
->old_sha1
) &&
576 !has_sha1_file(item
->util
) &&
577 !will_fetch(head
, item
->util
))
584 * If item is non-NULL here, then we previously saw a
585 * ref not followed by a peeled reference, so we need
586 * to check if it is a lightweight tag that we want to
589 if (item
&& !has_sha1_file(item
->util
) &&
590 !will_fetch(head
, item
->util
))
595 /* skip duplicates and refs that we already have */
596 if (string_list_has_string(&remote_refs
, ref
->name
) ||
597 string_list_has_string(&existing_refs
, ref
->name
))
600 item
= string_list_insert(&remote_refs
, ref
->name
);
601 item
->util
= (void *)ref
->old_sha1
;
603 string_list_clear(&existing_refs
, 0);
606 * We may have a final lightweight tag that needs to be
607 * checked to see if it needs fetching.
609 if (item
&& !has_sha1_file(item
->util
) &&
610 !will_fetch(head
, item
->util
))
614 * For all the tags in the remote_refs string list,
615 * add them to the list of refs to be fetched
617 for_each_string_list_item(item
, &remote_refs
) {
618 /* Unless we have already decided to ignore this item... */
621 struct ref
*rm
= alloc_ref(item
->string
);
622 rm
->peer_ref
= alloc_ref(item
->string
);
623 hashcpy(rm
->old_sha1
, item
->util
);
629 string_list_clear(&remote_refs
, 0);
632 static void check_not_current_branch(struct ref
*ref_map
)
634 struct branch
*current_branch
= branch_get(NULL
);
636 if (is_bare_repository() || !current_branch
)
639 for (; ref_map
; ref_map
= ref_map
->next
)
640 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
641 ref_map
->peer_ref
->name
))
642 die("Refusing to fetch into current branch %s "
643 "of non-bare repository", current_branch
->refname
);
646 static int truncate_fetch_head(void)
648 char *filename
= git_path("FETCH_HEAD");
649 FILE *fp
= fopen(filename
, "w");
652 return error("cannot open %s: %s\n", filename
, strerror(errno
));
657 static int do_fetch(struct transport
*transport
,
658 struct refspec
*refs
, int ref_count
)
660 struct string_list existing_refs
= STRING_LIST_INIT_NODUP
;
661 struct string_list_item
*peer_item
= NULL
;
664 int autotags
= (transport
->remote
->fetch_tags
== 1);
666 for_each_ref(add_existing
, &existing_refs
);
668 if (transport
->remote
->fetch_tags
== 2 && tags
!= TAGS_UNSET
)
670 if (transport
->remote
->fetch_tags
== -1)
673 if (!transport
->get_refs_list
|| !transport
->fetch
)
674 die("Don't know how to fetch from %s", transport
->url
);
676 /* if not appending, truncate FETCH_HEAD */
677 if (!append
&& !dry_run
) {
678 int errcode
= truncate_fetch_head();
683 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
685 check_not_current_branch(ref_map
);
687 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
689 peer_item
= string_list_lookup(&existing_refs
,
692 hashcpy(rm
->peer_ref
->old_sha1
,
697 if (tags
== TAGS_DEFAULT
&& autotags
)
698 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
699 if (fetch_refs(transport
, ref_map
)) {
704 prune_refs(transport
, ref_map
);
707 /* if neither --no-tags nor --tags was specified, do automated tag
709 if (tags
== TAGS_DEFAULT
&& autotags
) {
710 struct ref
**tail
= &ref_map
;
712 find_non_local_tags(transport
, &ref_map
, &tail
);
714 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
715 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
716 fetch_refs(transport
, ref_map
);
724 static void set_option(const char *name
, const char *value
)
726 int r
= transport_set_option(transport
, name
, value
);
728 die("Option \"%s\" value \"%s\" is not valid for %s",
729 name
, value
, transport
->url
);
731 warning("Option \"%s\" is ignored for %s\n",
732 name
, transport
->url
);
735 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
737 struct string_list
*list
= priv
;
738 if (!remote
->skip_default_update
)
739 string_list_append(list
, remote
->name
);
743 struct remote_group_data
{
745 struct string_list
*list
;
748 static int get_remote_group(const char *key
, const char *value
, void *priv
)
750 struct remote_group_data
*g
= priv
;
752 if (!prefixcmp(key
, "remotes.") &&
753 !strcmp(key
+ 8, g
->name
)) {
754 /* split list by white space */
755 int space
= strcspn(value
, " \t\n");
758 string_list_append(g
->list
,
759 xstrndup(value
, space
));
761 value
+= space
+ (value
[space
] != '\0');
762 space
= strcspn(value
, " \t\n");
769 static int add_remote_or_group(const char *name
, struct string_list
*list
)
771 int prev_nr
= list
->nr
;
772 struct remote_group_data g
;
773 g
.name
= name
; g
.list
= list
;
775 git_config(get_remote_group
, &g
);
776 if (list
->nr
== prev_nr
) {
777 struct remote
*remote
;
778 if (!remote_is_configured(name
))
780 remote
= remote_get(name
);
781 string_list_append(list
, remote
->name
);
786 static void add_options_to_argv(int *argc
, const char **argv
)
789 argv
[(*argc
)++] = "--dry-run";
791 argv
[(*argc
)++] = "--prune";
793 argv
[(*argc
)++] = "--update-head-ok";
795 argv
[(*argc
)++] = "--force";
797 argv
[(*argc
)++] = "--keep";
798 if (recurse_submodules
)
799 argv
[(*argc
)++] = "--recurse-submodules";
801 argv
[(*argc
)++] = "-v";
803 argv
[(*argc
)++] = "-v";
804 else if (verbosity
< 0)
805 argv
[(*argc
)++] = "-q";
809 static int fetch_multiple(struct string_list
*list
)
812 const char *argv
[12] = { "fetch", "--append" };
815 add_options_to_argv(&argc
, argv
);
817 if (!append
&& !dry_run
) {
818 int errcode
= truncate_fetch_head();
823 for (i
= 0; i
< list
->nr
; i
++) {
824 const char *name
= list
->items
[i
].string
;
826 argv
[argc
+ 1] = NULL
;
828 printf("Fetching %s\n", name
);
829 if (run_command_v_opt(argv
, RUN_GIT_CMD
)) {
830 error("Could not fetch %s", name
);
838 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
841 static const char **refs
= NULL
;
846 die("No remote repository specified. Please, specify either a URL or a\n"
847 "remote name from which new revisions should be fetched.");
849 transport
= transport_get(remote
, NULL
);
850 transport_set_verbosity(transport
, verbosity
, progress
);
852 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
854 set_option(TRANS_OPT_KEEP
, "yes");
856 set_option(TRANS_OPT_DEPTH
, depth
);
860 refs
= xcalloc(argc
+ 1, sizeof(const char *));
861 for (i
= 0; i
< argc
; i
++) {
862 if (!strcmp(argv
[i
], "tag")) {
866 die("You need to specify a tag name.");
867 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
868 strcpy(ref
, "refs/tags/");
869 strcat(ref
, argv
[i
]);
870 strcat(ref
, ":refs/tags/");
871 strcat(ref
, argv
[i
]);
880 sigchain_push_common(unlock_pack_on_signal
);
882 exit_code
= do_fetch(transport
,
883 parse_fetch_refspec(ref_nr
, refs
), ref_nr
);
884 transport_disconnect(transport
);
889 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
892 struct string_list list
= STRING_LIST_INIT_NODUP
;
893 struct remote
*remote
;
896 /* Record the command line for the reflog */
897 strbuf_addstr(&default_rla
, "fetch");
898 for (i
= 1; i
< argc
; i
++)
899 strbuf_addf(&default_rla
, " %s", argv
[i
]);
901 argc
= parse_options(argc
, argv
, prefix
,
902 builtin_fetch_options
, builtin_fetch_usage
, 0);
906 die("fetch --all does not take a repository argument");
908 die("fetch --all does not make sense with refspecs");
909 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
910 result
= fetch_multiple(&list
);
911 } else if (argc
== 0) {
912 /* No arguments -- use default remote */
913 remote
= remote_get(NULL
);
914 result
= fetch_one(remote
, argc
, argv
);
915 } else if (multiple
) {
916 /* All arguments are assumed to be remotes or groups */
917 for (i
= 0; i
< argc
; i
++)
918 if (!add_remote_or_group(argv
[i
], &list
))
919 die("No such remote or remote group: %s", argv
[i
]);
920 result
= fetch_multiple(&list
);
922 /* Single remote or group */
923 (void) add_remote_or_group(argv
[0], &list
);
925 /* More than one remote */
927 die("Fetching a group and specifying refspecs does not make sense");
928 result
= fetch_multiple(&list
);
930 /* Zero or one remotes */
931 remote
= remote_get(argv
[0]);
932 result
= fetch_one(remote
, argc
-1, argv
+1);
936 if (!result
&& recurse_submodules
) {
937 const char *options
[10];
940 git_config(submodule_config
, NULL
);
941 add_options_to_argv(&num_options
, options
);
942 result
= fetch_populated_submodules(num_options
, options
,
947 /* All names were strdup()ed or strndup()ed */
948 list
.strdup_strings
= 1;
949 string_list_clear(&list
, 0);