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