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