]> asedeno.scripts.mit.edu Git - git.git/blob - builtin/remote.c
Allow disabling "inline"
[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 set-head <name> (-a | -d | <branch>)",
16         "git remote [-v | --verbose] show [-n] <name>",
17         "git remote prune [-n | --dry-run] <name>",
18         "git remote [-v | --verbose] update [-p | --prune] [group | remote]",
19         "git remote set-url <name> <newurl> [<oldurl>]",
20         "git remote set-url --add <name> <newurl>",
21         "git remote set-url --delete <name> <url>",
22         NULL
23 };
24
25 static const char * const builtin_remote_add_usage[] = {
26         "git remote add [<options>] <name> <url>",
27         NULL
28 };
29
30 static const char * const builtin_remote_rename_usage[] = {
31         "git remote rename <old> <new>",
32         NULL
33 };
34
35 static const char * const builtin_remote_rm_usage[] = {
36         "git remote rm <name>",
37         NULL
38 };
39
40 static const char * const builtin_remote_sethead_usage[] = {
41         "git remote set-head <name> (-a | -d | <branch>])",
42         NULL
43 };
44
45 static const char * const builtin_remote_show_usage[] = {
46         "git remote show [<options>] <name>",
47         NULL
48 };
49
50 static const char * const builtin_remote_prune_usage[] = {
51         "git remote prune [<options>] <name>",
52         NULL
53 };
54
55 static const char * const builtin_remote_update_usage[] = {
56         "git remote update [<options>] [<group> | <remote>]...",
57         NULL
58 };
59
60 static const char * const builtin_remote_seturl_usage[] = {
61         "git remote set-url [--push] <name> <newurl> [<oldurl>]",
62         "git remote set-url --add <name> <newurl>",
63         "git remote set-url --delete <name> <url>",
64         NULL
65 };
66
67 #define GET_REF_STATES (1<<0)
68 #define GET_HEAD_NAMES (1<<1)
69 #define GET_PUSH_REF_STATES (1<<2)
70
71 static int verbose;
72
73 static int show_all(void);
74 static int prune_remote(const char *remote, int dry_run);
75
76 static inline int postfixcmp(const char *string, const char *postfix)
77 {
78         int len1 = strlen(string), len2 = strlen(postfix);
79         if (len1 < len2)
80                 return 1;
81         return strcmp(string + len1 - len2, postfix);
82 }
83
84 static int opt_parse_track(const struct option *opt, const char *arg, int not)
85 {
86         struct string_list *list = opt->value;
87         if (not)
88                 string_list_clear(list, 0);
89         else
90                 string_list_append(arg, list);
91         return 0;
92 }
93
94 static int fetch_remote(const char *name)
95 {
96         const char *argv[] = { "fetch", name, NULL, NULL };
97         if (verbose) {
98                 argv[1] = "-v";
99                 argv[2] = name;
100         }
101         printf("Updating %s\n", name);
102         if (run_command_v_opt(argv, RUN_GIT_CMD))
103                 return error("Could not fetch %s", name);
104         return 0;
105 }
106
107 static int add(int argc, const char **argv)
108 {
109         int fetch = 0, mirror = 0;
110         struct string_list track = { NULL, 0, 0 };
111         const char *master = NULL;
112         struct remote *remote;
113         struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
114         const char *name, *url;
115         int i;
116
117         struct option options[] = {
118                 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
119                 OPT_CALLBACK('t', "track", &track, "branch",
120                         "branch(es) to track", opt_parse_track),
121                 OPT_STRING('m', "master", &master, "branch", "master branch"),
122                 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
123                 OPT_END()
124         };
125
126         argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage,
127                              0);
128
129         if (argc < 2)
130                 usage_with_options(builtin_remote_add_usage, options);
131
132         name = argv[0];
133         url = argv[1];
134
135         remote = remote_get(name);
136         if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
137                         remote->fetch_refspec_nr))
138                 die("remote %s already exists.", name);
139
140         strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
141         if (!valid_fetch_refspec(buf2.buf))
142                 die("'%s' is not a valid remote name", name);
143
144         strbuf_addf(&buf, "remote.%s.url", name);
145         if (git_config_set(buf.buf, url))
146                 return 1;
147
148         strbuf_reset(&buf);
149         strbuf_addf(&buf, "remote.%s.fetch", name);
150
151         if (track.nr == 0)
152                 string_list_append("*", &track);
153         for (i = 0; i < track.nr; i++) {
154                 struct string_list_item *item = track.items + i;
155
156                 strbuf_reset(&buf2);
157                 strbuf_addch(&buf2, '+');
158                 if (mirror)
159                         strbuf_addf(&buf2, "refs/%s:refs/%s",
160                                         item->string, item->string);
161                 else
162                         strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
163                                         item->string, name, item->string);
164                 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
165                         return 1;
166         }
167
168         if (mirror) {
169                 strbuf_reset(&buf);
170                 strbuf_addf(&buf, "remote.%s.mirror", name);
171                 if (git_config_set(buf.buf, "true"))
172                         return 1;
173         }
174
175         if (fetch && fetch_remote(name))
176                 return 1;
177
178         if (master) {
179                 strbuf_reset(&buf);
180                 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
181
182                 strbuf_reset(&buf2);
183                 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
184
185                 if (create_symref(buf.buf, buf2.buf, "remote add"))
186                         return error("Could not setup master '%s'", master);
187         }
188
189         strbuf_release(&buf);
190         strbuf_release(&buf2);
191         string_list_clear(&track, 0);
192
193         return 0;
194 }
195
196 struct branch_info {
197         char *remote_name;
198         struct string_list merge;
199         int rebase;
200 };
201
202 static struct string_list branch_list;
203
204 static const char *abbrev_ref(const char *name, const char *prefix)
205 {
206         const char *abbrev = skip_prefix(name, prefix);
207         if (abbrev)
208                 return abbrev;
209         return name;
210 }
211 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
212
213 static int config_read_branches(const char *key, const char *value, void *cb)
214 {
215         if (!prefixcmp(key, "branch.")) {
216                 const char *orig_key = key;
217                 char *name;
218                 struct string_list_item *item;
219                 struct branch_info *info;
220                 enum { REMOTE, MERGE, REBASE } type;
221
222                 key += 7;
223                 if (!postfixcmp(key, ".remote")) {
224                         name = xstrndup(key, strlen(key) - 7);
225                         type = REMOTE;
226                 } else if (!postfixcmp(key, ".merge")) {
227                         name = xstrndup(key, strlen(key) - 6);
228                         type = MERGE;
229                 } else if (!postfixcmp(key, ".rebase")) {
230                         name = xstrndup(key, strlen(key) - 7);
231                         type = REBASE;
232                 } else
233                         return 0;
234
235                 item = string_list_insert(name, &branch_list);
236
237                 if (!item->util)
238                         item->util = xcalloc(sizeof(struct branch_info), 1);
239                 info = item->util;
240                 if (type == REMOTE) {
241                         if (info->remote_name)
242                                 warning("more than one %s", orig_key);
243                         info->remote_name = xstrdup(value);
244                 } else if (type == MERGE) {
245                         char *space = strchr(value, ' ');
246                         value = abbrev_branch(value);
247                         while (space) {
248                                 char *merge;
249                                 merge = xstrndup(value, space - value);
250                                 string_list_append(merge, &info->merge);
251                                 value = abbrev_branch(space + 1);
252                                 space = strchr(value, ' ');
253                         }
254                         string_list_append(xstrdup(value), &info->merge);
255                 } else
256                         info->rebase = git_config_bool(orig_key, value);
257         }
258         return 0;
259 }
260
261 static void read_branches(void)
262 {
263         if (branch_list.nr)
264                 return;
265         git_config(config_read_branches, NULL);
266 }
267
268 struct ref_states {
269         struct remote *remote;
270         struct string_list new, stale, tracked, heads, push;
271         int queried;
272 };
273
274 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
275 {
276         struct ref *fetch_map = NULL, **tail = &fetch_map;
277         struct ref *ref, *stale_refs;
278         int i;
279
280         for (i = 0; i < states->remote->fetch_refspec_nr; i++)
281                 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
282                         die("Could not get fetch map for refspec %s",
283                                 states->remote->fetch_refspec[i]);
284
285         states->new.strdup_strings = 1;
286         states->tracked.strdup_strings = 1;
287         states->stale.strdup_strings = 1;
288         for (ref = fetch_map; ref; ref = ref->next) {
289                 unsigned char sha1[20];
290                 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
291                         string_list_append(abbrev_branch(ref->name), &states->new);
292                 else
293                         string_list_append(abbrev_branch(ref->name), &states->tracked);
294         }
295         stale_refs = get_stale_heads(states->remote, fetch_map);
296         for (ref = stale_refs; ref; ref = ref->next) {
297                 struct string_list_item *item =
298                         string_list_append(abbrev_branch(ref->name), &states->stale);
299                 item->util = xstrdup(ref->name);
300         }
301         free_refs(stale_refs);
302         free_refs(fetch_map);
303
304         sort_string_list(&states->new);
305         sort_string_list(&states->tracked);
306         sort_string_list(&states->stale);
307
308         return 0;
309 }
310
311 struct push_info {
312         char *dest;
313         int forced;
314         enum {
315                 PUSH_STATUS_CREATE = 0,
316                 PUSH_STATUS_DELETE,
317                 PUSH_STATUS_UPTODATE,
318                 PUSH_STATUS_FASTFORWARD,
319                 PUSH_STATUS_OUTOFDATE,
320                 PUSH_STATUS_NOTQUERIED
321         } status;
322 };
323
324 static int get_push_ref_states(const struct ref *remote_refs,
325         struct ref_states *states)
326 {
327         struct remote *remote = states->remote;
328         struct ref *ref, *local_refs, *push_map;
329         if (remote->mirror)
330                 return 0;
331
332         local_refs = get_local_heads();
333         push_map = copy_ref_list(remote_refs);
334
335         match_refs(local_refs, &push_map, remote->push_refspec_nr,
336                    remote->push_refspec, MATCH_REFS_NONE);
337
338         states->push.strdup_strings = 1;
339         for (ref = push_map; ref; ref = ref->next) {
340                 struct string_list_item *item;
341                 struct push_info *info;
342
343                 if (!ref->peer_ref)
344                         continue;
345                 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
346
347                 item = string_list_append(abbrev_branch(ref->peer_ref->name),
348                                           &states->push);
349                 item->util = xcalloc(sizeof(struct push_info), 1);
350                 info = item->util;
351                 info->forced = ref->force;
352                 info->dest = xstrdup(abbrev_branch(ref->name));
353
354                 if (is_null_sha1(ref->new_sha1)) {
355                         info->status = PUSH_STATUS_DELETE;
356                 } else if (!hashcmp(ref->old_sha1, ref->new_sha1))
357                         info->status = PUSH_STATUS_UPTODATE;
358                 else if (is_null_sha1(ref->old_sha1))
359                         info->status = PUSH_STATUS_CREATE;
360                 else if (has_sha1_file(ref->old_sha1) &&
361                          ref_newer(ref->new_sha1, ref->old_sha1))
362                         info->status = PUSH_STATUS_FASTFORWARD;
363                 else
364                         info->status = PUSH_STATUS_OUTOFDATE;
365         }
366         free_refs(local_refs);
367         free_refs(push_map);
368         return 0;
369 }
370
371 static int get_push_ref_states_noquery(struct ref_states *states)
372 {
373         int i;
374         struct remote *remote = states->remote;
375         struct string_list_item *item;
376         struct push_info *info;
377
378         if (remote->mirror)
379                 return 0;
380
381         states->push.strdup_strings = 1;
382         if (!remote->push_refspec_nr) {
383                 item = string_list_append("(matching)", &states->push);
384                 info = item->util = xcalloc(sizeof(struct push_info), 1);
385                 info->status = PUSH_STATUS_NOTQUERIED;
386                 info->dest = xstrdup(item->string);
387         }
388         for (i = 0; i < remote->push_refspec_nr; i++) {
389                 struct refspec *spec = remote->push + i;
390                 if (spec->matching)
391                         item = string_list_append("(matching)", &states->push);
392                 else if (strlen(spec->src))
393                         item = string_list_append(spec->src, &states->push);
394                 else
395                         item = string_list_append("(delete)", &states->push);
396
397                 info = item->util = xcalloc(sizeof(struct push_info), 1);
398                 info->forced = spec->force;
399                 info->status = PUSH_STATUS_NOTQUERIED;
400                 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
401         }
402         return 0;
403 }
404
405 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
406 {
407         struct ref *ref, *matches;
408         struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
409         struct refspec refspec;
410
411         refspec.force = 0;
412         refspec.pattern = 1;
413         refspec.src = refspec.dst = "refs/heads/*";
414         states->heads.strdup_strings = 1;
415         get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
416         matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
417                                     fetch_map, 1);
418         for (ref = matches; ref; ref = ref->next)
419                 string_list_append(abbrev_branch(ref->name), &states->heads);
420
421         free_refs(fetch_map);
422         free_refs(matches);
423
424         return 0;
425 }
426
427 struct known_remote {
428         struct known_remote *next;
429         struct remote *remote;
430 };
431
432 struct known_remotes {
433         struct remote *to_delete;
434         struct known_remote *list;
435 };
436
437 static int add_known_remote(struct remote *remote, void *cb_data)
438 {
439         struct known_remotes *all = cb_data;
440         struct known_remote *r;
441
442         if (!strcmp(all->to_delete->name, remote->name))
443                 return 0;
444
445         r = xmalloc(sizeof(*r));
446         r->remote = remote;
447         r->next = all->list;
448         all->list = r;
449         return 0;
450 }
451
452 struct branches_for_remote {
453         struct remote *remote;
454         struct string_list *branches, *skipped;
455         struct known_remotes *keep;
456 };
457
458 static int add_branch_for_removal(const char *refname,
459         const unsigned char *sha1, int flags, void *cb_data)
460 {
461         struct branches_for_remote *branches = cb_data;
462         struct refspec refspec;
463         struct string_list_item *item;
464         struct known_remote *kr;
465
466         memset(&refspec, 0, sizeof(refspec));
467         refspec.dst = (char *)refname;
468         if (remote_find_tracking(branches->remote, &refspec))
469                 return 0;
470
471         /* don't delete a branch if another remote also uses it */
472         for (kr = branches->keep->list; kr; kr = kr->next) {
473                 memset(&refspec, 0, sizeof(refspec));
474                 refspec.dst = (char *)refname;
475                 if (!remote_find_tracking(kr->remote, &refspec))
476                         return 0;
477         }
478
479         /* don't delete non-remote refs */
480         if (prefixcmp(refname, "refs/remotes")) {
481                 /* advise user how to delete local branches */
482                 if (!prefixcmp(refname, "refs/heads/"))
483                         string_list_append(abbrev_branch(refname),
484                                            branches->skipped);
485                 /* silently skip over other non-remote refs */
486                 return 0;
487         }
488
489         /* make sure that symrefs are deleted */
490         if (flags & REF_ISSYMREF)
491                 return unlink(git_path("%s", refname));
492
493         item = string_list_append(refname, branches->branches);
494         item->util = xmalloc(20);
495         hashcpy(item->util, sha1);
496
497         return 0;
498 }
499
500 struct rename_info {
501         const char *old;
502         const char *new;
503         struct string_list *remote_branches;
504 };
505
506 static int read_remote_branches(const char *refname,
507         const unsigned char *sha1, int flags, void *cb_data)
508 {
509         struct rename_info *rename = cb_data;
510         struct strbuf buf = STRBUF_INIT;
511         struct string_list_item *item;
512         int flag;
513         unsigned char orig_sha1[20];
514         const char *symref;
515
516         strbuf_addf(&buf, "refs/remotes/%s", rename->old);
517         if (!prefixcmp(refname, buf.buf)) {
518                 item = string_list_append(xstrdup(refname), rename->remote_branches);
519                 symref = resolve_ref(refname, orig_sha1, 1, &flag);
520                 if (flag & REF_ISSYMREF)
521                         item->util = xstrdup(symref);
522                 else
523                         item->util = NULL;
524         }
525
526         return 0;
527 }
528
529 static int migrate_file(struct remote *remote)
530 {
531         struct strbuf buf = STRBUF_INIT;
532         int i;
533         char *path = NULL;
534
535         strbuf_addf(&buf, "remote.%s.url", remote->name);
536         for (i = 0; i < remote->url_nr; i++)
537                 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
538                         return error("Could not append '%s' to '%s'",
539                                         remote->url[i], buf.buf);
540         strbuf_reset(&buf);
541         strbuf_addf(&buf, "remote.%s.push", remote->name);
542         for (i = 0; i < remote->push_refspec_nr; i++)
543                 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
544                         return error("Could not append '%s' to '%s'",
545                                         remote->push_refspec[i], buf.buf);
546         strbuf_reset(&buf);
547         strbuf_addf(&buf, "remote.%s.fetch", remote->name);
548         for (i = 0; i < remote->fetch_refspec_nr; i++)
549                 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
550                         return error("Could not append '%s' to '%s'",
551                                         remote->fetch_refspec[i], buf.buf);
552         if (remote->origin == REMOTE_REMOTES)
553                 path = git_path("remotes/%s", remote->name);
554         else if (remote->origin == REMOTE_BRANCHES)
555                 path = git_path("branches/%s", remote->name);
556         if (path)
557                 unlink_or_warn(path);
558         return 0;
559 }
560
561 static int mv(int argc, const char **argv)
562 {
563         struct option options[] = {
564                 OPT_END()
565         };
566         struct remote *oldremote, *newremote;
567         struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
568         struct string_list remote_branches = { NULL, 0, 0, 0 };
569         struct rename_info rename;
570         int i;
571
572         if (argc != 3)
573                 usage_with_options(builtin_remote_rename_usage, options);
574
575         rename.old = argv[1];
576         rename.new = argv[2];
577         rename.remote_branches = &remote_branches;
578
579         oldremote = remote_get(rename.old);
580         if (!oldremote)
581                 die("No such remote: %s", rename.old);
582
583         if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
584                 return migrate_file(oldremote);
585
586         newremote = remote_get(rename.new);
587         if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
588                 die("remote %s already exists.", rename.new);
589
590         strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
591         if (!valid_fetch_refspec(buf.buf))
592                 die("'%s' is not a valid remote name", rename.new);
593
594         strbuf_reset(&buf);
595         strbuf_addf(&buf, "remote.%s", rename.old);
596         strbuf_addf(&buf2, "remote.%s", rename.new);
597         if (git_config_rename_section(buf.buf, buf2.buf) < 1)
598                 return error("Could not rename config section '%s' to '%s'",
599                                 buf.buf, buf2.buf);
600
601         strbuf_reset(&buf);
602         strbuf_addf(&buf, "remote.%s.fetch", rename.new);
603         if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
604                 return error("Could not remove config section '%s'", buf.buf);
605         for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
606                 char *ptr;
607
608                 strbuf_reset(&buf2);
609                 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
610                 ptr = strstr(buf2.buf, rename.old);
611                 if (ptr)
612                         strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
613                                         rename.new, strlen(rename.new));
614                 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
615                         return error("Could not append '%s'", buf.buf);
616         }
617
618         read_branches();
619         for (i = 0; i < branch_list.nr; i++) {
620                 struct string_list_item *item = branch_list.items + i;
621                 struct branch_info *info = item->util;
622                 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
623                         strbuf_reset(&buf);
624                         strbuf_addf(&buf, "branch.%s.remote", item->string);
625                         if (git_config_set(buf.buf, rename.new)) {
626                                 return error("Could not set '%s'", buf.buf);
627                         }
628                 }
629         }
630
631         /*
632          * First remove symrefs, then rename the rest, finally create
633          * the new symrefs.
634          */
635         for_each_ref(read_remote_branches, &rename);
636         for (i = 0; i < remote_branches.nr; i++) {
637                 struct string_list_item *item = remote_branches.items + i;
638                 int flag = 0;
639                 unsigned char sha1[20];
640
641                 resolve_ref(item->string, sha1, 1, &flag);
642                 if (!(flag & REF_ISSYMREF))
643                         continue;
644                 if (delete_ref(item->string, NULL, REF_NODEREF))
645                         die("deleting '%s' failed", item->string);
646         }
647         for (i = 0; i < remote_branches.nr; i++) {
648                 struct string_list_item *item = remote_branches.items + i;
649
650                 if (item->util)
651                         continue;
652                 strbuf_reset(&buf);
653                 strbuf_addstr(&buf, item->string);
654                 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
655                                 rename.new, strlen(rename.new));
656                 strbuf_reset(&buf2);
657                 strbuf_addf(&buf2, "remote: renamed %s to %s",
658                                 item->string, buf.buf);
659                 if (rename_ref(item->string, buf.buf, buf2.buf))
660                         die("renaming '%s' failed", item->string);
661         }
662         for (i = 0; i < remote_branches.nr; i++) {
663                 struct string_list_item *item = remote_branches.items + i;
664
665                 if (!item->util)
666                         continue;
667                 strbuf_reset(&buf);
668                 strbuf_addstr(&buf, item->string);
669                 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
670                                 rename.new, strlen(rename.new));
671                 strbuf_reset(&buf2);
672                 strbuf_addstr(&buf2, item->util);
673                 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
674                                 rename.new, strlen(rename.new));
675                 strbuf_reset(&buf3);
676                 strbuf_addf(&buf3, "remote: renamed %s to %s",
677                                 item->string, buf.buf);
678                 if (create_symref(buf.buf, buf2.buf, buf3.buf))
679                         die("creating '%s' failed", buf.buf);
680         }
681         return 0;
682 }
683
684 static int remove_branches(struct string_list *branches)
685 {
686         int i, result = 0;
687         for (i = 0; i < branches->nr; i++) {
688                 struct string_list_item *item = branches->items + i;
689                 const char *refname = item->string;
690                 unsigned char *sha1 = item->util;
691
692                 if (delete_ref(refname, sha1, 0))
693                         result |= error("Could not remove branch %s", refname);
694         }
695         return result;
696 }
697
698 static int rm(int argc, const char **argv)
699 {
700         struct option options[] = {
701                 OPT_END()
702         };
703         struct remote *remote;
704         struct strbuf buf = STRBUF_INIT;
705         struct known_remotes known_remotes = { NULL, NULL };
706         struct string_list branches = { NULL, 0, 0, 1 };
707         struct string_list skipped = { NULL, 0, 0, 1 };
708         struct branches_for_remote cb_data;
709         int i, result;
710
711         memset(&cb_data, 0, sizeof(cb_data));
712         cb_data.branches = &branches;
713         cb_data.skipped = &skipped;
714         cb_data.keep = &known_remotes;
715
716         if (argc != 2)
717                 usage_with_options(builtin_remote_rm_usage, options);
718
719         remote = remote_get(argv[1]);
720         if (!remote)
721                 die("No such remote: %s", argv[1]);
722
723         known_remotes.to_delete = remote;
724         for_each_remote(add_known_remote, &known_remotes);
725
726         strbuf_addf(&buf, "remote.%s", remote->name);
727         if (git_config_rename_section(buf.buf, NULL) < 1)
728                 return error("Could not remove config section '%s'", buf.buf);
729
730         read_branches();
731         for (i = 0; i < branch_list.nr; i++) {
732                 struct string_list_item *item = branch_list.items + i;
733                 struct branch_info *info = item->util;
734                 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
735                         const char *keys[] = { "remote", "merge", NULL }, **k;
736                         for (k = keys; *k; k++) {
737                                 strbuf_reset(&buf);
738                                 strbuf_addf(&buf, "branch.%s.%s",
739                                                 item->string, *k);
740                                 if (git_config_set(buf.buf, NULL)) {
741                                         strbuf_release(&buf);
742                                         return -1;
743                                 }
744                         }
745                 }
746         }
747
748         /*
749          * We cannot just pass a function to for_each_ref() which deletes
750          * the branches one by one, since for_each_ref() relies on cached
751          * refs, which are invalidated when deleting a branch.
752          */
753         cb_data.remote = remote;
754         result = for_each_ref(add_branch_for_removal, &cb_data);
755         strbuf_release(&buf);
756
757         if (!result)
758                 result = remove_branches(&branches);
759         string_list_clear(&branches, 1);
760
761         if (skipped.nr) {
762                 fprintf(stderr, skipped.nr == 1 ?
763                         "Note: A non-remote branch was not removed; "
764                         "to delete it, use:\n" :
765                         "Note: Non-remote branches were not removed; "
766                         "to delete them, use:\n");
767                 for (i = 0; i < skipped.nr; i++)
768                         fprintf(stderr, "  git branch -d %s\n",
769                                 skipped.items[i].string);
770         }
771         string_list_clear(&skipped, 0);
772
773         return result;
774 }
775
776 static void clear_push_info(void *util, const char *string)
777 {
778         struct push_info *info = util;
779         free(info->dest);
780         free(info);
781 }
782
783 static void free_remote_ref_states(struct ref_states *states)
784 {
785         string_list_clear(&states->new, 0);
786         string_list_clear(&states->stale, 1);
787         string_list_clear(&states->tracked, 0);
788         string_list_clear(&states->heads, 0);
789         string_list_clear_func(&states->push, clear_push_info);
790 }
791
792 static int append_ref_to_tracked_list(const char *refname,
793         const unsigned char *sha1, int flags, void *cb_data)
794 {
795         struct ref_states *states = cb_data;
796         struct refspec refspec;
797
798         if (flags & REF_ISSYMREF)
799                 return 0;
800
801         memset(&refspec, 0, sizeof(refspec));
802         refspec.dst = (char *)refname;
803         if (!remote_find_tracking(states->remote, &refspec))
804                 string_list_append(abbrev_branch(refspec.src), &states->tracked);
805
806         return 0;
807 }
808
809 static int get_remote_ref_states(const char *name,
810                                  struct ref_states *states,
811                                  int query)
812 {
813         struct transport *transport;
814         const struct ref *remote_refs;
815
816         states->remote = remote_get(name);
817         if (!states->remote)
818                 return error("No such remote: %s", name);
819
820         read_branches();
821
822         if (query) {
823                 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
824                         states->remote->url[0] : NULL);
825                 remote_refs = transport_get_remote_refs(transport);
826                 transport_disconnect(transport);
827
828                 states->queried = 1;
829                 if (query & GET_REF_STATES)
830                         get_ref_states(remote_refs, states);
831                 if (query & GET_HEAD_NAMES)
832                         get_head_names(remote_refs, states);
833                 if (query & GET_PUSH_REF_STATES)
834                         get_push_ref_states(remote_refs, states);
835         } else {
836                 for_each_ref(append_ref_to_tracked_list, states);
837                 sort_string_list(&states->tracked);
838                 get_push_ref_states_noquery(states);
839         }
840
841         return 0;
842 }
843
844 struct show_info {
845         struct string_list *list;
846         struct ref_states *states;
847         int width, width2;
848         int any_rebase;
849 };
850
851 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
852 {
853         struct show_info *info = cb_data;
854         int n = strlen(item->string);
855         if (n > info->width)
856                 info->width = n;
857         string_list_insert(item->string, info->list);
858         return 0;
859 }
860
861 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
862 {
863         struct show_info *info = cb_data;
864         struct ref_states *states = info->states;
865         const char *name = item->string;
866
867         if (states->queried) {
868                 const char *fmt = "%s";
869                 const char *arg = "";
870                 if (string_list_has_string(&states->new, name)) {
871                         fmt = " new (next fetch will store in remotes/%s)";
872                         arg = states->remote->name;
873                 } else if (string_list_has_string(&states->tracked, name))
874                         arg = " tracked";
875                 else if (string_list_has_string(&states->stale, name))
876                         arg = " stale (use 'git remote prune' to remove)";
877                 else
878                         arg = " ???";
879                 printf("    %-*s", info->width, name);
880                 printf(fmt, arg);
881                 printf("\n");
882         } else
883                 printf("    %s\n", name);
884
885         return 0;
886 }
887
888 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
889 {
890         struct show_info *show_info = cb_data;
891         struct ref_states *states = show_info->states;
892         struct branch_info *branch_info = branch_item->util;
893         struct string_list_item *item;
894         int n;
895
896         if (!branch_info->merge.nr || !branch_info->remote_name ||
897             strcmp(states->remote->name, branch_info->remote_name))
898                 return 0;
899         if ((n = strlen(branch_item->string)) > show_info->width)
900                 show_info->width = n;
901         if (branch_info->rebase)
902                 show_info->any_rebase = 1;
903
904         item = string_list_insert(branch_item->string, show_info->list);
905         item->util = branch_info;
906
907         return 0;
908 }
909
910 static int show_local_info_item(struct string_list_item *item, void *cb_data)
911 {
912         struct show_info *show_info = cb_data;
913         struct branch_info *branch_info = item->util;
914         struct string_list *merge = &branch_info->merge;
915         const char *also;
916         int i;
917
918         if (branch_info->rebase && branch_info->merge.nr > 1) {
919                 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
920                         item->string);
921                 return 0;
922         }
923
924         printf("    %-*s ", show_info->width, item->string);
925         if (branch_info->rebase) {
926                 printf("rebases onto remote %s\n", merge->items[0].string);
927                 return 0;
928         } else if (show_info->any_rebase) {
929                 printf(" merges with remote %s\n", merge->items[0].string);
930                 also = "    and with remote";
931         } else {
932                 printf("merges with remote %s\n", merge->items[0].string);
933                 also = "   and with remote";
934         }
935         for (i = 1; i < merge->nr; i++)
936                 printf("    %-*s %s %s\n", show_info->width, "", also,
937                        merge->items[i].string);
938
939         return 0;
940 }
941
942 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
943 {
944         struct show_info *show_info = cb_data;
945         struct push_info *push_info = push_item->util;
946         struct string_list_item *item;
947         int n;
948         if ((n = strlen(push_item->string)) > show_info->width)
949                 show_info->width = n;
950         if ((n = strlen(push_info->dest)) > show_info->width2)
951                 show_info->width2 = n;
952         item = string_list_append(push_item->string, show_info->list);
953         item->util = push_item->util;
954         return 0;
955 }
956
957 /*
958  * Sorting comparison for a string list that has push_info
959  * structs in its util field
960  */
961 static int cmp_string_with_push(const void *va, const void *vb)
962 {
963         const struct string_list_item *a = va;
964         const struct string_list_item *b = vb;
965         const struct push_info *a_push = a->util;
966         const struct push_info *b_push = b->util;
967         int cmp = strcmp(a->string, b->string);
968         return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
969 }
970
971 static int show_push_info_item(struct string_list_item *item, void *cb_data)
972 {
973         struct show_info *show_info = cb_data;
974         struct push_info *push_info = item->util;
975         char *src = item->string, *status = NULL;
976
977         switch (push_info->status) {
978         case PUSH_STATUS_CREATE:
979                 status = "create";
980                 break;
981         case PUSH_STATUS_DELETE:
982                 status = "delete";
983                 src = "(none)";
984                 break;
985         case PUSH_STATUS_UPTODATE:
986                 status = "up to date";
987                 break;
988         case PUSH_STATUS_FASTFORWARD:
989                 status = "fast-forwardable";
990                 break;
991         case PUSH_STATUS_OUTOFDATE:
992                 status = "local out of date";
993                 break;
994         case PUSH_STATUS_NOTQUERIED:
995                 break;
996         }
997         if (status)
998                 printf("    %-*s %s to %-*s (%s)\n", show_info->width, src,
999                         push_info->forced ? "forces" : "pushes",
1000                         show_info->width2, push_info->dest, status);
1001         else
1002                 printf("    %-*s %s to %s\n", show_info->width, src,
1003                         push_info->forced ? "forces" : "pushes",
1004                         push_info->dest);
1005         return 0;
1006 }
1007
1008 static int show(int argc, const char **argv)
1009 {
1010         int no_query = 0, result = 0, query_flag = 0;
1011         struct option options[] = {
1012                 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
1013                 OPT_END()
1014         };
1015         struct ref_states states;
1016         struct string_list info_list = { NULL, 0, 0, 0 };
1017         struct show_info info;
1018
1019         argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1020                              0);
1021
1022         if (argc < 1)
1023                 return show_all();
1024
1025         if (!no_query)
1026                 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1027
1028         memset(&states, 0, sizeof(states));
1029         memset(&info, 0, sizeof(info));
1030         info.states = &states;
1031         info.list = &info_list;
1032         for (; argc; argc--, argv++) {
1033                 int i;
1034                 const char **url;
1035                 int url_nr;
1036
1037                 get_remote_ref_states(*argv, &states, query_flag);
1038
1039                 printf("* remote %s\n", *argv);
1040                 printf("  Fetch URL: %s\n", states.remote->url_nr > 0 ?
1041                         states.remote->url[0] : "(no URL)");
1042                 if (states.remote->pushurl_nr) {
1043                         url = states.remote->pushurl;
1044                         url_nr = states.remote->pushurl_nr;
1045                 } else {
1046                         url = states.remote->url;
1047                         url_nr = states.remote->url_nr;
1048                 }
1049                 for (i=0; i < url_nr; i++)
1050                         printf("  Push  URL: %s\n", url[i]);
1051                 if (!i)
1052                         printf("  Push  URL: %s\n", "(no URL)");
1053                 if (no_query)
1054                         printf("  HEAD branch: (not queried)\n");
1055                 else if (!states.heads.nr)
1056                         printf("  HEAD branch: (unknown)\n");
1057                 else if (states.heads.nr == 1)
1058                         printf("  HEAD branch: %s\n", states.heads.items[0].string);
1059                 else {
1060                         printf("  HEAD branch (remote HEAD is ambiguous,"
1061                                " may be one of the following):\n");
1062                         for (i = 0; i < states.heads.nr; i++)
1063                                 printf("    %s\n", states.heads.items[i].string);
1064                 }
1065
1066                 /* remote branch info */
1067                 info.width = 0;
1068                 for_each_string_list(add_remote_to_show_info, &states.new, &info);
1069                 for_each_string_list(add_remote_to_show_info, &states.tracked, &info);
1070                 for_each_string_list(add_remote_to_show_info, &states.stale, &info);
1071                 if (info.list->nr)
1072                         printf("  Remote branch%s:%s\n",
1073                                info.list->nr > 1 ? "es" : "",
1074                                 no_query ? " (status not queried)" : "");
1075                 for_each_string_list(show_remote_info_item, info.list, &info);
1076                 string_list_clear(info.list, 0);
1077
1078                 /* git pull info */
1079                 info.width = 0;
1080                 info.any_rebase = 0;
1081                 for_each_string_list(add_local_to_show_info, &branch_list, &info);
1082                 if (info.list->nr)
1083                         printf("  Local branch%s configured for 'git pull':\n",
1084                                info.list->nr > 1 ? "es" : "");
1085                 for_each_string_list(show_local_info_item, info.list, &info);
1086                 string_list_clear(info.list, 0);
1087
1088                 /* git push info */
1089                 if (states.remote->mirror)
1090                         printf("  Local refs will be mirrored by 'git push'\n");
1091
1092                 info.width = info.width2 = 0;
1093                 for_each_string_list(add_push_to_show_info, &states.push, &info);
1094                 qsort(info.list->items, info.list->nr,
1095                         sizeof(*info.list->items), cmp_string_with_push);
1096                 if (info.list->nr)
1097                         printf("  Local ref%s configured for 'git push'%s:\n",
1098                                 info.list->nr > 1 ? "s" : "",
1099                                 no_query ? " (status not queried)" : "");
1100                 for_each_string_list(show_push_info_item, info.list, &info);
1101                 string_list_clear(info.list, 0);
1102
1103                 free_remote_ref_states(&states);
1104         }
1105
1106         return result;
1107 }
1108
1109 static int set_head(int argc, const char **argv)
1110 {
1111         int i, opt_a = 0, opt_d = 0, result = 0;
1112         struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1113         char *head_name = NULL;
1114
1115         struct option options[] = {
1116                 OPT_BOOLEAN('a', "auto", &opt_a,
1117                             "set refs/remotes/<name>/HEAD according to remote"),
1118                 OPT_BOOLEAN('d', "delete", &opt_d,
1119                             "delete refs/remotes/<name>/HEAD"),
1120                 OPT_END()
1121         };
1122         argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1123                              0);
1124         if (argc)
1125                 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1126
1127         if (!opt_a && !opt_d && argc == 2) {
1128                 head_name = xstrdup(argv[1]);
1129         } else if (opt_a && !opt_d && argc == 1) {
1130                 struct ref_states states;
1131                 memset(&states, 0, sizeof(states));
1132                 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1133                 if (!states.heads.nr)
1134                         result |= error("Cannot determine remote HEAD");
1135                 else if (states.heads.nr > 1) {
1136                         result |= error("Multiple remote HEAD branches. "
1137                                         "Please choose one explicitly with:");
1138                         for (i = 0; i < states.heads.nr; i++)
1139                                 fprintf(stderr, "  git remote set-head %s %s\n",
1140                                         argv[0], states.heads.items[i].string);
1141                 } else
1142                         head_name = xstrdup(states.heads.items[0].string);
1143                 free_remote_ref_states(&states);
1144         } else if (opt_d && !opt_a && argc == 1) {
1145                 if (delete_ref(buf.buf, NULL, REF_NODEREF))
1146                         result |= error("Could not delete %s", buf.buf);
1147         } else
1148                 usage_with_options(builtin_remote_sethead_usage, options);
1149
1150         if (head_name) {
1151                 unsigned char sha1[20];
1152                 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1153                 /* make sure it's valid */
1154                 if (!resolve_ref(buf2.buf, sha1, 1, NULL))
1155                         result |= error("Not a valid ref: %s", buf2.buf);
1156                 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1157                         result |= error("Could not setup %s", buf.buf);
1158                 if (opt_a)
1159                         printf("%s/HEAD set to %s\n", argv[0], head_name);
1160                 free(head_name);
1161         }
1162
1163         strbuf_release(&buf);
1164         strbuf_release(&buf2);
1165         return result;
1166 }
1167
1168 static int prune(int argc, const char **argv)
1169 {
1170         int dry_run = 0, result = 0;
1171         struct option options[] = {
1172                 OPT__DRY_RUN(&dry_run),
1173                 OPT_END()
1174         };
1175
1176         argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1177                              0);
1178
1179         if (argc < 1)
1180                 usage_with_options(builtin_remote_prune_usage, options);
1181
1182         for (; argc; argc--, argv++)
1183                 result |= prune_remote(*argv, dry_run);
1184
1185         return result;
1186 }
1187
1188 static int prune_remote(const char *remote, int dry_run)
1189 {
1190         int result = 0, i;
1191         struct ref_states states;
1192         const char *dangling_msg = dry_run
1193                 ? " %s will become dangling!\n"
1194                 : " %s has become dangling!\n";
1195
1196         memset(&states, 0, sizeof(states));
1197         get_remote_ref_states(remote, &states, GET_REF_STATES);
1198
1199         if (states.stale.nr) {
1200                 printf("Pruning %s\n", remote);
1201                 printf("URL: %s\n",
1202                        states.remote->url_nr
1203                        ? states.remote->url[0]
1204                        : "(no URL)");
1205         }
1206
1207         for (i = 0; i < states.stale.nr; i++) {
1208                 const char *refname = states.stale.items[i].util;
1209
1210                 if (!dry_run)
1211                         result |= delete_ref(refname, NULL, 0);
1212
1213                 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
1214                        abbrev_ref(refname, "refs/remotes/"));
1215                 warn_dangling_symref(stdout, dangling_msg, refname);
1216         }
1217
1218         free_remote_ref_states(&states);
1219         return result;
1220 }
1221
1222 static int get_remote_default(const char *key, const char *value, void *priv)
1223 {
1224         if (strcmp(key, "remotes.default") == 0) {
1225                 int *found = priv;
1226                 *found = 1;
1227         }
1228         return 0;
1229 }
1230
1231 static int update(int argc, const char **argv)
1232 {
1233         int i, prune = 0;
1234         struct option options[] = {
1235                 OPT_BOOLEAN('p', "prune", &prune,
1236                             "prune remotes after fetching"),
1237                 OPT_END()
1238         };
1239         const char **fetch_argv;
1240         int fetch_argc = 0;
1241         int default_defined = 0;
1242
1243         fetch_argv = xmalloc(sizeof(char *) * (argc+5));
1244
1245         argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1246                              PARSE_OPT_KEEP_ARGV0);
1247
1248         fetch_argv[fetch_argc++] = "fetch";
1249
1250         if (prune)
1251                 fetch_argv[fetch_argc++] = "--prune";
1252         if (verbose)
1253                 fetch_argv[fetch_argc++] = "-v";
1254         fetch_argv[fetch_argc++] = "--multiple";
1255         if (argc < 2)
1256                 fetch_argv[fetch_argc++] = "default";
1257         for (i = 1; i < argc; i++)
1258                 fetch_argv[fetch_argc++] = argv[i];
1259
1260         if (strcmp(fetch_argv[fetch_argc-1], "default") == 0) {
1261                 git_config(get_remote_default, &default_defined);
1262                 if (!default_defined)
1263                         fetch_argv[fetch_argc-1] = "--all";
1264         }
1265
1266         fetch_argv[fetch_argc] = NULL;
1267
1268         return run_command_v_opt(fetch_argv, RUN_GIT_CMD);
1269 }
1270
1271 static int set_url(int argc, const char **argv)
1272 {
1273         int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1274         int matches = 0, negative_matches = 0;
1275         const char *remotename = NULL;
1276         const char *newurl = NULL;
1277         const char *oldurl = NULL;
1278         struct remote *remote;
1279         regex_t old_regex;
1280         const char **urlset;
1281         int urlset_nr;
1282         struct strbuf name_buf = STRBUF_INIT;
1283         struct option options[] = {
1284                 OPT_BOOLEAN('\0', "push", &push_mode,
1285                             "manipulate push URLs"),
1286                 OPT_BOOLEAN('\0', "add", &add_mode,
1287                             "add URL"),
1288                 OPT_BOOLEAN('\0', "delete", &delete_mode,
1289                             "delete URLs"),
1290                 OPT_END()
1291         };
1292         argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1293                              PARSE_OPT_KEEP_ARGV0);
1294
1295         if (add_mode && delete_mode)
1296                 die("--add --delete doesn't make sense");
1297
1298         if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1299                 usage_with_options(builtin_remote_seturl_usage, options);
1300
1301         remotename = argv[1];
1302         newurl = argv[2];
1303         if (argc > 3)
1304                 oldurl = argv[3];
1305
1306         if (delete_mode)
1307                 oldurl = newurl;
1308
1309         if (!remote_is_configured(remotename))
1310                 die("No such remote '%s'", remotename);
1311         remote = remote_get(remotename);
1312
1313         if (push_mode) {
1314                 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1315                 urlset = remote->pushurl;
1316                 urlset_nr = remote->pushurl_nr;
1317         } else {
1318                 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1319                 urlset = remote->url;
1320                 urlset_nr = remote->url_nr;
1321         }
1322
1323         /* Special cases that add new entry. */
1324         if ((!oldurl && !delete_mode) || add_mode) {
1325                 if (add_mode)
1326                         git_config_set_multivar(name_buf.buf, newurl,
1327                                 "^$", 0);
1328                 else
1329                         git_config_set(name_buf.buf, newurl);
1330                 strbuf_release(&name_buf);
1331                 return 0;
1332         }
1333
1334         /* Old URL specified. Demand that one matches. */
1335         if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1336                 die("Invalid old URL pattern: %s", oldurl);
1337
1338         for (i = 0; i < urlset_nr; i++)
1339                 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1340                         matches++;
1341                 else
1342                         negative_matches++;
1343         if (!delete_mode && !matches)
1344                 die("No such URL found: %s", oldurl);
1345         if (delete_mode && !negative_matches && !push_mode)
1346                 die("Will not delete all non-push URLs");
1347
1348         regfree(&old_regex);
1349
1350         if (!delete_mode)
1351                 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1352         else
1353                 git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
1354         return 0;
1355 }
1356
1357 static int get_one_entry(struct remote *remote, void *priv)
1358 {
1359         struct string_list *list = priv;
1360         struct strbuf url_buf = STRBUF_INIT;
1361         const char **url;
1362         int i, url_nr;
1363
1364         if (remote->url_nr > 0) {
1365                 strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
1366                 string_list_append(remote->name, list)->util =
1367                                 strbuf_detach(&url_buf, NULL);
1368         } else
1369                 string_list_append(remote->name, list)->util = NULL;
1370         if (remote->pushurl_nr) {
1371                 url = remote->pushurl;
1372                 url_nr = remote->pushurl_nr;
1373         } else {
1374                 url = remote->url;
1375                 url_nr = remote->url_nr;
1376         }
1377         for (i = 0; i < url_nr; i++)
1378         {
1379                 strbuf_addf(&url_buf, "%s (push)", url[i]);
1380                 string_list_append(remote->name, list)->util =
1381                                 strbuf_detach(&url_buf, NULL);
1382         }
1383
1384         return 0;
1385 }
1386
1387 static int show_all(void)
1388 {
1389         struct string_list list = { NULL, 0, 0 };
1390         int result;
1391
1392         list.strdup_strings = 1;
1393         result = for_each_remote(get_one_entry, &list);
1394
1395         if (!result) {
1396                 int i;
1397
1398                 sort_string_list(&list);
1399                 for (i = 0; i < list.nr; i++) {
1400                         struct string_list_item *item = list.items + i;
1401                         if (verbose)
1402                                 printf("%s\t%s\n", item->string,
1403                                         item->util ? (const char *)item->util : "");
1404                         else {
1405                                 if (i && !strcmp((item - 1)->string, item->string))
1406                                         continue;
1407                                 printf("%s\n", item->string);
1408                         }
1409                 }
1410         }
1411         string_list_clear(&list, 1);
1412         return result;
1413 }
1414
1415 int cmd_remote(int argc, const char **argv, const char *prefix)
1416 {
1417         struct option options[] = {
1418                 OPT_BOOLEAN('v', "verbose", &verbose, "be verbose; must be placed before a subcommand"),
1419                 OPT_END()
1420         };
1421         int result;
1422
1423         argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1424                 PARSE_OPT_STOP_AT_NON_OPTION);
1425
1426         if (argc < 1)
1427                 result = show_all();
1428         else if (!strcmp(argv[0], "add"))
1429                 result = add(argc, argv);
1430         else if (!strcmp(argv[0], "rename"))
1431                 result = mv(argc, argv);
1432         else if (!strcmp(argv[0], "rm"))
1433                 result = rm(argc, argv);
1434         else if (!strcmp(argv[0], "set-head"))
1435                 result = set_head(argc, argv);
1436         else if (!strcmp(argv[0], "set-url"))
1437                 result = set_url(argc, argv);
1438         else if (!strcmp(argv[0], "show"))
1439                 result = show(argc, argv);
1440         else if (!strcmp(argv[0], "prune"))
1441                 result = prune(argc, argv);
1442         else if (!strcmp(argv[0], "update"))
1443                 result = update(argc, argv);
1444         else {
1445                 error("Unknown subcommand: %s", argv[0]);
1446                 usage_with_options(builtin_remote_usage, options);
1447         }
1448
1449         return result ? 1 : 0;
1450 }