The twelfth batch
[alt-git.git] / builtin / remote.c
blob447ef1d3c929a807505599755127aae8717787d0
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 (refs_update_symref(get_main_ref_store(the_repository), 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 || !refs_ref_exists(get_main_ref_store(the_repository), 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 = refs_resolve_ref_unsafe(get_main_ref_store(the_repository),
602 refname, RESOLVE_REF_READING,
603 NULL, &flag);
604 if (symref && (flag & REF_ISSYMREF)) {
605 item->util = xstrdup(symref);
606 rename->symrefs_nr++;
607 } else {
608 item->util = NULL;
611 strbuf_release(&buf);
613 return 0;
616 static int migrate_file(struct remote *remote)
618 struct strbuf buf = STRBUF_INIT;
619 int i;
621 strbuf_addf(&buf, "remote.%s.url", remote->name);
622 for (i = 0; i < remote->url_nr; i++)
623 git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
624 strbuf_reset(&buf);
625 strbuf_addf(&buf, "remote.%s.push", remote->name);
626 for (i = 0; i < remote->push.raw_nr; i++)
627 git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0);
628 strbuf_reset(&buf);
629 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
630 for (i = 0; i < remote->fetch.raw_nr; i++)
631 git_config_set_multivar(buf.buf, remote->fetch.raw[i], "^$", 0);
632 if (remote->origin == REMOTE_REMOTES)
633 unlink_or_warn(git_path("remotes/%s", remote->name));
634 else if (remote->origin == REMOTE_BRANCHES)
635 unlink_or_warn(git_path("branches/%s", remote->name));
636 strbuf_release(&buf);
638 return 0;
641 struct push_default_info
643 const char *old_name;
644 enum config_scope scope;
645 struct strbuf origin;
646 int linenr;
649 static int config_read_push_default(const char *key, const char *value,
650 const struct config_context *ctx, void *cb)
652 const struct key_value_info *kvi = ctx->kvi;
654 struct push_default_info* info = cb;
655 if (strcmp(key, "remote.pushdefault") ||
656 !value || strcmp(value, info->old_name))
657 return 0;
659 info->scope = kvi->scope;
660 strbuf_reset(&info->origin);
661 strbuf_addstr(&info->origin, config_origin_type_name(kvi->origin_type));
662 info->linenr = kvi->linenr;
664 return 0;
667 static void handle_push_default(const char* old_name, const char* new_name)
669 struct push_default_info push_default = {
670 old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 };
671 git_config(config_read_push_default, &push_default);
672 if (push_default.scope >= CONFIG_SCOPE_COMMAND)
673 ; /* pass */
674 else if (push_default.scope >= CONFIG_SCOPE_LOCAL) {
675 int result = git_config_set_gently("remote.pushDefault",
676 new_name);
677 if (new_name && result && result != CONFIG_NOTHING_SET)
678 die(_("could not set '%s'"), "remote.pushDefault");
679 else if (!new_name && result && result != CONFIG_NOTHING_SET)
680 die(_("could not unset '%s'"), "remote.pushDefault");
681 } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) {
682 /* warn */
683 warning(_("The %s configuration remote.pushDefault in:\n"
684 "\t%s:%d\n"
685 "now names the non-existent remote '%s'"),
686 config_scope_name(push_default.scope),
687 push_default.origin.buf, push_default.linenr,
688 old_name);
693 static int mv(int argc, const char **argv, const char *prefix)
695 int show_progress = isatty(2);
696 struct option options[] = {
697 OPT_BOOL(0, "progress", &show_progress, N_("force progress reporting")),
698 OPT_END()
700 struct remote *oldremote, *newremote;
701 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
702 old_remote_context = STRBUF_INIT;
703 struct string_list remote_branches = STRING_LIST_INIT_DUP;
704 struct rename_info rename;
705 int i, refs_renamed_nr = 0, refspec_updated = 0;
706 struct progress *progress = NULL;
708 argc = parse_options(argc, argv, prefix, options,
709 builtin_remote_rename_usage, 0);
711 if (argc != 2)
712 usage_with_options(builtin_remote_rename_usage, options);
714 rename.old_name = argv[0];
715 rename.new_name = argv[1];
716 rename.remote_branches = &remote_branches;
717 rename.symrefs_nr = 0;
719 oldremote = remote_get(rename.old_name);
720 if (!remote_is_configured(oldremote, 1)) {
721 error(_("No such remote: '%s'"), rename.old_name);
722 exit(2);
725 if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
726 return migrate_file(oldremote);
728 newremote = remote_get(rename.new_name);
729 if (remote_is_configured(newremote, 1)) {
730 error(_("remote %s already exists."), rename.new_name);
731 exit(3);
734 if (!valid_remote_name(rename.new_name))
735 die(_("'%s' is not a valid remote name"), rename.new_name);
737 strbuf_addf(&buf, "remote.%s", rename.old_name);
738 strbuf_addf(&buf2, "remote.%s", rename.new_name);
739 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
740 return error(_("Could not rename config section '%s' to '%s'"),
741 buf.buf, buf2.buf);
743 if (oldremote->fetch.raw_nr) {
744 strbuf_reset(&buf);
745 strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
746 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
747 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
748 for (i = 0; i < oldremote->fetch.raw_nr; i++) {
749 char *ptr;
751 strbuf_reset(&buf2);
752 strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
753 ptr = strstr(buf2.buf, old_remote_context.buf);
754 if (ptr) {
755 refspec_updated = 1;
756 strbuf_splice(&buf2,
757 ptr-buf2.buf + strlen(":refs/remotes/"),
758 strlen(rename.old_name), rename.new_name,
759 strlen(rename.new_name));
760 } else
761 warning(_("Not updating non-default fetch refspec\n"
762 "\t%s\n"
763 "\tPlease update the configuration manually if necessary."),
764 buf2.buf);
766 git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
770 read_branches();
771 for (i = 0; i < branch_list.nr; i++) {
772 struct string_list_item *item = branch_list.items + i;
773 struct branch_info *info = item->util;
774 if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) {
775 strbuf_reset(&buf);
776 strbuf_addf(&buf, "branch.%s.remote", item->string);
777 git_config_set(buf.buf, rename.new_name);
779 if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) {
780 strbuf_reset(&buf);
781 strbuf_addf(&buf, "branch.%s.pushRemote", item->string);
782 git_config_set(buf.buf, rename.new_name);
786 if (!refspec_updated)
787 return 0;
790 * First remove symrefs, then rename the rest, finally create
791 * the new symrefs.
793 refs_for_each_ref(get_main_ref_store(the_repository),
794 read_remote_branches, &rename);
795 if (show_progress) {
797 * Count symrefs twice, since "renaming" them is done by
798 * deleting and recreating them in two separate passes.
800 progress = start_progress(_("Renaming remote references"),
801 rename.remote_branches->nr + rename.symrefs_nr);
803 for (i = 0; i < remote_branches.nr; i++) {
804 struct string_list_item *item = remote_branches.items + i;
805 struct strbuf referent = STRBUF_INIT;
807 if (refs_read_symbolic_ref(get_main_ref_store(the_repository), item->string,
808 &referent))
809 continue;
810 if (refs_delete_ref(get_main_ref_store(the_repository), NULL, item->string, NULL, REF_NO_DEREF))
811 die(_("deleting '%s' failed"), item->string);
813 strbuf_release(&referent);
814 display_progress(progress, ++refs_renamed_nr);
816 for (i = 0; i < remote_branches.nr; i++) {
817 struct string_list_item *item = remote_branches.items + i;
819 if (item->util)
820 continue;
821 strbuf_reset(&buf);
822 strbuf_addstr(&buf, item->string);
823 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
824 rename.new_name, strlen(rename.new_name));
825 strbuf_reset(&buf2);
826 strbuf_addf(&buf2, "remote: renamed %s to %s",
827 item->string, buf.buf);
828 if (refs_rename_ref(get_main_ref_store(the_repository), item->string, buf.buf, buf2.buf))
829 die(_("renaming '%s' failed"), item->string);
830 display_progress(progress, ++refs_renamed_nr);
832 for (i = 0; i < remote_branches.nr; i++) {
833 struct string_list_item *item = remote_branches.items + i;
835 if (!item->util)
836 continue;
837 strbuf_reset(&buf);
838 strbuf_addstr(&buf, item->string);
839 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
840 rename.new_name, strlen(rename.new_name));
841 strbuf_reset(&buf2);
842 strbuf_addstr(&buf2, item->util);
843 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name),
844 rename.new_name, strlen(rename.new_name));
845 strbuf_reset(&buf3);
846 strbuf_addf(&buf3, "remote: renamed %s to %s",
847 item->string, buf.buf);
848 if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, buf3.buf))
849 die(_("creating '%s' failed"), buf.buf);
850 display_progress(progress, ++refs_renamed_nr);
852 stop_progress(&progress);
853 string_list_clear(&remote_branches, 1);
855 handle_push_default(rename.old_name, rename.new_name);
857 return 0;
860 static int rm(int argc, const char **argv, const char *prefix)
862 struct option options[] = {
863 OPT_END()
865 struct remote *remote;
866 struct strbuf buf = STRBUF_INIT;
867 struct known_remotes known_remotes = { NULL, NULL };
868 struct string_list branches = STRING_LIST_INIT_DUP;
869 struct string_list skipped = STRING_LIST_INIT_DUP;
870 struct branches_for_remote cb_data;
871 int i, result;
873 memset(&cb_data, 0, sizeof(cb_data));
874 cb_data.branches = &branches;
875 cb_data.skipped = &skipped;
876 cb_data.keep = &known_remotes;
878 argc = parse_options(argc, argv, prefix, options,
879 builtin_remote_rm_usage, 0);
880 if (argc != 1)
881 usage_with_options(builtin_remote_rm_usage, options);
883 remote = remote_get(argv[0]);
884 if (!remote_is_configured(remote, 1)) {
885 error(_("No such remote: '%s'"), argv[0]);
886 exit(2);
889 known_remotes.to_delete = remote;
890 for_each_remote(add_known_remote, &known_remotes);
892 read_branches();
893 for (i = 0; i < branch_list.nr; i++) {
894 struct string_list_item *item = branch_list.items + i;
895 struct branch_info *info = item->util;
896 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
897 const char *keys[] = { "remote", "merge", NULL }, **k;
898 for (k = keys; *k; k++) {
899 strbuf_reset(&buf);
900 strbuf_addf(&buf, "branch.%s.%s",
901 item->string, *k);
902 result = git_config_set_gently(buf.buf, NULL);
903 if (result && result != CONFIG_NOTHING_SET)
904 die(_("could not unset '%s'"), buf.buf);
907 if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) {
908 strbuf_reset(&buf);
909 strbuf_addf(&buf, "branch.%s.pushremote", item->string);
910 result = git_config_set_gently(buf.buf, NULL);
911 if (result && result != CONFIG_NOTHING_SET)
912 die(_("could not unset '%s'"), buf.buf);
917 * We cannot just pass a function to for_each_ref() which deletes
918 * the branches one by one, since for_each_ref() relies on cached
919 * refs, which are invalidated when deleting a branch.
921 cb_data.remote = remote;
922 result = refs_for_each_ref(get_main_ref_store(the_repository),
923 add_branch_for_removal, &cb_data);
924 strbuf_release(&buf);
926 if (!result)
927 result = refs_delete_refs(get_main_ref_store(the_repository),
928 "remote: remove", &branches,
929 REF_NO_DEREF);
930 string_list_clear(&branches, 0);
932 if (skipped.nr) {
933 fprintf_ln(stderr,
934 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
935 "to delete it, use:",
936 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
937 "to delete them, use:",
938 skipped.nr));
939 for (i = 0; i < skipped.nr; i++)
940 fprintf(stderr, " git branch -d %s\n",
941 skipped.items[i].string);
943 string_list_clear(&skipped, 0);
945 if (!result) {
946 strbuf_addf(&buf, "remote.%s", remote->name);
947 if (git_config_rename_section(buf.buf, NULL) < 1)
948 return error(_("Could not remove config section '%s'"), buf.buf);
950 handle_push_default(remote->name, NULL);
953 return result;
956 static void clear_push_info(void *util, const char *string UNUSED)
958 struct push_info *info = util;
959 free(info->dest);
960 free(info);
963 static void free_remote_ref_states(struct ref_states *states)
965 string_list_clear(&states->new_refs, 0);
966 string_list_clear(&states->skipped, 0);
967 string_list_clear(&states->stale, 1);
968 string_list_clear(&states->tracked, 0);
969 string_list_clear(&states->heads, 0);
970 string_list_clear_func(&states->push, clear_push_info);
973 static int append_ref_to_tracked_list(const char *refname,
974 const struct object_id *oid UNUSED,
975 int flags, void *cb_data)
977 struct ref_states *states = cb_data;
978 struct refspec_item refspec;
980 if (flags & REF_ISSYMREF)
981 return 0;
983 memset(&refspec, 0, sizeof(refspec));
984 refspec.dst = (char *)refname;
985 if (!remote_find_tracking(states->remote, &refspec))
986 string_list_append(&states->tracked, abbrev_branch(refspec.src));
988 return 0;
991 static int get_remote_ref_states(const char *name,
992 struct ref_states *states,
993 int query)
995 states->remote = remote_get(name);
996 if (!states->remote)
997 return error(_("No such remote: '%s'"), name);
999 read_branches();
1001 if (query) {
1002 struct transport *transport;
1003 const struct ref *remote_refs;
1005 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
1006 states->remote->url[0] : NULL);
1007 remote_refs = transport_get_remote_refs(transport, NULL);
1009 states->queried = 1;
1010 if (query & GET_REF_STATES)
1011 get_ref_states(remote_refs, states);
1012 if (query & GET_HEAD_NAMES)
1013 get_head_names(remote_refs, states);
1014 if (query & GET_PUSH_REF_STATES)
1015 get_push_ref_states(remote_refs, states);
1016 transport_disconnect(transport);
1017 } else {
1018 refs_for_each_ref(get_main_ref_store(the_repository),
1019 append_ref_to_tracked_list, states);
1020 string_list_sort(&states->tracked);
1021 get_push_ref_states_noquery(states);
1024 return 0;
1027 struct show_info {
1028 struct string_list list;
1029 struct ref_states states;
1030 int width, width2;
1031 int any_rebase;
1034 #define SHOW_INFO_INIT { \
1035 .list = STRING_LIST_INIT_DUP, \
1036 .states = REF_STATES_INIT, \
1039 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
1041 struct show_info *info = cb_data;
1042 int n = strlen(item->string);
1043 if (n > info->width)
1044 info->width = n;
1045 string_list_insert(&info->list, item->string);
1046 return 0;
1049 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
1051 struct show_info *info = cb_data;
1052 struct ref_states *states = &info->states;
1053 const char *name = item->string;
1055 if (states->queried) {
1056 const char *fmt = "%s";
1057 const char *arg = "";
1058 if (string_list_has_string(&states->new_refs, name)) {
1059 fmt = _(" new (next fetch will store in remotes/%s)");
1060 arg = states->remote->name;
1061 } else if (string_list_has_string(&states->tracked, name))
1062 arg = _(" tracked");
1063 else if (string_list_has_string(&states->skipped, name))
1064 arg = _(" skipped");
1065 else if (string_list_has_string(&states->stale, name))
1066 arg = _(" stale (use 'git remote prune' to remove)");
1067 else
1068 arg = _(" ???");
1069 printf(" %-*s", info->width, name);
1070 printf(fmt, arg);
1071 printf("\n");
1072 } else
1073 printf(" %s\n", name);
1075 return 0;
1078 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
1080 struct show_info *show_info = cb_data;
1081 struct ref_states *states = &show_info->states;
1082 struct branch_info *branch_info = branch_item->util;
1083 struct string_list_item *item;
1084 int n;
1086 if (!branch_info->merge.nr || !branch_info->remote_name ||
1087 strcmp(states->remote->name, branch_info->remote_name))
1088 return 0;
1089 if ((n = strlen(branch_item->string)) > show_info->width)
1090 show_info->width = n;
1091 if (branch_info->rebase >= REBASE_TRUE)
1092 show_info->any_rebase = 1;
1094 item = string_list_insert(&show_info->list, branch_item->string);
1095 item->util = branch_info;
1097 return 0;
1100 static int show_local_info_item(struct string_list_item *item, void *cb_data)
1102 struct show_info *show_info = cb_data;
1103 struct branch_info *branch_info = item->util;
1104 struct string_list *merge = &branch_info->merge;
1105 int width = show_info->width + 4;
1106 int i;
1108 if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) {
1109 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1110 item->string);
1111 return 0;
1114 printf(" %-*s ", show_info->width, item->string);
1115 if (branch_info->rebase >= REBASE_TRUE) {
1116 const char *msg;
1117 if (branch_info->rebase == REBASE_INTERACTIVE)
1118 msg = _("rebases interactively onto remote %s");
1119 else if (branch_info->rebase == REBASE_MERGES)
1120 msg = _("rebases interactively (with merges) onto "
1121 "remote %s");
1122 else
1123 msg = _("rebases onto remote %s");
1124 printf_ln(msg, merge->items[0].string);
1125 return 0;
1126 } else if (show_info->any_rebase) {
1127 printf_ln(_(" merges with remote %s"), merge->items[0].string);
1128 width++;
1129 } else {
1130 printf_ln(_("merges with remote %s"), merge->items[0].string);
1132 for (i = 1; i < merge->nr; i++)
1133 printf(_("%-*s and with remote %s\n"), width, "",
1134 merge->items[i].string);
1136 return 0;
1139 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
1141 struct show_info *show_info = cb_data;
1142 struct push_info *push_info = push_item->util;
1143 struct string_list_item *item;
1144 int n;
1145 if ((n = strlen(push_item->string)) > show_info->width)
1146 show_info->width = n;
1147 if ((n = strlen(push_info->dest)) > show_info->width2)
1148 show_info->width2 = n;
1149 item = string_list_append(&show_info->list, push_item->string);
1150 item->util = push_item->util;
1151 return 0;
1155 * Sorting comparison for a string list that has push_info
1156 * structs in its util field
1158 static int cmp_string_with_push(const void *va, const void *vb)
1160 const struct string_list_item *a = va;
1161 const struct string_list_item *b = vb;
1162 const struct push_info *a_push = a->util;
1163 const struct push_info *b_push = b->util;
1164 int cmp = strcmp(a->string, b->string);
1165 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1168 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1170 struct show_info *show_info = cb_data;
1171 struct push_info *push_info = item->util;
1172 const char *src = item->string, *status = NULL;
1174 switch (push_info->status) {
1175 case PUSH_STATUS_CREATE:
1176 status = _("create");
1177 break;
1178 case PUSH_STATUS_DELETE:
1179 status = _("delete");
1180 src = _("(none)");
1181 break;
1182 case PUSH_STATUS_UPTODATE:
1183 status = _("up to date");
1184 break;
1185 case PUSH_STATUS_FASTFORWARD:
1186 status = _("fast-forwardable");
1187 break;
1188 case PUSH_STATUS_OUTOFDATE:
1189 status = _("local out of date");
1190 break;
1191 case PUSH_STATUS_NOTQUERIED:
1192 break;
1194 if (status) {
1195 if (push_info->forced)
1196 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1197 show_info->width2, push_info->dest, status);
1198 else
1199 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1200 show_info->width2, push_info->dest, status);
1201 } else {
1202 if (push_info->forced)
1203 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1204 push_info->dest);
1205 else
1206 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1207 push_info->dest);
1209 return 0;
1212 static int get_one_entry(struct remote *remote, void *priv)
1214 struct string_list *list = priv;
1215 struct strbuf remote_info_buf = STRBUF_INIT;
1216 const char **url;
1217 int i, url_nr;
1219 if (remote->url_nr > 0) {
1220 struct strbuf promisor_config = STRBUF_INIT;
1221 const char *partial_clone_filter = NULL;
1223 strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
1224 strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url[0]);
1225 if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
1226 strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
1228 strbuf_release(&promisor_config);
1229 string_list_append(list, remote->name)->util =
1230 strbuf_detach(&remote_info_buf, NULL);
1231 } else
1232 string_list_append(list, remote->name)->util = NULL;
1233 if (remote->pushurl_nr) {
1234 url = remote->pushurl;
1235 url_nr = remote->pushurl_nr;
1236 } else {
1237 url = remote->url;
1238 url_nr = remote->url_nr;
1240 for (i = 0; i < url_nr; i++)
1242 strbuf_addf(&remote_info_buf, "%s (push)", url[i]);
1243 string_list_append(list, remote->name)->util =
1244 strbuf_detach(&remote_info_buf, NULL);
1247 return 0;
1250 static int show_all(void)
1252 struct string_list list = STRING_LIST_INIT_DUP;
1253 int result;
1255 result = for_each_remote(get_one_entry, &list);
1257 if (!result) {
1258 int i;
1260 string_list_sort(&list);
1261 for (i = 0; i < list.nr; i++) {
1262 struct string_list_item *item = list.items + i;
1263 if (verbose)
1264 printf("%s\t%s\n", item->string,
1265 item->util ? (const char *)item->util : "");
1266 else {
1267 if (i && !strcmp((item - 1)->string, item->string))
1268 continue;
1269 printf("%s\n", item->string);
1273 string_list_clear(&list, 1);
1274 return result;
1277 static int show(int argc, const char **argv, const char *prefix)
1279 int no_query = 0, result = 0, query_flag = 0;
1280 struct option options[] = {
1281 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1282 OPT_END()
1284 struct show_info info = SHOW_INFO_INIT;
1286 argc = parse_options(argc, argv, prefix, options,
1287 builtin_remote_show_usage,
1290 if (argc < 1)
1291 return show_all();
1293 if (!no_query)
1294 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1296 for (; argc; argc--, argv++) {
1297 int i;
1298 const char **url;
1299 int url_nr;
1301 get_remote_ref_states(*argv, &info.states, query_flag);
1303 printf_ln(_("* remote %s"), *argv);
1304 printf_ln(_(" Fetch URL: %s"), info.states.remote->url_nr > 0 ?
1305 info.states.remote->url[0] : _("(no URL)"));
1306 if (info.states.remote->pushurl_nr) {
1307 url = info.states.remote->pushurl;
1308 url_nr = info.states.remote->pushurl_nr;
1309 } else {
1310 url = info.states.remote->url;
1311 url_nr = info.states.remote->url_nr;
1313 for (i = 0; i < url_nr; i++)
1315 * TRANSLATORS: the colon ':' should align
1316 * with the one in " Fetch URL: %s"
1317 * translation.
1319 printf_ln(_(" Push URL: %s"), url[i]);
1320 if (!i)
1321 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1322 if (no_query)
1323 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1324 else if (!info.states.heads.nr)
1325 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1326 else if (info.states.heads.nr == 1)
1327 printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string);
1328 else {
1329 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1330 " may be one of the following):\n"));
1331 for (i = 0; i < info.states.heads.nr; i++)
1332 printf(" %s\n", info.states.heads.items[i].string);
1335 /* remote branch info */
1336 info.width = 0;
1337 for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info);
1338 for_each_string_list(&info.states.skipped, add_remote_to_show_info, &info);
1339 for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info);
1340 for_each_string_list(&info.states.stale, add_remote_to_show_info, &info);
1341 if (info.list.nr)
1342 printf_ln(Q_(" Remote branch:%s",
1343 " Remote branches:%s",
1344 info.list.nr),
1345 no_query ? _(" (status not queried)") : "");
1346 for_each_string_list(&info.list, show_remote_info_item, &info);
1347 string_list_clear(&info.list, 0);
1349 /* git pull info */
1350 info.width = 0;
1351 info.any_rebase = 0;
1352 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1353 if (info.list.nr)
1354 printf_ln(Q_(" Local branch configured for 'git pull':",
1355 " Local branches configured for 'git pull':",
1356 info.list.nr));
1357 for_each_string_list(&info.list, show_local_info_item, &info);
1358 string_list_clear(&info.list, 0);
1360 /* git push info */
1361 if (info.states.remote->mirror)
1362 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1364 info.width = info.width2 = 0;
1365 for_each_string_list(&info.states.push, add_push_to_show_info, &info);
1366 QSORT(info.list.items, info.list.nr, cmp_string_with_push);
1367 if (info.list.nr)
1368 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1369 " Local refs configured for 'git push'%s:",
1370 info.list.nr),
1371 no_query ? _(" (status not queried)") : "");
1372 for_each_string_list(&info.list, show_push_info_item, &info);
1373 string_list_clear(&info.list, 0);
1375 free_remote_ref_states(&info.states);
1378 return result;
1381 static int set_head(int argc, const char **argv, const char *prefix)
1383 int i, opt_a = 0, opt_d = 0, result = 0;
1384 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1385 char *head_name = NULL;
1387 struct option options[] = {
1388 OPT_BOOL('a', "auto", &opt_a,
1389 N_("set refs/remotes/<name>/HEAD according to remote")),
1390 OPT_BOOL('d', "delete", &opt_d,
1391 N_("delete refs/remotes/<name>/HEAD")),
1392 OPT_END()
1394 argc = parse_options(argc, argv, prefix, options,
1395 builtin_remote_sethead_usage, 0);
1396 if (argc)
1397 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1399 if (!opt_a && !opt_d && argc == 2) {
1400 head_name = xstrdup(argv[1]);
1401 } else if (opt_a && !opt_d && argc == 1) {
1402 struct ref_states states = REF_STATES_INIT;
1403 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1404 if (!states.heads.nr)
1405 result |= error(_("Cannot determine remote HEAD"));
1406 else if (states.heads.nr > 1) {
1407 result |= error(_("Multiple remote HEAD branches. "
1408 "Please choose one explicitly with:"));
1409 for (i = 0; i < states.heads.nr; i++)
1410 fprintf(stderr, " git remote set-head %s %s\n",
1411 argv[0], states.heads.items[i].string);
1412 } else
1413 head_name = xstrdup(states.heads.items[0].string);
1414 free_remote_ref_states(&states);
1415 } else if (opt_d && !opt_a && argc == 1) {
1416 if (refs_delete_ref(get_main_ref_store(the_repository), NULL, buf.buf, NULL, REF_NO_DEREF))
1417 result |= error(_("Could not delete %s"), buf.buf);
1418 } else
1419 usage_with_options(builtin_remote_sethead_usage, options);
1421 if (head_name) {
1422 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1423 /* make sure it's valid */
1424 if (!refs_ref_exists(get_main_ref_store(the_repository), buf2.buf))
1425 result |= error(_("Not a valid ref: %s"), buf2.buf);
1426 else if (refs_update_symref(get_main_ref_store(the_repository), buf.buf, buf2.buf, "remote set-head"))
1427 result |= error(_("Could not setup %s"), buf.buf);
1428 else if (opt_a)
1429 printf("%s/HEAD set to %s\n", argv[0], head_name);
1430 free(head_name);
1433 strbuf_release(&buf);
1434 strbuf_release(&buf2);
1435 return result;
1438 static int prune_remote(const char *remote, int dry_run)
1440 int result = 0;
1441 struct ref_states states = REF_STATES_INIT;
1442 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1443 struct string_list_item *item;
1444 const char *dangling_msg = dry_run
1445 ? _(" %s will become dangling!")
1446 : _(" %s has become dangling!");
1448 get_remote_ref_states(remote, &states, GET_REF_STATES);
1450 if (!states.stale.nr) {
1451 free_remote_ref_states(&states);
1452 return 0;
1455 printf_ln(_("Pruning %s"), remote);
1456 printf_ln(_("URL: %s"),
1457 states.remote->url_nr
1458 ? states.remote->url[0]
1459 : _("(no URL)"));
1461 for_each_string_list_item(item, &states.stale)
1462 string_list_append(&refs_to_prune, item->util);
1463 string_list_sort(&refs_to_prune);
1465 if (!dry_run)
1466 result |= refs_delete_refs(get_main_ref_store(the_repository),
1467 "remote: prune", &refs_to_prune, 0);
1469 for_each_string_list_item(item, &states.stale) {
1470 const char *refname = item->util;
1472 if (dry_run)
1473 printf_ln(_(" * [would prune] %s"),
1474 abbrev_ref(refname, "refs/remotes/"));
1475 else
1476 printf_ln(_(" * [pruned] %s"),
1477 abbrev_ref(refname, "refs/remotes/"));
1480 refs_warn_dangling_symrefs(get_main_ref_store(the_repository),
1481 stdout, dangling_msg, &refs_to_prune);
1483 string_list_clear(&refs_to_prune, 0);
1484 free_remote_ref_states(&states);
1485 return result;
1488 static int prune(int argc, const char **argv, const char *prefix)
1490 int dry_run = 0, result = 0;
1491 struct option options[] = {
1492 OPT__DRY_RUN(&dry_run, N_("dry run")),
1493 OPT_END()
1496 argc = parse_options(argc, argv, prefix, options,
1497 builtin_remote_prune_usage, 0);
1499 if (argc < 1)
1500 usage_with_options(builtin_remote_prune_usage, options);
1502 for (; argc; argc--, argv++)
1503 result |= prune_remote(*argv, dry_run);
1505 return result;
1508 static int get_remote_default(const char *key, const char *value UNUSED,
1509 const struct config_context *ctx UNUSED,
1510 void *priv)
1512 if (strcmp(key, "remotes.default") == 0) {
1513 int *found = priv;
1514 *found = 1;
1516 return 0;
1519 static int update(int argc, const char **argv, const char *prefix)
1521 int i, prune = -1;
1522 struct option options[] = {
1523 OPT_BOOL('p', "prune", &prune,
1524 N_("prune remotes after fetching")),
1525 OPT_END()
1527 struct child_process cmd = CHILD_PROCESS_INIT;
1528 int default_defined = 0;
1530 argc = parse_options(argc, argv, prefix, options,
1531 builtin_remote_update_usage,
1532 PARSE_OPT_KEEP_ARGV0);
1534 strvec_push(&cmd.args, "fetch");
1536 if (prune != -1)
1537 strvec_push(&cmd.args, prune ? "--prune" : "--no-prune");
1538 if (verbose)
1539 strvec_push(&cmd.args, "-v");
1540 strvec_push(&cmd.args, "--multiple");
1541 if (argc < 2)
1542 strvec_push(&cmd.args, "default");
1543 for (i = 1; i < argc; i++)
1544 strvec_push(&cmd.args, argv[i]);
1546 if (strcmp(cmd.args.v[cmd.args.nr-1], "default") == 0) {
1547 git_config(get_remote_default, &default_defined);
1548 if (!default_defined) {
1549 strvec_pop(&cmd.args);
1550 strvec_push(&cmd.args, "--all");
1554 cmd.git_cmd = 1;
1555 return run_command(&cmd);
1558 static int remove_all_fetch_refspecs(const char *key)
1560 return git_config_set_multivar_gently(key, NULL, NULL,
1561 CONFIG_FLAGS_MULTI_REPLACE);
1564 static void add_branches(struct remote *remote, const char **branches,
1565 const char *key)
1567 const char *remotename = remote->name;
1568 int mirror = remote->mirror;
1569 struct strbuf refspec = STRBUF_INIT;
1571 for (; *branches; branches++)
1572 add_branch(key, *branches, remotename, mirror, &refspec);
1574 strbuf_release(&refspec);
1577 static int set_remote_branches(const char *remotename, const char **branches,
1578 int add_mode)
1580 struct strbuf key = STRBUF_INIT;
1581 struct remote *remote;
1583 strbuf_addf(&key, "remote.%s.fetch", remotename);
1585 remote = remote_get(remotename);
1586 if (!remote_is_configured(remote, 1)) {
1587 error(_("No such remote '%s'"), remotename);
1588 exit(2);
1591 if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
1592 strbuf_release(&key);
1593 return 1;
1595 add_branches(remote, branches, key.buf);
1597 strbuf_release(&key);
1598 return 0;
1601 static int set_branches(int argc, const char **argv, const char *prefix)
1603 int add_mode = 0;
1604 struct option options[] = {
1605 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1606 OPT_END()
1609 argc = parse_options(argc, argv, prefix, options,
1610 builtin_remote_setbranches_usage, 0);
1611 if (argc == 0) {
1612 error(_("no remote specified"));
1613 usage_with_options(builtin_remote_setbranches_usage, options);
1615 argv[argc] = NULL;
1617 return set_remote_branches(argv[0], argv + 1, add_mode);
1620 static int get_url(int argc, const char **argv, const char *prefix)
1622 int i, push_mode = 0, all_mode = 0;
1623 const char *remotename = NULL;
1624 struct remote *remote;
1625 const char **url;
1626 int url_nr;
1627 struct option options[] = {
1628 OPT_BOOL('\0', "push", &push_mode,
1629 N_("query push URLs rather than fetch URLs")),
1630 OPT_BOOL('\0', "all", &all_mode,
1631 N_("return all URLs")),
1632 OPT_END()
1634 argc = parse_options(argc, argv, prefix, options,
1635 builtin_remote_geturl_usage, 0);
1637 if (argc != 1)
1638 usage_with_options(builtin_remote_geturl_usage, options);
1640 remotename = argv[0];
1642 remote = remote_get(remotename);
1643 if (!remote_is_configured(remote, 1)) {
1644 error(_("No such remote '%s'"), remotename);
1645 exit(2);
1648 url_nr = 0;
1649 if (push_mode) {
1650 url = remote->pushurl;
1651 url_nr = remote->pushurl_nr;
1653 /* else fetch mode */
1655 /* Use the fetch URL when no push URLs were found or requested. */
1656 if (!url_nr) {
1657 url = remote->url;
1658 url_nr = remote->url_nr;
1661 if (!url_nr)
1662 die(_("no URLs configured for remote '%s'"), remotename);
1664 if (all_mode) {
1665 for (i = 0; i < url_nr; i++)
1666 printf_ln("%s", url[i]);
1667 } else {
1668 printf_ln("%s", *url);
1671 return 0;
1674 static int set_url(int argc, const char **argv, const char *prefix)
1676 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1677 int matches = 0, negative_matches = 0;
1678 const char *remotename = NULL;
1679 const char *newurl = NULL;
1680 const char *oldurl = NULL;
1681 struct remote *remote;
1682 regex_t old_regex;
1683 const char **urlset;
1684 int urlset_nr;
1685 struct strbuf name_buf = STRBUF_INIT;
1686 struct option options[] = {
1687 OPT_BOOL('\0', "push", &push_mode,
1688 N_("manipulate push URLs")),
1689 OPT_BOOL('\0', "add", &add_mode,
1690 N_("add URL")),
1691 OPT_BOOL('\0', "delete", &delete_mode,
1692 N_("delete URLs")),
1693 OPT_END()
1695 argc = parse_options(argc, argv, prefix, options,
1696 builtin_remote_seturl_usage,
1697 PARSE_OPT_KEEP_ARGV0);
1699 if (add_mode && delete_mode)
1700 die(_("--add --delete doesn't make sense"));
1702 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1703 usage_with_options(builtin_remote_seturl_usage, options);
1705 remotename = argv[1];
1706 newurl = argv[2];
1707 if (argc > 3)
1708 oldurl = argv[3];
1710 if (delete_mode)
1711 oldurl = newurl;
1713 remote = remote_get(remotename);
1714 if (!remote_is_configured(remote, 1)) {
1715 error(_("No such remote '%s'"), remotename);
1716 exit(2);
1719 if (push_mode) {
1720 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1721 urlset = remote->pushurl;
1722 urlset_nr = remote->pushurl_nr;
1723 } else {
1724 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1725 urlset = remote->url;
1726 urlset_nr = remote->url_nr;
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[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, const char **argv, const char *prefix)
1767 parse_opt_subcommand_fn *fn = NULL;
1768 struct option options[] = {
1769 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1770 OPT_SUBCOMMAND("add", &fn, add),
1771 OPT_SUBCOMMAND("rename", &fn, mv),
1772 OPT_SUBCOMMAND_F("rm", &fn, rm, PARSE_OPT_NOCOMPLETE),
1773 OPT_SUBCOMMAND("remove", &fn, rm),
1774 OPT_SUBCOMMAND("set-head", &fn, set_head),
1775 OPT_SUBCOMMAND("set-branches", &fn, set_branches),
1776 OPT_SUBCOMMAND("get-url", &fn, get_url),
1777 OPT_SUBCOMMAND("set-url", &fn, set_url),
1778 OPT_SUBCOMMAND("show", &fn, show),
1779 OPT_SUBCOMMAND("prune", &fn, prune),
1780 OPT_SUBCOMMAND("update", &fn, update),
1781 OPT_END()
1784 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1785 PARSE_OPT_SUBCOMMAND_OPTIONAL);
1787 if (fn) {
1788 return !!fn(argc, argv, prefix);
1789 } else {
1790 if (argc) {
1791 error(_("unknown subcommand: `%s'"), argv[0]);
1792 usage_with_options(builtin_remote_usage, options);
1794 return !!show_all();