reftable/merged: handle subiter cleanup on close only
[alt-git.git] / builtin / remote.c
blobd91bbe728d739e074e5f4fa54f70c186f371c1b7
1 #include "builtin.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "parse-options.h"
5 #include "path.h"
6 #include "transport.h"
7 #include "remote.h"
8 #include "string-list.h"
9 #include "strbuf.h"
10 #include "run-command.h"
11 #include "rebase.h"
12 #include "refs.h"
13 #include "refspec.h"
14 #include "object-store-ll.h"
15 #include "strvec.h"
16 #include "commit-reach.h"
17 #include "progress.h"
19 static const char * const builtin_remote_usage[] = {
20 "git remote [-v | --verbose]",
21 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
22 N_("git remote rename [--[no-]progress] <old> <new>"),
23 N_("git remote remove <name>"),
24 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
25 N_("git remote [-v | --verbose] show [-n] <name>"),
26 N_("git remote prune [-n | --dry-run] <name>"),
27 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
28 N_("git remote set-branches [--add] <name> <branch>..."),
29 N_("git remote get-url [--push] [--all] <name>"),
30 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
31 N_("git remote set-url --add <name> <newurl>"),
32 N_("git remote set-url --delete <name> <url>"),
33 NULL
36 static const char * const builtin_remote_add_usage[] = {
37 N_("git remote add [<options>] <name> <url>"),
38 NULL
41 static const char * const builtin_remote_rename_usage[] = {
42 N_("git remote rename [--[no-]progress] <old> <new>"),
43 NULL
46 static const char * const builtin_remote_rm_usage[] = {
47 N_("git remote remove <name>"),
48 NULL
51 static const char * const builtin_remote_sethead_usage[] = {
52 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
53 NULL
56 static const char * const builtin_remote_setbranches_usage[] = {
57 N_("git remote set-branches <name> <branch>..."),
58 N_("git remote set-branches --add <name> <branch>..."),
59 NULL
62 static const char * const builtin_remote_show_usage[] = {
63 N_("git remote show [<options>] <name>"),
64 NULL
67 static const char * const builtin_remote_prune_usage[] = {
68 N_("git remote prune [<options>] <name>"),
69 NULL
72 static const char * const builtin_remote_update_usage[] = {
73 N_("git remote update [<options>] [<group> | <remote>]..."),
74 NULL
77 static const char * const builtin_remote_geturl_usage[] = {
78 N_("git remote get-url [--push] [--all] <name>"),
79 NULL
82 static const char * const builtin_remote_seturl_usage[] = {
83 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
84 N_("git remote set-url --add <name> <newurl>"),
85 N_("git remote set-url --delete <name> <url>"),
86 NULL
89 #define GET_REF_STATES (1<<0)
90 #define GET_HEAD_NAMES (1<<1)
91 #define GET_PUSH_REF_STATES (1<<2)
93 static int verbose;
95 static int fetch_remote(const char *name)
97 struct child_process cmd = CHILD_PROCESS_INIT;
99 strvec_push(&cmd.args, "fetch");
100 if (verbose)
101 strvec_push(&cmd.args, "-v");
102 strvec_push(&cmd.args, name);
103 cmd.git_cmd = 1;
104 printf_ln(_("Updating %s"), name);
105 if (run_command(&cmd))
106 return error(_("Could not fetch %s"), name);
107 return 0;
110 enum {
111 TAGS_UNSET = 0,
112 TAGS_DEFAULT = 1,
113 TAGS_SET = 2
116 #define MIRROR_NONE 0
117 #define MIRROR_FETCH 1
118 #define MIRROR_PUSH 2
119 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
121 static void add_branch(const char *key, const char *branchname,
122 const char *remotename, int mirror, struct strbuf *tmp)
124 strbuf_reset(tmp);
125 strbuf_addch(tmp, '+');
126 if (mirror)
127 strbuf_addf(tmp, "refs/%s:refs/%s",
128 branchname, branchname);
129 else
130 strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
131 branchname, remotename, branchname);
132 git_config_set_multivar(key, tmp->buf, "^$", 0);
135 static const char mirror_advice[] =
136 N_("--mirror is dangerous and deprecated; please\n"
137 "\t use --mirror=fetch or --mirror=push instead");
139 static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
141 unsigned *mirror = opt->value;
142 if (not)
143 *mirror = MIRROR_NONE;
144 else if (!arg) {
145 warning("%s", _(mirror_advice));
146 *mirror = MIRROR_BOTH;
148 else if (!strcmp(arg, "fetch"))
149 *mirror = MIRROR_FETCH;
150 else if (!strcmp(arg, "push"))
151 *mirror = MIRROR_PUSH;
152 else
153 return error(_("unknown mirror argument: %s"), arg);
154 return 0;
157 static int add(int argc, const char **argv, const char *prefix)
159 int fetch = 0, fetch_tags = TAGS_DEFAULT;
160 unsigned mirror = MIRROR_NONE;
161 struct string_list track = STRING_LIST_INIT_NODUP;
162 const char *master = NULL;
163 struct remote *remote;
164 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
165 const char *name, *url;
166 int i;
168 struct option options[] = {
169 OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
170 OPT_SET_INT(0, "tags", &fetch_tags,
171 N_("import all tags and associated objects when fetching\n"
172 "or do not fetch any tag at all (--no-tags)"),
173 TAGS_SET),
174 OPT_STRING_LIST('t', "track", &track, N_("branch"),
175 N_("branch(es) to track")),
176 OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")),
177 OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)",
178 N_("set up remote as a mirror to push to or fetch from"),
179 PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),
180 OPT_END()
183 argc = parse_options(argc, argv, prefix, options,
184 builtin_remote_add_usage, 0);
186 if (argc != 2)
187 usage_with_options(builtin_remote_add_usage, options);
189 if (mirror && master)
190 die(_("specifying a master branch makes no sense with --mirror"));
191 if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
192 die(_("specifying branches to track makes sense only with fetch mirrors"));
194 name = argv[0];
195 url = argv[1];
197 remote = remote_get(name);
198 if (remote_is_configured(remote, 1)) {
199 error(_("remote %s already exists."), name);
200 exit(3);
203 if (!valid_remote_name(name))
204 die(_("'%s' is not a valid remote name"), name);
206 strbuf_addf(&buf, "remote.%s.url", name);
207 git_config_set(buf.buf, url);
209 if (!mirror || mirror & MIRROR_FETCH) {
210 strbuf_reset(&buf);
211 strbuf_addf(&buf, "remote.%s.fetch", name);
212 if (track.nr == 0)
213 string_list_append(&track, "*");
214 for (i = 0; i < track.nr; i++) {
215 add_branch(buf.buf, track.items[i].string,
216 name, mirror, &buf2);
220 if (mirror & MIRROR_PUSH) {
221 strbuf_reset(&buf);
222 strbuf_addf(&buf, "remote.%s.mirror", name);
223 git_config_set(buf.buf, "true");
226 if (fetch_tags != TAGS_DEFAULT) {
227 strbuf_reset(&buf);
228 strbuf_addf(&buf, "remote.%s.tagOpt", name);
229 git_config_set(buf.buf,
230 fetch_tags == TAGS_SET ? "--tags" : "--no-tags");
233 if (fetch && fetch_remote(name))
234 return 1;
236 if (master) {
237 strbuf_reset(&buf);
238 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
240 strbuf_reset(&buf2);
241 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
243 if (create_symref(buf.buf, buf2.buf, "remote add"))
244 return error(_("Could not setup master '%s'"), master);
247 strbuf_release(&buf);
248 strbuf_release(&buf2);
249 string_list_clear(&track, 0);
251 return 0;
254 struct branch_info {
255 char *remote_name;
256 struct string_list merge;
257 enum rebase_type rebase;
258 char *push_remote_name;
261 static struct string_list branch_list = STRING_LIST_INIT_NODUP;
263 static const char *abbrev_ref(const char *name, const char *prefix)
265 skip_prefix(name, prefix, &name);
266 return name;
268 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
270 static int config_read_branches(const char *key, const char *value,
271 const struct config_context *ctx UNUSED,
272 void *data UNUSED)
274 const char *orig_key = key;
275 char *name;
276 struct string_list_item *item;
277 struct branch_info *info;
278 enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type;
279 size_t key_len;
281 if (!starts_with(key, "branch."))
282 return 0;
284 key += strlen("branch.");
285 if (strip_suffix(key, ".remote", &key_len))
286 type = REMOTE;
287 else if (strip_suffix(key, ".merge", &key_len))
288 type = MERGE;
289 else if (strip_suffix(key, ".rebase", &key_len))
290 type = REBASE;
291 else if (strip_suffix(key, ".pushremote", &key_len))
292 type = PUSH_REMOTE;
293 else
294 return 0;
295 name = xmemdupz(key, key_len);
297 item = string_list_insert(&branch_list, name);
299 if (!item->util)
300 item->util = xcalloc(1, sizeof(struct branch_info));
301 info = item->util;
302 switch (type) {
303 case REMOTE:
304 if (info->remote_name)
305 warning(_("more than one %s"), orig_key);
306 info->remote_name = xstrdup(value);
307 break;
308 case MERGE: {
309 char *space = strchr(value, ' ');
310 value = abbrev_branch(value);
311 while (space) {
312 char *merge;
313 merge = xstrndup(value, space - value);
314 string_list_append(&info->merge, merge);
315 value = abbrev_branch(space + 1);
316 space = strchr(value, ' ');
318 string_list_append(&info->merge, xstrdup(value));
319 break;
321 case REBASE:
323 * Consider invalid values as false and check the
324 * truth value with >= REBASE_TRUE.
326 info->rebase = rebase_parse_value(value);
327 if (info->rebase == REBASE_INVALID)
328 warning(_("unhandled branch.%s.rebase=%s; assuming "
329 "'true'"), name, value);
330 break;
331 case PUSH_REMOTE:
332 if (info->push_remote_name)
333 warning(_("more than one %s"), orig_key);
334 info->push_remote_name = xstrdup(value);
335 break;
336 default:
337 BUG("unexpected type=%d", type);
340 return 0;
343 static void read_branches(void)
345 if (branch_list.nr)
346 return;
347 git_config(config_read_branches, NULL);
350 struct ref_states {
351 struct remote *remote;
352 struct string_list new_refs, skipped, stale, tracked, heads, push;
353 int queried;
356 #define REF_STATES_INIT { \
357 .new_refs = STRING_LIST_INIT_DUP, \
358 .skipped = STRING_LIST_INIT_DUP, \
359 .stale = STRING_LIST_INIT_DUP, \
360 .tracked = STRING_LIST_INIT_DUP, \
361 .heads = STRING_LIST_INIT_DUP, \
362 .push = STRING_LIST_INIT_DUP, \
365 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
367 struct ref *fetch_map = NULL, **tail = &fetch_map;
368 struct ref *ref, *stale_refs;
369 int i;
371 for (i = 0; i < states->remote->fetch.nr; i++)
372 if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1))
373 die(_("Could not get fetch map for refspec %s"),
374 states->remote->fetch.raw[i]);
376 for (ref = fetch_map; ref; ref = ref->next) {
377 if (omit_name_by_refspec(ref->name, &states->remote->fetch))
378 string_list_append(&states->skipped, abbrev_branch(ref->name));
379 else if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
380 string_list_append(&states->new_refs, abbrev_branch(ref->name));
381 else
382 string_list_append(&states->tracked, abbrev_branch(ref->name));
384 stale_refs = get_stale_heads(&states->remote->fetch, fetch_map);
385 for (ref = stale_refs; ref; ref = ref->next) {
386 struct string_list_item *item =
387 string_list_append(&states->stale, abbrev_branch(ref->name));
388 item->util = xstrdup(ref->name);
390 free_refs(stale_refs);
391 free_refs(fetch_map);
393 string_list_sort(&states->new_refs);
394 string_list_sort(&states->skipped);
395 string_list_sort(&states->tracked);
396 string_list_sort(&states->stale);
398 return 0;
401 struct push_info {
402 char *dest;
403 int forced;
404 enum {
405 PUSH_STATUS_CREATE = 0,
406 PUSH_STATUS_DELETE,
407 PUSH_STATUS_UPTODATE,
408 PUSH_STATUS_FASTFORWARD,
409 PUSH_STATUS_OUTOFDATE,
410 PUSH_STATUS_NOTQUERIED
411 } status;
414 static int get_push_ref_states(const struct ref *remote_refs,
415 struct ref_states *states)
417 struct remote *remote = states->remote;
418 struct ref *ref, *local_refs, *push_map;
419 if (remote->mirror)
420 return 0;
422 local_refs = get_local_heads();
423 push_map = copy_ref_list(remote_refs);
425 match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE);
427 for (ref = push_map; ref; ref = ref->next) {
428 struct string_list_item *item;
429 struct push_info *info;
431 if (!ref->peer_ref)
432 continue;
433 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
435 item = string_list_append(&states->push,
436 abbrev_branch(ref->peer_ref->name));
437 item->util = xcalloc(1, sizeof(struct push_info));
438 info = item->util;
439 info->forced = ref->force;
440 info->dest = xstrdup(abbrev_branch(ref->name));
442 if (is_null_oid(&ref->new_oid)) {
443 info->status = PUSH_STATUS_DELETE;
444 } else if (oideq(&ref->old_oid, &ref->new_oid))
445 info->status = PUSH_STATUS_UPTODATE;
446 else if (is_null_oid(&ref->old_oid))
447 info->status = PUSH_STATUS_CREATE;
448 else if (repo_has_object_file(the_repository, &ref->old_oid) &&
449 ref_newer(&ref->new_oid, &ref->old_oid))
450 info->status = PUSH_STATUS_FASTFORWARD;
451 else
452 info->status = PUSH_STATUS_OUTOFDATE;
454 free_refs(local_refs);
455 free_refs(push_map);
456 return 0;
459 static int get_push_ref_states_noquery(struct ref_states *states)
461 int i;
462 struct remote *remote = states->remote;
463 struct string_list_item *item;
464 struct push_info *info;
466 if (remote->mirror)
467 return 0;
469 if (!remote->push.nr) {
470 item = string_list_append(&states->push, _("(matching)"));
471 info = item->util = xcalloc(1, sizeof(struct push_info));
472 info->status = PUSH_STATUS_NOTQUERIED;
473 info->dest = xstrdup(item->string);
475 for (i = 0; i < remote->push.nr; i++) {
476 const struct refspec_item *spec = &remote->push.items[i];
477 if (spec->matching)
478 item = string_list_append(&states->push, _("(matching)"));
479 else if (strlen(spec->src))
480 item = string_list_append(&states->push, spec->src);
481 else
482 item = string_list_append(&states->push, _("(delete)"));
484 info = item->util = xcalloc(1, sizeof(struct push_info));
485 info->forced = spec->force;
486 info->status = PUSH_STATUS_NOTQUERIED;
487 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
489 return 0;
492 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
494 struct ref *ref, *matches;
495 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
496 struct refspec_item refspec;
498 memset(&refspec, 0, sizeof(refspec));
499 refspec.force = 0;
500 refspec.pattern = 1;
501 refspec.src = refspec.dst = "refs/heads/*";
502 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
503 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
504 fetch_map, 1);
505 for (ref = matches; ref; ref = ref->next)
506 string_list_append(&states->heads, abbrev_branch(ref->name));
508 free_refs(fetch_map);
509 free_refs(matches);
511 return 0;
514 struct known_remote {
515 struct known_remote *next;
516 struct remote *remote;
519 struct known_remotes {
520 struct remote *to_delete;
521 struct known_remote *list;
524 static int add_known_remote(struct remote *remote, void *cb_data)
526 struct known_remotes *all = cb_data;
527 struct known_remote *r;
529 if (!strcmp(all->to_delete->name, remote->name))
530 return 0;
532 r = xmalloc(sizeof(*r));
533 r->remote = remote;
534 r->next = all->list;
535 all->list = r;
536 return 0;
539 struct branches_for_remote {
540 struct remote *remote;
541 struct string_list *branches, *skipped;
542 struct known_remotes *keep;
545 static int add_branch_for_removal(const char *refname,
546 const struct object_id *oid UNUSED,
547 int flags UNUSED, void *cb_data)
549 struct branches_for_remote *branches = cb_data;
550 struct refspec_item refspec;
551 struct known_remote *kr;
553 memset(&refspec, 0, sizeof(refspec));
554 refspec.dst = (char *)refname;
555 if (remote_find_tracking(branches->remote, &refspec))
556 return 0;
558 /* don't delete a branch if another remote also uses it */
559 for (kr = branches->keep->list; kr; kr = kr->next) {
560 memset(&refspec, 0, sizeof(refspec));
561 refspec.dst = (char *)refname;
562 if (!remote_find_tracking(kr->remote, &refspec))
563 return 0;
566 /* don't delete non-remote-tracking refs */
567 if (!starts_with(refname, "refs/remotes/")) {
568 /* advise user how to delete local branches */
569 if (starts_with(refname, "refs/heads/"))
570 string_list_append(branches->skipped,
571 abbrev_branch(refname));
572 /* silently skip over other non-remote refs */
573 return 0;
576 string_list_append(branches->branches, refname);
578 return 0;
581 struct rename_info {
582 const char *old_name;
583 const char *new_name;
584 struct string_list *remote_branches;
585 uint32_t symrefs_nr;
588 static int read_remote_branches(const char *refname,
589 const struct object_id *oid UNUSED,
590 int flags UNUSED, void *cb_data)
592 struct rename_info *rename = cb_data;
593 struct strbuf buf = STRBUF_INIT;
594 struct string_list_item *item;
595 int flag;
596 const char *symref;
598 strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name);
599 if (starts_with(refname, buf.buf)) {
600 item = string_list_append(rename->remote_branches, refname);
601 symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
602 NULL, &flag);
603 if (symref && (flag & REF_ISSYMREF)) {
604 item->util = xstrdup(symref);
605 rename->symrefs_nr++;
606 } else {
607 item->util = NULL;
610 strbuf_release(&buf);
612 return 0;
615 static int migrate_file(struct remote *remote)
617 struct strbuf buf = STRBUF_INIT;
618 int i;
620 strbuf_addf(&buf, "remote.%s.url", remote->name);
621 for (i = 0; i < remote->url_nr; i++)
622 git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
623 strbuf_reset(&buf);
624 strbuf_addf(&buf, "remote.%s.push", remote->name);
625 for (i = 0; i < remote->push.raw_nr; i++)
626 git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0);
627 strbuf_reset(&buf);
628 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
629 for (i = 0; i < remote->fetch.raw_nr; i++)
630 git_config_set_multivar(buf.buf, remote->fetch.raw[i], "^$", 0);
631 if (remote->origin == REMOTE_REMOTES)
632 unlink_or_warn(git_path("remotes/%s", remote->name));
633 else if (remote->origin == REMOTE_BRANCHES)
634 unlink_or_warn(git_path("branches/%s", remote->name));
635 strbuf_release(&buf);
637 return 0;
640 struct push_default_info
642 const char *old_name;
643 enum config_scope scope;
644 struct strbuf origin;
645 int linenr;
648 static int config_read_push_default(const char *key, const char *value,
649 const struct config_context *ctx, void *cb)
651 const struct key_value_info *kvi = ctx->kvi;
653 struct push_default_info* info = cb;
654 if (strcmp(key, "remote.pushdefault") ||
655 !value || strcmp(value, info->old_name))
656 return 0;
658 info->scope = kvi->scope;
659 strbuf_reset(&info->origin);
660 strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type));
661 info->linenr = kvi->linenr;
663 return 0;
666 static void handle_push_default(const char* old_name, const char* new_name)
668 struct push_default_info push_default = {
669 old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 };
670 git_config(config_read_push_default, &push_default);
671 if (push_default.scope >= CONFIG_SCOPE_COMMAND)
672 ; /* pass */
673 else if (push_default.scope >= CONFIG_SCOPE_LOCAL) {
674 int result = git_config_set_gently("remote.pushDefault",
675 new_name);
676 if (new_name && result && result != CONFIG_NOTHING_SET)
677 die(_("could not set '%s'"), "remote.pushDefault");
678 else if (!new_name && result && result != CONFIG_NOTHING_SET)
679 die(_("could not unset '%s'"), "remote.pushDefault");
680 } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) {
681 /* warn */
682 warning(_("The %s configuration remote.pushDefault in:\n"
683 "\t%s:%d\n"
684 "now names the non-existent remote '%s'"),
685 config_scope_name(push_default.scope),
686 push_default.origin.buf, push_default.linenr,
687 old_name);
692 static int mv(int argc, const char **argv, const char *prefix)
694 int show_progress = isatty(2);
695 struct option options[] = {
696 OPT_BOOL(0, "progress", &show_progress, N_("force progress reporting")),
697 OPT_END()
699 struct remote *oldremote, *newremote;
700 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
701 old_remote_context = STRBUF_INIT;
702 struct string_list remote_branches = STRING_LIST_INIT_DUP;
703 struct rename_info rename;
704 int i, refs_renamed_nr = 0, refspec_updated = 0;
705 struct progress *progress = NULL;
707 argc = parse_options(argc, argv, prefix, options,
708 builtin_remote_rename_usage, 0);
710 if (argc != 2)
711 usage_with_options(builtin_remote_rename_usage, options);
713 rename.old_name = argv[0];
714 rename.new_name = argv[1];
715 rename.remote_branches = &remote_branches;
716 rename.symrefs_nr = 0;
718 oldremote = remote_get(rename.old_name);
719 if (!remote_is_configured(oldremote, 1)) {
720 error(_("No such remote: '%s'"), rename.old_name);
721 exit(2);
724 if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
725 return migrate_file(oldremote);
727 newremote = remote_get(rename.new_name);
728 if (remote_is_configured(newremote, 1)) {
729 error(_("remote %s already exists."), rename.new_name);
730 exit(3);
733 if (!valid_remote_name(rename.new_name))
734 die(_("'%s' is not a valid remote name"), rename.new_name);
736 strbuf_addf(&buf, "remote.%s", rename.old_name);
737 strbuf_addf(&buf2, "remote.%s", rename.new_name);
738 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
739 return error(_("Could not rename config section '%s' to '%s'"),
740 buf.buf, buf2.buf);
742 if (oldremote->fetch.raw_nr) {
743 strbuf_reset(&buf);
744 strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
745 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
746 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
747 for (i = 0; i < oldremote->fetch.raw_nr; i++) {
748 char *ptr;
750 strbuf_reset(&buf2);
751 strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
752 ptr = strstr(buf2.buf, old_remote_context.buf);
753 if (ptr) {
754 refspec_updated = 1;
755 strbuf_splice(&buf2,
756 ptr-buf2.buf + strlen(":refs/remotes/"),
757 strlen(rename.old_name), rename.new_name,
758 strlen(rename.new_name));
759 } else
760 warning(_("Not updating non-default fetch refspec\n"
761 "\t%s\n"
762 "\tPlease update the configuration manually if necessary."),
763 buf2.buf);
765 git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
769 read_branches();
770 for (i = 0; i < branch_list.nr; i++) {
771 struct string_list_item *item = branch_list.items + i;
772 struct branch_info *info = item->util;
773 if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) {
774 strbuf_reset(&buf);
775 strbuf_addf(&buf, "branch.%s.remote", item->string);
776 git_config_set(buf.buf, rename.new_name);
778 if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) {
779 strbuf_reset(&buf);
780 strbuf_addf(&buf, "branch.%s.pushRemote", item->string);
781 git_config_set(buf.buf, rename.new_name);
785 if (!refspec_updated)
786 return 0;
789 * First remove symrefs, then rename the rest, finally create
790 * the new symrefs.
792 for_each_ref(read_remote_branches, &rename);
793 if (show_progress) {
795 * Count symrefs twice, since "renaming" them is done by
796 * deleting and recreating them in two separate passes.
798 progress = start_progress(_("Renaming remote references"),
799 rename.remote_branches->nr + rename.symrefs_nr);
801 for (i = 0; i < remote_branches.nr; i++) {
802 struct string_list_item *item = remote_branches.items + i;
803 struct strbuf referent = STRBUF_INIT;
805 if (refs_read_symbolic_ref(get_main_ref_store(the_repository), item->string,
806 &referent))
807 continue;
808 if (delete_ref(NULL, item->string, NULL, REF_NO_DEREF))
809 die(_("deleting '%s' failed"), item->string);
811 strbuf_release(&referent);
812 display_progress(progress, ++refs_renamed_nr);
814 for (i = 0; i < remote_branches.nr; i++) {
815 struct string_list_item *item = remote_branches.items + i;
817 if (item->util)
818 continue;
819 strbuf_reset(&buf);
820 strbuf_addstr(&buf, item->string);
821 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
822 rename.new_name, strlen(rename.new_name));
823 strbuf_reset(&buf2);
824 strbuf_addf(&buf2, "remote: renamed %s to %s",
825 item->string, buf.buf);
826 if (rename_ref(item->string, buf.buf, buf2.buf))
827 die(_("renaming '%s' failed"), item->string);
828 display_progress(progress, ++refs_renamed_nr);
830 for (i = 0; i < remote_branches.nr; i++) {
831 struct string_list_item *item = remote_branches.items + i;
833 if (!item->util)
834 continue;
835 strbuf_reset(&buf);
836 strbuf_addstr(&buf, item->string);
837 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
838 rename.new_name, strlen(rename.new_name));
839 strbuf_reset(&buf2);
840 strbuf_addstr(&buf2, item->util);
841 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name),
842 rename.new_name, strlen(rename.new_name));
843 strbuf_reset(&buf3);
844 strbuf_addf(&buf3, "remote: renamed %s to %s",
845 item->string, buf.buf);
846 if (create_symref(buf.buf, buf2.buf, buf3.buf))
847 die(_("creating '%s' failed"), buf.buf);
848 display_progress(progress, ++refs_renamed_nr);
850 stop_progress(&progress);
851 string_list_clear(&remote_branches, 1);
853 handle_push_default(rename.old_name, rename.new_name);
855 return 0;
858 static int rm(int argc, const char **argv, const char *prefix)
860 struct option options[] = {
861 OPT_END()
863 struct remote *remote;
864 struct strbuf buf = STRBUF_INIT;
865 struct known_remotes known_remotes = { NULL, NULL };
866 struct string_list branches = STRING_LIST_INIT_DUP;
867 struct string_list skipped = STRING_LIST_INIT_DUP;
868 struct branches_for_remote cb_data;
869 int i, result;
871 memset(&cb_data, 0, sizeof(cb_data));
872 cb_data.branches = &branches;
873 cb_data.skipped = &skipped;
874 cb_data.keep = &known_remotes;
876 argc = parse_options(argc, argv, prefix, options,
877 builtin_remote_rm_usage, 0);
878 if (argc != 1)
879 usage_with_options(builtin_remote_rm_usage, options);
881 remote = remote_get(argv[0]);
882 if (!remote_is_configured(remote, 1)) {
883 error(_("No such remote: '%s'"), argv[0]);
884 exit(2);
887 known_remotes.to_delete = remote;
888 for_each_remote(add_known_remote, &known_remotes);
890 read_branches();
891 for (i = 0; i < branch_list.nr; i++) {
892 struct string_list_item *item = branch_list.items + i;
893 struct branch_info *info = item->util;
894 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
895 const char *keys[] = { "remote", "merge", NULL }, **k;
896 for (k = keys; *k; k++) {
897 strbuf_reset(&buf);
898 strbuf_addf(&buf, "branch.%s.%s",
899 item->string, *k);
900 result = git_config_set_gently(buf.buf, NULL);
901 if (result && result != CONFIG_NOTHING_SET)
902 die(_("could not unset '%s'"), buf.buf);
905 if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) {
906 strbuf_reset(&buf);
907 strbuf_addf(&buf, "branch.%s.pushremote", item->string);
908 result = git_config_set_gently(buf.buf, NULL);
909 if (result && result != CONFIG_NOTHING_SET)
910 die(_("could not unset '%s'"), buf.buf);
915 * We cannot just pass a function to for_each_ref() which deletes
916 * the branches one by one, since for_each_ref() relies on cached
917 * refs, which are invalidated when deleting a branch.
919 cb_data.remote = remote;
920 result = for_each_ref(add_branch_for_removal, &cb_data);
921 strbuf_release(&buf);
923 if (!result)
924 result = delete_refs("remote: remove", &branches, REF_NO_DEREF);
925 string_list_clear(&branches, 0);
927 if (skipped.nr) {
928 fprintf_ln(stderr,
929 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
930 "to delete it, use:",
931 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
932 "to delete them, use:",
933 skipped.nr));
934 for (i = 0; i < skipped.nr; i++)
935 fprintf(stderr, " git branch -d %s\n",
936 skipped.items[i].string);
938 string_list_clear(&skipped, 0);
940 if (!result) {
941 strbuf_addf(&buf, "remote.%s", remote->name);
942 if (git_config_rename_section(buf.buf, NULL) < 1)
943 return error(_("Could not remove config section '%s'"), buf.buf);
945 handle_push_default(remote->name, NULL);
948 return result;
951 static void clear_push_info(void *util, const char *string UNUSED)
953 struct push_info *info = util;
954 free(info->dest);
955 free(info);
958 static void free_remote_ref_states(struct ref_states *states)
960 string_list_clear(&states->new_refs, 0);
961 string_list_clear(&states->skipped, 0);
962 string_list_clear(&states->stale, 1);
963 string_list_clear(&states->tracked, 0);
964 string_list_clear(&states->heads, 0);
965 string_list_clear_func(&states->push, clear_push_info);
968 static int append_ref_to_tracked_list(const char *refname,
969 const struct object_id *oid UNUSED,
970 int flags, void *cb_data)
972 struct ref_states *states = cb_data;
973 struct refspec_item refspec;
975 if (flags & REF_ISSYMREF)
976 return 0;
978 memset(&refspec, 0, sizeof(refspec));
979 refspec.dst = (char *)refname;
980 if (!remote_find_tracking(states->remote, &refspec))
981 string_list_append(&states->tracked, abbrev_branch(refspec.src));
983 return 0;
986 static int get_remote_ref_states(const char *name,
987 struct ref_states *states,
988 int query)
990 states->remote = remote_get(name);
991 if (!states->remote)
992 return error(_("No such remote: '%s'"), name);
994 read_branches();
996 if (query) {
997 struct transport *transport;
998 const struct ref *remote_refs;
1000 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
1001 states->remote->url[0] : NULL);
1002 remote_refs = transport_get_remote_refs(transport, NULL);
1004 states->queried = 1;
1005 if (query & GET_REF_STATES)
1006 get_ref_states(remote_refs, states);
1007 if (query & GET_HEAD_NAMES)
1008 get_head_names(remote_refs, states);
1009 if (query & GET_PUSH_REF_STATES)
1010 get_push_ref_states(remote_refs, states);
1011 transport_disconnect(transport);
1012 } else {
1013 for_each_ref(append_ref_to_tracked_list, states);
1014 string_list_sort(&states->tracked);
1015 get_push_ref_states_noquery(states);
1018 return 0;
1021 struct show_info {
1022 struct string_list list;
1023 struct ref_states states;
1024 int width, width2;
1025 int any_rebase;
1028 #define SHOW_INFO_INIT { \
1029 .list = STRING_LIST_INIT_DUP, \
1030 .states = REF_STATES_INIT, \
1033 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
1035 struct show_info *info = cb_data;
1036 int n = strlen(item->string);
1037 if (n > info->width)
1038 info->width = n;
1039 string_list_insert(&info->list, item->string);
1040 return 0;
1043 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
1045 struct show_info *info = cb_data;
1046 struct ref_states *states = &info->states;
1047 const char *name = item->string;
1049 if (states->queried) {
1050 const char *fmt = "%s";
1051 const char *arg = "";
1052 if (string_list_has_string(&states->new_refs, name)) {
1053 fmt = _(" new (next fetch will store in remotes/%s)");
1054 arg = states->remote->name;
1055 } else if (string_list_has_string(&states->tracked, name))
1056 arg = _(" tracked");
1057 else if (string_list_has_string(&states->skipped, name))
1058 arg = _(" skipped");
1059 else if (string_list_has_string(&states->stale, name))
1060 arg = _(" stale (use 'git remote prune' to remove)");
1061 else
1062 arg = _(" ???");
1063 printf(" %-*s", info->width, name);
1064 printf(fmt, arg);
1065 printf("\n");
1066 } else
1067 printf(" %s\n", name);
1069 return 0;
1072 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
1074 struct show_info *show_info = cb_data;
1075 struct ref_states *states = &show_info->states;
1076 struct branch_info *branch_info = branch_item->util;
1077 struct string_list_item *item;
1078 int n;
1080 if (!branch_info->merge.nr || !branch_info->remote_name ||
1081 strcmp(states->remote->name, branch_info->remote_name))
1082 return 0;
1083 if ((n = strlen(branch_item->string)) > show_info->width)
1084 show_info->width = n;
1085 if (branch_info->rebase >= REBASE_TRUE)
1086 show_info->any_rebase = 1;
1088 item = string_list_insert(&show_info->list, branch_item->string);
1089 item->util = branch_info;
1091 return 0;
1094 static int show_local_info_item(struct string_list_item *item, void *cb_data)
1096 struct show_info *show_info = cb_data;
1097 struct branch_info *branch_info = item->util;
1098 struct string_list *merge = &branch_info->merge;
1099 int width = show_info->width + 4;
1100 int i;
1102 if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) {
1103 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1104 item->string);
1105 return 0;
1108 printf(" %-*s ", show_info->width, item->string);
1109 if (branch_info->rebase >= REBASE_TRUE) {
1110 const char *msg;
1111 if (branch_info->rebase == REBASE_INTERACTIVE)
1112 msg = _("rebases interactively onto remote %s");
1113 else if (branch_info->rebase == REBASE_MERGES)
1114 msg = _("rebases interactively (with merges) onto "
1115 "remote %s");
1116 else
1117 msg = _("rebases onto remote %s");
1118 printf_ln(msg, merge->items[0].string);
1119 return 0;
1120 } else if (show_info->any_rebase) {
1121 printf_ln(_(" merges with remote %s"), merge->items[0].string);
1122 width++;
1123 } else {
1124 printf_ln(_("merges with remote %s"), merge->items[0].string);
1126 for (i = 1; i < merge->nr; i++)
1127 printf(_("%-*s and with remote %s\n"), width, "",
1128 merge->items[i].string);
1130 return 0;
1133 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
1135 struct show_info *show_info = cb_data;
1136 struct push_info *push_info = push_item->util;
1137 struct string_list_item *item;
1138 int n;
1139 if ((n = strlen(push_item->string)) > show_info->width)
1140 show_info->width = n;
1141 if ((n = strlen(push_info->dest)) > show_info->width2)
1142 show_info->width2 = n;
1143 item = string_list_append(&show_info->list, push_item->string);
1144 item->util = push_item->util;
1145 return 0;
1149 * Sorting comparison for a string list that has push_info
1150 * structs in its util field
1152 static int cmp_string_with_push(const void *va, const void *vb)
1154 const struct string_list_item *a = va;
1155 const struct string_list_item *b = vb;
1156 const struct push_info *a_push = a->util;
1157 const struct push_info *b_push = b->util;
1158 int cmp = strcmp(a->string, b->string);
1159 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1162 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1164 struct show_info *show_info = cb_data;
1165 struct push_info *push_info = item->util;
1166 const char *src = item->string, *status = NULL;
1168 switch (push_info->status) {
1169 case PUSH_STATUS_CREATE:
1170 status = _("create");
1171 break;
1172 case PUSH_STATUS_DELETE:
1173 status = _("delete");
1174 src = _("(none)");
1175 break;
1176 case PUSH_STATUS_UPTODATE:
1177 status = _("up to date");
1178 break;
1179 case PUSH_STATUS_FASTFORWARD:
1180 status = _("fast-forwardable");
1181 break;
1182 case PUSH_STATUS_OUTOFDATE:
1183 status = _("local out of date");
1184 break;
1185 case PUSH_STATUS_NOTQUERIED:
1186 break;
1188 if (status) {
1189 if (push_info->forced)
1190 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1191 show_info->width2, push_info->dest, status);
1192 else
1193 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1194 show_info->width2, push_info->dest, status);
1195 } else {
1196 if (push_info->forced)
1197 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1198 push_info->dest);
1199 else
1200 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1201 push_info->dest);
1203 return 0;
1206 static int get_one_entry(struct remote *remote, void *priv)
1208 struct string_list *list = priv;
1209 struct strbuf remote_info_buf = STRBUF_INIT;
1210 const char **url;
1211 int i, url_nr;
1213 if (remote->url_nr > 0) {
1214 struct strbuf promisor_config = STRBUF_INIT;
1215 const char *partial_clone_filter = NULL;
1217 strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
1218 strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url[0]);
1219 if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
1220 strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
1222 strbuf_release(&promisor_config);
1223 string_list_append(list, remote->name)->util =
1224 strbuf_detach(&remote_info_buf, NULL);
1225 } else
1226 string_list_append(list, remote->name)->util = NULL;
1227 if (remote->pushurl_nr) {
1228 url = remote->pushurl;
1229 url_nr = remote->pushurl_nr;
1230 } else {
1231 url = remote->url;
1232 url_nr = remote->url_nr;
1234 for (i = 0; i < url_nr; i++)
1236 strbuf_addf(&remote_info_buf, "%s (push)", url[i]);
1237 string_list_append(list, remote->name)->util =
1238 strbuf_detach(&remote_info_buf, NULL);
1241 return 0;
1244 static int show_all(void)
1246 struct string_list list = STRING_LIST_INIT_DUP;
1247 int result;
1249 result = for_each_remote(get_one_entry, &list);
1251 if (!result) {
1252 int i;
1254 string_list_sort(&list);
1255 for (i = 0; i < list.nr; i++) {
1256 struct string_list_item *item = list.items + i;
1257 if (verbose)
1258 printf("%s\t%s\n", item->string,
1259 item->util ? (const char *)item->util : "");
1260 else {
1261 if (i && !strcmp((item - 1)->string, item->string))
1262 continue;
1263 printf("%s\n", item->string);
1267 string_list_clear(&list, 1);
1268 return result;
1271 static int show(int argc, const char **argv, const char *prefix)
1273 int no_query = 0, result = 0, query_flag = 0;
1274 struct option options[] = {
1275 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1276 OPT_END()
1278 struct show_info info = SHOW_INFO_INIT;
1280 argc = parse_options(argc, argv, prefix, options,
1281 builtin_remote_show_usage,
1284 if (argc < 1)
1285 return show_all();
1287 if (!no_query)
1288 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1290 for (; argc; argc--, argv++) {
1291 int i;
1292 const char **url;
1293 int url_nr;
1295 get_remote_ref_states(*argv, &info.states, query_flag);
1297 printf_ln(_("* remote %s"), *argv);
1298 printf_ln(_(" Fetch URL: %s"), info.states.remote->url_nr > 0 ?
1299 info.states.remote->url[0] : _("(no URL)"));
1300 if (info.states.remote->pushurl_nr) {
1301 url = info.states.remote->pushurl;
1302 url_nr = info.states.remote->pushurl_nr;
1303 } else {
1304 url = info.states.remote->url;
1305 url_nr = info.states.remote->url_nr;
1307 for (i = 0; i < url_nr; i++)
1309 * TRANSLATORS: the colon ':' should align
1310 * with the one in " Fetch URL: %s"
1311 * translation.
1313 printf_ln(_(" Push URL: %s"), url[i]);
1314 if (!i)
1315 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1316 if (no_query)
1317 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1318 else if (!info.states.heads.nr)
1319 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1320 else if (info.states.heads.nr == 1)
1321 printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string);
1322 else {
1323 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1324 " may be one of the following):\n"));
1325 for (i = 0; i < info.states.heads.nr; i++)
1326 printf(" %s\n", info.states.heads.items[i].string);
1329 /* remote branch info */
1330 info.width = 0;
1331 for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info);
1332 for_each_string_list(&info.states.skipped, add_remote_to_show_info, &info);
1333 for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info);
1334 for_each_string_list(&info.states.stale, add_remote_to_show_info, &info);
1335 if (info.list.nr)
1336 printf_ln(Q_(" Remote branch:%s",
1337 " Remote branches:%s",
1338 info.list.nr),
1339 no_query ? _(" (status not queried)") : "");
1340 for_each_string_list(&info.list, show_remote_info_item, &info);
1341 string_list_clear(&info.list, 0);
1343 /* git pull info */
1344 info.width = 0;
1345 info.any_rebase = 0;
1346 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1347 if (info.list.nr)
1348 printf_ln(Q_(" Local branch configured for 'git pull':",
1349 " Local branches configured for 'git pull':",
1350 info.list.nr));
1351 for_each_string_list(&info.list, show_local_info_item, &info);
1352 string_list_clear(&info.list, 0);
1354 /* git push info */
1355 if (info.states.remote->mirror)
1356 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1358 info.width = info.width2 = 0;
1359 for_each_string_list(&info.states.push, add_push_to_show_info, &info);
1360 QSORT(info.list.items, info.list.nr, cmp_string_with_push);
1361 if (info.list.nr)
1362 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1363 " Local refs configured for 'git push'%s:",
1364 info.list.nr),
1365 no_query ? _(" (status not queried)") : "");
1366 for_each_string_list(&info.list, show_push_info_item, &info);
1367 string_list_clear(&info.list, 0);
1369 free_remote_ref_states(&info.states);
1372 return result;
1375 static int set_head(int argc, const char **argv, const char *prefix)
1377 int i, opt_a = 0, opt_d = 0, result = 0;
1378 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1379 char *head_name = NULL;
1381 struct option options[] = {
1382 OPT_BOOL('a', "auto", &opt_a,
1383 N_("set refs/remotes/<name>/HEAD according to remote")),
1384 OPT_BOOL('d', "delete", &opt_d,
1385 N_("delete refs/remotes/<name>/HEAD")),
1386 OPT_END()
1388 argc = parse_options(argc, argv, prefix, options,
1389 builtin_remote_sethead_usage, 0);
1390 if (argc)
1391 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1393 if (!opt_a && !opt_d && argc == 2) {
1394 head_name = xstrdup(argv[1]);
1395 } else if (opt_a && !opt_d && argc == 1) {
1396 struct ref_states states = REF_STATES_INIT;
1397 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1398 if (!states.heads.nr)
1399 result |= error(_("Cannot determine remote HEAD"));
1400 else if (states.heads.nr > 1) {
1401 result |= error(_("Multiple remote HEAD branches. "
1402 "Please choose one explicitly with:"));
1403 for (i = 0; i < states.heads.nr; i++)
1404 fprintf(stderr, " git remote set-head %s %s\n",
1405 argv[0], states.heads.items[i].string);
1406 } else
1407 head_name = xstrdup(states.heads.items[0].string);
1408 free_remote_ref_states(&states);
1409 } else if (opt_d && !opt_a && argc == 1) {
1410 if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF))
1411 result |= error(_("Could not delete %s"), buf.buf);
1412 } else
1413 usage_with_options(builtin_remote_sethead_usage, options);
1415 if (head_name) {
1416 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1417 /* make sure it's valid */
1418 if (!ref_exists(buf2.buf))
1419 result |= error(_("Not a valid ref: %s"), buf2.buf);
1420 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1421 result |= error(_("Could not setup %s"), buf.buf);
1422 else if (opt_a)
1423 printf("%s/HEAD set to %s\n", argv[0], head_name);
1424 free(head_name);
1427 strbuf_release(&buf);
1428 strbuf_release(&buf2);
1429 return result;
1432 static int prune_remote(const char *remote, int dry_run)
1434 int result = 0;
1435 struct ref_states states = REF_STATES_INIT;
1436 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1437 struct string_list_item *item;
1438 const char *dangling_msg = dry_run
1439 ? _(" %s will become dangling!")
1440 : _(" %s has become dangling!");
1442 get_remote_ref_states(remote, &states, GET_REF_STATES);
1444 if (!states.stale.nr) {
1445 free_remote_ref_states(&states);
1446 return 0;
1449 printf_ln(_("Pruning %s"), remote);
1450 printf_ln(_("URL: %s"),
1451 states.remote->url_nr
1452 ? states.remote->url[0]
1453 : _("(no URL)"));
1455 for_each_string_list_item(item, &states.stale)
1456 string_list_append(&refs_to_prune, item->util);
1457 string_list_sort(&refs_to_prune);
1459 if (!dry_run)
1460 result |= delete_refs("remote: prune", &refs_to_prune, 0);
1462 for_each_string_list_item(item, &states.stale) {
1463 const char *refname = item->util;
1465 if (dry_run)
1466 printf_ln(_(" * [would prune] %s"),
1467 abbrev_ref(refname, "refs/remotes/"));
1468 else
1469 printf_ln(_(" * [pruned] %s"),
1470 abbrev_ref(refname, "refs/remotes/"));
1473 warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
1475 string_list_clear(&refs_to_prune, 0);
1476 free_remote_ref_states(&states);
1477 return result;
1480 static int prune(int argc, const char **argv, const char *prefix)
1482 int dry_run = 0, result = 0;
1483 struct option options[] = {
1484 OPT__DRY_RUN(&dry_run, N_("dry run")),
1485 OPT_END()
1488 argc = parse_options(argc, argv, prefix, options,
1489 builtin_remote_prune_usage, 0);
1491 if (argc < 1)
1492 usage_with_options(builtin_remote_prune_usage, options);
1494 for (; argc; argc--, argv++)
1495 result |= prune_remote(*argv, dry_run);
1497 return result;
1500 static int get_remote_default(const char *key, const char *value UNUSED,
1501 const struct config_context *ctx UNUSED,
1502 void *priv)
1504 if (strcmp(key, "remotes.default") == 0) {
1505 int *found = priv;
1506 *found = 1;
1508 return 0;
1511 static int update(int argc, const char **argv, const char *prefix)
1513 int i, prune = -1;
1514 struct option options[] = {
1515 OPT_BOOL('p', "prune", &prune,
1516 N_("prune remotes after fetching")),
1517 OPT_END()
1519 struct child_process cmd = CHILD_PROCESS_INIT;
1520 int default_defined = 0;
1522 argc = parse_options(argc, argv, prefix, options,
1523 builtin_remote_update_usage,
1524 PARSE_OPT_KEEP_ARGV0);
1526 strvec_push(&cmd.args, "fetch");
1528 if (prune != -1)
1529 strvec_push(&cmd.args, prune ? "--prune" : "--no-prune");
1530 if (verbose)
1531 strvec_push(&cmd.args, "-v");
1532 strvec_push(&cmd.args, "--multiple");
1533 if (argc < 2)
1534 strvec_push(&cmd.args, "default");
1535 for (i = 1; i < argc; i++)
1536 strvec_push(&cmd.args, argv[i]);
1538 if (strcmp(cmd.args.v[cmd.args.nr-1], "default") == 0) {
1539 git_config(get_remote_default, &default_defined);
1540 if (!default_defined) {
1541 strvec_pop(&cmd.args);
1542 strvec_push(&cmd.args, "--all");
1546 cmd.git_cmd = 1;
1547 return run_command(&cmd);
1550 static int remove_all_fetch_refspecs(const char *key)
1552 return git_config_set_multivar_gently(key, NULL, NULL,
1553 CONFIG_FLAGS_MULTI_REPLACE);
1556 static void add_branches(struct remote *remote, const char **branches,
1557 const char *key)
1559 const char *remotename = remote->name;
1560 int mirror = remote->mirror;
1561 struct strbuf refspec = STRBUF_INIT;
1563 for (; *branches; branches++)
1564 add_branch(key, *branches, remotename, mirror, &refspec);
1566 strbuf_release(&refspec);
1569 static int set_remote_branches(const char *remotename, const char **branches,
1570 int add_mode)
1572 struct strbuf key = STRBUF_INIT;
1573 struct remote *remote;
1575 strbuf_addf(&key, "remote.%s.fetch", remotename);
1577 remote = remote_get(remotename);
1578 if (!remote_is_configured(remote, 1)) {
1579 error(_("No such remote '%s'"), remotename);
1580 exit(2);
1583 if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
1584 strbuf_release(&key);
1585 return 1;
1587 add_branches(remote, branches, key.buf);
1589 strbuf_release(&key);
1590 return 0;
1593 static int set_branches(int argc, const char **argv, const char *prefix)
1595 int add_mode = 0;
1596 struct option options[] = {
1597 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1598 OPT_END()
1601 argc = parse_options(argc, argv, prefix, options,
1602 builtin_remote_setbranches_usage, 0);
1603 if (argc == 0) {
1604 error(_("no remote specified"));
1605 usage_with_options(builtin_remote_setbranches_usage, options);
1607 argv[argc] = NULL;
1609 return set_remote_branches(argv[0], argv + 1, add_mode);
1612 static int get_url(int argc, const char **argv, const char *prefix)
1614 int i, push_mode = 0, all_mode = 0;
1615 const char *remotename = NULL;
1616 struct remote *remote;
1617 const char **url;
1618 int url_nr;
1619 struct option options[] = {
1620 OPT_BOOL('\0', "push", &push_mode,
1621 N_("query push URLs rather than fetch URLs")),
1622 OPT_BOOL('\0', "all", &all_mode,
1623 N_("return all URLs")),
1624 OPT_END()
1626 argc = parse_options(argc, argv, prefix, options,
1627 builtin_remote_geturl_usage, 0);
1629 if (argc != 1)
1630 usage_with_options(builtin_remote_geturl_usage, options);
1632 remotename = argv[0];
1634 remote = remote_get(remotename);
1635 if (!remote_is_configured(remote, 1)) {
1636 error(_("No such remote '%s'"), remotename);
1637 exit(2);
1640 url_nr = 0;
1641 if (push_mode) {
1642 url = remote->pushurl;
1643 url_nr = remote->pushurl_nr;
1645 /* else fetch mode */
1647 /* Use the fetch URL when no push URLs were found or requested. */
1648 if (!url_nr) {
1649 url = remote->url;
1650 url_nr = remote->url_nr;
1653 if (!url_nr)
1654 die(_("no URLs configured for remote '%s'"), remotename);
1656 if (all_mode) {
1657 for (i = 0; i < url_nr; i++)
1658 printf_ln("%s", url[i]);
1659 } else {
1660 printf_ln("%s", *url);
1663 return 0;
1666 static int set_url(int argc, const char **argv, const char *prefix)
1668 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1669 int matches = 0, negative_matches = 0;
1670 const char *remotename = NULL;
1671 const char *newurl = NULL;
1672 const char *oldurl = NULL;
1673 struct remote *remote;
1674 regex_t old_regex;
1675 const char **urlset;
1676 int urlset_nr;
1677 struct strbuf name_buf = STRBUF_INIT;
1678 struct option options[] = {
1679 OPT_BOOL('\0', "push", &push_mode,
1680 N_("manipulate push URLs")),
1681 OPT_BOOL('\0', "add", &add_mode,
1682 N_("add URL")),
1683 OPT_BOOL('\0', "delete", &delete_mode,
1684 N_("delete URLs")),
1685 OPT_END()
1687 argc = parse_options(argc, argv, prefix, options,
1688 builtin_remote_seturl_usage,
1689 PARSE_OPT_KEEP_ARGV0);
1691 if (add_mode && delete_mode)
1692 die(_("--add --delete doesn't make sense"));
1694 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1695 usage_with_options(builtin_remote_seturl_usage, options);
1697 remotename = argv[1];
1698 newurl = argv[2];
1699 if (argc > 3)
1700 oldurl = argv[3];
1702 if (delete_mode)
1703 oldurl = newurl;
1705 remote = remote_get(remotename);
1706 if (!remote_is_configured(remote, 1)) {
1707 error(_("No such remote '%s'"), remotename);
1708 exit(2);
1711 if (push_mode) {
1712 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1713 urlset = remote->pushurl;
1714 urlset_nr = remote->pushurl_nr;
1715 } else {
1716 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1717 urlset = remote->url;
1718 urlset_nr = remote->url_nr;
1721 /* Special cases that add new entry. */
1722 if ((!oldurl && !delete_mode) || add_mode) {
1723 if (add_mode)
1724 git_config_set_multivar(name_buf.buf, newurl,
1725 "^$", 0);
1726 else
1727 git_config_set(name_buf.buf, newurl);
1728 goto out;
1731 /* Old URL specified. Demand that one matches. */
1732 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1733 die(_("Invalid old URL pattern: %s"), oldurl);
1735 for (i = 0; i < urlset_nr; i++)
1736 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1737 matches++;
1738 else
1739 negative_matches++;
1740 if (!delete_mode && !matches)
1741 die(_("No such URL found: %s"), oldurl);
1742 if (delete_mode && !negative_matches && !push_mode)
1743 die(_("Will not delete all non-push URLs"));
1745 regfree(&old_regex);
1747 if (!delete_mode)
1748 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1749 else
1750 git_config_set_multivar(name_buf.buf, NULL, oldurl,
1751 CONFIG_FLAGS_MULTI_REPLACE);
1752 out:
1753 strbuf_release(&name_buf);
1754 return 0;
1757 int cmd_remote(int argc, const char **argv, const char *prefix)
1759 parse_opt_subcommand_fn *fn = NULL;
1760 struct option options[] = {
1761 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1762 OPT_SUBCOMMAND("add", &fn, add),
1763 OPT_SUBCOMMAND("rename", &fn, mv),
1764 OPT_SUBCOMMAND_F("rm", &fn, rm, PARSE_OPT_NOCOMPLETE),
1765 OPT_SUBCOMMAND("remove", &fn, rm),
1766 OPT_SUBCOMMAND("set-head", &fn, set_head),
1767 OPT_SUBCOMMAND("set-branches", &fn, set_branches),
1768 OPT_SUBCOMMAND("get-url", &fn, get_url),
1769 OPT_SUBCOMMAND("set-url", &fn, set_url),
1770 OPT_SUBCOMMAND("show", &fn, show),
1771 OPT_SUBCOMMAND("prune", &fn, prune),
1772 OPT_SUBCOMMAND("update", &fn, update),
1773 OPT_END()
1776 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1777 PARSE_OPT_SUBCOMMAND_OPTIONAL);
1779 if (fn) {
1780 return !!fn(argc, argv, prefix);
1781 } else {
1782 if (argc) {
1783 error(_("unknown subcommand: `%s'"), argv[0]);
1784 usage_with_options(builtin_remote_usage, options);
1786 return !!show_all();