Code

Teach the --all option to 'git fetch'
[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 --all [options]",
19         NULL
20 };
22 enum {
23         TAGS_UNSET = 0,
24         TAGS_DEFAULT = 1,
25         TAGS_SET = 2
26 };
28 static int all, append, force, keep, update_head_ok, verbosity;
29 static int tags = TAGS_DEFAULT;
30 static const char *depth;
31 static const char *upload_pack;
32 static struct strbuf default_rla = STRBUF_INIT;
33 static struct transport *transport;
35 static struct option builtin_fetch_options[] = {
36         OPT__VERBOSITY(&verbosity),
37         OPT_BOOLEAN(0, "all", &all,
38                     "fetch from all remotes"),
39         OPT_BOOLEAN('a', "append", &append,
40                     "append to .git/FETCH_HEAD instead of overwriting"),
41         OPT_STRING(0, "upload-pack", &upload_pack, "PATH",
42                    "path to upload pack on remote end"),
43         OPT_BOOLEAN('f', "force", &force,
44                     "force overwrite of local branch"),
45         OPT_SET_INT('t', "tags", &tags,
46                     "fetch all tags and associated objects", TAGS_SET),
47         OPT_SET_INT('n', NULL, &tags,
48                     "do not fetch all tags (--no-tags)", TAGS_UNSET),
49         OPT_BOOLEAN('k', "keep", &keep, "keep downloaded pack"),
50         OPT_BOOLEAN('u', "update-head-ok", &update_head_ok,
51                     "allow updating of HEAD ref"),
52         OPT_STRING(0, "depth", &depth, "DEPTH",
53                    "deepen history of shallow clone"),
54         OPT_END()
55 };
57 static void unlock_pack(void)
58 {
59         if (transport)
60                 transport_unlock_pack(transport);
61 }
63 static void unlock_pack_on_signal(int signo)
64 {
65         unlock_pack();
66         sigchain_pop(signo);
67         raise(signo);
68 }
70 static void add_merge_config(struct ref **head,
71                            const struct ref *remote_refs,
72                            struct branch *branch,
73                            struct ref ***tail)
74 {
75         int i;
77         for (i = 0; i < branch->merge_nr; i++) {
78                 struct ref *rm, **old_tail = *tail;
79                 struct refspec refspec;
81                 for (rm = *head; rm; rm = rm->next) {
82                         if (branch_merge_matches(branch, i, rm->name)) {
83                                 rm->merge = 1;
84                                 break;
85                         }
86                 }
87                 if (rm)
88                         continue;
90                 /*
91                  * Not fetched to a tracking branch?  We need to fetch
92                  * it anyway to allow this branch's "branch.$name.merge"
93                  * to be honored by 'git pull', but we do not have to
94                  * fail if branch.$name.merge is misconfigured to point
95                  * at a nonexisting branch.  If we were indeed called by
96                  * 'git pull', it will notice the misconfiguration because
97                  * there is no entry in the resulting FETCH_HEAD marked
98                  * for merging.
99                  */
100                 refspec.src = branch->merge[i]->src;
101                 refspec.dst = NULL;
102                 refspec.pattern = 0;
103                 refspec.force = 0;
104                 get_fetch_map(remote_refs, &refspec, tail, 1);
105                 for (rm = *old_tail; rm; rm = rm->next)
106                         rm->merge = 1;
107         }
110 static void find_non_local_tags(struct transport *transport,
111                         struct ref **head,
112                         struct ref ***tail);
114 static struct ref *get_ref_map(struct transport *transport,
115                                struct refspec *refs, int ref_count, int tags,
116                                int *autotags)
118         int i;
119         struct ref *rm;
120         struct ref *ref_map = NULL;
121         struct ref **tail = &ref_map;
123         const struct ref *remote_refs = transport_get_remote_refs(transport);
125         if (ref_count || tags == TAGS_SET) {
126                 for (i = 0; i < ref_count; i++) {
127                         get_fetch_map(remote_refs, &refs[i], &tail, 0);
128                         if (refs[i].dst && refs[i].dst[0])
129                                 *autotags = 1;
130                 }
131                 /* Merge everything on the command line, but not --tags */
132                 for (rm = ref_map; rm; rm = rm->next)
133                         rm->merge = 1;
134                 if (tags == TAGS_SET)
135                         get_fetch_map(remote_refs, tag_refspec, &tail, 0);
136         } else {
137                 /* Use the defaults */
138                 struct remote *remote = transport->remote;
139                 struct branch *branch = branch_get(NULL);
140                 int has_merge = branch_has_merge_config(branch);
141                 if (remote && (remote->fetch_refspec_nr || has_merge)) {
142                         for (i = 0; i < remote->fetch_refspec_nr; i++) {
143                                 get_fetch_map(remote_refs, &remote->fetch[i], &tail, 0);
144                                 if (remote->fetch[i].dst &&
145                                     remote->fetch[i].dst[0])
146                                         *autotags = 1;
147                                 if (!i && !has_merge && ref_map &&
148                                     !remote->fetch[0].pattern)
149                                         ref_map->merge = 1;
150                         }
151                         /*
152                          * if the remote we're fetching from is the same
153                          * as given in branch.<name>.remote, we add the
154                          * ref given in branch.<name>.merge, too.
155                          */
156                         if (has_merge &&
157                             !strcmp(branch->remote_name, remote->name))
158                                 add_merge_config(&ref_map, remote_refs, branch, &tail);
159                 } else {
160                         ref_map = get_remote_ref(remote_refs, "HEAD");
161                         if (!ref_map)
162                                 die("Couldn't find remote ref HEAD");
163                         ref_map->merge = 1;
164                         tail = &ref_map->next;
165                 }
166         }
167         if (tags == TAGS_DEFAULT && *autotags)
168                 find_non_local_tags(transport, &ref_map, &tail);
169         ref_remove_duplicates(ref_map);
171         return ref_map;
174 #define STORE_REF_ERROR_OTHER 1
175 #define STORE_REF_ERROR_DF_CONFLICT 2
177 static int s_update_ref(const char *action,
178                         struct ref *ref,
179                         int check_old)
181         char msg[1024];
182         char *rla = getenv("GIT_REFLOG_ACTION");
183         static struct ref_lock *lock;
185         if (!rla)
186                 rla = default_rla.buf;
187         snprintf(msg, sizeof(msg), "%s: %s", rla, action);
188         lock = lock_any_ref_for_update(ref->name,
189                                        check_old ? ref->old_sha1 : NULL, 0);
190         if (!lock)
191                 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
192                                           STORE_REF_ERROR_OTHER;
193         if (write_ref_sha1(lock, ref->new_sha1, msg) < 0)
194                 return errno == ENOTDIR ? STORE_REF_ERROR_DF_CONFLICT :
195                                           STORE_REF_ERROR_OTHER;
196         return 0;
199 #define SUMMARY_WIDTH (2 * DEFAULT_ABBREV + 3)
200 #define REFCOL_WIDTH  10
202 static int update_local_ref(struct ref *ref,
203                             const char *remote,
204                             char *display)
206         struct commit *current = NULL, *updated;
207         enum object_type type;
208         struct branch *current_branch = branch_get(NULL);
209         const char *pretty_ref = prettify_refname(ref->name);
211         *display = 0;
212         type = sha1_object_info(ref->new_sha1, NULL);
213         if (type < 0)
214                 die("object %s not found", sha1_to_hex(ref->new_sha1));
216         if (!hashcmp(ref->old_sha1, ref->new_sha1)) {
217                 if (verbosity > 0)
218                         sprintf(display, "= %-*s %-*s -> %s", SUMMARY_WIDTH,
219                                 "[up to date]", REFCOL_WIDTH, remote,
220                                 pretty_ref);
221                 return 0;
222         }
224         if (current_branch &&
225             !strcmp(ref->name, current_branch->name) &&
226             !(update_head_ok || is_bare_repository()) &&
227             !is_null_sha1(ref->old_sha1)) {
228                 /*
229                  * If this is the head, and it's not okay to update
230                  * the head, and the old value of the head isn't empty...
231                  */
232                 sprintf(display, "! %-*s %-*s -> %s  (can't fetch in current branch)",
233                         SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
234                         pretty_ref);
235                 return 1;
236         }
238         if (!is_null_sha1(ref->old_sha1) &&
239             !prefixcmp(ref->name, "refs/tags/")) {
240                 int r;
241                 r = s_update_ref("updating tag", ref, 0);
242                 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '-',
243                         SUMMARY_WIDTH, "[tag update]", REFCOL_WIDTH, remote,
244                         pretty_ref, r ? "  (unable to update local ref)" : "");
245                 return r;
246         }
248         current = lookup_commit_reference_gently(ref->old_sha1, 1);
249         updated = lookup_commit_reference_gently(ref->new_sha1, 1);
250         if (!current || !updated) {
251                 const char *msg;
252                 const char *what;
253                 int r;
254                 if (!strncmp(ref->name, "refs/tags/", 10)) {
255                         msg = "storing tag";
256                         what = "[new tag]";
257                 }
258                 else {
259                         msg = "storing head";
260                         what = "[new branch]";
261                 }
263                 r = s_update_ref(msg, ref, 0);
264                 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : '*',
265                         SUMMARY_WIDTH, what, REFCOL_WIDTH, remote, pretty_ref,
266                         r ? "  (unable to update local ref)" : "");
267                 return r;
268         }
270         if (in_merge_bases(current, &updated, 1)) {
271                 char quickref[83];
272                 int r;
273                 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
274                 strcat(quickref, "..");
275                 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
276                 r = s_update_ref("fast forward", ref, 1);
277                 sprintf(display, "%c %-*s %-*s -> %s%s", r ? '!' : ' ',
278                         SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
279                         pretty_ref, r ? "  (unable to update local ref)" : "");
280                 return r;
281         } else if (force || ref->force) {
282                 char quickref[84];
283                 int r;
284                 strcpy(quickref, find_unique_abbrev(current->object.sha1, DEFAULT_ABBREV));
285                 strcat(quickref, "...");
286                 strcat(quickref, find_unique_abbrev(ref->new_sha1, DEFAULT_ABBREV));
287                 r = s_update_ref("forced-update", ref, 1);
288                 sprintf(display, "%c %-*s %-*s -> %s  (%s)", r ? '!' : '+',
289                         SUMMARY_WIDTH, quickref, REFCOL_WIDTH, remote,
290                         pretty_ref,
291                         r ? "unable to update local ref" : "forced update");
292                 return r;
293         } else {
294                 sprintf(display, "! %-*s %-*s -> %s  (non fast forward)",
295                         SUMMARY_WIDTH, "[rejected]", REFCOL_WIDTH, remote,
296                         pretty_ref);
297                 return 1;
298         }
301 static int store_updated_refs(const char *raw_url, const char *remote_name,
302                 struct ref *ref_map)
304         FILE *fp;
305         struct commit *commit;
306         int url_len, i, note_len, shown_url = 0, rc = 0;
307         char note[1024];
308         const char *what, *kind;
309         struct ref *rm;
310         char *url, *filename = git_path("FETCH_HEAD");
312         fp = fopen(filename, "a");
313         if (!fp)
314                 return error("cannot open %s: %s\n", filename, strerror(errno));
316         url = transport_anonymize_url(raw_url);
317         for (rm = ref_map; rm; rm = rm->next) {
318                 struct ref *ref = NULL;
320                 if (rm->peer_ref) {
321                         ref = xcalloc(1, sizeof(*ref) + strlen(rm->peer_ref->name) + 1);
322                         strcpy(ref->name, rm->peer_ref->name);
323                         hashcpy(ref->old_sha1, rm->peer_ref->old_sha1);
324                         hashcpy(ref->new_sha1, rm->old_sha1);
325                         ref->force = rm->peer_ref->force;
326                 }
328                 commit = lookup_commit_reference_gently(rm->old_sha1, 1);
329                 if (!commit)
330                         rm->merge = 0;
332                 if (!strcmp(rm->name, "HEAD")) {
333                         kind = "";
334                         what = "";
335                 }
336                 else if (!prefixcmp(rm->name, "refs/heads/")) {
337                         kind = "branch";
338                         what = rm->name + 11;
339                 }
340                 else if (!prefixcmp(rm->name, "refs/tags/")) {
341                         kind = "tag";
342                         what = rm->name + 10;
343                 }
344                 else if (!prefixcmp(rm->name, "refs/remotes/")) {
345                         kind = "remote branch";
346                         what = rm->name + 13;
347                 }
348                 else {
349                         kind = "";
350                         what = rm->name;
351                 }
353                 url_len = strlen(url);
354                 for (i = url_len - 1; url[i] == '/' && 0 <= i; i--)
355                         ;
356                 url_len = i + 1;
357                 if (4 < i && !strncmp(".git", url + i - 3, 4))
358                         url_len = i - 3;
360                 note_len = 0;
361                 if (*what) {
362                         if (*kind)
363                                 note_len += sprintf(note + note_len, "%s ",
364                                                     kind);
365                         note_len += sprintf(note + note_len, "'%s' of ", what);
366                 }
367                 note[note_len] = '\0';
368                 fprintf(fp, "%s\t%s\t%s",
369                         sha1_to_hex(commit ? commit->object.sha1 :
370                                     rm->old_sha1),
371                         rm->merge ? "" : "not-for-merge",
372                         note);
373                 for (i = 0; i < url_len; ++i)
374                         if ('\n' == url[i])
375                                 fputs("\\n", fp);
376                         else
377                                 fputc(url[i], fp);
378                 fputc('\n', fp);
380                 if (ref)
381                         rc |= update_local_ref(ref, what, note);
382                 else
383                         sprintf(note, "* %-*s %-*s -> FETCH_HEAD",
384                                 SUMMARY_WIDTH, *kind ? kind : "branch",
385                                  REFCOL_WIDTH, *what ? what : "HEAD");
386                 if (*note) {
387                         if (verbosity >= 0 && !shown_url) {
388                                 fprintf(stderr, "From %.*s\n",
389                                                 url_len, url);
390                                 shown_url = 1;
391                         }
392                         if (verbosity >= 0)
393                                 fprintf(stderr, " %s\n", note);
394                 }
395         }
396         free(url);
397         fclose(fp);
398         if (rc & STORE_REF_ERROR_DF_CONFLICT)
399                 error("some local refs could not be updated; try running\n"
400                       " 'git remote prune %s' to remove any old, conflicting "
401                       "branches", remote_name);
402         return rc;
405 /*
406  * We would want to bypass the object transfer altogether if
407  * everything we are going to fetch already exists and is connected
408  * locally.
409  *
410  * The refs we are going to fetch are in ref_map.  If running
411  *
412  *  $ git rev-list --objects --stdin --not --all
413  *
414  * (feeding all the refs in ref_map on its standard input)
415  * does not error out, that means everything reachable from the
416  * refs we are going to fetch exists and is connected to some of
417  * our existing refs.
418  */
419 static int quickfetch(struct ref *ref_map)
421         struct child_process revlist;
422         struct ref *ref;
423         int err;
424         const char *argv[] = {"rev-list",
425                 "--quiet", "--objects", "--stdin", "--not", "--all", NULL};
427         /*
428          * If we are deepening a shallow clone we already have these
429          * objects reachable.  Running rev-list here will return with
430          * a good (0) exit status and we'll bypass the fetch that we
431          * really need to perform.  Claiming failure now will ensure
432          * we perform the network exchange to deepen our history.
433          */
434         if (depth)
435                 return -1;
437         if (!ref_map)
438                 return 0;
440         memset(&revlist, 0, sizeof(revlist));
441         revlist.argv = argv;
442         revlist.git_cmd = 1;
443         revlist.no_stdout = 1;
444         revlist.no_stderr = 1;
445         revlist.in = -1;
447         err = start_command(&revlist);
448         if (err) {
449                 error("could not run rev-list");
450                 return err;
451         }
453         /*
454          * If rev-list --stdin encounters an unknown commit, it terminates,
455          * which will cause SIGPIPE in the write loop below.
456          */
457         sigchain_push(SIGPIPE, SIG_IGN);
459         for (ref = ref_map; ref; ref = ref->next) {
460                 if (write_in_full(revlist.in, sha1_to_hex(ref->old_sha1), 40) < 0 ||
461                     write_str_in_full(revlist.in, "\n") < 0) {
462                         if (errno != EPIPE && errno != EINVAL)
463                                 error("failed write to rev-list: %s", strerror(errno));
464                         err = -1;
465                         break;
466                 }
467         }
469         if (close(revlist.in)) {
470                 error("failed to close rev-list's stdin: %s", strerror(errno));
471                 err = -1;
472         }
474         sigchain_pop(SIGPIPE);
476         return finish_command(&revlist) || err;
479 static int fetch_refs(struct transport *transport, struct ref *ref_map)
481         int ret = quickfetch(ref_map);
482         if (ret)
483                 ret = transport_fetch_refs(transport, ref_map);
484         if (!ret)
485                 ret |= store_updated_refs(transport->url,
486                                 transport->remote->name,
487                                 ref_map);
488         transport_unlock_pack(transport);
489         return ret;
492 static int add_existing(const char *refname, const unsigned char *sha1,
493                         int flag, void *cbdata)
495         struct string_list *list = (struct string_list *)cbdata;
496         string_list_insert(refname, list);
497         return 0;
500 static int will_fetch(struct ref **head, const unsigned char *sha1)
502         struct ref *rm = *head;
503         while (rm) {
504                 if (!hashcmp(rm->old_sha1, sha1))
505                         return 1;
506                 rm = rm->next;
507         }
508         return 0;
511 static void find_non_local_tags(struct transport *transport,
512                         struct ref **head,
513                         struct ref ***tail)
515         struct string_list existing_refs = { NULL, 0, 0, 0 };
516         struct string_list new_refs = { NULL, 0, 0, 1 };
517         char *ref_name;
518         int ref_name_len;
519         const unsigned char *ref_sha1;
520         const struct ref *tag_ref;
521         struct ref *rm = NULL;
522         const struct ref *ref;
524         for_each_ref(add_existing, &existing_refs);
525         for (ref = transport_get_remote_refs(transport); ref; ref = ref->next) {
526                 if (prefixcmp(ref->name, "refs/tags"))
527                         continue;
529                 ref_name = xstrdup(ref->name);
530                 ref_name_len = strlen(ref_name);
531                 ref_sha1 = ref->old_sha1;
533                 if (!strcmp(ref_name + ref_name_len - 3, "^{}")) {
534                         ref_name[ref_name_len - 3] = 0;
535                         tag_ref = transport_get_remote_refs(transport);
536                         while (tag_ref) {
537                                 if (!strcmp(tag_ref->name, ref_name)) {
538                                         ref_sha1 = tag_ref->old_sha1;
539                                         break;
540                                 }
541                                 tag_ref = tag_ref->next;
542                         }
543                 }
545                 if (!string_list_has_string(&existing_refs, ref_name) &&
546                     !string_list_has_string(&new_refs, ref_name) &&
547                     (has_sha1_file(ref->old_sha1) ||
548                      will_fetch(head, ref->old_sha1))) {
549                         string_list_insert(ref_name, &new_refs);
551                         rm = alloc_ref(ref_name);
552                         rm->peer_ref = alloc_ref(ref_name);
553                         hashcpy(rm->old_sha1, ref_sha1);
555                         **tail = rm;
556                         *tail = &rm->next;
557                 }
558                 free(ref_name);
559         }
560         string_list_clear(&existing_refs, 0);
561         string_list_clear(&new_refs, 0);
564 static void check_not_current_branch(struct ref *ref_map)
566         struct branch *current_branch = branch_get(NULL);
568         if (is_bare_repository() || !current_branch)
569                 return;
571         for (; ref_map; ref_map = ref_map->next)
572                 if (ref_map->peer_ref && !strcmp(current_branch->refname,
573                                         ref_map->peer_ref->name))
574                         die("Refusing to fetch into current branch %s "
575                             "of non-bare repository", current_branch->refname);
578 static int do_fetch(struct transport *transport,
579                     struct refspec *refs, int ref_count)
581         struct ref *ref_map;
582         struct ref *rm;
583         int autotags = (transport->remote->fetch_tags == 1);
584         if (transport->remote->fetch_tags == 2 && tags != TAGS_UNSET)
585                 tags = TAGS_SET;
586         if (transport->remote->fetch_tags == -1)
587                 tags = TAGS_UNSET;
589         if (!transport->get_refs_list || !transport->fetch)
590                 die("Don't know how to fetch from %s", transport->url);
592         /* if not appending, truncate FETCH_HEAD */
593         if (!append) {
594                 char *filename = git_path("FETCH_HEAD");
595                 FILE *fp = fopen(filename, "w");
596                 if (!fp)
597                         return error("cannot open %s: %s\n", filename, strerror(errno));
598                 fclose(fp);
599         }
601         ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
602         if (!update_head_ok)
603                 check_not_current_branch(ref_map);
605         for (rm = ref_map; rm; rm = rm->next) {
606                 if (rm->peer_ref)
607                         read_ref(rm->peer_ref->name, rm->peer_ref->old_sha1);
608         }
610         if (tags == TAGS_DEFAULT && autotags)
611                 transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
612         if (fetch_refs(transport, ref_map)) {
613                 free_refs(ref_map);
614                 return 1;
615         }
616         free_refs(ref_map);
618         /* if neither --no-tags nor --tags was specified, do automated tag
619          * following ... */
620         if (tags == TAGS_DEFAULT && autotags) {
621                 struct ref **tail = &ref_map;
622                 ref_map = NULL;
623                 find_non_local_tags(transport, &ref_map, &tail);
624                 if (ref_map) {
625                         transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, NULL);
626                         transport_set_option(transport, TRANS_OPT_DEPTH, "0");
627                         fetch_refs(transport, ref_map);
628                 }
629                 free_refs(ref_map);
630         }
632         return 0;
635 static void set_option(const char *name, const char *value)
637         int r = transport_set_option(transport, name, value);
638         if (r < 0)
639                 die("Option \"%s\" value \"%s\" is not valid for %s",
640                         name, value, transport->url);
641         if (r > 0)
642                 warning("Option \"%s\" is ignored for %s\n",
643                         name, transport->url);
646 static int get_one_remote_for_fetch(struct remote *remote, void *priv)
648         struct string_list *list = priv;
649         string_list_append(remote->name, list);
650         return 0;
653 struct remote_group_data {
654         const char *name;
655         struct string_list *list;
656 };
658 static int get_remote_group(const char *key, const char *value, void *priv)
660         struct remote_group_data *g = priv;
662         if (!prefixcmp(key, "remotes.") &&
663                         !strcmp(key + 8, g->name)) {
664                 /* split list by white space */
665                 int space = strcspn(value, " \t\n");
666                 while (*value) {
667                         if (space > 1) {
668                                 string_list_append(xstrndup(value, space),
669                                                    g->list);
670                         }
671                         value += space + (value[space] != '\0');
672                         space = strcspn(value, " \t\n");
673                 }
674         }
676         return 0;
679 static int add_remote_or_group(const char *name, struct string_list *list)
681         int prev_nr = list->nr;
682         struct remote_group_data g = { name, list };
684         git_config(get_remote_group, &g);
685         if (list->nr == prev_nr) {
686                 struct remote *remote;
687                 if (!remote_is_configured(name))
688                         return 0;
689                 remote = remote_get(name);
690                 string_list_append(remote->name, list);
691         }
692         return 1;
695 static int fetch_multiple(struct string_list *list)
697         int i, result = 0;
698         const char *argv[] = { "fetch", NULL, NULL, NULL, NULL };
699         int argc = 1;
701         if (verbosity >= 2)
702                 argv[argc++] = "-v";
703         if (verbosity >= 1)
704                 argv[argc++] = "-v";
705         else if (verbosity < 0)
706                 argv[argc++] = "-q";
708         for (i = 0; i < list->nr; i++) {
709                 const char *name = list->items[i].string;
710                 argv[argc] = name;
711                 if (verbosity >= 0)
712                         printf("Fetching %s\n", name);
713                 if (run_command_v_opt(argv, RUN_GIT_CMD)) {
714                         error("Could not fetch %s", name);
715                         result = 1;
716                 }
717         }
719         return result;
722 static int fetch_one(struct remote *remote, int argc, const char **argv)
724         int i;
725         static const char **refs = NULL;
726         int ref_nr = 0;
727         int exit_code;
729         if (!remote)
730                 die("Where do you want to fetch from today?");
732         transport = transport_get(remote, remote->url[0]);
733         if (verbosity >= 2)
734                 transport->verbose = 1;
735         if (verbosity < 0)
736                 transport->verbose = -1;
737         if (upload_pack)
738                 set_option(TRANS_OPT_UPLOADPACK, upload_pack);
739         if (keep)
740                 set_option(TRANS_OPT_KEEP, "yes");
741         if (depth)
742                 set_option(TRANS_OPT_DEPTH, depth);
744         if (argc > 0) {
745                 int j = 0;
746                 refs = xcalloc(argc + 1, sizeof(const char *));
747                 for (i = 0; i < argc; i++) {
748                         if (!strcmp(argv[i], "tag")) {
749                                 char *ref;
750                                 i++;
751                                 if (i >= argc)
752                                         die("You need to specify a tag name.");
753                                 ref = xmalloc(strlen(argv[i]) * 2 + 22);
754                                 strcpy(ref, "refs/tags/");
755                                 strcat(ref, argv[i]);
756                                 strcat(ref, ":refs/tags/");
757                                 strcat(ref, argv[i]);
758                                 refs[j++] = ref;
759                         } else
760                                 refs[j++] = argv[i];
761                 }
762                 refs[j] = NULL;
763                 ref_nr = j;
764         }
766         sigchain_push_common(unlock_pack_on_signal);
767         atexit(unlock_pack);
768         exit_code = do_fetch(transport,
769                         parse_fetch_refspec(ref_nr, refs), ref_nr);
770         transport_disconnect(transport);
771         transport = NULL;
772         return exit_code;
775 int cmd_fetch(int argc, const char **argv, const char *prefix)
777         int i;
778         struct string_list list = { NULL, 0, 0, 0 };
779         struct remote *remote;
780         int result = 0;
782         /* Record the command line for the reflog */
783         strbuf_addstr(&default_rla, "fetch");
784         for (i = 1; i < argc; i++)
785                 strbuf_addf(&default_rla, " %s", argv[i]);
787         argc = parse_options(argc, argv, prefix,
788                              builtin_fetch_options, builtin_fetch_usage, 0);
790         if (all) {
791                 if (argc == 1)
792                         die("fetch --all does not take a repository argument");
793                 else if (argc > 1)
794                         die("fetch --all does not make sense with refspecs");
795                 (void) for_each_remote(get_one_remote_for_fetch, &list);
796                 result = fetch_multiple(&list);
797         } else if (argc == 0) {
798                 /* No arguments -- use default remote */
799                 remote = remote_get(NULL);
800                 result = fetch_one(remote, argc, argv);
801         } else {
802                 /* Single remote or group */
803                 (void) add_remote_or_group(argv[0], &list);
804                 if (list.nr > 1) {
805                         /* More than one remote */
806                         if (argc > 1)
807                                 die("Fetching a group and specifying refspecs does not make sense");
808                         result = fetch_multiple(&list);
809                 } else {
810                         /* Zero or one remotes */
811                         remote = remote_get(argv[0]);
812                         result = fetch_one(remote, argc-1, argv+1);
813                 }
814         }
816         /* All names were strdup()ed or strndup()ed */
817         list.strdup_strings = 1;
818         string_list_clear(&list, 0);
820         return result;