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_OPT_COMP_ARG, 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, "recreate"))
310 info->rebase = NORMAL_REBASE;
311 else if (!strcmp(value, "interactive"))
312 info->rebase = INTERACTIVE_REBASE;
318 static void read_branches(void)
322 git_config(config_read_branches, NULL);
326 struct remote *remote;
327 struct string_list new_refs, stale, tracked, heads, push;
331 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
333 struct ref *fetch_map = NULL, **tail = &fetch_map;
334 struct ref *ref, *stale_refs;
337 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
338 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
339 die(_("Could not get fetch map for refspec %s"),
340 states->remote->fetch_refspec[i]);
342 states->new_refs.strdup_strings = 1;
343 states->tracked.strdup_strings = 1;
344 states->stale.strdup_strings = 1;
345 for (ref = fetch_map; ref; ref = ref->next) {
346 if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
347 string_list_append(&states->new_refs, abbrev_branch(ref->name));
349 string_list_append(&states->tracked, abbrev_branch(ref->name));
351 stale_refs = get_stale_heads(states->remote->fetch,
352 states->remote->fetch_refspec_nr, fetch_map);
353 for (ref = stale_refs; ref; ref = ref->next) {
354 struct string_list_item *item =
355 string_list_append(&states->stale, abbrev_branch(ref->name));
356 item->util = xstrdup(ref->name);
358 free_refs(stale_refs);
359 free_refs(fetch_map);
361 string_list_sort(&states->new_refs);
362 string_list_sort(&states->tracked);
363 string_list_sort(&states->stale);
372 PUSH_STATUS_CREATE = 0,
374 PUSH_STATUS_UPTODATE,
375 PUSH_STATUS_FASTFORWARD,
376 PUSH_STATUS_OUTOFDATE,
377 PUSH_STATUS_NOTQUERIED
381 static int get_push_ref_states(const struct ref *remote_refs,
382 struct ref_states *states)
384 struct remote *remote = states->remote;
385 struct ref *ref, *local_refs, *push_map;
389 local_refs = get_local_heads();
390 push_map = copy_ref_list(remote_refs);
392 match_push_refs(local_refs, &push_map, remote->push_refspec_nr,
393 remote->push_refspec, MATCH_REFS_NONE);
395 states->push.strdup_strings = 1;
396 for (ref = push_map; ref; ref = ref->next) {
397 struct string_list_item *item;
398 struct push_info *info;
402 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
404 item = string_list_append(&states->push,
405 abbrev_branch(ref->peer_ref->name));
406 item->util = xcalloc(1, sizeof(struct push_info));
408 info->forced = ref->force;
409 info->dest = xstrdup(abbrev_branch(ref->name));
411 if (is_null_oid(&ref->new_oid)) {
412 info->status = PUSH_STATUS_DELETE;
413 } else if (!oidcmp(&ref->old_oid, &ref->new_oid))
414 info->status = PUSH_STATUS_UPTODATE;
415 else if (is_null_oid(&ref->old_oid))
416 info->status = PUSH_STATUS_CREATE;
417 else if (has_object_file(&ref->old_oid) &&
418 ref_newer(&ref->new_oid, &ref->old_oid))
419 info->status = PUSH_STATUS_FASTFORWARD;
421 info->status = PUSH_STATUS_OUTOFDATE;
423 free_refs(local_refs);
428 static int get_push_ref_states_noquery(struct ref_states *states)
431 struct remote *remote = states->remote;
432 struct string_list_item *item;
433 struct push_info *info;
438 states->push.strdup_strings = 1;
439 if (!remote->push_refspec_nr) {
440 item = string_list_append(&states->push, _("(matching)"));
441 info = item->util = xcalloc(1, sizeof(struct push_info));
442 info->status = PUSH_STATUS_NOTQUERIED;
443 info->dest = xstrdup(item->string);
445 for (i = 0; i < remote->push_refspec_nr; i++) {
446 struct refspec *spec = remote->push + i;
448 item = string_list_append(&states->push, _("(matching)"));
449 else if (strlen(spec->src))
450 item = string_list_append(&states->push, spec->src);
452 item = string_list_append(&states->push, _("(delete)"));
454 info = item->util = xcalloc(1, sizeof(struct push_info));
455 info->forced = spec->force;
456 info->status = PUSH_STATUS_NOTQUERIED;
457 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
462 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
464 struct ref *ref, *matches;
465 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
466 struct refspec refspec;
470 refspec.src = refspec.dst = "refs/heads/*";
471 states->heads.strdup_strings = 1;
472 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
473 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
475 for (ref = matches; ref; ref = ref->next)
476 string_list_append(&states->heads, abbrev_branch(ref->name));
478 free_refs(fetch_map);
484 struct known_remote {
485 struct known_remote *next;
486 struct remote *remote;
489 struct known_remotes {
490 struct remote *to_delete;
491 struct known_remote *list;
494 static int add_known_remote(struct remote *remote, void *cb_data)
496 struct known_remotes *all = cb_data;
497 struct known_remote *r;
499 if (!strcmp(all->to_delete->name, remote->name))
502 r = xmalloc(sizeof(*r));
509 struct branches_for_remote {
510 struct remote *remote;
511 struct string_list *branches, *skipped;
512 struct known_remotes *keep;
515 static int add_branch_for_removal(const char *refname,
516 const struct object_id *oid, int flags, void *cb_data)
518 struct branches_for_remote *branches = cb_data;
519 struct refspec refspec;
520 struct known_remote *kr;
522 memset(&refspec, 0, sizeof(refspec));
523 refspec.dst = (char *)refname;
524 if (remote_find_tracking(branches->remote, &refspec))
527 /* don't delete a branch if another remote also uses it */
528 for (kr = branches->keep->list; kr; kr = kr->next) {
529 memset(&refspec, 0, sizeof(refspec));
530 refspec.dst = (char *)refname;
531 if (!remote_find_tracking(kr->remote, &refspec))
535 /* don't delete non-remote-tracking refs */
536 if (!starts_with(refname, "refs/remotes/")) {
537 /* advise user how to delete local branches */
538 if (starts_with(refname, "refs/heads/"))
539 string_list_append(branches->skipped,
540 abbrev_branch(refname));
541 /* silently skip over other non-remote refs */
545 string_list_append(branches->branches, refname);
551 const char *old_name;
552 const char *new_name;
553 struct string_list *remote_branches;
556 static int read_remote_branches(const char *refname,
557 const struct object_id *oid, int flags, void *cb_data)
559 struct rename_info *rename = cb_data;
560 struct strbuf buf = STRBUF_INIT;
561 struct string_list_item *item;
565 strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name);
566 if (starts_with(refname, buf.buf)) {
567 item = string_list_append(rename->remote_branches, xstrdup(refname));
568 symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
570 if (symref && (flag & REF_ISSYMREF))
571 item->util = xstrdup(symref);
575 strbuf_release(&buf);
580 static int migrate_file(struct remote *remote)
582 struct strbuf buf = STRBUF_INIT;
585 strbuf_addf(&buf, "remote.%s.url", remote->name);
586 for (i = 0; i < remote->url_nr; i++)
587 git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
589 strbuf_addf(&buf, "remote.%s.push", remote->name);
590 for (i = 0; i < remote->push_refspec_nr; i++)
591 git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0);
593 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
594 for (i = 0; i < remote->fetch_refspec_nr; i++)
595 git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0);
596 if (remote->origin == REMOTE_REMOTES)
597 unlink_or_warn(git_path("remotes/%s", remote->name));
598 else if (remote->origin == REMOTE_BRANCHES)
599 unlink_or_warn(git_path("branches/%s", remote->name));
600 strbuf_release(&buf);
605 static int mv(int argc, const char **argv)
607 struct option options[] = {
610 struct remote *oldremote, *newremote;
611 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
612 old_remote_context = STRBUF_INIT;
613 struct string_list remote_branches = STRING_LIST_INIT_NODUP;
614 struct rename_info rename;
615 int i, refspec_updated = 0;
618 usage_with_options(builtin_remote_rename_usage, options);
620 rename.old_name = argv[1];
621 rename.new_name = argv[2];
622 rename.remote_branches = &remote_branches;
624 oldremote = remote_get(rename.old_name);
625 if (!remote_is_configured(oldremote, 1))
626 die(_("No such remote: %s"), rename.old_name);
628 if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
629 return migrate_file(oldremote);
631 newremote = remote_get(rename.new_name);
632 if (remote_is_configured(newremote, 1))
633 die(_("remote %s already exists."), rename.new_name);
635 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new_name);
636 if (!valid_fetch_refspec(buf.buf))
637 die(_("'%s' is not a valid remote name"), rename.new_name);
640 strbuf_addf(&buf, "remote.%s", rename.old_name);
641 strbuf_addf(&buf2, "remote.%s", rename.new_name);
642 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
643 return error(_("Could not rename config section '%s' to '%s'"),
647 strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
648 git_config_set_multivar(buf.buf, NULL, NULL, 1);
649 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
650 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
654 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
655 ptr = strstr(buf2.buf, old_remote_context.buf);
659 ptr-buf2.buf + strlen(":refs/remotes/"),
660 strlen(rename.old_name), rename.new_name,
661 strlen(rename.new_name));
663 warning(_("Not updating non-default fetch refspec\n"
665 "\tPlease update the configuration manually if necessary."),
668 git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
672 for (i = 0; i < branch_list.nr; i++) {
673 struct string_list_item *item = branch_list.items + i;
674 struct branch_info *info = item->util;
675 if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) {
677 strbuf_addf(&buf, "branch.%s.remote", item->string);
678 git_config_set(buf.buf, rename.new_name);
682 if (!refspec_updated)
686 * First remove symrefs, then rename the rest, finally create
689 for_each_ref(read_remote_branches, &rename);
690 for (i = 0; i < remote_branches.nr; i++) {
691 struct string_list_item *item = remote_branches.items + i;
693 struct object_id oid;
695 read_ref_full(item->string, RESOLVE_REF_READING, &oid, &flag);
696 if (!(flag & REF_ISSYMREF))
698 if (delete_ref(NULL, item->string, NULL, REF_NO_DEREF))
699 die(_("deleting '%s' failed"), item->string);
701 for (i = 0; i < remote_branches.nr; i++) {
702 struct string_list_item *item = remote_branches.items + i;
707 strbuf_addstr(&buf, item->string);
708 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
709 rename.new_name, strlen(rename.new_name));
711 strbuf_addf(&buf2, "remote: renamed %s to %s",
712 item->string, buf.buf);
713 if (rename_ref(item->string, buf.buf, buf2.buf))
714 die(_("renaming '%s' failed"), item->string);
716 for (i = 0; i < remote_branches.nr; i++) {
717 struct string_list_item *item = remote_branches.items + i;
722 strbuf_addstr(&buf, item->string);
723 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
724 rename.new_name, strlen(rename.new_name));
726 strbuf_addstr(&buf2, item->util);
727 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name),
728 rename.new_name, strlen(rename.new_name));
730 strbuf_addf(&buf3, "remote: renamed %s to %s",
731 item->string, buf.buf);
732 if (create_symref(buf.buf, buf2.buf, buf3.buf))
733 die(_("creating '%s' failed"), buf.buf);
738 static int rm(int argc, const char **argv)
740 struct option options[] = {
743 struct remote *remote;
744 struct strbuf buf = STRBUF_INIT;
745 struct known_remotes known_remotes = { NULL, NULL };
746 struct string_list branches = STRING_LIST_INIT_DUP;
747 struct string_list skipped = STRING_LIST_INIT_DUP;
748 struct branches_for_remote cb_data;
751 memset(&cb_data, 0, sizeof(cb_data));
752 cb_data.branches = &branches;
753 cb_data.skipped = &skipped;
754 cb_data.keep = &known_remotes;
757 usage_with_options(builtin_remote_rm_usage, options);
759 remote = remote_get(argv[1]);
760 if (!remote_is_configured(remote, 1))
761 die(_("No such remote: %s"), argv[1]);
763 known_remotes.to_delete = remote;
764 for_each_remote(add_known_remote, &known_remotes);
767 for (i = 0; i < branch_list.nr; i++) {
768 struct string_list_item *item = branch_list.items + i;
769 struct branch_info *info = item->util;
770 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
771 const char *keys[] = { "remote", "merge", NULL }, **k;
772 for (k = keys; *k; k++) {
774 strbuf_addf(&buf, "branch.%s.%s",
776 result = git_config_set_gently(buf.buf, NULL);
777 if (result && result != CONFIG_NOTHING_SET)
778 die(_("could not unset '%s'"), buf.buf);
784 * We cannot just pass a function to for_each_ref() which deletes
785 * the branches one by one, since for_each_ref() relies on cached
786 * refs, which are invalidated when deleting a branch.
788 cb_data.remote = remote;
789 result = for_each_ref(add_branch_for_removal, &cb_data);
790 strbuf_release(&buf);
793 result = delete_refs("remote: remove", &branches, REF_NO_DEREF);
794 string_list_clear(&branches, 0);
798 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
799 "to delete it, use:",
800 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
801 "to delete them, use:",
803 for (i = 0; i < skipped.nr; i++)
804 fprintf(stderr, " git branch -d %s\n",
805 skipped.items[i].string);
807 string_list_clear(&skipped, 0);
810 strbuf_addf(&buf, "remote.%s", remote->name);
811 if (git_config_rename_section(buf.buf, NULL) < 1)
812 return error(_("Could not remove config section '%s'"), buf.buf);
818 static void clear_push_info(void *util, const char *string)
820 struct push_info *info = util;
825 static void free_remote_ref_states(struct ref_states *states)
827 string_list_clear(&states->new_refs, 0);
828 string_list_clear(&states->stale, 1);
829 string_list_clear(&states->tracked, 0);
830 string_list_clear(&states->heads, 0);
831 string_list_clear_func(&states->push, clear_push_info);
834 static int append_ref_to_tracked_list(const char *refname,
835 const struct object_id *oid, int flags, void *cb_data)
837 struct ref_states *states = cb_data;
838 struct refspec refspec;
840 if (flags & REF_ISSYMREF)
843 memset(&refspec, 0, sizeof(refspec));
844 refspec.dst = (char *)refname;
845 if (!remote_find_tracking(states->remote, &refspec))
846 string_list_append(&states->tracked, abbrev_branch(refspec.src));
851 static int get_remote_ref_states(const char *name,
852 struct ref_states *states,
855 struct transport *transport;
856 const struct ref *remote_refs;
858 states->remote = remote_get(name);
860 return error(_("No such remote: %s"), name);
865 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
866 states->remote->url[0] : NULL);
867 remote_refs = transport_get_remote_refs(transport, NULL);
868 transport_disconnect(transport);
871 if (query & GET_REF_STATES)
872 get_ref_states(remote_refs, states);
873 if (query & GET_HEAD_NAMES)
874 get_head_names(remote_refs, states);
875 if (query & GET_PUSH_REF_STATES)
876 get_push_ref_states(remote_refs, states);
878 for_each_ref(append_ref_to_tracked_list, states);
879 string_list_sort(&states->tracked);
880 get_push_ref_states_noquery(states);
887 struct string_list *list;
888 struct ref_states *states;
893 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
895 struct show_info *info = cb_data;
896 int n = strlen(item->string);
899 string_list_insert(info->list, item->string);
903 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
905 struct show_info *info = cb_data;
906 struct ref_states *states = info->states;
907 const char *name = item->string;
909 if (states->queried) {
910 const char *fmt = "%s";
911 const char *arg = "";
912 if (string_list_has_string(&states->new_refs, name)) {
913 fmt = _(" new (next fetch will store in remotes/%s)");
914 arg = states->remote->name;
915 } else if (string_list_has_string(&states->tracked, name))
917 else if (string_list_has_string(&states->stale, name))
918 arg = _(" stale (use 'git remote prune' to remove)");
921 printf(" %-*s", info->width, name);
925 printf(" %s\n", name);
930 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
932 struct show_info *show_info = cb_data;
933 struct ref_states *states = show_info->states;
934 struct branch_info *branch_info = branch_item->util;
935 struct string_list_item *item;
938 if (!branch_info->merge.nr || !branch_info->remote_name ||
939 strcmp(states->remote->name, branch_info->remote_name))
941 if ((n = strlen(branch_item->string)) > show_info->width)
942 show_info->width = n;
943 if (branch_info->rebase)
944 show_info->any_rebase = 1;
946 item = string_list_insert(show_info->list, branch_item->string);
947 item->util = branch_info;
952 static int show_local_info_item(struct string_list_item *item, void *cb_data)
954 struct show_info *show_info = cb_data;
955 struct branch_info *branch_info = item->util;
956 struct string_list *merge = &branch_info->merge;
957 int width = show_info->width + 4;
960 if (branch_info->rebase && branch_info->merge.nr > 1) {
961 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
966 printf(" %-*s ", show_info->width, item->string);
967 if (branch_info->rebase) {
968 printf_ln(branch_info->rebase == INTERACTIVE_REBASE
969 ? _("rebases interactively onto remote %s")
970 : _("rebases onto remote %s"), merge->items[0].string);
972 } else if (show_info->any_rebase) {
973 printf_ln(_(" merges with remote %s"), merge->items[0].string);
976 printf_ln(_("merges with remote %s"), merge->items[0].string);
978 for (i = 1; i < merge->nr; i++)
979 printf(_("%-*s and with remote %s\n"), width, "",
980 merge->items[i].string);
985 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
987 struct show_info *show_info = cb_data;
988 struct push_info *push_info = push_item->util;
989 struct string_list_item *item;
991 if ((n = strlen(push_item->string)) > show_info->width)
992 show_info->width = n;
993 if ((n = strlen(push_info->dest)) > show_info->width2)
994 show_info->width2 = n;
995 item = string_list_append(show_info->list, push_item->string);
996 item->util = push_item->util;
1001 * Sorting comparison for a string list that has push_info
1002 * structs in its util field
1004 static int cmp_string_with_push(const void *va, const void *vb)
1006 const struct string_list_item *a = va;
1007 const struct string_list_item *b = vb;
1008 const struct push_info *a_push = a->util;
1009 const struct push_info *b_push = b->util;
1010 int cmp = strcmp(a->string, b->string);
1011 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1014 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1016 struct show_info *show_info = cb_data;
1017 struct push_info *push_info = item->util;
1018 const char *src = item->string, *status = NULL;
1020 switch (push_info->status) {
1021 case PUSH_STATUS_CREATE:
1022 status = _("create");
1024 case PUSH_STATUS_DELETE:
1025 status = _("delete");
1028 case PUSH_STATUS_UPTODATE:
1029 status = _("up to date");
1031 case PUSH_STATUS_FASTFORWARD:
1032 status = _("fast-forwardable");
1034 case PUSH_STATUS_OUTOFDATE:
1035 status = _("local out of date");
1037 case PUSH_STATUS_NOTQUERIED:
1041 if (push_info->forced)
1042 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1043 show_info->width2, push_info->dest, status);
1045 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1046 show_info->width2, push_info->dest, status);
1048 if (push_info->forced)
1049 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1052 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1058 static int get_one_entry(struct remote *remote, void *priv)
1060 struct string_list *list = priv;
1061 struct strbuf url_buf = STRBUF_INIT;
1065 if (remote->url_nr > 0) {
1066 strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
1067 string_list_append(list, remote->name)->util =
1068 strbuf_detach(&url_buf, NULL);
1070 string_list_append(list, remote->name)->util = NULL;
1071 if (remote->pushurl_nr) {
1072 url = remote->pushurl;
1073 url_nr = remote->pushurl_nr;
1076 url_nr = remote->url_nr;
1078 for (i = 0; i < url_nr; i++)
1080 strbuf_addf(&url_buf, "%s (push)", url[i]);
1081 string_list_append(list, remote->name)->util =
1082 strbuf_detach(&url_buf, NULL);
1088 static int show_all(void)
1090 struct string_list list = STRING_LIST_INIT_NODUP;
1093 list.strdup_strings = 1;
1094 result = for_each_remote(get_one_entry, &list);
1099 string_list_sort(&list);
1100 for (i = 0; i < list.nr; i++) {
1101 struct string_list_item *item = list.items + i;
1103 printf("%s\t%s\n", item->string,
1104 item->util ? (const char *)item->util : "");
1106 if (i && !strcmp((item - 1)->string, item->string))
1108 printf("%s\n", item->string);
1112 string_list_clear(&list, 1);
1116 static int show(int argc, const char **argv)
1118 int no_query = 0, result = 0, query_flag = 0;
1119 struct option options[] = {
1120 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1123 struct ref_states states;
1124 struct string_list info_list = STRING_LIST_INIT_NODUP;
1125 struct show_info info;
1127 argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1134 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1136 memset(&states, 0, sizeof(states));
1137 memset(&info, 0, sizeof(info));
1138 info.states = &states;
1139 info.list = &info_list;
1140 for (; argc; argc--, argv++) {
1145 get_remote_ref_states(*argv, &states, query_flag);
1147 printf_ln(_("* remote %s"), *argv);
1148 printf_ln(_(" Fetch URL: %s"), states.remote->url_nr > 0 ?
1149 states.remote->url[0] : _("(no URL)"));
1150 if (states.remote->pushurl_nr) {
1151 url = states.remote->pushurl;
1152 url_nr = states.remote->pushurl_nr;
1154 url = states.remote->url;
1155 url_nr = states.remote->url_nr;
1157 for (i = 0; i < url_nr; i++)
1159 * TRANSLATORS: the colon ':' should align
1160 * with the one in " Fetch URL: %s"
1163 printf_ln(_(" Push URL: %s"), url[i]);
1165 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1167 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1168 else if (!states.heads.nr)
1169 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1170 else if (states.heads.nr == 1)
1171 printf_ln(_(" HEAD branch: %s"), states.heads.items[0].string);
1173 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1174 " may be one of the following):\n"));
1175 for (i = 0; i < states.heads.nr; i++)
1176 printf(" %s\n", states.heads.items[i].string);
1179 /* remote branch info */
1181 for_each_string_list(&states.new_refs, add_remote_to_show_info, &info);
1182 for_each_string_list(&states.tracked, add_remote_to_show_info, &info);
1183 for_each_string_list(&states.stale, add_remote_to_show_info, &info);
1185 printf_ln(Q_(" Remote branch:%s",
1186 " Remote branches:%s",
1188 no_query ? _(" (status not queried)") : "");
1189 for_each_string_list(info.list, show_remote_info_item, &info);
1190 string_list_clear(info.list, 0);
1194 info.any_rebase = 0;
1195 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1197 printf_ln(Q_(" Local branch configured for 'git pull':",
1198 " Local branches configured for 'git pull':",
1200 for_each_string_list(info.list, show_local_info_item, &info);
1201 string_list_clear(info.list, 0);
1204 if (states.remote->mirror)
1205 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1207 info.width = info.width2 = 0;
1208 for_each_string_list(&states.push, add_push_to_show_info, &info);
1209 QSORT(info.list->items, info.list->nr, cmp_string_with_push);
1211 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1212 " Local refs configured for 'git push'%s:",
1214 no_query ? _(" (status not queried)") : "");
1215 for_each_string_list(info.list, show_push_info_item, &info);
1216 string_list_clear(info.list, 0);
1218 free_remote_ref_states(&states);
1224 static int set_head(int argc, const char **argv)
1226 int i, opt_a = 0, opt_d = 0, result = 0;
1227 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1228 char *head_name = NULL;
1230 struct option options[] = {
1231 OPT_BOOL('a', "auto", &opt_a,
1232 N_("set refs/remotes/<name>/HEAD according to remote")),
1233 OPT_BOOL('d', "delete", &opt_d,
1234 N_("delete refs/remotes/<name>/HEAD")),
1237 argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1240 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1242 if (!opt_a && !opt_d && argc == 2) {
1243 head_name = xstrdup(argv[1]);
1244 } else if (opt_a && !opt_d && argc == 1) {
1245 struct ref_states states;
1246 memset(&states, 0, sizeof(states));
1247 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1248 if (!states.heads.nr)
1249 result |= error(_("Cannot determine remote HEAD"));
1250 else if (states.heads.nr > 1) {
1251 result |= error(_("Multiple remote HEAD branches. "
1252 "Please choose one explicitly with:"));
1253 for (i = 0; i < states.heads.nr; i++)
1254 fprintf(stderr, " git remote set-head %s %s\n",
1255 argv[0], states.heads.items[i].string);
1257 head_name = xstrdup(states.heads.items[0].string);
1258 free_remote_ref_states(&states);
1259 } else if (opt_d && !opt_a && argc == 1) {
1260 if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF))
1261 result |= error(_("Could not delete %s"), buf.buf);
1263 usage_with_options(builtin_remote_sethead_usage, options);
1266 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1267 /* make sure it's valid */
1268 if (!ref_exists(buf2.buf))
1269 result |= error(_("Not a valid ref: %s"), buf2.buf);
1270 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1271 result |= error(_("Could not setup %s"), buf.buf);
1273 printf("%s/HEAD set to %s\n", argv[0], head_name);
1277 strbuf_release(&buf);
1278 strbuf_release(&buf2);
1282 static int prune_remote(const char *remote, int dry_run)
1285 struct ref_states states;
1286 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1287 struct string_list_item *item;
1288 const char *dangling_msg = dry_run
1289 ? _(" %s will become dangling!")
1290 : _(" %s has become dangling!");
1292 memset(&states, 0, sizeof(states));
1293 get_remote_ref_states(remote, &states, GET_REF_STATES);
1295 if (!states.stale.nr) {
1296 free_remote_ref_states(&states);
1300 printf_ln(_("Pruning %s"), remote);
1301 printf_ln(_("URL: %s"),
1302 states.remote->url_nr
1303 ? states.remote->url[0]
1306 for_each_string_list_item(item, &states.stale)
1307 string_list_append(&refs_to_prune, item->util);
1308 string_list_sort(&refs_to_prune);
1311 result |= delete_refs("remote: prune", &refs_to_prune, 0);
1313 for_each_string_list_item(item, &states.stale) {
1314 const char *refname = item->util;
1317 printf_ln(_(" * [would prune] %s"),
1318 abbrev_ref(refname, "refs/remotes/"));
1320 printf_ln(_(" * [pruned] %s"),
1321 abbrev_ref(refname, "refs/remotes/"));
1324 warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
1326 string_list_clear(&refs_to_prune, 0);
1327 free_remote_ref_states(&states);
1331 static int prune(int argc, const char **argv)
1333 int dry_run = 0, result = 0;
1334 struct option options[] = {
1335 OPT__DRY_RUN(&dry_run, N_("dry run")),
1339 argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1343 usage_with_options(builtin_remote_prune_usage, options);
1345 for (; argc; argc--, argv++)
1346 result |= prune_remote(*argv, dry_run);
1351 static int get_remote_default(const char *key, const char *value, void *priv)
1353 if (strcmp(key, "remotes.default") == 0) {
1360 static int update(int argc, const char **argv)
1363 struct option options[] = {
1364 OPT_BOOL('p', "prune", &prune,
1365 N_("prune remotes after fetching")),
1368 struct argv_array fetch_argv = ARGV_ARRAY_INIT;
1369 int default_defined = 0;
1372 argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1373 PARSE_OPT_KEEP_ARGV0);
1375 argv_array_push(&fetch_argv, "fetch");
1378 argv_array_push(&fetch_argv, prune ? "--prune" : "--no-prune");
1380 argv_array_push(&fetch_argv, "-v");
1381 argv_array_push(&fetch_argv, "--multiple");
1383 argv_array_push(&fetch_argv, "default");
1384 for (i = 1; i < argc; i++)
1385 argv_array_push(&fetch_argv, argv[i]);
1387 if (strcmp(fetch_argv.argv[fetch_argv.argc-1], "default") == 0) {
1388 git_config(get_remote_default, &default_defined);
1389 if (!default_defined) {
1390 argv_array_pop(&fetch_argv);
1391 argv_array_push(&fetch_argv, "--all");
1395 retval = run_command_v_opt(fetch_argv.argv, RUN_GIT_CMD);
1396 argv_array_clear(&fetch_argv);
1400 static int remove_all_fetch_refspecs(const char *remote, const char *key)
1402 return git_config_set_multivar_gently(key, NULL, NULL, 1);
1405 static void add_branches(struct remote *remote, const char **branches,
1408 const char *remotename = remote->name;
1409 int mirror = remote->mirror;
1410 struct strbuf refspec = STRBUF_INIT;
1412 for (; *branches; branches++)
1413 add_branch(key, *branches, remotename, mirror, &refspec);
1415 strbuf_release(&refspec);
1418 static int set_remote_branches(const char *remotename, const char **branches,
1421 struct strbuf key = STRBUF_INIT;
1422 struct remote *remote;
1424 strbuf_addf(&key, "remote.%s.fetch", remotename);
1426 remote = remote_get(remotename);
1427 if (!remote_is_configured(remote, 1))
1428 die(_("No such remote '%s'"), remotename);
1430 if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) {
1431 strbuf_release(&key);
1434 add_branches(remote, branches, key.buf);
1436 strbuf_release(&key);
1440 static int set_branches(int argc, const char **argv)
1443 struct option options[] = {
1444 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1448 argc = parse_options(argc, argv, NULL, options,
1449 builtin_remote_setbranches_usage, 0);
1451 error(_("no remote specified"));
1452 usage_with_options(builtin_remote_setbranches_usage, options);
1456 return set_remote_branches(argv[0], argv + 1, add_mode);
1459 static int get_url(int argc, const char **argv)
1461 int i, push_mode = 0, all_mode = 0;
1462 const char *remotename = NULL;
1463 struct remote *remote;
1466 struct option options[] = {
1467 OPT_BOOL('\0', "push", &push_mode,
1468 N_("query push URLs rather than fetch URLs")),
1469 OPT_BOOL('\0', "all", &all_mode,
1470 N_("return all URLs")),
1473 argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0);
1476 usage_with_options(builtin_remote_geturl_usage, options);
1478 remotename = argv[0];
1480 remote = remote_get(remotename);
1481 if (!remote_is_configured(remote, 1))
1482 die(_("No such remote '%s'"), remotename);
1486 url = remote->pushurl;
1487 url_nr = remote->pushurl_nr;
1489 /* else fetch mode */
1491 /* Use the fetch URL when no push URLs were found or requested. */
1494 url_nr = remote->url_nr;
1498 die(_("no URLs configured for remote '%s'"), remotename);
1501 for (i = 0; i < url_nr; i++)
1502 printf_ln("%s", url[i]);
1504 printf_ln("%s", *url);
1510 static int set_url(int argc, const char **argv)
1512 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1513 int matches = 0, negative_matches = 0;
1514 const char *remotename = NULL;
1515 const char *newurl = NULL;
1516 const char *oldurl = NULL;
1517 struct remote *remote;
1519 const char **urlset;
1521 struct strbuf name_buf = STRBUF_INIT;
1522 struct option options[] = {
1523 OPT_BOOL('\0', "push", &push_mode,
1524 N_("manipulate push URLs")),
1525 OPT_BOOL('\0', "add", &add_mode,
1527 OPT_BOOL('\0', "delete", &delete_mode,
1531 argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage,
1532 PARSE_OPT_KEEP_ARGV0);
1534 if (add_mode && delete_mode)
1535 die(_("--add --delete doesn't make sense"));
1537 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1538 usage_with_options(builtin_remote_seturl_usage, options);
1540 remotename = argv[1];
1548 remote = remote_get(remotename);
1549 if (!remote_is_configured(remote, 1))
1550 die(_("No such remote '%s'"), remotename);
1553 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1554 urlset = remote->pushurl;
1555 urlset_nr = remote->pushurl_nr;
1557 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1558 urlset = remote->url;
1559 urlset_nr = remote->url_nr;
1562 /* Special cases that add new entry. */
1563 if ((!oldurl && !delete_mode) || add_mode) {
1565 git_config_set_multivar(name_buf.buf, newurl,
1568 git_config_set(name_buf.buf, newurl);
1572 /* Old URL specified. Demand that one matches. */
1573 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1574 die(_("Invalid old URL pattern: %s"), oldurl);
1576 for (i = 0; i < urlset_nr; i++)
1577 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1581 if (!delete_mode && !matches)
1582 die(_("No such URL found: %s"), oldurl);
1583 if (delete_mode && !negative_matches && !push_mode)
1584 die(_("Will not delete all non-push URLs"));
1586 regfree(&old_regex);
1589 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1591 git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
1593 strbuf_release(&name_buf);
1597 int cmd_remote(int argc, const char **argv, const char *prefix)
1599 struct option options[] = {
1600 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1605 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1606 PARSE_OPT_STOP_AT_NON_OPTION);
1609 result = show_all();
1610 else if (!strcmp(argv[0], "add"))
1611 result = add(argc, argv);
1612 else if (!strcmp(argv[0], "rename"))
1613 result = mv(argc, argv);
1614 else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove"))
1615 result = rm(argc, argv);
1616 else if (!strcmp(argv[0], "set-head"))
1617 result = set_head(argc, argv);
1618 else if (!strcmp(argv[0], "set-branches"))
1619 result = set_branches(argc, argv);
1620 else if (!strcmp(argv[0], "get-url"))
1621 result = get_url(argc, argv);
1622 else if (!strcmp(argv[0], "set-url"))
1623 result = set_url(argc, argv);
1624 else if (!strcmp(argv[0], "show"))
1625 result = show(argc, argv);
1626 else if (!strcmp(argv[0], "prune"))
1627 result = prune(argc, argv);
1628 else if (!strcmp(argv[0], "update"))
1629 result = update(argc, argv);
1631 error(_("Unknown subcommand: %s"), argv[0]);
1632 usage_with_options(builtin_remote_usage, options);
1635 return result ? 1 : 0;