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>]",
33 RECURSIVE_DEFAULT
= 1,
37 static int all
, append
, dry_run
, force
, keep
, multiple
, prune
, recursive
= RECURSIVE_DEFAULT
, update_head_ok
, verbosity
;
39 static int tags
= TAGS_DEFAULT
;
40 static const char *depth
;
41 static const char *upload_pack
;
42 static struct strbuf default_rla
= STRBUF_INIT
;
43 static struct transport
*transport
;
45 static struct option builtin_fetch_options
[] = {
46 OPT__VERBOSITY(&verbosity
),
47 OPT_BOOLEAN(0, "all", &all
,
48 "fetch from all remotes"),
49 OPT_BOOLEAN('a', "append", &append
,
50 "append to .git/FETCH_HEAD instead of overwriting"),
51 OPT_STRING(0, "upload-pack", &upload_pack
, "PATH",
52 "path to upload pack on remote end"),
53 OPT_BOOLEAN('f', "force", &force
,
54 "force overwrite of local branch"),
55 OPT_BOOLEAN('m', "multiple", &multiple
,
56 "fetch from multiple remotes"),
57 OPT_SET_INT('t', "tags", &tags
,
58 "fetch all tags and associated objects", TAGS_SET
),
59 OPT_SET_INT('n', NULL
, &tags
,
60 "do not fetch all tags (--no-tags)", TAGS_UNSET
),
61 OPT_BOOLEAN('p', "prune", &prune
,
62 "prune tracking branches no longer on remote"),
63 OPT_SET_INT(0, "recursive", &recursive
,
64 "control recursive fetching of submodules", RECURSIVE_SET
),
65 OPT_BOOLEAN(0, "dry-run", &dry_run
,
67 OPT_BOOLEAN('k', "keep", &keep
, "keep downloaded pack"),
68 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok
,
69 "allow updating of HEAD ref"),
70 OPT_BOOLEAN(0, "progress", &progress
, "force progress reporting"),
71 OPT_STRING(0, "depth", &depth
, "DEPTH",
72 "deepen history of shallow clone"),
76 static void unlock_pack(void)
79 transport_unlock_pack(transport
);
82 static void unlock_pack_on_signal(int signo
)
89 static void add_merge_config(struct ref
**head
,
90 const struct ref
*remote_refs
,
91 struct branch
*branch
,
96 for (i
= 0; i
< branch
->merge_nr
; i
++) {
97 struct ref
*rm
, **old_tail
= *tail
;
98 struct refspec refspec
;
100 for (rm
= *head
; rm
; rm
= rm
->next
) {
101 if (branch_merge_matches(branch
, i
, rm
->name
)) {
110 * Not fetched to a tracking branch? We need to fetch
111 * it anyway to allow this branch's "branch.$name.merge"
112 * to be honored by 'git pull', but we do not have to
113 * fail if branch.$name.merge is misconfigured to point
114 * at a nonexisting branch. If we were indeed called by
115 * 'git pull', it will notice the misconfiguration because
116 * there is no entry in the resulting FETCH_HEAD marked
119 memset(&refspec
, 0, sizeof(refspec
));
120 refspec
.src
= branch
->merge
[i
]->src
;
121 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
122 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
127 static void find_non_local_tags(struct transport
*transport
,
131 static struct ref
*get_ref_map(struct transport
*transport
,
132 struct refspec
*refs
, int ref_count
, int tags
,
137 struct ref
*ref_map
= NULL
;
138 struct ref
**tail
= &ref_map
;
140 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
142 if (ref_count
|| tags
== TAGS_SET
) {
143 for (i
= 0; i
< ref_count
; i
++) {
144 get_fetch_map(remote_refs
, &refs
[i
], &tail
, 0);
145 if (refs
[i
].dst
&& refs
[i
].dst
[0])
148 /* Merge everything on the command line, but not --tags */
149 for (rm
= ref_map
; rm
; rm
= rm
->next
)
151 if (tags
== TAGS_SET
)
152 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
154 /* Use the defaults */
155 struct remote
*remote
= transport
->remote
;
156 struct branch
*branch
= branch_get(NULL
);
157 int has_merge
= branch_has_merge_config(branch
);
159 (remote
->fetch_refspec_nr
||
160 /* Note: has_merge implies non-NULL branch->remote_name */
161 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
162 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
163 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
164 if (remote
->fetch
[i
].dst
&&
165 remote
->fetch
[i
].dst
[0])
167 if (!i
&& !has_merge
&& ref_map
&&
168 !remote
->fetch
[0].pattern
)
172 * if the remote we're fetching from is the same
173 * as given in branch.<name>.remote, we add the
174 * ref given in branch.<name>.merge, too.
176 * Note: has_merge implies non-NULL branch->remote_name
179 !strcmp(branch
->remote_name
, remote
->name
))
180 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
182 ref_map
= get_remote_ref(remote_refs
, "HEAD");
184 die("Couldn't find remote ref HEAD");
186 tail
= &ref_map
->next
;
189 if (tags
== TAGS_DEFAULT
&& *autotags
)
190 find_non_local_tags(transport
, &ref_map
, &tail
);
191 ref_remove_duplicates(ref_map
);
196 #define STORE_REF_ERROR_OTHER 1
197 #define STORE_REF_ERROR_DF_CONFLICT 2
199 static int s_update_ref(const char *action
,
204 char *rla
= getenv("GIT_REFLOG_ACTION");
205 static struct ref_lock
*lock
;
210 rla
= default_rla
.buf
;
211 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
212 lock
= lock_any_ref_for_update(ref
->name
,
213 check_old
? ref
->old_sha1
: NULL
, 0);
215 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
216 STORE_REF_ERROR_OTHER
;
217 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
218 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
219 STORE_REF_ERROR_OTHER
;
223 #define REFCOL_WIDTH 10
225 static int update_local_ref(struct ref
*ref
,
229 struct commit
*current
= NULL
, *updated
;
230 enum object_type type
;
231 struct branch
*current_branch
= branch_get(NULL
);
232 const char *pretty_ref
= prettify_refname(ref
->name
);
235 type
= sha1_object_info(ref
->new_sha1
, NULL
);
237 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
239 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
241 sprintf(display
, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH
,
242 "[up to date]", REFCOL_WIDTH
, remote
,
247 if (current_branch
&&
248 !strcmp(ref
->name
, current_branch
->name
) &&
249 !(update_head_ok
|| is_bare_repository()) &&
250 !is_null_sha1(ref
->old_sha1
)) {
252 * If this is the head, and it's not okay to update
253 * the head, and the old value of the head isn't empty...
255 sprintf(display
, "! %-*s %-*s -> %s (can't fetch in current branch)",
256 TRANSPORT_SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
261 if (!is_null_sha1(ref
->old_sha1
) &&
262 !prefixcmp(ref
->name
, "refs/tags/")) {
264 r
= s_update_ref("updating tag", ref
, 0);
265 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '-',
266 TRANSPORT_SUMMARY_WIDTH
, "[tag update]", REFCOL_WIDTH
, remote
,
267 pretty_ref
, r
? " (unable to update local ref)" : "");
271 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
272 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
273 if (!current
|| !updated
) {
277 if (!strncmp(ref
->name
, "refs/tags/", 10)) {
282 msg
= "storing head";
283 what
= "[new branch]";
286 r
= s_update_ref(msg
, ref
, 0);
287 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '*',
288 TRANSPORT_SUMMARY_WIDTH
, what
, REFCOL_WIDTH
, remote
, pretty_ref
,
289 r
? " (unable to update local ref)" : "");
293 if (in_merge_bases(current
, &updated
, 1)) {
296 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
297 strcat(quickref
, "..");
298 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
299 r
= s_update_ref("fast-forward", ref
, 1);
300 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : ' ',
301 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
302 pretty_ref
, r
? " (unable to update local ref)" : "");
304 } else if (force
|| ref
->force
) {
307 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
308 strcat(quickref
, "...");
309 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
310 r
= s_update_ref("forced-update", ref
, 1);
311 sprintf(display
, "%c %-*s %-*s -> %s (%s)", r
? '!' : '+',
312 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
314 r
? "unable to update local ref" : "forced update");
317 sprintf(display
, "! %-*s %-*s -> %s (non-fast-forward)",
318 TRANSPORT_SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
324 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
328 struct commit
*commit
;
329 int url_len
, i
, note_len
, shown_url
= 0, rc
= 0;
331 const char *what
, *kind
;
333 char *url
, *filename
= dry_run
? "/dev/null" : git_path("FETCH_HEAD");
335 fp
= fopen(filename
, "a");
337 return error("cannot open %s: %s\n", filename
, strerror(errno
));
340 url
= transport_anonymize_url(raw_url
);
342 url
= xstrdup("foreign");
343 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
344 struct ref
*ref
= NULL
;
347 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
348 strcpy(ref
->name
, rm
->peer_ref
->name
);
349 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
350 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
351 ref
->force
= rm
->peer_ref
->force
;
354 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
358 if (!strcmp(rm
->name
, "HEAD")) {
362 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
364 what
= rm
->name
+ 11;
366 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
368 what
= rm
->name
+ 10;
370 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
371 kind
= "remote branch";
372 what
= rm
->name
+ 13;
379 url_len
= strlen(url
);
380 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
383 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
389 note_len
+= sprintf(note
+ note_len
, "%s ",
391 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
393 note
[note_len
] = '\0';
394 fprintf(fp
, "%s\t%s\t%s",
395 sha1_to_hex(commit
? commit
->object
.sha1
:
397 rm
->merge
? "" : "not-for-merge",
399 for (i
= 0; i
< url_len
; ++i
)
407 rc
|= update_local_ref(ref
, what
, note
);
410 sprintf(note
, "* %-*s %-*s -> FETCH_HEAD",
411 TRANSPORT_SUMMARY_WIDTH
, *kind
? kind
: "branch",
412 REFCOL_WIDTH
, *what
? what
: "HEAD");
414 if (verbosity
>= 0 && !shown_url
) {
415 fprintf(stderr
, "From %.*s\n",
420 fprintf(stderr
, " %s\n", note
);
425 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
426 error("some local refs could not be updated; try running\n"
427 " 'git remote prune %s' to remove any old, conflicting "
428 "branches", remote_name
);
433 * We would want to bypass the object transfer altogether if
434 * everything we are going to fetch already exists and is connected
437 * The refs we are going to fetch are in ref_map. If running
439 * $ git rev-list --objects --stdin --not --all
441 * (feeding all the refs in ref_map on its standard input)
442 * does not error out, that means everything reachable from the
443 * refs we are going to fetch exists and is connected to some of
446 static int quickfetch(struct ref
*ref_map
)
448 struct child_process revlist
;
451 const char *argv
[] = {"rev-list",
452 "--quiet", "--objects", "--stdin", "--not", "--all", NULL
};
455 * If we are deepening a shallow clone we already have these
456 * objects reachable. Running rev-list here will return with
457 * a good (0) exit status and we'll bypass the fetch that we
458 * really need to perform. Claiming failure now will ensure
459 * we perform the network exchange to deepen our history.
467 memset(&revlist
, 0, sizeof(revlist
));
470 revlist
.no_stdout
= 1;
471 revlist
.no_stderr
= 1;
474 err
= start_command(&revlist
);
476 error("could not run rev-list");
481 * If rev-list --stdin encounters an unknown commit, it terminates,
482 * which will cause SIGPIPE in the write loop below.
484 sigchain_push(SIGPIPE
, SIG_IGN
);
486 for (ref
= ref_map
; ref
; ref
= ref
->next
) {
487 if (write_in_full(revlist
.in
, sha1_to_hex(ref
->old_sha1
), 40) < 0 ||
488 write_str_in_full(revlist
.in
, "\n") < 0) {
489 if (errno
!= EPIPE
&& errno
!= EINVAL
)
490 error("failed write to rev-list: %s", strerror(errno
));
496 if (close(revlist
.in
)) {
497 error("failed to close rev-list's stdin: %s", strerror(errno
));
501 sigchain_pop(SIGPIPE
);
503 return finish_command(&revlist
) || err
;
506 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
508 int ret
= quickfetch(ref_map
);
510 ret
= transport_fetch_refs(transport
, ref_map
);
512 ret
|= store_updated_refs(transport
->url
,
513 transport
->remote
->name
,
515 transport_unlock_pack(transport
);
519 static int prune_refs(struct transport
*transport
, struct ref
*ref_map
)
522 struct ref
*ref
, *stale_refs
= get_stale_heads(transport
->remote
, ref_map
);
523 const char *dangling_msg
= dry_run
524 ? " (%s will become dangling)\n"
525 : " (%s has become dangling)\n";
527 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
529 result
|= delete_ref(ref
->name
, NULL
, 0);
530 if (verbosity
>= 0) {
531 fprintf(stderr
, " x %-*s %-*s -> %s\n",
532 TRANSPORT_SUMMARY_WIDTH
, "[deleted]",
533 REFCOL_WIDTH
, "(none)", prettify_refname(ref
->name
));
534 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
537 free_refs(stale_refs
);
541 static int add_existing(const char *refname
, const unsigned char *sha1
,
542 int flag
, void *cbdata
)
544 struct string_list
*list
= (struct string_list
*)cbdata
;
545 struct string_list_item
*item
= string_list_insert(list
, refname
);
546 item
->util
= (void *)sha1
;
550 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
552 struct ref
*rm
= *head
;
554 if (!hashcmp(rm
->old_sha1
, sha1
))
561 static void find_non_local_tags(struct transport
*transport
,
565 struct string_list existing_refs
= STRING_LIST_INIT_NODUP
;
566 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
567 const struct ref
*ref
;
568 struct string_list_item
*item
= NULL
;
570 for_each_ref(add_existing
, &existing_refs
);
571 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
572 if (prefixcmp(ref
->name
, "refs/tags"))
576 * The peeled ref always follows the matching base
577 * ref, so if we see a peeled ref that we don't want
578 * to fetch then we can mark the ref entry in the list
579 * as one to ignore by setting util to NULL.
581 if (!suffixcmp(ref
->name
, "^{}")) {
582 if (item
&& !has_sha1_file(ref
->old_sha1
) &&
583 !will_fetch(head
, ref
->old_sha1
) &&
584 !has_sha1_file(item
->util
) &&
585 !will_fetch(head
, item
->util
))
592 * If item is non-NULL here, then we previously saw a
593 * ref not followed by a peeled reference, so we need
594 * to check if it is a lightweight tag that we want to
597 if (item
&& !has_sha1_file(item
->util
) &&
598 !will_fetch(head
, item
->util
))
603 /* skip duplicates and refs that we already have */
604 if (string_list_has_string(&remote_refs
, ref
->name
) ||
605 string_list_has_string(&existing_refs
, ref
->name
))
608 item
= string_list_insert(&remote_refs
, ref
->name
);
609 item
->util
= (void *)ref
->old_sha1
;
611 string_list_clear(&existing_refs
, 0);
614 * We may have a final lightweight tag that needs to be
615 * checked to see if it needs fetching.
617 if (item
&& !has_sha1_file(item
->util
) &&
618 !will_fetch(head
, item
->util
))
622 * For all the tags in the remote_refs string list,
623 * add them to the list of refs to be fetched
625 for_each_string_list_item(item
, &remote_refs
) {
626 /* Unless we have already decided to ignore this item... */
629 struct ref
*rm
= alloc_ref(item
->string
);
630 rm
->peer_ref
= alloc_ref(item
->string
);
631 hashcpy(rm
->old_sha1
, item
->util
);
637 string_list_clear(&remote_refs
, 0);
640 static void check_not_current_branch(struct ref
*ref_map
)
642 struct branch
*current_branch
= branch_get(NULL
);
644 if (is_bare_repository() || !current_branch
)
647 for (; ref_map
; ref_map
= ref_map
->next
)
648 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
649 ref_map
->peer_ref
->name
))
650 die("Refusing to fetch into current branch %s "
651 "of non-bare repository", current_branch
->refname
);
654 static int truncate_fetch_head(void)
656 char *filename
= git_path("FETCH_HEAD");
657 FILE *fp
= fopen(filename
, "w");
660 return error("cannot open %s: %s\n", filename
, strerror(errno
));
665 static int do_fetch(struct transport
*transport
,
666 struct refspec
*refs
, int ref_count
)
668 struct string_list existing_refs
= STRING_LIST_INIT_NODUP
;
669 struct string_list_item
*peer_item
= NULL
;
672 int autotags
= (transport
->remote
->fetch_tags
== 1);
674 for_each_ref(add_existing
, &existing_refs
);
676 if (tags
== TAGS_DEFAULT
) {
677 if (transport
->remote
->fetch_tags
== 2)
679 if (transport
->remote
->fetch_tags
== -1)
683 if (!transport
->get_refs_list
|| !transport
->fetch
)
684 die("Don't know how to fetch from %s", transport
->url
);
686 /* if not appending, truncate FETCH_HEAD */
687 if (!append
&& !dry_run
) {
688 int errcode
= truncate_fetch_head();
693 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
695 check_not_current_branch(ref_map
);
697 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
699 peer_item
= string_list_lookup(&existing_refs
,
702 hashcpy(rm
->peer_ref
->old_sha1
,
707 if (tags
== TAGS_DEFAULT
&& autotags
)
708 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
709 if (fetch_refs(transport
, ref_map
)) {
714 prune_refs(transport
, ref_map
);
717 /* if neither --no-tags nor --tags was specified, do automated tag
719 if (tags
== TAGS_DEFAULT
&& autotags
) {
720 struct ref
**tail
= &ref_map
;
722 find_non_local_tags(transport
, &ref_map
, &tail
);
724 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
725 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
726 fetch_refs(transport
, ref_map
);
734 static void set_option(const char *name
, const char *value
)
736 int r
= transport_set_option(transport
, name
, value
);
738 die("Option \"%s\" value \"%s\" is not valid for %s",
739 name
, value
, transport
->url
);
741 warning("Option \"%s\" is ignored for %s\n",
742 name
, transport
->url
);
745 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
747 struct string_list
*list
= priv
;
748 if (!remote
->skip_default_update
)
749 string_list_append(list
, remote
->name
);
753 struct remote_group_data
{
755 struct string_list
*list
;
758 static int get_remote_group(const char *key
, const char *value
, void *priv
)
760 struct remote_group_data
*g
= priv
;
762 if (!prefixcmp(key
, "remotes.") &&
763 !strcmp(key
+ 8, g
->name
)) {
764 /* split list by white space */
765 int space
= strcspn(value
, " \t\n");
768 string_list_append(g
->list
,
769 xstrndup(value
, space
));
771 value
+= space
+ (value
[space
] != '\0');
772 space
= strcspn(value
, " \t\n");
779 static int add_remote_or_group(const char *name
, struct string_list
*list
)
781 int prev_nr
= list
->nr
;
782 struct remote_group_data g
;
783 g
.name
= name
; g
.list
= list
;
785 git_config(get_remote_group
, &g
);
786 if (list
->nr
== prev_nr
) {
787 struct remote
*remote
;
788 if (!remote_is_configured(name
))
790 remote
= remote_get(name
);
791 string_list_append(list
, remote
->name
);
796 static int fetch_multiple(struct string_list
*list
)
799 const char *argv
[11] = { "fetch", "--append" };
803 argv
[argc
++] = "--dry-run";
805 argv
[argc
++] = "--prune";
807 argv
[argc
++] = "--update-head-ok";
809 argv
[argc
++] = "--force";
811 argv
[argc
++] = "--keep";
816 else if (verbosity
< 0)
819 if (!append
&& !dry_run
) {
820 int errcode
= truncate_fetch_head();
825 for (i
= 0; i
< list
->nr
; i
++) {
826 const char *name
= list
->items
[i
].string
;
828 argv
[argc
+ 1] = NULL
;
830 printf("Fetching %s\n", name
);
831 if (run_command_v_opt(argv
, RUN_GIT_CMD
)) {
832 error("Could not fetch %s", name
);
840 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
843 static const char **refs
= NULL
;
848 die("No remote repository specified. Please, specify either a URL or a\n"
849 "remote name from which new revisions should be fetched.");
851 transport
= transport_get(remote
, NULL
);
852 transport_set_verbosity(transport
, verbosity
, progress
);
854 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
856 set_option(TRANS_OPT_KEEP
, "yes");
858 set_option(TRANS_OPT_DEPTH
, depth
);
862 refs
= xcalloc(argc
+ 1, sizeof(const char *));
863 for (i
= 0; i
< argc
; i
++) {
864 if (!strcmp(argv
[i
], "tag")) {
868 die("You need to specify a tag name.");
869 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
870 strcpy(ref
, "refs/tags/");
871 strcat(ref
, argv
[i
]);
872 strcat(ref
, ":refs/tags/");
873 strcat(ref
, argv
[i
]);
882 sigchain_push_common(unlock_pack_on_signal
);
884 exit_code
= do_fetch(transport
,
885 parse_fetch_refspec(ref_nr
, refs
), ref_nr
);
886 transport_disconnect(transport
);
891 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
894 struct string_list list
= STRING_LIST_INIT_NODUP
;
895 struct remote
*remote
;
898 /* Record the command line for the reflog */
899 strbuf_addstr(&default_rla
, "fetch");
900 for (i
= 1; i
< argc
; i
++)
901 strbuf_addf(&default_rla
, " %s", argv
[i
]);
903 argc
= parse_options(argc
, argv
, prefix
,
904 builtin_fetch_options
, builtin_fetch_usage
, 0);
908 die("fetch --all does not take a repository argument");
910 die("fetch --all does not make sense with refspecs");
911 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
912 result
= fetch_multiple(&list
);
913 } else if (argc
== 0) {
914 /* No arguments -- use default remote */
915 remote
= remote_get(NULL
);
916 result
= fetch_one(remote
, argc
, argv
);
917 } else if (multiple
) {
918 /* All arguments are assumed to be remotes or groups */
919 for (i
= 0; i
< argc
; i
++)
920 if (!add_remote_or_group(argv
[i
], &list
))
921 die("No such remote or remote group: %s", argv
[i
]);
922 result
= fetch_multiple(&list
);
924 /* Single remote or group */
925 (void) add_remote_or_group(argv
[0], &list
);
927 /* More than one remote */
929 die("Fetching a group and specifying refspecs does not make sense");
930 result
= fetch_multiple(&list
);
932 /* Zero or one remotes */
933 remote
= remote_get(argv
[0]);
934 result
= fetch_one(remote
, argc
-1, argv
+1);
938 if (!result
&& recursive
) {
940 git_config(submodule_config
, NULL
);
941 result
= fetch_populated_submodules(recursive
== RECURSIVE_SET
);
944 /* All names were strdup()ed or strndup()ed */
945 list
.strdup_strings
= 1;
946 string_list_clear(&list
, 0);