builtin-remote: teach show to display remote HEAD
[git/mingw.git] / builtin-remote.c
blob4543cf082653f7273a44dcc9c18914302d688d3e
1 #include "cache.h"
2 #include "parse-options.h"
3 #include "transport.h"
4 #include "remote.h"
5 #include "string-list.h"
6 #include "strbuf.h"
7 #include "run-command.h"
8 #include "refs.h"
10 static const char * const builtin_remote_usage[] = {
11 "git remote [-v | --verbose]",
12 "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
13 "git remote rename <old> <new>",
14 "git remote rm <name>",
15 "git remote show [-n] <name>",
16 "git remote prune [-n | --dry-run] <name>",
17 "git remote [-v | --verbose] update [group]",
18 NULL
21 #define GET_REF_STATES (1<<0)
22 #define GET_HEAD_NAMES (1<<1)
24 static int verbose;
26 static int show_all(void);
28 static inline int postfixcmp(const char *string, const char *postfix)
30 int len1 = strlen(string), len2 = strlen(postfix);
31 if (len1 < len2)
32 return 1;
33 return strcmp(string + len1 - len2, postfix);
36 static int opt_parse_track(const struct option *opt, const char *arg, int not)
38 struct string_list *list = opt->value;
39 if (not)
40 string_list_clear(list, 0);
41 else
42 string_list_append(arg, list);
43 return 0;
46 static int fetch_remote(const char *name)
48 const char *argv[] = { "fetch", name, NULL, NULL };
49 if (verbose) {
50 argv[1] = "-v";
51 argv[2] = name;
53 printf("Updating %s\n", name);
54 if (run_command_v_opt(argv, RUN_GIT_CMD))
55 return error("Could not fetch %s", name);
56 return 0;
59 static int add(int argc, const char **argv)
61 int fetch = 0, mirror = 0;
62 struct string_list track = { NULL, 0, 0 };
63 const char *master = NULL;
64 struct remote *remote;
65 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
66 const char *name, *url;
67 int i;
69 struct option options[] = {
70 OPT_GROUP("add specific options"),
71 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
72 OPT_CALLBACK('t', "track", &track, "branch",
73 "branch(es) to track", opt_parse_track),
74 OPT_STRING('m', "master", &master, "branch", "master branch"),
75 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
76 OPT_END()
79 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
81 if (argc < 2)
82 usage_with_options(builtin_remote_usage, options);
84 name = argv[0];
85 url = argv[1];
87 remote = remote_get(name);
88 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
89 remote->fetch_refspec_nr))
90 die("remote %s already exists.", name);
92 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
93 if (!valid_fetch_refspec(buf2.buf))
94 die("'%s' is not a valid remote name", name);
96 strbuf_addf(&buf, "remote.%s.url", name);
97 if (git_config_set(buf.buf, url))
98 return 1;
100 strbuf_reset(&buf);
101 strbuf_addf(&buf, "remote.%s.fetch", name);
103 if (track.nr == 0)
104 string_list_append("*", &track);
105 for (i = 0; i < track.nr; i++) {
106 struct string_list_item *item = track.items + i;
108 strbuf_reset(&buf2);
109 strbuf_addch(&buf2, '+');
110 if (mirror)
111 strbuf_addf(&buf2, "refs/%s:refs/%s",
112 item->string, item->string);
113 else
114 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
115 item->string, name, item->string);
116 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
117 return 1;
120 if (mirror) {
121 strbuf_reset(&buf);
122 strbuf_addf(&buf, "remote.%s.mirror", name);
123 if (git_config_set(buf.buf, "true"))
124 return 1;
127 if (fetch && fetch_remote(name))
128 return 1;
130 if (master) {
131 strbuf_reset(&buf);
132 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
134 strbuf_reset(&buf2);
135 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
137 if (create_symref(buf.buf, buf2.buf, "remote add"))
138 return error("Could not setup master '%s'", master);
141 strbuf_release(&buf);
142 strbuf_release(&buf2);
143 string_list_clear(&track, 0);
145 return 0;
148 struct branch_info {
149 char *remote_name;
150 struct string_list merge;
153 static struct string_list branch_list;
155 static const char *abbrev_ref(const char *name, const char *prefix)
157 const char *abbrev = skip_prefix(name, prefix);
158 if (abbrev)
159 return abbrev;
160 return name;
162 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
164 static int config_read_branches(const char *key, const char *value, void *cb)
166 if (!prefixcmp(key, "branch.")) {
167 char *name;
168 struct string_list_item *item;
169 struct branch_info *info;
170 enum { REMOTE, MERGE } type;
172 key += 7;
173 if (!postfixcmp(key, ".remote")) {
174 name = xstrndup(key, strlen(key) - 7);
175 type = REMOTE;
176 } else if (!postfixcmp(key, ".merge")) {
177 name = xstrndup(key, strlen(key) - 6);
178 type = MERGE;
179 } else
180 return 0;
182 item = string_list_insert(name, &branch_list);
184 if (!item->util)
185 item->util = xcalloc(sizeof(struct branch_info), 1);
186 info = item->util;
187 if (type == REMOTE) {
188 if (info->remote_name)
189 warning("more than one branch.%s", key);
190 info->remote_name = xstrdup(value);
191 } else {
192 char *space = strchr(value, ' ');
193 value = abbrev_branch(value);
194 while (space) {
195 char *merge;
196 merge = xstrndup(value, space - value);
197 string_list_append(merge, &info->merge);
198 value = abbrev_branch(space + 1);
199 space = strchr(value, ' ');
201 string_list_append(xstrdup(value), &info->merge);
204 return 0;
207 static void read_branches(void)
209 if (branch_list.nr)
210 return;
211 git_config(config_read_branches, NULL);
214 struct ref_states {
215 struct remote *remote;
216 struct string_list new, stale, tracked, heads;
219 static int handle_one_branch(const char *refname,
220 const unsigned char *sha1, int flags, void *cb_data)
222 struct ref_states *states = cb_data;
223 struct refspec refspec;
225 memset(&refspec, 0, sizeof(refspec));
226 refspec.dst = (char *)refname;
227 if (!remote_find_tracking(states->remote, &refspec)) {
228 struct string_list_item *item;
229 const char *name = abbrev_branch(refspec.src);
230 /* symbolic refs pointing nowhere were handled already */
231 if ((flags & REF_ISSYMREF) ||
232 string_list_has_string(&states->tracked, name) ||
233 string_list_has_string(&states->new, name))
234 return 0;
235 item = string_list_append(name, &states->stale);
236 item->util = xstrdup(refname);
238 return 0;
241 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
243 struct ref *fetch_map = NULL, **tail = &fetch_map;
244 struct ref *ref;
245 int i;
247 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
248 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
249 die("Could not get fetch map for refspec %s",
250 states->remote->fetch_refspec[i]);
252 states->new.strdup_strings = states->tracked.strdup_strings = 1;
253 for (ref = fetch_map; ref; ref = ref->next) {
254 unsigned char sha1[20];
255 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
256 string_list_append(abbrev_branch(ref->name), &states->new);
257 else
258 string_list_append(abbrev_branch(ref->name), &states->tracked);
260 free_refs(fetch_map);
262 sort_string_list(&states->new);
263 sort_string_list(&states->tracked);
264 for_each_ref(handle_one_branch, states);
265 sort_string_list(&states->stale);
267 return 0;
270 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
272 struct ref *ref, *matches;
273 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
274 struct refspec refspec;
276 refspec.force = 0;
277 refspec.pattern = 1;
278 refspec.src = refspec.dst = "refs/heads/";
279 states->heads.strdup_strings = 1;
280 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
281 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
282 fetch_map, 1);
283 for(ref = matches; ref; ref = ref->next)
284 string_list_append(abbrev_branch(ref->name), &states->heads);
286 free_refs(fetch_map);
287 free_refs(matches);
289 return 0;
292 struct known_remote {
293 struct known_remote *next;
294 struct remote *remote;
297 struct known_remotes {
298 struct remote *to_delete;
299 struct known_remote *list;
302 static int add_known_remote(struct remote *remote, void *cb_data)
304 struct known_remotes *all = cb_data;
305 struct known_remote *r;
307 if (!strcmp(all->to_delete->name, remote->name))
308 return 0;
310 r = xmalloc(sizeof(*r));
311 r->remote = remote;
312 r->next = all->list;
313 all->list = r;
314 return 0;
317 struct branches_for_remote {
318 struct remote *remote;
319 struct string_list *branches, *skipped;
320 struct known_remotes *keep;
323 static int add_branch_for_removal(const char *refname,
324 const unsigned char *sha1, int flags, void *cb_data)
326 struct branches_for_remote *branches = cb_data;
327 struct refspec refspec;
328 struct string_list_item *item;
329 struct known_remote *kr;
331 memset(&refspec, 0, sizeof(refspec));
332 refspec.dst = (char *)refname;
333 if (remote_find_tracking(branches->remote, &refspec))
334 return 0;
336 /* don't delete a branch if another remote also uses it */
337 for (kr = branches->keep->list; kr; kr = kr->next) {
338 memset(&refspec, 0, sizeof(refspec));
339 refspec.dst = (char *)refname;
340 if (!remote_find_tracking(kr->remote, &refspec))
341 return 0;
344 /* don't delete non-remote refs */
345 if (prefixcmp(refname, "refs/remotes")) {
346 /* advise user how to delete local branches */
347 if (!prefixcmp(refname, "refs/heads/"))
348 string_list_append(abbrev_branch(refname),
349 branches->skipped);
350 /* silently skip over other non-remote refs */
351 return 0;
354 /* make sure that symrefs are deleted */
355 if (flags & REF_ISSYMREF)
356 return unlink(git_path("%s", refname));
358 item = string_list_append(refname, branches->branches);
359 item->util = xmalloc(20);
360 hashcpy(item->util, sha1);
362 return 0;
365 struct rename_info {
366 const char *old;
367 const char *new;
368 struct string_list *remote_branches;
371 static int read_remote_branches(const char *refname,
372 const unsigned char *sha1, int flags, void *cb_data)
374 struct rename_info *rename = cb_data;
375 struct strbuf buf = STRBUF_INIT;
376 struct string_list_item *item;
377 int flag;
378 unsigned char orig_sha1[20];
379 const char *symref;
381 strbuf_addf(&buf, "refs/remotes/%s", rename->old);
382 if(!prefixcmp(refname, buf.buf)) {
383 item = string_list_append(xstrdup(refname), rename->remote_branches);
384 symref = resolve_ref(refname, orig_sha1, 1, &flag);
385 if (flag & REF_ISSYMREF)
386 item->util = xstrdup(symref);
387 else
388 item->util = NULL;
391 return 0;
394 static int migrate_file(struct remote *remote)
396 struct strbuf buf = STRBUF_INIT;
397 int i;
398 char *path = NULL;
400 strbuf_addf(&buf, "remote.%s.url", remote->name);
401 for (i = 0; i < remote->url_nr; i++)
402 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
403 return error("Could not append '%s' to '%s'",
404 remote->url[i], buf.buf);
405 strbuf_reset(&buf);
406 strbuf_addf(&buf, "remote.%s.push", remote->name);
407 for (i = 0; i < remote->push_refspec_nr; i++)
408 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
409 return error("Could not append '%s' to '%s'",
410 remote->push_refspec[i], buf.buf);
411 strbuf_reset(&buf);
412 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
413 for (i = 0; i < remote->fetch_refspec_nr; i++)
414 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
415 return error("Could not append '%s' to '%s'",
416 remote->fetch_refspec[i], buf.buf);
417 if (remote->origin == REMOTE_REMOTES)
418 path = git_path("remotes/%s", remote->name);
419 else if (remote->origin == REMOTE_BRANCHES)
420 path = git_path("branches/%s", remote->name);
421 if (path && unlink(path))
422 warning("failed to remove '%s'", path);
423 return 0;
426 static int mv(int argc, const char **argv)
428 struct option options[] = {
429 OPT_END()
431 struct remote *oldremote, *newremote;
432 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
433 struct string_list remote_branches = { NULL, 0, 0, 0 };
434 struct rename_info rename;
435 int i;
437 if (argc != 3)
438 usage_with_options(builtin_remote_usage, options);
440 rename.old = argv[1];
441 rename.new = argv[2];
442 rename.remote_branches = &remote_branches;
444 oldremote = remote_get(rename.old);
445 if (!oldremote)
446 die("No such remote: %s", rename.old);
448 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
449 return migrate_file(oldremote);
451 newremote = remote_get(rename.new);
452 if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
453 die("remote %s already exists.", rename.new);
455 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
456 if (!valid_fetch_refspec(buf.buf))
457 die("'%s' is not a valid remote name", rename.new);
459 strbuf_reset(&buf);
460 strbuf_addf(&buf, "remote.%s", rename.old);
461 strbuf_addf(&buf2, "remote.%s", rename.new);
462 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
463 return error("Could not rename config section '%s' to '%s'",
464 buf.buf, buf2.buf);
466 strbuf_reset(&buf);
467 strbuf_addf(&buf, "remote.%s.fetch", rename.new);
468 if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
469 return error("Could not remove config section '%s'", buf.buf);
470 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
471 char *ptr;
473 strbuf_reset(&buf2);
474 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
475 ptr = strstr(buf2.buf, rename.old);
476 if (ptr)
477 strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
478 rename.new, strlen(rename.new));
479 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
480 return error("Could not append '%s'", buf.buf);
483 read_branches();
484 for (i = 0; i < branch_list.nr; i++) {
485 struct string_list_item *item = branch_list.items + i;
486 struct branch_info *info = item->util;
487 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
488 strbuf_reset(&buf);
489 strbuf_addf(&buf, "branch.%s.remote", item->string);
490 if (git_config_set(buf.buf, rename.new)) {
491 return error("Could not set '%s'", buf.buf);
497 * First remove symrefs, then rename the rest, finally create
498 * the new symrefs.
500 for_each_ref(read_remote_branches, &rename);
501 for (i = 0; i < remote_branches.nr; i++) {
502 struct string_list_item *item = remote_branches.items + i;
503 int flag = 0;
504 unsigned char sha1[20];
505 const char *symref;
507 symref = resolve_ref(item->string, sha1, 1, &flag);
508 if (!(flag & REF_ISSYMREF))
509 continue;
510 if (delete_ref(item->string, NULL, REF_NODEREF))
511 die("deleting '%s' failed", item->string);
513 for (i = 0; i < remote_branches.nr; i++) {
514 struct string_list_item *item = remote_branches.items + i;
516 if (item->util)
517 continue;
518 strbuf_reset(&buf);
519 strbuf_addstr(&buf, item->string);
520 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
521 rename.new, strlen(rename.new));
522 strbuf_reset(&buf2);
523 strbuf_addf(&buf2, "remote: renamed %s to %s",
524 item->string, buf.buf);
525 if (rename_ref(item->string, buf.buf, buf2.buf))
526 die("renaming '%s' failed", item->string);
528 for (i = 0; i < remote_branches.nr; i++) {
529 struct string_list_item *item = remote_branches.items + i;
531 if (!item->util)
532 continue;
533 strbuf_reset(&buf);
534 strbuf_addstr(&buf, item->string);
535 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
536 rename.new, strlen(rename.new));
537 strbuf_reset(&buf2);
538 strbuf_addstr(&buf2, item->util);
539 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
540 rename.new, strlen(rename.new));
541 strbuf_reset(&buf3);
542 strbuf_addf(&buf3, "remote: renamed %s to %s",
543 item->string, buf.buf);
544 if (create_symref(buf.buf, buf2.buf, buf3.buf))
545 die("creating '%s' failed", buf.buf);
547 return 0;
550 static int remove_branches(struct string_list *branches)
552 int i, result = 0;
553 for (i = 0; i < branches->nr; i++) {
554 struct string_list_item *item = branches->items + i;
555 const char *refname = item->string;
556 unsigned char *sha1 = item->util;
558 if (delete_ref(refname, sha1, 0))
559 result |= error("Could not remove branch %s", refname);
561 return result;
564 static int rm(int argc, const char **argv)
566 struct option options[] = {
567 OPT_END()
569 struct remote *remote;
570 struct strbuf buf = STRBUF_INIT;
571 struct known_remotes known_remotes = { NULL, NULL };
572 struct string_list branches = { NULL, 0, 0, 1 };
573 struct string_list skipped = { NULL, 0, 0, 1 };
574 struct branches_for_remote cb_data = {
575 NULL, &branches, &skipped, &known_remotes
577 int i, result;
579 if (argc != 2)
580 usage_with_options(builtin_remote_usage, options);
582 remote = remote_get(argv[1]);
583 if (!remote)
584 die("No such remote: %s", argv[1]);
586 known_remotes.to_delete = remote;
587 for_each_remote(add_known_remote, &known_remotes);
589 strbuf_addf(&buf, "remote.%s", remote->name);
590 if (git_config_rename_section(buf.buf, NULL) < 1)
591 return error("Could not remove config section '%s'", buf.buf);
593 read_branches();
594 for (i = 0; i < branch_list.nr; i++) {
595 struct string_list_item *item = branch_list.items + i;
596 struct branch_info *info = item->util;
597 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
598 const char *keys[] = { "remote", "merge", NULL }, **k;
599 for (k = keys; *k; k++) {
600 strbuf_reset(&buf);
601 strbuf_addf(&buf, "branch.%s.%s",
602 item->string, *k);
603 if (git_config_set(buf.buf, NULL)) {
604 strbuf_release(&buf);
605 return -1;
612 * We cannot just pass a function to for_each_ref() which deletes
613 * the branches one by one, since for_each_ref() relies on cached
614 * refs, which are invalidated when deleting a branch.
616 cb_data.remote = remote;
617 result = for_each_ref(add_branch_for_removal, &cb_data);
618 strbuf_release(&buf);
620 if (!result)
621 result = remove_branches(&branches);
622 string_list_clear(&branches, 1);
624 if (skipped.nr) {
625 fprintf(stderr, skipped.nr == 1 ?
626 "Note: A non-remote branch was not removed; "
627 "to delete it, use:\n" :
628 "Note: Non-remote branches were not removed; "
629 "to delete them, use:\n");
630 for (i = 0; i < skipped.nr; i++)
631 fprintf(stderr, " git branch -d %s\n",
632 skipped.items[i].string);
634 string_list_clear(&skipped, 0);
636 return result;
639 static void show_list(const char *title, struct string_list *list,
640 const char *extra_arg)
642 int i;
644 if (!list->nr)
645 return;
647 printf(title, list->nr > 1 ? "es" : "", extra_arg);
648 printf("\n");
649 for (i = 0; i < list->nr; i++)
650 printf(" %s\n", list->items[i].string);
653 static void free_remote_ref_states(struct ref_states *states)
655 string_list_clear(&states->new, 0);
656 string_list_clear(&states->stale, 0);
657 string_list_clear(&states->tracked, 0);
658 string_list_clear(&states->heads, 0);
661 static int append_ref_to_tracked_list(const char *refname,
662 const unsigned char *sha1, int flags, void *cb_data)
664 struct ref_states *states = cb_data;
665 struct refspec refspec;
667 if (flags & REF_ISSYMREF)
668 return 0;
670 memset(&refspec, 0, sizeof(refspec));
671 refspec.dst = (char *)refname;
672 if (!remote_find_tracking(states->remote, &refspec))
673 string_list_append(abbrev_branch(refspec.src), &states->tracked);
675 return 0;
678 static int get_remote_ref_states(const char *name,
679 struct ref_states *states,
680 int query)
682 struct transport *transport;
683 const struct ref *remote_refs;
685 states->remote = remote_get(name);
686 if (!states->remote)
687 return error("No such remote: %s", name);
689 read_branches();
691 if (query) {
692 transport = transport_get(NULL, states->remote->url_nr > 0 ?
693 states->remote->url[0] : NULL);
694 remote_refs = transport_get_remote_refs(transport);
695 transport_disconnect(transport);
697 if (query & GET_REF_STATES)
698 get_ref_states(remote_refs, states);
699 if (query & GET_HEAD_NAMES)
700 get_head_names(remote_refs, states);
701 } else {
702 for_each_ref(append_ref_to_tracked_list, states);
703 sort_string_list(&states->tracked);
706 return 0;
709 static int show(int argc, const char **argv)
711 int no_query = 0, result = 0, query_flag = 0;
712 struct option options[] = {
713 OPT_GROUP("show specific options"),
714 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
715 OPT_END()
717 struct ref_states states;
719 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
721 if (argc < 1)
722 return show_all();
724 if (!no_query)
725 query_flag = (GET_REF_STATES | GET_HEAD_NAMES);
727 memset(&states, 0, sizeof(states));
728 for (; argc; argc--, argv++) {
729 int i;
731 get_remote_ref_states(*argv, &states, query_flag);
733 printf("* remote %s\n URL: %s\n", *argv,
734 states.remote->url_nr > 0 ?
735 states.remote->url[0] : "(no URL)");
736 if (no_query)
737 printf(" HEAD branch: (not queried)\n");
738 else if (!states.heads.nr)
739 printf(" HEAD branch: (unknown)\n");
740 else if (states.heads.nr == 1)
741 printf(" HEAD branch: %s\n", states.heads.items[0].string);
742 else {
743 printf(" HEAD branch (remote HEAD is ambiguous,"
744 " may be one of the following):\n");
745 for (i = 0; i < states.heads.nr; i++)
746 printf(" %s\n", states.heads.items[i].string);
749 for (i = 0; i < branch_list.nr; i++) {
750 struct string_list_item *branch = branch_list.items + i;
751 struct branch_info *info = branch->util;
752 int j;
754 if (!info->merge.nr || strcmp(*argv, info->remote_name))
755 continue;
756 printf(" Remote branch%s merged with 'git pull' "
757 "while on branch %s\n ",
758 info->merge.nr > 1 ? "es" : "",
759 branch->string);
760 for (j = 0; j < info->merge.nr; j++)
761 printf(" %s", info->merge.items[j].string);
762 printf("\n");
765 if (!no_query) {
766 show_list(" New remote branch%s (next fetch "
767 "will store in remotes/%s)",
768 &states.new, states.remote->name);
769 show_list(" Stale tracking branch%s (use 'git remote "
770 "prune')", &states.stale, "");
773 show_list(" Tracked remote branch%s", &states.tracked, "");
775 if (states.remote->push_refspec_nr) {
776 printf(" Local branch%s pushed with 'git push'\n",
777 states.remote->push_refspec_nr > 1 ?
778 "es" : "");
779 for (i = 0; i < states.remote->push_refspec_nr; i++) {
780 struct refspec *spec = states.remote->push + i;
781 printf(" %s%s%s%s\n",
782 spec->force ? "+" : "",
783 abbrev_branch(spec->src),
784 spec->dst ? ":" : "",
785 spec->dst ? abbrev_branch(spec->dst) : "");
789 free_remote_ref_states(&states);
792 return result;
795 static int prune(int argc, const char **argv)
797 int dry_run = 0, result = 0;
798 struct option options[] = {
799 OPT_GROUP("prune specific options"),
800 OPT__DRY_RUN(&dry_run),
801 OPT_END()
803 struct ref_states states;
804 const char *dangling_msg;
806 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
808 if (argc < 1)
809 usage_with_options(builtin_remote_usage, options);
811 dangling_msg = (dry_run
812 ? " %s will become dangling!\n"
813 : " %s has become dangling!\n");
815 memset(&states, 0, sizeof(states));
816 for (; argc; argc--, argv++) {
817 int i;
819 get_remote_ref_states(*argv, &states, GET_REF_STATES);
821 if (states.stale.nr) {
822 printf("Pruning %s\n", *argv);
823 printf("URL: %s\n",
824 states.remote->url_nr
825 ? states.remote->url[0]
826 : "(no URL)");
829 for (i = 0; i < states.stale.nr; i++) {
830 const char *refname = states.stale.items[i].util;
832 if (!dry_run)
833 result |= delete_ref(refname, NULL, 0);
835 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
836 abbrev_ref(refname, "refs/remotes/"));
837 warn_dangling_symref(dangling_msg, refname);
840 free_remote_ref_states(&states);
843 return result;
846 static int get_one_remote_for_update(struct remote *remote, void *priv)
848 struct string_list *list = priv;
849 if (!remote->skip_default_update)
850 string_list_append(remote->name, list);
851 return 0;
854 struct remote_group {
855 const char *name;
856 struct string_list *list;
857 } remote_group;
859 static int get_remote_group(const char *key, const char *value, void *cb)
861 if (!prefixcmp(key, "remotes.") &&
862 !strcmp(key + 8, remote_group.name)) {
863 /* split list by white space */
864 int space = strcspn(value, " \t\n");
865 while (*value) {
866 if (space > 1)
867 string_list_append(xstrndup(value, space),
868 remote_group.list);
869 value += space + (value[space] != '\0');
870 space = strcspn(value, " \t\n");
874 return 0;
877 static int update(int argc, const char **argv)
879 int i, result = 0;
880 struct string_list list = { NULL, 0, 0, 0 };
881 static const char *default_argv[] = { NULL, "default", NULL };
883 if (argc < 2) {
884 argc = 2;
885 argv = default_argv;
888 remote_group.list = &list;
889 for (i = 1; i < argc; i++) {
890 remote_group.name = argv[i];
891 result = git_config(get_remote_group, NULL);
894 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
895 result = for_each_remote(get_one_remote_for_update, &list);
897 for (i = 0; i < list.nr; i++)
898 result |= fetch_remote(list.items[i].string);
900 /* all names were strdup()ed or strndup()ed */
901 list.strdup_strings = 1;
902 string_list_clear(&list, 0);
904 return result;
907 static int get_one_entry(struct remote *remote, void *priv)
909 struct string_list *list = priv;
911 if (remote->url_nr > 0) {
912 int i;
914 for (i = 0; i < remote->url_nr; i++)
915 string_list_append(remote->name, list)->util = (void *)remote->url[i];
916 } else
917 string_list_append(remote->name, list)->util = NULL;
919 return 0;
922 static int show_all(void)
924 struct string_list list = { NULL, 0, 0 };
925 int result = for_each_remote(get_one_entry, &list);
927 if (!result) {
928 int i;
930 sort_string_list(&list);
931 for (i = 0; i < list.nr; i++) {
932 struct string_list_item *item = list.items + i;
933 if (verbose)
934 printf("%s\t%s\n", item->string,
935 item->util ? (const char *)item->util : "");
936 else {
937 if (i && !strcmp((item - 1)->string, item->string))
938 continue;
939 printf("%s\n", item->string);
943 return result;
946 int cmd_remote(int argc, const char **argv, const char *prefix)
948 struct option options[] = {
949 OPT__VERBOSE(&verbose),
950 OPT_END()
952 int result;
954 argc = parse_options(argc, argv, options, builtin_remote_usage,
955 PARSE_OPT_STOP_AT_NON_OPTION);
957 if (argc < 1)
958 result = show_all();
959 else if (!strcmp(argv[0], "add"))
960 result = add(argc, argv);
961 else if (!strcmp(argv[0], "rename"))
962 result = mv(argc, argv);
963 else if (!strcmp(argv[0], "rm"))
964 result = rm(argc, argv);
965 else if (!strcmp(argv[0], "show"))
966 result = show(argc, argv);
967 else if (!strcmp(argv[0], "prune"))
968 result = prune(argc, argv);
969 else if (!strcmp(argv[0], "update"))
970 result = update(argc, argv);
971 else {
972 error("Unknown subcommand: %s", argv[0]);
973 usage_with_options(builtin_remote_usage, options);
976 return result ? 1 : 0;