Code

Fix "git remote update" with remotes.defalt set
[git.git] / builtin-fetch.c
1 /*
2  * "git fetch"
3  */
4 #include "cache.h"
5 #include "refs.h"
6 #include "commit.h"
7 #include "builtin.h"
8 #include "string-list.h"
9 #include "remote.h"
10 #include "transport.h"
11 #include "run-command.h"
12 #include "parse-options.h"
13 #include "sigchain.h"
15 static const char * const builtin_fetch_usage[] = {
16         "git fetch [options] [<repository> <refspec>...]",
17         "git fetch [options] <group>",
18         "git fetch --multiple [options] [<repository> | <group>]...",
19         "git fetch --all [options]",
20         NULL
21 };
23 enum {
24         TAGS_UNSET = 0,
25         TAGS_DEFAULT = 1,
26         TAGS_SET = 2
27 };
29 static int all, append, dry_run, force, keep, multiple, prune, update_head_ok, verbosity;
30 static int tags = TAGS_DEFAULT;
31 static const char *depth;
32 static const char *upload_pack;
33 static struct strbuf default_rla = STRBUF_INIT;
34 static struct transport *transport;
36 static struct option builtin_fetch_options[] = {
37         OPT__VERBOSITY(&verbosity),
38         OPT_BOOLEAN(0, "all", &all,
39                     "fetch from all remotes"),
40         OPT_BOOLEAN('a', "append", &append,
41                     "append to .git/FETCH_HEAD instead of overwriting"),
42         OPT_STRING(0, "upload-pack", &upload_pack, "PATH",
43                    "path to upload pack on remote end"),
44         OPT_BOOLEAN('f', "force", &force,
45                     "force overwrite of local branch"),
46         OPT_BOOLEAN('m', "multiple", &multiple,
47                     "fetch from multiple remotes"),
48         OPT_SET_INT('t', "tags", &tags,
49                     "fetch all tags and associated objects", TAGS_SET),
50         OPT_SET_INT('n', NULL, &tags,
51                     "do not fetch all tags (--no-tags)", TAGS_UNSET),
52         OPT_BOOLEAN('p', "prune", &prune,
53                     "prune tracking branches no longer on remote"),
54         OPT_BOOLEAN(0, "dry-run", &dry_run,
55                     "dry run"),
56         OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
57         OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
58                     "allow updating of HEAD ref"),
59         OPT_STRING(0, "depth", &depth, "DEPTH",
60                    "deepen history of shallow clone"),
61         OPT_END()
62 };
64 static void unlock_pack(void)
65 {
66         if (transport)
67                 transport_unlock_pack(transport);
68 }
70 static void unlock_pack_on_signal(int signo)
71 {
72         unlock_pack();
73         sigchain_pop(signo);
74         raise(signo);
75 }
77 static void add_merge_config(struct ref **head,
78                            const struct ref *remote_refs,
79                            struct branch *branch,
80                            struct ref ***tail)
81 {
82         int i;
84         for (i = 0; i < branch->merge_nr; i++) {
85                 struct ref *rm, **old_tail = *tail;
86                 struct refspec refspec;
88                 for (rm = *head; rm; rm = rm->next) {
89                         if (branch_merge_matches(branch, i, rm->name)) {
90                                 rm->merge = 1;
91                                 break;
92                         }
93                 }
94                 if (rm)
95                         continue;
97                 /*
98                  * Not fetched to a tracking branch?  We need to fetch
99                  * it anyway to allow this branch's "branch.$name.merge"
100                  * to be honored by 'git pull', but we do not have to
101                  * fail if branch.$name.merge is misconfigured to point
102                  * at a nonexisting branch.  If we were indeed called by
103                  * 'git pull', it will notice the misconfiguration because
104                  * there is no entry in the resulting FETCH_HEAD marked
105                  * for merging.
106                  */
107                 refspec.src = branch->merge[i]->src;
108                 refspec.dst = NULL;
109                 refspec.pattern = 0;
110                 refspec.force = 0;
111                 get_fetch_map(remote_refs, &refspec, tail, 1);
112                 for (rm = *old_tail; rm; rm = rm->next)
113                         rm->merge = 1;
114         }
117 static void find_non_local_tags(struct transport *transport,
118                         struct ref **head,
119                         struct ref ***tail);
121 static struct ref *get_ref_map(struct transport *transport,
122                                struct refspec *refs, int ref_count, int tags,
123                                int *autotags)
125         int i;
126         struct ref *rm;
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])
136                                 *autotags = 1;
137                 }
138                 /* Merge everything on the command line, but not --tags */
139                 for (rm = ref_map; rm; rm = rm->next)
140                         rm->merge = 1;
141                 if (tags == TAGS_SET)
142                         get_fetch_map(remote_refs, tag_refspec, &tail, 0);
143         } else {
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);
148                 if (remote && (remote->fetch_refspec_nr || has_merge)) {
149                         for (i = 0; i < remote->fetch_refspec_nr; i++) {
150                                 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
151                                 if (remote->fetch[i].dst &&
152                                     remote->fetch[i].dst[0])
153                                         *autotags = 1;
154                                 if (!i && !has_merge && ref_map &&
155                                     !remote->fetch[0].pattern)
156                                         ref_map->merge = 1;
157                         }
158                         /*
159                          * if the remote we're fetching from is the same
160                          * as given in branch.<name>.remote, we add the
161                          * ref given in branch.<name>.merge, too.
162                          */
163                         if (has_merge &&
164                             !strcmp(branch->remote_name, remote->name))
165                                 add_merge_config(&ref_map, remote_refs, branch, &tail);
166                 } else {
167                         ref_map = get_remote_ref(remote_refs, "HEAD");
168                         if (!ref_map)
169                                 die("Couldn't find remote ref HEAD");
170                         ref_map->merge = 1;
171                         tail = &ref_map->next;
172                 }
173         }
174         if (tags == TAGS_DEFAULT && *autotags)
175                 find_non_local_tags(transport, &ref_map, &tail);
176         ref_remove_duplicates(ref_map);
178         return ref_map;
181 #define STORE_REF_ERROR_OTHER 1
182 #define STORE_REF_ERROR_DF_CONFLICT 2
184 static int s_update_ref(const char *action,
185                         struct ref *ref,
186                         int check_old)
188         char msg[1024];
189         char *rla = getenv("GIT_REFLOG_ACTION");
190         static struct ref_lock *lock;
192         if (dry_run)
193                 return 0;
194         if (!rla)
195                 rla = default_rla.buf;
196         snprintf(msg, sizeof(msg), "%s: %s", rla, action);
197         lock = lock_any_ref_for_update(ref->name,
198                                        check_old ? ref->old_sha1 : NULL, 0);
199         if (!lock)
200                 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
201                                           STORE_REF_ERROR_OTHER;
202         if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
203                 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
204                                           STORE_REF_ERROR_OTHER;
205         return 0;
208 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
209 #define REFCOL_WIDTH  10
211 static int update_local_ref(struct ref *ref,
212                             const char *remote,
213                             char *display)
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);
220         *display = 0;
221         type = sha1_object_info(ref->new_sha1, NULL);
222         if (type < 0)
223                 die("object %s not found", sha1_to_hex(ref->new_sha1));
225         if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
226                 if (verbosity > 0)
227                         sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH,
228                                 "[up to date]", REFCOL_WIDTH, remote,
229                                 pretty_ref);
230                 return 0;
231         }
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)) {
237                 /*
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...
240                  */
241                 sprintf(display, "! %-*s %-*s -> %s  (can't fetch in current branch)",
242                         SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
243                         pretty_ref);
244                 return 1;
245         }
247         if (!is_null_sha1(ref->old_sha1) &&
248             !prefixcmp(ref->name, "refs/tags/")) {
249                 int r;
250                 r = s_update_ref("updating tag", ref, 0);
251                 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
252                         SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
253                         pretty_ref, r ? "  (unable to update local ref)" : "");
254                 return r;
255         }
257         current = lookup_commit_reference_gently(ref->old_sha1, 1);
258         updated = lookup_commit_reference_gently(ref->new_sha1, 1);
259         if (!current || !updated) {
260                 const char *msg;
261                 const char *what;
262                 int r;
263                 if (!strncmp(ref->name, "refs/tags/", 10)) {
264                         msg = "storing tag";
265                         what = "[new tag]";
266                 }
267                 else {
268                         msg = "storing head";
269                         what = "[new branch]";
270                 }
272                 r = s_update_ref(msg, ref, 0);
273                 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
274                         SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
275                         r ? "  (unable to update local ref)" : "");
276                 return r;
277         }
279         if (in_merge_bases(current, &updated, 1)) {
280                 char quickref[83];
281                 int r;
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                         SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
288                         pretty_ref, r ? "  (unable to update local ref)" : "");
289                 return r;
290         } else if (force || ref->force) {
291                 char quickref[84];
292                 int r;
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                         SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
299                         pretty_ref,
300                         r ? "unable to update local ref" : "forced update");
301                 return r;
302         } else {
303                 sprintf(display, "! %-*s %-*s -> %s  (non fast forward)",
304                         SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
305                         pretty_ref);
306                 return 1;
307         }
310 static int store_updated_refs(const char *raw_url, const char *remote_name,
311                 struct ref *ref_map)
313         FILE *fp;
314         struct commit *commit;
315         int url_len, i, note_len, shown_url = 0, rc = 0;
316         char note[1024];
317         const char *what, *kind;
318         struct ref *rm;
319         char *url, *filename = dry_run ? "/dev/null" : git_path("FETCH_HEAD");
321         fp = fopen(filename, "a");
322         if (!fp)
323                 return error("cannot open %s: %s\n", filename, strerror(errno));
325         url = transport_anonymize_url(raw_url);
326         for (rm = ref_map; rm; rm = rm->next) {
327                 struct ref *ref = NULL;
329                 if (rm->peer_ref) {
330                         ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
331                         strcpy(ref->name, rm->peer_ref->name);
332                         hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
333                         hashcpy(ref->new_sha1, rm->old_sha1);
334                         ref->force = rm->peer_ref->force;
335                 }
337                 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
338                 if (!commit)
339                         rm->merge = 0;
341                 if (!strcmp(rm->name, "HEAD")) {
342                         kind = "";
343                         what = "";
344                 }
345                 else if (!prefixcmp(rm->name, "refs/heads/")) {
346                         kind = "branch";
347                         what = rm->name + 11;
348                 }
349                 else if (!prefixcmp(rm->name, "refs/tags/")) {
350                         kind = "tag";
351                         what = rm->name + 10;
352                 }
353                 else if (!prefixcmp(rm->name, "refs/remotes/")) {
354                         kind = "remote branch";
355                         what = rm->name + 13;
356                 }
357                 else {
358                         kind = "";
359                         what = rm->name;
360                 }
362                 url_len = strlen(url);
363                 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
364                         ;
365                 url_len = i + 1;
366                 if (4 < i && !strncmp(".git", url + i - 3, 4))
367                         url_len = i - 3;
369                 note_len = 0;
370                 if (*what) {
371                         if (*kind)
372                                 note_len += sprintf(note + note_len, "%s ",
373                                                     kind);
374                         note_len += sprintf(note + note_len, "'%s' of ", what);
375                 }
376                 note[note_len] = '\0';
377                 fprintf(fp, "%s\t%s\t%s",
378                         sha1_to_hex(commit ? commit->object.sha1 :
379                                     rm->old_sha1),
380                         rm->merge ? "" : "not-for-merge",
381                         note);
382                 for (i = 0; i < url_len; ++i)
383                         if ('\n' == url[i])
384                                 fputs("\\n", fp);
385                         else
386                                 fputc(url[i], fp);
387                 fputc('\n', fp);
389                 if (ref)
390                         rc |= update_local_ref(ref, what, note);
391                 else
392                         sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
393                                 SUMMARY_WIDTH, *kind ? kind : "branch",
394                                  REFCOL_WIDTH, *what ? what : "HEAD");
395                 if (*note) {
396                         if (verbosity >= 0 && !shown_url) {
397                                 fprintf(stderr, "From %.*s\n",
398                                                 url_len, url);
399                                 shown_url = 1;
400                         }
401                         if (verbosity >= 0)
402                                 fprintf(stderr, " %s\n", note);
403                 }
404         }
405         free(url);
406         fclose(fp);
407         if (rc & STORE_REF_ERROR_DF_CONFLICT)
408                 error("some local refs could not be updated; try running\n"
409                       " 'git remote prune %s' to remove any old, conflicting "
410                       "branches", remote_name);
411         return rc;
414 /*
415  * We would want to bypass the object transfer altogether if
416  * everything we are going to fetch already exists and is connected
417  * locally.
418  *
419  * The refs we are going to fetch are in ref_map.  If running
420  *
421  *  $ git rev-list --objects --stdin --not --all
422  *
423  * (feeding all the refs in ref_map on its standard input)
424  * does not error out, that means everything reachable from the
425  * refs we are going to fetch exists and is connected to some of
426  * our existing refs.
427  */
428 static int quickfetch(struct ref *ref_map)
430         struct child_process revlist;
431         struct ref *ref;
432         int err;
433         const char *argv[] = {"rev-list",
434                 "--quiet", "--objects", "--stdin", "--not", "--all", NULL};
436         /*
437          * If we are deepening a shallow clone we already have these
438          * objects reachable.  Running rev-list here will return with
439          * a good (0) exit status and we'll bypass the fetch that we
440          * really need to perform.  Claiming failure now will ensure
441          * we perform the network exchange to deepen our history.
442          */
443         if (depth)
444                 return -1;
446         if (!ref_map)
447                 return 0;
449         memset(&revlist, 0, sizeof(revlist));
450         revlist.argv = argv;
451         revlist.git_cmd = 1;
452         revlist.no_stdout = 1;
453         revlist.no_stderr = 1;
454         revlist.in = -1;
456         err = start_command(&revlist);
457         if (err) {
458                 error("could not run rev-list");
459                 return err;
460         }
462         /*
463          * If rev-list --stdin encounters an unknown commit, it terminates,
464          * which will cause SIGPIPE in the write loop below.
465          */
466         sigchain_push(SIGPIPE, SIG_IGN);
468         for (ref = ref_map; ref; ref = ref->next) {
469                 if (write_in_full(revlist.in, sha1_to_hex(ref->old_sha1), 40) < 0 ||
470                     write_str_in_full(revlist.in, "\n") < 0) {
471                         if (errno != EPIPE && errno != EINVAL)
472                                 error("failed write to rev-list: %s", strerror(errno));
473                         err = -1;
474                         break;
475                 }
476         }
478         if (close(revlist.in)) {
479                 error("failed to close rev-list's stdin: %s", strerror(errno));
480                 err = -1;
481         }
483         sigchain_pop(SIGPIPE);
485         return finish_command(&revlist) || err;
488 static int fetch_refs(struct transport *transport, struct ref *ref_map)
490         int ret = quickfetch(ref_map);
491         if (ret)
492                 ret = transport_fetch_refs(transport, ref_map);
493         if (!ret)
494                 ret |= store_updated_refs(transport->url,
495                                 transport->remote->name,
496                                 ref_map);
497         transport_unlock_pack(transport);
498         return ret;
501 static int prune_refs(struct transport *transport, struct ref *ref_map)
503         int result = 0;
504         struct ref *ref, *stale_refs = get_stale_heads(transport->remote, ref_map);
505         const char *dangling_msg = dry_run
506                 ? "   (%s will become dangling)\n"
507                 : "   (%s has become dangling)\n";
509         for (ref = stale_refs; ref; ref = ref->next) {
510                 if (!dry_run)
511                         result |= delete_ref(ref->name, NULL, 0);
512                 if (verbosity >= 0) {
513                         fprintf(stderr, " x %-*s %-*s -> %s\n",
514                                 SUMMARY_WIDTH, "[deleted]",
515                                 REFCOL_WIDTH, "(none)", prettify_refname(ref->name));
516                         warn_dangling_symref(stderr, dangling_msg, ref->name);
517                 }
518         }
519         free_refs(stale_refs);
520         return result;
523 static int add_existing(const char *refname, const unsigned char *sha1,
524                         int flag, void *cbdata)
526         struct string_list *list = (struct string_list *)cbdata;
527         string_list_insert(refname, list);
528         return 0;
531 static int will_fetch(struct ref **head, const unsigned char *sha1)
533         struct ref *rm = *head;
534         while (rm) {
535                 if (!hashcmp(rm->old_sha1, sha1))
536                         return 1;
537                 rm = rm->next;
538         }
539         return 0;
542 static void find_non_local_tags(struct transport *transport,
543                         struct ref **head,
544                         struct ref ***tail)
546         struct string_list existing_refs = { NULL, 0, 0, 0 };
547         struct string_list new_refs = { NULL, 0, 0, 1 };
548         char *ref_name;
549         int ref_name_len;
550         const unsigned char *ref_sha1;
551         const struct ref *tag_ref;
552         struct ref *rm = NULL;
553         const struct ref *ref;
555         for_each_ref(add_existing, &existing_refs);
556         for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
557                 if (prefixcmp(ref->name, "refs/tags"))
558                         continue;
560                 ref_name = xstrdup(ref->name);
561                 ref_name_len = strlen(ref_name);
562                 ref_sha1 = ref->old_sha1;
564                 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
565                         ref_name[ref_name_len - 3] = 0;
566                         tag_ref = transport_get_remote_refs(transport);
567                         while (tag_ref) {
568                                 if (!strcmp(tag_ref->name, ref_name)) {
569                                         ref_sha1 = tag_ref->old_sha1;
570                                         break;
571                                 }
572                                 tag_ref = tag_ref->next;
573                         }
574                 }
576                 if (!string_list_has_string(&existing_refs, ref_name) &&
577                     !string_list_has_string(&new_refs, ref_name) &&
578                     (has_sha1_file(ref->old_sha1) ||
579                      will_fetch(head, ref->old_sha1))) {
580                         string_list_insert(ref_name, &new_refs);
582                         rm = alloc_ref(ref_name);
583                         rm->peer_ref = alloc_ref(ref_name);
584                         hashcpy(rm->old_sha1, ref_sha1);
586                         **tail = rm;
587                         *tail = &rm->next;
588                 }
589                 free(ref_name);
590         }
591         string_list_clear(&existing_refs, 0);
592         string_list_clear(&new_refs, 0);
595 static void check_not_current_branch(struct ref *ref_map)
597         struct branch *current_branch = branch_get(NULL);
599         if (is_bare_repository() || !current_branch)
600                 return;
602         for (; ref_map; ref_map = ref_map->next)
603                 if (ref_map->peer_ref && !strcmp(current_branch->refname,
604                                         ref_map->peer_ref->name))
605                         die("Refusing to fetch into current branch %s "
606                             "of non-bare repository", current_branch->refname);
609 static int do_fetch(struct transport *transport,
610                     struct refspec *refs, int ref_count)
612         struct ref *ref_map;
613         struct ref *rm;
614         int autotags = (transport->remote->fetch_tags == 1);
615         if (transport->remote->fetch_tags == 2 && tags != TAGS_UNSET)
616                 tags = TAGS_SET;
617         if (transport->remote->fetch_tags == -1)
618                 tags = TAGS_UNSET;
620         if (!transport->get_refs_list || !transport->fetch)
621                 die("Don't know how to fetch from %s", transport->url);
623         /* if not appending, truncate FETCH_HEAD */
624         if (!append && !dry_run) {
625                 char *filename = git_path("FETCH_HEAD");
626                 FILE *fp = fopen(filename, "w");
627                 if (!fp)
628                         return error("cannot open %s: %s\n", filename, strerror(errno));
629                 fclose(fp);
630         }
632         ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
633         if (!update_head_ok)
634                 check_not_current_branch(ref_map);
636         for (rm = ref_map; rm; rm = rm->next) {
637                 if (rm->peer_ref)
638                         read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
639         }
641         if (tags == TAGS_DEFAULT && autotags)
642                 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
643         if (fetch_refs(transport, ref_map)) {
644                 free_refs(ref_map);
645                 return 1;
646         }
647         if (prune)
648                 prune_refs(transport, ref_map);
649         free_refs(ref_map);
651         /* if neither --no-tags nor --tags was specified, do automated tag
652          * following ... */
653         if (tags == TAGS_DEFAULT && autotags) {
654                 struct ref **tail = &ref_map;
655                 ref_map = NULL;
656                 find_non_local_tags(transport, &ref_map, &tail);
657                 if (ref_map) {
658                         transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
659                         transport_set_option(transport, TRANS_OPT_DEPTH, "0");
660                         fetch_refs(transport, ref_map);
661                 }
662                 free_refs(ref_map);
663         }
665         return 0;
668 static void set_option(const char *name, const char *value)
670         int r = transport_set_option(transport, name, value);
671         if (r < 0)
672                 die("Option \"%s\" value \"%s\" is not valid for %s",
673                         name, value, transport->url);
674         if (r > 0)
675                 warning("Option \"%s\" is ignored for %s\n",
676                         name, transport->url);
679 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
681         struct string_list *list = priv;
682         if (!remote->skip_default_update)
683                 string_list_append(remote->name, list);
684         return 0;
687 struct remote_group_data {
688         const char *name;
689         struct string_list *list;
690 };
692 static int get_remote_group(const char *key, const char *value, void *priv)
694         struct remote_group_data *g = priv;
696         if (!prefixcmp(key, "remotes.") &&
697                         !strcmp(key + 8, g->name)) {
698                 /* split list by white space */
699                 int space = strcspn(value, " \t\n");
700                 while (*value) {
701                         if (space > 1) {
702                                 string_list_append(xstrndup(value, space),
703                                                    g->list);
704                         }
705                         value += space + (value[space] != '\0');
706                         space = strcspn(value, " \t\n");
707                 }
708         }
710         return 0;
713 static int add_remote_or_group(const char *name, struct string_list *list)
715         int prev_nr = list->nr;
716         struct remote_group_data g = { name, list };
718         git_config(get_remote_group, &g);
719         if (list->nr == prev_nr) {
720                 struct remote *remote;
721                 if (!remote_is_configured(name))
722                         return 0;
723                 remote = remote_get(name);
724                 string_list_append(remote->name, list);
725         }
726         return 1;
729 static int fetch_multiple(struct string_list *list)
731         int i, result = 0;
732         const char *argv[] = { "fetch", NULL, NULL, NULL, NULL, NULL, NULL };
733         int argc = 1;
735         if (dry_run)
736                 argv[argc++] = "--dry-run";
737         if (prune)
738                 argv[argc++] = "--prune";
739         if (verbosity >= 2)
740                 argv[argc++] = "-v";
741         if (verbosity >= 1)
742                 argv[argc++] = "-v";
743         else if (verbosity < 0)
744                 argv[argc++] = "-q";
746         for (i = 0; i < list->nr; i++) {
747                 const char *name = list->items[i].string;
748                 argv[argc] = name;
749                 if (verbosity >= 0)
750                         printf("Fetching %s\n", name);
751                 if (run_command_v_opt(argv, RUN_GIT_CMD)) {
752                         error("Could not fetch %s", name);
753                         result = 1;
754                 }
755         }
757         return result;
760 static int fetch_one(struct remote *remote, int argc, const char **argv)
762         int i;
763         static const char **refs = NULL;
764         int ref_nr = 0;
765         int exit_code;
767         if (!remote)
768                 die("Where do you want to fetch from today?");
770         transport = transport_get(remote, remote->url[0]);
771         if (verbosity >= 2)
772                 transport->verbose = 1;
773         if (verbosity < 0)
774                 transport->verbose = -1;
775         if (upload_pack)
776                 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
777         if (keep)
778                 set_option(TRANS_OPT_KEEP, "yes");
779         if (depth)
780                 set_option(TRANS_OPT_DEPTH, depth);
782         if (argc > 0) {
783                 int j = 0;
784                 refs = xcalloc(argc + 1, sizeof(const char *));
785                 for (i = 0; i < argc; i++) {
786                         if (!strcmp(argv[i], "tag")) {
787                                 char *ref;
788                                 i++;
789                                 if (i >= argc)
790                                         die("You need to specify a tag name.");
791                                 ref = xmalloc(strlen(argv[i]) * 2 + 22);
792                                 strcpy(ref, "refs/tags/");
793                                 strcat(ref, argv[i]);
794                                 strcat(ref, ":refs/tags/");
795                                 strcat(ref, argv[i]);
796                                 refs[j++] = ref;
797                         } else
798                                 refs[j++] = argv[i];
799                 }
800                 refs[j] = NULL;
801                 ref_nr = j;
802         }
804         sigchain_push_common(unlock_pack_on_signal);
805         atexit(unlock_pack);
806         exit_code = do_fetch(transport,
807                         parse_fetch_refspec(ref_nr, refs), ref_nr);
808         transport_disconnect(transport);
809         transport = NULL;
810         return exit_code;
813 int cmd_fetch(int argc, const char **argv, const char *prefix)
815         int i;
816         struct string_list list = { NULL, 0, 0, 0 };
817         struct remote *remote;
818         int result = 0;
820         /* Record the command line for the reflog */
821         strbuf_addstr(&default_rla, "fetch");
822         for (i = 1; i < argc; i++)
823                 strbuf_addf(&default_rla, " %s", argv[i]);
825         argc = parse_options(argc, argv, prefix,
826                              builtin_fetch_options, builtin_fetch_usage, 0);
828         if (all) {
829                 if (argc == 1)
830                         die("fetch --all does not take a repository argument");
831                 else if (argc > 1)
832                         die("fetch --all does not make sense with refspecs");
833                 (void) for_each_remote(get_one_remote_for_fetch, &list);
834                 result = fetch_multiple(&list);
835         } else if (argc == 0) {
836                 /* No arguments -- use default remote */
837                 remote = remote_get(NULL);
838                 result = fetch_one(remote, argc, argv);
839         } else if (multiple) {
840                 /* All arguments are assumed to be remotes or groups */
841                 for (i = 0; i < argc; i++)
842                         if (!add_remote_or_group(argv[i], &list))
843                                 die("No such remote or remote group: %s", argv[i]);
844                 result = fetch_multiple(&list);
845         } else {
846                 /* Single remote or group */
847                 (void) add_remote_or_group(argv[0], &list);
848                 if (list.nr > 1) {
849                         /* More than one remote */
850                         if (argc > 1)
851                                 die("Fetching a group and specifying refspecs does not make sense");
852                         result = fetch_multiple(&list);
853                 } else {
854                         /* Zero or one remotes */
855                         remote = remote_get(argv[0]);
856                         result = fetch_one(remote, argc-1, argv+1);
857                 }
858         }
860         /* All names were strdup()ed or strndup()ed */
861         list.strdup_strings = 1;
862         string_list_clear(&list, 0);
864         return result;