completion: complete --break-rewrites
[git/debian.git] / builtin / remote.c
blob1e0b137d977bb74da381cf173f82e25c5c548537
1 #include "builtin.h"
2 #include "config.h"
3 #include "gettext.h"
4 #include "parse-options.h"
5 #include "transport.h"
6 #include "remote.h"
7 #include "string-list.h"
8 #include "strbuf.h"
9 #include "run-command.h"
10 #include "rebase.h"
11 #include "refs.h"
12 #include "refspec.h"
13 #include "object-store.h"
14 #include "strvec.h"
15 #include "commit-reach.h"
16 #include "progress.h"
18 static const char * const builtin_remote_usage[] = {
19 "git remote [-v | --verbose]",
20 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
21 N_("git remote rename [--[no-]progress] <old> <new>"),
22 N_("git remote remove <name>"),
23 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
24 N_("git remote [-v | --verbose] show [-n] <name>"),
25 N_("git remote prune [-n | --dry-run] <name>"),
26 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
27 N_("git remote set-branches [--add] <name> <branch>..."),
28 N_("git remote get-url [--push] [--all] <name>"),
29 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
30 N_("git remote set-url --add <name> <newurl>"),
31 N_("git remote set-url --delete <name> <url>"),
32 NULL
35 static const char * const builtin_remote_add_usage[] = {
36 N_("git remote add [<options>] <name> <url>"),
37 NULL
40 static const char * const builtin_remote_rename_usage[] = {
41 N_("git remote rename [--[no-]progress] <old> <new>"),
42 NULL
45 static const char * const builtin_remote_rm_usage[] = {
46 N_("git remote remove <name>"),
47 NULL
50 static const char * const builtin_remote_sethead_usage[] = {
51 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
52 NULL
55 static const char * const builtin_remote_setbranches_usage[] = {
56 N_("git remote set-branches <name> <branch>..."),
57 N_("git remote set-branches --add <name> <branch>..."),
58 NULL
61 static const char * const builtin_remote_show_usage[] = {
62 N_("git remote show [<options>] <name>"),
63 NULL
66 static const char * const builtin_remote_prune_usage[] = {
67 N_("git remote prune [<options>] <name>"),
68 NULL
71 static const char * const builtin_remote_update_usage[] = {
72 N_("git remote update [<options>] [<group> | <remote>]..."),
73 NULL
76 static const char * const builtin_remote_geturl_usage[] = {
77 N_("git remote get-url [--push] [--all] <name>"),
78 NULL
81 static const char * const builtin_remote_seturl_usage[] = {
82 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
83 N_("git remote set-url --add <name> <newurl>"),
84 N_("git remote set-url --delete <name> <url>"),
85 NULL
88 #define GET_REF_STATES (1<<0)
89 #define GET_HEAD_NAMES (1<<1)
90 #define GET_PUSH_REF_STATES (1<<2)
92 static int verbose;
94 static int fetch_remote(const char *name)
96 struct child_process cmd = CHILD_PROCESS_INIT;
98 strvec_push(&cmd.args, "fetch");
99 if (verbose)
100 strvec_push(&cmd.args, "-v");
101 strvec_push(&cmd.args, name);
102 cmd.git_cmd = 1;
103 printf_ln(_("Updating %s"), name);
104 if (run_command(&cmd))
105 return error(_("Could not fetch %s"), name);
106 return 0;
109 enum {
110 TAGS_UNSET = 0,
111 TAGS_DEFAULT = 1,
112 TAGS_SET = 2
115 #define MIRROR_NONE 0
116 #define MIRROR_FETCH 1
117 #define MIRROR_PUSH 2
118 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
120 static void add_branch(const char *key, const char *branchname,
121 const char *remotename, int mirror, struct strbuf *tmp)
123 strbuf_reset(tmp);
124 strbuf_addch(tmp, '+');
125 if (mirror)
126 strbuf_addf(tmp, "refs/%s:refs/%s",
127 branchname, branchname);
128 else
129 strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
130 branchname, remotename, branchname);
131 git_config_set_multivar(key, tmp->buf, "^$", 0);
134 static const char mirror_advice[] =
135 N_("--mirror is dangerous and deprecated; please\n"
136 "\t use --mirror=fetch or --mirror=push instead");
138 static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
140 unsigned *mirror = opt->value;
141 if (not)
142 *mirror = MIRROR_NONE;
143 else if (!arg) {
144 warning("%s", _(mirror_advice));
145 *mirror = MIRROR_BOTH;
147 else if (!strcmp(arg, "fetch"))
148 *mirror = MIRROR_FETCH;
149 else if (!strcmp(arg, "push"))
150 *mirror = MIRROR_PUSH;
151 else
152 return error(_("unknown mirror argument: %s"), arg);
153 return 0;
156 static int add(int argc, const char **argv, const char *prefix)
158 int fetch = 0, fetch_tags = TAGS_DEFAULT;
159 unsigned mirror = MIRROR_NONE;
160 struct string_list track = STRING_LIST_INIT_NODUP;
161 const char *master = NULL;
162 struct remote *remote;
163 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
164 const char *name, *url;
165 int i;
167 struct option options[] = {
168 OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
169 OPT_SET_INT(0, "tags", &fetch_tags,
170 N_("import all tags and associated objects when fetching"),
171 TAGS_SET),
172 OPT_SET_INT(0, NULL, &fetch_tags,
173 N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET),
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 void *data UNUSED)
273 const char *orig_key = key;
274 char *name;
275 struct string_list_item *item;
276 struct branch_info *info;
277 enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type;
278 size_t key_len;
280 if (!starts_with(key, "branch."))
281 return 0;
283 key += strlen("branch.");
284 if (strip_suffix(key, ".remote", &key_len))
285 type = REMOTE;
286 else if (strip_suffix(key, ".merge", &key_len))
287 type = MERGE;
288 else if (strip_suffix(key, ".rebase", &key_len))
289 type = REBASE;
290 else if (strip_suffix(key, ".pushremote", &key_len))
291 type = PUSH_REMOTE;
292 else
293 return 0;
294 name = xmemdupz(key, key_len);
296 item = string_list_insert(&branch_list, name);
298 if (!item->util)
299 item->util = xcalloc(1, sizeof(struct branch_info));
300 info = item->util;
301 switch (type) {
302 case REMOTE:
303 if (info->remote_name)
304 warning(_("more than one %s"), orig_key);
305 info->remote_name = xstrdup(value);
306 break;
307 case MERGE: {
308 char *space = strchr(value, ' ');
309 value = abbrev_branch(value);
310 while (space) {
311 char *merge;
312 merge = xstrndup(value, space - value);
313 string_list_append(&info->merge, merge);
314 value = abbrev_branch(space + 1);
315 space = strchr(value, ' ');
317 string_list_append(&info->merge, xstrdup(value));
318 break;
320 case REBASE:
322 * Consider invalid values as false and check the
323 * truth value with >= REBASE_TRUE.
325 info->rebase = rebase_parse_value(value);
326 if (info->rebase == REBASE_INVALID)
327 warning(_("unhandled branch.%s.rebase=%s; assuming "
328 "'true'"), name, value);
329 break;
330 case PUSH_REMOTE:
331 if (info->push_remote_name)
332 warning(_("more than one %s"), orig_key);
333 info->push_remote_name = xstrdup(value);
334 break;
335 default:
336 BUG("unexpected type=%d", type);
339 return 0;
342 static void read_branches(void)
344 if (branch_list.nr)
345 return;
346 git_config(config_read_branches, NULL);
349 struct ref_states {
350 struct remote *remote;
351 struct string_list new_refs, skipped, stale, tracked, heads, push;
352 int queried;
355 #define REF_STATES_INIT { \
356 .new_refs = STRING_LIST_INIT_DUP, \
357 .skipped = STRING_LIST_INIT_DUP, \
358 .stale = STRING_LIST_INIT_DUP, \
359 .tracked = STRING_LIST_INIT_DUP, \
360 .heads = STRING_LIST_INIT_DUP, \
361 .push = STRING_LIST_INIT_DUP, \
364 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
366 struct ref *fetch_map = NULL, **tail = &fetch_map;
367 struct ref *ref, *stale_refs;
368 int i;
370 for (i = 0; i < states->remote->fetch.nr; i++)
371 if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1))
372 die(_("Could not get fetch map for refspec %s"),
373 states->remote->fetch.raw[i]);
375 for (ref = fetch_map; ref; ref = ref->next) {
376 if (omit_name_by_refspec(ref->name, &states->remote->fetch))
377 string_list_append(&states->skipped, abbrev_branch(ref->name));
378 else if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
379 string_list_append(&states->new_refs, abbrev_branch(ref->name));
380 else
381 string_list_append(&states->tracked, abbrev_branch(ref->name));
383 stale_refs = get_stale_heads(&states->remote->fetch, fetch_map);
384 for (ref = stale_refs; ref; ref = ref->next) {
385 struct string_list_item *item =
386 string_list_append(&states->stale, abbrev_branch(ref->name));
387 item->util = xstrdup(ref->name);
389 free_refs(stale_refs);
390 free_refs(fetch_map);
392 string_list_sort(&states->new_refs);
393 string_list_sort(&states->skipped);
394 string_list_sort(&states->tracked);
395 string_list_sort(&states->stale);
397 return 0;
400 struct push_info {
401 char *dest;
402 int forced;
403 enum {
404 PUSH_STATUS_CREATE = 0,
405 PUSH_STATUS_DELETE,
406 PUSH_STATUS_UPTODATE,
407 PUSH_STATUS_FASTFORWARD,
408 PUSH_STATUS_OUTOFDATE,
409 PUSH_STATUS_NOTQUERIED
410 } status;
413 static int get_push_ref_states(const struct ref *remote_refs,
414 struct ref_states *states)
416 struct remote *remote = states->remote;
417 struct ref *ref, *local_refs, *push_map;
418 if (remote->mirror)
419 return 0;
421 local_refs = get_local_heads();
422 push_map = copy_ref_list(remote_refs);
424 match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE);
426 for (ref = push_map; ref; ref = ref->next) {
427 struct string_list_item *item;
428 struct push_info *info;
430 if (!ref->peer_ref)
431 continue;
432 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
434 item = string_list_append(&states->push,
435 abbrev_branch(ref->peer_ref->name));
436 item->util = xcalloc(1, sizeof(struct push_info));
437 info = item->util;
438 info->forced = ref->force;
439 info->dest = xstrdup(abbrev_branch(ref->name));
441 if (is_null_oid(&ref->new_oid)) {
442 info->status = PUSH_STATUS_DELETE;
443 } else if (oideq(&ref->old_oid, &ref->new_oid))
444 info->status = PUSH_STATUS_UPTODATE;
445 else if (is_null_oid(&ref->old_oid))
446 info->status = PUSH_STATUS_CREATE;
447 else if (repo_has_object_file(the_repository, &ref->old_oid) &&
448 ref_newer(&ref->new_oid, &ref->old_oid))
449 info->status = PUSH_STATUS_FASTFORWARD;
450 else
451 info->status = PUSH_STATUS_OUTOFDATE;
453 free_refs(local_refs);
454 free_refs(push_map);
455 return 0;
458 static int get_push_ref_states_noquery(struct ref_states *states)
460 int i;
461 struct remote *remote = states->remote;
462 struct string_list_item *item;
463 struct push_info *info;
465 if (remote->mirror)
466 return 0;
468 if (!remote->push.nr) {
469 item = string_list_append(&states->push, _("(matching)"));
470 info = item->util = xcalloc(1, sizeof(struct push_info));
471 info->status = PUSH_STATUS_NOTQUERIED;
472 info->dest = xstrdup(item->string);
474 for (i = 0; i < remote->push.nr; i++) {
475 const struct refspec_item *spec = &remote->push.items[i];
476 if (spec->matching)
477 item = string_list_append(&states->push, _("(matching)"));
478 else if (strlen(spec->src))
479 item = string_list_append(&states->push, spec->src);
480 else
481 item = string_list_append(&states->push, _("(delete)"));
483 info = item->util = xcalloc(1, sizeof(struct push_info));
484 info->forced = spec->force;
485 info->status = PUSH_STATUS_NOTQUERIED;
486 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
488 return 0;
491 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
493 struct ref *ref, *matches;
494 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
495 struct refspec_item refspec;
497 memset(&refspec, 0, sizeof(refspec));
498 refspec.force = 0;
499 refspec.pattern = 1;
500 refspec.src = refspec.dst = "refs/heads/*";
501 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
502 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
503 fetch_map, 1);
504 for (ref = matches; ref; ref = ref->next)
505 string_list_append(&states->heads, abbrev_branch(ref->name));
507 free_refs(fetch_map);
508 free_refs(matches);
510 return 0;
513 struct known_remote {
514 struct known_remote *next;
515 struct remote *remote;
518 struct known_remotes {
519 struct remote *to_delete;
520 struct known_remote *list;
523 static int add_known_remote(struct remote *remote, void *cb_data)
525 struct known_remotes *all = cb_data;
526 struct known_remote *r;
528 if (!strcmp(all->to_delete->name, remote->name))
529 return 0;
531 r = xmalloc(sizeof(*r));
532 r->remote = remote;
533 r->next = all->list;
534 all->list = r;
535 return 0;
538 struct branches_for_remote {
539 struct remote *remote;
540 struct string_list *branches, *skipped;
541 struct known_remotes *keep;
544 static int add_branch_for_removal(const char *refname,
545 const struct object_id *oid UNUSED,
546 int flags UNUSED, void *cb_data)
548 struct branches_for_remote *branches = cb_data;
549 struct refspec_item refspec;
550 struct known_remote *kr;
552 memset(&refspec, 0, sizeof(refspec));
553 refspec.dst = (char *)refname;
554 if (remote_find_tracking(branches->remote, &refspec))
555 return 0;
557 /* don't delete a branch if another remote also uses it */
558 for (kr = branches->keep->list; kr; kr = kr->next) {
559 memset(&refspec, 0, sizeof(refspec));
560 refspec.dst = (char *)refname;
561 if (!remote_find_tracking(kr->remote, &refspec))
562 return 0;
565 /* don't delete non-remote-tracking refs */
566 if (!starts_with(refname, "refs/remotes/")) {
567 /* advise user how to delete local branches */
568 if (starts_with(refname, "refs/heads/"))
569 string_list_append(branches->skipped,
570 abbrev_branch(refname));
571 /* silently skip over other non-remote refs */
572 return 0;
575 string_list_append(branches->branches, refname);
577 return 0;
580 struct rename_info {
581 const char *old_name;
582 const char *new_name;
583 struct string_list *remote_branches;
584 uint32_t symrefs_nr;
587 static int read_remote_branches(const char *refname,
588 const struct object_id *oid UNUSED,
589 int flags UNUSED, void *cb_data)
591 struct rename_info *rename = cb_data;
592 struct strbuf buf = STRBUF_INIT;
593 struct string_list_item *item;
594 int flag;
595 const char *symref;
597 strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name);
598 if (starts_with(refname, buf.buf)) {
599 item = string_list_append(rename->remote_branches, refname);
600 symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
601 NULL, &flag);
602 if (symref && (flag & REF_ISSYMREF)) {
603 item->util = xstrdup(symref);
604 rename->symrefs_nr++;
605 } else {
606 item->util = NULL;
609 strbuf_release(&buf);
611 return 0;
614 static int migrate_file(struct remote *remote)
616 struct strbuf buf = STRBUF_INIT;
617 int i;
619 strbuf_addf(&buf, "remote.%s.url", remote->name);
620 for (i = 0; i < remote->url_nr; i++)
621 git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
622 strbuf_reset(&buf);
623 strbuf_addf(&buf, "remote.%s.push", remote->name);
624 for (i = 0; i < remote->push.raw_nr; i++)
625 git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0);
626 strbuf_reset(&buf);
627 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
628 for (i = 0; i < remote->fetch.raw_nr; i++)
629 git_config_set_multivar(buf.buf, remote->fetch.raw[i], "^$", 0);
630 if (remote->origin == REMOTE_REMOTES)
631 unlink_or_warn(git_path("remotes/%s", remote->name));
632 else if (remote->origin == REMOTE_BRANCHES)
633 unlink_or_warn(git_path("branches/%s", remote->name));
634 strbuf_release(&buf);
636 return 0;
639 struct push_default_info
641 const char *old_name;
642 enum config_scope scope;
643 struct strbuf origin;
644 int linenr;
647 static int config_read_push_default(const char *key, const char *value,
648 void *cb)
650 struct push_default_info* info = cb;
651 if (strcmp(key, "remote.pushdefault") ||
652 !value || strcmp(value, info->old_name))
653 return 0;
655 info->scope = current_config_scope();
656 strbuf_reset(&info->origin);
657 strbuf_addstr(&info->origin, current_config_name());
658 info->linenr = current_config_line();
660 return 0;
663 static void handle_push_default(const char* old_name, const char* new_name)
665 struct push_default_info push_default = {
666 old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 };
667 git_config(config_read_push_default, &push_default);
668 if (push_default.scope >= CONFIG_SCOPE_COMMAND)
669 ; /* pass */
670 else if (push_default.scope >= CONFIG_SCOPE_LOCAL) {
671 int result = git_config_set_gently("remote.pushDefault",
672 new_name);
673 if (new_name && result && result != CONFIG_NOTHING_SET)
674 die(_("could not set '%s'"), "remote.pushDefault");
675 else if (!new_name && result && result != CONFIG_NOTHING_SET)
676 die(_("could not unset '%s'"), "remote.pushDefault");
677 } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) {
678 /* warn */
679 warning(_("The %s configuration remote.pushDefault in:\n"
680 "\t%s:%d\n"
681 "now names the non-existent remote '%s'"),
682 config_scope_name(push_default.scope),
683 push_default.origin.buf, push_default.linenr,
684 old_name);
689 static int mv(int argc, const char **argv, const char *prefix)
691 int show_progress = isatty(2);
692 struct option options[] = {
693 OPT_BOOL(0, "progress", &show_progress, N_("force progress reporting")),
694 OPT_END()
696 struct remote *oldremote, *newremote;
697 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
698 old_remote_context = STRBUF_INIT;
699 struct string_list remote_branches = STRING_LIST_INIT_DUP;
700 struct rename_info rename;
701 int i, refs_renamed_nr = 0, refspec_updated = 0;
702 struct progress *progress = NULL;
704 argc = parse_options(argc, argv, prefix, options,
705 builtin_remote_rename_usage, 0);
707 if (argc != 2)
708 usage_with_options(builtin_remote_rename_usage, options);
710 rename.old_name = argv[0];
711 rename.new_name = argv[1];
712 rename.remote_branches = &remote_branches;
713 rename.symrefs_nr = 0;
715 oldremote = remote_get(rename.old_name);
716 if (!remote_is_configured(oldremote, 1)) {
717 error(_("No such remote: '%s'"), rename.old_name);
718 exit(2);
721 if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
722 return migrate_file(oldremote);
724 newremote = remote_get(rename.new_name);
725 if (remote_is_configured(newremote, 1)) {
726 error(_("remote %s already exists."), rename.new_name);
727 exit(3);
730 if (!valid_remote_name(rename.new_name))
731 die(_("'%s' is not a valid remote name"), rename.new_name);
733 strbuf_addf(&buf, "remote.%s", rename.old_name);
734 strbuf_addf(&buf2, "remote.%s", rename.new_name);
735 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
736 return error(_("Could not rename config section '%s' to '%s'"),
737 buf.buf, buf2.buf);
739 if (oldremote->fetch.raw_nr) {
740 strbuf_reset(&buf);
741 strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
742 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
743 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
744 for (i = 0; i < oldremote->fetch.raw_nr; i++) {
745 char *ptr;
747 strbuf_reset(&buf2);
748 strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
749 ptr = strstr(buf2.buf, old_remote_context.buf);
750 if (ptr) {
751 refspec_updated = 1;
752 strbuf_splice(&buf2,
753 ptr-buf2.buf + strlen(":refs/remotes/"),
754 strlen(rename.old_name), rename.new_name,
755 strlen(rename.new_name));
756 } else
757 warning(_("Not updating non-default fetch refspec\n"
758 "\t%s\n"
759 "\tPlease update the configuration manually if necessary."),
760 buf2.buf);
762 git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
766 read_branches();
767 for (i = 0; i < branch_list.nr; i++) {
768 struct string_list_item *item = branch_list.items + i;
769 struct branch_info *info = item->util;
770 if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) {
771 strbuf_reset(&buf);
772 strbuf_addf(&buf, "branch.%s.remote", item->string);
773 git_config_set(buf.buf, rename.new_name);
775 if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) {
776 strbuf_reset(&buf);
777 strbuf_addf(&buf, "branch.%s.pushRemote", item->string);
778 git_config_set(buf.buf, rename.new_name);
782 if (!refspec_updated)
783 return 0;
786 * First remove symrefs, then rename the rest, finally create
787 * the new symrefs.
789 for_each_ref(read_remote_branches, &rename);
790 if (show_progress) {
792 * Count symrefs twice, since "renaming" them is done by
793 * deleting and recreating them in two separate passes.
795 progress = start_progress(_("Renaming remote references"),
796 rename.remote_branches->nr + rename.symrefs_nr);
798 for (i = 0; i < remote_branches.nr; i++) {
799 struct string_list_item *item = remote_branches.items + i;
800 struct strbuf referent = STRBUF_INIT;
802 if (refs_read_symbolic_ref(get_main_ref_store(the_repository), item->string,
803 &referent))
804 continue;
805 if (delete_ref(NULL, item->string, NULL, REF_NO_DEREF))
806 die(_("deleting '%s' failed"), item->string);
808 strbuf_release(&referent);
809 display_progress(progress, ++refs_renamed_nr);
811 for (i = 0; i < remote_branches.nr; i++) {
812 struct string_list_item *item = remote_branches.items + i;
814 if (item->util)
815 continue;
816 strbuf_reset(&buf);
817 strbuf_addstr(&buf, item->string);
818 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
819 rename.new_name, strlen(rename.new_name));
820 strbuf_reset(&buf2);
821 strbuf_addf(&buf2, "remote: renamed %s to %s",
822 item->string, buf.buf);
823 if (rename_ref(item->string, buf.buf, buf2.buf))
824 die(_("renaming '%s' failed"), item->string);
825 display_progress(progress, ++refs_renamed_nr);
827 for (i = 0; i < remote_branches.nr; i++) {
828 struct string_list_item *item = remote_branches.items + i;
830 if (!item->util)
831 continue;
832 strbuf_reset(&buf);
833 strbuf_addstr(&buf, item->string);
834 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
835 rename.new_name, strlen(rename.new_name));
836 strbuf_reset(&buf2);
837 strbuf_addstr(&buf2, item->util);
838 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name),
839 rename.new_name, strlen(rename.new_name));
840 strbuf_reset(&buf3);
841 strbuf_addf(&buf3, "remote: renamed %s to %s",
842 item->string, buf.buf);
843 if (create_symref(buf.buf, buf2.buf, buf3.buf))
844 die(_("creating '%s' failed"), buf.buf);
845 display_progress(progress, ++refs_renamed_nr);
847 stop_progress(&progress);
848 string_list_clear(&remote_branches, 1);
850 handle_push_default(rename.old_name, rename.new_name);
852 return 0;
855 static int rm(int argc, const char **argv, const char *prefix)
857 struct option options[] = {
858 OPT_END()
860 struct remote *remote;
861 struct strbuf buf = STRBUF_INIT;
862 struct known_remotes known_remotes = { NULL, NULL };
863 struct string_list branches = STRING_LIST_INIT_DUP;
864 struct string_list skipped = STRING_LIST_INIT_DUP;
865 struct branches_for_remote cb_data;
866 int i, result;
868 memset(&cb_data, 0, sizeof(cb_data));
869 cb_data.branches = &branches;
870 cb_data.skipped = &skipped;
871 cb_data.keep = &known_remotes;
873 argc = parse_options(argc, argv, prefix, options,
874 builtin_remote_rm_usage, 0);
875 if (argc != 1)
876 usage_with_options(builtin_remote_rm_usage, options);
878 remote = remote_get(argv[0]);
879 if (!remote_is_configured(remote, 1)) {
880 error(_("No such remote: '%s'"), argv[0]);
881 exit(2);
884 known_remotes.to_delete = remote;
885 for_each_remote(add_known_remote, &known_remotes);
887 read_branches();
888 for (i = 0; i < branch_list.nr; i++) {
889 struct string_list_item *item = branch_list.items + i;
890 struct branch_info *info = item->util;
891 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
892 const char *keys[] = { "remote", "merge", NULL }, **k;
893 for (k = keys; *k; k++) {
894 strbuf_reset(&buf);
895 strbuf_addf(&buf, "branch.%s.%s",
896 item->string, *k);
897 result = git_config_set_gently(buf.buf, NULL);
898 if (result && result != CONFIG_NOTHING_SET)
899 die(_("could not unset '%s'"), buf.buf);
902 if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) {
903 strbuf_reset(&buf);
904 strbuf_addf(&buf, "branch.%s.pushremote", item->string);
905 result = git_config_set_gently(buf.buf, NULL);
906 if (result && result != CONFIG_NOTHING_SET)
907 die(_("could not unset '%s'"), buf.buf);
912 * We cannot just pass a function to for_each_ref() which deletes
913 * the branches one by one, since for_each_ref() relies on cached
914 * refs, which are invalidated when deleting a branch.
916 cb_data.remote = remote;
917 result = for_each_ref(add_branch_for_removal, &cb_data);
918 strbuf_release(&buf);
920 if (!result)
921 result = delete_refs("remote: remove", &branches, REF_NO_DEREF);
922 string_list_clear(&branches, 0);
924 if (skipped.nr) {
925 fprintf_ln(stderr,
926 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
927 "to delete it, use:",
928 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
929 "to delete them, use:",
930 skipped.nr));
931 for (i = 0; i < skipped.nr; i++)
932 fprintf(stderr, " git branch -d %s\n",
933 skipped.items[i].string);
935 string_list_clear(&skipped, 0);
937 if (!result) {
938 strbuf_addf(&buf, "remote.%s", remote->name);
939 if (git_config_rename_section(buf.buf, NULL) < 1)
940 return error(_("Could not remove config section '%s'"), buf.buf);
942 handle_push_default(remote->name, NULL);
945 return result;
948 static void clear_push_info(void *util, const char *string UNUSED)
950 struct push_info *info = util;
951 free(info->dest);
952 free(info);
955 static void free_remote_ref_states(struct ref_states *states)
957 string_list_clear(&states->new_refs, 0);
958 string_list_clear(&states->skipped, 0);
959 string_list_clear(&states->stale, 1);
960 string_list_clear(&states->tracked, 0);
961 string_list_clear(&states->heads, 0);
962 string_list_clear_func(&states->push, clear_push_info);
965 static int append_ref_to_tracked_list(const char *refname,
966 const struct object_id *oid UNUSED,
967 int flags, void *cb_data)
969 struct ref_states *states = cb_data;
970 struct refspec_item refspec;
972 if (flags & REF_ISSYMREF)
973 return 0;
975 memset(&refspec, 0, sizeof(refspec));
976 refspec.dst = (char *)refname;
977 if (!remote_find_tracking(states->remote, &refspec))
978 string_list_append(&states->tracked, abbrev_branch(refspec.src));
980 return 0;
983 static int get_remote_ref_states(const char *name,
984 struct ref_states *states,
985 int query)
987 states->remote = remote_get(name);
988 if (!states->remote)
989 return error(_("No such remote: '%s'"), name);
991 read_branches();
993 if (query) {
994 struct transport *transport;
995 const struct ref *remote_refs;
997 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
998 states->remote->url[0] : NULL);
999 remote_refs = transport_get_remote_refs(transport, NULL);
1001 states->queried = 1;
1002 if (query & GET_REF_STATES)
1003 get_ref_states(remote_refs, states);
1004 if (query & GET_HEAD_NAMES)
1005 get_head_names(remote_refs, states);
1006 if (query & GET_PUSH_REF_STATES)
1007 get_push_ref_states(remote_refs, states);
1008 transport_disconnect(transport);
1009 } else {
1010 for_each_ref(append_ref_to_tracked_list, states);
1011 string_list_sort(&states->tracked);
1012 get_push_ref_states_noquery(states);
1015 return 0;
1018 struct show_info {
1019 struct string_list list;
1020 struct ref_states states;
1021 int width, width2;
1022 int any_rebase;
1025 #define SHOW_INFO_INIT { \
1026 .list = STRING_LIST_INIT_DUP, \
1027 .states = REF_STATES_INIT, \
1030 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
1032 struct show_info *info = cb_data;
1033 int n = strlen(item->string);
1034 if (n > info->width)
1035 info->width = n;
1036 string_list_insert(&info->list, item->string);
1037 return 0;
1040 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
1042 struct show_info *info = cb_data;
1043 struct ref_states *states = &info->states;
1044 const char *name = item->string;
1046 if (states->queried) {
1047 const char *fmt = "%s";
1048 const char *arg = "";
1049 if (string_list_has_string(&states->new_refs, name)) {
1050 fmt = _(" new (next fetch will store in remotes/%s)");
1051 arg = states->remote->name;
1052 } else if (string_list_has_string(&states->tracked, name))
1053 arg = _(" tracked");
1054 else if (string_list_has_string(&states->skipped, name))
1055 arg = _(" skipped");
1056 else if (string_list_has_string(&states->stale, name))
1057 arg = _(" stale (use 'git remote prune' to remove)");
1058 else
1059 arg = _(" ???");
1060 printf(" %-*s", info->width, name);
1061 printf(fmt, arg);
1062 printf("\n");
1063 } else
1064 printf(" %s\n", name);
1066 return 0;
1069 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
1071 struct show_info *show_info = cb_data;
1072 struct ref_states *states = &show_info->states;
1073 struct branch_info *branch_info = branch_item->util;
1074 struct string_list_item *item;
1075 int n;
1077 if (!branch_info->merge.nr || !branch_info->remote_name ||
1078 strcmp(states->remote->name, branch_info->remote_name))
1079 return 0;
1080 if ((n = strlen(branch_item->string)) > show_info->width)
1081 show_info->width = n;
1082 if (branch_info->rebase >= REBASE_TRUE)
1083 show_info->any_rebase = 1;
1085 item = string_list_insert(&show_info->list, branch_item->string);
1086 item->util = branch_info;
1088 return 0;
1091 static int show_local_info_item(struct string_list_item *item, void *cb_data)
1093 struct show_info *show_info = cb_data;
1094 struct branch_info *branch_info = item->util;
1095 struct string_list *merge = &branch_info->merge;
1096 int width = show_info->width + 4;
1097 int i;
1099 if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) {
1100 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1101 item->string);
1102 return 0;
1105 printf(" %-*s ", show_info->width, item->string);
1106 if (branch_info->rebase >= REBASE_TRUE) {
1107 const char *msg;
1108 if (branch_info->rebase == REBASE_INTERACTIVE)
1109 msg = _("rebases interactively onto remote %s");
1110 else if (branch_info->rebase == REBASE_MERGES)
1111 msg = _("rebases interactively (with merges) onto "
1112 "remote %s");
1113 else
1114 msg = _("rebases onto remote %s");
1115 printf_ln(msg, merge->items[0].string);
1116 return 0;
1117 } else if (show_info->any_rebase) {
1118 printf_ln(_(" merges with remote %s"), merge->items[0].string);
1119 width++;
1120 } else {
1121 printf_ln(_("merges with remote %s"), merge->items[0].string);
1123 for (i = 1; i < merge->nr; i++)
1124 printf(_("%-*s and with remote %s\n"), width, "",
1125 merge->items[i].string);
1127 return 0;
1130 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
1132 struct show_info *show_info = cb_data;
1133 struct push_info *push_info = push_item->util;
1134 struct string_list_item *item;
1135 int n;
1136 if ((n = strlen(push_item->string)) > show_info->width)
1137 show_info->width = n;
1138 if ((n = strlen(push_info->dest)) > show_info->width2)
1139 show_info->width2 = n;
1140 item = string_list_append(&show_info->list, push_item->string);
1141 item->util = push_item->util;
1142 return 0;
1146 * Sorting comparison for a string list that has push_info
1147 * structs in its util field
1149 static int cmp_string_with_push(const void *va, const void *vb)
1151 const struct string_list_item *a = va;
1152 const struct string_list_item *b = vb;
1153 const struct push_info *a_push = a->util;
1154 const struct push_info *b_push = b->util;
1155 int cmp = strcmp(a->string, b->string);
1156 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1159 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1161 struct show_info *show_info = cb_data;
1162 struct push_info *push_info = item->util;
1163 const char *src = item->string, *status = NULL;
1165 switch (push_info->status) {
1166 case PUSH_STATUS_CREATE:
1167 status = _("create");
1168 break;
1169 case PUSH_STATUS_DELETE:
1170 status = _("delete");
1171 src = _("(none)");
1172 break;
1173 case PUSH_STATUS_UPTODATE:
1174 status = _("up to date");
1175 break;
1176 case PUSH_STATUS_FASTFORWARD:
1177 status = _("fast-forwardable");
1178 break;
1179 case PUSH_STATUS_OUTOFDATE:
1180 status = _("local out of date");
1181 break;
1182 case PUSH_STATUS_NOTQUERIED:
1183 break;
1185 if (status) {
1186 if (push_info->forced)
1187 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1188 show_info->width2, push_info->dest, status);
1189 else
1190 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1191 show_info->width2, push_info->dest, status);
1192 } else {
1193 if (push_info->forced)
1194 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1195 push_info->dest);
1196 else
1197 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1198 push_info->dest);
1200 return 0;
1203 static int get_one_entry(struct remote *remote, void *priv)
1205 struct string_list *list = priv;
1206 struct strbuf remote_info_buf = STRBUF_INIT;
1207 const char **url;
1208 int i, url_nr;
1210 if (remote->url_nr > 0) {
1211 struct strbuf promisor_config = STRBUF_INIT;
1212 const char *partial_clone_filter = NULL;
1214 strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
1215 strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url[0]);
1216 if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
1217 strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
1219 strbuf_release(&promisor_config);
1220 string_list_append(list, remote->name)->util =
1221 strbuf_detach(&remote_info_buf, NULL);
1222 } else
1223 string_list_append(list, remote->name)->util = NULL;
1224 if (remote->pushurl_nr) {
1225 url = remote->pushurl;
1226 url_nr = remote->pushurl_nr;
1227 } else {
1228 url = remote->url;
1229 url_nr = remote->url_nr;
1231 for (i = 0; i < url_nr; i++)
1233 strbuf_addf(&remote_info_buf, "%s (push)", url[i]);
1234 string_list_append(list, remote->name)->util =
1235 strbuf_detach(&remote_info_buf, NULL);
1238 return 0;
1241 static int show_all(void)
1243 struct string_list list = STRING_LIST_INIT_DUP;
1244 int result;
1246 result = for_each_remote(get_one_entry, &list);
1248 if (!result) {
1249 int i;
1251 string_list_sort(&list);
1252 for (i = 0; i < list.nr; i++) {
1253 struct string_list_item *item = list.items + i;
1254 if (verbose)
1255 printf("%s\t%s\n", item->string,
1256 item->util ? (const char *)item->util : "");
1257 else {
1258 if (i && !strcmp((item - 1)->string, item->string))
1259 continue;
1260 printf("%s\n", item->string);
1264 string_list_clear(&list, 1);
1265 return result;
1268 static int show(int argc, const char **argv, const char *prefix)
1270 int no_query = 0, result = 0, query_flag = 0;
1271 struct option options[] = {
1272 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1273 OPT_END()
1275 struct show_info info = SHOW_INFO_INIT;
1277 argc = parse_options(argc, argv, prefix, options,
1278 builtin_remote_show_usage,
1281 if (argc < 1)
1282 return show_all();
1284 if (!no_query)
1285 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1287 for (; argc; argc--, argv++) {
1288 int i;
1289 const char **url;
1290 int url_nr;
1292 get_remote_ref_states(*argv, &info.states, query_flag);
1294 printf_ln(_("* remote %s"), *argv);
1295 printf_ln(_(" Fetch URL: %s"), info.states.remote->url_nr > 0 ?
1296 info.states.remote->url[0] : _("(no URL)"));
1297 if (info.states.remote->pushurl_nr) {
1298 url = info.states.remote->pushurl;
1299 url_nr = info.states.remote->pushurl_nr;
1300 } else {
1301 url = info.states.remote->url;
1302 url_nr = info.states.remote->url_nr;
1304 for (i = 0; i < url_nr; i++)
1306 * TRANSLATORS: the colon ':' should align
1307 * with the one in " Fetch URL: %s"
1308 * translation.
1310 printf_ln(_(" Push URL: %s"), url[i]);
1311 if (!i)
1312 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1313 if (no_query)
1314 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1315 else if (!info.states.heads.nr)
1316 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1317 else if (info.states.heads.nr == 1)
1318 printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string);
1319 else {
1320 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1321 " may be one of the following):\n"));
1322 for (i = 0; i < info.states.heads.nr; i++)
1323 printf(" %s\n", info.states.heads.items[i].string);
1326 /* remote branch info */
1327 info.width = 0;
1328 for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info);
1329 for_each_string_list(&info.states.skipped, add_remote_to_show_info, &info);
1330 for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info);
1331 for_each_string_list(&info.states.stale, add_remote_to_show_info, &info);
1332 if (info.list.nr)
1333 printf_ln(Q_(" Remote branch:%s",
1334 " Remote branches:%s",
1335 info.list.nr),
1336 no_query ? _(" (status not queried)") : "");
1337 for_each_string_list(&info.list, show_remote_info_item, &info);
1338 string_list_clear(&info.list, 0);
1340 /* git pull info */
1341 info.width = 0;
1342 info.any_rebase = 0;
1343 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1344 if (info.list.nr)
1345 printf_ln(Q_(" Local branch configured for 'git pull':",
1346 " Local branches configured for 'git pull':",
1347 info.list.nr));
1348 for_each_string_list(&info.list, show_local_info_item, &info);
1349 string_list_clear(&info.list, 0);
1351 /* git push info */
1352 if (info.states.remote->mirror)
1353 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1355 info.width = info.width2 = 0;
1356 for_each_string_list(&info.states.push, add_push_to_show_info, &info);
1357 QSORT(info.list.items, info.list.nr, cmp_string_with_push);
1358 if (info.list.nr)
1359 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1360 " Local refs configured for 'git push'%s:",
1361 info.list.nr),
1362 no_query ? _(" (status not queried)") : "");
1363 for_each_string_list(&info.list, show_push_info_item, &info);
1364 string_list_clear(&info.list, 0);
1366 free_remote_ref_states(&info.states);
1369 return result;
1372 static int set_head(int argc, const char **argv, const char *prefix)
1374 int i, opt_a = 0, opt_d = 0, result = 0;
1375 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1376 char *head_name = NULL;
1378 struct option options[] = {
1379 OPT_BOOL('a', "auto", &opt_a,
1380 N_("set refs/remotes/<name>/HEAD according to remote")),
1381 OPT_BOOL('d', "delete", &opt_d,
1382 N_("delete refs/remotes/<name>/HEAD")),
1383 OPT_END()
1385 argc = parse_options(argc, argv, prefix, options,
1386 builtin_remote_sethead_usage, 0);
1387 if (argc)
1388 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1390 if (!opt_a && !opt_d && argc == 2) {
1391 head_name = xstrdup(argv[1]);
1392 } else if (opt_a && !opt_d && argc == 1) {
1393 struct ref_states states = REF_STATES_INIT;
1394 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1395 if (!states.heads.nr)
1396 result |= error(_("Cannot determine remote HEAD"));
1397 else if (states.heads.nr > 1) {
1398 result |= error(_("Multiple remote HEAD branches. "
1399 "Please choose one explicitly with:"));
1400 for (i = 0; i < states.heads.nr; i++)
1401 fprintf(stderr, " git remote set-head %s %s\n",
1402 argv[0], states.heads.items[i].string);
1403 } else
1404 head_name = xstrdup(states.heads.items[0].string);
1405 free_remote_ref_states(&states);
1406 } else if (opt_d && !opt_a && argc == 1) {
1407 if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF))
1408 result |= error(_("Could not delete %s"), buf.buf);
1409 } else
1410 usage_with_options(builtin_remote_sethead_usage, options);
1412 if (head_name) {
1413 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1414 /* make sure it's valid */
1415 if (!ref_exists(buf2.buf))
1416 result |= error(_("Not a valid ref: %s"), buf2.buf);
1417 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1418 result |= error(_("Could not setup %s"), buf.buf);
1419 else if (opt_a)
1420 printf("%s/HEAD set to %s\n", argv[0], head_name);
1421 free(head_name);
1424 strbuf_release(&buf);
1425 strbuf_release(&buf2);
1426 return result;
1429 static int prune_remote(const char *remote, int dry_run)
1431 int result = 0;
1432 struct ref_states states = REF_STATES_INIT;
1433 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1434 struct string_list_item *item;
1435 const char *dangling_msg = dry_run
1436 ? _(" %s will become dangling!")
1437 : _(" %s has become dangling!");
1439 get_remote_ref_states(remote, &states, GET_REF_STATES);
1441 if (!states.stale.nr) {
1442 free_remote_ref_states(&states);
1443 return 0;
1446 printf_ln(_("Pruning %s"), remote);
1447 printf_ln(_("URL: %s"),
1448 states.remote->url_nr
1449 ? states.remote->url[0]
1450 : _("(no URL)"));
1452 for_each_string_list_item(item, &states.stale)
1453 string_list_append(&refs_to_prune, item->util);
1454 string_list_sort(&refs_to_prune);
1456 if (!dry_run)
1457 result |= delete_refs("remote: prune", &refs_to_prune, 0);
1459 for_each_string_list_item(item, &states.stale) {
1460 const char *refname = item->util;
1462 if (dry_run)
1463 printf_ln(_(" * [would prune] %s"),
1464 abbrev_ref(refname, "refs/remotes/"));
1465 else
1466 printf_ln(_(" * [pruned] %s"),
1467 abbrev_ref(refname, "refs/remotes/"));
1470 warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
1472 string_list_clear(&refs_to_prune, 0);
1473 free_remote_ref_states(&states);
1474 return result;
1477 static int prune(int argc, const char **argv, const char *prefix)
1479 int dry_run = 0, result = 0;
1480 struct option options[] = {
1481 OPT__DRY_RUN(&dry_run, N_("dry run")),
1482 OPT_END()
1485 argc = parse_options(argc, argv, prefix, options,
1486 builtin_remote_prune_usage, 0);
1488 if (argc < 1)
1489 usage_with_options(builtin_remote_prune_usage, options);
1491 for (; argc; argc--, argv++)
1492 result |= prune_remote(*argv, dry_run);
1494 return result;
1497 static int get_remote_default(const char *key, const char *value UNUSED, void *priv)
1499 if (strcmp(key, "remotes.default") == 0) {
1500 int *found = priv;
1501 *found = 1;
1503 return 0;
1506 static int update(int argc, const char **argv, const char *prefix)
1508 int i, prune = -1;
1509 struct option options[] = {
1510 OPT_BOOL('p', "prune", &prune,
1511 N_("prune remotes after fetching")),
1512 OPT_END()
1514 struct child_process cmd = CHILD_PROCESS_INIT;
1515 int default_defined = 0;
1517 argc = parse_options(argc, argv, prefix, options,
1518 builtin_remote_update_usage,
1519 PARSE_OPT_KEEP_ARGV0);
1521 strvec_push(&cmd.args, "fetch");
1523 if (prune != -1)
1524 strvec_push(&cmd.args, prune ? "--prune" : "--no-prune");
1525 if (verbose)
1526 strvec_push(&cmd.args, "-v");
1527 strvec_push(&cmd.args, "--multiple");
1528 if (argc < 2)
1529 strvec_push(&cmd.args, "default");
1530 for (i = 1; i < argc; i++)
1531 strvec_push(&cmd.args, argv[i]);
1533 if (strcmp(cmd.args.v[cmd.args.nr-1], "default") == 0) {
1534 git_config(get_remote_default, &default_defined);
1535 if (!default_defined) {
1536 strvec_pop(&cmd.args);
1537 strvec_push(&cmd.args, "--all");
1541 cmd.git_cmd = 1;
1542 return run_command(&cmd);
1545 static int remove_all_fetch_refspecs(const char *key)
1547 return git_config_set_multivar_gently(key, NULL, NULL,
1548 CONFIG_FLAGS_MULTI_REPLACE);
1551 static void add_branches(struct remote *remote, const char **branches,
1552 const char *key)
1554 const char *remotename = remote->name;
1555 int mirror = remote->mirror;
1556 struct strbuf refspec = STRBUF_INIT;
1558 for (; *branches; branches++)
1559 add_branch(key, *branches, remotename, mirror, &refspec);
1561 strbuf_release(&refspec);
1564 static int set_remote_branches(const char *remotename, const char **branches,
1565 int add_mode)
1567 struct strbuf key = STRBUF_INIT;
1568 struct remote *remote;
1570 strbuf_addf(&key, "remote.%s.fetch", remotename);
1572 remote = remote_get(remotename);
1573 if (!remote_is_configured(remote, 1)) {
1574 error(_("No such remote '%s'"), remotename);
1575 exit(2);
1578 if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
1579 strbuf_release(&key);
1580 return 1;
1582 add_branches(remote, branches, key.buf);
1584 strbuf_release(&key);
1585 return 0;
1588 static int set_branches(int argc, const char **argv, const char *prefix)
1590 int add_mode = 0;
1591 struct option options[] = {
1592 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1593 OPT_END()
1596 argc = parse_options(argc, argv, prefix, options,
1597 builtin_remote_setbranches_usage, 0);
1598 if (argc == 0) {
1599 error(_("no remote specified"));
1600 usage_with_options(builtin_remote_setbranches_usage, options);
1602 argv[argc] = NULL;
1604 return set_remote_branches(argv[0], argv + 1, add_mode);
1607 static int get_url(int argc, const char **argv, const char *prefix)
1609 int i, push_mode = 0, all_mode = 0;
1610 const char *remotename = NULL;
1611 struct remote *remote;
1612 const char **url;
1613 int url_nr;
1614 struct option options[] = {
1615 OPT_BOOL('\0', "push", &push_mode,
1616 N_("query push URLs rather than fetch URLs")),
1617 OPT_BOOL('\0', "all", &all_mode,
1618 N_("return all URLs")),
1619 OPT_END()
1621 argc = parse_options(argc, argv, prefix, options,
1622 builtin_remote_geturl_usage, 0);
1624 if (argc != 1)
1625 usage_with_options(builtin_remote_geturl_usage, options);
1627 remotename = argv[0];
1629 remote = remote_get(remotename);
1630 if (!remote_is_configured(remote, 1)) {
1631 error(_("No such remote '%s'"), remotename);
1632 exit(2);
1635 url_nr = 0;
1636 if (push_mode) {
1637 url = remote->pushurl;
1638 url_nr = remote->pushurl_nr;
1640 /* else fetch mode */
1642 /* Use the fetch URL when no push URLs were found or requested. */
1643 if (!url_nr) {
1644 url = remote->url;
1645 url_nr = remote->url_nr;
1648 if (!url_nr)
1649 die(_("no URLs configured for remote '%s'"), remotename);
1651 if (all_mode) {
1652 for (i = 0; i < url_nr; i++)
1653 printf_ln("%s", url[i]);
1654 } else {
1655 printf_ln("%s", *url);
1658 return 0;
1661 static int set_url(int argc, const char **argv, const char *prefix)
1663 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1664 int matches = 0, negative_matches = 0;
1665 const char *remotename = NULL;
1666 const char *newurl = NULL;
1667 const char *oldurl = NULL;
1668 struct remote *remote;
1669 regex_t old_regex;
1670 const char **urlset;
1671 int urlset_nr;
1672 struct strbuf name_buf = STRBUF_INIT;
1673 struct option options[] = {
1674 OPT_BOOL('\0', "push", &push_mode,
1675 N_("manipulate push URLs")),
1676 OPT_BOOL('\0', "add", &add_mode,
1677 N_("add URL")),
1678 OPT_BOOL('\0', "delete", &delete_mode,
1679 N_("delete URLs")),
1680 OPT_END()
1682 argc = parse_options(argc, argv, prefix, options,
1683 builtin_remote_seturl_usage,
1684 PARSE_OPT_KEEP_ARGV0);
1686 if (add_mode && delete_mode)
1687 die(_("--add --delete doesn't make sense"));
1689 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1690 usage_with_options(builtin_remote_seturl_usage, options);
1692 remotename = argv[1];
1693 newurl = argv[2];
1694 if (argc > 3)
1695 oldurl = argv[3];
1697 if (delete_mode)
1698 oldurl = newurl;
1700 remote = remote_get(remotename);
1701 if (!remote_is_configured(remote, 1)) {
1702 error(_("No such remote '%s'"), remotename);
1703 exit(2);
1706 if (push_mode) {
1707 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1708 urlset = remote->pushurl;
1709 urlset_nr = remote->pushurl_nr;
1710 } else {
1711 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1712 urlset = remote->url;
1713 urlset_nr = remote->url_nr;
1716 /* Special cases that add new entry. */
1717 if ((!oldurl && !delete_mode) || add_mode) {
1718 if (add_mode)
1719 git_config_set_multivar(name_buf.buf, newurl,
1720 "^$", 0);
1721 else
1722 git_config_set(name_buf.buf, newurl);
1723 goto out;
1726 /* Old URL specified. Demand that one matches. */
1727 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1728 die(_("Invalid old URL pattern: %s"), oldurl);
1730 for (i = 0; i < urlset_nr; i++)
1731 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1732 matches++;
1733 else
1734 negative_matches++;
1735 if (!delete_mode && !matches)
1736 die(_("No such URL found: %s"), oldurl);
1737 if (delete_mode && !negative_matches && !push_mode)
1738 die(_("Will not delete all non-push URLs"));
1740 regfree(&old_regex);
1742 if (!delete_mode)
1743 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1744 else
1745 git_config_set_multivar(name_buf.buf, NULL, oldurl,
1746 CONFIG_FLAGS_MULTI_REPLACE);
1747 out:
1748 strbuf_release(&name_buf);
1749 return 0;
1752 int cmd_remote(int argc, const char **argv, const char *prefix)
1754 parse_opt_subcommand_fn *fn = NULL;
1755 struct option options[] = {
1756 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1757 OPT_SUBCOMMAND("add", &fn, add),
1758 OPT_SUBCOMMAND("rename", &fn, mv),
1759 OPT_SUBCOMMAND_F("rm", &fn, rm, PARSE_OPT_NOCOMPLETE),
1760 OPT_SUBCOMMAND("remove", &fn, rm),
1761 OPT_SUBCOMMAND("set-head", &fn, set_head),
1762 OPT_SUBCOMMAND("set-branches", &fn, set_branches),
1763 OPT_SUBCOMMAND("get-url", &fn, get_url),
1764 OPT_SUBCOMMAND("set-url", &fn, set_url),
1765 OPT_SUBCOMMAND("show", &fn, show),
1766 OPT_SUBCOMMAND("prune", &fn, prune),
1767 OPT_SUBCOMMAND("update", &fn, update),
1768 OPT_END()
1771 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1772 PARSE_OPT_SUBCOMMAND_OPTIONAL);
1774 if (fn) {
1775 return !!fn(argc, argv, prefix);
1776 } else {
1777 if (argc) {
1778 error(_("unknown subcommand: `%s'"), argv[0]);
1779 usage_with_options(builtin_remote_usage, options);
1781 return !!show_all();