8 #include "string-list.h"
10 #include "transport.h"
11 #include "run-command.h"
12 #include "parse-options.h"
14 #include "transport.h"
16 static const char * const builtin_fetch_usage
[] = {
17 "git fetch [<options>] [<repository> [<refspec>...]]",
18 "git fetch [<options>] <group>",
19 "git fetch --multiple [<options>] [<repository> | <group>]...",
20 "git fetch --all [<options>]",
30 static int all
, append
, dry_run
, force
, keep
, multiple
, prune
, update_head_ok
, verbosity
;
32 static int tags
= TAGS_DEFAULT
;
33 static const char *depth
;
34 static const char *upload_pack
;
35 static struct strbuf default_rla
= STRBUF_INIT
;
36 static struct transport
*transport
;
38 static struct option builtin_fetch_options
[] = {
39 OPT__VERBOSITY(&verbosity
),
40 OPT_BOOLEAN(0, "all", &all
,
41 "fetch from all remotes"),
42 OPT_BOOLEAN('a', "append", &append
,
43 "append to .git/FETCH_HEAD instead of overwriting"),
44 OPT_STRING(0, "upload-pack", &upload_pack
, "PATH",
45 "path to upload pack on remote end"),
46 OPT_BOOLEAN('f', "force", &force
,
47 "force overwrite of local branch"),
48 OPT_BOOLEAN('m', "multiple", &multiple
,
49 "fetch from multiple remotes"),
50 OPT_SET_INT('t', "tags", &tags
,
51 "fetch all tags and associated objects", TAGS_SET
),
52 OPT_SET_INT('n', NULL
, &tags
,
53 "do not fetch all tags (--no-tags)", TAGS_UNSET
),
54 OPT_BOOLEAN('p', "prune", &prune
,
55 "prune tracking branches no longer on remote"),
56 OPT_BOOLEAN(0, "dry-run", &dry_run
,
58 OPT_BOOLEAN('k', "keep", &keep
, "keep downloaded pack"),
59 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok
,
60 "allow updating of HEAD ref"),
61 OPT_BOOLEAN(0, "progress", &progress
, "force progress reporting"),
62 OPT_STRING(0, "depth", &depth
, "DEPTH",
63 "deepen history of shallow clone"),
67 static void unlock_pack(void)
70 transport_unlock_pack(transport
);
73 static void unlock_pack_on_signal(int signo
)
80 static void add_merge_config(struct ref
**head
,
81 const struct ref
*remote_refs
,
82 struct branch
*branch
,
87 for (i
= 0; i
< branch
->merge_nr
; i
++) {
88 struct ref
*rm
, **old_tail
= *tail
;
89 struct refspec refspec
;
91 for (rm
= *head
; rm
; rm
= rm
->next
) {
92 if (branch_merge_matches(branch
, i
, rm
->name
)) {
101 * Not fetched to a tracking branch? We need to fetch
102 * it anyway to allow this branch's "branch.$name.merge"
103 * to be honored by 'git pull', but we do not have to
104 * fail if branch.$name.merge is misconfigured to point
105 * at a nonexisting branch. If we were indeed called by
106 * 'git pull', it will notice the misconfiguration because
107 * there is no entry in the resulting FETCH_HEAD marked
110 memset(&refspec
, 0, sizeof(refspec
));
111 refspec
.src
= branch
->merge
[i
]->src
;
112 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
113 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
118 static void find_non_local_tags(struct transport
*transport
,
122 static struct ref
*get_ref_map(struct transport
*transport
,
123 struct refspec
*refs
, int ref_count
, int tags
,
128 struct ref
*ref_map
= NULL
;
129 struct ref
**tail
= &ref_map
;
131 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
133 if (ref_count
|| tags
== TAGS_SET
) {
134 for (i
= 0; i
< ref_count
; i
++) {
135 get_fetch_map(remote_refs
, &refs
[i
], &tail
, 0);
136 if (refs
[i
].dst
&& refs
[i
].dst
[0])
139 /* Merge everything on the command line, but not --tags */
140 for (rm
= ref_map
; rm
; rm
= rm
->next
)
142 if (tags
== TAGS_SET
)
143 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
145 /* Use the defaults */
146 struct remote
*remote
= transport
->remote
;
147 struct branch
*branch
= branch_get(NULL
);
148 int has_merge
= branch_has_merge_config(branch
);
150 (remote
->fetch_refspec_nr
||
151 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
152 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
153 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
154 if (remote
->fetch
[i
].dst
&&
155 remote
->fetch
[i
].dst
[0])
157 if (!i
&& !has_merge
&& ref_map
&&
158 !remote
->fetch
[0].pattern
)
162 * if the remote we're fetching from is the same
163 * as given in branch.<name>.remote, we add the
164 * ref given in branch.<name>.merge, too.
167 !strcmp(branch
->remote_name
, remote
->name
))
168 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
170 ref_map
= get_remote_ref(remote_refs
, "HEAD");
172 die("Couldn't find remote ref HEAD");
174 tail
= &ref_map
->next
;
177 if (tags
== TAGS_DEFAULT
&& *autotags
)
178 find_non_local_tags(transport
, &ref_map
, &tail
);
179 ref_remove_duplicates(ref_map
);
184 #define STORE_REF_ERROR_OTHER 1
185 #define STORE_REF_ERROR_DF_CONFLICT 2
187 static int s_update_ref(const char *action
,
192 char *rla
= getenv("GIT_REFLOG_ACTION");
193 static struct ref_lock
*lock
;
198 rla
= default_rla
.buf
;
199 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
200 lock
= lock_any_ref_for_update(ref
->name
,
201 check_old
? ref
->old_sha1
: NULL
, 0);
203 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
204 STORE_REF_ERROR_OTHER
;
205 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
206 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
207 STORE_REF_ERROR_OTHER
;
211 #define REFCOL_WIDTH 10
213 static int update_local_ref(struct ref
*ref
,
217 struct commit
*current
= NULL
, *updated
;
218 enum object_type type
;
219 struct branch
*current_branch
= branch_get(NULL
);
220 const char *pretty_ref
= prettify_refname(ref
->name
);
223 type
= sha1_object_info(ref
->new_sha1
, NULL
);
225 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
227 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
229 sprintf(display
, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH
,
230 "[up to date]", REFCOL_WIDTH
, remote
,
235 if (current_branch
&&
236 !strcmp(ref
->name
, current_branch
->name
) &&
237 !(update_head_ok
|| is_bare_repository()) &&
238 !is_null_sha1(ref
->old_sha1
)) {
240 * If this is the head, and it's not okay to update
241 * the head, and the old value of the head isn't empty...
243 sprintf(display
, "! %-*s %-*s -> %s (can't fetch in current branch)",
244 TRANSPORT_SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
249 if (!is_null_sha1(ref
->old_sha1
) &&
250 !prefixcmp(ref
->name
, "refs/tags/")) {
252 r
= s_update_ref("updating tag", ref
, 0);
253 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '-',
254 TRANSPORT_SUMMARY_WIDTH
, "[tag update]", REFCOL_WIDTH
, remote
,
255 pretty_ref
, r
? " (unable to update local ref)" : "");
259 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
260 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
261 if (!current
|| !updated
) {
265 if (!strncmp(ref
->name
, "refs/tags/", 10)) {
270 msg
= "storing head";
271 what
= "[new branch]";
274 r
= s_update_ref(msg
, ref
, 0);
275 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '*',
276 TRANSPORT_SUMMARY_WIDTH
, what
, REFCOL_WIDTH
, remote
, pretty_ref
,
277 r
? " (unable to update local ref)" : "");
281 if (in_merge_bases(current
, &updated
, 1)) {
284 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
285 strcat(quickref
, "..");
286 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
287 r
= s_update_ref("fast-forward", ref
, 1);
288 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : ' ',
289 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
290 pretty_ref
, r
? " (unable to update local ref)" : "");
292 } else if (force
|| ref
->force
) {
295 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
296 strcat(quickref
, "...");
297 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
298 r
= s_update_ref("forced-update", ref
, 1);
299 sprintf(display
, "%c %-*s %-*s -> %s (%s)", r
? '!' : '+',
300 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
302 r
? "unable to update local ref" : "forced update");
305 sprintf(display
, "! %-*s %-*s -> %s (non-fast-forward)",
306 TRANSPORT_SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
312 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
316 struct commit
*commit
;
317 int url_len
, i
, note_len
, shown_url
= 0, rc
= 0;
319 const char *what
, *kind
;
321 char *url
, *filename
= dry_run
? "/dev/null" : git_path("FETCH_HEAD");
323 fp
= fopen(filename
, "a");
325 return error("cannot open %s: %s\n", filename
, strerror(errno
));
328 url
= transport_anonymize_url(raw_url
);
330 url
= xstrdup("foreign");
331 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
332 struct ref
*ref
= NULL
;
335 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
336 strcpy(ref
->name
, rm
->peer_ref
->name
);
337 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
338 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
339 ref
->force
= rm
->peer_ref
->force
;
342 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
346 if (!strcmp(rm
->name
, "HEAD")) {
350 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
352 what
= rm
->name
+ 11;
354 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
356 what
= rm
->name
+ 10;
358 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
359 kind
= "remote branch";
360 what
= rm
->name
+ 13;
367 url_len
= strlen(url
);
368 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
371 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
377 note_len
+= sprintf(note
+ note_len
, "%s ",
379 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
381 note
[note_len
] = '\0';
382 fprintf(fp
, "%s\t%s\t%s",
383 sha1_to_hex(commit
? commit
->object
.sha1
:
385 rm
->merge
? "" : "not-for-merge",
387 for (i
= 0; i
< url_len
; ++i
)
395 rc
|= update_local_ref(ref
, what
, note
);
398 sprintf(note
, "* %-*s %-*s -> FETCH_HEAD",
399 TRANSPORT_SUMMARY_WIDTH
, *kind
? kind
: "branch",
400 REFCOL_WIDTH
, *what
? what
: "HEAD");
402 if (verbosity
>= 0 && !shown_url
) {
403 fprintf(stderr
, "From %.*s\n",
408 fprintf(stderr
, " %s\n", note
);
413 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
414 error("some local refs could not be updated; try running\n"
415 " 'git remote prune %s' to remove any old, conflicting "
416 "branches", remote_name
);
421 * We would want to bypass the object transfer altogether if
422 * everything we are going to fetch already exists and is connected
425 * The refs we are going to fetch are in ref_map. If running
427 * $ git rev-list --objects --stdin --not --all
429 * (feeding all the refs in ref_map on its standard input)
430 * does not error out, that means everything reachable from the
431 * refs we are going to fetch exists and is connected to some of
434 static int quickfetch(struct ref
*ref_map
)
436 struct child_process revlist
;
439 const char *argv
[] = {"rev-list",
440 "--quiet", "--objects", "--stdin", "--not", "--all", NULL
};
443 * If we are deepening a shallow clone we already have these
444 * objects reachable. Running rev-list here will return with
445 * a good (0) exit status and we'll bypass the fetch that we
446 * really need to perform. Claiming failure now will ensure
447 * we perform the network exchange to deepen our history.
455 memset(&revlist
, 0, sizeof(revlist
));
458 revlist
.no_stdout
= 1;
459 revlist
.no_stderr
= 1;
462 err
= start_command(&revlist
);
464 error("could not run rev-list");
469 * If rev-list --stdin encounters an unknown commit, it terminates,
470 * which will cause SIGPIPE in the write loop below.
472 sigchain_push(SIGPIPE
, SIG_IGN
);
474 for (ref
= ref_map
; ref
; ref
= ref
->next
) {
475 if (write_in_full(revlist
.in
, sha1_to_hex(ref
->old_sha1
), 40) < 0 ||
476 write_str_in_full(revlist
.in
, "\n") < 0) {
477 if (errno
!= EPIPE
&& errno
!= EINVAL
)
478 error("failed write to rev-list: %s", strerror(errno
));
484 if (close(revlist
.in
)) {
485 error("failed to close rev-list's stdin: %s", strerror(errno
));
489 sigchain_pop(SIGPIPE
);
491 return finish_command(&revlist
) || err
;
494 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
496 int ret
= quickfetch(ref_map
);
498 ret
= transport_fetch_refs(transport
, ref_map
);
500 ret
|= store_updated_refs(transport
->url
,
501 transport
->remote
->name
,
503 transport_unlock_pack(transport
);
507 static int prune_refs(struct transport
*transport
, struct ref
*ref_map
)
510 struct ref
*ref
, *stale_refs
= get_stale_heads(transport
->remote
, ref_map
);
511 const char *dangling_msg
= dry_run
512 ? " (%s will become dangling)\n"
513 : " (%s has become dangling)\n";
515 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
517 result
|= delete_ref(ref
->name
, NULL
, 0);
518 if (verbosity
>= 0) {
519 fprintf(stderr
, " x %-*s %-*s -> %s\n",
520 TRANSPORT_SUMMARY_WIDTH
, "[deleted]",
521 REFCOL_WIDTH
, "(none)", prettify_refname(ref
->name
));
522 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
525 free_refs(stale_refs
);
529 static int add_existing(const char *refname
, const unsigned char *sha1
,
530 int flag
, void *cbdata
)
532 struct string_list
*list
= (struct string_list
*)cbdata
;
533 struct string_list_item
*item
= string_list_insert(list
, refname
);
534 item
->util
= (void *)sha1
;
538 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
540 struct ref
*rm
= *head
;
542 if (!hashcmp(rm
->old_sha1
, sha1
))
549 static void find_non_local_tags(struct transport
*transport
,
553 struct string_list existing_refs
= STRING_LIST_INIT_NODUP
;
554 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
555 const struct ref
*ref
;
556 struct string_list_item
*item
= NULL
;
558 for_each_ref(add_existing
, &existing_refs
);
559 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
560 if (prefixcmp(ref
->name
, "refs/tags"))
564 * The peeled ref always follows the matching base
565 * ref, so if we see a peeled ref that we don't want
566 * to fetch then we can mark the ref entry in the list
567 * as one to ignore by setting util to NULL.
569 if (!suffixcmp(ref
->name
, "^{}")) {
570 if (item
&& !has_sha1_file(ref
->old_sha1
) &&
571 !will_fetch(head
, ref
->old_sha1
) &&
572 !has_sha1_file(item
->util
) &&
573 !will_fetch(head
, item
->util
))
580 * If item is non-NULL here, then we previously saw a
581 * ref not followed by a peeled reference, so we need
582 * to check if it is a lightweight tag that we want to
585 if (item
&& !has_sha1_file(item
->util
) &&
586 !will_fetch(head
, item
->util
))
591 /* skip duplicates and refs that we already have */
592 if (string_list_has_string(&remote_refs
, ref
->name
) ||
593 string_list_has_string(&existing_refs
, ref
->name
))
596 item
= string_list_insert(&remote_refs
, ref
->name
);
597 item
->util
= (void *)ref
->old_sha1
;
599 string_list_clear(&existing_refs
, 0);
602 * We may have a final lightweight tag that needs to be
603 * checked to see if it needs fetching.
605 if (item
&& !has_sha1_file(item
->util
) &&
606 !will_fetch(head
, item
->util
))
610 * For all the tags in the remote_refs string list,
611 * add them to the list of refs to be fetched
613 for_each_string_list_item(item
, &remote_refs
) {
614 /* Unless we have already decided to ignore this item... */
617 struct ref
*rm
= alloc_ref(item
->string
);
618 rm
->peer_ref
= alloc_ref(item
->string
);
619 hashcpy(rm
->old_sha1
, item
->util
);
625 string_list_clear(&remote_refs
, 0);
628 static void check_not_current_branch(struct ref
*ref_map
)
630 struct branch
*current_branch
= branch_get(NULL
);
632 if (is_bare_repository() || !current_branch
)
635 for (; ref_map
; ref_map
= ref_map
->next
)
636 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
637 ref_map
->peer_ref
->name
))
638 die("Refusing to fetch into current branch %s "
639 "of non-bare repository", current_branch
->refname
);
642 static int truncate_fetch_head(void)
644 char *filename
= git_path("FETCH_HEAD");
645 FILE *fp
= fopen(filename
, "w");
648 return error("cannot open %s: %s\n", filename
, strerror(errno
));
653 static int do_fetch(struct transport
*transport
,
654 struct refspec
*refs
, int ref_count
)
656 struct string_list existing_refs
= STRING_LIST_INIT_NODUP
;
657 struct string_list_item
*peer_item
= NULL
;
660 int autotags
= (transport
->remote
->fetch_tags
== 1);
662 for_each_ref(add_existing
, &existing_refs
);
664 if (tags
== TAGS_DEFAULT
) {
665 if (transport
->remote
->fetch_tags
== 2)
667 if (transport
->remote
->fetch_tags
== -1)
671 if (!transport
->get_refs_list
|| !transport
->fetch
)
672 die("Don't know how to fetch from %s", transport
->url
);
674 /* if not appending, truncate FETCH_HEAD */
675 if (!append
&& !dry_run
) {
676 int errcode
= truncate_fetch_head();
681 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
683 check_not_current_branch(ref_map
);
685 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
687 peer_item
= string_list_lookup(&existing_refs
,
690 hashcpy(rm
->peer_ref
->old_sha1
,
695 if (tags
== TAGS_DEFAULT
&& autotags
)
696 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
697 if (fetch_refs(transport
, ref_map
)) {
702 prune_refs(transport
, ref_map
);
705 /* if neither --no-tags nor --tags was specified, do automated tag
707 if (tags
== TAGS_DEFAULT
&& autotags
) {
708 struct ref
**tail
= &ref_map
;
710 find_non_local_tags(transport
, &ref_map
, &tail
);
712 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
713 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
714 fetch_refs(transport
, ref_map
);
722 static void set_option(const char *name
, const char *value
)
724 int r
= transport_set_option(transport
, name
, value
);
726 die("Option \"%s\" value \"%s\" is not valid for %s",
727 name
, value
, transport
->url
);
729 warning("Option \"%s\" is ignored for %s\n",
730 name
, transport
->url
);
733 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
735 struct string_list
*list
= priv
;
736 if (!remote
->skip_default_update
)
737 string_list_append(list
, remote
->name
);
741 struct remote_group_data
{
743 struct string_list
*list
;
746 static int get_remote_group(const char *key
, const char *value
, void *priv
)
748 struct remote_group_data
*g
= priv
;
750 if (!prefixcmp(key
, "remotes.") &&
751 !strcmp(key
+ 8, g
->name
)) {
752 /* split list by white space */
753 int space
= strcspn(value
, " \t\n");
756 string_list_append(g
->list
,
757 xstrndup(value
, space
));
759 value
+= space
+ (value
[space
] != '\0');
760 space
= strcspn(value
, " \t\n");
767 static int add_remote_or_group(const char *name
, struct string_list
*list
)
769 int prev_nr
= list
->nr
;
770 struct remote_group_data g
;
771 g
.name
= name
; g
.list
= list
;
773 git_config(get_remote_group
, &g
);
774 if (list
->nr
== prev_nr
) {
775 struct remote
*remote
;
776 if (!remote_is_configured(name
))
778 remote
= remote_get(name
);
779 string_list_append(list
, remote
->name
);
784 static int fetch_multiple(struct string_list
*list
)
787 const char *argv
[11] = { "fetch", "--append" };
791 argv
[argc
++] = "--dry-run";
793 argv
[argc
++] = "--prune";
795 argv
[argc
++] = "--update-head-ok";
797 argv
[argc
++] = "--force";
799 argv
[argc
++] = "--keep";
804 else if (verbosity
< 0)
807 if (!append
&& !dry_run
) {
808 int errcode
= truncate_fetch_head();
813 for (i
= 0; i
< list
->nr
; i
++) {
814 const char *name
= list
->items
[i
].string
;
816 argv
[argc
+ 1] = NULL
;
818 printf("Fetching %s\n", name
);
819 if (run_command_v_opt(argv
, RUN_GIT_CMD
)) {
820 error("Could not fetch %s", name
);
828 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
831 static const char **refs
= NULL
;
836 die("No remote repository specified. Please, specify either a URL or a\n"
837 "remote name from which new revisions should be fetched.");
839 transport
= transport_get(remote
, NULL
);
840 transport_set_verbosity(transport
, verbosity
, progress
);
842 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
844 set_option(TRANS_OPT_KEEP
, "yes");
846 set_option(TRANS_OPT_DEPTH
, depth
);
850 refs
= xcalloc(argc
+ 1, sizeof(const char *));
851 for (i
= 0; i
< argc
; i
++) {
852 if (!strcmp(argv
[i
], "tag")) {
856 die("You need to specify a tag name.");
857 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
858 strcpy(ref
, "refs/tags/");
859 strcat(ref
, argv
[i
]);
860 strcat(ref
, ":refs/tags/");
861 strcat(ref
, argv
[i
]);
870 sigchain_push_common(unlock_pack_on_signal
);
872 exit_code
= do_fetch(transport
,
873 parse_fetch_refspec(ref_nr
, refs
), ref_nr
);
874 transport_disconnect(transport
);
879 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
882 struct string_list list
= STRING_LIST_INIT_NODUP
;
883 struct remote
*remote
;
886 /* Record the command line for the reflog */
887 strbuf_addstr(&default_rla
, "fetch");
888 for (i
= 1; i
< argc
; i
++)
889 strbuf_addf(&default_rla
, " %s", argv
[i
]);
891 argc
= parse_options(argc
, argv
, prefix
,
892 builtin_fetch_options
, builtin_fetch_usage
, 0);
896 die("fetch --all does not take a repository argument");
898 die("fetch --all does not make sense with refspecs");
899 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
900 result
= fetch_multiple(&list
);
901 } else if (argc
== 0) {
902 /* No arguments -- use default remote */
903 remote
= remote_get(NULL
);
904 result
= fetch_one(remote
, argc
, argv
);
905 } else if (multiple
) {
906 /* All arguments are assumed to be remotes or groups */
907 for (i
= 0; i
< argc
; i
++)
908 if (!add_remote_or_group(argv
[i
], &list
))
909 die("No such remote or remote group: %s", argv
[i
]);
910 result
= fetch_multiple(&list
);
912 /* Single remote or group */
913 (void) add_remote_or_group(argv
[0], &list
);
915 /* More than one remote */
917 die("Fetching a group and specifying refspecs does not make sense");
918 result
= fetch_multiple(&list
);
920 /* Zero or one remotes */
921 remote
= remote_get(argv
[0]);
922 result
= fetch_one(remote
, argc
-1, argv
+1);
926 /* All names were strdup()ed or strndup()ed */
927 list
.strdup_strings
= 1;
928 string_list_clear(&list
, 0);