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