builtin/remote: remove postfixcmp() and use suffixcmp() instead
[git/mingw.git] / builtin / remote.c
blob9b3a98e415fc2170d2d528171f1beb2196850de0
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"
10 static const char * const builtin_remote_usage[] = {
11 N_("git remote [-v | --verbose]"),
12 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags|--no-tags] [--mirror=<fetch|push>] <name> <url>"),
13 N_("git remote rename <old> <new>"),
14 N_("git remote remove <name>"),
15 N_("git remote set-head <name> (-a | --auto | -d | --delete |<branch>)"),
16 N_("git remote [-v | --verbose] show [-n] <name>"),
17 N_("git remote prune [-n | --dry-run] <name>"),
18 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
19 N_("git remote set-branches [--add] <name> <branch>..."),
20 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
21 N_("git remote set-url --add <name> <newurl>"),
22 N_("git remote set-url --delete <name> <url>"),
23 NULL
26 static const char * const builtin_remote_add_usage[] = {
27 N_("git remote add [<options>] <name> <url>"),
28 NULL
31 static const char * const builtin_remote_rename_usage[] = {
32 N_("git remote rename <old> <new>"),
33 NULL
36 static const char * const builtin_remote_rm_usage[] = {
37 N_("git remote remove <name>"),
38 NULL
41 static const char * const builtin_remote_sethead_usage[] = {
42 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
43 NULL
46 static const char * const builtin_remote_setbranches_usage[] = {
47 N_("git remote set-branches <name> <branch>..."),
48 N_("git remote set-branches --add <name> <branch>..."),
49 NULL
52 static const char * const builtin_remote_show_usage[] = {
53 N_("git remote show [<options>] <name>"),
54 NULL
57 static const char * const builtin_remote_prune_usage[] = {
58 N_("git remote prune [<options>] <name>"),
59 NULL
62 static const char * const builtin_remote_update_usage[] = {
63 N_("git remote update [<options>] [<group> | <remote>]..."),
64 NULL
67 static const char * const builtin_remote_seturl_usage[] = {
68 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
69 N_("git remote set-url --add <name> <newurl>"),
70 N_("git remote set-url --delete <name> <url>"),
71 NULL
74 #define GET_REF_STATES (1<<0)
75 #define GET_HEAD_NAMES (1<<1)
76 #define GET_PUSH_REF_STATES (1<<2)
78 static int verbose;
80 static int show_all(void);
81 static int prune_remote(const char *remote, int dry_run);
83 static int fetch_remote(const char *name)
85 const char *argv[] = { "fetch", name, NULL, NULL };
86 if (verbose) {
87 argv[1] = "-v";
88 argv[2] = name;
90 printf_ln(_("Updating %s"), name);
91 if (run_command_v_opt(argv, RUN_GIT_CMD))
92 return error(_("Could not fetch %s"), name);
93 return 0;
96 enum {
97 TAGS_UNSET = 0,
98 TAGS_DEFAULT = 1,
99 TAGS_SET = 2
102 #define MIRROR_NONE 0
103 #define MIRROR_FETCH 1
104 #define MIRROR_PUSH 2
105 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
107 static int add_branch(const char *key, const char *branchname,
108 const char *remotename, int mirror, struct strbuf *tmp)
110 strbuf_reset(tmp);
111 strbuf_addch(tmp, '+');
112 if (mirror)
113 strbuf_addf(tmp, "refs/%s:refs/%s",
114 branchname, branchname);
115 else
116 strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
117 branchname, remotename, branchname);
118 return git_config_set_multivar(key, tmp->buf, "^$", 0);
121 static const char mirror_advice[] =
122 N_("--mirror is dangerous and deprecated; please\n"
123 "\t use --mirror=fetch or --mirror=push instead");
125 static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
127 unsigned *mirror = opt->value;
128 if (not)
129 *mirror = MIRROR_NONE;
130 else if (!arg) {
131 warning("%s", _(mirror_advice));
132 *mirror = MIRROR_BOTH;
134 else if (!strcmp(arg, "fetch"))
135 *mirror = MIRROR_FETCH;
136 else if (!strcmp(arg, "push"))
137 *mirror = MIRROR_PUSH;
138 else
139 return error(_("unknown mirror argument: %s"), arg);
140 return 0;
143 static int add(int argc, const char **argv)
145 int fetch = 0, fetch_tags = TAGS_DEFAULT;
146 unsigned mirror = MIRROR_NONE;
147 struct string_list track = STRING_LIST_INIT_NODUP;
148 const char *master = NULL;
149 struct remote *remote;
150 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
151 const char *name, *url;
152 int i;
154 struct option options[] = {
155 OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
156 OPT_SET_INT(0, "tags", &fetch_tags,
157 N_("import all tags and associated objects when fetching"),
158 TAGS_SET),
159 OPT_SET_INT(0, NULL, &fetch_tags,
160 N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET),
161 OPT_STRING_LIST('t', "track", &track, N_("branch"),
162 N_("branch(es) to track")),
163 OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")),
164 { OPTION_CALLBACK, 0, "mirror", &mirror, N_("push|fetch"),
165 N_("set up remote as a mirror to push to or fetch from"),
166 PARSE_OPT_OPTARG, parse_mirror_opt },
167 OPT_END()
170 argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage,
173 if (argc != 2)
174 usage_with_options(builtin_remote_add_usage, options);
176 if (mirror && master)
177 die(_("specifying a master branch makes no sense with --mirror"));
178 if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
179 die(_("specifying branches to track makes sense only with fetch mirrors"));
181 name = argv[0];
182 url = argv[1];
184 remote = remote_get(name);
185 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
186 remote->fetch_refspec_nr))
187 die(_("remote %s already exists."), name);
189 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
190 if (!valid_fetch_refspec(buf2.buf))
191 die(_("'%s' is not a valid remote name"), name);
193 strbuf_addf(&buf, "remote.%s.url", name);
194 if (git_config_set(buf.buf, url))
195 return 1;
197 if (!mirror || mirror & MIRROR_FETCH) {
198 strbuf_reset(&buf);
199 strbuf_addf(&buf, "remote.%s.fetch", name);
200 if (track.nr == 0)
201 string_list_append(&track, "*");
202 for (i = 0; i < track.nr; i++) {
203 if (add_branch(buf.buf, track.items[i].string,
204 name, mirror, &buf2))
205 return 1;
209 if (mirror & MIRROR_PUSH) {
210 strbuf_reset(&buf);
211 strbuf_addf(&buf, "remote.%s.mirror", name);
212 if (git_config_set(buf.buf, "true"))
213 return 1;
216 if (fetch_tags != TAGS_DEFAULT) {
217 strbuf_reset(&buf);
218 strbuf_addf(&buf, "remote.%s.tagopt", name);
219 if (git_config_set(buf.buf,
220 fetch_tags == TAGS_SET ? "--tags" : "--no-tags"))
221 return 1;
224 if (fetch && fetch_remote(name))
225 return 1;
227 if (master) {
228 strbuf_reset(&buf);
229 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
231 strbuf_reset(&buf2);
232 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
234 if (create_symref(buf.buf, buf2.buf, "remote add"))
235 return error(_("Could not setup master '%s'"), master);
238 strbuf_release(&buf);
239 strbuf_release(&buf2);
240 string_list_clear(&track, 0);
242 return 0;
245 struct branch_info {
246 char *remote_name;
247 struct string_list merge;
248 int rebase;
251 static struct string_list branch_list;
253 static const char *abbrev_ref(const char *name, const char *prefix)
255 const char *abbrev = skip_prefix(name, prefix);
256 if (abbrev)
257 return abbrev;
258 return name;
260 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
262 static int config_read_branches(const char *key, const char *value, void *cb)
264 if (!prefixcmp(key, "branch.")) {
265 const char *orig_key = key;
266 char *name;
267 struct string_list_item *item;
268 struct branch_info *info;
269 enum { REMOTE, MERGE, REBASE } type;
271 key += 7;
272 if (!suffixcmp(key, ".remote")) {
273 name = xstrndup(key, strlen(key) - 7);
274 type = REMOTE;
275 } else if (!suffixcmp(key, ".merge")) {
276 name = xstrndup(key, strlen(key) - 6);
277 type = MERGE;
278 } else if (!suffixcmp(key, ".rebase")) {
279 name = xstrndup(key, strlen(key) - 7);
280 type = REBASE;
281 } else
282 return 0;
284 item = string_list_insert(&branch_list, name);
286 if (!item->util)
287 item->util = xcalloc(sizeof(struct branch_info), 1);
288 info = item->util;
289 if (type == REMOTE) {
290 if (info->remote_name)
291 warning(_("more than one %s"), orig_key);
292 info->remote_name = xstrdup(value);
293 } else if (type == MERGE) {
294 char *space = strchr(value, ' ');
295 value = abbrev_branch(value);
296 while (space) {
297 char *merge;
298 merge = xstrndup(value, space - value);
299 string_list_append(&info->merge, merge);
300 value = abbrev_branch(space + 1);
301 space = strchr(value, ' ');
303 string_list_append(&info->merge, xstrdup(value));
304 } else
305 info->rebase = git_config_bool(orig_key, value);
307 return 0;
310 static void read_branches(void)
312 if (branch_list.nr)
313 return;
314 git_config(config_read_branches, NULL);
317 struct ref_states {
318 struct remote *remote;
319 struct string_list new, stale, tracked, heads, push;
320 int queried;
323 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
325 struct ref *fetch_map = NULL, **tail = &fetch_map;
326 struct ref *ref, *stale_refs;
327 int i;
329 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
330 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
331 die(_("Could not get fetch map for refspec %s"),
332 states->remote->fetch_refspec[i]);
334 states->new.strdup_strings = 1;
335 states->tracked.strdup_strings = 1;
336 states->stale.strdup_strings = 1;
337 for (ref = fetch_map; ref; ref = ref->next) {
338 if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
339 string_list_append(&states->new, abbrev_branch(ref->name));
340 else
341 string_list_append(&states->tracked, abbrev_branch(ref->name));
343 stale_refs = get_stale_heads(states->remote->fetch,
344 states->remote->fetch_refspec_nr, fetch_map);
345 for (ref = stale_refs; ref; ref = ref->next) {
346 struct string_list_item *item =
347 string_list_append(&states->stale, abbrev_branch(ref->name));
348 item->util = xstrdup(ref->name);
350 free_refs(stale_refs);
351 free_refs(fetch_map);
353 sort_string_list(&states->new);
354 sort_string_list(&states->tracked);
355 sort_string_list(&states->stale);
357 return 0;
360 struct push_info {
361 char *dest;
362 int forced;
363 enum {
364 PUSH_STATUS_CREATE = 0,
365 PUSH_STATUS_DELETE,
366 PUSH_STATUS_UPTODATE,
367 PUSH_STATUS_FASTFORWARD,
368 PUSH_STATUS_OUTOFDATE,
369 PUSH_STATUS_NOTQUERIED
370 } status;
373 static int get_push_ref_states(const struct ref *remote_refs,
374 struct ref_states *states)
376 struct remote *remote = states->remote;
377 struct ref *ref, *local_refs, *push_map;
378 if (remote->mirror)
379 return 0;
381 local_refs = get_local_heads();
382 push_map = copy_ref_list(remote_refs);
384 match_push_refs(local_refs, &push_map, remote->push_refspec_nr,
385 remote->push_refspec, MATCH_REFS_NONE);
387 states->push.strdup_strings = 1;
388 for (ref = push_map; ref; ref = ref->next) {
389 struct string_list_item *item;
390 struct push_info *info;
392 if (!ref->peer_ref)
393 continue;
394 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
396 item = string_list_append(&states->push,
397 abbrev_branch(ref->peer_ref->name));
398 item->util = xcalloc(sizeof(struct push_info), 1);
399 info = item->util;
400 info->forced = ref->force;
401 info->dest = xstrdup(abbrev_branch(ref->name));
403 if (is_null_sha1(ref->new_sha1)) {
404 info->status = PUSH_STATUS_DELETE;
405 } else if (!hashcmp(ref->old_sha1, ref->new_sha1))
406 info->status = PUSH_STATUS_UPTODATE;
407 else if (is_null_sha1(ref->old_sha1))
408 info->status = PUSH_STATUS_CREATE;
409 else if (has_sha1_file(ref->old_sha1) &&
410 ref_newer(ref->new_sha1, ref->old_sha1))
411 info->status = PUSH_STATUS_FASTFORWARD;
412 else
413 info->status = PUSH_STATUS_OUTOFDATE;
415 free_refs(local_refs);
416 free_refs(push_map);
417 return 0;
420 static int get_push_ref_states_noquery(struct ref_states *states)
422 int i;
423 struct remote *remote = states->remote;
424 struct string_list_item *item;
425 struct push_info *info;
427 if (remote->mirror)
428 return 0;
430 states->push.strdup_strings = 1;
431 if (!remote->push_refspec_nr) {
432 item = string_list_append(&states->push, _("(matching)"));
433 info = item->util = xcalloc(sizeof(struct push_info), 1);
434 info->status = PUSH_STATUS_NOTQUERIED;
435 info->dest = xstrdup(item->string);
437 for (i = 0; i < remote->push_refspec_nr; i++) {
438 struct refspec *spec = remote->push + i;
439 if (spec->matching)
440 item = string_list_append(&states->push, _("(matching)"));
441 else if (strlen(spec->src))
442 item = string_list_append(&states->push, spec->src);
443 else
444 item = string_list_append(&states->push, _("(delete)"));
446 info = item->util = xcalloc(sizeof(struct push_info), 1);
447 info->forced = spec->force;
448 info->status = PUSH_STATUS_NOTQUERIED;
449 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
451 return 0;
454 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
456 struct ref *ref, *matches;
457 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
458 struct refspec refspec;
460 refspec.force = 0;
461 refspec.pattern = 1;
462 refspec.src = refspec.dst = "refs/heads/*";
463 states->heads.strdup_strings = 1;
464 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
465 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
466 fetch_map, 1);
467 for (ref = matches; ref; ref = ref->next)
468 string_list_append(&states->heads, abbrev_branch(ref->name));
470 free_refs(fetch_map);
471 free_refs(matches);
473 return 0;
476 struct known_remote {
477 struct known_remote *next;
478 struct remote *remote;
481 struct known_remotes {
482 struct remote *to_delete;
483 struct known_remote *list;
486 static int add_known_remote(struct remote *remote, void *cb_data)
488 struct known_remotes *all = cb_data;
489 struct known_remote *r;
491 if (!strcmp(all->to_delete->name, remote->name))
492 return 0;
494 r = xmalloc(sizeof(*r));
495 r->remote = remote;
496 r->next = all->list;
497 all->list = r;
498 return 0;
501 struct branches_for_remote {
502 struct remote *remote;
503 struct string_list *branches, *skipped;
504 struct known_remotes *keep;
507 static int add_branch_for_removal(const char *refname,
508 const unsigned char *sha1, int flags, void *cb_data)
510 struct branches_for_remote *branches = cb_data;
511 struct refspec refspec;
512 struct string_list_item *item;
513 struct known_remote *kr;
515 memset(&refspec, 0, sizeof(refspec));
516 refspec.dst = (char *)refname;
517 if (remote_find_tracking(branches->remote, &refspec))
518 return 0;
520 /* don't delete a branch if another remote also uses it */
521 for (kr = branches->keep->list; kr; kr = kr->next) {
522 memset(&refspec, 0, sizeof(refspec));
523 refspec.dst = (char *)refname;
524 if (!remote_find_tracking(kr->remote, &refspec))
525 return 0;
528 /* don't delete non-remote-tracking refs */
529 if (prefixcmp(refname, "refs/remotes/")) {
530 /* advise user how to delete local branches */
531 if (!prefixcmp(refname, "refs/heads/"))
532 string_list_append(branches->skipped,
533 abbrev_branch(refname));
534 /* silently skip over other non-remote refs */
535 return 0;
538 /* make sure that symrefs are deleted */
539 if (flags & REF_ISSYMREF)
540 return unlink(git_path("%s", refname));
542 item = string_list_append(branches->branches, refname);
543 item->util = xmalloc(20);
544 hashcpy(item->util, sha1);
546 return 0;
549 struct rename_info {
550 const char *old;
551 const char *new;
552 struct string_list *remote_branches;
555 static int read_remote_branches(const char *refname,
556 const unsigned char *sha1, int flags, void *cb_data)
558 struct rename_info *rename = cb_data;
559 struct strbuf buf = STRBUF_INIT;
560 struct string_list_item *item;
561 int flag;
562 unsigned char orig_sha1[20];
563 const char *symref;
565 strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
566 if (!prefixcmp(refname, buf.buf)) {
567 item = string_list_append(rename->remote_branches, xstrdup(refname));
568 symref = resolve_ref_unsafe(refname, orig_sha1, 1, &flag);
569 if (flag & REF_ISSYMREF)
570 item->util = xstrdup(symref);
571 else
572 item->util = NULL;
575 return 0;
578 static int migrate_file(struct remote *remote)
580 struct strbuf buf = STRBUF_INIT;
581 int i;
582 char *path = NULL;
584 strbuf_addf(&buf, "remote.%s.url", remote->name);
585 for (i = 0; i < remote->url_nr; i++)
586 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
587 return error(_("Could not append '%s' to '%s'"),
588 remote->url[i], buf.buf);
589 strbuf_reset(&buf);
590 strbuf_addf(&buf, "remote.%s.push", remote->name);
591 for (i = 0; i < remote->push_refspec_nr; i++)
592 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
593 return error(_("Could not append '%s' to '%s'"),
594 remote->push_refspec[i], buf.buf);
595 strbuf_reset(&buf);
596 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
597 for (i = 0; i < remote->fetch_refspec_nr; i++)
598 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
599 return error(_("Could not append '%s' to '%s'"),
600 remote->fetch_refspec[i], buf.buf);
601 if (remote->origin == REMOTE_REMOTES)
602 path = git_path("remotes/%s", remote->name);
603 else if (remote->origin == REMOTE_BRANCHES)
604 path = git_path("branches/%s", remote->name);
605 if (path)
606 unlink_or_warn(path);
607 return 0;
610 static int mv(int argc, const char **argv)
612 struct option options[] = {
613 OPT_END()
615 struct remote *oldremote, *newremote;
616 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
617 old_remote_context = STRBUF_INIT;
618 struct string_list remote_branches = STRING_LIST_INIT_NODUP;
619 struct rename_info rename;
620 int i, refspec_updated = 0;
622 if (argc != 3)
623 usage_with_options(builtin_remote_rename_usage, options);
625 rename.old = argv[1];
626 rename.new = argv[2];
627 rename.remote_branches = &remote_branches;
629 oldremote = remote_get(rename.old);
630 if (!oldremote)
631 die(_("No such remote: %s"), rename.old);
633 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
634 return migrate_file(oldremote);
636 newremote = remote_get(rename.new);
637 if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
638 die(_("remote %s already exists."), rename.new);
640 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
641 if (!valid_fetch_refspec(buf.buf))
642 die(_("'%s' is not a valid remote name"), rename.new);
644 strbuf_reset(&buf);
645 strbuf_addf(&buf, "remote.%s", rename.old);
646 strbuf_addf(&buf2, "remote.%s", rename.new);
647 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
648 return error(_("Could not rename config section '%s' to '%s'"),
649 buf.buf, buf2.buf);
651 strbuf_reset(&buf);
652 strbuf_addf(&buf, "remote.%s.fetch", rename.new);
653 if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
654 return error(_("Could not remove config section '%s'"), buf.buf);
655 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old);
656 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
657 char *ptr;
659 strbuf_reset(&buf2);
660 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
661 ptr = strstr(buf2.buf, old_remote_context.buf);
662 if (ptr) {
663 refspec_updated = 1;
664 strbuf_splice(&buf2,
665 ptr-buf2.buf + strlen(":refs/remotes/"),
666 strlen(rename.old), rename.new,
667 strlen(rename.new));
668 } else
669 warning(_("Not updating non-default fetch refspec\n"
670 "\t%s\n"
671 "\tPlease update the configuration manually if necessary."),
672 buf2.buf);
674 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
675 return error(_("Could not append '%s'"), buf.buf);
678 read_branches();
679 for (i = 0; i < branch_list.nr; i++) {
680 struct string_list_item *item = branch_list.items + i;
681 struct branch_info *info = item->util;
682 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
683 strbuf_reset(&buf);
684 strbuf_addf(&buf, "branch.%s.remote", item->string);
685 if (git_config_set(buf.buf, rename.new)) {
686 return error(_("Could not set '%s'"), buf.buf);
691 if (!refspec_updated)
692 return 0;
695 * First remove symrefs, then rename the rest, finally create
696 * the new symrefs.
698 for_each_ref(read_remote_branches, &rename);
699 for (i = 0; i < remote_branches.nr; i++) {
700 struct string_list_item *item = remote_branches.items + i;
701 int flag = 0;
702 unsigned char sha1[20];
704 read_ref_full(item->string, sha1, 1, &flag);
705 if (!(flag & REF_ISSYMREF))
706 continue;
707 if (delete_ref(item->string, NULL, REF_NODEREF))
708 die(_("deleting '%s' failed"), item->string);
710 for (i = 0; i < remote_branches.nr; i++) {
711 struct string_list_item *item = remote_branches.items + i;
713 if (item->util)
714 continue;
715 strbuf_reset(&buf);
716 strbuf_addstr(&buf, item->string);
717 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
718 rename.new, strlen(rename.new));
719 strbuf_reset(&buf2);
720 strbuf_addf(&buf2, "remote: renamed %s to %s",
721 item->string, buf.buf);
722 if (rename_ref(item->string, buf.buf, buf2.buf))
723 die(_("renaming '%s' failed"), item->string);
725 for (i = 0; i < remote_branches.nr; i++) {
726 struct string_list_item *item = remote_branches.items + i;
728 if (!item->util)
729 continue;
730 strbuf_reset(&buf);
731 strbuf_addstr(&buf, item->string);
732 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
733 rename.new, strlen(rename.new));
734 strbuf_reset(&buf2);
735 strbuf_addstr(&buf2, item->util);
736 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
737 rename.new, strlen(rename.new));
738 strbuf_reset(&buf3);
739 strbuf_addf(&buf3, "remote: renamed %s to %s",
740 item->string, buf.buf);
741 if (create_symref(buf.buf, buf2.buf, buf3.buf))
742 die(_("creating '%s' failed"), buf.buf);
744 return 0;
747 static int remove_branches(struct string_list *branches)
749 int i, result = 0;
750 for (i = 0; i < branches->nr; i++) {
751 struct string_list_item *item = branches->items + i;
752 const char *refname = item->string;
753 unsigned char *sha1 = item->util;
755 if (delete_ref(refname, sha1, 0))
756 result |= error(_("Could not remove branch %s"), refname);
758 return result;
761 static int rm(int argc, const char **argv)
763 struct option options[] = {
764 OPT_END()
766 struct remote *remote;
767 struct strbuf buf = STRBUF_INIT;
768 struct known_remotes known_remotes = { NULL, NULL };
769 struct string_list branches = STRING_LIST_INIT_DUP;
770 struct string_list skipped = STRING_LIST_INIT_DUP;
771 struct branches_for_remote cb_data;
772 int i, result;
774 memset(&cb_data, 0, sizeof(cb_data));
775 cb_data.branches = &branches;
776 cb_data.skipped = &skipped;
777 cb_data.keep = &known_remotes;
779 if (argc != 2)
780 usage_with_options(builtin_remote_rm_usage, options);
782 remote = remote_get(argv[1]);
783 if (!remote)
784 die(_("No such remote: %s"), argv[1]);
786 known_remotes.to_delete = remote;
787 for_each_remote(add_known_remote, &known_remotes);
789 strbuf_addf(&buf, "remote.%s", remote->name);
790 if (git_config_rename_section(buf.buf, NULL) < 1)
791 return error(_("Could not remove config section '%s'"), buf.buf);
793 read_branches();
794 for (i = 0; i < branch_list.nr; i++) {
795 struct string_list_item *item = branch_list.items + i;
796 struct branch_info *info = item->util;
797 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
798 const char *keys[] = { "remote", "merge", NULL }, **k;
799 for (k = keys; *k; k++) {
800 strbuf_reset(&buf);
801 strbuf_addf(&buf, "branch.%s.%s",
802 item->string, *k);
803 if (git_config_set(buf.buf, NULL)) {
804 strbuf_release(&buf);
805 return -1;
812 * We cannot just pass a function to for_each_ref() which deletes
813 * the branches one by one, since for_each_ref() relies on cached
814 * refs, which are invalidated when deleting a branch.
816 cb_data.remote = remote;
817 result = for_each_ref(add_branch_for_removal, &cb_data);
818 strbuf_release(&buf);
820 if (!result)
821 result = remove_branches(&branches);
822 string_list_clear(&branches, 1);
824 if (skipped.nr) {
825 fprintf_ln(stderr,
826 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
827 "to delete it, use:",
828 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
829 "to delete them, use:",
830 skipped.nr));
831 for (i = 0; i < skipped.nr; i++)
832 fprintf(stderr, " git branch -d %s\n",
833 skipped.items[i].string);
835 string_list_clear(&skipped, 0);
837 return result;
840 static void clear_push_info(void *util, const char *string)
842 struct push_info *info = util;
843 free(info->dest);
844 free(info);
847 static void free_remote_ref_states(struct ref_states *states)
849 string_list_clear(&states->new, 0);
850 string_list_clear(&states->stale, 1);
851 string_list_clear(&states->tracked, 0);
852 string_list_clear(&states->heads, 0);
853 string_list_clear_func(&states->push, clear_push_info);
856 static int append_ref_to_tracked_list(const char *refname,
857 const unsigned char *sha1, int flags, void *cb_data)
859 struct ref_states *states = cb_data;
860 struct refspec refspec;
862 if (flags & REF_ISSYMREF)
863 return 0;
865 memset(&refspec, 0, sizeof(refspec));
866 refspec.dst = (char *)refname;
867 if (!remote_find_tracking(states->remote, &refspec))
868 string_list_append(&states->tracked, abbrev_branch(refspec.src));
870 return 0;
873 static int get_remote_ref_states(const char *name,
874 struct ref_states *states,
875 int query)
877 struct transport *transport;
878 const struct ref *remote_refs;
880 states->remote = remote_get(name);
881 if (!states->remote)
882 return error(_("No such remote: %s"), name);
884 read_branches();
886 if (query) {
887 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
888 states->remote->url[0] : NULL);
889 remote_refs = transport_get_remote_refs(transport);
890 transport_disconnect(transport);
892 states->queried = 1;
893 if (query & GET_REF_STATES)
894 get_ref_states(remote_refs, states);
895 if (query & GET_HEAD_NAMES)
896 get_head_names(remote_refs, states);
897 if (query & GET_PUSH_REF_STATES)
898 get_push_ref_states(remote_refs, states);
899 } else {
900 for_each_ref(append_ref_to_tracked_list, states);
901 sort_string_list(&states->tracked);
902 get_push_ref_states_noquery(states);
905 return 0;
908 struct show_info {
909 struct string_list *list;
910 struct ref_states *states;
911 int width, width2;
912 int any_rebase;
915 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
917 struct show_info *info = cb_data;
918 int n = strlen(item->string);
919 if (n > info->width)
920 info->width = n;
921 string_list_insert(info->list, item->string);
922 return 0;
925 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
927 struct show_info *info = cb_data;
928 struct ref_states *states = info->states;
929 const char *name = item->string;
931 if (states->queried) {
932 const char *fmt = "%s";
933 const char *arg = "";
934 if (string_list_has_string(&states->new, name)) {
935 fmt = _(" new (next fetch will store in remotes/%s)");
936 arg = states->remote->name;
937 } else if (string_list_has_string(&states->tracked, name))
938 arg = _(" tracked");
939 else if (string_list_has_string(&states->stale, name))
940 arg = _(" stale (use 'git remote prune' to remove)");
941 else
942 arg = _(" ???");
943 printf(" %-*s", info->width, name);
944 printf(fmt, arg);
945 printf("\n");
946 } else
947 printf(" %s\n", name);
949 return 0;
952 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
954 struct show_info *show_info = cb_data;
955 struct ref_states *states = show_info->states;
956 struct branch_info *branch_info = branch_item->util;
957 struct string_list_item *item;
958 int n;
960 if (!branch_info->merge.nr || !branch_info->remote_name ||
961 strcmp(states->remote->name, branch_info->remote_name))
962 return 0;
963 if ((n = strlen(branch_item->string)) > show_info->width)
964 show_info->width = n;
965 if (branch_info->rebase)
966 show_info->any_rebase = 1;
968 item = string_list_insert(show_info->list, branch_item->string);
969 item->util = branch_info;
971 return 0;
974 static int show_local_info_item(struct string_list_item *item, void *cb_data)
976 struct show_info *show_info = cb_data;
977 struct branch_info *branch_info = item->util;
978 struct string_list *merge = &branch_info->merge;
979 const char *also;
980 int i;
982 if (branch_info->rebase && branch_info->merge.nr > 1) {
983 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
984 item->string);
985 return 0;
988 printf(" %-*s ", show_info->width, item->string);
989 if (branch_info->rebase) {
990 printf_ln(_("rebases onto remote %s"), merge->items[0].string);
991 return 0;
992 } else if (show_info->any_rebase) {
993 printf_ln(_(" merges with remote %s"), merge->items[0].string);
994 also = _(" and with remote");
995 } else {
996 printf_ln(_("merges with remote %s"), merge->items[0].string);
997 also = _(" and with remote");
999 for (i = 1; i < merge->nr; i++)
1000 printf(" %-*s %s %s\n", show_info->width, "", also,
1001 merge->items[i].string);
1003 return 0;
1006 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
1008 struct show_info *show_info = cb_data;
1009 struct push_info *push_info = push_item->util;
1010 struct string_list_item *item;
1011 int n;
1012 if ((n = strlen(push_item->string)) > show_info->width)
1013 show_info->width = n;
1014 if ((n = strlen(push_info->dest)) > show_info->width2)
1015 show_info->width2 = n;
1016 item = string_list_append(show_info->list, push_item->string);
1017 item->util = push_item->util;
1018 return 0;
1022 * Sorting comparison for a string list that has push_info
1023 * structs in its util field
1025 static int cmp_string_with_push(const void *va, const void *vb)
1027 const struct string_list_item *a = va;
1028 const struct string_list_item *b = vb;
1029 const struct push_info *a_push = a->util;
1030 const struct push_info *b_push = b->util;
1031 int cmp = strcmp(a->string, b->string);
1032 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1035 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1037 struct show_info *show_info = cb_data;
1038 struct push_info *push_info = item->util;
1039 const char *src = item->string, *status = NULL;
1041 switch (push_info->status) {
1042 case PUSH_STATUS_CREATE:
1043 status = _("create");
1044 break;
1045 case PUSH_STATUS_DELETE:
1046 status = _("delete");
1047 src = _("(none)");
1048 break;
1049 case PUSH_STATUS_UPTODATE:
1050 status = _("up to date");
1051 break;
1052 case PUSH_STATUS_FASTFORWARD:
1053 status = _("fast-forwardable");
1054 break;
1055 case PUSH_STATUS_OUTOFDATE:
1056 status = _("local out of date");
1057 break;
1058 case PUSH_STATUS_NOTQUERIED:
1059 break;
1061 if (status) {
1062 if (push_info->forced)
1063 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1064 show_info->width2, push_info->dest, status);
1065 else
1066 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1067 show_info->width2, push_info->dest, status);
1068 } else {
1069 if (push_info->forced)
1070 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1071 push_info->dest);
1072 else
1073 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1074 push_info->dest);
1076 return 0;
1079 static int show(int argc, const char **argv)
1081 int no_query = 0, result = 0, query_flag = 0;
1082 struct option options[] = {
1083 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1084 OPT_END()
1086 struct ref_states states;
1087 struct string_list info_list = STRING_LIST_INIT_NODUP;
1088 struct show_info info;
1090 argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1093 if (argc < 1)
1094 return show_all();
1096 if (!no_query)
1097 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1099 memset(&states, 0, sizeof(states));
1100 memset(&info, 0, sizeof(info));
1101 info.states = &states;
1102 info.list = &info_list;
1103 for (; argc; argc--, argv++) {
1104 int i;
1105 const char **url;
1106 int url_nr;
1108 get_remote_ref_states(*argv, &states, query_flag);
1110 printf_ln(_("* remote %s"), *argv);
1111 printf_ln(_(" Fetch URL: %s"), states.remote->url_nr > 0 ?
1112 states.remote->url[0] : _("(no URL)"));
1113 if (states.remote->pushurl_nr) {
1114 url = states.remote->pushurl;
1115 url_nr = states.remote->pushurl_nr;
1116 } else {
1117 url = states.remote->url;
1118 url_nr = states.remote->url_nr;
1120 for (i = 0; i < url_nr; i++)
1121 printf_ln(_(" Push URL: %s"), url[i]);
1122 if (!i)
1123 printf_ln(_(" Push URL: %s"), "(no URL)");
1124 if (no_query)
1125 printf_ln(_(" HEAD branch: %s"), "(not queried)");
1126 else if (!states.heads.nr)
1127 printf_ln(_(" HEAD branch: %s"), "(unknown)");
1128 else if (states.heads.nr == 1)
1129 printf_ln(_(" HEAD branch: %s"), states.heads.items[0].string);
1130 else {
1131 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1132 " may be one of the following):\n"));
1133 for (i = 0; i < states.heads.nr; i++)
1134 printf(" %s\n", states.heads.items[i].string);
1137 /* remote branch info */
1138 info.width = 0;
1139 for_each_string_list(&states.new, add_remote_to_show_info, &info);
1140 for_each_string_list(&states.tracked, add_remote_to_show_info, &info);
1141 for_each_string_list(&states.stale, add_remote_to_show_info, &info);
1142 if (info.list->nr)
1143 printf_ln(Q_(" Remote branch:%s",
1144 " Remote branches:%s",
1145 info.list->nr),
1146 no_query ? _(" (status not queried)") : "");
1147 for_each_string_list(info.list, show_remote_info_item, &info);
1148 string_list_clear(info.list, 0);
1150 /* git pull info */
1151 info.width = 0;
1152 info.any_rebase = 0;
1153 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1154 if (info.list->nr)
1155 printf_ln(Q_(" Local branch configured for 'git pull':",
1156 " Local branches configured for 'git pull':",
1157 info.list->nr));
1158 for_each_string_list(info.list, show_local_info_item, &info);
1159 string_list_clear(info.list, 0);
1161 /* git push info */
1162 if (states.remote->mirror)
1163 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1165 info.width = info.width2 = 0;
1166 for_each_string_list(&states.push, add_push_to_show_info, &info);
1167 qsort(info.list->items, info.list->nr,
1168 sizeof(*info.list->items), cmp_string_with_push);
1169 if (info.list->nr)
1170 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1171 " Local refs configured for 'git push'%s:",
1172 info.list->nr),
1173 no_query ? _(" (status not queried)") : "");
1174 for_each_string_list(info.list, show_push_info_item, &info);
1175 string_list_clear(info.list, 0);
1177 free_remote_ref_states(&states);
1180 return result;
1183 static int set_head(int argc, const char **argv)
1185 int i, opt_a = 0, opt_d = 0, result = 0;
1186 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1187 char *head_name = NULL;
1189 struct option options[] = {
1190 OPT_BOOL('a', "auto", &opt_a,
1191 N_("set refs/remotes/<name>/HEAD according to remote")),
1192 OPT_BOOL('d', "delete", &opt_d,
1193 N_("delete refs/remotes/<name>/HEAD")),
1194 OPT_END()
1196 argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1198 if (argc)
1199 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1201 if (!opt_a && !opt_d && argc == 2) {
1202 head_name = xstrdup(argv[1]);
1203 } else if (opt_a && !opt_d && argc == 1) {
1204 struct ref_states states;
1205 memset(&states, 0, sizeof(states));
1206 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1207 if (!states.heads.nr)
1208 result |= error(_("Cannot determine remote HEAD"));
1209 else if (states.heads.nr > 1) {
1210 result |= error(_("Multiple remote HEAD branches. "
1211 "Please choose one explicitly with:"));
1212 for (i = 0; i < states.heads.nr; i++)
1213 fprintf(stderr, " git remote set-head %s %s\n",
1214 argv[0], states.heads.items[i].string);
1215 } else
1216 head_name = xstrdup(states.heads.items[0].string);
1217 free_remote_ref_states(&states);
1218 } else if (opt_d && !opt_a && argc == 1) {
1219 if (delete_ref(buf.buf, NULL, REF_NODEREF))
1220 result |= error(_("Could not delete %s"), buf.buf);
1221 } else
1222 usage_with_options(builtin_remote_sethead_usage, options);
1224 if (head_name) {
1225 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1226 /* make sure it's valid */
1227 if (!ref_exists(buf2.buf))
1228 result |= error(_("Not a valid ref: %s"), buf2.buf);
1229 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1230 result |= error(_("Could not setup %s"), buf.buf);
1231 if (opt_a)
1232 printf("%s/HEAD set to %s\n", argv[0], head_name);
1233 free(head_name);
1236 strbuf_release(&buf);
1237 strbuf_release(&buf2);
1238 return result;
1241 static int prune(int argc, const char **argv)
1243 int dry_run = 0, result = 0;
1244 struct option options[] = {
1245 OPT__DRY_RUN(&dry_run, N_("dry run")),
1246 OPT_END()
1249 argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1252 if (argc < 1)
1253 usage_with_options(builtin_remote_prune_usage, options);
1255 for (; argc; argc--, argv++)
1256 result |= prune_remote(*argv, dry_run);
1258 return result;
1261 static int prune_remote(const char *remote, int dry_run)
1263 int result = 0, i;
1264 struct ref_states states;
1265 const char *dangling_msg = dry_run
1266 ? _(" %s will become dangling!")
1267 : _(" %s has become dangling!");
1269 memset(&states, 0, sizeof(states));
1270 get_remote_ref_states(remote, &states, GET_REF_STATES);
1272 if (states.stale.nr) {
1273 printf_ln(_("Pruning %s"), remote);
1274 printf_ln(_("URL: %s"),
1275 states.remote->url_nr
1276 ? states.remote->url[0]
1277 : _("(no URL)"));
1280 for (i = 0; i < states.stale.nr; i++) {
1281 const char *refname = states.stale.items[i].util;
1283 if (!dry_run)
1284 result |= delete_ref(refname, NULL, 0);
1286 if (dry_run)
1287 printf_ln(_(" * [would prune] %s"),
1288 abbrev_ref(refname, "refs/remotes/"));
1289 else
1290 printf_ln(_(" * [pruned] %s"),
1291 abbrev_ref(refname, "refs/remotes/"));
1292 warn_dangling_symref(stdout, dangling_msg, refname);
1295 free_remote_ref_states(&states);
1296 return result;
1299 static int get_remote_default(const char *key, const char *value, void *priv)
1301 if (strcmp(key, "remotes.default") == 0) {
1302 int *found = priv;
1303 *found = 1;
1305 return 0;
1308 static int update(int argc, const char **argv)
1310 int i, prune = 0;
1311 struct option options[] = {
1312 OPT_BOOL('p', "prune", &prune,
1313 N_("prune remotes after fetching")),
1314 OPT_END()
1316 const char **fetch_argv;
1317 int fetch_argc = 0;
1318 int default_defined = 0;
1320 fetch_argv = xmalloc(sizeof(char *) * (argc+5));
1322 argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1323 PARSE_OPT_KEEP_ARGV0);
1325 fetch_argv[fetch_argc++] = "fetch";
1327 if (prune)
1328 fetch_argv[fetch_argc++] = "--prune";
1329 if (verbose)
1330 fetch_argv[fetch_argc++] = "-v";
1331 fetch_argv[fetch_argc++] = "--multiple";
1332 if (argc < 2)
1333 fetch_argv[fetch_argc++] = "default";
1334 for (i = 1; i < argc; i++)
1335 fetch_argv[fetch_argc++] = argv[i];
1337 if (strcmp(fetch_argv[fetch_argc-1], "default") == 0) {
1338 git_config(get_remote_default, &default_defined);
1339 if (!default_defined)
1340 fetch_argv[fetch_argc-1] = "--all";
1343 fetch_argv[fetch_argc] = NULL;
1345 return run_command_v_opt(fetch_argv, RUN_GIT_CMD);
1348 static int remove_all_fetch_refspecs(const char *remote, const char *key)
1350 return git_config_set_multivar(key, NULL, NULL, 1);
1353 static int add_branches(struct remote *remote, const char **branches,
1354 const char *key)
1356 const char *remotename = remote->name;
1357 int mirror = remote->mirror;
1358 struct strbuf refspec = STRBUF_INIT;
1360 for (; *branches; branches++)
1361 if (add_branch(key, *branches, remotename, mirror, &refspec)) {
1362 strbuf_release(&refspec);
1363 return 1;
1366 strbuf_release(&refspec);
1367 return 0;
1370 static int set_remote_branches(const char *remotename, const char **branches,
1371 int add_mode)
1373 struct strbuf key = STRBUF_INIT;
1374 struct remote *remote;
1376 strbuf_addf(&key, "remote.%s.fetch", remotename);
1378 if (!remote_is_configured(remotename))
1379 die(_("No such remote '%s'"), remotename);
1380 remote = remote_get(remotename);
1382 if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) {
1383 strbuf_release(&key);
1384 return 1;
1386 if (add_branches(remote, branches, key.buf)) {
1387 strbuf_release(&key);
1388 return 1;
1391 strbuf_release(&key);
1392 return 0;
1395 static int set_branches(int argc, const char **argv)
1397 int add_mode = 0;
1398 struct option options[] = {
1399 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1400 OPT_END()
1403 argc = parse_options(argc, argv, NULL, options,
1404 builtin_remote_setbranches_usage, 0);
1405 if (argc == 0) {
1406 error(_("no remote specified"));
1407 usage_with_options(builtin_remote_setbranches_usage, options);
1409 argv[argc] = NULL;
1411 return set_remote_branches(argv[0], argv + 1, add_mode);
1414 static int set_url(int argc, const char **argv)
1416 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1417 int matches = 0, negative_matches = 0;
1418 const char *remotename = NULL;
1419 const char *newurl = NULL;
1420 const char *oldurl = NULL;
1421 struct remote *remote;
1422 regex_t old_regex;
1423 const char **urlset;
1424 int urlset_nr;
1425 struct strbuf name_buf = STRBUF_INIT;
1426 struct option options[] = {
1427 OPT_BOOL('\0', "push", &push_mode,
1428 N_("manipulate push URLs")),
1429 OPT_BOOL('\0', "add", &add_mode,
1430 N_("add URL")),
1431 OPT_BOOL('\0', "delete", &delete_mode,
1432 N_("delete URLs")),
1433 OPT_END()
1435 argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage,
1436 PARSE_OPT_KEEP_ARGV0);
1438 if (add_mode && delete_mode)
1439 die(_("--add --delete doesn't make sense"));
1441 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1442 usage_with_options(builtin_remote_seturl_usage, options);
1444 remotename = argv[1];
1445 newurl = argv[2];
1446 if (argc > 3)
1447 oldurl = argv[3];
1449 if (delete_mode)
1450 oldurl = newurl;
1452 if (!remote_is_configured(remotename))
1453 die(_("No such remote '%s'"), remotename);
1454 remote = remote_get(remotename);
1456 if (push_mode) {
1457 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1458 urlset = remote->pushurl;
1459 urlset_nr = remote->pushurl_nr;
1460 } else {
1461 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1462 urlset = remote->url;
1463 urlset_nr = remote->url_nr;
1466 /* Special cases that add new entry. */
1467 if ((!oldurl && !delete_mode) || add_mode) {
1468 if (add_mode)
1469 git_config_set_multivar(name_buf.buf, newurl,
1470 "^$", 0);
1471 else
1472 git_config_set(name_buf.buf, newurl);
1473 strbuf_release(&name_buf);
1474 return 0;
1477 /* Old URL specified. Demand that one matches. */
1478 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1479 die(_("Invalid old URL pattern: %s"), oldurl);
1481 for (i = 0; i < urlset_nr; i++)
1482 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1483 matches++;
1484 else
1485 negative_matches++;
1486 if (!delete_mode && !matches)
1487 die(_("No such URL found: %s"), oldurl);
1488 if (delete_mode && !negative_matches && !push_mode)
1489 die(_("Will not delete all non-push URLs"));
1491 regfree(&old_regex);
1493 if (!delete_mode)
1494 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1495 else
1496 git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
1497 return 0;
1500 static int get_one_entry(struct remote *remote, void *priv)
1502 struct string_list *list = priv;
1503 struct strbuf url_buf = STRBUF_INIT;
1504 const char **url;
1505 int i, url_nr;
1507 if (remote->url_nr > 0) {
1508 strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
1509 string_list_append(list, remote->name)->util =
1510 strbuf_detach(&url_buf, NULL);
1511 } else
1512 string_list_append(list, remote->name)->util = NULL;
1513 if (remote->pushurl_nr) {
1514 url = remote->pushurl;
1515 url_nr = remote->pushurl_nr;
1516 } else {
1517 url = remote->url;
1518 url_nr = remote->url_nr;
1520 for (i = 0; i < url_nr; i++)
1522 strbuf_addf(&url_buf, "%s (push)", url[i]);
1523 string_list_append(list, remote->name)->util =
1524 strbuf_detach(&url_buf, NULL);
1527 return 0;
1530 static int show_all(void)
1532 struct string_list list = STRING_LIST_INIT_NODUP;
1533 int result;
1535 list.strdup_strings = 1;
1536 result = for_each_remote(get_one_entry, &list);
1538 if (!result) {
1539 int i;
1541 sort_string_list(&list);
1542 for (i = 0; i < list.nr; i++) {
1543 struct string_list_item *item = list.items + i;
1544 if (verbose)
1545 printf("%s\t%s\n", item->string,
1546 item->util ? (const char *)item->util : "");
1547 else {
1548 if (i && !strcmp((item - 1)->string, item->string))
1549 continue;
1550 printf("%s\n", item->string);
1554 string_list_clear(&list, 1);
1555 return result;
1558 int cmd_remote(int argc, const char **argv, const char *prefix)
1560 struct option options[] = {
1561 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1562 OPT_END()
1564 int result;
1566 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1567 PARSE_OPT_STOP_AT_NON_OPTION);
1569 if (argc < 1)
1570 result = show_all();
1571 else if (!strcmp(argv[0], "add"))
1572 result = add(argc, argv);
1573 else if (!strcmp(argv[0], "rename"))
1574 result = mv(argc, argv);
1575 else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove"))
1576 result = rm(argc, argv);
1577 else if (!strcmp(argv[0], "set-head"))
1578 result = set_head(argc, argv);
1579 else if (!strcmp(argv[0], "set-branches"))
1580 result = set_branches(argc, argv);
1581 else if (!strcmp(argv[0], "set-url"))
1582 result = set_url(argc, argv);
1583 else if (!strcmp(argv[0], "show"))
1584 result = show(argc, argv);
1585 else if (!strcmp(argv[0], "prune"))
1586 result = prune(argc, argv);
1587 else if (!strcmp(argv[0], "update"))
1588 result = update(argc, argv);
1589 else {
1590 error(_("Unknown subcommand: %s"), argv[0]);
1591 usage_with_options(builtin_remote_usage, options);
1594 return result ? 1 : 0;