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