3 #include "parse-options.h"
6 #include "string-list.h"
8 #include "run-command.h"
10 #include "argv-array.h"
12 static const char * const builtin_remote_usage[] = {
13 N_("git remote [-v | --verbose]"),
14 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
15 N_("git remote rename <old> <new>"),
16 N_("git remote remove <name>"),
17 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
18 N_("git remote [-v | --verbose] show [-n] <name>"),
19 N_("git remote prune [-n | --dry-run] <name>"),
20 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
21 N_("git remote set-branches [--add] <name> <branch>..."),
22 N_("git remote get-url [--push] [--all] <name>"),
23 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
24 N_("git remote set-url --add <name> <newurl>"),
25 N_("git remote set-url --delete <name> <url>"),
29 static const char * const builtin_remote_add_usage[] = {
30 N_("git remote add [<options>] <name> <url>"),
34 static const char * const builtin_remote_rename_usage[] = {
35 N_("git remote rename <old> <new>"),
39 static const char * const builtin_remote_rm_usage[] = {
40 N_("git remote remove <name>"),
44 static const char * const builtin_remote_sethead_usage[] = {
45 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
49 static const char * const builtin_remote_setbranches_usage[] = {
50 N_("git remote set-branches <name> <branch>..."),
51 N_("git remote set-branches --add <name> <branch>..."),
55 static const char * const builtin_remote_show_usage[] = {
56 N_("git remote show [<options>] <name>"),
60 static const char * const builtin_remote_prune_usage[] = {
61 N_("git remote prune [<options>] <name>"),
65 static const char * const builtin_remote_update_usage[] = {
66 N_("git remote update [<options>] [<group> | <remote>]..."),
70 static const char * const builtin_remote_geturl_usage[] = {
71 N_("git remote get-url [--push] [--all] <name>"),
75 static const char * const builtin_remote_seturl_usage[] = {
76 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
77 N_("git remote set-url --add <name> <newurl>"),
78 N_("git remote set-url --delete <name> <url>"),
82 #define GET_REF_STATES (1<<0)
83 #define GET_HEAD_NAMES (1<<1)
84 #define GET_PUSH_REF_STATES (1<<2)
88 static int fetch_remote(const char *name)
90 const char *argv[] = { "fetch", name, NULL, NULL };
95 printf_ln(_("Updating %s"), name);
96 if (run_command_v_opt(argv, RUN_GIT_CMD))
97 return error(_("Could not fetch %s"), name);
107 #define MIRROR_NONE 0
108 #define MIRROR_FETCH 1
109 #define MIRROR_PUSH 2
110 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
112 static void add_branch(const char *key, const char *branchname,
113 const char *remotename, int mirror, struct strbuf *tmp)
116 strbuf_addch(tmp, '+');
118 strbuf_addf(tmp, "refs/%s:refs/%s",
119 branchname, branchname);
121 strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
122 branchname, remotename, branchname);
123 git_config_set_multivar(key, tmp->buf, "^$", 0);
126 static const char mirror_advice[] =
127 N_("--mirror is dangerous and deprecated; please\n"
128 "\t use --mirror=fetch or --mirror=push instead");
130 static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
132 unsigned *mirror = opt->value;
134 *mirror = MIRROR_NONE;
136 warning("%s", _(mirror_advice));
137 *mirror = MIRROR_BOTH;
139 else if (!strcmp(arg, "fetch"))
140 *mirror = MIRROR_FETCH;
141 else if (!strcmp(arg, "push"))
142 *mirror = MIRROR_PUSH;
144 return error(_("unknown mirror argument: %s"), arg);
148 static int add(int argc, const char **argv)
150 int fetch = 0, fetch_tags = TAGS_DEFAULT;
151 unsigned mirror = MIRROR_NONE;
152 struct string_list track = STRING_LIST_INIT_NODUP;
153 const char *master = NULL;
154 struct remote *remote;
155 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
156 const char *name, *url;
159 struct option options[] = {
160 OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
161 OPT_SET_INT(0, "tags", &fetch_tags,
162 N_("import all tags and associated objects when fetching"),
164 OPT_SET_INT(0, NULL, &fetch_tags,
165 N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET),
166 OPT_STRING_LIST('t', "track", &track, N_("branch"),
167 N_("branch(es) to track")),
168 OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")),
169 { OPTION_CALLBACK, 0, "mirror", &mirror, N_("push|fetch"),
170 N_("set up remote as a mirror to push to or fetch from"),
171 PARSE_OPT_OPTARG, parse_mirror_opt },
175 argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage,
179 usage_with_options(builtin_remote_add_usage, options);
181 if (mirror && master)
182 die(_("specifying a master branch makes no sense with --mirror"));
183 if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
184 die(_("specifying branches to track makes sense only with fetch mirrors"));
189 remote = remote_get(name);
190 if (remote_is_configured(remote, 1))
191 die(_("remote %s already exists."), name);
193 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
194 if (!valid_fetch_refspec(buf2.buf))
195 die(_("'%s' is not a valid remote name"), name);
197 strbuf_addf(&buf, "remote.%s.url", name);
198 git_config_set(buf.buf, url);
200 if (!mirror || mirror & MIRROR_FETCH) {
202 strbuf_addf(&buf, "remote.%s.fetch", name);
204 string_list_append(&track, "*");
205 for (i = 0; i < track.nr; i++) {
206 add_branch(buf.buf, track.items[i].string,
207 name, mirror, &buf2);
211 if (mirror & MIRROR_PUSH) {
213 strbuf_addf(&buf, "remote.%s.mirror", name);
214 git_config_set(buf.buf, "true");
217 if (fetch_tags != TAGS_DEFAULT) {
219 strbuf_addf(&buf, "remote.%s.tagopt", name);
220 git_config_set(buf.buf,
221 fetch_tags == TAGS_SET ? "--tags" : "--no-tags");
224 if (fetch && fetch_remote(name))
229 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
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);
247 struct string_list merge;
248 enum { NO_REBASE, NORMAL_REBASE, INTERACTIVE_REBASE } rebase;
251 static struct string_list branch_list = STRING_LIST_INIT_NODUP;
253 static const char *abbrev_ref(const char *name, const char *prefix)
255 skip_prefix(name, prefix, &name);
258 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
260 static int config_read_branches(const char *key, const char *value, void *cb)
262 if (starts_with(key, "branch.")) {
263 const char *orig_key = key;
265 struct string_list_item *item;
266 struct branch_info *info;
267 enum { REMOTE, MERGE, REBASE } type;
271 if (strip_suffix(key, ".remote", &key_len)) {
272 name = xmemdupz(key, key_len);
274 } else if (strip_suffix(key, ".merge", &key_len)) {
275 name = xmemdupz(key, key_len);
277 } else if (strip_suffix(key, ".rebase", &key_len)) {
278 name = xmemdupz(key, key_len);
283 item = string_list_insert(&branch_list, name);
286 item->util = xcalloc(1, sizeof(struct branch_info));
288 if (type == REMOTE) {
289 if (info->remote_name)
290 warning(_("more than one %s"), orig_key);
291 info->remote_name = xstrdup(value);
292 } else if (type == MERGE) {
293 char *space = strchr(value, ' ');
294 value = abbrev_branch(value);
297 merge = xstrndup(value, space - value);
298 string_list_append(&info->merge, merge);
299 value = abbrev_branch(space + 1);
300 space = strchr(value, ' ');
302 string_list_append(&info->merge, xstrdup(value));
304 int v = git_parse_maybe_bool(value);
307 else if (!strcmp(value, "preserve"))
308 info->rebase = NORMAL_REBASE;
309 else if (!strcmp(value, "interactive"))
310 info->rebase = INTERACTIVE_REBASE;
316 static void read_branches(void)
320 git_config(config_read_branches, NULL);
324 struct remote *remote;
325 struct string_list new, stale, tracked, heads, push;
329 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
331 struct ref *fetch_map = NULL, **tail = &fetch_map;
332 struct ref *ref, *stale_refs;
335 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
336 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
337 die(_("Could not get fetch map for refspec %s"),
338 states->remote->fetch_refspec[i]);
340 states->new.strdup_strings = 1;
341 states->tracked.strdup_strings = 1;
342 states->stale.strdup_strings = 1;
343 for (ref = fetch_map; ref; ref = ref->next) {
344 if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
345 string_list_append(&states->new, abbrev_branch(ref->name));
347 string_list_append(&states->tracked, abbrev_branch(ref->name));
349 stale_refs = get_stale_heads(states->remote->fetch,
350 states->remote->fetch_refspec_nr, fetch_map);
351 for (ref = stale_refs; ref; ref = ref->next) {
352 struct string_list_item *item =
353 string_list_append(&states->stale, abbrev_branch(ref->name));
354 item->util = xstrdup(ref->name);
356 free_refs(stale_refs);
357 free_refs(fetch_map);
359 string_list_sort(&states->new);
360 string_list_sort(&states->tracked);
361 string_list_sort(&states->stale);
370 PUSH_STATUS_CREATE = 0,
372 PUSH_STATUS_UPTODATE,
373 PUSH_STATUS_FASTFORWARD,
374 PUSH_STATUS_OUTOFDATE,
375 PUSH_STATUS_NOTQUERIED
379 static int get_push_ref_states(const struct ref *remote_refs,
380 struct ref_states *states)
382 struct remote *remote = states->remote;
383 struct ref *ref, *local_refs, *push_map;
387 local_refs = get_local_heads();
388 push_map = copy_ref_list(remote_refs);
390 match_push_refs(local_refs, &push_map, remote->push_refspec_nr,
391 remote->push_refspec, MATCH_REFS_NONE);
393 states->push.strdup_strings = 1;
394 for (ref = push_map; ref; ref = ref->next) {
395 struct string_list_item *item;
396 struct push_info *info;
400 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
402 item = string_list_append(&states->push,
403 abbrev_branch(ref->peer_ref->name));
404 item->util = xcalloc(1, sizeof(struct push_info));
406 info->forced = ref->force;
407 info->dest = xstrdup(abbrev_branch(ref->name));
409 if (is_null_oid(&ref->new_oid)) {
410 info->status = PUSH_STATUS_DELETE;
411 } else if (!oidcmp(&ref->old_oid, &ref->new_oid))
412 info->status = PUSH_STATUS_UPTODATE;
413 else if (is_null_oid(&ref->old_oid))
414 info->status = PUSH_STATUS_CREATE;
415 else if (has_object_file(&ref->old_oid) &&
416 ref_newer(&ref->new_oid, &ref->old_oid))
417 info->status = PUSH_STATUS_FASTFORWARD;
419 info->status = PUSH_STATUS_OUTOFDATE;
421 free_refs(local_refs);
426 static int get_push_ref_states_noquery(struct ref_states *states)
429 struct remote *remote = states->remote;
430 struct string_list_item *item;
431 struct push_info *info;
436 states->push.strdup_strings = 1;
437 if (!remote->push_refspec_nr) {
438 item = string_list_append(&states->push, _("(matching)"));
439 info = item->util = xcalloc(1, sizeof(struct push_info));
440 info->status = PUSH_STATUS_NOTQUERIED;
441 info->dest = xstrdup(item->string);
443 for (i = 0; i < remote->push_refspec_nr; i++) {
444 struct refspec *spec = remote->push + i;
446 item = string_list_append(&states->push, _("(matching)"));
447 else if (strlen(spec->src))
448 item = string_list_append(&states->push, spec->src);
450 item = string_list_append(&states->push, _("(delete)"));
452 info = item->util = xcalloc(1, sizeof(struct push_info));
453 info->forced = spec->force;
454 info->status = PUSH_STATUS_NOTQUERIED;
455 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
460 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
462 struct ref *ref, *matches;
463 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
464 struct refspec refspec;
468 refspec.src = refspec.dst = "refs/heads/*";
469 states->heads.strdup_strings = 1;
470 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
471 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
473 for (ref = matches; ref; ref = ref->next)
474 string_list_append(&states->heads, abbrev_branch(ref->name));
476 free_refs(fetch_map);
482 struct known_remote {
483 struct known_remote *next;
484 struct remote *remote;
487 struct known_remotes {
488 struct remote *to_delete;
489 struct known_remote *list;
492 static int add_known_remote(struct remote *remote, void *cb_data)
494 struct known_remotes *all = cb_data;
495 struct known_remote *r;
497 if (!strcmp(all->to_delete->name, remote->name))
500 r = xmalloc(sizeof(*r));
507 struct branches_for_remote {
508 struct remote *remote;
509 struct string_list *branches, *skipped;
510 struct known_remotes *keep;
513 static int add_branch_for_removal(const char *refname,
514 const struct object_id *oid, int flags, void *cb_data)
516 struct branches_for_remote *branches = cb_data;
517 struct refspec refspec;
518 struct known_remote *kr;
520 memset(&refspec, 0, sizeof(refspec));
521 refspec.dst = (char *)refname;
522 if (remote_find_tracking(branches->remote, &refspec))
525 /* don't delete a branch if another remote also uses it */
526 for (kr = branches->keep->list; kr; kr = kr->next) {
527 memset(&refspec, 0, sizeof(refspec));
528 refspec.dst = (char *)refname;
529 if (!remote_find_tracking(kr->remote, &refspec))
533 /* don't delete non-remote-tracking refs */
534 if (!starts_with(refname, "refs/remotes/")) {
535 /* advise user how to delete local branches */
536 if (starts_with(refname, "refs/heads/"))
537 string_list_append(branches->skipped,
538 abbrev_branch(refname));
539 /* silently skip over other non-remote refs */
543 string_list_append(branches->branches, refname);
551 struct string_list *remote_branches;
554 static int read_remote_branches(const char *refname,
555 const struct object_id *oid, int flags, void *cb_data)
557 struct rename_info *rename = cb_data;
558 struct strbuf buf = STRBUF_INIT;
559 struct string_list_item *item;
561 struct object_id orig_oid;
564 strbuf_addf(&buf, "refs/remotes/%s/", rename->old);
565 if (starts_with(refname, buf.buf)) {
566 item = string_list_append(rename->remote_branches, xstrdup(refname));
567 symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
568 orig_oid.hash, &flag);
569 if (flag & REF_ISSYMREF)
570 item->util = xstrdup(symref);
574 strbuf_release(&buf);
579 static int migrate_file(struct remote *remote)
581 struct strbuf buf = STRBUF_INIT;
584 strbuf_addf(&buf, "remote.%s.url", remote->name);
585 for (i = 0; i < remote->url_nr; i++)
586 git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
588 strbuf_addf(&buf, "remote.%s.push", remote->name);
589 for (i = 0; i < remote->push_refspec_nr; i++)
590 git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0);
592 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
593 for (i = 0; i < remote->fetch_refspec_nr; i++)
594 git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0);
595 if (remote->origin == REMOTE_REMOTES)
596 unlink_or_warn(git_path("remotes/%s", remote->name));
597 else if (remote->origin == REMOTE_BRANCHES)
598 unlink_or_warn(git_path("branches/%s", remote->name));
599 strbuf_release(&buf);
604 static int mv(int argc, const char **argv)
606 struct option options[] = {
609 struct remote *oldremote, *newremote;
610 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
611 old_remote_context = STRBUF_INIT;
612 struct string_list remote_branches = STRING_LIST_INIT_NODUP;
613 struct rename_info rename;
614 int i, refspec_updated = 0;
617 usage_with_options(builtin_remote_rename_usage, options);
619 rename.old = argv[1];
620 rename.new = argv[2];
621 rename.remote_branches = &remote_branches;
623 oldremote = remote_get(rename.old);
624 if (!remote_is_configured(oldremote, 1))
625 die(_("No such remote: %s"), rename.old);
627 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
628 return migrate_file(oldremote);
630 newremote = remote_get(rename.new);
631 if (remote_is_configured(newremote, 1))
632 die(_("remote %s already exists."), rename.new);
634 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
635 if (!valid_fetch_refspec(buf.buf))
636 die(_("'%s' is not a valid remote name"), rename.new);
639 strbuf_addf(&buf, "remote.%s", rename.old);
640 strbuf_addf(&buf2, "remote.%s", rename.new);
641 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
642 return error(_("Could not rename config section '%s' to '%s'"),
646 strbuf_addf(&buf, "remote.%s.fetch", rename.new);
647 git_config_set_multivar(buf.buf, NULL, NULL, 1);
648 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old);
649 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
653 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
654 ptr = strstr(buf2.buf, old_remote_context.buf);
658 ptr-buf2.buf + strlen(":refs/remotes/"),
659 strlen(rename.old), rename.new,
662 warning(_("Not updating non-default fetch refspec\n"
664 "\tPlease update the configuration manually if necessary."),
667 git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
671 for (i = 0; i < branch_list.nr; i++) {
672 struct string_list_item *item = branch_list.items + i;
673 struct branch_info *info = item->util;
674 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
676 strbuf_addf(&buf, "branch.%s.remote", item->string);
677 git_config_set(buf.buf, rename.new);
681 if (!refspec_updated)
685 * First remove symrefs, then rename the rest, finally create
688 for_each_ref(read_remote_branches, &rename);
689 for (i = 0; i < remote_branches.nr; i++) {
690 struct string_list_item *item = remote_branches.items + i;
692 struct object_id oid;
694 read_ref_full(item->string, RESOLVE_REF_READING, oid.hash, &flag);
695 if (!(flag & REF_ISSYMREF))
697 if (delete_ref(NULL, item->string, NULL, REF_NODEREF))
698 die(_("deleting '%s' failed"), item->string);
700 for (i = 0; i < remote_branches.nr; i++) {
701 struct string_list_item *item = remote_branches.items + i;
706 strbuf_addstr(&buf, item->string);
707 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
708 rename.new, strlen(rename.new));
710 strbuf_addf(&buf2, "remote: renamed %s to %s",
711 item->string, buf.buf);
712 if (rename_ref(item->string, buf.buf, buf2.buf))
713 die(_("renaming '%s' failed"), item->string);
715 for (i = 0; i < remote_branches.nr; i++) {
716 struct string_list_item *item = remote_branches.items + i;
721 strbuf_addstr(&buf, item->string);
722 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
723 rename.new, strlen(rename.new));
725 strbuf_addstr(&buf2, item->util);
726 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
727 rename.new, strlen(rename.new));
729 strbuf_addf(&buf3, "remote: renamed %s to %s",
730 item->string, buf.buf);
731 if (create_symref(buf.buf, buf2.buf, buf3.buf))
732 die(_("creating '%s' failed"), buf.buf);
737 static int rm(int argc, const char **argv)
739 struct option options[] = {
742 struct remote *remote;
743 struct strbuf buf = STRBUF_INIT;
744 struct known_remotes known_remotes = { NULL, NULL };
745 struct string_list branches = STRING_LIST_INIT_DUP;
746 struct string_list skipped = STRING_LIST_INIT_DUP;
747 struct branches_for_remote cb_data;
750 memset(&cb_data, 0, sizeof(cb_data));
751 cb_data.branches = &branches;
752 cb_data.skipped = &skipped;
753 cb_data.keep = &known_remotes;
756 usage_with_options(builtin_remote_rm_usage, options);
758 remote = remote_get(argv[1]);
759 if (!remote_is_configured(remote, 1))
760 die(_("No such remote: %s"), argv[1]);
762 known_remotes.to_delete = remote;
763 for_each_remote(add_known_remote, &known_remotes);
766 for (i = 0; i < branch_list.nr; i++) {
767 struct string_list_item *item = branch_list.items + i;
768 struct branch_info *info = item->util;
769 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
770 const char *keys[] = { "remote", "merge", NULL }, **k;
771 for (k = keys; *k; k++) {
773 strbuf_addf(&buf, "branch.%s.%s",
775 result = git_config_set_gently(buf.buf, NULL);
776 if (result && result != CONFIG_NOTHING_SET)
777 die(_("could not unset '%s'"), buf.buf);
783 * We cannot just pass a function to for_each_ref() which deletes
784 * the branches one by one, since for_each_ref() relies on cached
785 * refs, which are invalidated when deleting a branch.
787 cb_data.remote = remote;
788 result = for_each_ref(add_branch_for_removal, &cb_data);
789 strbuf_release(&buf);
792 result = delete_refs("remote: remove", &branches, REF_NODEREF);
793 string_list_clear(&branches, 0);
797 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
798 "to delete it, use:",
799 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
800 "to delete them, use:",
802 for (i = 0; i < skipped.nr; i++)
803 fprintf(stderr, " git branch -d %s\n",
804 skipped.items[i].string);
806 string_list_clear(&skipped, 0);
809 strbuf_addf(&buf, "remote.%s", remote->name);
810 if (git_config_rename_section(buf.buf, NULL) < 1)
811 return error(_("Could not remove config section '%s'"), buf.buf);
817 static void clear_push_info(void *util, const char *string)
819 struct push_info *info = util;
824 static void free_remote_ref_states(struct ref_states *states)
826 string_list_clear(&states->new, 0);
827 string_list_clear(&states->stale, 1);
828 string_list_clear(&states->tracked, 0);
829 string_list_clear(&states->heads, 0);
830 string_list_clear_func(&states->push, clear_push_info);
833 static int append_ref_to_tracked_list(const char *refname,
834 const struct object_id *oid, int flags, void *cb_data)
836 struct ref_states *states = cb_data;
837 struct refspec refspec;
839 if (flags & REF_ISSYMREF)
842 memset(&refspec, 0, sizeof(refspec));
843 refspec.dst = (char *)refname;
844 if (!remote_find_tracking(states->remote, &refspec))
845 string_list_append(&states->tracked, abbrev_branch(refspec.src));
850 static int get_remote_ref_states(const char *name,
851 struct ref_states *states,
854 struct transport *transport;
855 const struct ref *remote_refs;
857 states->remote = remote_get(name);
859 return error(_("No such remote: %s"), name);
864 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
865 states->remote->url[0] : NULL);
866 remote_refs = transport_get_remote_refs(transport);
867 transport_disconnect(transport);
870 if (query & GET_REF_STATES)
871 get_ref_states(remote_refs, states);
872 if (query & GET_HEAD_NAMES)
873 get_head_names(remote_refs, states);
874 if (query & GET_PUSH_REF_STATES)
875 get_push_ref_states(remote_refs, states);
877 for_each_ref(append_ref_to_tracked_list, states);
878 string_list_sort(&states->tracked);
879 get_push_ref_states_noquery(states);
886 struct string_list *list;
887 struct ref_states *states;
892 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
894 struct show_info *info = cb_data;
895 int n = strlen(item->string);
898 string_list_insert(info->list, item->string);
902 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
904 struct show_info *info = cb_data;
905 struct ref_states *states = info->states;
906 const char *name = item->string;
908 if (states->queried) {
909 const char *fmt = "%s";
910 const char *arg = "";
911 if (string_list_has_string(&states->new, name)) {
912 fmt = _(" new (next fetch will store in remotes/%s)");
913 arg = states->remote->name;
914 } else if (string_list_has_string(&states->tracked, name))
916 else if (string_list_has_string(&states->stale, name))
917 arg = _(" stale (use 'git remote prune' to remove)");
920 printf(" %-*s", info->width, name);
924 printf(" %s\n", name);
929 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
931 struct show_info *show_info = cb_data;
932 struct ref_states *states = show_info->states;
933 struct branch_info *branch_info = branch_item->util;
934 struct string_list_item *item;
937 if (!branch_info->merge.nr || !branch_info->remote_name ||
938 strcmp(states->remote->name, branch_info->remote_name))
940 if ((n = strlen(branch_item->string)) > show_info->width)
941 show_info->width = n;
942 if (branch_info->rebase)
943 show_info->any_rebase = 1;
945 item = string_list_insert(show_info->list, branch_item->string);
946 item->util = branch_info;
951 static int show_local_info_item(struct string_list_item *item, void *cb_data)
953 struct show_info *show_info = cb_data;
954 struct branch_info *branch_info = item->util;
955 struct string_list *merge = &branch_info->merge;
956 int width = show_info->width + 4;
959 if (branch_info->rebase && branch_info->merge.nr > 1) {
960 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
965 printf(" %-*s ", show_info->width, item->string);
966 if (branch_info->rebase) {
967 printf_ln(branch_info->rebase == INTERACTIVE_REBASE
968 ? _("rebases interactively onto remote %s")
969 : _("rebases onto remote %s"), merge->items[0].string);
971 } else if (show_info->any_rebase) {
972 printf_ln(_(" merges with remote %s"), merge->items[0].string);
975 printf_ln(_("merges with remote %s"), merge->items[0].string);
977 for (i = 1; i < merge->nr; i++)
978 printf(_("%-*s and with remote %s\n"), width, "",
979 merge->items[i].string);
984 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
986 struct show_info *show_info = cb_data;
987 struct push_info *push_info = push_item->util;
988 struct string_list_item *item;
990 if ((n = strlen(push_item->string)) > show_info->width)
991 show_info->width = n;
992 if ((n = strlen(push_info->dest)) > show_info->width2)
993 show_info->width2 = n;
994 item = string_list_append(show_info->list, push_item->string);
995 item->util = push_item->util;
1000 * Sorting comparison for a string list that has push_info
1001 * structs in its util field
1003 static int cmp_string_with_push(const void *va, const void *vb)
1005 const struct string_list_item *a = va;
1006 const struct string_list_item *b = vb;
1007 const struct push_info *a_push = a->util;
1008 const struct push_info *b_push = b->util;
1009 int cmp = strcmp(a->string, b->string);
1010 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1013 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1015 struct show_info *show_info = cb_data;
1016 struct push_info *push_info = item->util;
1017 const char *src = item->string, *status = NULL;
1019 switch (push_info->status) {
1020 case PUSH_STATUS_CREATE:
1021 status = _("create");
1023 case PUSH_STATUS_DELETE:
1024 status = _("delete");
1027 case PUSH_STATUS_UPTODATE:
1028 status = _("up to date");
1030 case PUSH_STATUS_FASTFORWARD:
1031 status = _("fast-forwardable");
1033 case PUSH_STATUS_OUTOFDATE:
1034 status = _("local out of date");
1036 case PUSH_STATUS_NOTQUERIED:
1040 if (push_info->forced)
1041 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1042 show_info->width2, push_info->dest, status);
1044 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1045 show_info->width2, push_info->dest, status);
1047 if (push_info->forced)
1048 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1051 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1057 static int get_one_entry(struct remote *remote, void *priv)
1059 struct string_list *list = priv;
1060 struct strbuf url_buf = STRBUF_INIT;
1064 if (remote->url_nr > 0) {
1065 strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
1066 string_list_append(list, remote->name)->util =
1067 strbuf_detach(&url_buf, NULL);
1069 string_list_append(list, remote->name)->util = NULL;
1070 if (remote->pushurl_nr) {
1071 url = remote->pushurl;
1072 url_nr = remote->pushurl_nr;
1075 url_nr = remote->url_nr;
1077 for (i = 0; i < url_nr; i++)
1079 strbuf_addf(&url_buf, "%s (push)", url[i]);
1080 string_list_append(list, remote->name)->util =
1081 strbuf_detach(&url_buf, NULL);
1087 static int show_all(void)
1089 struct string_list list = STRING_LIST_INIT_NODUP;
1092 list.strdup_strings = 1;
1093 result = for_each_remote(get_one_entry, &list);
1098 string_list_sort(&list);
1099 for (i = 0; i < list.nr; i++) {
1100 struct string_list_item *item = list.items + i;
1102 printf("%s\t%s\n", item->string,
1103 item->util ? (const char *)item->util : "");
1105 if (i && !strcmp((item - 1)->string, item->string))
1107 printf("%s\n", item->string);
1111 string_list_clear(&list, 1);
1115 static int show(int argc, const char **argv)
1117 int no_query = 0, result = 0, query_flag = 0;
1118 struct option options[] = {
1119 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1122 struct ref_states states;
1123 struct string_list info_list = STRING_LIST_INIT_NODUP;
1124 struct show_info info;
1126 argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1133 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1135 memset(&states, 0, sizeof(states));
1136 memset(&info, 0, sizeof(info));
1137 info.states = &states;
1138 info.list = &info_list;
1139 for (; argc; argc--, argv++) {
1144 get_remote_ref_states(*argv, &states, query_flag);
1146 printf_ln(_("* remote %s"), *argv);
1147 printf_ln(_(" Fetch URL: %s"), states.remote->url_nr > 0 ?
1148 states.remote->url[0] : _("(no URL)"));
1149 if (states.remote->pushurl_nr) {
1150 url = states.remote->pushurl;
1151 url_nr = states.remote->pushurl_nr;
1153 url = states.remote->url;
1154 url_nr = states.remote->url_nr;
1156 for (i = 0; i < url_nr; i++)
1158 * TRANSLATORS: the colon ':' should align
1159 * with the one in " Fetch URL: %s"
1162 printf_ln(_(" Push URL: %s"), url[i]);
1164 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1166 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1167 else if (!states.heads.nr)
1168 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1169 else if (states.heads.nr == 1)
1170 printf_ln(_(" HEAD branch: %s"), states.heads.items[0].string);
1172 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1173 " may be one of the following):\n"));
1174 for (i = 0; i < states.heads.nr; i++)
1175 printf(" %s\n", states.heads.items[i].string);
1178 /* remote branch info */
1180 for_each_string_list(&states.new, add_remote_to_show_info, &info);
1181 for_each_string_list(&states.tracked, add_remote_to_show_info, &info);
1182 for_each_string_list(&states.stale, add_remote_to_show_info, &info);
1184 printf_ln(Q_(" Remote branch:%s",
1185 " Remote branches:%s",
1187 no_query ? _(" (status not queried)") : "");
1188 for_each_string_list(info.list, show_remote_info_item, &info);
1189 string_list_clear(info.list, 0);
1193 info.any_rebase = 0;
1194 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1196 printf_ln(Q_(" Local branch configured for 'git pull':",
1197 " Local branches configured for 'git pull':",
1199 for_each_string_list(info.list, show_local_info_item, &info);
1200 string_list_clear(info.list, 0);
1203 if (states.remote->mirror)
1204 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1206 info.width = info.width2 = 0;
1207 for_each_string_list(&states.push, add_push_to_show_info, &info);
1208 QSORT(info.list->items, info.list->nr, cmp_string_with_push);
1210 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1211 " Local refs configured for 'git push'%s:",
1213 no_query ? _(" (status not queried)") : "");
1214 for_each_string_list(info.list, show_push_info_item, &info);
1215 string_list_clear(info.list, 0);
1217 free_remote_ref_states(&states);
1223 static int set_head(int argc, const char **argv)
1225 int i, opt_a = 0, opt_d = 0, result = 0;
1226 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1227 char *head_name = NULL;
1229 struct option options[] = {
1230 OPT_BOOL('a', "auto", &opt_a,
1231 N_("set refs/remotes/<name>/HEAD according to remote")),
1232 OPT_BOOL('d', "delete", &opt_d,
1233 N_("delete refs/remotes/<name>/HEAD")),
1236 argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1239 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1241 if (!opt_a && !opt_d && argc == 2) {
1242 head_name = xstrdup(argv[1]);
1243 } else if (opt_a && !opt_d && argc == 1) {
1244 struct ref_states states;
1245 memset(&states, 0, sizeof(states));
1246 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1247 if (!states.heads.nr)
1248 result |= error(_("Cannot determine remote HEAD"));
1249 else if (states.heads.nr > 1) {
1250 result |= error(_("Multiple remote HEAD branches. "
1251 "Please choose one explicitly with:"));
1252 for (i = 0; i < states.heads.nr; i++)
1253 fprintf(stderr, " git remote set-head %s %s\n",
1254 argv[0], states.heads.items[i].string);
1256 head_name = xstrdup(states.heads.items[0].string);
1257 free_remote_ref_states(&states);
1258 } else if (opt_d && !opt_a && argc == 1) {
1259 if (delete_ref(NULL, buf.buf, NULL, REF_NODEREF))
1260 result |= error(_("Could not delete %s"), buf.buf);
1262 usage_with_options(builtin_remote_sethead_usage, options);
1265 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1266 /* make sure it's valid */
1267 if (!ref_exists(buf2.buf))
1268 result |= error(_("Not a valid ref: %s"), buf2.buf);
1269 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1270 result |= error(_("Could not setup %s"), buf.buf);
1272 printf("%s/HEAD set to %s\n", argv[0], head_name);
1276 strbuf_release(&buf);
1277 strbuf_release(&buf2);
1281 static int prune_remote(const char *remote, int dry_run)
1284 struct ref_states states;
1285 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1286 struct string_list_item *item;
1287 const char *dangling_msg = dry_run
1288 ? _(" %s will become dangling!")
1289 : _(" %s has become dangling!");
1291 memset(&states, 0, sizeof(states));
1292 get_remote_ref_states(remote, &states, GET_REF_STATES);
1294 if (!states.stale.nr) {
1295 free_remote_ref_states(&states);
1299 printf_ln(_("Pruning %s"), remote);
1300 printf_ln(_("URL: %s"),
1301 states.remote->url_nr
1302 ? states.remote->url[0]
1305 for_each_string_list_item(item, &states.stale)
1306 string_list_append(&refs_to_prune, item->util);
1307 string_list_sort(&refs_to_prune);
1310 result |= delete_refs("remote: prune", &refs_to_prune, 0);
1312 for_each_string_list_item(item, &states.stale) {
1313 const char *refname = item->util;
1316 printf_ln(_(" * [would prune] %s"),
1317 abbrev_ref(refname, "refs/remotes/"));
1319 printf_ln(_(" * [pruned] %s"),
1320 abbrev_ref(refname, "refs/remotes/"));
1323 warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
1325 string_list_clear(&refs_to_prune, 0);
1326 free_remote_ref_states(&states);
1330 static int prune(int argc, const char **argv)
1332 int dry_run = 0, result = 0;
1333 struct option options[] = {
1334 OPT__DRY_RUN(&dry_run, N_("dry run")),
1338 argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1342 usage_with_options(builtin_remote_prune_usage, options);
1344 for (; argc; argc--, argv++)
1345 result |= prune_remote(*argv, dry_run);
1350 static int get_remote_default(const char *key, const char *value, void *priv)
1352 if (strcmp(key, "remotes.default") == 0) {
1359 static int update(int argc, const char **argv)
1362 struct option options[] = {
1363 OPT_BOOL('p', "prune", &prune,
1364 N_("prune remotes after fetching")),
1367 struct argv_array fetch_argv = ARGV_ARRAY_INIT;
1368 int default_defined = 0;
1371 argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1372 PARSE_OPT_KEEP_ARGV0);
1374 argv_array_push(&fetch_argv, "fetch");
1377 argv_array_push(&fetch_argv, prune ? "--prune" : "--no-prune");
1379 argv_array_push(&fetch_argv, "-v");
1380 argv_array_push(&fetch_argv, "--multiple");
1382 argv_array_push(&fetch_argv, "default");
1383 for (i = 1; i < argc; i++)
1384 argv_array_push(&fetch_argv, argv[i]);
1386 if (strcmp(fetch_argv.argv[fetch_argv.argc-1], "default") == 0) {
1387 git_config(get_remote_default, &default_defined);
1388 if (!default_defined) {
1389 argv_array_pop(&fetch_argv);
1390 argv_array_push(&fetch_argv, "--all");
1394 retval = run_command_v_opt(fetch_argv.argv, RUN_GIT_CMD);
1395 argv_array_clear(&fetch_argv);
1399 static int remove_all_fetch_refspecs(const char *remote, const char *key)
1401 return git_config_set_multivar_gently(key, NULL, NULL, 1);
1404 static void add_branches(struct remote *remote, const char **branches,
1407 const char *remotename = remote->name;
1408 int mirror = remote->mirror;
1409 struct strbuf refspec = STRBUF_INIT;
1411 for (; *branches; branches++)
1412 add_branch(key, *branches, remotename, mirror, &refspec);
1414 strbuf_release(&refspec);
1417 static int set_remote_branches(const char *remotename, const char **branches,
1420 struct strbuf key = STRBUF_INIT;
1421 struct remote *remote;
1423 strbuf_addf(&key, "remote.%s.fetch", remotename);
1425 remote = remote_get(remotename);
1426 if (!remote_is_configured(remote, 1))
1427 die(_("No such remote '%s'"), remotename);
1429 if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) {
1430 strbuf_release(&key);
1433 add_branches(remote, branches, key.buf);
1435 strbuf_release(&key);
1439 static int set_branches(int argc, const char **argv)
1442 struct option options[] = {
1443 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1447 argc = parse_options(argc, argv, NULL, options,
1448 builtin_remote_setbranches_usage, 0);
1450 error(_("no remote specified"));
1451 usage_with_options(builtin_remote_setbranches_usage, options);
1455 return set_remote_branches(argv[0], argv + 1, add_mode);
1458 static int get_url(int argc, const char **argv)
1460 int i, push_mode = 0, all_mode = 0;
1461 const char *remotename = NULL;
1462 struct remote *remote;
1465 struct option options[] = {
1466 OPT_BOOL('\0', "push", &push_mode,
1467 N_("query push URLs rather than fetch URLs")),
1468 OPT_BOOL('\0', "all", &all_mode,
1469 N_("return all URLs")),
1472 argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0);
1475 usage_with_options(builtin_remote_geturl_usage, options);
1477 remotename = argv[0];
1479 remote = remote_get(remotename);
1480 if (!remote_is_configured(remote, 1))
1481 die(_("No such remote '%s'"), remotename);
1485 url = remote->pushurl;
1486 url_nr = remote->pushurl_nr;
1488 /* else fetch mode */
1490 /* Use the fetch URL when no push URLs were found or requested. */
1493 url_nr = remote->url_nr;
1497 die(_("no URLs configured for remote '%s'"), remotename);
1500 for (i = 0; i < url_nr; i++)
1501 printf_ln("%s", url[i]);
1503 printf_ln("%s", *url);
1509 static int set_url(int argc, const char **argv)
1511 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1512 int matches = 0, negative_matches = 0;
1513 const char *remotename = NULL;
1514 const char *newurl = NULL;
1515 const char *oldurl = NULL;
1516 struct remote *remote;
1518 const char **urlset;
1520 struct strbuf name_buf = STRBUF_INIT;
1521 struct option options[] = {
1522 OPT_BOOL('\0', "push", &push_mode,
1523 N_("manipulate push URLs")),
1524 OPT_BOOL('\0', "add", &add_mode,
1526 OPT_BOOL('\0', "delete", &delete_mode,
1530 argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage,
1531 PARSE_OPT_KEEP_ARGV0);
1533 if (add_mode && delete_mode)
1534 die(_("--add --delete doesn't make sense"));
1536 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1537 usage_with_options(builtin_remote_seturl_usage, options);
1539 remotename = argv[1];
1547 remote = remote_get(remotename);
1548 if (!remote_is_configured(remote, 1))
1549 die(_("No such remote '%s'"), remotename);
1552 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1553 urlset = remote->pushurl;
1554 urlset_nr = remote->pushurl_nr;
1556 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1557 urlset = remote->url;
1558 urlset_nr = remote->url_nr;
1561 /* Special cases that add new entry. */
1562 if ((!oldurl && !delete_mode) || add_mode) {
1564 git_config_set_multivar(name_buf.buf, newurl,
1567 git_config_set(name_buf.buf, newurl);
1571 /* Old URL specified. Demand that one matches. */
1572 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1573 die(_("Invalid old URL pattern: %s"), oldurl);
1575 for (i = 0; i < urlset_nr; i++)
1576 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1580 if (!delete_mode && !matches)
1581 die(_("No such URL found: %s"), oldurl);
1582 if (delete_mode && !negative_matches && !push_mode)
1583 die(_("Will not delete all non-push URLs"));
1585 regfree(&old_regex);
1588 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1590 git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
1592 strbuf_release(&name_buf);
1596 int cmd_remote(int argc, const char **argv, const char *prefix)
1598 struct option options[] = {
1599 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1604 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1605 PARSE_OPT_STOP_AT_NON_OPTION);
1608 result = show_all();
1609 else if (!strcmp(argv[0], "add"))
1610 result = add(argc, argv);
1611 else if (!strcmp(argv[0], "rename"))
1612 result = mv(argc, argv);
1613 else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove"))
1614 result = rm(argc, argv);
1615 else if (!strcmp(argv[0], "set-head"))
1616 result = set_head(argc, argv);
1617 else if (!strcmp(argv[0], "set-branches"))
1618 result = set_branches(argc, argv);
1619 else if (!strcmp(argv[0], "get-url"))
1620 result = get_url(argc, argv);
1621 else if (!strcmp(argv[0], "set-url"))
1622 result = set_url(argc, argv);
1623 else if (!strcmp(argv[0], "show"))
1624 result = show(argc, argv);
1625 else if (!strcmp(argv[0], "prune"))
1626 result = prune(argc, argv);
1627 else if (!strcmp(argv[0], "update"))
1628 result = update(argc, argv);
1630 error(_("Unknown subcommand: %s"), argv[0]);
1631 usage_with_options(builtin_remote_usage, options);
1634 return result ? 1 : 0;