Merge branch 'maint'
[git.git] / builtin-remote.c
1 #include "cache.h"
2 #include "parse-options.h"
3 #include "transport.h"
4 #include "remote.h"
5 #include "string-list.h"
6 #include "strbuf.h"
7 #include "run-command.h"
8 #include "refs.h"
9
10 static const char * const builtin_remote_usage[] = {
11         "git remote [-v | --verbose]",
12         "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
13         "git remote rename <old> <new>",
14         "git remote rm <name>",
15         "git remote show [-n] <name>",
16         "git remote prune [-n | --dry-run] <name>",
17         "git remote update [group]",
18         NULL
19 };
20
21 static int verbose;
22
23 static int show_all(void);
24
25 static inline int postfixcmp(const char *string, const char *postfix)
26 {
27         int len1 = strlen(string), len2 = strlen(postfix);
28         if (len1 < len2)
29                 return 1;
30         return strcmp(string + len1 - len2, postfix);
31 }
32
33 static int opt_parse_track(const struct option *opt, const char *arg, int not)
34 {
35         struct string_list *list = opt->value;
36         if (not)
37                 string_list_clear(list, 0);
38         else
39                 string_list_append(arg, list);
40         return 0;
41 }
42
43 static int fetch_remote(const char *name)
44 {
45         const char *argv[] = { "fetch", name, NULL };
46         printf("Updating %s\n", name);
47         if (run_command_v_opt(argv, RUN_GIT_CMD))
48                 return error("Could not fetch %s", name);
49         return 0;
50 }
51
52 static int add(int argc, const char **argv)
53 {
54         int fetch = 0, mirror = 0;
55         struct string_list track = { NULL, 0, 0 };
56         const char *master = NULL;
57         struct remote *remote;
58         struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
59         const char *name, *url;
60         int i;
61
62         struct option options[] = {
63                 OPT_GROUP("add specific options"),
64                 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
65                 OPT_CALLBACK('t', "track", &track, "branch",
66                         "branch(es) to track", opt_parse_track),
67                 OPT_STRING('m', "master", &master, "branch", "master branch"),
68                 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
69                 OPT_END()
70         };
71
72         argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
73
74         if (argc < 2)
75                 usage_with_options(builtin_remote_usage, options);
76
77         name = argv[0];
78         url = argv[1];
79
80         remote = remote_get(name);
81         if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
82                         remote->fetch_refspec_nr))
83                 die("remote %s already exists.", name);
84
85         strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
86         if (!valid_fetch_refspec(buf2.buf))
87                 die("'%s' is not a valid remote name", name);
88
89         strbuf_addf(&buf, "remote.%s.url", name);
90         if (git_config_set(buf.buf, url))
91                 return 1;
92
93         strbuf_reset(&buf);
94         strbuf_addf(&buf, "remote.%s.fetch", name);
95
96         if (track.nr == 0)
97                 string_list_append("*", &track);
98         for (i = 0; i < track.nr; i++) {
99                 struct string_list_item *item = track.items + i;
100
101                 strbuf_reset(&buf2);
102                 strbuf_addch(&buf2, '+');
103                 if (mirror)
104                         strbuf_addf(&buf2, "refs/%s:refs/%s",
105                                         item->string, item->string);
106                 else
107                         strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
108                                         item->string, name, item->string);
109                 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
110                         return 1;
111         }
112
113         if (mirror) {
114                 strbuf_reset(&buf);
115                 strbuf_addf(&buf, "remote.%s.mirror", name);
116                 if (git_config_set(buf.buf, "true"))
117                         return 1;
118         }
119
120         if (fetch && fetch_remote(name))
121                 return 1;
122
123         if (master) {
124                 strbuf_reset(&buf);
125                 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
126
127                 strbuf_reset(&buf2);
128                 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
129
130                 if (create_symref(buf.buf, buf2.buf, "remote add"))
131                         return error("Could not setup master '%s'", master);
132         }
133
134         strbuf_release(&buf);
135         strbuf_release(&buf2);
136         string_list_clear(&track, 0);
137
138         return 0;
139 }
140
141 struct branch_info {
142         char *remote;
143         struct string_list merge;
144 };
145
146 static struct string_list branch_list;
147
148 static const char *abbrev_ref(const char *name, const char *prefix)
149 {
150         const char *abbrev = skip_prefix(name, prefix);
151         if (abbrev)
152                 return abbrev;
153         return name;
154 }
155 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
156
157 static int config_read_branches(const char *key, const char *value, void *cb)
158 {
159         if (!prefixcmp(key, "branch.")) {
160                 char *name;
161                 struct string_list_item *item;
162                 struct branch_info *info;
163                 enum { REMOTE, MERGE } type;
164
165                 key += 7;
166                 if (!postfixcmp(key, ".remote")) {
167                         name = xstrndup(key, strlen(key) - 7);
168                         type = REMOTE;
169                 } else if (!postfixcmp(key, ".merge")) {
170                         name = xstrndup(key, strlen(key) - 6);
171                         type = MERGE;
172                 } else
173                         return 0;
174
175                 item = string_list_insert(name, &branch_list);
176
177                 if (!item->util)
178                         item->util = xcalloc(sizeof(struct branch_info), 1);
179                 info = item->util;
180                 if (type == REMOTE) {
181                         if (info->remote)
182                                 warning("more than one branch.%s", key);
183                         info->remote = xstrdup(value);
184                 } else {
185                         char *space = strchr(value, ' ');
186                         value = abbrev_branch(value);
187                         while (space) {
188                                 char *merge;
189                                 merge = xstrndup(value, space - value);
190                                 string_list_append(merge, &info->merge);
191                                 value = abbrev_branch(space + 1);
192                                 space = strchr(value, ' ');
193                         }
194                         string_list_append(xstrdup(value), &info->merge);
195                 }
196         }
197         return 0;
198 }
199
200 static void read_branches(void)
201 {
202         if (branch_list.nr)
203                 return;
204         git_config(config_read_branches, NULL);
205         sort_string_list(&branch_list);
206 }
207
208 struct ref_states {
209         struct remote *remote;
210         struct string_list new, stale, tracked;
211 };
212
213 static int handle_one_branch(const char *refname,
214         const unsigned char *sha1, int flags, void *cb_data)
215 {
216         struct ref_states *states = cb_data;
217         struct refspec refspec;
218
219         memset(&refspec, 0, sizeof(refspec));
220         refspec.dst = (char *)refname;
221         if (!remote_find_tracking(states->remote, &refspec)) {
222                 struct string_list_item *item;
223                 const char *name = abbrev_branch(refspec.src);
224                 /* symbolic refs pointing nowhere were handled already */
225                 if ((flags & REF_ISSYMREF) ||
226                                 unsorted_string_list_has_string(&states->tracked,
227                                         name) ||
228                                 unsorted_string_list_has_string(&states->new,
229                                         name))
230                         return 0;
231                 item = string_list_append(name, &states->stale);
232                 item->util = xstrdup(refname);
233         }
234         return 0;
235 }
236
237 static int get_ref_states(const struct ref *ref, struct ref_states *states)
238 {
239         struct ref *fetch_map = NULL, **tail = &fetch_map;
240         int i;
241
242         for (i = 0; i < states->remote->fetch_refspec_nr; i++)
243                 if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
244                         die("Could not get fetch map for refspec %s",
245                                 states->remote->fetch_refspec[i]);
246
247         states->new.strdup_strings = states->tracked.strdup_strings = 1;
248         for (ref = fetch_map; ref; ref = ref->next) {
249                 struct string_list *target = &states->tracked;
250                 unsigned char sha1[20];
251                 void *util = NULL;
252
253                 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
254                         target = &states->new;
255                 else {
256                         target = &states->tracked;
257                         if (hashcmp(sha1, ref->new_sha1))
258                                 util = &states;
259                 }
260                 string_list_append(abbrev_branch(ref->name), target)->util = util;
261         }
262         free_refs(fetch_map);
263
264         for_each_ref(handle_one_branch, states);
265         sort_string_list(&states->stale);
266
267         return 0;
268 }
269
270 struct known_remote {
271         struct known_remote *next;
272         struct remote *remote;
273 };
274
275 struct known_remotes {
276         struct remote *to_delete;
277         struct known_remote *list;
278 };
279
280 static int add_known_remote(struct remote *remote, void *cb_data)
281 {
282         struct known_remotes *all = cb_data;
283         struct known_remote *r;
284
285         if (!strcmp(all->to_delete->name, remote->name))
286                 return 0;
287
288         r = xmalloc(sizeof(*r));
289         r->remote = remote;
290         r->next = all->list;
291         all->list = r;
292         return 0;
293 }
294
295 struct branches_for_remote {
296         struct remote *remote;
297         struct string_list *branches;
298         struct known_remotes *keep;
299 };
300
301 static int add_branch_for_removal(const char *refname,
302         const unsigned char *sha1, int flags, void *cb_data)
303 {
304         struct branches_for_remote *branches = cb_data;
305         struct refspec refspec;
306         struct string_list_item *item;
307         struct known_remote *kr;
308
309         memset(&refspec, 0, sizeof(refspec));
310         refspec.dst = (char *)refname;
311         if (remote_find_tracking(branches->remote, &refspec))
312                 return 0;
313
314         /* don't delete a branch if another remote also uses it */
315         for (kr = branches->keep->list; kr; kr = kr->next) {
316                 memset(&refspec, 0, sizeof(refspec));
317                 refspec.dst = (char *)refname;
318                 if (!remote_find_tracking(kr->remote, &refspec))
319                         return 0;
320         }
321
322         /* make sure that symrefs are deleted */
323         if (flags & REF_ISSYMREF)
324                 return unlink(git_path("%s", refname));
325
326         item = string_list_append(refname, branches->branches);
327         item->util = xmalloc(20);
328         hashcpy(item->util, sha1);
329
330         return 0;
331 }
332
333 struct rename_info {
334         const char *old;
335         const char *new;
336         struct string_list *remote_branches;
337 };
338
339 static int read_remote_branches(const char *refname,
340         const unsigned char *sha1, int flags, void *cb_data)
341 {
342         struct rename_info *rename = cb_data;
343         struct strbuf buf = STRBUF_INIT;
344         struct string_list_item *item;
345         int flag;
346         unsigned char orig_sha1[20];
347         const char *symref;
348
349         strbuf_addf(&buf, "refs/remotes/%s", rename->old);
350         if(!prefixcmp(refname, buf.buf)) {
351                 item = string_list_append(xstrdup(refname), rename->remote_branches);
352                 symref = resolve_ref(refname, orig_sha1, 1, &flag);
353                 if (flag & REF_ISSYMREF)
354                         item->util = xstrdup(symref);
355                 else
356                         item->util = NULL;
357         }
358
359         return 0;
360 }
361
362 static int migrate_file(struct remote *remote)
363 {
364         struct strbuf buf = STRBUF_INIT;
365         int i;
366         char *path = NULL;
367
368         strbuf_addf(&buf, "remote.%s.url", remote->name);
369         for (i = 0; i < remote->url_nr; i++)
370                 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
371                         return error("Could not append '%s' to '%s'",
372                                         remote->url[i], buf.buf);
373         strbuf_reset(&buf);
374         strbuf_addf(&buf, "remote.%s.push", remote->name);
375         for (i = 0; i < remote->push_refspec_nr; i++)
376                 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
377                         return error("Could not append '%s' to '%s'",
378                                         remote->push_refspec[i], buf.buf);
379         strbuf_reset(&buf);
380         strbuf_addf(&buf, "remote.%s.fetch", remote->name);
381         for (i = 0; i < remote->fetch_refspec_nr; i++)
382                 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
383                         return error("Could not append '%s' to '%s'",
384                                         remote->fetch_refspec[i], buf.buf);
385         if (remote->origin == REMOTE_REMOTES)
386                 path = git_path("remotes/%s", remote->name);
387         else if (remote->origin == REMOTE_BRANCHES)
388                 path = git_path("branches/%s", remote->name);
389         if (path && unlink(path))
390                 warning("failed to remove '%s'", path);
391         return 0;
392 }
393
394 static int mv(int argc, const char **argv)
395 {
396         struct option options[] = {
397                 OPT_END()
398         };
399         struct remote *oldremote, *newremote;
400         struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
401         struct string_list remote_branches = { NULL, 0, 0, 0 };
402         struct rename_info rename;
403         int i;
404
405         if (argc != 3)
406                 usage_with_options(builtin_remote_usage, options);
407
408         rename.old = argv[1];
409         rename.new = argv[2];
410         rename.remote_branches = &remote_branches;
411
412         oldremote = remote_get(rename.old);
413         if (!oldremote)
414                 die("No such remote: %s", rename.old);
415
416         if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
417                 return migrate_file(oldremote);
418
419         newremote = remote_get(rename.new);
420         if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
421                 die("remote %s already exists.", rename.new);
422
423         strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
424         if (!valid_fetch_refspec(buf.buf))
425                 die("'%s' is not a valid remote name", rename.new);
426
427         strbuf_reset(&buf);
428         strbuf_addf(&buf, "remote.%s", rename.old);
429         strbuf_addf(&buf2, "remote.%s", rename.new);
430         if (git_config_rename_section(buf.buf, buf2.buf) < 1)
431                 return error("Could not rename config section '%s' to '%s'",
432                                 buf.buf, buf2.buf);
433
434         strbuf_reset(&buf);
435         strbuf_addf(&buf, "remote.%s.fetch", rename.new);
436         if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
437                 return error("Could not remove config section '%s'", buf.buf);
438         for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
439                 char *ptr;
440
441                 strbuf_reset(&buf2);
442                 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
443                 ptr = strstr(buf2.buf, rename.old);
444                 if (ptr)
445                         strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
446                                         rename.new, strlen(rename.new));
447                 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
448                         return error("Could not append '%s'", buf.buf);
449         }
450
451         read_branches();
452         for (i = 0; i < branch_list.nr; i++) {
453                 struct string_list_item *item = branch_list.items + i;
454                 struct branch_info *info = item->util;
455                 if (info->remote && !strcmp(info->remote, rename.old)) {
456                         strbuf_reset(&buf);
457                         strbuf_addf(&buf, "branch.%s.remote", item->string);
458                         if (git_config_set(buf.buf, rename.new)) {
459                                 return error("Could not set '%s'", buf.buf);
460                         }
461                 }
462         }
463
464         /*
465          * First remove symrefs, then rename the rest, finally create
466          * the new symrefs.
467          */
468         for_each_ref(read_remote_branches, &rename);
469         for (i = 0; i < remote_branches.nr; i++) {
470                 struct string_list_item *item = remote_branches.items + i;
471                 int flag = 0;
472                 unsigned char sha1[20];
473                 const char *symref;
474
475                 symref = resolve_ref(item->string, sha1, 1, &flag);
476                 if (!(flag & REF_ISSYMREF))
477                         continue;
478                 if (delete_ref(item->string, NULL, REF_NODEREF))
479                         die("deleting '%s' failed", item->string);
480         }
481         for (i = 0; i < remote_branches.nr; i++) {
482                 struct string_list_item *item = remote_branches.items + i;
483
484                 if (item->util)
485                         continue;
486                 strbuf_reset(&buf);
487                 strbuf_addstr(&buf, item->string);
488                 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
489                                 rename.new, strlen(rename.new));
490                 strbuf_reset(&buf2);
491                 strbuf_addf(&buf2, "remote: renamed %s to %s",
492                                 item->string, buf.buf);
493                 if (rename_ref(item->string, buf.buf, buf2.buf))
494                         die("renaming '%s' failed", item->string);
495         }
496         for (i = 0; i < remote_branches.nr; i++) {
497                 struct string_list_item *item = remote_branches.items + i;
498
499                 if (!item->util)
500                         continue;
501                 strbuf_reset(&buf);
502                 strbuf_addstr(&buf, item->string);
503                 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
504                                 rename.new, strlen(rename.new));
505                 strbuf_reset(&buf2);
506                 strbuf_addstr(&buf2, item->util);
507                 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
508                                 rename.new, strlen(rename.new));
509                 strbuf_reset(&buf3);
510                 strbuf_addf(&buf3, "remote: renamed %s to %s",
511                                 item->string, buf.buf);
512                 if (create_symref(buf.buf, buf2.buf, buf3.buf))
513                         die("creating '%s' failed", buf.buf);
514         }
515         return 0;
516 }
517
518 static int remove_branches(struct string_list *branches)
519 {
520         int i, result = 0;
521         for (i = 0; i < branches->nr; i++) {
522                 struct string_list_item *item = branches->items + i;
523                 const char *refname = item->string;
524                 unsigned char *sha1 = item->util;
525
526                 if (delete_ref(refname, sha1, 0))
527                         result |= error("Could not remove branch %s", refname);
528         }
529         return result;
530 }
531
532 static int rm(int argc, const char **argv)
533 {
534         struct option options[] = {
535                 OPT_END()
536         };
537         struct remote *remote;
538         struct strbuf buf = STRBUF_INIT;
539         struct known_remotes known_remotes = { NULL, NULL };
540         struct string_list branches = { NULL, 0, 0, 1 };
541         struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
542         int i;
543
544         if (argc != 2)
545                 usage_with_options(builtin_remote_usage, options);
546
547         remote = remote_get(argv[1]);
548         if (!remote)
549                 die("No such remote: %s", argv[1]);
550
551         known_remotes.to_delete = remote;
552         for_each_remote(add_known_remote, &known_remotes);
553
554         strbuf_addf(&buf, "remote.%s", remote->name);
555         if (git_config_rename_section(buf.buf, NULL) < 1)
556                 return error("Could not remove config section '%s'", buf.buf);
557
558         read_branches();
559         for (i = 0; i < branch_list.nr; i++) {
560                 struct string_list_item *item = branch_list.items + i;
561                 struct branch_info *info = item->util;
562                 if (info->remote && !strcmp(info->remote, remote->name)) {
563                         const char *keys[] = { "remote", "merge", NULL }, **k;
564                         for (k = keys; *k; k++) {
565                                 strbuf_reset(&buf);
566                                 strbuf_addf(&buf, "branch.%s.%s",
567                                                 item->string, *k);
568                                 if (git_config_set(buf.buf, NULL)) {
569                                         strbuf_release(&buf);
570                                         return -1;
571                                 }
572                         }
573                 }
574         }
575
576         /*
577          * We cannot just pass a function to for_each_ref() which deletes
578          * the branches one by one, since for_each_ref() relies on cached
579          * refs, which are invalidated when deleting a branch.
580          */
581         cb_data.remote = remote;
582         i = for_each_ref(add_branch_for_removal, &cb_data);
583         strbuf_release(&buf);
584
585         if (!i)
586                 i = remove_branches(&branches);
587         string_list_clear(&branches, 1);
588
589         return i;
590 }
591
592 static void show_list(const char *title, struct string_list *list,
593                       const char *extra_arg)
594 {
595         int i;
596
597         if (!list->nr)
598                 return;
599
600         printf(title, list->nr > 1 ? "es" : "", extra_arg);
601         printf("\n");
602         for (i = 0; i < list->nr; i++)
603                 printf("    %s\n", list->items[i].string);
604 }
605
606 static int get_remote_ref_states(const char *name,
607                                  struct ref_states *states,
608                                  int query)
609 {
610         struct transport *transport;
611         const struct ref *ref;
612
613         states->remote = remote_get(name);
614         if (!states->remote)
615                 return error("No such remote: %s", name);
616
617         read_branches();
618
619         if (query) {
620                 transport = transport_get(NULL, states->remote->url_nr > 0 ?
621                         states->remote->url[0] : NULL);
622                 ref = transport_get_remote_refs(transport);
623                 transport_disconnect(transport);
624
625                 get_ref_states(ref, states);
626         }
627
628         return 0;
629 }
630
631 static int append_ref_to_tracked_list(const char *refname,
632         const unsigned char *sha1, int flags, void *cb_data)
633 {
634         struct ref_states *states = cb_data;
635         struct refspec refspec;
636
637         memset(&refspec, 0, sizeof(refspec));
638         refspec.dst = (char *)refname;
639         if (!remote_find_tracking(states->remote, &refspec))
640                 string_list_append(abbrev_branch(refspec.src), &states->tracked);
641
642         return 0;
643 }
644
645 static int show(int argc, const char **argv)
646 {
647         int no_query = 0, result = 0;
648         struct option options[] = {
649                 OPT_GROUP("show specific options"),
650                 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
651                 OPT_END()
652         };
653         struct ref_states states;
654
655         argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
656
657         if (argc < 1)
658                 return show_all();
659
660         memset(&states, 0, sizeof(states));
661         for (; argc; argc--, argv++) {
662                 int i;
663
664                 get_remote_ref_states(*argv, &states, !no_query);
665
666                 printf("* remote %s\n  URL: %s\n", *argv,
667                         states.remote->url_nr > 0 ?
668                                 states.remote->url[0] : "(no URL)");
669
670                 for (i = 0; i < branch_list.nr; i++) {
671                         struct string_list_item *branch = branch_list.items + i;
672                         struct branch_info *info = branch->util;
673                         int j;
674
675                         if (!info->merge.nr || strcmp(*argv, info->remote))
676                                 continue;
677                         printf("  Remote branch%s merged with 'git pull' "
678                                 "while on branch %s\n   ",
679                                 info->merge.nr > 1 ? "es" : "",
680                                 branch->string);
681                         for (j = 0; j < info->merge.nr; j++)
682                                 printf(" %s", info->merge.items[j].string);
683                         printf("\n");
684                 }
685
686                 if (!no_query) {
687                         show_list("  New remote branch%s (next fetch "
688                                 "will store in remotes/%s)",
689                                 &states.new, states.remote->name);
690                         show_list("  Stale tracking branch%s (use 'git remote "
691                                 "prune')", &states.stale, "");
692                 }
693
694                 if (no_query)
695                         for_each_ref(append_ref_to_tracked_list, &states);
696                 show_list("  Tracked remote branch%s", &states.tracked, "");
697
698                 if (states.remote->push_refspec_nr) {
699                         printf("  Local branch%s pushed with 'git push'\n",
700                                 states.remote->push_refspec_nr > 1 ?
701                                         "es" : "");
702                         for (i = 0; i < states.remote->push_refspec_nr; i++) {
703                                 struct refspec *spec = states.remote->push + i;
704                                 printf("    %s%s%s%s\n",
705                                        spec->force ? "+" : "",
706                                        abbrev_branch(spec->src),
707                                        spec->dst ? ":" : "",
708                                        spec->dst ? abbrev_branch(spec->dst) : "");
709                         }
710                 }
711
712                 /* NEEDSWORK: free remote */
713                 string_list_clear(&states.new, 0);
714                 string_list_clear(&states.stale, 0);
715                 string_list_clear(&states.tracked, 0);
716         }
717
718         return result;
719 }
720
721 static int prune(int argc, const char **argv)
722 {
723         int dry_run = 0, result = 0;
724         struct option options[] = {
725                 OPT_GROUP("prune specific options"),
726                 OPT__DRY_RUN(&dry_run),
727                 OPT_END()
728         };
729         struct ref_states states;
730
731         argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
732
733         if (argc < 1)
734                 usage_with_options(builtin_remote_usage, options);
735
736         memset(&states, 0, sizeof(states));
737         for (; argc; argc--, argv++) {
738                 int i;
739
740                 get_remote_ref_states(*argv, &states, 1);
741
742                 if (states.stale.nr) {
743                         printf("Pruning %s\n", *argv);
744                         printf("URL: %s\n",
745                                states.remote->url_nr
746                                ? states.remote->url[0]
747                                : "(no URL)");
748                 }
749
750                 for (i = 0; i < states.stale.nr; i++) {
751                         const char *refname = states.stale.items[i].util;
752
753                         if (!dry_run)
754                                 result |= delete_ref(refname, NULL, 0);
755
756                         printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
757                                abbrev_ref(refname, "refs/remotes/"));
758                 }
759
760                 /* NEEDSWORK: free remote */
761                 string_list_clear(&states.new, 0);
762                 string_list_clear(&states.stale, 0);
763                 string_list_clear(&states.tracked, 0);
764         }
765
766         return result;
767 }
768
769 static int get_one_remote_for_update(struct remote *remote, void *priv)
770 {
771         struct string_list *list = priv;
772         if (!remote->skip_default_update)
773                 string_list_append(remote->name, list);
774         return 0;
775 }
776
777 struct remote_group {
778         const char *name;
779         struct string_list *list;
780 } remote_group;
781
782 static int get_remote_group(const char *key, const char *value, void *cb)
783 {
784         if (!prefixcmp(key, "remotes.") &&
785                         !strcmp(key + 8, remote_group.name)) {
786                 /* split list by white space */
787                 int space = strcspn(value, " \t\n");
788                 while (*value) {
789                         if (space > 1)
790                                 string_list_append(xstrndup(value, space),
791                                                 remote_group.list);
792                         value += space + (value[space] != '\0');
793                         space = strcspn(value, " \t\n");
794                 }
795         }
796
797         return 0;
798 }
799
800 static int update(int argc, const char **argv)
801 {
802         int i, result = 0;
803         struct string_list list = { NULL, 0, 0, 0 };
804         static const char *default_argv[] = { NULL, "default", NULL };
805
806         if (argc < 2) {
807                 argc = 2;
808                 argv = default_argv;
809         }
810
811         remote_group.list = &list;
812         for (i = 1; i < argc; i++) {
813                 remote_group.name = argv[i];
814                 result = git_config(get_remote_group, NULL);
815         }
816
817         if (!result && !list.nr  && argc == 2 && !strcmp(argv[1], "default"))
818                 result = for_each_remote(get_one_remote_for_update, &list);
819
820         for (i = 0; i < list.nr; i++)
821                 result |= fetch_remote(list.items[i].string);
822
823         /* all names were strdup()ed or strndup()ed */
824         list.strdup_strings = 1;
825         string_list_clear(&list, 0);
826
827         return result;
828 }
829
830 static int get_one_entry(struct remote *remote, void *priv)
831 {
832         struct string_list *list = priv;
833
834         if (remote->url_nr > 0) {
835                 int i;
836
837                 for (i = 0; i < remote->url_nr; i++)
838                         string_list_append(remote->name, list)->util = (void *)remote->url[i];
839         } else
840                 string_list_append(remote->name, list)->util = NULL;
841
842         return 0;
843 }
844
845 static int show_all(void)
846 {
847         struct string_list list = { NULL, 0, 0 };
848         int result = for_each_remote(get_one_entry, &list);
849
850         if (!result) {
851                 int i;
852
853                 sort_string_list(&list);
854                 for (i = 0; i < list.nr; i++) {
855                         struct string_list_item *item = list.items + i;
856                         if (verbose)
857                                 printf("%s\t%s\n", item->string,
858                                         item->util ? (const char *)item->util : "");
859                         else {
860                                 if (i && !strcmp((item - 1)->string, item->string))
861                                         continue;
862                                 printf("%s\n", item->string);
863                         }
864                 }
865         }
866         return result;
867 }
868
869 int cmd_remote(int argc, const char **argv, const char *prefix)
870 {
871         struct option options[] = {
872                 OPT__VERBOSE(&verbose),
873                 OPT_END()
874         };
875         int result;
876
877         argc = parse_options(argc, argv, options, builtin_remote_usage,
878                 PARSE_OPT_STOP_AT_NON_OPTION);
879
880         if (argc < 1)
881                 result = show_all();
882         else if (!strcmp(argv[0], "add"))
883                 result = add(argc, argv);
884         else if (!strcmp(argv[0], "rename"))
885                 result = mv(argc, argv);
886         else if (!strcmp(argv[0], "rm"))
887                 result = rm(argc, argv);
888         else if (!strcmp(argv[0], "show"))
889                 result = show(argc, argv);
890         else if (!strcmp(argv[0], "prune"))
891                 result = prune(argc, argv);
892         else if (!strcmp(argv[0], "update"))
893                 result = update(argc, argv);
894         else {
895                 error("Unknown subcommand: %s", argv[0]);
896                 usage_with_options(builtin_remote_usage, options);
897         }
898
899         return result ? 1 : 0;
900 }