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