t0410: make test description clearer
[git/gitster.git] / builtin / remote.c
blob76670ddd8b44e5b118c76522613c445ad92c5364
1 #define USE_THE_REPOSITORY_VARIABLE
2 #include "builtin.h"
3 #include "config.h"
4 #include "gettext.h"
5 #include "parse-options.h"
6 #include "path.h"
7 #include "transport.h"
8 #include "remote.h"
9 #include "string-list.h"
10 #include "strbuf.h"
11 #include "run-command.h"
12 #include "rebase.h"
13 #include "refs.h"
14 #include "refspec.h"
15 #include "object-store-ll.h"
16 #include "strvec.h"
17 #include "commit-reach.h"
18 #include "progress.h"
20 static const char * const builtin_remote_usage[] = {
21 "git remote [-v | --verbose]",
22 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
23 N_("git remote rename [--[no-]progress] <old> <new>"),
24 N_("git remote remove <name>"),
25 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
26 N_("git remote [-v | --verbose] show [-n] <name>"),
27 N_("git remote prune [-n | --dry-run] <name>"),
28 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
29 N_("git remote set-branches [--add] <name> <branch>..."),
30 N_("git remote get-url [--push] [--all] <name>"),
31 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
32 N_("git remote set-url --add <name> <newurl>"),
33 N_("git remote set-url --delete <name> <url>"),
34 NULL
37 static const char * const builtin_remote_add_usage[] = {
38 N_("git remote add [<options>] <name> <url>"),
39 NULL
42 static const char * const builtin_remote_rename_usage[] = {
43 N_("git remote rename [--[no-]progress] <old> <new>"),
44 NULL
47 static const char * const builtin_remote_rm_usage[] = {
48 N_("git remote remove <name>"),
49 NULL
52 static const char * const builtin_remote_sethead_usage[] = {
53 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
54 NULL
57 static const char * const builtin_remote_setbranches_usage[] = {
58 N_("git remote set-branches <name> <branch>..."),
59 N_("git remote set-branches --add <name> <branch>..."),
60 NULL
63 static const char * const builtin_remote_show_usage[] = {
64 N_("git remote show [<options>] <name>"),
65 NULL
68 static const char * const builtin_remote_prune_usage[] = {
69 N_("git remote prune [<options>] <name>"),
70 NULL
73 static const char * const builtin_remote_update_usage[] = {
74 N_("git remote update [<options>] [<group> | <remote>]..."),
75 NULL
78 static const char * const builtin_remote_geturl_usage[] = {
79 N_("git remote get-url [--push] [--all] <name>"),
80 NULL
83 static const char * const builtin_remote_seturl_usage[] = {
84 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
85 N_("git remote set-url --add <name> <newurl>"),
86 N_("git remote set-url --delete <name> <url>"),
87 NULL
90 #define GET_REF_STATES (1<<0)
91 #define GET_HEAD_NAMES (1<<1)
92 #define GET_PUSH_REF_STATES (1<<2)
94 static int verbose;
96 static int fetch_remote(const char *name)
98 struct child_process cmd = CHILD_PROCESS_INIT;
100 strvec_push(&cmd.args, "fetch");
101 if (verbose)
102 strvec_push(&cmd.args, "-v");
103 strvec_push(&cmd.args, name);
104 cmd.git_cmd = 1;
105 printf_ln(_("Updating %s"), name);
106 if (run_command(&cmd))
107 return error(_("Could not fetch %s"), name);
108 return 0;
111 enum {
112 TAGS_UNSET = 0,
113 TAGS_DEFAULT = 1,
114 TAGS_SET = 2
117 #define MIRROR_NONE 0
118 #define MIRROR_FETCH 1
119 #define MIRROR_PUSH 2
120 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
122 static void add_branch(const char *key, const char *branchname,
123 const char *remotename, int mirror, struct strbuf *tmp)
125 strbuf_reset(tmp);
126 strbuf_addch(tmp, '+');
127 if (mirror)
128 strbuf_addf(tmp, "refs/%s:refs/%s",
129 branchname, branchname);
130 else
131 strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
132 branchname, remotename, branchname);
133 git_config_set_multivar(key, tmp->buf, "^$", 0);
136 static const char mirror_advice[] =
137 N_("--mirror is dangerous and deprecated; please\n"
138 "\t use --mirror=fetch or --mirror=push instead");
140 static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
142 unsigned *mirror = opt->value;
143 if (not)
144 *mirror = MIRROR_NONE;
145 else if (!arg) {
146 warning("%s", _(mirror_advice));
147 *mirror = MIRROR_BOTH;
149 else if (!strcmp(arg, "fetch"))
150 *mirror = MIRROR_FETCH;
151 else if (!strcmp(arg, "push"))
152 *mirror = MIRROR_PUSH;
153 else
154 return error(_("unknown --mirror argument: %s"), arg);
155 return 0;
158 static int add(int argc, const char **argv, const char *prefix)
160 int fetch = 0, fetch_tags = TAGS_DEFAULT;
161 unsigned mirror = MIRROR_NONE;
162 struct string_list track = STRING_LIST_INIT_NODUP;
163 const char *master = NULL;
164 struct remote *remote;
165 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
166 const char *name, *url;
167 int i;
168 int result = 0;
170 struct option options[] = {
171 OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
172 OPT_SET_INT(0, "tags", &fetch_tags,
173 N_("import all tags and associated objects when fetching\n"
174 "or do not fetch any tag at all (--no-tags)"),
175 TAGS_SET),
176 OPT_STRING_LIST('t', "track", &track, N_("branch"),
177 N_("branch(es) to track")),
178 OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")),
179 OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)",
180 N_("set up remote as a mirror to push to or fetch from"),
181 PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),
182 OPT_END()
185 argc = parse_options(argc, argv, prefix, options,
186 builtin_remote_add_usage, 0);
188 if (argc != 2)
189 usage_with_options(builtin_remote_add_usage, options);
191 if (mirror && master)
192 die(_("specifying a master branch makes no sense with --mirror"));
193 if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
194 die(_("specifying branches to track makes sense only with fetch mirrors"));
196 name = argv[0];
197 url = argv[1];
199 remote = remote_get(name);
200 if (remote_is_configured(remote, 1)) {
201 error(_("remote %s already exists."), name);
202 exit(3);
205 if (!valid_remote_name(name))
206 die(_("'%s' is not a valid remote name"), name);
208 strbuf_addf(&buf, "remote.%s.url", name);
209 git_config_set(buf.buf, url);
211 if (!mirror || mirror & MIRROR_FETCH) {
212 strbuf_reset(&buf);
213 strbuf_addf(&buf, "remote.%s.fetch", name);
214 if (track.nr == 0)
215 string_list_append(&track, "*");
216 for (i = 0; i < track.nr; i++) {
217 add_branch(buf.buf, track.items[i].string,
218 name, mirror, &buf2);
222 if (mirror & MIRROR_PUSH) {
223 strbuf_reset(&buf);
224 strbuf_addf(&buf, "remote.%s.mirror", name);
225 git_config_set(buf.buf, "true");
228 if (fetch_tags != TAGS_DEFAULT) {
229 strbuf_reset(&buf);
230 strbuf_addf(&buf, "remote.%s.tagOpt", name);
231 git_config_set(buf.buf,
232 fetch_tags == TAGS_SET ? "--tags" : "--no-tags");
235 if (fetch && fetch_remote(name)) {
236 result = 1;
237 goto out;
240 if (master) {
241 strbuf_reset(&buf);
242 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
244 strbuf_reset(&buf2);
245 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
247 if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote add"))
248 result = error(_("Could not setup master '%s'"), master);
251 out:
252 strbuf_release(&buf);
253 strbuf_release(&buf2);
254 string_list_clear(&track, 0);
256 return result;
259 struct branch_info {
260 char *remote_name;
261 struct string_list merge;
262 enum rebase_type rebase;
263 char *push_remote_name;
266 static struct string_list branch_list = STRING_LIST_INIT_DUP;
268 static const char *abbrev_ref(const char *name, const char *prefix)
270 skip_prefix(name, prefix, &name);
271 return name;
273 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
275 static int config_read_branches(const char *key, const char *value,
276 const struct config_context *ctx UNUSED,
277 void *data UNUSED)
279 const char *orig_key = key;
280 char *name;
281 struct string_list_item *item;
282 struct branch_info *info;
283 enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type;
284 size_t key_len;
286 if (!starts_with(key, "branch."))
287 return 0;
289 key += strlen("branch.");
290 if (strip_suffix(key, ".remote", &key_len))
291 type = REMOTE;
292 else if (strip_suffix(key, ".merge", &key_len))
293 type = MERGE;
294 else if (strip_suffix(key, ".rebase", &key_len))
295 type = REBASE;
296 else if (strip_suffix(key, ".pushremote", &key_len))
297 type = PUSH_REMOTE;
298 else
299 return 0;
301 name = xmemdupz(key, key_len);
302 item = string_list_insert(&branch_list, name);
304 if (!item->util)
305 item->util = xcalloc(1, sizeof(struct branch_info));
306 info = item->util;
307 switch (type) {
308 case REMOTE:
309 if (info->remote_name)
310 warning(_("more than one %s"), orig_key);
311 info->remote_name = xstrdup(value);
312 break;
313 case MERGE: {
314 char *space = strchr(value, ' ');
315 value = abbrev_branch(value);
316 while (space) {
317 char *merge;
318 merge = xstrndup(value, space - value);
319 string_list_append(&info->merge, merge);
320 value = abbrev_branch(space + 1);
321 space = strchr(value, ' ');
323 string_list_append(&info->merge, xstrdup(value));
324 break;
326 case REBASE:
328 * Consider invalid values as false and check the
329 * truth value with >= REBASE_TRUE.
331 info->rebase = rebase_parse_value(value);
332 if (info->rebase == REBASE_INVALID)
333 warning(_("unhandled branch.%s.rebase=%s; assuming "
334 "'true'"), name, value);
335 break;
336 case PUSH_REMOTE:
337 if (info->push_remote_name)
338 warning(_("more than one %s"), orig_key);
339 info->push_remote_name = xstrdup(value);
340 break;
341 default:
342 BUG("unexpected type=%d", type);
345 free(name);
346 return 0;
349 static void read_branches(void)
351 if (branch_list.nr)
352 return;
353 git_config(config_read_branches, NULL);
356 struct ref_states {
357 struct remote *remote;
358 struct string_list new_refs, skipped, stale, tracked, heads, push;
359 int queried;
362 #define REF_STATES_INIT { \
363 .new_refs = STRING_LIST_INIT_DUP, \
364 .skipped = STRING_LIST_INIT_DUP, \
365 .stale = STRING_LIST_INIT_DUP, \
366 .tracked = STRING_LIST_INIT_DUP, \
367 .heads = STRING_LIST_INIT_DUP, \
368 .push = STRING_LIST_INIT_DUP, \
371 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
373 struct ref *fetch_map = NULL, **tail = &fetch_map;
374 struct ref *ref, *stale_refs;
375 int i;
377 for (i = 0; i < states->remote->fetch.nr; i++)
378 if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1))
379 die(_("Could not get fetch map for refspec %s"),
380 states->remote->fetch.raw[i]);
382 for (ref = fetch_map; ref; ref = ref->next) {
383 if (omit_name_by_refspec(ref->name, &states->remote->fetch))
384 string_list_append(&states->skipped, abbrev_branch(ref->name));
385 else if (!ref->peer_ref || !refs_ref_exists(get_main_ref_store(the_repository), ref->peer_ref->name))
386 string_list_append(&states->new_refs, abbrev_branch(ref->name));
387 else
388 string_list_append(&states->tracked, abbrev_branch(ref->name));
390 stale_refs = get_stale_heads(&states->remote->fetch, fetch_map);
391 for (ref = stale_refs; ref; ref = ref->next) {
392 struct string_list_item *item =
393 string_list_append(&states->stale, abbrev_branch(ref->name));
394 item->util = xstrdup(ref->name);
396 free_refs(stale_refs);
397 free_refs(fetch_map);
399 string_list_sort(&states->new_refs);
400 string_list_sort(&states->skipped);
401 string_list_sort(&states->tracked);
402 string_list_sort(&states->stale);
404 return 0;
407 struct push_info {
408 char *dest;
409 int forced;
410 enum {
411 PUSH_STATUS_CREATE = 0,
412 PUSH_STATUS_DELETE,
413 PUSH_STATUS_UPTODATE,
414 PUSH_STATUS_FASTFORWARD,
415 PUSH_STATUS_OUTOFDATE,
416 PUSH_STATUS_NOTQUERIED
417 } status;
420 static int get_push_ref_states(const struct ref *remote_refs,
421 struct ref_states *states)
423 struct remote *remote = states->remote;
424 struct ref *ref, *local_refs, *push_map;
425 if (remote->mirror)
426 return 0;
428 local_refs = get_local_heads();
429 push_map = copy_ref_list(remote_refs);
431 match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE);
433 for (ref = push_map; ref; ref = ref->next) {
434 struct string_list_item *item;
435 struct push_info *info;
437 if (!ref->peer_ref)
438 continue;
439 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
441 item = string_list_append(&states->push,
442 abbrev_branch(ref->peer_ref->name));
443 item->util = xcalloc(1, sizeof(struct push_info));
444 info = item->util;
445 info->forced = ref->force;
446 info->dest = xstrdup(abbrev_branch(ref->name));
448 if (is_null_oid(&ref->new_oid)) {
449 info->status = PUSH_STATUS_DELETE;
450 } else if (oideq(&ref->old_oid, &ref->new_oid))
451 info->status = PUSH_STATUS_UPTODATE;
452 else if (is_null_oid(&ref->old_oid))
453 info->status = PUSH_STATUS_CREATE;
454 else if (repo_has_object_file(the_repository, &ref->old_oid) &&
455 ref_newer(&ref->new_oid, &ref->old_oid))
456 info->status = PUSH_STATUS_FASTFORWARD;
457 else
458 info->status = PUSH_STATUS_OUTOFDATE;
460 free_refs(local_refs);
461 free_refs(push_map);
462 return 0;
465 static int get_push_ref_states_noquery(struct ref_states *states)
467 int i;
468 struct remote *remote = states->remote;
469 struct string_list_item *item;
470 struct push_info *info;
472 if (remote->mirror)
473 return 0;
475 if (!remote->push.nr) {
476 item = string_list_append(&states->push, _("(matching)"));
477 info = item->util = xcalloc(1, sizeof(struct push_info));
478 info->status = PUSH_STATUS_NOTQUERIED;
479 info->dest = xstrdup(item->string);
481 for (i = 0; i < remote->push.nr; i++) {
482 const struct refspec_item *spec = &remote->push.items[i];
483 if (spec->matching)
484 item = string_list_append(&states->push, _("(matching)"));
485 else if (strlen(spec->src))
486 item = string_list_append(&states->push, spec->src);
487 else
488 item = string_list_append(&states->push, _("(delete)"));
490 info = item->util = xcalloc(1, sizeof(struct push_info));
491 info->forced = spec->force;
492 info->status = PUSH_STATUS_NOTQUERIED;
493 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
495 return 0;
498 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
500 struct ref *ref, *matches;
501 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
502 struct refspec_item refspec = {
503 .force = 0,
504 .pattern = 1,
505 .src = (char *) "refs/heads/*",
506 .dst = (char *) "refs/heads/*",
509 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
510 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
511 fetch_map, 1);
512 for (ref = matches; ref; ref = ref->next)
513 string_list_append(&states->heads, abbrev_branch(ref->name));
515 free_refs(fetch_map);
516 free_refs(matches);
517 return 0;
520 struct known_remote {
521 struct known_remote *next;
522 struct remote *remote;
525 struct known_remotes {
526 struct remote *to_delete;
527 struct known_remote *list;
530 static int add_known_remote(struct remote *remote, void *cb_data)
532 struct known_remotes *all = cb_data;
533 struct known_remote *r;
535 if (!strcmp(all->to_delete->name, remote->name))
536 return 0;
538 r = xmalloc(sizeof(*r));
539 r->remote = remote;
540 r->next = all->list;
541 all->list = r;
542 return 0;
545 struct branches_for_remote {
546 struct remote *remote;
547 struct string_list *branches, *skipped;
548 struct known_remotes *keep;
551 static int add_branch_for_removal(const char *refname,
552 const char *referent UNUSED,
553 const struct object_id *oid UNUSED,
554 int flags UNUSED, void *cb_data)
556 struct branches_for_remote *branches = cb_data;
557 struct refspec_item refspec;
558 struct known_remote *kr;
560 memset(&refspec, 0, sizeof(refspec));
561 refspec.dst = (char *)refname;
562 if (remote_find_tracking(branches->remote, &refspec))
563 return 0;
564 free(refspec.src);
566 /* don't delete a branch if another remote also uses it */
567 for (kr = branches->keep->list; kr; kr = kr->next) {
568 memset(&refspec, 0, sizeof(refspec));
569 refspec.dst = (char *)refname;
570 if (!remote_find_tracking(kr->remote, &refspec)) {
571 free(refspec.src);
572 return 0;
576 /* don't delete non-remote-tracking refs */
577 if (!starts_with(refname, "refs/remotes/")) {
578 /* advise user how to delete local branches */
579 if (starts_with(refname, "refs/heads/"))
580 string_list_append(branches->skipped,
581 abbrev_branch(refname));
582 /* silently skip over other non-remote refs */
583 return 0;
586 string_list_append(branches->branches, refname);
588 return 0;
591 struct rename_info {
592 const char *old_name;
593 const char *new_name;
594 struct string_list *remote_branches;
595 uint32_t symrefs_nr;
598 static int read_remote_branches(const char *refname, const char *referent UNUSED,
599 const struct object_id *oid UNUSED,
600 int flags UNUSED, void *cb_data)
602 struct rename_info *rename = cb_data;
603 struct strbuf buf = STRBUF_INIT;
604 struct string_list_item *item;
605 int flag;
606 const char *symref;
608 strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name);
609 if (starts_with(refname, buf.buf)) {
610 item = string_list_append(rename->remote_branches, refname);
611 symref = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
612 refname, RESOLVE_REF_READING,
613 NULL, &flag);
614 if (symref && (flag & REF_ISSYMREF)) {
615 item->util = xstrdup(symref);
616 rename->symrefs_nr++;
617 } else {
618 item->util = NULL;
621 strbuf_release(&buf);
623 return 0;
626 static int migrate_file(struct remote *remote)
628 struct strbuf buf = STRBUF_INIT;
629 int i;
631 strbuf_addf(&buf, "remote.%s.url", remote->name);
632 for (i = 0; i < remote->url.nr; i++)
633 git_config_set_multivar(buf.buf, remote->url.v[i], "^$", 0);
634 strbuf_reset(&buf);
635 strbuf_addf(&buf, "remote.%s.push", remote->name);
636 for (i = 0; i < remote->push.raw_nr; i++)
637 git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0);
638 strbuf_reset(&buf);
639 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
640 for (i = 0; i < remote->fetch.raw_nr; i++)
641 git_config_set_multivar(buf.buf, remote->fetch.raw[i], "^$", 0);
642 if (remote->origin == REMOTE_REMOTES)
643 unlink_or_warn(git_path("remotes/%s", remote->name));
644 else if (remote->origin == REMOTE_BRANCHES)
645 unlink_or_warn(git_path("branches/%s", remote->name));
646 strbuf_release(&buf);
648 return 0;
651 struct push_default_info
653 const char *old_name;
654 enum config_scope scope;
655 struct strbuf origin;
656 int linenr;
659 static int config_read_push_default(const char *key, const char *value,
660 const struct config_context *ctx, void *cb)
662 const struct key_value_info *kvi = ctx->kvi;
664 struct push_default_info* info = cb;
665 if (strcmp(key, "remote.pushdefault") ||
666 !value || strcmp(value, info->old_name))
667 return 0;
669 info->scope = kvi->scope;
670 strbuf_reset(&info->origin);
671 strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type));
672 info->linenr = kvi->linenr;
674 return 0;
677 static void handle_push_default(const char* old_name, const char* new_name)
679 struct push_default_info push_default = {
680 .old_name = old_name,
681 .scope = CONFIG_SCOPE_UNKNOWN,
682 .origin = STRBUF_INIT,
683 .linenr = -1,
685 git_config(config_read_push_default, &push_default);
686 if (push_default.scope >= CONFIG_SCOPE_COMMAND)
687 ; /* pass */
688 else if (push_default.scope >= CONFIG_SCOPE_LOCAL) {
689 int result = git_config_set_gently("remote.pushDefault",
690 new_name);
691 if (new_name && result && result != CONFIG_NOTHING_SET)
692 die(_("could not set '%s'"), "remote.pushDefault");
693 else if (!new_name && result && result != CONFIG_NOTHING_SET)
694 die(_("could not unset '%s'"), "remote.pushDefault");
695 } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) {
696 /* warn */
697 warning(_("The %s configuration remote.pushDefault in:\n"
698 "\t%s:%d\n"
699 "now names the non-existent remote '%s'"),
700 config_scope_name(push_default.scope),
701 push_default.origin.buf, push_default.linenr,
702 old_name);
705 strbuf_release(&push_default.origin);
709 static int mv(int argc, const char **argv, const char *prefix)
711 int show_progress = isatty(2);
712 struct option options[] = {
713 OPT_BOOL(0, "progress", &show_progress, N_("force progress reporting")),
714 OPT_END()
716 struct remote *oldremote, *newremote;
717 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
718 old_remote_context = STRBUF_INIT;
719 struct string_list remote_branches = STRING_LIST_INIT_DUP;
720 struct rename_info rename;
721 int i, refs_renamed_nr = 0, refspec_updated = 0;
722 struct progress *progress = NULL;
723 int result = 0;
725 argc = parse_options(argc, argv, prefix, options,
726 builtin_remote_rename_usage, 0);
728 if (argc != 2)
729 usage_with_options(builtin_remote_rename_usage, options);
731 rename.old_name = argv[0];
732 rename.new_name = argv[1];
733 rename.remote_branches = &remote_branches;
734 rename.symrefs_nr = 0;
736 oldremote = remote_get(rename.old_name);
737 if (!remote_is_configured(oldremote, 1)) {
738 error(_("No such remote: '%s'"), rename.old_name);
739 exit(2);
742 if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
743 return migrate_file(oldremote);
745 newremote = remote_get(rename.new_name);
746 if (remote_is_configured(newremote, 1)) {
747 error(_("remote %s already exists."), rename.new_name);
748 exit(3);
751 if (!valid_remote_name(rename.new_name))
752 die(_("'%s' is not a valid remote name"), rename.new_name);
754 strbuf_addf(&buf, "remote.%s", rename.old_name);
755 strbuf_addf(&buf2, "remote.%s", rename.new_name);
756 if (repo_config_rename_section(the_repository, buf.buf, buf2.buf) < 1) {
757 result = error(_("Could not rename config section '%s' to '%s'"),
758 buf.buf, buf2.buf);
759 goto out;
762 if (oldremote->fetch.raw_nr) {
763 strbuf_reset(&buf);
764 strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
765 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
766 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
767 for (i = 0; i < oldremote->fetch.raw_nr; i++) {
768 char *ptr;
770 strbuf_reset(&buf2);
771 strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
772 ptr = strstr(buf2.buf, old_remote_context.buf);
773 if (ptr) {
774 refspec_updated = 1;
775 strbuf_splice(&buf2,
776 ptr-buf2.buf + strlen(":refs/remotes/"),
777 strlen(rename.old_name), rename.new_name,
778 strlen(rename.new_name));
779 } else
780 warning(_("Not updating non-default fetch refspec\n"
781 "\t%s\n"
782 "\tPlease update the configuration manually if necessary."),
783 buf2.buf);
785 git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
789 read_branches();
790 for (i = 0; i < branch_list.nr; i++) {
791 struct string_list_item *item = branch_list.items + i;
792 struct branch_info *info = item->util;
793 if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) {
794 strbuf_reset(&buf);
795 strbuf_addf(&buf, "branch.%s.remote", item->string);
796 git_config_set(buf.buf, rename.new_name);
798 if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) {
799 strbuf_reset(&buf);
800 strbuf_addf(&buf, "branch.%s.pushRemote", item->string);
801 git_config_set(buf.buf, rename.new_name);
805 if (!refspec_updated)
806 goto out;
809 * First remove symrefs, then rename the rest, finally create
810 * the new symrefs.
812 refs_for_each_ref(get_main_ref_store(the_repository),
813 read_remote_branches, &rename);
814 if (show_progress) {
816 * Count symrefs twice, since "renaming" them is done by
817 * deleting and recreating them in two separate passes.
819 progress = start_progress(_("Renaming remote references"),
820 rename.remote_branches->nr + rename.symrefs_nr);
822 for (i = 0; i < remote_branches.nr; i++) {
823 struct string_list_item *item = remote_branches.items + i;
824 struct strbuf referent = STRBUF_INIT;
826 if (refs_read_symbolic_ref(get_main_ref_store(the_repository), item->string,
827 &referent))
828 continue;
829 if (refs_delete_ref(get_main_ref_store(the_repository), NULL, item->string, NULL, REF_NO_DEREF))
830 die(_("deleting '%s' failed"), item->string);
832 strbuf_release(&referent);
833 display_progress(progress, ++refs_renamed_nr);
835 for (i = 0; i < remote_branches.nr; i++) {
836 struct string_list_item *item = remote_branches.items + i;
838 if (item->util)
839 continue;
840 strbuf_reset(&buf);
841 strbuf_addstr(&buf, item->string);
842 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
843 rename.new_name, strlen(rename.new_name));
844 strbuf_reset(&buf2);
845 strbuf_addf(&buf2, "remote: renamed %s to %s",
846 item->string, buf.buf);
847 if (refs_rename_ref(get_main_ref_store(the_repository), item->string, buf.buf, buf2.buf))
848 die(_("renaming '%s' failed"), item->string);
849 display_progress(progress, ++refs_renamed_nr);
851 for (i = 0; i < remote_branches.nr; i++) {
852 struct string_list_item *item = remote_branches.items + i;
854 if (!item->util)
855 continue;
856 strbuf_reset(&buf);
857 strbuf_addstr(&buf, item->string);
858 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
859 rename.new_name, strlen(rename.new_name));
860 strbuf_reset(&buf2);
861 strbuf_addstr(&buf2, item->util);
862 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name),
863 rename.new_name, strlen(rename.new_name));
864 strbuf_reset(&buf3);
865 strbuf_addf(&buf3, "remote: renamed %s to %s",
866 item->string, buf.buf);
867 if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, buf3.buf))
868 die(_("creating '%s' failed"), buf.buf);
869 display_progress(progress, ++refs_renamed_nr);
871 stop_progress(&progress);
873 handle_push_default(rename.old_name, rename.new_name);
875 out:
876 string_list_clear(&remote_branches, 1);
877 strbuf_release(&old_remote_context);
878 strbuf_release(&buf);
879 strbuf_release(&buf2);
880 strbuf_release(&buf3);
881 return result;
884 static int rm(int argc, const char **argv, const char *prefix)
886 struct option options[] = {
887 OPT_END()
889 struct remote *remote;
890 struct strbuf buf = STRBUF_INIT;
891 struct known_remotes known_remotes = { NULL, NULL };
892 struct string_list branches = STRING_LIST_INIT_DUP;
893 struct string_list skipped = STRING_LIST_INIT_DUP;
894 struct branches_for_remote cb_data;
895 int i, result;
897 memset(&cb_data, 0, sizeof(cb_data));
898 cb_data.branches = &branches;
899 cb_data.skipped = &skipped;
900 cb_data.keep = &known_remotes;
902 argc = parse_options(argc, argv, prefix, options,
903 builtin_remote_rm_usage, 0);
904 if (argc != 1)
905 usage_with_options(builtin_remote_rm_usage, options);
907 remote = remote_get(argv[0]);
908 if (!remote_is_configured(remote, 1)) {
909 error(_("No such remote: '%s'"), argv[0]);
910 exit(2);
913 known_remotes.to_delete = remote;
914 for_each_remote(add_known_remote, &known_remotes);
916 read_branches();
917 for (i = 0; i < branch_list.nr; i++) {
918 struct string_list_item *item = branch_list.items + i;
919 struct branch_info *info = item->util;
920 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
921 const char *keys[] = { "remote", "merge", NULL }, **k;
922 for (k = keys; *k; k++) {
923 strbuf_reset(&buf);
924 strbuf_addf(&buf, "branch.%s.%s",
925 item->string, *k);
926 result = git_config_set_gently(buf.buf, NULL);
927 if (result && result != CONFIG_NOTHING_SET)
928 die(_("could not unset '%s'"), buf.buf);
931 if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) {
932 strbuf_reset(&buf);
933 strbuf_addf(&buf, "branch.%s.pushremote", item->string);
934 result = git_config_set_gently(buf.buf, NULL);
935 if (result && result != CONFIG_NOTHING_SET)
936 die(_("could not unset '%s'"), buf.buf);
941 * We cannot just pass a function to for_each_ref() which deletes
942 * the branches one by one, since for_each_ref() relies on cached
943 * refs, which are invalidated when deleting a branch.
945 cb_data.remote = remote;
946 result = refs_for_each_ref(get_main_ref_store(the_repository),
947 add_branch_for_removal, &cb_data);
948 strbuf_release(&buf);
950 if (!result)
951 result = refs_delete_refs(get_main_ref_store(the_repository),
952 "remote: remove", &branches,
953 REF_NO_DEREF);
954 string_list_clear(&branches, 0);
956 if (skipped.nr) {
957 fprintf_ln(stderr,
958 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
959 "to delete it, use:",
960 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
961 "to delete them, use:",
962 skipped.nr));
963 for (i = 0; i < skipped.nr; i++)
964 fprintf(stderr, " git branch -d %s\n",
965 skipped.items[i].string);
967 string_list_clear(&skipped, 0);
969 if (!result) {
970 strbuf_addf(&buf, "remote.%s", remote->name);
971 if (repo_config_rename_section(the_repository, buf.buf, NULL) < 1) {
972 result = error(_("Could not remove config section '%s'"), buf.buf);
973 goto out;
976 handle_push_default(remote->name, NULL);
979 out:
980 for (struct known_remote *r = known_remotes.list; r;) {
981 struct known_remote *next = r->next;
982 free(r);
983 r = next;
985 strbuf_release(&buf);
986 return result;
989 static void clear_push_info(void *util, const char *string UNUSED)
991 struct push_info *info = util;
992 free(info->dest);
993 free(info);
996 static void free_remote_ref_states(struct ref_states *states)
998 string_list_clear(&states->new_refs, 0);
999 string_list_clear(&states->skipped, 0);
1000 string_list_clear(&states->stale, 1);
1001 string_list_clear(&states->tracked, 0);
1002 string_list_clear(&states->heads, 0);
1003 string_list_clear_func(&states->push, clear_push_info);
1006 static int append_ref_to_tracked_list(const char *refname,
1007 const char *referent UNUSED,
1008 const struct object_id *oid UNUSED,
1009 int flags, void *cb_data)
1011 struct ref_states *states = cb_data;
1012 struct refspec_item refspec;
1014 if (flags & REF_ISSYMREF)
1015 return 0;
1017 memset(&refspec, 0, sizeof(refspec));
1018 refspec.dst = (char *)refname;
1019 if (!remote_find_tracking(states->remote, &refspec)) {
1020 string_list_append(&states->tracked, abbrev_branch(refspec.src));
1021 free(refspec.src);
1024 return 0;
1027 static int get_remote_ref_states(const char *name,
1028 struct ref_states *states,
1029 int query)
1031 states->remote = remote_get(name);
1032 if (!states->remote)
1033 return error(_("No such remote: '%s'"), name);
1035 read_branches();
1037 if (query) {
1038 struct transport *transport;
1039 const struct ref *remote_refs;
1041 transport = transport_get(states->remote, states->remote->url.v[0]);
1042 remote_refs = transport_get_remote_refs(transport, NULL);
1044 states->queried = 1;
1045 if (query & GET_REF_STATES)
1046 get_ref_states(remote_refs, states);
1047 if (query & GET_HEAD_NAMES)
1048 get_head_names(remote_refs, states);
1049 if (query & GET_PUSH_REF_STATES)
1050 get_push_ref_states(remote_refs, states);
1051 transport_disconnect(transport);
1052 } else {
1053 refs_for_each_ref(get_main_ref_store(the_repository),
1054 append_ref_to_tracked_list, states);
1055 string_list_sort(&states->tracked);
1056 get_push_ref_states_noquery(states);
1059 return 0;
1062 struct show_info {
1063 struct string_list list;
1064 struct ref_states states;
1065 int width, width2;
1066 int any_rebase;
1069 #define SHOW_INFO_INIT { \
1070 .list = STRING_LIST_INIT_DUP, \
1071 .states = REF_STATES_INIT, \
1074 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
1076 struct show_info *info = cb_data;
1077 int n = strlen(item->string);
1078 if (n > info->width)
1079 info->width = n;
1080 string_list_insert(&info->list, item->string);
1081 return 0;
1084 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
1086 struct show_info *info = cb_data;
1087 struct ref_states *states = &info->states;
1088 const char *name = item->string;
1090 if (states->queried) {
1091 const char *fmt = "%s";
1092 const char *arg = "";
1093 if (string_list_has_string(&states->new_refs, name)) {
1094 fmt = _(" new (next fetch will store in remotes/%s)");
1095 arg = states->remote->name;
1096 } else if (string_list_has_string(&states->tracked, name))
1097 arg = _(" tracked");
1098 else if (string_list_has_string(&states->skipped, name))
1099 arg = _(" skipped");
1100 else if (string_list_has_string(&states->stale, name))
1101 arg = _(" stale (use 'git remote prune' to remove)");
1102 else
1103 arg = _(" ???");
1104 printf(" %-*s", info->width, name);
1105 printf(fmt, arg);
1106 printf("\n");
1107 } else
1108 printf(" %s\n", name);
1110 return 0;
1113 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
1115 struct show_info *show_info = cb_data;
1116 struct ref_states *states = &show_info->states;
1117 struct branch_info *branch_info = branch_item->util;
1118 struct string_list_item *item;
1119 int n;
1121 if (!branch_info->merge.nr || !branch_info->remote_name ||
1122 strcmp(states->remote->name, branch_info->remote_name))
1123 return 0;
1124 if ((n = strlen(branch_item->string)) > show_info->width)
1125 show_info->width = n;
1126 if (branch_info->rebase >= REBASE_TRUE)
1127 show_info->any_rebase = 1;
1129 item = string_list_insert(&show_info->list, branch_item->string);
1130 item->util = branch_info;
1132 return 0;
1135 static int show_local_info_item(struct string_list_item *item, void *cb_data)
1137 struct show_info *show_info = cb_data;
1138 struct branch_info *branch_info = item->util;
1139 struct string_list *merge = &branch_info->merge;
1140 int width = show_info->width + 4;
1141 int i;
1143 if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) {
1144 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1145 item->string);
1146 return 0;
1149 printf(" %-*s ", show_info->width, item->string);
1150 if (branch_info->rebase >= REBASE_TRUE) {
1151 const char *msg;
1152 if (branch_info->rebase == REBASE_INTERACTIVE)
1153 msg = _("rebases interactively onto remote %s");
1154 else if (branch_info->rebase == REBASE_MERGES)
1155 msg = _("rebases interactively (with merges) onto "
1156 "remote %s");
1157 else
1158 msg = _("rebases onto remote %s");
1159 printf_ln(msg, merge->items[0].string);
1160 return 0;
1161 } else if (show_info->any_rebase) {
1162 printf_ln(_(" merges with remote %s"), merge->items[0].string);
1163 width++;
1164 } else {
1165 printf_ln(_("merges with remote %s"), merge->items[0].string);
1167 for (i = 1; i < merge->nr; i++)
1168 printf(_("%-*s and with remote %s\n"), width, "",
1169 merge->items[i].string);
1171 return 0;
1174 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
1176 struct show_info *show_info = cb_data;
1177 struct push_info *push_info = push_item->util;
1178 struct string_list_item *item;
1179 int n;
1180 if ((n = strlen(push_item->string)) > show_info->width)
1181 show_info->width = n;
1182 if ((n = strlen(push_info->dest)) > show_info->width2)
1183 show_info->width2 = n;
1184 item = string_list_append(&show_info->list, push_item->string);
1185 item->util = push_item->util;
1186 return 0;
1190 * Sorting comparison for a string list that has push_info
1191 * structs in its util field
1193 static int cmp_string_with_push(const void *va, const void *vb)
1195 const struct string_list_item *a = va;
1196 const struct string_list_item *b = vb;
1197 const struct push_info *a_push = a->util;
1198 const struct push_info *b_push = b->util;
1199 int cmp = strcmp(a->string, b->string);
1200 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1203 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1205 struct show_info *show_info = cb_data;
1206 struct push_info *push_info = item->util;
1207 const char *src = item->string, *status = NULL;
1209 switch (push_info->status) {
1210 case PUSH_STATUS_CREATE:
1211 status = _("create");
1212 break;
1213 case PUSH_STATUS_DELETE:
1214 status = _("delete");
1215 src = _("(none)");
1216 break;
1217 case PUSH_STATUS_UPTODATE:
1218 status = _("up to date");
1219 break;
1220 case PUSH_STATUS_FASTFORWARD:
1221 status = _("fast-forwardable");
1222 break;
1223 case PUSH_STATUS_OUTOFDATE:
1224 status = _("local out of date");
1225 break;
1226 case PUSH_STATUS_NOTQUERIED:
1227 break;
1229 if (status) {
1230 if (push_info->forced)
1231 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1232 show_info->width2, push_info->dest, status);
1233 else
1234 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1235 show_info->width2, push_info->dest, status);
1236 } else {
1237 if (push_info->forced)
1238 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1239 push_info->dest);
1240 else
1241 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1242 push_info->dest);
1244 return 0;
1247 static int get_one_entry(struct remote *remote, void *priv)
1249 struct string_list *list = priv;
1250 struct strbuf remote_info_buf = STRBUF_INIT;
1251 struct strvec *url;
1252 int i;
1254 if (remote->url.nr > 0) {
1255 struct strbuf promisor_config = STRBUF_INIT;
1256 const char *partial_clone_filter = NULL;
1258 strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
1259 strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url.v[0]);
1260 if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
1261 strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
1263 strbuf_release(&promisor_config);
1264 string_list_append(list, remote->name)->util =
1265 strbuf_detach(&remote_info_buf, NULL);
1266 } else
1267 string_list_append(list, remote->name)->util = NULL;
1268 url = push_url_of_remote(remote);
1269 for (i = 0; i < url->nr; i++)
1271 strbuf_addf(&remote_info_buf, "%s (push)", url->v[i]);
1272 string_list_append(list, remote->name)->util =
1273 strbuf_detach(&remote_info_buf, NULL);
1276 return 0;
1279 static int show_all(void)
1281 struct string_list list = STRING_LIST_INIT_DUP;
1282 int result;
1284 result = for_each_remote(get_one_entry, &list);
1286 if (!result) {
1287 int i;
1289 string_list_sort(&list);
1290 for (i = 0; i < list.nr; i++) {
1291 struct string_list_item *item = list.items + i;
1292 if (verbose)
1293 printf("%s\t%s\n", item->string,
1294 item->util ? (const char *)item->util : "");
1295 else {
1296 if (i && !strcmp((item - 1)->string, item->string))
1297 continue;
1298 printf("%s\n", item->string);
1302 string_list_clear(&list, 1);
1303 return result;
1306 static int show(int argc, const char **argv, const char *prefix)
1308 int no_query = 0, result = 0, query_flag = 0;
1309 struct option options[] = {
1310 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1311 OPT_END()
1313 struct show_info info = SHOW_INFO_INIT;
1315 argc = parse_options(argc, argv, prefix, options,
1316 builtin_remote_show_usage,
1319 if (argc < 1)
1320 return show_all();
1322 if (!no_query)
1323 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1325 for (; argc; argc--, argv++) {
1326 int i;
1327 struct strvec *url;
1329 get_remote_ref_states(*argv, &info.states, query_flag);
1331 printf_ln(_("* remote %s"), *argv);
1332 printf_ln(_(" Fetch URL: %s"), info.states.remote->url.v[0]);
1333 url = push_url_of_remote(info.states.remote);
1334 for (i = 0; i < url->nr; i++)
1336 * TRANSLATORS: the colon ':' should align
1337 * with the one in " Fetch URL: %s"
1338 * translation.
1340 printf_ln(_(" Push URL: %s"), url->v[i]);
1341 if (!i)
1342 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1343 if (no_query)
1344 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1345 else if (!info.states.heads.nr)
1346 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1347 else if (info.states.heads.nr == 1)
1348 printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string);
1349 else {
1350 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1351 " may be one of the following):\n"));
1352 for (i = 0; i < info.states.heads.nr; i++)
1353 printf(" %s\n", info.states.heads.items[i].string);
1356 /* remote branch info */
1357 info.width = 0;
1358 for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info);
1359 for_each_string_list(&info.states.skipped, add_remote_to_show_info, &info);
1360 for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info);
1361 for_each_string_list(&info.states.stale, add_remote_to_show_info, &info);
1362 if (info.list.nr)
1363 printf_ln(Q_(" Remote branch:%s",
1364 " Remote branches:%s",
1365 info.list.nr),
1366 no_query ? _(" (status not queried)") : "");
1367 for_each_string_list(&info.list, show_remote_info_item, &info);
1368 string_list_clear(&info.list, 0);
1370 /* git pull info */
1371 info.width = 0;
1372 info.any_rebase = 0;
1373 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1374 if (info.list.nr)
1375 printf_ln(Q_(" Local branch configured for 'git pull':",
1376 " Local branches configured for 'git pull':",
1377 info.list.nr));
1378 for_each_string_list(&info.list, show_local_info_item, &info);
1379 string_list_clear(&info.list, 0);
1381 /* git push info */
1382 if (info.states.remote->mirror)
1383 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1385 info.width = info.width2 = 0;
1386 for_each_string_list(&info.states.push, add_push_to_show_info, &info);
1387 QSORT(info.list.items, info.list.nr, cmp_string_with_push);
1388 if (info.list.nr)
1389 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1390 " Local refs configured for 'git push'%s:",
1391 info.list.nr),
1392 no_query ? _(" (status not queried)") : "");
1393 for_each_string_list(&info.list, show_push_info_item, &info);
1394 string_list_clear(&info.list, 0);
1396 free_remote_ref_states(&info.states);
1399 return result;
1402 static int set_head(int argc, const char **argv, const char *prefix)
1404 int i, opt_a = 0, opt_d = 0, result = 0;
1405 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1406 char *head_name = NULL;
1408 struct option options[] = {
1409 OPT_BOOL('a', "auto", &opt_a,
1410 N_("set refs/remotes/<name>/HEAD according to remote")),
1411 OPT_BOOL('d', "delete", &opt_d,
1412 N_("delete refs/remotes/<name>/HEAD")),
1413 OPT_END()
1415 argc = parse_options(argc, argv, prefix, options,
1416 builtin_remote_sethead_usage, 0);
1417 if (argc)
1418 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1420 if (!opt_a && !opt_d && argc == 2) {
1421 head_name = xstrdup(argv[1]);
1422 } else if (opt_a && !opt_d && argc == 1) {
1423 struct ref_states states = REF_STATES_INIT;
1424 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1425 if (!states.heads.nr)
1426 result |= error(_("Cannot determine remote HEAD"));
1427 else if (states.heads.nr > 1) {
1428 result |= error(_("Multiple remote HEAD branches. "
1429 "Please choose one explicitly with:"));
1430 for (i = 0; i < states.heads.nr; i++)
1431 fprintf(stderr, " git remote set-head %s %s\n",
1432 argv[0], states.heads.items[i].string);
1433 } else
1434 head_name = xstrdup(states.heads.items[0].string);
1435 free_remote_ref_states(&states);
1436 } else if (opt_d && !opt_a && argc == 1) {
1437 if (refs_delete_ref(get_main_ref_store(the_repository), NULL, buf.buf, NULL, REF_NO_DEREF))
1438 result |= error(_("Could not delete %s"), buf.buf);
1439 } else
1440 usage_with_options(builtin_remote_sethead_usage, options);
1442 if (head_name) {
1443 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1444 /* make sure it's valid */
1445 if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
1446 result |= error(_("Not a valid ref: %s"), buf2.buf);
1447 else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
1448 result |= error(_("Could not setup %s"), buf.buf);
1449 else if (opt_a)
1450 printf("%s/HEAD set to %s\n", argv[0], head_name);
1451 free(head_name);
1454 strbuf_release(&buf);
1455 strbuf_release(&buf2);
1456 return result;
1459 static int prune_remote(const char *remote, int dry_run)
1461 int result = 0;
1462 struct ref_states states = REF_STATES_INIT;
1463 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1464 struct string_list_item *item;
1465 const char *dangling_msg = dry_run
1466 ? _(" %s will become dangling!")
1467 : _(" %s has become dangling!");
1469 get_remote_ref_states(remote, &states, GET_REF_STATES);
1471 if (!states.stale.nr) {
1472 free_remote_ref_states(&states);
1473 return 0;
1476 printf_ln(_("Pruning %s"), remote);
1477 printf_ln(_("URL: %s"), states.remote->url.v[0]);
1479 for_each_string_list_item(item, &states.stale)
1480 string_list_append(&refs_to_prune, item->util);
1481 string_list_sort(&refs_to_prune);
1483 if (!dry_run)
1484 result |= refs_delete_refs(get_main_ref_store(the_repository),
1485 "remote: prune", &refs_to_prune, 0);
1487 for_each_string_list_item(item, &states.stale) {
1488 const char *refname = item->util;
1490 if (dry_run)
1491 printf_ln(_(" * [would prune] %s"),
1492 abbrev_ref(refname, "refs/remotes/"));
1493 else
1494 printf_ln(_(" * [pruned] %s"),
1495 abbrev_ref(refname, "refs/remotes/"));
1498 refs_warn_dangling_symrefs(get_main_ref_store(the_repository),
1499 stdout, dangling_msg, &refs_to_prune);
1501 string_list_clear(&refs_to_prune, 0);
1502 free_remote_ref_states(&states);
1503 return result;
1506 static int prune(int argc, const char **argv, const char *prefix)
1508 int dry_run = 0, result = 0;
1509 struct option options[] = {
1510 OPT__DRY_RUN(&dry_run, N_("dry run")),
1511 OPT_END()
1514 argc = parse_options(argc, argv, prefix, options,
1515 builtin_remote_prune_usage, 0);
1517 if (argc < 1)
1518 usage_with_options(builtin_remote_prune_usage, options);
1520 for (; argc; argc--, argv++)
1521 result |= prune_remote(*argv, dry_run);
1523 return result;
1526 static int get_remote_default(const char *key, const char *value UNUSED,
1527 const struct config_context *ctx UNUSED,
1528 void *priv)
1530 if (strcmp(key, "remotes.default") == 0) {
1531 int *found = priv;
1532 *found = 1;
1534 return 0;
1537 static int update(int argc, const char **argv, const char *prefix)
1539 int i, prune = -1;
1540 struct option options[] = {
1541 OPT_BOOL('p', "prune", &prune,
1542 N_("prune remotes after fetching")),
1543 OPT_END()
1545 struct child_process cmd = CHILD_PROCESS_INIT;
1546 int default_defined = 0;
1548 argc = parse_options(argc, argv, prefix, options,
1549 builtin_remote_update_usage,
1550 PARSE_OPT_KEEP_ARGV0);
1552 strvec_push(&cmd.args, "fetch");
1554 if (prune != -1)
1555 strvec_push(&cmd.args, prune ? "--prune" : "--no-prune");
1556 if (verbose)
1557 strvec_push(&cmd.args, "-v");
1558 strvec_push(&cmd.args, "--multiple");
1559 if (argc < 2)
1560 strvec_push(&cmd.args, "default");
1561 for (i = 1; i < argc; i++)
1562 strvec_push(&cmd.args, argv[i]);
1564 if (strcmp(cmd.args.v[cmd.args.nr-1], "default") == 0) {
1565 git_config(get_remote_default, &default_defined);
1566 if (!default_defined) {
1567 strvec_pop(&cmd.args);
1568 strvec_push(&cmd.args, "--all");
1572 cmd.git_cmd = 1;
1573 return run_command(&cmd);
1576 static int remove_all_fetch_refspecs(const char *key)
1578 return git_config_set_multivar_gently(key, NULL, NULL,
1579 CONFIG_FLAGS_MULTI_REPLACE);
1582 static void add_branches(struct remote *remote, const char **branches,
1583 const char *key)
1585 const char *remotename = remote->name;
1586 int mirror = remote->mirror;
1587 struct strbuf refspec = STRBUF_INIT;
1589 for (; *branches; branches++)
1590 add_branch(key, *branches, remotename, mirror, &refspec);
1592 strbuf_release(&refspec);
1595 static int set_remote_branches(const char *remotename, const char **branches,
1596 int add_mode)
1598 struct strbuf key = STRBUF_INIT;
1599 struct remote *remote;
1601 strbuf_addf(&key, "remote.%s.fetch", remotename);
1603 remote = remote_get(remotename);
1604 if (!remote_is_configured(remote, 1)) {
1605 error(_("No such remote '%s'"), remotename);
1606 exit(2);
1609 if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
1610 strbuf_release(&key);
1611 return 1;
1613 add_branches(remote, branches, key.buf);
1615 strbuf_release(&key);
1616 return 0;
1619 static int set_branches(int argc, const char **argv, const char *prefix)
1621 int add_mode = 0;
1622 struct option options[] = {
1623 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1624 OPT_END()
1627 argc = parse_options(argc, argv, prefix, options,
1628 builtin_remote_setbranches_usage, 0);
1629 if (argc == 0) {
1630 error(_("no remote specified"));
1631 usage_with_options(builtin_remote_setbranches_usage, options);
1633 argv[argc] = NULL;
1635 return set_remote_branches(argv[0], argv + 1, add_mode);
1638 static int get_url(int argc, const char **argv, const char *prefix)
1640 int i, push_mode = 0, all_mode = 0;
1641 const char *remotename = NULL;
1642 struct remote *remote;
1643 struct strvec *url;
1644 struct option options[] = {
1645 OPT_BOOL('\0', "push", &push_mode,
1646 N_("query push URLs rather than fetch URLs")),
1647 OPT_BOOL('\0', "all", &all_mode,
1648 N_("return all URLs")),
1649 OPT_END()
1651 argc = parse_options(argc, argv, prefix, options,
1652 builtin_remote_geturl_usage, 0);
1654 if (argc != 1)
1655 usage_with_options(builtin_remote_geturl_usage, options);
1657 remotename = argv[0];
1659 remote = remote_get(remotename);
1660 if (!remote_is_configured(remote, 1)) {
1661 error(_("No such remote '%s'"), remotename);
1662 exit(2);
1665 url = push_mode ? push_url_of_remote(remote) : &remote->url;
1667 if (all_mode) {
1668 for (i = 0; i < url->nr; i++)
1669 printf_ln("%s", url->v[i]);
1670 } else {
1671 printf_ln("%s", url->v[0]);
1674 return 0;
1677 static int set_url(int argc, const char **argv, const char *prefix)
1679 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1680 int matches = 0, negative_matches = 0;
1681 const char *remotename = NULL;
1682 const char *newurl = NULL;
1683 const char *oldurl = NULL;
1684 struct remote *remote;
1685 regex_t old_regex;
1686 struct strvec *urlset;
1687 struct strbuf name_buf = STRBUF_INIT;
1688 struct option options[] = {
1689 OPT_BOOL('\0', "push", &push_mode,
1690 N_("manipulate push URLs")),
1691 OPT_BOOL('\0', "add", &add_mode,
1692 N_("add URL")),
1693 OPT_BOOL('\0', "delete", &delete_mode,
1694 N_("delete URLs")),
1695 OPT_END()
1697 argc = parse_options(argc, argv, prefix, options,
1698 builtin_remote_seturl_usage,
1699 PARSE_OPT_KEEP_ARGV0);
1701 if (add_mode && delete_mode)
1702 die(_("--add --delete doesn't make sense"));
1704 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1705 usage_with_options(builtin_remote_seturl_usage, options);
1707 remotename = argv[1];
1708 newurl = argv[2];
1709 if (argc > 3)
1710 oldurl = argv[3];
1712 if (delete_mode)
1713 oldurl = newurl;
1715 remote = remote_get(remotename);
1716 if (!remote_is_configured(remote, 1)) {
1717 error(_("No such remote '%s'"), remotename);
1718 exit(2);
1721 if (push_mode) {
1722 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1723 urlset = &remote->pushurl;
1724 } else {
1725 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1726 urlset = &remote->url;
1729 /* Special cases that add new entry. */
1730 if ((!oldurl && !delete_mode) || add_mode) {
1731 if (add_mode)
1732 git_config_set_multivar(name_buf.buf, newurl,
1733 "^$", 0);
1734 else
1735 git_config_set(name_buf.buf, newurl);
1736 goto out;
1739 /* Old URL specified. Demand that one matches. */
1740 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1741 die(_("Invalid old URL pattern: %s"), oldurl);
1743 for (i = 0; i < urlset->nr; i++)
1744 if (!regexec(&old_regex, urlset->v[i], 0, NULL, 0))
1745 matches++;
1746 else
1747 negative_matches++;
1748 if (!delete_mode && !matches)
1749 die(_("No such URL found: %s"), oldurl);
1750 if (delete_mode && !negative_matches && !push_mode)
1751 die(_("Will not delete all non-push URLs"));
1753 regfree(&old_regex);
1755 if (!delete_mode)
1756 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1757 else
1758 git_config_set_multivar(name_buf.buf, NULL, oldurl,
1759 CONFIG_FLAGS_MULTI_REPLACE);
1760 out:
1761 strbuf_release(&name_buf);
1762 return 0;
1765 int cmd_remote(int argc,
1766 const char **argv,
1767 const char *prefix,
1768 struct repository *repo UNUSED)
1770 parse_opt_subcommand_fn *fn = NULL;
1771 struct option options[] = {
1772 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1773 OPT_SUBCOMMAND("add", &fn, add),
1774 OPT_SUBCOMMAND("rename", &fn, mv),
1775 OPT_SUBCOMMAND_F("rm", &fn, rm, PARSE_OPT_NOCOMPLETE),
1776 OPT_SUBCOMMAND("remove", &fn, rm),
1777 OPT_SUBCOMMAND("set-head", &fn, set_head),
1778 OPT_SUBCOMMAND("set-branches", &fn, set_branches),
1779 OPT_SUBCOMMAND("get-url", &fn, get_url),
1780 OPT_SUBCOMMAND("set-url", &fn, set_url),
1781 OPT_SUBCOMMAND("show", &fn, show),
1782 OPT_SUBCOMMAND("prune", &fn, prune),
1783 OPT_SUBCOMMAND("update", &fn, update),
1784 OPT_END()
1787 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1788 PARSE_OPT_SUBCOMMAND_OPTIONAL);
1790 if (fn) {
1791 return !!fn(argc, argv, prefix);
1792 } else {
1793 if (argc) {
1794 error(_("unknown subcommand: `%s'"), argv[0]);
1795 usage_with_options(builtin_remote_usage, options);
1797 return !!show_all();