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__FORCE(&force
, "force overwrite of local branch"),
47 OPT_BOOLEAN('m', "multiple", &multiple
,
48 "fetch from multiple remotes"),
49 OPT_SET_INT('t', "tags", &tags
,
50 "fetch all tags and associated objects", TAGS_SET
),
51 OPT_SET_INT('n', NULL
, &tags
,
52 "do not fetch all tags (--no-tags)", TAGS_UNSET
),
53 OPT_BOOLEAN('p', "prune", &prune
,
54 "prune tracking branches no longer on remote"),
55 OPT_BOOLEAN(0, "dry-run", &dry_run
,
57 OPT_BOOLEAN('k', "keep", &keep
, "keep downloaded pack"),
58 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok
,
59 "allow updating of HEAD ref"),
60 OPT_BOOLEAN(0, "progress", &progress
, "force progress reporting"),
61 OPT_STRING(0, "depth", &depth
, "DEPTH",
62 "deepen history of shallow clone"),
66 static void unlock_pack(void)
69 transport_unlock_pack(transport
);
72 static void unlock_pack_on_signal(int signo
)
79 static void add_merge_config(struct ref
**head
,
80 const struct ref
*remote_refs
,
81 struct branch
*branch
,
86 for (i
= 0; i
< branch
->merge_nr
; i
++) {
87 struct ref
*rm
, **old_tail
= *tail
;
88 struct refspec refspec
;
90 for (rm
= *head
; rm
; rm
= rm
->next
) {
91 if (branch_merge_matches(branch
, i
, rm
->name
)) {
100 * Not fetched to a tracking branch? We need to fetch
101 * it anyway to allow this branch's "branch.$name.merge"
102 * to be honored by 'git pull', but we do not have to
103 * fail if branch.$name.merge is misconfigured to point
104 * at a nonexisting branch. If we were indeed called by
105 * 'git pull', it will notice the misconfiguration because
106 * there is no entry in the resulting FETCH_HEAD marked
109 memset(&refspec
, 0, sizeof(refspec
));
110 refspec
.src
= branch
->merge
[i
]->src
;
111 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
112 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
117 static void find_non_local_tags(struct transport
*transport
,
121 static struct ref
*get_ref_map(struct transport
*transport
,
122 struct refspec
*refs
, int ref_count
, int tags
,
127 struct ref
*ref_map
= NULL
;
128 struct ref
**tail
= &ref_map
;
130 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
132 if (ref_count
|| tags
== TAGS_SET
) {
133 for (i
= 0; i
< ref_count
; i
++) {
134 get_fetch_map(remote_refs
, &refs
[i
], &tail
, 0);
135 if (refs
[i
].dst
&& refs
[i
].dst
[0])
138 /* Merge everything on the command line, but not --tags */
139 for (rm
= ref_map
; rm
; rm
= rm
->next
)
141 if (tags
== TAGS_SET
)
142 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
144 /* Use the defaults */
145 struct remote
*remote
= transport
->remote
;
146 struct branch
*branch
= branch_get(NULL
);
147 int has_merge
= branch_has_merge_config(branch
);
149 (remote
->fetch_refspec_nr
||
150 /* Note: has_merge implies non-NULL branch->remote_name */
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.
166 * Note: has_merge implies non-NULL branch->remote_name
169 !strcmp(branch
->remote_name
, remote
->name
))
170 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
172 ref_map
= get_remote_ref(remote_refs
, "HEAD");
174 die("Couldn't find remote ref HEAD");
176 tail
= &ref_map
->next
;
179 if (tags
== TAGS_DEFAULT
&& *autotags
)
180 find_non_local_tags(transport
, &ref_map
, &tail
);
181 ref_remove_duplicates(ref_map
);
186 #define STORE_REF_ERROR_OTHER 1
187 #define STORE_REF_ERROR_DF_CONFLICT 2
189 static int s_update_ref(const char *action
,
194 char *rla
= getenv("GIT_REFLOG_ACTION");
195 static struct ref_lock
*lock
;
200 rla
= default_rla
.buf
;
201 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
202 lock
= lock_any_ref_for_update(ref
->name
,
203 check_old
? ref
->old_sha1
: NULL
, 0);
205 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
206 STORE_REF_ERROR_OTHER
;
207 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
208 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
209 STORE_REF_ERROR_OTHER
;
213 #define REFCOL_WIDTH 10
215 static int update_local_ref(struct ref
*ref
,
219 struct commit
*current
= NULL
, *updated
;
220 enum object_type type
;
221 struct branch
*current_branch
= branch_get(NULL
);
222 const char *pretty_ref
= prettify_refname(ref
->name
);
225 type
= sha1_object_info(ref
->new_sha1
, NULL
);
227 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
229 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
231 sprintf(display
, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH
,
232 "[up to date]", REFCOL_WIDTH
, remote
,
237 if (current_branch
&&
238 !strcmp(ref
->name
, current_branch
->name
) &&
239 !(update_head_ok
|| is_bare_repository()) &&
240 !is_null_sha1(ref
->old_sha1
)) {
242 * If this is the head, and it's not okay to update
243 * the head, and the old value of the head isn't empty...
245 sprintf(display
, "! %-*s %-*s -> %s (can't fetch in current branch)",
246 TRANSPORT_SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
251 if (!is_null_sha1(ref
->old_sha1
) &&
252 !prefixcmp(ref
->name
, "refs/tags/")) {
254 r
= s_update_ref("updating tag", ref
, 0);
255 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '-',
256 TRANSPORT_SUMMARY_WIDTH
, "[tag update]", REFCOL_WIDTH
, remote
,
257 pretty_ref
, r
? " (unable to update local ref)" : "");
261 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
262 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
263 if (!current
|| !updated
) {
267 if (!strncmp(ref
->name
, "refs/tags/", 10)) {
272 msg
= "storing head";
273 what
= "[new branch]";
276 r
= s_update_ref(msg
, ref
, 0);
277 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '*',
278 TRANSPORT_SUMMARY_WIDTH
, what
, REFCOL_WIDTH
, remote
, pretty_ref
,
279 r
? " (unable to update local ref)" : "");
283 if (in_merge_bases(current
, &updated
, 1)) {
286 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
287 strcat(quickref
, "..");
288 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
289 r
= s_update_ref("fast-forward", ref
, 1);
290 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : ' ',
291 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
292 pretty_ref
, r
? " (unable to update local ref)" : "");
294 } else if (force
|| ref
->force
) {
297 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
298 strcat(quickref
, "...");
299 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
300 r
= s_update_ref("forced-update", ref
, 1);
301 sprintf(display
, "%c %-*s %-*s -> %s (%s)", r
? '!' : '+',
302 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
304 r
? "unable to update local ref" : "forced update");
307 sprintf(display
, "! %-*s %-*s -> %s (non-fast-forward)",
308 TRANSPORT_SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
314 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
318 struct commit
*commit
;
319 int url_len
, i
, note_len
, shown_url
= 0, rc
= 0;
321 const char *what
, *kind
;
323 char *url
, *filename
= dry_run
? "/dev/null" : git_path("FETCH_HEAD");
325 fp
= fopen(filename
, "a");
327 return error("cannot open %s: %s\n", filename
, strerror(errno
));
330 url
= transport_anonymize_url(raw_url
);
332 url
= xstrdup("foreign");
333 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
334 struct ref
*ref
= NULL
;
337 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
338 strcpy(ref
->name
, rm
->peer_ref
->name
);
339 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
340 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
341 ref
->force
= rm
->peer_ref
->force
;
344 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
348 if (!strcmp(rm
->name
, "HEAD")) {
352 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
354 what
= rm
->name
+ 11;
356 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
358 what
= rm
->name
+ 10;
360 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
361 kind
= "remote branch";
362 what
= rm
->name
+ 13;
369 url_len
= strlen(url
);
370 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
373 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
379 note_len
+= sprintf(note
+ note_len
, "%s ",
381 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
383 note
[note_len
] = '\0';
384 fprintf(fp
, "%s\t%s\t%s",
385 sha1_to_hex(commit
? commit
->object
.sha1
:
387 rm
->merge
? "" : "not-for-merge",
389 for (i
= 0; i
< url_len
; ++i
)
397 rc
|= update_local_ref(ref
, what
, note
);
400 sprintf(note
, "* %-*s %-*s -> FETCH_HEAD",
401 TRANSPORT_SUMMARY_WIDTH
, *kind
? kind
: "branch",
402 REFCOL_WIDTH
, *what
? what
: "HEAD");
404 if (verbosity
>= 0 && !shown_url
) {
405 fprintf(stderr
, "From %.*s\n",
410 fprintf(stderr
, " %s\n", note
);
415 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
416 error("some local refs could not be updated; try running\n"
417 " 'git remote prune %s' to remove any old, conflicting "
418 "branches", remote_name
);
423 * We would want to bypass the object transfer altogether if
424 * everything we are going to fetch already exists and is connected
427 * The refs we are going to fetch are in ref_map. If running
429 * $ git rev-list --objects --stdin --not --all
431 * (feeding all the refs in ref_map on its standard input)
432 * does not error out, that means everything reachable from the
433 * refs we are going to fetch exists and is connected to some of
436 static int quickfetch(struct ref
*ref_map
)
438 struct child_process revlist
;
441 const char *argv
[] = {"rev-list",
442 "--quiet", "--objects", "--stdin", "--not", "--all", NULL
};
445 * If we are deepening a shallow clone we already have these
446 * objects reachable. Running rev-list here will return with
447 * a good (0) exit status and we'll bypass the fetch that we
448 * really need to perform. Claiming failure now will ensure
449 * we perform the network exchange to deepen our history.
457 memset(&revlist
, 0, sizeof(revlist
));
460 revlist
.no_stdout
= 1;
461 revlist
.no_stderr
= 1;
464 err
= start_command(&revlist
);
466 error("could not run rev-list");
471 * If rev-list --stdin encounters an unknown commit, it terminates,
472 * which will cause SIGPIPE in the write loop below.
474 sigchain_push(SIGPIPE
, SIG_IGN
);
476 for (ref
= ref_map
; ref
; ref
= ref
->next
) {
477 if (write_in_full(revlist
.in
, sha1_to_hex(ref
->old_sha1
), 40) < 0 ||
478 write_str_in_full(revlist
.in
, "\n") < 0) {
479 if (errno
!= EPIPE
&& errno
!= EINVAL
)
480 error("failed write to rev-list: %s", strerror(errno
));
486 if (close(revlist
.in
)) {
487 error("failed to close rev-list's stdin: %s", strerror(errno
));
491 sigchain_pop(SIGPIPE
);
493 return finish_command(&revlist
) || err
;
496 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
498 int ret
= quickfetch(ref_map
);
500 ret
= transport_fetch_refs(transport
, ref_map
);
502 ret
|= store_updated_refs(transport
->url
,
503 transport
->remote
->name
,
505 transport_unlock_pack(transport
);
509 static int prune_refs(struct transport
*transport
, struct ref
*ref_map
)
512 struct ref
*ref
, *stale_refs
= get_stale_heads(transport
->remote
, ref_map
);
513 const char *dangling_msg
= dry_run
514 ? " (%s will become dangling)\n"
515 : " (%s has become dangling)\n";
517 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
519 result
|= delete_ref(ref
->name
, NULL
, 0);
520 if (verbosity
>= 0) {
521 fprintf(stderr
, " x %-*s %-*s -> %s\n",
522 TRANSPORT_SUMMARY_WIDTH
, "[deleted]",
523 REFCOL_WIDTH
, "(none)", prettify_refname(ref
->name
));
524 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
527 free_refs(stale_refs
);
531 static int add_existing(const char *refname
, const unsigned char *sha1
,
532 int flag
, void *cbdata
)
534 struct string_list
*list
= (struct string_list
*)cbdata
;
535 struct string_list_item
*item
= string_list_insert(list
, refname
);
536 item
->util
= (void *)sha1
;
540 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
542 struct ref
*rm
= *head
;
544 if (!hashcmp(rm
->old_sha1
, sha1
))
551 static void find_non_local_tags(struct transport
*transport
,
555 struct string_list existing_refs
= STRING_LIST_INIT_NODUP
;
556 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
557 const struct ref
*ref
;
558 struct string_list_item
*item
= NULL
;
560 for_each_ref(add_existing
, &existing_refs
);
561 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
562 if (prefixcmp(ref
->name
, "refs/tags"))
566 * The peeled ref always follows the matching base
567 * ref, so if we see a peeled ref that we don't want
568 * to fetch then we can mark the ref entry in the list
569 * as one to ignore by setting util to NULL.
571 if (!suffixcmp(ref
->name
, "^{}")) {
572 if (item
&& !has_sha1_file(ref
->old_sha1
) &&
573 !will_fetch(head
, ref
->old_sha1
) &&
574 !has_sha1_file(item
->util
) &&
575 !will_fetch(head
, item
->util
))
582 * If item is non-NULL here, then we previously saw a
583 * ref not followed by a peeled reference, so we need
584 * to check if it is a lightweight tag that we want to
587 if (item
&& !has_sha1_file(item
->util
) &&
588 !will_fetch(head
, item
->util
))
593 /* skip duplicates and refs that we already have */
594 if (string_list_has_string(&remote_refs
, ref
->name
) ||
595 string_list_has_string(&existing_refs
, ref
->name
))
598 item
= string_list_insert(&remote_refs
, ref
->name
);
599 item
->util
= (void *)ref
->old_sha1
;
601 string_list_clear(&existing_refs
, 0);
604 * We may have a final lightweight tag that needs to be
605 * checked to see if it needs fetching.
607 if (item
&& !has_sha1_file(item
->util
) &&
608 !will_fetch(head
, item
->util
))
612 * For all the tags in the remote_refs string list,
613 * add them to the list of refs to be fetched
615 for_each_string_list_item(item
, &remote_refs
) {
616 /* Unless we have already decided to ignore this item... */
619 struct ref
*rm
= alloc_ref(item
->string
);
620 rm
->peer_ref
= alloc_ref(item
->string
);
621 hashcpy(rm
->old_sha1
, item
->util
);
627 string_list_clear(&remote_refs
, 0);
630 static void check_not_current_branch(struct ref
*ref_map
)
632 struct branch
*current_branch
= branch_get(NULL
);
634 if (is_bare_repository() || !current_branch
)
637 for (; ref_map
; ref_map
= ref_map
->next
)
638 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
639 ref_map
->peer_ref
->name
))
640 die("Refusing to fetch into current branch %s "
641 "of non-bare repository", current_branch
->refname
);
644 static int truncate_fetch_head(void)
646 char *filename
= git_path("FETCH_HEAD");
647 FILE *fp
= fopen(filename
, "w");
650 return error("cannot open %s: %s\n", filename
, strerror(errno
));
655 static int do_fetch(struct transport
*transport
,
656 struct refspec
*refs
, int ref_count
)
658 struct string_list existing_refs
= STRING_LIST_INIT_NODUP
;
659 struct string_list_item
*peer_item
= NULL
;
662 int autotags
= (transport
->remote
->fetch_tags
== 1);
664 for_each_ref(add_existing
, &existing_refs
);
666 if (tags
== TAGS_DEFAULT
) {
667 if (transport
->remote
->fetch_tags
== 2)
669 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 int fetch_multiple(struct string_list
*list
)
789 const char *argv
[11] = { "fetch", "--append" };
793 argv
[argc
++] = "--dry-run";
795 argv
[argc
++] = "--prune";
797 argv
[argc
++] = "--update-head-ok";
799 argv
[argc
++] = "--force";
801 argv
[argc
++] = "--keep";
806 else if (verbosity
< 0)
809 if (!append
&& !dry_run
) {
810 int errcode
= truncate_fetch_head();
815 for (i
= 0; i
< list
->nr
; i
++) {
816 const char *name
= list
->items
[i
].string
;
818 argv
[argc
+ 1] = NULL
;
820 printf("Fetching %s\n", name
);
821 if (run_command_v_opt(argv
, RUN_GIT_CMD
)) {
822 error("Could not fetch %s", name
);
830 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
833 static const char **refs
= NULL
;
838 die("No remote repository specified. Please, specify either a URL or a\n"
839 "remote name from which new revisions should be fetched.");
841 transport
= transport_get(remote
, NULL
);
842 transport_set_verbosity(transport
, verbosity
, progress
);
844 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
846 set_option(TRANS_OPT_KEEP
, "yes");
848 set_option(TRANS_OPT_DEPTH
, depth
);
852 refs
= xcalloc(argc
+ 1, sizeof(const char *));
853 for (i
= 0; i
< argc
; i
++) {
854 if (!strcmp(argv
[i
], "tag")) {
858 die("You need to specify a tag name.");
859 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
860 strcpy(ref
, "refs/tags/");
861 strcat(ref
, argv
[i
]);
862 strcat(ref
, ":refs/tags/");
863 strcat(ref
, argv
[i
]);
872 sigchain_push_common(unlock_pack_on_signal
);
874 exit_code
= do_fetch(transport
,
875 parse_fetch_refspec(ref_nr
, refs
), ref_nr
);
876 transport_disconnect(transport
);
881 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
884 struct string_list list
= STRING_LIST_INIT_NODUP
;
885 struct remote
*remote
;
888 /* Record the command line for the reflog */
889 strbuf_addstr(&default_rla
, "fetch");
890 for (i
= 1; i
< argc
; i
++)
891 strbuf_addf(&default_rla
, " %s", argv
[i
]);
893 argc
= parse_options(argc
, argv
, prefix
,
894 builtin_fetch_options
, builtin_fetch_usage
, 0);
898 die("fetch --all does not take a repository argument");
900 die("fetch --all does not make sense with refspecs");
901 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
902 result
= fetch_multiple(&list
);
903 } else if (argc
== 0) {
904 /* No arguments -- use default remote */
905 remote
= remote_get(NULL
);
906 result
= fetch_one(remote
, argc
, argv
);
907 } else if (multiple
) {
908 /* All arguments are assumed to be remotes or groups */
909 for (i
= 0; i
< argc
; i
++)
910 if (!add_remote_or_group(argv
[i
], &list
))
911 die("No such remote or remote group: %s", argv
[i
]);
912 result
= fetch_multiple(&list
);
914 /* Single remote or group */
915 (void) add_remote_or_group(argv
[0], &list
);
917 /* More than one remote */
919 die("Fetching a group and specifying refspecs does not make sense");
920 result
= fetch_multiple(&list
);
922 /* Zero or one remotes */
923 remote
= remote_get(argv
[0]);
924 result
= fetch_one(remote
, argc
-1, argv
+1);
928 /* All names were strdup()ed or strndup()ed */
929 list
.strdup_strings
= 1;
930 string_list_clear(&list
, 0);