2 #include "parse-options.h"
5 #include "string-list.h"
7 #include "run-command.h"
9 #include "argv-array.h"
11 static const char * const builtin_remote_usage[] = {
12 N_("git remote [-v | --verbose]"),
13 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
14 N_("git remote rename <old> <new>"),
15 N_("git remote remove <name>"),
16 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
17 N_("git remote [-v | --verbose] show [-n] <name>"),
18 N_("git remote prune [-n | --dry-run] <name>"),
19 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
20 N_("git remote set-branches [--add] <name> <branch>..."),
21 N_("git remote get-url [--push] [--all] <name>"),
22 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
23 N_("git remote set-url --add <name> <newurl>"),
24 N_("git remote set-url --delete <name> <url>"),
28 static const char * const builtin_remote_add_usage[] = {
29 N_("git remote add [<options>] <name> <url>"),
33 static const char * const builtin_remote_rename_usage[] = {
34 N_("git remote rename <old> <new>"),
38 static const char * const builtin_remote_rm_usage[] = {
39 N_("git remote remove <name>"),
43 static const char * const builtin_remote_sethead_usage[] = {
44 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
48 static const char * const builtin_remote_setbranches_usage[] = {
49 N_("git remote set-branches <name> <branch>..."),
50 N_("git remote set-branches --add <name> <branch>..."),
54 static const char * const builtin_remote_show_usage[] = {
55 N_("git remote show [<options>] <name>"),
59 static const char * const builtin_remote_prune_usage[] = {
60 N_("git remote prune [<options>] <name>"),
64 static const char * const builtin_remote_update_usage[] = {
65 N_("git remote update [<options>] [<group> | <remote>]..."),
69 static const char * const builtin_remote_geturl_usage[] = {
70 N_("git remote get-url [--push] [--all] <name>"),
74 static const char * const builtin_remote_seturl_usage[] = {
75 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
76 N_("git remote set-url --add <name> <newurl>"),
77 N_("git remote set-url --delete <name> <url>"),
81 #define GET_REF_STATES (1<<0)
82 #define GET_HEAD_NAMES (1<<1)
83 #define GET_PUSH_REF_STATES (1<<2)
87 static int fetch_remote(const char *name)
89 const char *argv[] = { "fetch", name, NULL, NULL };
94 printf_ln(_("Updating %s"), name);
95 if (run_command_v_opt(argv, RUN_GIT_CMD))
96 return error(_("Could not fetch %s"), name);
106 #define MIRROR_NONE 0
107 #define MIRROR_FETCH 1
108 #define MIRROR_PUSH 2
109 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
111 static void add_branch(const char *key, const char *branchname,
112 const char *remotename, int mirror, struct strbuf *tmp)
115 strbuf_addch(tmp, '+');
117 strbuf_addf(tmp, "refs/%s:refs/%s",
118 branchname, branchname);
120 strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
121 branchname, remotename, branchname);
122 git_config_set_multivar(key, tmp->buf, "^$", 0);
125 static const char mirror_advice[] =
126 N_("--mirror is dangerous and deprecated; please\n"
127 "\t use --mirror=fetch or --mirror=push instead");
129 static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
131 unsigned *mirror = opt->value;
133 *mirror = MIRROR_NONE;
135 warning("%s", _(mirror_advice));
136 *mirror = MIRROR_BOTH;
138 else if (!strcmp(arg, "fetch"))
139 *mirror = MIRROR_FETCH;
140 else if (!strcmp(arg, "push"))
141 *mirror = MIRROR_PUSH;
143 return error(_("unknown mirror argument: %s"), arg);
147 static int add(int argc, const char **argv)
149 int fetch = 0, fetch_tags = TAGS_DEFAULT;
150 unsigned mirror = MIRROR_NONE;
151 struct string_list track = STRING_LIST_INIT_NODUP;
152 const char *master = NULL;
153 struct remote *remote;
154 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
155 const char *name, *url;
158 struct option options[] = {
159 OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
160 OPT_SET_INT(0, "tags", &fetch_tags,
161 N_("import all tags and associated objects when fetching"),
163 OPT_SET_INT(0, NULL, &fetch_tags,
164 N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET),
165 OPT_STRING_LIST('t', "track", &track, N_("branch"),
166 N_("branch(es) to track")),
167 OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")),
168 { OPTION_CALLBACK, 0, "mirror", &mirror, N_("push|fetch"),
169 N_("set up remote as a mirror to push to or fetch from"),
170 PARSE_OPT_OPTARG, parse_mirror_opt },
174 argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage,
178 usage_with_options(builtin_remote_add_usage, options);
180 if (mirror && master)
181 die(_("specifying a master branch makes no sense with --mirror"));
182 if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
183 die(_("specifying branches to track makes sense only with fetch mirrors"));
188 remote = remote_get(name);
189 if (remote_is_configured(remote, 1))
190 die(_("remote %s already exists."), name);
192 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
193 if (!valid_fetch_refspec(buf2.buf))
194 die(_("'%s' is not a valid remote name"), name);
196 strbuf_addf(&buf, "remote.%s.url", name);
197 git_config_set(buf.buf, url);
199 if (!mirror || mirror & MIRROR_FETCH) {
201 strbuf_addf(&buf, "remote.%s.fetch", name);
203 string_list_append(&track, "*");
204 for (i = 0; i < track.nr; i++) {
205 add_branch(buf.buf, track.items[i].string,
206 name, mirror, &buf2);
210 if (mirror & MIRROR_PUSH) {
212 strbuf_addf(&buf, "remote.%s.mirror", name);
213 git_config_set(buf.buf, "true");
216 if (fetch_tags != TAGS_DEFAULT) {
218 strbuf_addf(&buf, "remote.%s.tagopt", name);
219 git_config_set(buf.buf,
220 fetch_tags == TAGS_SET ? "--tags" : "--no-tags");
223 if (fetch && fetch_remote(name))
228 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
231 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
233 if (create_symref(buf.buf, buf2.buf, "remote add"))
234 return error(_("Could not setup master '%s'"), master);
237 strbuf_release(&buf);
238 strbuf_release(&buf2);
239 string_list_clear(&track, 0);
246 struct string_list merge;
247 enum { NO_REBASE, NORMAL_REBASE, INTERACTIVE_REBASE } rebase;
250 static struct string_list branch_list = STRING_LIST_INIT_NODUP;
252 static const char *abbrev_ref(const char *name, const char *prefix)
254 skip_prefix(name, prefix, &name);
257 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
259 static int config_read_branches(const char *key, const char *value, void *cb)
261 if (starts_with(key, "branch.")) {
262 const char *orig_key = key;
264 struct string_list_item *item;
265 struct branch_info *info;
266 enum { REMOTE, MERGE, REBASE } type;
270 if (strip_suffix(key, ".remote", &key_len)) {
271 name = xmemdupz(key, key_len);
273 } else if (strip_suffix(key, ".merge", &key_len)) {
274 name = xmemdupz(key, key_len);
276 } else if (strip_suffix(key, ".rebase", &key_len)) {
277 name = xmemdupz(key, key_len);
282 item = string_list_insert(&branch_list, name);
285 item->util = xcalloc(1, sizeof(struct branch_info));
287 if (type == REMOTE) {
288 if (info->remote_name)
289 warning(_("more than one %s"), orig_key);
290 info->remote_name = xstrdup(value);
291 } else if (type == MERGE) {
292 char *space = strchr(value, ' ');
293 value = abbrev_branch(value);
296 merge = xstrndup(value, space - value);
297 string_list_append(&info->merge, merge);
298 value = abbrev_branch(space + 1);
299 space = strchr(value, ' ');
301 string_list_append(&info->merge, xstrdup(value));
303 int v = git_config_maybe_bool(orig_key, value);
306 else if (!strcmp(value, "preserve"))
307 info->rebase = NORMAL_REBASE;
308 else if (!strcmp(value, "interactive"))
309 info->rebase = INTERACTIVE_REBASE;
315 static void read_branches(void)
319 git_config(config_read_branches, NULL);
323 struct remote *remote;
324 struct string_list new, stale, tracked, heads, push;
328 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
330 struct ref *fetch_map = NULL, **tail = &fetch_map;
331 struct ref *ref, *stale_refs;
334 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
335 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
336 die(_("Could not get fetch map for refspec %s"),
337 states->remote->fetch_refspec[i]);
339 states->new.strdup_strings = 1;
340 states->tracked.strdup_strings = 1;
341 states->stale.strdup_strings = 1;
342 for (ref = fetch_map; ref; ref = ref->next) {
343 if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
344 string_list_append(&states->new, abbrev_branch(ref->name));
346 string_list_append(&states->tracked, abbrev_branch(ref->name));
348 stale_refs = get_stale_heads(states->remote->fetch,
349 states->remote->fetch_refspec_nr, fetch_map);
350 for (ref = stale_refs; ref; ref = ref->next) {
351 struct string_list_item *item =
352 string_list_append(&states->stale, abbrev_branch(ref->name));
353 item->util = xstrdup(ref->name);
355 free_refs(stale_refs);
356 free_refs(fetch_map);
358 string_list_sort(&states->new);
359 string_list_sort(&states->tracked);
360 string_list_sort(&states->stale);
369 PUSH_STATUS_CREATE = 0,
371 PUSH_STATUS_UPTODATE,
372 PUSH_STATUS_FASTFORWARD,
373 PUSH_STATUS_OUTOFDATE,
374 PUSH_STATUS_NOTQUERIED
378 static int get_push_ref_states(const struct ref *remote_refs,
379 struct ref_states *states)
381 struct remote *remote = states->remote;
382 struct ref *ref, *local_refs, *push_map;
386 local_refs = get_local_heads();
387 push_map = copy_ref_list(remote_refs);
389 match_push_refs(local_refs, &push_map, remote->push_refspec_nr,
390 remote->push_refspec, MATCH_REFS_NONE);
392 states->push.strdup_strings = 1;
393 for (ref = push_map; ref; ref = ref->next) {
394 struct string_list_item *item;
395 struct push_info *info;
399 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
401 item = string_list_append(&states->push,
402 abbrev_branch(ref->peer_ref->name));
403 item->util = xcalloc(1, sizeof(struct push_info));
405 info->forced = ref->force;
406 info->dest = xstrdup(abbrev_branch(ref->name));
408 if (is_null_oid(&ref->new_oid)) {
409 info->status = PUSH_STATUS_DELETE;
410 } else if (!oidcmp(&ref->old_oid, &ref->new_oid))
411 info->status = PUSH_STATUS_UPTODATE;
412 else if (is_null_oid(&ref->old_oid))
413 info->status = PUSH_STATUS_CREATE;
414 else if (has_object_file(&ref->old_oid) &&
415 ref_newer(&ref->new_oid, &ref->old_oid))
416 info->status = PUSH_STATUS_FASTFORWARD;
418 info->status = PUSH_STATUS_OUTOFDATE;
420 free_refs(local_refs);
425 static int get_push_ref_states_noquery(struct ref_states *states)
428 struct remote *remote = states->remote;
429 struct string_list_item *item;
430 struct push_info *info;
435 states->push.strdup_strings = 1;
436 if (!remote->push_refspec_nr) {
437 item = string_list_append(&states->push, _("(matching)"));
438 info = item->util = xcalloc(1, sizeof(struct push_info));
439 info->status = PUSH_STATUS_NOTQUERIED;
440 info->dest = xstrdup(item->string);
442 for (i = 0; i < remote->push_refspec_nr; i++) {
443 struct refspec *spec = remote->push + i;
445 item = string_list_append(&states->push, _("(matching)"));
446 else if (strlen(spec->src))
447 item = string_list_append(&states->push, spec->src);
449 item = string_list_append(&states->push, _("(delete)"));
451 info = item->util = xcalloc(1, sizeof(struct push_info));
452 info->forced = spec->force;
453 info->status = PUSH_STATUS_NOTQUERIED;
454 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
459 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
461 struct ref *ref, *matches;
462 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
463 struct refspec refspec;
467 refspec.src = refspec.dst = "refs/heads/*";
468 states->heads.strdup_strings = 1;
469 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
470 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
472 for (ref = matches; ref; ref = ref->next)
473 string_list_append(&states->heads, abbrev_branch(ref->name));
475 free_refs(fetch_map);
481 struct known_remote {
482 struct known_remote *next;
483 struct remote *remote;
486 struct known_remotes {
487 struct remote *to_delete;
488 struct known_remote *list;
491 static int add_known_remote(struct remote *remote, void *cb_data)
493 struct known_remotes *all = cb_data;
494 struct known_remote *r;
496 if (!strcmp(all->to_delete->name, remote->name))
499 r = xmalloc(sizeof(*r));
506 struct branches_for_remote {
507 struct remote *remote;
508 struct string_list *branches, *skipped;
509 struct known_remotes *keep;
512 static int add_branch_for_removal(const char *refname,
513 const struct object_id *oid, int flags, void *cb_data)
515 struct branches_for_remote *branches = cb_data;
516 struct refspec refspec;
517 struct known_remote *kr;
519 memset(&refspec, 0, sizeof(refspec));
520 refspec.dst = (char *)refname;
521 if (remote_find_tracking(branches->remote, &refspec))
524 /* don't delete a branch if another remote also uses it */
525 for (kr = branches->keep->list; kr; kr = kr->next) {
526 memset(&refspec, 0, sizeof(refspec));
527 refspec.dst = (char *)refname;
528 if (!remote_find_tracking(kr->remote, &refspec))
532 /* don't delete non-remote-tracking refs */
533 if (!starts_with(refname, "refs/remotes/")) {
534 /* advise user how to delete local branches */
535 if (starts_with(refname, "refs/heads/"))
536 string_list_append(branches->skipped,
537 abbrev_branch(refname));
538 /* silently skip over other non-remote refs */
542 string_list_append(branches->branches, refname);
550 struct string_list *remote_branches;
553 static int read_remote_branches(const char *refname,
554 const struct object_id *oid, int flags, void *cb_data)
556 struct rename_info *rename = cb_data;
557 struct strbuf buf = STRBUF_INIT;
558 struct string_list_item *item;
560 struct object_id orig_oid;
563 strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
564 if (starts_with(refname, buf.buf)) {
565 item = string_list_append(rename->remote_branches, xstrdup(refname));
566 symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
567 orig_oid.hash, &flag);
568 if (flag & REF_ISSYMREF)
569 item->util = xstrdup(symref);
577 static int migrate_file(struct remote *remote)
579 struct strbuf buf = STRBUF_INIT;
582 strbuf_addf(&buf, "remote.%s.url", remote->name);
583 for (i = 0; i < remote->url_nr; i++)
584 git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
586 strbuf_addf(&buf, "remote.%s.push", remote->name);
587 for (i = 0; i < remote->push_refspec_nr; i++)
588 git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0);
590 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
591 for (i = 0; i < remote->fetch_refspec_nr; i++)
592 git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0);
593 if (remote->origin == REMOTE_REMOTES)
594 unlink_or_warn(git_path("remotes/%s", remote->name));
595 else if (remote->origin == REMOTE_BRANCHES)
596 unlink_or_warn(git_path("branches/%s", remote->name));
601 static int mv(int argc, const char **argv)
603 struct option options[] = {
606 struct remote *oldremote, *newremote;
607 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
608 old_remote_context = STRBUF_INIT;
609 struct string_list remote_branches = STRING_LIST_INIT_NODUP;
610 struct rename_info rename;
611 int i, refspec_updated = 0;
614 usage_with_options(builtin_remote_rename_usage, options);
616 rename.old = argv[1];
617 rename.new = argv[2];
618 rename.remote_branches = &remote_branches;
620 oldremote = remote_get(rename.old);
621 if (!remote_is_configured(oldremote, 1))
622 die(_("No such remote: %s"), rename.old);
624 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
625 return migrate_file(oldremote);
627 newremote = remote_get(rename.new);
628 if (remote_is_configured(newremote, 1))
629 die(_("remote %s already exists."), rename.new);
631 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
632 if (!valid_fetch_refspec(buf.buf))
633 die(_("'%s' is not a valid remote name"), rename.new);
636 strbuf_addf(&buf, "remote.%s", rename.old);
637 strbuf_addf(&buf2, "remote.%s", rename.new);
638 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
639 return error(_("Could not rename config section '%s' to '%s'"),
643 strbuf_addf(&buf, "remote.%s.fetch", rename.new);
644 git_config_set_multivar(buf.buf, NULL, NULL, 1);
645 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old);
646 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
650 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
651 ptr = strstr(buf2.buf, old_remote_context.buf);
655 ptr-buf2.buf + strlen(":refs/remotes/"),
656 strlen(rename.old), rename.new,
659 warning(_("Not updating non-default fetch refspec\n"
661 "\tPlease update the configuration manually if necessary."),
664 git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
668 for (i = 0; i < branch_list.nr; i++) {
669 struct string_list_item *item = branch_list.items + i;
670 struct branch_info *info = item->util;
671 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
673 strbuf_addf(&buf, "branch.%s.remote", item->string);
674 git_config_set(buf.buf, rename.new);
678 if (!refspec_updated)
682 * First remove symrefs, then rename the rest, finally create
685 for_each_ref(read_remote_branches, &rename);
686 for (i = 0; i < remote_branches.nr; i++) {
687 struct string_list_item *item = remote_branches.items + i;
689 struct object_id oid;
691 read_ref_full(item->string, RESOLVE_REF_READING, oid.hash, &flag);
692 if (!(flag & REF_ISSYMREF))
694 if (delete_ref(NULL, item->string, NULL, REF_NODEREF))
695 die(_("deleting '%s' failed"), item->string);
697 for (i = 0; i < remote_branches.nr; i++) {
698 struct string_list_item *item = remote_branches.items + i;
703 strbuf_addstr(&buf, item->string);
704 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
705 rename.new, strlen(rename.new));
707 strbuf_addf(&buf2, "remote: renamed %s to %s",
708 item->string, buf.buf);
709 if (rename_ref(item->string, buf.buf, buf2.buf))
710 die(_("renaming '%s' failed"), item->string);
712 for (i = 0; i < remote_branches.nr; i++) {
713 struct string_list_item *item = remote_branches.items + i;
718 strbuf_addstr(&buf, item->string);
719 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
720 rename.new, strlen(rename.new));
722 strbuf_addstr(&buf2, item->util);
723 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
724 rename.new, strlen(rename.new));
726 strbuf_addf(&buf3, "remote: renamed %s to %s",
727 item->string, buf.buf);
728 if (create_symref(buf.buf, buf2.buf, buf3.buf))
729 die(_("creating '%s' failed"), buf.buf);
734 static int rm(int argc, const char **argv)
736 struct option options[] = {
739 struct remote *remote;
740 struct strbuf buf = STRBUF_INIT;
741 struct known_remotes known_remotes = { NULL, NULL };
742 struct string_list branches = STRING_LIST_INIT_DUP;
743 struct string_list skipped = STRING_LIST_INIT_DUP;
744 struct branches_for_remote cb_data;
747 memset(&cb_data, 0, sizeof(cb_data));
748 cb_data.branches = &branches;
749 cb_data.skipped = &skipped;
750 cb_data.keep = &known_remotes;
753 usage_with_options(builtin_remote_rm_usage, options);
755 remote = remote_get(argv[1]);
756 if (!remote_is_configured(remote, 1))
757 die(_("No such remote: %s"), argv[1]);
759 known_remotes.to_delete = remote;
760 for_each_remote(add_known_remote, &known_remotes);
763 for (i = 0; i < branch_list.nr; i++) {
764 struct string_list_item *item = branch_list.items + i;
765 struct branch_info *info = item->util;
766 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
767 const char *keys[] = { "remote", "merge", NULL }, **k;
768 for (k = keys; *k; k++) {
770 strbuf_addf(&buf, "branch.%s.%s",
772 result = git_config_set_gently(buf.buf, NULL);
773 if (result && result != CONFIG_NOTHING_SET)
774 die(_("could not unset '%s'"), buf.buf);
780 * We cannot just pass a function to for_each_ref() which deletes
781 * the branches one by one, since for_each_ref() relies on cached
782 * refs, which are invalidated when deleting a branch.
784 cb_data.remote = remote;
785 result = for_each_ref(add_branch_for_removal, &cb_data);
786 strbuf_release(&buf);
789 result = delete_refs(&branches, REF_NODEREF);
790 string_list_clear(&branches, 0);
794 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
795 "to delete it, use:",
796 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
797 "to delete them, use:",
799 for (i = 0; i < skipped.nr; i++)
800 fprintf(stderr, " git branch -d %s\n",
801 skipped.items[i].string);
803 string_list_clear(&skipped, 0);
806 strbuf_addf(&buf, "remote.%s", remote->name);
807 if (git_config_rename_section(buf.buf, NULL) < 1)
808 return error(_("Could not remove config section '%s'"), buf.buf);
814 static void clear_push_info(void *util, const char *string)
816 struct push_info *info = util;
821 static void free_remote_ref_states(struct ref_states *states)
823 string_list_clear(&states->new, 0);
824 string_list_clear(&states->stale, 1);
825 string_list_clear(&states->tracked, 0);
826 string_list_clear(&states->heads, 0);
827 string_list_clear_func(&states->push, clear_push_info);
830 static int append_ref_to_tracked_list(const char *refname,
831 const struct object_id *oid, int flags, void *cb_data)
833 struct ref_states *states = cb_data;
834 struct refspec refspec;
836 if (flags & REF_ISSYMREF)
839 memset(&refspec, 0, sizeof(refspec));
840 refspec.dst = (char *)refname;
841 if (!remote_find_tracking(states->remote, &refspec))
842 string_list_append(&states->tracked, abbrev_branch(refspec.src));
847 static int get_remote_ref_states(const char *name,
848 struct ref_states *states,
851 struct transport *transport;
852 const struct ref *remote_refs;
854 states->remote = remote_get(name);
856 return error(_("No such remote: %s"), name);
861 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
862 states->remote->url[0] : NULL);
863 remote_refs = transport_get_remote_refs(transport);
864 transport_disconnect(transport);
867 if (query & GET_REF_STATES)
868 get_ref_states(remote_refs, states);
869 if (query & GET_HEAD_NAMES)
870 get_head_names(remote_refs, states);
871 if (query & GET_PUSH_REF_STATES)
872 get_push_ref_states(remote_refs, states);
874 for_each_ref(append_ref_to_tracked_list, states);
875 string_list_sort(&states->tracked);
876 get_push_ref_states_noquery(states);
883 struct string_list *list;
884 struct ref_states *states;
889 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
891 struct show_info *info = cb_data;
892 int n = strlen(item->string);
895 string_list_insert(info->list, item->string);
899 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
901 struct show_info *info = cb_data;
902 struct ref_states *states = info->states;
903 const char *name = item->string;
905 if (states->queried) {
906 const char *fmt = "%s";
907 const char *arg = "";
908 if (string_list_has_string(&states->new, name)) {
909 fmt = _(" new (next fetch will store in remotes/%s)");
910 arg = states->remote->name;
911 } else if (string_list_has_string(&states->tracked, name))
913 else if (string_list_has_string(&states->stale, name))
914 arg = _(" stale (use 'git remote prune' to remove)");
917 printf(" %-*s", info->width, name);
921 printf(" %s\n", name);
926 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
928 struct show_info *show_info = cb_data;
929 struct ref_states *states = show_info->states;
930 struct branch_info *branch_info = branch_item->util;
931 struct string_list_item *item;
934 if (!branch_info->merge.nr || !branch_info->remote_name ||
935 strcmp(states->remote->name, branch_info->remote_name))
937 if ((n = strlen(branch_item->string)) > show_info->width)
938 show_info->width = n;
939 if (branch_info->rebase)
940 show_info->any_rebase = 1;
942 item = string_list_insert(show_info->list, branch_item->string);
943 item->util = branch_info;
948 static int show_local_info_item(struct string_list_item *item, void *cb_data)
950 struct show_info *show_info = cb_data;
951 struct branch_info *branch_info = item->util;
952 struct string_list *merge = &branch_info->merge;
953 int width = show_info->width + 4;
956 if (branch_info->rebase && branch_info->merge.nr > 1) {
957 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
962 printf(" %-*s ", show_info->width, item->string);
963 if (branch_info->rebase) {
964 printf_ln(branch_info->rebase == INTERACTIVE_REBASE
965 ? _("rebases interactively onto remote %s")
966 : _("rebases onto remote %s"), merge->items[0].string);
968 } else if (show_info->any_rebase) {
969 printf_ln(_(" merges with remote %s"), merge->items[0].string);
972 printf_ln(_("merges with remote %s"), merge->items[0].string);
974 for (i = 1; i < merge->nr; i++)
975 printf(_("%-*s and with remote %s\n"), width, "",
976 merge->items[i].string);
981 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
983 struct show_info *show_info = cb_data;
984 struct push_info *push_info = push_item->util;
985 struct string_list_item *item;
987 if ((n = strlen(push_item->string)) > show_info->width)
988 show_info->width = n;
989 if ((n = strlen(push_info->dest)) > show_info->width2)
990 show_info->width2 = n;
991 item = string_list_append(show_info->list, push_item->string);
992 item->util = push_item->util;
997 * Sorting comparison for a string list that has push_info
998 * structs in its util field
1000 static int cmp_string_with_push(const void *va, const void *vb)
1002 const struct string_list_item *a = va;
1003 const struct string_list_item *b = vb;
1004 const struct push_info *a_push = a->util;
1005 const struct push_info *b_push = b->util;
1006 int cmp = strcmp(a->string, b->string);
1007 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1010 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1012 struct show_info *show_info = cb_data;
1013 struct push_info *push_info = item->util;
1014 const char *src = item->string, *status = NULL;
1016 switch (push_info->status) {
1017 case PUSH_STATUS_CREATE:
1018 status = _("create");
1020 case PUSH_STATUS_DELETE:
1021 status = _("delete");
1024 case PUSH_STATUS_UPTODATE:
1025 status = _("up to date");
1027 case PUSH_STATUS_FASTFORWARD:
1028 status = _("fast-forwardable");
1030 case PUSH_STATUS_OUTOFDATE:
1031 status = _("local out of date");
1033 case PUSH_STATUS_NOTQUERIED:
1037 if (push_info->forced)
1038 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1039 show_info->width2, push_info->dest, status);
1041 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1042 show_info->width2, push_info->dest, status);
1044 if (push_info->forced)
1045 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1048 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1054 static int get_one_entry(struct remote *remote, void *priv)
1056 struct string_list *list = priv;
1057 struct strbuf url_buf = STRBUF_INIT;
1061 if (remote->url_nr > 0) {
1062 strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
1063 string_list_append(list, remote->name)->util =
1064 strbuf_detach(&url_buf, NULL);
1066 string_list_append(list, remote->name)->util = NULL;
1067 if (remote->pushurl_nr) {
1068 url = remote->pushurl;
1069 url_nr = remote->pushurl_nr;
1072 url_nr = remote->url_nr;
1074 for (i = 0; i < url_nr; i++)
1076 strbuf_addf(&url_buf, "%s (push)", url[i]);
1077 string_list_append(list, remote->name)->util =
1078 strbuf_detach(&url_buf, NULL);
1084 static int show_all(void)
1086 struct string_list list = STRING_LIST_INIT_NODUP;
1089 list.strdup_strings = 1;
1090 result = for_each_remote(get_one_entry, &list);
1095 string_list_sort(&list);
1096 for (i = 0; i < list.nr; i++) {
1097 struct string_list_item *item = list.items + i;
1099 printf("%s\t%s\n", item->string,
1100 item->util ? (const char *)item->util : "");
1102 if (i && !strcmp((item - 1)->string, item->string))
1104 printf("%s\n", item->string);
1108 string_list_clear(&list, 1);
1112 static int show(int argc, const char **argv)
1114 int no_query = 0, result = 0, query_flag = 0;
1115 struct option options[] = {
1116 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1119 struct ref_states states;
1120 struct string_list info_list = STRING_LIST_INIT_NODUP;
1121 struct show_info info;
1123 argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1130 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1132 memset(&states, 0, sizeof(states));
1133 memset(&info, 0, sizeof(info));
1134 info.states = &states;
1135 info.list = &info_list;
1136 for (; argc; argc--, argv++) {
1141 get_remote_ref_states(*argv, &states, query_flag);
1143 printf_ln(_("* remote %s"), *argv);
1144 printf_ln(_(" Fetch URL: %s"), states.remote->url_nr > 0 ?
1145 states.remote->url[0] : _("(no URL)"));
1146 if (states.remote->pushurl_nr) {
1147 url = states.remote->pushurl;
1148 url_nr = states.remote->pushurl_nr;
1150 url = states.remote->url;
1151 url_nr = states.remote->url_nr;
1153 for (i = 0; i < url_nr; i++)
1154 /* TRANSLATORS: the colon ':' should align with
1155 the one in " Fetch URL: %s" translation */
1156 printf_ln(_(" Push URL: %s"), url[i]);
1158 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1160 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1161 else if (!states.heads.nr)
1162 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1163 else if (states.heads.nr == 1)
1164 printf_ln(_(" HEAD branch: %s"), states.heads.items[0].string);
1166 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1167 " may be one of the following):\n"));
1168 for (i = 0; i < states.heads.nr; i++)
1169 printf(" %s\n", states.heads.items[i].string);
1172 /* remote branch info */
1174 for_each_string_list(&states.new, add_remote_to_show_info, &info);
1175 for_each_string_list(&states.tracked, add_remote_to_show_info, &info);
1176 for_each_string_list(&states.stale, add_remote_to_show_info, &info);
1178 printf_ln(Q_(" Remote branch:%s",
1179 " Remote branches:%s",
1181 no_query ? _(" (status not queried)") : "");
1182 for_each_string_list(info.list, show_remote_info_item, &info);
1183 string_list_clear(info.list, 0);
1187 info.any_rebase = 0;
1188 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1190 printf_ln(Q_(" Local branch configured for 'git pull':",
1191 " Local branches configured for 'git pull':",
1193 for_each_string_list(info.list, show_local_info_item, &info);
1194 string_list_clear(info.list, 0);
1197 if (states.remote->mirror)
1198 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1200 info.width = info.width2 = 0;
1201 for_each_string_list(&states.push, add_push_to_show_info, &info);
1202 QSORT(info.list->items, info.list->nr, cmp_string_with_push);
1204 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1205 " Local refs configured for 'git push'%s:",
1207 no_query ? _(" (status not queried)") : "");
1208 for_each_string_list(info.list, show_push_info_item, &info);
1209 string_list_clear(info.list, 0);
1211 free_remote_ref_states(&states);
1217 static int set_head(int argc, const char **argv)
1219 int i, opt_a = 0, opt_d = 0, result = 0;
1220 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1221 char *head_name = NULL;
1223 struct option options[] = {
1224 OPT_BOOL('a', "auto", &opt_a,
1225 N_("set refs/remotes/<name>/HEAD according to remote")),
1226 OPT_BOOL('d', "delete", &opt_d,
1227 N_("delete refs/remotes/<name>/HEAD")),
1230 argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1233 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1235 if (!opt_a && !opt_d && argc == 2) {
1236 head_name = xstrdup(argv[1]);
1237 } else if (opt_a && !opt_d && argc == 1) {
1238 struct ref_states states;
1239 memset(&states, 0, sizeof(states));
1240 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1241 if (!states.heads.nr)
1242 result |= error(_("Cannot determine remote HEAD"));
1243 else if (states.heads.nr > 1) {
1244 result |= error(_("Multiple remote HEAD branches. "
1245 "Please choose one explicitly with:"));
1246 for (i = 0; i < states.heads.nr; i++)
1247 fprintf(stderr, " git remote set-head %s %s\n",
1248 argv[0], states.heads.items[i].string);
1250 head_name = xstrdup(states.heads.items[0].string);
1251 free_remote_ref_states(&states);
1252 } else if (opt_d && !opt_a && argc == 1) {
1253 if (delete_ref(NULL, buf.buf, NULL, REF_NODEREF))
1254 result |= error(_("Could not delete %s"), buf.buf);
1256 usage_with_options(builtin_remote_sethead_usage, options);
1259 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1260 /* make sure it's valid */
1261 if (!ref_exists(buf2.buf))
1262 result |= error(_("Not a valid ref: %s"), buf2.buf);
1263 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1264 result |= error(_("Could not setup %s"), buf.buf);
1266 printf("%s/HEAD set to %s\n", argv[0], head_name);
1270 strbuf_release(&buf);
1271 strbuf_release(&buf2);
1275 static int prune_remote(const char *remote, int dry_run)
1278 struct ref_states states;
1279 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1280 struct string_list_item *item;
1281 const char *dangling_msg = dry_run
1282 ? _(" %s will become dangling!")
1283 : _(" %s has become dangling!");
1285 memset(&states, 0, sizeof(states));
1286 get_remote_ref_states(remote, &states, GET_REF_STATES);
1288 if (!states.stale.nr) {
1289 free_remote_ref_states(&states);
1293 printf_ln(_("Pruning %s"), remote);
1294 printf_ln(_("URL: %s"),
1295 states.remote->url_nr
1296 ? states.remote->url[0]
1299 for_each_string_list_item(item, &states.stale)
1300 string_list_append(&refs_to_prune, item->util);
1301 string_list_sort(&refs_to_prune);
1304 result |= delete_refs(&refs_to_prune, 0);
1306 for_each_string_list_item(item, &states.stale) {
1307 const char *refname = item->util;
1310 printf_ln(_(" * [would prune] %s"),
1311 abbrev_ref(refname, "refs/remotes/"));
1313 printf_ln(_(" * [pruned] %s"),
1314 abbrev_ref(refname, "refs/remotes/"));
1317 warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
1319 string_list_clear(&refs_to_prune, 0);
1320 free_remote_ref_states(&states);
1324 static int prune(int argc, const char **argv)
1326 int dry_run = 0, result = 0;
1327 struct option options[] = {
1328 OPT__DRY_RUN(&dry_run, N_("dry run")),
1332 argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1336 usage_with_options(builtin_remote_prune_usage, options);
1338 for (; argc; argc--, argv++)
1339 result |= prune_remote(*argv, dry_run);
1344 static int get_remote_default(const char *key, const char *value, void *priv)
1346 if (strcmp(key, "remotes.default") == 0) {
1353 static int update(int argc, const char **argv)
1356 struct option options[] = {
1357 OPT_BOOL('p', "prune", &prune,
1358 N_("prune remotes after fetching")),
1361 struct argv_array fetch_argv = ARGV_ARRAY_INIT;
1362 int default_defined = 0;
1365 argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1366 PARSE_OPT_KEEP_ARGV0);
1368 argv_array_push(&fetch_argv, "fetch");
1371 argv_array_push(&fetch_argv, prune ? "--prune" : "--no-prune");
1373 argv_array_push(&fetch_argv, "-v");
1374 argv_array_push(&fetch_argv, "--multiple");
1376 argv_array_push(&fetch_argv, "default");
1377 for (i = 1; i < argc; i++)
1378 argv_array_push(&fetch_argv, argv[i]);
1380 if (strcmp(fetch_argv.argv[fetch_argv.argc-1], "default") == 0) {
1381 git_config(get_remote_default, &default_defined);
1382 if (!default_defined) {
1383 argv_array_pop(&fetch_argv);
1384 argv_array_push(&fetch_argv, "--all");
1388 retval = run_command_v_opt(fetch_argv.argv, RUN_GIT_CMD);
1389 argv_array_clear(&fetch_argv);
1393 static int remove_all_fetch_refspecs(const char *remote, const char *key)
1395 return git_config_set_multivar_gently(key, NULL, NULL, 1);
1398 static void add_branches(struct remote *remote, const char **branches,
1401 const char *remotename = remote->name;
1402 int mirror = remote->mirror;
1403 struct strbuf refspec = STRBUF_INIT;
1405 for (; *branches; branches++)
1406 add_branch(key, *branches, remotename, mirror, &refspec);
1408 strbuf_release(&refspec);
1411 static int set_remote_branches(const char *remotename, const char **branches,
1414 struct strbuf key = STRBUF_INIT;
1415 struct remote *remote;
1417 strbuf_addf(&key, "remote.%s.fetch", remotename);
1419 remote = remote_get(remotename);
1420 if (!remote_is_configured(remote, 1))
1421 die(_("No such remote '%s'"), remotename);
1423 if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) {
1424 strbuf_release(&key);
1427 add_branches(remote, branches, key.buf);
1429 strbuf_release(&key);
1433 static int set_branches(int argc, const char **argv)
1436 struct option options[] = {
1437 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1441 argc = parse_options(argc, argv, NULL, options,
1442 builtin_remote_setbranches_usage, 0);
1444 error(_("no remote specified"));
1445 usage_with_options(builtin_remote_setbranches_usage, options);
1449 return set_remote_branches(argv[0], argv + 1, add_mode);
1452 static int get_url(int argc, const char **argv)
1454 int i, push_mode = 0, all_mode = 0;
1455 const char *remotename = NULL;
1456 struct remote *remote;
1459 struct option options[] = {
1460 OPT_BOOL('\0', "push", &push_mode,
1461 N_("query push URLs rather than fetch URLs")),
1462 OPT_BOOL('\0', "all", &all_mode,
1463 N_("return all URLs")),
1466 argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0);
1469 usage_with_options(builtin_remote_geturl_usage, options);
1471 remotename = argv[0];
1473 remote = remote_get(remotename);
1474 if (!remote_is_configured(remote, 1))
1475 die(_("No such remote '%s'"), remotename);
1479 url = remote->pushurl;
1480 url_nr = remote->pushurl_nr;
1482 /* else fetch mode */
1484 /* Use the fetch URL when no push URLs were found or requested. */
1487 url_nr = remote->url_nr;
1491 die(_("no URLs configured for remote '%s'"), remotename);
1494 for (i = 0; i < url_nr; i++)
1495 printf_ln("%s", url[i]);
1497 printf_ln("%s", *url);
1503 static int set_url(int argc, const char **argv)
1505 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1506 int matches = 0, negative_matches = 0;
1507 const char *remotename = NULL;
1508 const char *newurl = NULL;
1509 const char *oldurl = NULL;
1510 struct remote *remote;
1512 const char **urlset;
1514 struct strbuf name_buf = STRBUF_INIT;
1515 struct option options[] = {
1516 OPT_BOOL('\0', "push", &push_mode,
1517 N_("manipulate push URLs")),
1518 OPT_BOOL('\0', "add", &add_mode,
1520 OPT_BOOL('\0', "delete", &delete_mode,
1524 argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage,
1525 PARSE_OPT_KEEP_ARGV0);
1527 if (add_mode && delete_mode)
1528 die(_("--add --delete doesn't make sense"));
1530 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1531 usage_with_options(builtin_remote_seturl_usage, options);
1533 remotename = argv[1];
1541 remote = remote_get(remotename);
1542 if (!remote_is_configured(remote, 1))
1543 die(_("No such remote '%s'"), remotename);
1546 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1547 urlset = remote->pushurl;
1548 urlset_nr = remote->pushurl_nr;
1550 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1551 urlset = remote->url;
1552 urlset_nr = remote->url_nr;
1555 /* Special cases that add new entry. */
1556 if ((!oldurl && !delete_mode) || add_mode) {
1558 git_config_set_multivar(name_buf.buf, newurl,
1561 git_config_set(name_buf.buf, newurl);
1562 strbuf_release(&name_buf);
1567 /* Old URL specified. Demand that one matches. */
1568 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1569 die(_("Invalid old URL pattern: %s"), oldurl);
1571 for (i = 0; i < urlset_nr; i++)
1572 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1576 if (!delete_mode && !matches)
1577 die(_("No such URL found: %s"), oldurl);
1578 if (delete_mode && !negative_matches && !push_mode)
1579 die(_("Will not delete all non-push URLs"));
1581 regfree(&old_regex);
1584 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1586 git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
1590 int cmd_remote(int argc, const char **argv, const char *prefix)
1592 struct option options[] = {
1593 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1598 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1599 PARSE_OPT_STOP_AT_NON_OPTION);
1602 result = show_all();
1603 else if (!strcmp(argv[0], "add"))
1604 result = add(argc, argv);
1605 else if (!strcmp(argv[0], "rename"))
1606 result = mv(argc, argv);
1607 else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove"))
1608 result = rm(argc, argv);
1609 else if (!strcmp(argv[0], "set-head"))
1610 result = set_head(argc, argv);
1611 else if (!strcmp(argv[0], "set-branches"))
1612 result = set_branches(argc, argv);
1613 else if (!strcmp(argv[0], "get-url"))
1614 result = get_url(argc, argv);
1615 else if (!strcmp(argv[0], "set-url"))
1616 result = set_url(argc, argv);
1617 else if (!strcmp(argv[0], "show"))
1618 result = show(argc, argv);
1619 else if (!strcmp(argv[0], "prune"))
1620 result = prune(argc, argv);
1621 else if (!strcmp(argv[0], "update"))
1622 result = update(argc, argv);
1624 error(_("Unknown subcommand: %s"), argv[0]);
1625 usage_with_options(builtin_remote_usage, options);
1628 return result ? 1 : 0;