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