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
);
149 if (remote
&& (remote
->fetch_refspec_nr
|| has_merge
)) {
150 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
151 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
152 if (remote
->fetch
[i
].dst
&&
153 remote
->fetch
[i
].dst
[0])
155 if (!i
&& !has_merge
&& ref_map
&&
156 !remote
->fetch
[0].pattern
)
160 * if the remote we're fetching from is the same
161 * as given in branch.<name>.remote, we add the
162 * ref given in branch.<name>.merge, too.
165 !strcmp(branch
->remote_name
, remote
->name
))
166 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
168 ref_map
= get_remote_ref(remote_refs
, "HEAD");
170 die("Couldn't find remote ref HEAD");
172 tail
= &ref_map
->next
;
175 if (tags
== TAGS_DEFAULT
&& *autotags
)
176 find_non_local_tags(transport
, &ref_map
, &tail
);
177 ref_remove_duplicates(ref_map
);
182 #define STORE_REF_ERROR_OTHER 1
183 #define STORE_REF_ERROR_DF_CONFLICT 2
185 static int s_update_ref(const char *action
,
190 char *rla
= getenv("GIT_REFLOG_ACTION");
191 static struct ref_lock
*lock
;
196 rla
= default_rla
.buf
;
197 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
198 lock
= lock_any_ref_for_update(ref
->name
,
199 check_old
? ref
->old_sha1
: NULL
, 0);
201 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
202 STORE_REF_ERROR_OTHER
;
203 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
204 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
205 STORE_REF_ERROR_OTHER
;
209 #define REFCOL_WIDTH 10
211 static int update_local_ref(struct ref
*ref
,
215 struct commit
*current
= NULL
, *updated
;
216 enum object_type type
;
217 struct branch
*current_branch
= branch_get(NULL
);
218 const char *pretty_ref
= prettify_refname(ref
->name
);
221 type
= sha1_object_info(ref
->new_sha1
, NULL
);
223 die("object %s not found", sha1_to_hex(ref
->new_sha1
));
225 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
227 sprintf(display
, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH
,
228 "[up to date]", REFCOL_WIDTH
, remote
,
233 if (current_branch
&&
234 !strcmp(ref
->name
, current_branch
->name
) &&
235 !(update_head_ok
|| is_bare_repository()) &&
236 !is_null_sha1(ref
->old_sha1
)) {
238 * If this is the head, and it's not okay to update
239 * the head, and the old value of the head isn't empty...
241 sprintf(display
, "! %-*s %-*s -> %s (can't fetch in current branch)",
242 TRANSPORT_SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
247 if (!is_null_sha1(ref
->old_sha1
) &&
248 !prefixcmp(ref
->name
, "refs/tags/")) {
250 r
= s_update_ref("updating tag", ref
, 0);
251 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '-',
252 TRANSPORT_SUMMARY_WIDTH
, "[tag update]", REFCOL_WIDTH
, remote
,
253 pretty_ref
, r
? " (unable to update local ref)" : "");
257 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
258 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
259 if (!current
|| !updated
) {
263 if (!strncmp(ref
->name
, "refs/tags/", 10)) {
268 msg
= "storing head";
269 what
= "[new branch]";
272 r
= s_update_ref(msg
, ref
, 0);
273 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '*',
274 TRANSPORT_SUMMARY_WIDTH
, what
, REFCOL_WIDTH
, remote
, pretty_ref
,
275 r
? " (unable to update local ref)" : "");
279 if (in_merge_bases(current
, &updated
, 1)) {
282 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
283 strcat(quickref
, "..");
284 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
285 r
= s_update_ref("fast-forward", ref
, 1);
286 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : ' ',
287 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
288 pretty_ref
, r
? " (unable to update local ref)" : "");
290 } else if (force
|| ref
->force
) {
293 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
294 strcat(quickref
, "...");
295 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
296 r
= s_update_ref("forced-update", ref
, 1);
297 sprintf(display
, "%c %-*s %-*s -> %s (%s)", r
? '!' : '+',
298 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
300 r
? "unable to update local ref" : "forced update");
303 sprintf(display
, "! %-*s %-*s -> %s (non-fast-forward)",
304 TRANSPORT_SUMMARY_WIDTH
, "[rejected]", REFCOL_WIDTH
, remote
,
310 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
314 struct commit
*commit
;
315 int url_len
, i
, note_len
, shown_url
= 0, rc
= 0;
317 const char *what
, *kind
;
319 char *url
, *filename
= dry_run
? "/dev/null" : git_path("FETCH_HEAD");
321 fp
= fopen(filename
, "a");
323 return error("cannot open %s: %s\n", filename
, strerror(errno
));
326 url
= transport_anonymize_url(raw_url
);
328 url
= xstrdup("foreign");
329 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
330 struct ref
*ref
= NULL
;
333 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
334 strcpy(ref
->name
, rm
->peer_ref
->name
);
335 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
336 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
337 ref
->force
= rm
->peer_ref
->force
;
340 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
344 if (!strcmp(rm
->name
, "HEAD")) {
348 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
350 what
= rm
->name
+ 11;
352 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
354 what
= rm
->name
+ 10;
356 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
357 kind
= "remote branch";
358 what
= rm
->name
+ 13;
365 url_len
= strlen(url
);
366 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
369 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
375 note_len
+= sprintf(note
+ note_len
, "%s ",
377 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
379 note
[note_len
] = '\0';
380 fprintf(fp
, "%s\t%s\t%s",
381 sha1_to_hex(commit
? commit
->object
.sha1
:
383 rm
->merge
? "" : "not-for-merge",
385 for (i
= 0; i
< url_len
; ++i
)
393 rc
|= update_local_ref(ref
, what
, note
);
396 sprintf(note
, "* %-*s %-*s -> FETCH_HEAD",
397 TRANSPORT_SUMMARY_WIDTH
, *kind
? kind
: "branch",
398 REFCOL_WIDTH
, *what
? what
: "HEAD");
400 if (verbosity
>= 0 && !shown_url
) {
401 fprintf(stderr
, "From %.*s\n",
406 fprintf(stderr
, " %s\n", note
);
411 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
412 error("some local refs could not be updated; try running\n"
413 " 'git remote prune %s' to remove any old, conflicting "
414 "branches", remote_name
);
419 * We would want to bypass the object transfer altogether if
420 * everything we are going to fetch already exists and is connected
423 * The refs we are going to fetch are in ref_map. If running
425 * $ git rev-list --objects --stdin --not --all
427 * (feeding all the refs in ref_map on its standard input)
428 * does not error out, that means everything reachable from the
429 * refs we are going to fetch exists and is connected to some of
432 static int quickfetch(struct ref
*ref_map
)
434 struct child_process revlist
;
437 const char *argv
[] = {"rev-list",
438 "--quiet", "--objects", "--stdin", "--not", "--all", NULL
};
441 * If we are deepening a shallow clone we already have these
442 * objects reachable. Running rev-list here will return with
443 * a good (0) exit status and we'll bypass the fetch that we
444 * really need to perform. Claiming failure now will ensure
445 * we perform the network exchange to deepen our history.
453 memset(&revlist
, 0, sizeof(revlist
));
456 revlist
.no_stdout
= 1;
457 revlist
.no_stderr
= 1;
460 err
= start_command(&revlist
);
462 error("could not run rev-list");
467 * If rev-list --stdin encounters an unknown commit, it terminates,
468 * which will cause SIGPIPE in the write loop below.
470 sigchain_push(SIGPIPE
, SIG_IGN
);
472 for (ref
= ref_map
; ref
; ref
= ref
->next
) {
473 if (write_in_full(revlist
.in
, sha1_to_hex(ref
->old_sha1
), 40) < 0 ||
474 write_str_in_full(revlist
.in
, "\n") < 0) {
475 if (errno
!= EPIPE
&& errno
!= EINVAL
)
476 error("failed write to rev-list: %s", strerror(errno
));
482 if (close(revlist
.in
)) {
483 error("failed to close rev-list's stdin: %s", strerror(errno
));
487 sigchain_pop(SIGPIPE
);
489 return finish_command(&revlist
) || err
;
492 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
494 int ret
= quickfetch(ref_map
);
496 ret
= transport_fetch_refs(transport
, ref_map
);
498 ret
|= store_updated_refs(transport
->url
,
499 transport
->remote
->name
,
501 transport_unlock_pack(transport
);
505 static int prune_refs(struct transport
*transport
, struct ref
*ref_map
)
508 struct ref
*ref
, *stale_refs
= get_stale_heads(transport
->remote
, ref_map
);
509 const char *dangling_msg
= dry_run
510 ? " (%s will become dangling)\n"
511 : " (%s has become dangling)\n";
513 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
515 result
|= delete_ref(ref
->name
, NULL
, 0);
516 if (verbosity
>= 0) {
517 fprintf(stderr
, " x %-*s %-*s -> %s\n",
518 TRANSPORT_SUMMARY_WIDTH
, "[deleted]",
519 REFCOL_WIDTH
, "(none)", prettify_refname(ref
->name
));
520 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
523 free_refs(stale_refs
);
527 static int add_existing(const char *refname
, const unsigned char *sha1
,
528 int flag
, void *cbdata
)
530 struct string_list
*list
= (struct string_list
*)cbdata
;
531 struct string_list_item
*item
= string_list_insert(list
, refname
);
532 item
->util
= (void *)sha1
;
536 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
538 struct ref
*rm
= *head
;
540 if (!hashcmp(rm
->old_sha1
, sha1
))
552 static int add_to_tail(struct string_list_item
*item
, void *cb_data
)
554 struct tag_data
*data
= (struct tag_data
*)cb_data
;
555 struct ref
*rm
= NULL
;
557 /* We have already decided to ignore this item */
561 rm
= alloc_ref(item
->string
);
562 rm
->peer_ref
= alloc_ref(item
->string
);
563 hashcpy(rm
->old_sha1
, item
->util
);
566 *data
->tail
= &rm
->next
;
571 static void find_non_local_tags(struct transport
*transport
,
575 struct string_list existing_refs
= { NULL
, 0, 0, 0 };
576 struct string_list remote_refs
= { NULL
, 0, 0, 0 };
577 struct tag_data data
;
578 const struct ref
*ref
;
579 struct string_list_item
*item
= NULL
;
580 data
.head
= head
; data
.tail
= tail
;
582 for_each_ref(add_existing
, &existing_refs
);
583 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
584 if (prefixcmp(ref
->name
, "refs/tags"))
588 * The peeled ref always follows the matching base
589 * ref, so if we see a peeled ref that we don't want
590 * to fetch then we can mark the ref entry in the list
591 * as one to ignore by setting util to NULL.
593 if (!suffixcmp(ref
->name
, "^{}")) {
594 if (item
&& !has_sha1_file(ref
->old_sha1
) &&
595 !will_fetch(head
, ref
->old_sha1
) &&
596 !has_sha1_file(item
->util
) &&
597 !will_fetch(head
, item
->util
))
604 * If item is non-NULL here, then we previously saw a
605 * ref not followed by a peeled reference, so we need
606 * to check if it is a lightweight tag that we want to
609 if (item
&& !has_sha1_file(item
->util
) &&
610 !will_fetch(head
, item
->util
))
615 /* skip duplicates and refs that we already have */
616 if (string_list_has_string(&remote_refs
, ref
->name
) ||
617 string_list_has_string(&existing_refs
, ref
->name
))
620 item
= string_list_insert(&remote_refs
, ref
->name
);
621 item
->util
= (void *)ref
->old_sha1
;
623 string_list_clear(&existing_refs
, 0);
626 * We may have a final lightweight tag that needs to be
627 * checked to see if it needs fetching.
629 if (item
&& !has_sha1_file(item
->util
) &&
630 !will_fetch(head
, item
->util
))
634 * For all the tags in the remote_refs string list, call
635 * add_to_tail to add them to the list of refs to be fetched
637 for_each_string_list(&remote_refs
, add_to_tail
, &data
);
639 string_list_clear(&remote_refs
, 0);
642 static void check_not_current_branch(struct ref
*ref_map
)
644 struct branch
*current_branch
= branch_get(NULL
);
646 if (is_bare_repository() || !current_branch
)
649 for (; ref_map
; ref_map
= ref_map
->next
)
650 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
651 ref_map
->peer_ref
->name
))
652 die("Refusing to fetch into current branch %s "
653 "of non-bare repository", current_branch
->refname
);
656 static int truncate_fetch_head(void)
658 char *filename
= git_path("FETCH_HEAD");
659 FILE *fp
= fopen(filename
, "w");
662 return error("cannot open %s: %s\n", filename
, strerror(errno
));
667 static int do_fetch(struct transport
*transport
,
668 struct refspec
*refs
, int ref_count
)
670 struct string_list existing_refs
= { NULL
, 0, 0, 0 };
671 struct string_list_item
*peer_item
= NULL
;
674 int autotags
= (transport
->remote
->fetch_tags
== 1);
676 for_each_ref(add_existing
, &existing_refs
);
678 if (tags
== TAGS_DEFAULT
) {
679 if (transport
->remote
->fetch_tags
== 2)
681 if (transport
->remote
->fetch_tags
== -1)
685 if (!transport
->get_refs_list
|| !transport
->fetch
)
686 die("Don't know how to fetch from %s", transport
->url
);
688 /* if not appending, truncate FETCH_HEAD */
689 if (!append
&& !dry_run
) {
690 int errcode
= truncate_fetch_head();
695 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
697 check_not_current_branch(ref_map
);
699 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
701 peer_item
= string_list_lookup(&existing_refs
,
704 hashcpy(rm
->peer_ref
->old_sha1
,
709 if (tags
== TAGS_DEFAULT
&& autotags
)
710 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
711 if (fetch_refs(transport
, ref_map
)) {
716 prune_refs(transport
, ref_map
);
719 /* if neither --no-tags nor --tags was specified, do automated tag
721 if (tags
== TAGS_DEFAULT
&& autotags
) {
722 struct ref
**tail
= &ref_map
;
724 find_non_local_tags(transport
, &ref_map
, &tail
);
726 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
727 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
728 fetch_refs(transport
, ref_map
);
736 static void set_option(const char *name
, const char *value
)
738 int r
= transport_set_option(transport
, name
, value
);
740 die("Option \"%s\" value \"%s\" is not valid for %s",
741 name
, value
, transport
->url
);
743 warning("Option \"%s\" is ignored for %s\n",
744 name
, transport
->url
);
747 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
749 struct string_list
*list
= priv
;
750 if (!remote
->skip_default_update
)
751 string_list_append(list
, remote
->name
);
755 struct remote_group_data
{
757 struct string_list
*list
;
760 static int get_remote_group(const char *key
, const char *value
, void *priv
)
762 struct remote_group_data
*g
= priv
;
764 if (!prefixcmp(key
, "remotes.") &&
765 !strcmp(key
+ 8, g
->name
)) {
766 /* split list by white space */
767 int space
= strcspn(value
, " \t\n");
770 string_list_append(g
->list
,
771 xstrndup(value
, space
));
773 value
+= space
+ (value
[space
] != '\0');
774 space
= strcspn(value
, " \t\n");
781 static int add_remote_or_group(const char *name
, struct string_list
*list
)
783 int prev_nr
= list
->nr
;
784 struct remote_group_data g
;
785 g
.name
= name
; g
.list
= list
;
787 git_config(get_remote_group
, &g
);
788 if (list
->nr
== prev_nr
) {
789 struct remote
*remote
;
790 if (!remote_is_configured(name
))
792 remote
= remote_get(name
);
793 string_list_append(list
, remote
->name
);
798 static int fetch_multiple(struct string_list
*list
)
801 const char *argv
[11] = { "fetch", "--append" };
805 argv
[argc
++] = "--dry-run";
807 argv
[argc
++] = "--prune";
809 argv
[argc
++] = "--update-head-ok";
811 argv
[argc
++] = "--force";
813 argv
[argc
++] = "--keep";
818 else if (verbosity
< 0)
821 if (!append
&& !dry_run
) {
822 int errcode
= truncate_fetch_head();
827 for (i
= 0; i
< list
->nr
; i
++) {
828 const char *name
= list
->items
[i
].string
;
830 argv
[argc
+ 1] = NULL
;
832 printf("Fetching %s\n", name
);
833 if (run_command_v_opt(argv
, RUN_GIT_CMD
)) {
834 error("Could not fetch %s", name
);
842 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
845 static const char **refs
= NULL
;
850 die("No remote repository specified. Please, specify either a URL or a\n"
851 "remote name from which new revisions should be fetched.");
853 transport
= transport_get(remote
, NULL
);
854 transport_set_verbosity(transport
, verbosity
, progress
);
856 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
858 set_option(TRANS_OPT_KEEP
, "yes");
860 set_option(TRANS_OPT_DEPTH
, depth
);
864 refs
= xcalloc(argc
+ 1, sizeof(const char *));
865 for (i
= 0; i
< argc
; i
++) {
866 if (!strcmp(argv
[i
], "tag")) {
870 die("You need to specify a tag name.");
871 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
872 strcpy(ref
, "refs/tags/");
873 strcat(ref
, argv
[i
]);
874 strcat(ref
, ":refs/tags/");
875 strcat(ref
, argv
[i
]);
884 sigchain_push_common(unlock_pack_on_signal
);
886 exit_code
= do_fetch(transport
,
887 parse_fetch_refspec(ref_nr
, refs
), ref_nr
);
888 transport_disconnect(transport
);
893 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
896 struct string_list list
= { NULL
, 0, 0, 0 };
897 struct remote
*remote
;
900 /* Record the command line for the reflog */
901 strbuf_addstr(&default_rla
, "fetch");
902 for (i
= 1; i
< argc
; i
++)
903 strbuf_addf(&default_rla
, " %s", argv
[i
]);
905 argc
= parse_options(argc
, argv
, prefix
,
906 builtin_fetch_options
, builtin_fetch_usage
, 0);
910 die("fetch --all does not take a repository argument");
912 die("fetch --all does not make sense with refspecs");
913 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
914 result
= fetch_multiple(&list
);
915 } else if (argc
== 0) {
916 /* No arguments -- use default remote */
917 remote
= remote_get(NULL
);
918 result
= fetch_one(remote
, argc
, argv
);
919 } else if (multiple
) {
920 /* All arguments are assumed to be remotes or groups */
921 for (i
= 0; i
< argc
; i
++)
922 if (!add_remote_or_group(argv
[i
], &list
))
923 die("No such remote or remote group: %s", argv
[i
]);
924 result
= fetch_multiple(&list
);
926 /* Single remote or group */
927 (void) add_remote_or_group(argv
[0], &list
);
929 /* More than one remote */
931 die("Fetching a group and specifying refspecs does not make sense");
932 result
= fetch_multiple(&list
);
934 /* Zero or one remotes */
935 remote
= remote_get(argv
[0]);
936 result
= fetch_one(remote
, argc
-1, argv
+1);
940 /* All names were strdup()ed or strndup()ed */
941 list
.strdup_strings
= 1;
942 string_list_clear(&list
, 0);