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
= RECURSE_SUBMODULES_DEFAULT
;
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
= "";
39 static const char *recurse_submodules_default
;
41 static int option_parse_recurse_submodules(const struct option
*opt
,
42 const char *arg
, int unset
)
45 recurse_submodules
= RECURSE_SUBMODULES_OFF
;
48 recurse_submodules
= parse_fetch_recurse_submodules_arg(opt
->long_name
, arg
);
50 recurse_submodules
= RECURSE_SUBMODULES_ON
;
55 static struct option builtin_fetch_options
[] = {
56 OPT__VERBOSITY(&verbosity
),
57 OPT_BOOLEAN(0, "all", &all
,
58 "fetch from all remotes"),
59 OPT_BOOLEAN('a', "append", &append
,
60 "append to .git/FETCH_HEAD instead of overwriting"),
61 OPT_STRING(0, "upload-pack", &upload_pack
, "path",
62 "path to upload pack on remote end"),
63 OPT__FORCE(&force
, "force overwrite of local branch"),
64 OPT_BOOLEAN('m', "multiple", &multiple
,
65 "fetch from multiple remotes"),
66 OPT_SET_INT('t', "tags", &tags
,
67 "fetch all tags and associated objects", TAGS_SET
),
68 OPT_SET_INT('n', NULL
, &tags
,
69 "do not fetch all tags (--no-tags)", TAGS_UNSET
),
70 OPT_BOOLEAN('p', "prune", &prune
,
71 "prune remote-tracking branches no longer on remote"),
72 { OPTION_CALLBACK
, 0, "recurse-submodules", NULL
, "on-demand",
73 "control recursive fetching of submodules",
74 PARSE_OPT_OPTARG
, option_parse_recurse_submodules
},
75 OPT_BOOLEAN(0, "dry-run", &dry_run
,
77 OPT_BOOLEAN('k', "keep", &keep
, "keep downloaded pack"),
78 OPT_BOOLEAN('u', "update-head-ok", &update_head_ok
,
79 "allow updating of HEAD ref"),
80 OPT_BOOLEAN(0, "progress", &progress
, "force progress reporting"),
81 OPT_STRING(0, "depth", &depth
, "depth",
82 "deepen history of shallow clone"),
83 { OPTION_STRING
, 0, "submodule-prefix", &submodule_prefix
, "dir",
84 "prepend this to submodule path output", PARSE_OPT_HIDDEN
},
85 { OPTION_STRING
, 0, "recurse-submodules-default",
86 &recurse_submodules_default
, NULL
,
87 "default mode for recursion", PARSE_OPT_HIDDEN
},
91 static void unlock_pack(void)
94 transport_unlock_pack(transport
);
97 static void unlock_pack_on_signal(int signo
)
104 static void add_merge_config(struct ref
**head
,
105 const struct ref
*remote_refs
,
106 struct branch
*branch
,
111 for (i
= 0; i
< branch
->merge_nr
; i
++) {
112 struct ref
*rm
, **old_tail
= *tail
;
113 struct refspec refspec
;
115 for (rm
= *head
; rm
; rm
= rm
->next
) {
116 if (branch_merge_matches(branch
, i
, rm
->name
)) {
125 * Not fetched to a remote-tracking branch? We need to fetch
126 * it anyway to allow this branch's "branch.$name.merge"
127 * to be honored by 'git pull', but we do not have to
128 * fail if branch.$name.merge is misconfigured to point
129 * at a nonexisting branch. If we were indeed called by
130 * 'git pull', it will notice the misconfiguration because
131 * there is no entry in the resulting FETCH_HEAD marked
134 memset(&refspec
, 0, sizeof(refspec
));
135 refspec
.src
= branch
->merge
[i
]->src
;
136 get_fetch_map(remote_refs
, &refspec
, tail
, 1);
137 for (rm
= *old_tail
; rm
; rm
= rm
->next
)
142 static void find_non_local_tags(struct transport
*transport
,
146 static struct ref
*get_ref_map(struct transport
*transport
,
147 struct refspec
*refs
, int ref_count
, int tags
,
152 struct ref
*ref_map
= NULL
;
153 struct ref
**tail
= &ref_map
;
155 const struct ref
*remote_refs
= transport_get_remote_refs(transport
);
157 if (ref_count
|| tags
== TAGS_SET
) {
158 for (i
= 0; i
< ref_count
; i
++) {
159 get_fetch_map(remote_refs
, &refs
[i
], &tail
, 0);
160 if (refs
[i
].dst
&& refs
[i
].dst
[0])
163 /* Merge everything on the command line, but not --tags */
164 for (rm
= ref_map
; rm
; rm
= rm
->next
)
166 if (tags
== TAGS_SET
)
167 get_fetch_map(remote_refs
, tag_refspec
, &tail
, 0);
169 /* Use the defaults */
170 struct remote
*remote
= transport
->remote
;
171 struct branch
*branch
= branch_get(NULL
);
172 int has_merge
= branch_has_merge_config(branch
);
174 (remote
->fetch_refspec_nr
||
175 /* Note: has_merge implies non-NULL branch->remote_name */
176 (has_merge
&& !strcmp(branch
->remote_name
, remote
->name
)))) {
177 for (i
= 0; i
< remote
->fetch_refspec_nr
; i
++) {
178 get_fetch_map(remote_refs
, &remote
->fetch
[i
], &tail
, 0);
179 if (remote
->fetch
[i
].dst
&&
180 remote
->fetch
[i
].dst
[0])
182 if (!i
&& !has_merge
&& ref_map
&&
183 !remote
->fetch
[0].pattern
)
187 * if the remote we're fetching from is the same
188 * as given in branch.<name>.remote, we add the
189 * ref given in branch.<name>.merge, too.
191 * Note: has_merge implies non-NULL branch->remote_name
194 !strcmp(branch
->remote_name
, remote
->name
))
195 add_merge_config(&ref_map
, remote_refs
, branch
, &tail
);
197 ref_map
= get_remote_ref(remote_refs
, "HEAD");
199 die(_("Couldn't find remote ref HEAD"));
201 tail
= &ref_map
->next
;
204 if (tags
== TAGS_DEFAULT
&& *autotags
)
205 find_non_local_tags(transport
, &ref_map
, &tail
);
206 ref_remove_duplicates(ref_map
);
211 #define STORE_REF_ERROR_OTHER 1
212 #define STORE_REF_ERROR_DF_CONFLICT 2
214 static int s_update_ref(const char *action
,
219 char *rla
= getenv("GIT_REFLOG_ACTION");
220 static struct ref_lock
*lock
;
225 rla
= default_rla
.buf
;
226 snprintf(msg
, sizeof(msg
), "%s: %s", rla
, action
);
227 lock
= lock_any_ref_for_update(ref
->name
,
228 check_old
? ref
->old_sha1
: NULL
, 0);
230 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
231 STORE_REF_ERROR_OTHER
;
232 if (write_ref_sha1(lock
, ref
->new_sha1
, msg
) < 0)
233 return errno
== ENOTDIR
? STORE_REF_ERROR_DF_CONFLICT
:
234 STORE_REF_ERROR_OTHER
;
238 #define REFCOL_WIDTH 10
240 static int update_local_ref(struct ref
*ref
,
244 struct commit
*current
= NULL
, *updated
;
245 enum object_type type
;
246 struct branch
*current_branch
= branch_get(NULL
);
247 const char *pretty_ref
= prettify_refname(ref
->name
);
250 type
= sha1_object_info(ref
->new_sha1
, NULL
);
252 die(_("object %s not found"), sha1_to_hex(ref
->new_sha1
));
254 if (!hashcmp(ref
->old_sha1
, ref
->new_sha1
)) {
256 sprintf(display
, "= %-*s %-*s -> %s", TRANSPORT_SUMMARY_WIDTH
,
257 _("[up to date]"), REFCOL_WIDTH
, remote
,
262 if (current_branch
&&
263 !strcmp(ref
->name
, current_branch
->name
) &&
264 !(update_head_ok
|| is_bare_repository()) &&
265 !is_null_sha1(ref
->old_sha1
)) {
267 * If this is the head, and it's not okay to update
268 * the head, and the old value of the head isn't empty...
270 sprintf(display
, _("! %-*s %-*s -> %s (can't fetch in current branch)"),
271 TRANSPORT_SUMMARY_WIDTH
, _("[rejected]"), REFCOL_WIDTH
, remote
,
276 if (!is_null_sha1(ref
->old_sha1
) &&
277 !prefixcmp(ref
->name
, "refs/tags/")) {
279 r
= s_update_ref("updating tag", ref
, 0);
280 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '-',
281 TRANSPORT_SUMMARY_WIDTH
, _("[tag update]"), REFCOL_WIDTH
, remote
,
282 pretty_ref
, r
? _(" (unable to update local ref)") : "");
286 current
= lookup_commit_reference_gently(ref
->old_sha1
, 1);
287 updated
= lookup_commit_reference_gently(ref
->new_sha1
, 1);
288 if (!current
|| !updated
) {
292 if (!strncmp(ref
->name
, "refs/tags/", 10)) {
294 what
= _("[new tag]");
297 msg
= "storing head";
298 what
= _("[new branch]");
299 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
300 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
301 check_for_new_submodule_commits(ref
->new_sha1
);
304 r
= s_update_ref(msg
, ref
, 0);
305 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : '*',
306 TRANSPORT_SUMMARY_WIDTH
, what
, REFCOL_WIDTH
, remote
, pretty_ref
,
307 r
? _(" (unable to update local ref)") : "");
311 if (in_merge_bases(current
, &updated
, 1)) {
314 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
315 strcat(quickref
, "..");
316 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
317 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
318 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
319 check_for_new_submodule_commits(ref
->new_sha1
);
320 r
= s_update_ref("fast-forward", ref
, 1);
321 sprintf(display
, "%c %-*s %-*s -> %s%s", r
? '!' : ' ',
322 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
323 pretty_ref
, r
? _(" (unable to update local ref)") : "");
325 } else if (force
|| ref
->force
) {
328 strcpy(quickref
, find_unique_abbrev(current
->object
.sha1
, DEFAULT_ABBREV
));
329 strcat(quickref
, "...");
330 strcat(quickref
, find_unique_abbrev(ref
->new_sha1
, DEFAULT_ABBREV
));
331 if ((recurse_submodules
!= RECURSE_SUBMODULES_OFF
) &&
332 (recurse_submodules
!= RECURSE_SUBMODULES_ON
))
333 check_for_new_submodule_commits(ref
->new_sha1
);
334 r
= s_update_ref("forced-update", ref
, 1);
335 sprintf(display
, "%c %-*s %-*s -> %s (%s)", r
? '!' : '+',
336 TRANSPORT_SUMMARY_WIDTH
, quickref
, REFCOL_WIDTH
, remote
,
338 r
? _("unable to update local ref") : _("forced update"));
341 sprintf(display
, "! %-*s %-*s -> %s %s",
342 TRANSPORT_SUMMARY_WIDTH
, _("[rejected]"), REFCOL_WIDTH
, remote
,
343 pretty_ref
, _("(non-fast-forward)"));
348 static int store_updated_refs(const char *raw_url
, const char *remote_name
,
352 struct commit
*commit
;
353 int url_len
, i
, note_len
, shown_url
= 0, rc
= 0;
355 const char *what
, *kind
;
357 char *url
, *filename
= dry_run
? "/dev/null" : git_path("FETCH_HEAD");
359 fp
= fopen(filename
, "a");
361 return error(_("cannot open %s: %s\n"), filename
, strerror(errno
));
364 url
= transport_anonymize_url(raw_url
);
366 url
= xstrdup("foreign");
367 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
368 struct ref
*ref
= NULL
;
371 ref
= xcalloc(1, sizeof(*ref
) + strlen(rm
->peer_ref
->name
) + 1);
372 strcpy(ref
->name
, rm
->peer_ref
->name
);
373 hashcpy(ref
->old_sha1
, rm
->peer_ref
->old_sha1
);
374 hashcpy(ref
->new_sha1
, rm
->old_sha1
);
375 ref
->force
= rm
->peer_ref
->force
;
378 commit
= lookup_commit_reference_gently(rm
->old_sha1
, 1);
382 if (!strcmp(rm
->name
, "HEAD")) {
386 else if (!prefixcmp(rm
->name
, "refs/heads/")) {
388 what
= rm
->name
+ 11;
390 else if (!prefixcmp(rm
->name
, "refs/tags/")) {
392 what
= rm
->name
+ 10;
394 else if (!prefixcmp(rm
->name
, "refs/remotes/")) {
395 kind
= "remote-tracking branch";
396 what
= rm
->name
+ 13;
403 url_len
= strlen(url
);
404 for (i
= url_len
- 1; url
[i
] == '/' && 0 <= i
; i
--)
407 if (4 < i
&& !strncmp(".git", url
+ i
- 3, 4))
413 note_len
+= sprintf(note
+ note_len
, "%s ",
415 note_len
+= sprintf(note
+ note_len
, "'%s' of ", what
);
417 note
[note_len
] = '\0';
418 fprintf(fp
, "%s\t%s\t%s",
419 sha1_to_hex(commit
? commit
->object
.sha1
:
421 rm
->merge
? "" : "not-for-merge",
423 for (i
= 0; i
< url_len
; ++i
)
431 rc
|= update_local_ref(ref
, what
, note
);
434 sprintf(note
, "* %-*s %-*s -> FETCH_HEAD",
435 TRANSPORT_SUMMARY_WIDTH
, *kind
? kind
: "branch",
436 REFCOL_WIDTH
, *what
? what
: "HEAD");
438 if (verbosity
>= 0 && !shown_url
) {
439 fprintf(stderr
, _("From %.*s\n"),
444 fprintf(stderr
, " %s\n", note
);
449 if (rc
& STORE_REF_ERROR_DF_CONFLICT
)
450 error(_("some local refs could not be updated; try running\n"
451 " 'git remote prune %s' to remove any old, conflicting "
452 "branches"), remote_name
);
457 * We would want to bypass the object transfer altogether if
458 * everything we are going to fetch already exists and is connected
461 * The refs we are going to fetch are in ref_map. If running
463 * $ git rev-list --objects --stdin --not --all
465 * (feeding all the refs in ref_map on its standard input)
466 * does not error out, that means everything reachable from the
467 * refs we are going to fetch exists and is connected to some of
470 static int quickfetch(struct ref
*ref_map
)
472 struct child_process revlist
;
475 const char *argv
[] = {"rev-list",
476 "--quiet", "--objects", "--stdin", "--not", "--all", NULL
};
479 * If we are deepening a shallow clone we already have these
480 * objects reachable. Running rev-list here will return with
481 * a good (0) exit status and we'll bypass the fetch that we
482 * really need to perform. Claiming failure now will ensure
483 * we perform the network exchange to deepen our history.
491 memset(&revlist
, 0, sizeof(revlist
));
494 revlist
.no_stdout
= 1;
495 revlist
.no_stderr
= 1;
498 err
= start_command(&revlist
);
500 error(_("could not run rev-list"));
505 * If rev-list --stdin encounters an unknown commit, it terminates,
506 * which will cause SIGPIPE in the write loop below.
508 sigchain_push(SIGPIPE
, SIG_IGN
);
510 for (ref
= ref_map
; ref
; ref
= ref
->next
) {
511 if (write_in_full(revlist
.in
, sha1_to_hex(ref
->old_sha1
), 40) < 0 ||
512 write_str_in_full(revlist
.in
, "\n") < 0) {
513 if (errno
!= EPIPE
&& errno
!= EINVAL
)
514 error(_("failed write to rev-list: %s"), strerror(errno
));
520 if (close(revlist
.in
)) {
521 error(_("failed to close rev-list's stdin: %s"), strerror(errno
));
525 sigchain_pop(SIGPIPE
);
527 return finish_command(&revlist
) || err
;
530 static int fetch_refs(struct transport
*transport
, struct ref
*ref_map
)
532 int ret
= quickfetch(ref_map
);
534 ret
= transport_fetch_refs(transport
, ref_map
);
536 ret
|= store_updated_refs(transport
->url
,
537 transport
->remote
->name
,
539 transport_unlock_pack(transport
);
543 static int prune_refs(struct transport
*transport
, struct ref
*ref_map
)
546 struct ref
*ref
, *stale_refs
= get_stale_heads(transport
->remote
, ref_map
);
547 const char *dangling_msg
= dry_run
548 ? _(" (%s will become dangling)\n")
549 : _(" (%s has become dangling)\n");
551 for (ref
= stale_refs
; ref
; ref
= ref
->next
) {
553 result
|= delete_ref(ref
->name
, NULL
, 0);
554 if (verbosity
>= 0) {
555 fprintf(stderr
, " x %-*s %-*s -> %s\n",
556 TRANSPORT_SUMMARY_WIDTH
, _("[deleted]"),
557 REFCOL_WIDTH
, _("(none)"), prettify_refname(ref
->name
));
558 warn_dangling_symref(stderr
, dangling_msg
, ref
->name
);
561 free_refs(stale_refs
);
565 static int add_existing(const char *refname
, const unsigned char *sha1
,
566 int flag
, void *cbdata
)
568 struct string_list
*list
= (struct string_list
*)cbdata
;
569 struct string_list_item
*item
= string_list_insert(list
, refname
);
570 item
->util
= (void *)sha1
;
574 static int will_fetch(struct ref
**head
, const unsigned char *sha1
)
576 struct ref
*rm
= *head
;
578 if (!hashcmp(rm
->old_sha1
, sha1
))
585 static void find_non_local_tags(struct transport
*transport
,
589 struct string_list existing_refs
= STRING_LIST_INIT_NODUP
;
590 struct string_list remote_refs
= STRING_LIST_INIT_NODUP
;
591 const struct ref
*ref
;
592 struct string_list_item
*item
= NULL
;
594 for_each_ref(add_existing
, &existing_refs
);
595 for (ref
= transport_get_remote_refs(transport
); ref
; ref
= ref
->next
) {
596 if (prefixcmp(ref
->name
, "refs/tags"))
600 * The peeled ref always follows the matching base
601 * ref, so if we see a peeled ref that we don't want
602 * to fetch then we can mark the ref entry in the list
603 * as one to ignore by setting util to NULL.
605 if (!suffixcmp(ref
->name
, "^{}")) {
606 if (item
&& !has_sha1_file(ref
->old_sha1
) &&
607 !will_fetch(head
, ref
->old_sha1
) &&
608 !has_sha1_file(item
->util
) &&
609 !will_fetch(head
, item
->util
))
616 * If item is non-NULL here, then we previously saw a
617 * ref not followed by a peeled reference, so we need
618 * to check if it is a lightweight tag that we want to
621 if (item
&& !has_sha1_file(item
->util
) &&
622 !will_fetch(head
, item
->util
))
627 /* skip duplicates and refs that we already have */
628 if (string_list_has_string(&remote_refs
, ref
->name
) ||
629 string_list_has_string(&existing_refs
, ref
->name
))
632 item
= string_list_insert(&remote_refs
, ref
->name
);
633 item
->util
= (void *)ref
->old_sha1
;
635 string_list_clear(&existing_refs
, 0);
638 * We may have a final lightweight tag that needs to be
639 * checked to see if it needs fetching.
641 if (item
&& !has_sha1_file(item
->util
) &&
642 !will_fetch(head
, item
->util
))
646 * For all the tags in the remote_refs string list,
647 * add them to the list of refs to be fetched
649 for_each_string_list_item(item
, &remote_refs
) {
650 /* Unless we have already decided to ignore this item... */
653 struct ref
*rm
= alloc_ref(item
->string
);
654 rm
->peer_ref
= alloc_ref(item
->string
);
655 hashcpy(rm
->old_sha1
, item
->util
);
661 string_list_clear(&remote_refs
, 0);
664 static void check_not_current_branch(struct ref
*ref_map
)
666 struct branch
*current_branch
= branch_get(NULL
);
668 if (is_bare_repository() || !current_branch
)
671 for (; ref_map
; ref_map
= ref_map
->next
)
672 if (ref_map
->peer_ref
&& !strcmp(current_branch
->refname
,
673 ref_map
->peer_ref
->name
))
674 die(_("Refusing to fetch into current branch %s "
675 "of non-bare repository"), current_branch
->refname
);
678 static int truncate_fetch_head(void)
680 char *filename
= git_path("FETCH_HEAD");
681 FILE *fp
= fopen(filename
, "w");
684 return error(_("cannot open %s: %s\n"), filename
, strerror(errno
));
689 static int do_fetch(struct transport
*transport
,
690 struct refspec
*refs
, int ref_count
)
692 struct string_list existing_refs
= STRING_LIST_INIT_NODUP
;
693 struct string_list_item
*peer_item
= NULL
;
696 int autotags
= (transport
->remote
->fetch_tags
== 1);
698 for_each_ref(add_existing
, &existing_refs
);
700 if (tags
== TAGS_DEFAULT
) {
701 if (transport
->remote
->fetch_tags
== 2)
703 if (transport
->remote
->fetch_tags
== -1)
707 if (!transport
->get_refs_list
|| !transport
->fetch
)
708 die(_("Don't know how to fetch from %s"), transport
->url
);
710 /* if not appending, truncate FETCH_HEAD */
711 if (!append
&& !dry_run
) {
712 int errcode
= truncate_fetch_head();
717 ref_map
= get_ref_map(transport
, refs
, ref_count
, tags
, &autotags
);
719 check_not_current_branch(ref_map
);
721 for (rm
= ref_map
; rm
; rm
= rm
->next
) {
723 peer_item
= string_list_lookup(&existing_refs
,
726 hashcpy(rm
->peer_ref
->old_sha1
,
731 if (tags
== TAGS_DEFAULT
&& autotags
)
732 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, "1");
733 if (fetch_refs(transport
, ref_map
)) {
738 prune_refs(transport
, ref_map
);
741 /* if neither --no-tags nor --tags was specified, do automated tag
743 if (tags
== TAGS_DEFAULT
&& autotags
) {
744 struct ref
**tail
= &ref_map
;
746 find_non_local_tags(transport
, &ref_map
, &tail
);
748 transport_set_option(transport
, TRANS_OPT_FOLLOWTAGS
, NULL
);
749 transport_set_option(transport
, TRANS_OPT_DEPTH
, "0");
750 fetch_refs(transport
, ref_map
);
758 static void set_option(const char *name
, const char *value
)
760 int r
= transport_set_option(transport
, name
, value
);
762 die(_("Option \"%s\" value \"%s\" is not valid for %s"),
763 name
, value
, transport
->url
);
765 warning(_("Option \"%s\" is ignored for %s\n"),
766 name
, transport
->url
);
769 static int get_one_remote_for_fetch(struct remote
*remote
, void *priv
)
771 struct string_list
*list
= priv
;
772 if (!remote
->skip_default_update
)
773 string_list_append(list
, remote
->name
);
777 struct remote_group_data
{
779 struct string_list
*list
;
782 static int get_remote_group(const char *key
, const char *value
, void *priv
)
784 struct remote_group_data
*g
= priv
;
786 if (!prefixcmp(key
, "remotes.") &&
787 !strcmp(key
+ 8, g
->name
)) {
788 /* split list by white space */
789 int space
= strcspn(value
, " \t\n");
792 string_list_append(g
->list
,
793 xstrndup(value
, space
));
795 value
+= space
+ (value
[space
] != '\0');
796 space
= strcspn(value
, " \t\n");
803 static int add_remote_or_group(const char *name
, struct string_list
*list
)
805 int prev_nr
= list
->nr
;
806 struct remote_group_data g
;
807 g
.name
= name
; g
.list
= list
;
809 git_config(get_remote_group
, &g
);
810 if (list
->nr
== prev_nr
) {
811 struct remote
*remote
;
812 if (!remote_is_configured(name
))
814 remote
= remote_get(name
);
815 string_list_append(list
, remote
->name
);
820 static void add_options_to_argv(int *argc
, const char **argv
)
823 argv
[(*argc
)++] = "--dry-run";
825 argv
[(*argc
)++] = "--prune";
827 argv
[(*argc
)++] = "--update-head-ok";
829 argv
[(*argc
)++] = "--force";
831 argv
[(*argc
)++] = "--keep";
832 if (recurse_submodules
== RECURSE_SUBMODULES_ON
)
833 argv
[(*argc
)++] = "--recurse-submodules";
834 else if (recurse_submodules
== RECURSE_SUBMODULES_ON_DEMAND
)
835 argv
[(*argc
)++] = "--recurse-submodules=on-demand";
837 argv
[(*argc
)++] = "-v";
839 argv
[(*argc
)++] = "-v";
840 else if (verbosity
< 0)
841 argv
[(*argc
)++] = "-q";
845 static int fetch_multiple(struct string_list
*list
)
848 const char *argv
[12] = { "fetch", "--append" };
851 add_options_to_argv(&argc
, argv
);
853 if (!append
&& !dry_run
) {
854 int errcode
= truncate_fetch_head();
859 for (i
= 0; i
< list
->nr
; i
++) {
860 const char *name
= list
->items
[i
].string
;
862 argv
[argc
+ 1] = NULL
;
864 printf(_("Fetching %s\n"), name
);
865 if (run_command_v_opt(argv
, RUN_GIT_CMD
)) {
866 error(_("Could not fetch %s"), name
);
874 static int fetch_one(struct remote
*remote
, int argc
, const char **argv
)
877 static const char **refs
= NULL
;
878 struct refspec
*refspec
;
883 die(_("No remote repository specified. Please, specify either a URL or a\n"
884 "remote name from which new revisions should be fetched."));
886 transport
= transport_get(remote
, NULL
);
887 transport_set_verbosity(transport
, verbosity
, progress
);
889 set_option(TRANS_OPT_UPLOADPACK
, upload_pack
);
891 set_option(TRANS_OPT_KEEP
, "yes");
893 set_option(TRANS_OPT_DEPTH
, depth
);
897 refs
= xcalloc(argc
+ 1, sizeof(const char *));
898 for (i
= 0; i
< argc
; i
++) {
899 if (!strcmp(argv
[i
], "tag")) {
903 die(_("You need to specify a tag name."));
904 ref
= xmalloc(strlen(argv
[i
]) * 2 + 22);
905 strcpy(ref
, "refs/tags/");
906 strcat(ref
, argv
[i
]);
907 strcat(ref
, ":refs/tags/");
908 strcat(ref
, argv
[i
]);
917 sigchain_push_common(unlock_pack_on_signal
);
919 refspec
= parse_fetch_refspec(ref_nr
, refs
);
920 exit_code
= do_fetch(transport
, refspec
, ref_nr
);
922 transport_disconnect(transport
);
927 int cmd_fetch(int argc
, const char **argv
, const char *prefix
)
930 struct string_list list
= STRING_LIST_INIT_NODUP
;
931 struct remote
*remote
;
934 packet_trace_identity("fetch");
936 /* Record the command line for the reflog */
937 strbuf_addstr(&default_rla
, "fetch");
938 for (i
= 1; i
< argc
; i
++)
939 strbuf_addf(&default_rla
, " %s", argv
[i
]);
941 argc
= parse_options(argc
, argv
, prefix
,
942 builtin_fetch_options
, builtin_fetch_usage
, 0);
944 if (recurse_submodules
!= RECURSE_SUBMODULES_OFF
) {
945 if (recurse_submodules_default
) {
946 int arg
= parse_fetch_recurse_submodules_arg("--recurse-submodules-default", recurse_submodules_default
);
947 set_config_fetch_recurse_submodules(arg
);
950 git_config(submodule_config
, NULL
);
955 die(_("fetch --all does not take a repository argument"));
957 die(_("fetch --all does not make sense with refspecs"));
958 (void) for_each_remote(get_one_remote_for_fetch
, &list
);
959 result
= fetch_multiple(&list
);
960 } else if (argc
== 0) {
961 /* No arguments -- use default remote */
962 remote
= remote_get(NULL
);
963 result
= fetch_one(remote
, argc
, argv
);
964 } else if (multiple
) {
965 /* All arguments are assumed to be remotes or groups */
966 for (i
= 0; i
< argc
; i
++)
967 if (!add_remote_or_group(argv
[i
], &list
))
968 die(_("No such remote or remote group: %s"), argv
[i
]);
969 result
= fetch_multiple(&list
);
971 /* Single remote or group */
972 (void) add_remote_or_group(argv
[0], &list
);
974 /* More than one remote */
976 die(_("Fetching a group and specifying refspecs does not make sense"));
977 result
= fetch_multiple(&list
);
979 /* Zero or one remotes */
980 remote
= remote_get(argv
[0]);
981 result
= fetch_one(remote
, argc
-1, argv
+1);
985 if (!result
&& (recurse_submodules
!= RECURSE_SUBMODULES_OFF
)) {
986 const char *options
[10];
988 add_options_to_argv(&num_options
, options
);
989 result
= fetch_populated_submodules(num_options
, options
,
995 /* All names were strdup()ed or strndup()ed */
996 list
.strdup_strings
= 1;
997 string_list_clear(&list
, 0);