b89a3534de8ddf2d8dfa78123833e8696426ffe0
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"
9
10static 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
19};
20
21static int verbose;
22
23static int show_all(void);
24
25static inline int postfixcmp(const char *string, const char *postfix)
26{
27 int len1 = strlen(string), len2 = strlen(postfix);
28 if (len1 < len2)
29 return 1;
30 return strcmp(string + len1 - len2, postfix);
31}
32
33static int opt_parse_track(const struct option *opt, const char *arg, int not)
34{
35 struct string_list *list = opt->value;
36 if (not)
37 string_list_clear(list, 0);
38 else
39 string_list_append(arg, list);
40 return 0;
41}
42
43static int fetch_remote(const char *name)
44{
45 const char *argv[] = { "fetch", name, NULL, NULL };
46 if (verbose) {
47 argv[1] = "-v";
48 argv[2] = name;
49 }
50 printf("Updating %s\n", name);
51 if (run_command_v_opt(argv, RUN_GIT_CMD))
52 return error("Could not fetch %s", name);
53 return 0;
54}
55
56static int add(int argc, const char **argv)
57{
58 int fetch = 0, mirror = 0;
59 struct string_list track = { NULL, 0, 0 };
60 const char *master = NULL;
61 struct remote *remote;
62 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
63 const char *name, *url;
64 int i;
65
66 struct option options[] = {
67 OPT_GROUP("add specific options"),
68 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
69 OPT_CALLBACK('t', "track", &track, "branch",
70 "branch(es) to track", opt_parse_track),
71 OPT_STRING('m', "master", &master, "branch", "master branch"),
72 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
73 OPT_END()
74 };
75
76 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
77
78 if (argc < 2)
79 usage_with_options(builtin_remote_usage, options);
80
81 name = argv[0];
82 url = argv[1];
83
84 remote = remote_get(name);
85 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
86 remote->fetch_refspec_nr))
87 die("remote %s already exists.", name);
88
89 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
90 if (!valid_fetch_refspec(buf2.buf))
91 die("'%s' is not a valid remote name", name);
92
93 strbuf_addf(&buf, "remote.%s.url", name);
94 if (git_config_set(buf.buf, url))
95 return 1;
96
97 strbuf_reset(&buf);
98 strbuf_addf(&buf, "remote.%s.fetch", name);
99
100 if (track.nr == 0)
101 string_list_append("*", &track);
102 for (i = 0; i < track.nr; i++) {
103 struct string_list_item *item = track.items + i;
104
105 strbuf_reset(&buf2);
106 strbuf_addch(&buf2, '+');
107 if (mirror)
108 strbuf_addf(&buf2, "refs/%s:refs/%s",
109 item->string, item->string);
110 else
111 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
112 item->string, name, item->string);
113 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
114 return 1;
115 }
116
117 if (mirror) {
118 strbuf_reset(&buf);
119 strbuf_addf(&buf, "remote.%s.mirror", name);
120 if (git_config_set(buf.buf, "true"))
121 return 1;
122 }
123
124 if (fetch && fetch_remote(name))
125 return 1;
126
127 if (master) {
128 strbuf_reset(&buf);
129 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
130
131 strbuf_reset(&buf2);
132 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
133
134 if (create_symref(buf.buf, buf2.buf, "remote add"))
135 return error("Could not setup master '%s'", master);
136 }
137
138 strbuf_release(&buf);
139 strbuf_release(&buf2);
140 string_list_clear(&track, 0);
141
142 return 0;
143}
144
145struct branch_info {
146 char *remote;
147 struct string_list merge;
148};
149
150static struct string_list branch_list;
151
152static const char *abbrev_ref(const char *name, const char *prefix)
153{
154 const char *abbrev = skip_prefix(name, prefix);
155 if (abbrev)
156 return abbrev;
157 return name;
158}
159#define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
160
161static int config_read_branches(const char *key, const char *value, void *cb)
162{
163 if (!prefixcmp(key, "branch.")) {
164 char *name;
165 struct string_list_item *item;
166 struct branch_info *info;
167 enum { REMOTE, MERGE } type;
168
169 key += 7;
170 if (!postfixcmp(key, ".remote")) {
171 name = xstrndup(key, strlen(key) - 7);
172 type = REMOTE;
173 } else if (!postfixcmp(key, ".merge")) {
174 name = xstrndup(key, strlen(key) - 6);
175 type = MERGE;
176 } else
177 return 0;
178
179 item = string_list_insert(name, &branch_list);
180
181 if (!item->util)
182 item->util = xcalloc(sizeof(struct branch_info), 1);
183 info = item->util;
184 if (type == REMOTE) {
185 if (info->remote)
186 warning("more than one branch.%s", key);
187 info->remote = xstrdup(value);
188 } else {
189 char *space = strchr(value, ' ');
190 value = abbrev_branch(value);
191 while (space) {
192 char *merge;
193 merge = xstrndup(value, space - value);
194 string_list_append(merge, &info->merge);
195 value = abbrev_branch(space + 1);
196 space = strchr(value, ' ');
197 }
198 string_list_append(xstrdup(value), &info->merge);
199 }
200 }
201 return 0;
202}
203
204static void read_branches(void)
205{
206 if (branch_list.nr)
207 return;
208 git_config(config_read_branches, NULL);
209 sort_string_list(&branch_list);
210}
211
212struct ref_states {
213 struct remote *remote;
214 struct string_list new, stale, tracked;
215};
216
217static int handle_one_branch(const char *refname,
218 const unsigned char *sha1, int flags, void *cb_data)
219{
220 struct ref_states *states = cb_data;
221 struct refspec refspec;
222
223 memset(&refspec, 0, sizeof(refspec));
224 refspec.dst = (char *)refname;
225 if (!remote_find_tracking(states->remote, &refspec)) {
226 struct string_list_item *item;
227 const char *name = abbrev_branch(refspec.src);
228 /* symbolic refs pointing nowhere were handled already */
229 if ((flags & REF_ISSYMREF) ||
230 unsorted_string_list_has_string(&states->tracked,
231 name) ||
232 unsorted_string_list_has_string(&states->new,
233 name))
234 return 0;
235 item = string_list_append(name, &states->stale);
236 item->util = xstrdup(refname);
237 }
238 return 0;
239}
240
241static int get_ref_states(const struct ref *ref, struct ref_states *states)
242{
243 struct ref *fetch_map = NULL, **tail = &fetch_map;
244 int i;
245
246 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
247 if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
248 die("Could not get fetch map for refspec %s",
249 states->remote->fetch_refspec[i]);
250
251 states->new.strdup_strings = states->tracked.strdup_strings = 1;
252 for (ref = fetch_map; ref; ref = ref->next) {
253 struct string_list *target = &states->tracked;
254 unsigned char sha1[20];
255 void *util = NULL;
256
257 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
258 target = &states->new;
259 else {
260 target = &states->tracked;
261 if (hashcmp(sha1, ref->new_sha1))
262 util = &states;
263 }
264 string_list_append(abbrev_branch(ref->name), target)->util = util;
265 }
266 free_refs(fetch_map);
267
268 for_each_ref(handle_one_branch, states);
269 sort_string_list(&states->stale);
270
271 return 0;
272}
273
274struct known_remote {
275 struct known_remote *next;
276 struct remote *remote;
277};
278
279struct known_remotes {
280 struct remote *to_delete;
281 struct known_remote *list;
282};
283
284static int add_known_remote(struct remote *remote, void *cb_data)
285{
286 struct known_remotes *all = cb_data;
287 struct known_remote *r;
288
289 if (!strcmp(all->to_delete->name, remote->name))
290 return 0;
291
292 r = xmalloc(sizeof(*r));
293 r->remote = remote;
294 r->next = all->list;
295 all->list = r;
296 return 0;
297}
298
299struct branches_for_remote {
300 struct remote *remote;
301 struct string_list *branches, *skipped;
302 struct known_remotes *keep;
303};
304
305static int add_branch_for_removal(const char *refname,
306 const unsigned char *sha1, int flags, void *cb_data)
307{
308 struct branches_for_remote *branches = cb_data;
309 struct refspec refspec;
310 struct string_list_item *item;
311 struct known_remote *kr;
312
313 memset(&refspec, 0, sizeof(refspec));
314 refspec.dst = (char *)refname;
315 if (remote_find_tracking(branches->remote, &refspec))
316 return 0;
317
318 /* don't delete a branch if another remote also uses it */
319 for (kr = branches->keep->list; kr; kr = kr->next) {
320 memset(&refspec, 0, sizeof(refspec));
321 refspec.dst = (char *)refname;
322 if (!remote_find_tracking(kr->remote, &refspec))
323 return 0;
324 }
325
326 /* don't delete non-remote refs */
327 if (prefixcmp(refname, "refs/remotes")) {
328 /* advise user how to delete local branches */
329 if (!prefixcmp(refname, "refs/heads/"))
330 string_list_append(abbrev_branch(refname),
331 branches->skipped);
332 /* silently skip over other non-remote refs */
333 return 0;
334 }
335
336 /* make sure that symrefs are deleted */
337 if (flags & REF_ISSYMREF)
338 return unlink(git_path("%s", refname));
339
340 item = string_list_append(refname, branches->branches);
341 item->util = xmalloc(20);
342 hashcpy(item->util, sha1);
343
344 return 0;
345}
346
347struct rename_info {
348 const char *old;
349 const char *new;
350 struct string_list *remote_branches;
351};
352
353static int read_remote_branches(const char *refname,
354 const unsigned char *sha1, int flags, void *cb_data)
355{
356 struct rename_info *rename = cb_data;
357 struct strbuf buf = STRBUF_INIT;
358 struct string_list_item *item;
359 int flag;
360 unsigned char orig_sha1[20];
361 const char *symref;
362
363 strbuf_addf(&buf, "refs/remotes/%s", rename->old);
364 if(!prefixcmp(refname, buf.buf)) {
365 item = string_list_append(xstrdup(refname), rename->remote_branches);
366 symref = resolve_ref(refname, orig_sha1, 1, &flag);
367 if (flag & REF_ISSYMREF)
368 item->util = xstrdup(symref);
369 else
370 item->util = NULL;
371 }
372
373 return 0;
374}
375
376static int migrate_file(struct remote *remote)
377{
378 struct strbuf buf = STRBUF_INIT;
379 int i;
380 char *path = NULL;
381
382 strbuf_addf(&buf, "remote.%s.url", remote->name);
383 for (i = 0; i < remote->url_nr; i++)
384 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
385 return error("Could not append '%s' to '%s'",
386 remote->url[i], buf.buf);
387 strbuf_reset(&buf);
388 strbuf_addf(&buf, "remote.%s.push", remote->name);
389 for (i = 0; i < remote->push_refspec_nr; i++)
390 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
391 return error("Could not append '%s' to '%s'",
392 remote->push_refspec[i], buf.buf);
393 strbuf_reset(&buf);
394 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
395 for (i = 0; i < remote->fetch_refspec_nr; i++)
396 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
397 return error("Could not append '%s' to '%s'",
398 remote->fetch_refspec[i], buf.buf);
399 if (remote->origin == REMOTE_REMOTES)
400 path = git_path("remotes/%s", remote->name);
401 else if (remote->origin == REMOTE_BRANCHES)
402 path = git_path("branches/%s", remote->name);
403 if (path && unlink(path))
404 warning("failed to remove '%s'", path);
405 return 0;
406}
407
408static int mv(int argc, const char **argv)
409{
410 struct option options[] = {
411 OPT_END()
412 };
413 struct remote *oldremote, *newremote;
414 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
415 struct string_list remote_branches = { NULL, 0, 0, 0 };
416 struct rename_info rename;
417 int i;
418
419 if (argc != 3)
420 usage_with_options(builtin_remote_usage, options);
421
422 rename.old = argv[1];
423 rename.new = argv[2];
424 rename.remote_branches = &remote_branches;
425
426 oldremote = remote_get(rename.old);
427 if (!oldremote)
428 die("No such remote: %s", rename.old);
429
430 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
431 return migrate_file(oldremote);
432
433 newremote = remote_get(rename.new);
434 if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
435 die("remote %s already exists.", rename.new);
436
437 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
438 if (!valid_fetch_refspec(buf.buf))
439 die("'%s' is not a valid remote name", rename.new);
440
441 strbuf_reset(&buf);
442 strbuf_addf(&buf, "remote.%s", rename.old);
443 strbuf_addf(&buf2, "remote.%s", rename.new);
444 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
445 return error("Could not rename config section '%s' to '%s'",
446 buf.buf, buf2.buf);
447
448 strbuf_reset(&buf);
449 strbuf_addf(&buf, "remote.%s.fetch", rename.new);
450 if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
451 return error("Could not remove config section '%s'", buf.buf);
452 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
453 char *ptr;
454
455 strbuf_reset(&buf2);
456 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
457 ptr = strstr(buf2.buf, rename.old);
458 if (ptr)
459 strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
460 rename.new, strlen(rename.new));
461 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
462 return error("Could not append '%s'", buf.buf);
463 }
464
465 read_branches();
466 for (i = 0; i < branch_list.nr; i++) {
467 struct string_list_item *item = branch_list.items + i;
468 struct branch_info *info = item->util;
469 if (info->remote && !strcmp(info->remote, rename.old)) {
470 strbuf_reset(&buf);
471 strbuf_addf(&buf, "branch.%s.remote", item->string);
472 if (git_config_set(buf.buf, rename.new)) {
473 return error("Could not set '%s'", buf.buf);
474 }
475 }
476 }
477
478 /*
479 * First remove symrefs, then rename the rest, finally create
480 * the new symrefs.
481 */
482 for_each_ref(read_remote_branches, &rename);
483 for (i = 0; i < remote_branches.nr; i++) {
484 struct string_list_item *item = remote_branches.items + i;
485 int flag = 0;
486 unsigned char sha1[20];
487 const char *symref;
488
489 symref = resolve_ref(item->string, sha1, 1, &flag);
490 if (!(flag & REF_ISSYMREF))
491 continue;
492 if (delete_ref(item->string, NULL, REF_NODEREF))
493 die("deleting '%s' failed", item->string);
494 }
495 for (i = 0; i < remote_branches.nr; i++) {
496 struct string_list_item *item = remote_branches.items + i;
497
498 if (item->util)
499 continue;
500 strbuf_reset(&buf);
501 strbuf_addstr(&buf, item->string);
502 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
503 rename.new, strlen(rename.new));
504 strbuf_reset(&buf2);
505 strbuf_addf(&buf2, "remote: renamed %s to %s",
506 item->string, buf.buf);
507 if (rename_ref(item->string, buf.buf, buf2.buf))
508 die("renaming '%s' failed", item->string);
509 }
510 for (i = 0; i < remote_branches.nr; i++) {
511 struct string_list_item *item = remote_branches.items + i;
512
513 if (!item->util)
514 continue;
515 strbuf_reset(&buf);
516 strbuf_addstr(&buf, item->string);
517 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
518 rename.new, strlen(rename.new));
519 strbuf_reset(&buf2);
520 strbuf_addstr(&buf2, item->util);
521 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
522 rename.new, strlen(rename.new));
523 strbuf_reset(&buf3);
524 strbuf_addf(&buf3, "remote: renamed %s to %s",
525 item->string, buf.buf);
526 if (create_symref(buf.buf, buf2.buf, buf3.buf))
527 die("creating '%s' failed", buf.buf);
528 }
529 return 0;
530}
531
532static int remove_branches(struct string_list *branches)
533{
534 int i, result = 0;
535 for (i = 0; i < branches->nr; i++) {
536 struct string_list_item *item = branches->items + i;
537 const char *refname = item->string;
538 unsigned char *sha1 = item->util;
539
540 if (delete_ref(refname, sha1, 0))
541 result |= error("Could not remove branch %s", refname);
542 }
543 return result;
544}
545
546static int rm(int argc, const char **argv)
547{
548 struct option options[] = {
549 OPT_END()
550 };
551 struct remote *remote;
552 struct strbuf buf = STRBUF_INIT;
553 struct known_remotes known_remotes = { NULL, NULL };
554 struct string_list branches = { NULL, 0, 0, 1 };
555 struct string_list skipped = { NULL, 0, 0, 1 };
556 struct branches_for_remote cb_data = {
557 NULL, &branches, &skipped, &known_remotes
558 };
559 int i, result;
560
561 if (argc != 2)
562 usage_with_options(builtin_remote_usage, options);
563
564 remote = remote_get(argv[1]);
565 if (!remote)
566 die("No such remote: %s", argv[1]);
567
568 known_remotes.to_delete = remote;
569 for_each_remote(add_known_remote, &known_remotes);
570
571 strbuf_addf(&buf, "remote.%s", remote->name);
572 if (git_config_rename_section(buf.buf, NULL) < 1)
573 return error("Could not remove config section '%s'", buf.buf);
574
575 read_branches();
576 for (i = 0; i < branch_list.nr; i++) {
577 struct string_list_item *item = branch_list.items + i;
578 struct branch_info *info = item->util;
579 if (info->remote && !strcmp(info->remote, remote->name)) {
580 const char *keys[] = { "remote", "merge", NULL }, **k;
581 for (k = keys; *k; k++) {
582 strbuf_reset(&buf);
583 strbuf_addf(&buf, "branch.%s.%s",
584 item->string, *k);
585 if (git_config_set(buf.buf, NULL)) {
586 strbuf_release(&buf);
587 return -1;
588 }
589 }
590 }
591 }
592
593 /*
594 * We cannot just pass a function to for_each_ref() which deletes
595 * the branches one by one, since for_each_ref() relies on cached
596 * refs, which are invalidated when deleting a branch.
597 */
598 cb_data.remote = remote;
599 result = for_each_ref(add_branch_for_removal, &cb_data);
600 strbuf_release(&buf);
601
602 if (!result)
603 result = remove_branches(&branches);
604 string_list_clear(&branches, 1);
605
606 if (skipped.nr) {
607 fprintf(stderr, skipped.nr == 1 ?
608 "Note: A non-remote branch was not removed; "
609 "to delete it, use:\n" :
610 "Note: Non-remote branches were not removed; "
611 "to delete them, use:\n");
612 for (i = 0; i < skipped.nr; i++)
613 fprintf(stderr, " git branch -d %s\n",
614 skipped.items[i].string);
615 }
616 string_list_clear(&skipped, 0);
617
618 return result;
619}
620
621static void show_list(const char *title, struct string_list *list,
622 const char *extra_arg)
623{
624 int i;
625
626 if (!list->nr)
627 return;
628
629 printf(title, list->nr > 1 ? "es" : "", extra_arg);
630 printf("\n");
631 for (i = 0; i < list->nr; i++)
632 printf(" %s\n", list->items[i].string);
633}
634
635static void free_remote_ref_states(struct ref_states *states)
636{
637 string_list_clear(&states->new, 0);
638 string_list_clear(&states->stale, 0);
639 string_list_clear(&states->tracked, 0);
640}
641
642static int get_remote_ref_states(const char *name,
643 struct ref_states *states,
644 int query)
645{
646 struct transport *transport;
647 const struct ref *ref;
648
649 states->remote = remote_get(name);
650 if (!states->remote)
651 return error("No such remote: %s", name);
652
653 read_branches();
654
655 if (query) {
656 transport = transport_get(NULL, states->remote->url_nr > 0 ?
657 states->remote->url[0] : NULL);
658 ref = transport_get_remote_refs(transport);
659 transport_disconnect(transport);
660
661 get_ref_states(ref, states);
662 }
663
664 return 0;
665}
666
667static int append_ref_to_tracked_list(const char *refname,
668 const unsigned char *sha1, int flags, void *cb_data)
669{
670 struct ref_states *states = cb_data;
671 struct refspec refspec;
672
673 memset(&refspec, 0, sizeof(refspec));
674 refspec.dst = (char *)refname;
675 if (!remote_find_tracking(states->remote, &refspec))
676 string_list_append(abbrev_branch(refspec.src), &states->tracked);
677
678 return 0;
679}
680
681static int show(int argc, const char **argv)
682{
683 int no_query = 0, result = 0;
684 struct option options[] = {
685 OPT_GROUP("show specific options"),
686 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
687 OPT_END()
688 };
689 struct ref_states states;
690
691 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
692
693 if (argc < 1)
694 return show_all();
695
696 memset(&states, 0, sizeof(states));
697 for (; argc; argc--, argv++) {
698 int i;
699
700 get_remote_ref_states(*argv, &states, !no_query);
701
702 printf("* remote %s\n URL: %s\n", *argv,
703 states.remote->url_nr > 0 ?
704 states.remote->url[0] : "(no URL)");
705
706 for (i = 0; i < branch_list.nr; i++) {
707 struct string_list_item *branch = branch_list.items + i;
708 struct branch_info *info = branch->util;
709 int j;
710
711 if (!info->merge.nr || strcmp(*argv, info->remote))
712 continue;
713 printf(" Remote branch%s merged with 'git pull' "
714 "while on branch %s\n ",
715 info->merge.nr > 1 ? "es" : "",
716 branch->string);
717 for (j = 0; j < info->merge.nr; j++)
718 printf(" %s", info->merge.items[j].string);
719 printf("\n");
720 }
721
722 if (!no_query) {
723 show_list(" New remote branch%s (next fetch "
724 "will store in remotes/%s)",
725 &states.new, states.remote->name);
726 show_list(" Stale tracking branch%s (use 'git remote "
727 "prune')", &states.stale, "");
728 }
729
730 if (no_query)
731 for_each_ref(append_ref_to_tracked_list, &states);
732 show_list(" Tracked remote branch%s", &states.tracked, "");
733
734 if (states.remote->push_refspec_nr) {
735 printf(" Local branch%s pushed with 'git push'\n",
736 states.remote->push_refspec_nr > 1 ?
737 "es" : "");
738 for (i = 0; i < states.remote->push_refspec_nr; i++) {
739 struct refspec *spec = states.remote->push + i;
740 printf(" %s%s%s%s\n",
741 spec->force ? "+" : "",
742 abbrev_branch(spec->src),
743 spec->dst ? ":" : "",
744 spec->dst ? abbrev_branch(spec->dst) : "");
745 }
746 }
747
748 free_remote_ref_states(&states);
749 }
750
751 return result;
752}
753
754static int prune(int argc, const char **argv)
755{
756 int dry_run = 0, result = 0;
757 struct option options[] = {
758 OPT_GROUP("prune specific options"),
759 OPT__DRY_RUN(&dry_run),
760 OPT_END()
761 };
762 struct ref_states states;
763 const char *dangling_msg;
764
765 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
766
767 if (argc < 1)
768 usage_with_options(builtin_remote_usage, options);
769
770 dangling_msg = (dry_run
771 ? " %s will become dangling!\n"
772 : " %s has become dangling!\n");
773
774 memset(&states, 0, sizeof(states));
775 for (; argc; argc--, argv++) {
776 int i;
777
778 get_remote_ref_states(*argv, &states, 1);
779
780 if (states.stale.nr) {
781 printf("Pruning %s\n", *argv);
782 printf("URL: %s\n",
783 states.remote->url_nr
784 ? states.remote->url[0]
785 : "(no URL)");
786 }
787
788 for (i = 0; i < states.stale.nr; i++) {
789 const char *refname = states.stale.items[i].util;
790
791 if (!dry_run)
792 result |= delete_ref(refname, NULL, 0);
793
794 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
795 abbrev_ref(refname, "refs/remotes/"));
796 warn_dangling_symref(dangling_msg, refname);
797 }
798
799 free_remote_ref_states(&states);
800 }
801
802 return result;
803}
804
805static int get_one_remote_for_update(struct remote *remote, void *priv)
806{
807 struct string_list *list = priv;
808 if (!remote->skip_default_update)
809 string_list_append(remote->name, list);
810 return 0;
811}
812
813struct remote_group {
814 const char *name;
815 struct string_list *list;
816} remote_group;
817
818static int get_remote_group(const char *key, const char *value, void *cb)
819{
820 if (!prefixcmp(key, "remotes.") &&
821 !strcmp(key + 8, remote_group.name)) {
822 /* split list by white space */
823 int space = strcspn(value, " \t\n");
824 while (*value) {
825 if (space > 1)
826 string_list_append(xstrndup(value, space),
827 remote_group.list);
828 value += space + (value[space] != '\0');
829 space = strcspn(value, " \t\n");
830 }
831 }
832
833 return 0;
834}
835
836static int update(int argc, const char **argv)
837{
838 int i, result = 0;
839 struct string_list list = { NULL, 0, 0, 0 };
840 static const char *default_argv[] = { NULL, "default", NULL };
841
842 if (argc < 2) {
843 argc = 2;
844 argv = default_argv;
845 }
846
847 remote_group.list = &list;
848 for (i = 1; i < argc; i++) {
849 remote_group.name = argv[i];
850 result = git_config(get_remote_group, NULL);
851 }
852
853 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
854 result = for_each_remote(get_one_remote_for_update, &list);
855
856 for (i = 0; i < list.nr; i++)
857 result |= fetch_remote(list.items[i].string);
858
859 /* all names were strdup()ed or strndup()ed */
860 list.strdup_strings = 1;
861 string_list_clear(&list, 0);
862
863 return result;
864}
865
866static int get_one_entry(struct remote *remote, void *priv)
867{
868 struct string_list *list = priv;
869
870 if (remote->url_nr > 0) {
871 int i;
872
873 for (i = 0; i < remote->url_nr; i++)
874 string_list_append(remote->name, list)->util = (void *)remote->url[i];
875 } else
876 string_list_append(remote->name, list)->util = NULL;
877
878 return 0;
879}
880
881static int show_all(void)
882{
883 struct string_list list = { NULL, 0, 0 };
884 int result = for_each_remote(get_one_entry, &list);
885
886 if (!result) {
887 int i;
888
889 sort_string_list(&list);
890 for (i = 0; i < list.nr; i++) {
891 struct string_list_item *item = list.items + i;
892 if (verbose)
893 printf("%s\t%s\n", item->string,
894 item->util ? (const char *)item->util : "");
895 else {
896 if (i && !strcmp((item - 1)->string, item->string))
897 continue;
898 printf("%s\n", item->string);
899 }
900 }
901 }
902 return result;
903}
904
905int cmd_remote(int argc, const char **argv, const char *prefix)
906{
907 struct option options[] = {
908 OPT__VERBOSE(&verbose),
909 OPT_END()
910 };
911 int result;
912
913 argc = parse_options(argc, argv, options, builtin_remote_usage,
914 PARSE_OPT_STOP_AT_NON_OPTION);
915
916 if (argc < 1)
917 result = show_all();
918 else if (!strcmp(argv[0], "add"))
919 result = add(argc, argv);
920 else if (!strcmp(argv[0], "rename"))
921 result = mv(argc, argv);
922 else if (!strcmp(argv[0], "rm"))
923 result = rm(argc, argv);
924 else if (!strcmp(argv[0], "show"))
925 result = show(argc, argv);
926 else if (!strcmp(argv[0], "prune"))
927 result = prune(argc, argv);
928 else if (!strcmp(argv[0], "update"))
929 result = update(argc, argv);
930 else {
931 error("Unknown subcommand: %s", argv[0]);
932 usage_with_options(builtin_remote_usage, options);
933 }
934
935 return result ? 1 : 0;
936}