Merge branch 'cl/rerere-train-with-no-sign'
[git/debian.git] / builtin / remote.c
blobd9b8746cb3cb6cf78c8790971ad8f23e6a91d2e3
1 #include "builtin.h"
2 #include "config.h"
3 #include "parse-options.h"
4 #include "transport.h"
5 #include "remote.h"
6 #include "string-list.h"
7 #include "strbuf.h"
8 #include "run-command.h"
9 #include "rebase.h"
10 #include "refs.h"
11 #include "refspec.h"
12 #include "object-store.h"
13 #include "strvec.h"
14 #include "commit-reach.h"
15 #include "progress.h"
17 static const char * const builtin_remote_usage[] = {
18 "git remote [-v | --verbose]",
19 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
20 N_("git remote rename [--[no-]progress] <old> <new>"),
21 N_("git remote remove <name>"),
22 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
23 N_("git remote [-v | --verbose] show [-n] <name>"),
24 N_("git remote prune [-n | --dry-run] <name>"),
25 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
26 N_("git remote set-branches [--add] <name> <branch>..."),
27 N_("git remote get-url [--push] [--all] <name>"),
28 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
29 N_("git remote set-url --add <name> <newurl>"),
30 N_("git remote set-url --delete <name> <url>"),
31 NULL
34 static const char * const builtin_remote_add_usage[] = {
35 N_("git remote add [<options>] <name> <url>"),
36 NULL
39 static const char * const builtin_remote_rename_usage[] = {
40 N_("git remote rename [--[no-]progress] <old> <new>"),
41 NULL
44 static const char * const builtin_remote_rm_usage[] = {
45 N_("git remote remove <name>"),
46 NULL
49 static const char * const builtin_remote_sethead_usage[] = {
50 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
51 NULL
54 static const char * const builtin_remote_setbranches_usage[] = {
55 N_("git remote set-branches <name> <branch>..."),
56 N_("git remote set-branches --add <name> <branch>..."),
57 NULL
60 static const char * const builtin_remote_show_usage[] = {
61 N_("git remote show [<options>] <name>"),
62 NULL
65 static const char * const builtin_remote_prune_usage[] = {
66 N_("git remote prune [<options>] <name>"),
67 NULL
70 static const char * const builtin_remote_update_usage[] = {
71 N_("git remote update [<options>] [<group> | <remote>]..."),
72 NULL
75 static const char * const builtin_remote_geturl_usage[] = {
76 N_("git remote get-url [--push] [--all] <name>"),
77 NULL
80 static const char * const builtin_remote_seturl_usage[] = {
81 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
82 N_("git remote set-url --add <name> <newurl>"),
83 N_("git remote set-url --delete <name> <url>"),
84 NULL
87 #define GET_REF_STATES (1<<0)
88 #define GET_HEAD_NAMES (1<<1)
89 #define GET_PUSH_REF_STATES (1<<2)
91 static int verbose;
93 static int fetch_remote(const char *name)
95 const char *argv[] = { "fetch", name, NULL, NULL };
96 if (verbose) {
97 argv[1] = "-v";
98 argv[2] = name;
100 printf_ln(_("Updating %s"), name);
101 if (run_command_v_opt(argv, RUN_GIT_CMD))
102 return error(_("Could not fetch %s"), name);
103 return 0;
106 enum {
107 TAGS_UNSET = 0,
108 TAGS_DEFAULT = 1,
109 TAGS_SET = 2
112 #define MIRROR_NONE 0
113 #define MIRROR_FETCH 1
114 #define MIRROR_PUSH 2
115 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
117 static void add_branch(const char *key, const char *branchname,
118 const char *remotename, int mirror, struct strbuf *tmp)
120 strbuf_reset(tmp);
121 strbuf_addch(tmp, '+');
122 if (mirror)
123 strbuf_addf(tmp, "refs/%s:refs/%s",
124 branchname, branchname);
125 else
126 strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
127 branchname, remotename, branchname);
128 git_config_set_multivar(key, tmp->buf, "^$", 0);
131 static const char mirror_advice[] =
132 N_("--mirror is dangerous and deprecated; please\n"
133 "\t use --mirror=fetch or --mirror=push instead");
135 static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
137 unsigned *mirror = opt->value;
138 if (not)
139 *mirror = MIRROR_NONE;
140 else if (!arg) {
141 warning("%s", _(mirror_advice));
142 *mirror = MIRROR_BOTH;
144 else if (!strcmp(arg, "fetch"))
145 *mirror = MIRROR_FETCH;
146 else if (!strcmp(arg, "push"))
147 *mirror = MIRROR_PUSH;
148 else
149 return error(_("unknown mirror argument: %s"), arg);
150 return 0;
153 static int add(int argc, const char **argv)
155 int fetch = 0, fetch_tags = TAGS_DEFAULT;
156 unsigned mirror = MIRROR_NONE;
157 struct string_list track = STRING_LIST_INIT_NODUP;
158 const char *master = NULL;
159 struct remote *remote;
160 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
161 const char *name, *url;
162 int i;
164 struct option options[] = {
165 OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
166 OPT_SET_INT(0, "tags", &fetch_tags,
167 N_("import all tags and associated objects when fetching"),
168 TAGS_SET),
169 OPT_SET_INT(0, NULL, &fetch_tags,
170 N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET),
171 OPT_STRING_LIST('t', "track", &track, N_("branch"),
172 N_("branch(es) to track")),
173 OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")),
174 OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)",
175 N_("set up remote as a mirror to push to or fetch from"),
176 PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),
177 OPT_END()
180 argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage,
183 if (argc != 2)
184 usage_with_options(builtin_remote_add_usage, options);
186 if (mirror && master)
187 die(_("specifying a master branch makes no sense with --mirror"));
188 if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
189 die(_("specifying branches to track makes sense only with fetch mirrors"));
191 name = argv[0];
192 url = argv[1];
194 remote = remote_get(name);
195 if (remote_is_configured(remote, 1)) {
196 error(_("remote %s already exists."), name);
197 exit(3);
200 if (!valid_remote_name(name))
201 die(_("'%s' is not a valid remote name"), name);
203 strbuf_addf(&buf, "remote.%s.url", name);
204 git_config_set(buf.buf, url);
206 if (!mirror || mirror & MIRROR_FETCH) {
207 strbuf_reset(&buf);
208 strbuf_addf(&buf, "remote.%s.fetch", name);
209 if (track.nr == 0)
210 string_list_append(&track, "*");
211 for (i = 0; i < track.nr; i++) {
212 add_branch(buf.buf, track.items[i].string,
213 name, mirror, &buf2);
217 if (mirror & MIRROR_PUSH) {
218 strbuf_reset(&buf);
219 strbuf_addf(&buf, "remote.%s.mirror", name);
220 git_config_set(buf.buf, "true");
223 if (fetch_tags != TAGS_DEFAULT) {
224 strbuf_reset(&buf);
225 strbuf_addf(&buf, "remote.%s.tagOpt", name);
226 git_config_set(buf.buf,
227 fetch_tags == TAGS_SET ? "--tags" : "--no-tags");
230 if (fetch && fetch_remote(name))
231 return 1;
233 if (master) {
234 strbuf_reset(&buf);
235 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
237 strbuf_reset(&buf2);
238 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
240 if (create_symref(buf.buf, buf2.buf, "remote add"))
241 return error(_("Could not setup master '%s'"), master);
244 strbuf_release(&buf);
245 strbuf_release(&buf2);
246 string_list_clear(&track, 0);
248 return 0;
251 struct branch_info {
252 char *remote_name;
253 struct string_list merge;
254 enum rebase_type rebase;
255 char *push_remote_name;
258 static struct string_list branch_list = STRING_LIST_INIT_NODUP;
260 static const char *abbrev_ref(const char *name, const char *prefix)
262 skip_prefix(name, prefix, &name);
263 return name;
265 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
267 static int config_read_branches(const char *key, const char *value, void *cb)
269 const char *orig_key = key;
270 char *name;
271 struct string_list_item *item;
272 struct branch_info *info;
273 enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type;
274 size_t key_len;
276 if (!starts_with(key, "branch."))
277 return 0;
279 key += strlen("branch.");
280 if (strip_suffix(key, ".remote", &key_len))
281 type = REMOTE;
282 else if (strip_suffix(key, ".merge", &key_len))
283 type = MERGE;
284 else if (strip_suffix(key, ".rebase", &key_len))
285 type = REBASE;
286 else if (strip_suffix(key, ".pushremote", &key_len))
287 type = PUSH_REMOTE;
288 else
289 return 0;
290 name = xmemdupz(key, key_len);
292 item = string_list_insert(&branch_list, name);
294 if (!item->util)
295 item->util = xcalloc(1, sizeof(struct branch_info));
296 info = item->util;
297 switch (type) {
298 case REMOTE:
299 if (info->remote_name)
300 warning(_("more than one %s"), orig_key);
301 info->remote_name = xstrdup(value);
302 break;
303 case MERGE: {
304 char *space = strchr(value, ' ');
305 value = abbrev_branch(value);
306 while (space) {
307 char *merge;
308 merge = xstrndup(value, space - value);
309 string_list_append(&info->merge, merge);
310 value = abbrev_branch(space + 1);
311 space = strchr(value, ' ');
313 string_list_append(&info->merge, xstrdup(value));
314 break;
316 case REBASE:
318 * Consider invalid values as false and check the
319 * truth value with >= REBASE_TRUE.
321 info->rebase = rebase_parse_value(value);
322 if (info->rebase == REBASE_INVALID)
323 warning(_("unhandled branch.%s.rebase=%s; assuming "
324 "'true'"), name, value);
325 break;
326 case PUSH_REMOTE:
327 if (info->push_remote_name)
328 warning(_("more than one %s"), orig_key);
329 info->push_remote_name = xstrdup(value);
330 break;
331 default:
332 BUG("unexpected type=%d", type);
335 return 0;
338 static void read_branches(void)
340 if (branch_list.nr)
341 return;
342 git_config(config_read_branches, NULL);
345 struct ref_states {
346 struct remote *remote;
347 struct string_list new_refs, skipped, stale, tracked, heads, push;
348 int queried;
351 #define REF_STATES_INIT { \
352 .new_refs = STRING_LIST_INIT_DUP, \
353 .skipped = STRING_LIST_INIT_DUP, \
354 .stale = STRING_LIST_INIT_DUP, \
355 .tracked = STRING_LIST_INIT_DUP, \
356 .heads = STRING_LIST_INIT_DUP, \
357 .push = STRING_LIST_INIT_DUP, \
360 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
362 struct ref *fetch_map = NULL, **tail = &fetch_map;
363 struct ref *ref, *stale_refs;
364 int i;
366 for (i = 0; i < states->remote->fetch.nr; i++)
367 if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1))
368 die(_("Could not get fetch map for refspec %s"),
369 states->remote->fetch.raw[i]);
371 for (ref = fetch_map; ref; ref = ref->next) {
372 if (omit_name_by_refspec(ref->name, &states->remote->fetch))
373 string_list_append(&states->skipped, abbrev_branch(ref->name));
374 else if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
375 string_list_append(&states->new_refs, abbrev_branch(ref->name));
376 else
377 string_list_append(&states->tracked, abbrev_branch(ref->name));
379 stale_refs = get_stale_heads(&states->remote->fetch, fetch_map);
380 for (ref = stale_refs; ref; ref = ref->next) {
381 struct string_list_item *item =
382 string_list_append(&states->stale, abbrev_branch(ref->name));
383 item->util = xstrdup(ref->name);
385 free_refs(stale_refs);
386 free_refs(fetch_map);
388 string_list_sort(&states->new_refs);
389 string_list_sort(&states->skipped);
390 string_list_sort(&states->tracked);
391 string_list_sort(&states->stale);
393 return 0;
396 struct push_info {
397 char *dest;
398 int forced;
399 enum {
400 PUSH_STATUS_CREATE = 0,
401 PUSH_STATUS_DELETE,
402 PUSH_STATUS_UPTODATE,
403 PUSH_STATUS_FASTFORWARD,
404 PUSH_STATUS_OUTOFDATE,
405 PUSH_STATUS_NOTQUERIED
406 } status;
409 static int get_push_ref_states(const struct ref *remote_refs,
410 struct ref_states *states)
412 struct remote *remote = states->remote;
413 struct ref *ref, *local_refs, *push_map;
414 if (remote->mirror)
415 return 0;
417 local_refs = get_local_heads();
418 push_map = copy_ref_list(remote_refs);
420 match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE);
422 for (ref = push_map; ref; ref = ref->next) {
423 struct string_list_item *item;
424 struct push_info *info;
426 if (!ref->peer_ref)
427 continue;
428 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
430 item = string_list_append(&states->push,
431 abbrev_branch(ref->peer_ref->name));
432 item->util = xcalloc(1, sizeof(struct push_info));
433 info = item->util;
434 info->forced = ref->force;
435 info->dest = xstrdup(abbrev_branch(ref->name));
437 if (is_null_oid(&ref->new_oid)) {
438 info->status = PUSH_STATUS_DELETE;
439 } else if (oideq(&ref->old_oid, &ref->new_oid))
440 info->status = PUSH_STATUS_UPTODATE;
441 else if (is_null_oid(&ref->old_oid))
442 info->status = PUSH_STATUS_CREATE;
443 else if (has_object_file(&ref->old_oid) &&
444 ref_newer(&ref->new_oid, &ref->old_oid))
445 info->status = PUSH_STATUS_FASTFORWARD;
446 else
447 info->status = PUSH_STATUS_OUTOFDATE;
449 free_refs(local_refs);
450 free_refs(push_map);
451 return 0;
454 static int get_push_ref_states_noquery(struct ref_states *states)
456 int i;
457 struct remote *remote = states->remote;
458 struct string_list_item *item;
459 struct push_info *info;
461 if (remote->mirror)
462 return 0;
464 if (!remote->push.nr) {
465 item = string_list_append(&states->push, _("(matching)"));
466 info = item->util = xcalloc(1, sizeof(struct push_info));
467 info->status = PUSH_STATUS_NOTQUERIED;
468 info->dest = xstrdup(item->string);
470 for (i = 0; i < remote->push.nr; i++) {
471 const struct refspec_item *spec = &remote->push.items[i];
472 if (spec->matching)
473 item = string_list_append(&states->push, _("(matching)"));
474 else if (strlen(spec->src))
475 item = string_list_append(&states->push, spec->src);
476 else
477 item = string_list_append(&states->push, _("(delete)"));
479 info = item->util = xcalloc(1, sizeof(struct push_info));
480 info->forced = spec->force;
481 info->status = PUSH_STATUS_NOTQUERIED;
482 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
484 return 0;
487 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
489 struct ref *ref, *matches;
490 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
491 struct refspec_item refspec;
493 memset(&refspec, 0, sizeof(refspec));
494 refspec.force = 0;
495 refspec.pattern = 1;
496 refspec.src = refspec.dst = "refs/heads/*";
497 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
498 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
499 fetch_map, 1);
500 for (ref = matches; ref; ref = ref->next)
501 string_list_append(&states->heads, abbrev_branch(ref->name));
503 free_refs(fetch_map);
504 free_refs(matches);
506 return 0;
509 struct known_remote {
510 struct known_remote *next;
511 struct remote *remote;
514 struct known_remotes {
515 struct remote *to_delete;
516 struct known_remote *list;
519 static int add_known_remote(struct remote *remote, void *cb_data)
521 struct known_remotes *all = cb_data;
522 struct known_remote *r;
524 if (!strcmp(all->to_delete->name, remote->name))
525 return 0;
527 r = xmalloc(sizeof(*r));
528 r->remote = remote;
529 r->next = all->list;
530 all->list = r;
531 return 0;
534 struct branches_for_remote {
535 struct remote *remote;
536 struct string_list *branches, *skipped;
537 struct known_remotes *keep;
540 static int add_branch_for_removal(const char *refname,
541 const struct object_id *oid, int flags, void *cb_data)
543 struct branches_for_remote *branches = cb_data;
544 struct refspec_item refspec;
545 struct known_remote *kr;
547 memset(&refspec, 0, sizeof(refspec));
548 refspec.dst = (char *)refname;
549 if (remote_find_tracking(branches->remote, &refspec))
550 return 0;
552 /* don't delete a branch if another remote also uses it */
553 for (kr = branches->keep->list; kr; kr = kr->next) {
554 memset(&refspec, 0, sizeof(refspec));
555 refspec.dst = (char *)refname;
556 if (!remote_find_tracking(kr->remote, &refspec))
557 return 0;
560 /* don't delete non-remote-tracking refs */
561 if (!starts_with(refname, "refs/remotes/")) {
562 /* advise user how to delete local branches */
563 if (starts_with(refname, "refs/heads/"))
564 string_list_append(branches->skipped,
565 abbrev_branch(refname));
566 /* silently skip over other non-remote refs */
567 return 0;
570 string_list_append(branches->branches, refname);
572 return 0;
575 struct rename_info {
576 const char *old_name;
577 const char *new_name;
578 struct string_list *remote_branches;
579 uint32_t symrefs_nr;
582 static int read_remote_branches(const char *refname,
583 const struct object_id *oid, int flags, void *cb_data)
585 struct rename_info *rename = cb_data;
586 struct strbuf buf = STRBUF_INIT;
587 struct string_list_item *item;
588 int flag;
589 const char *symref;
591 strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name);
592 if (starts_with(refname, buf.buf)) {
593 item = string_list_append(rename->remote_branches, refname);
594 symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
595 NULL, &flag);
596 if (symref && (flag & REF_ISSYMREF)) {
597 item->util = xstrdup(symref);
598 rename->symrefs_nr++;
599 } else {
600 item->util = NULL;
603 strbuf_release(&buf);
605 return 0;
608 static int migrate_file(struct remote *remote)
610 struct strbuf buf = STRBUF_INIT;
611 int i;
613 strbuf_addf(&buf, "remote.%s.url", remote->name);
614 for (i = 0; i < remote->url_nr; i++)
615 git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
616 strbuf_reset(&buf);
617 strbuf_addf(&buf, "remote.%s.push", remote->name);
618 for (i = 0; i < remote->push.raw_nr; i++)
619 git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0);
620 strbuf_reset(&buf);
621 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
622 for (i = 0; i < remote->fetch.raw_nr; i++)
623 git_config_set_multivar(buf.buf, remote->fetch.raw[i], "^$", 0);
624 if (remote->origin == REMOTE_REMOTES)
625 unlink_or_warn(git_path("remotes/%s", remote->name));
626 else if (remote->origin == REMOTE_BRANCHES)
627 unlink_or_warn(git_path("branches/%s", remote->name));
628 strbuf_release(&buf);
630 return 0;
633 struct push_default_info
635 const char *old_name;
636 enum config_scope scope;
637 struct strbuf origin;
638 int linenr;
641 static int config_read_push_default(const char *key, const char *value,
642 void *cb)
644 struct push_default_info* info = cb;
645 if (strcmp(key, "remote.pushdefault") ||
646 !value || strcmp(value, info->old_name))
647 return 0;
649 info->scope = current_config_scope();
650 strbuf_reset(&info->origin);
651 strbuf_addstr(&info->origin, current_config_name());
652 info->linenr = current_config_line();
654 return 0;
657 static void handle_push_default(const char* old_name, const char* new_name)
659 struct push_default_info push_default = {
660 old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 };
661 git_config(config_read_push_default, &push_default);
662 if (push_default.scope >= CONFIG_SCOPE_COMMAND)
663 ; /* pass */
664 else if (push_default.scope >= CONFIG_SCOPE_LOCAL) {
665 int result = git_config_set_gently("remote.pushDefault",
666 new_name);
667 if (new_name && result && result != CONFIG_NOTHING_SET)
668 die(_("could not set '%s'"), "remote.pushDefault");
669 else if (!new_name && result && result != CONFIG_NOTHING_SET)
670 die(_("could not unset '%s'"), "remote.pushDefault");
671 } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) {
672 /* warn */
673 warning(_("The %s configuration remote.pushDefault in:\n"
674 "\t%s:%d\n"
675 "now names the non-existent remote '%s'"),
676 config_scope_name(push_default.scope),
677 push_default.origin.buf, push_default.linenr,
678 old_name);
683 static int mv(int argc, const char **argv)
685 int show_progress = isatty(2);
686 struct option options[] = {
687 OPT_BOOL(0, "progress", &show_progress, N_("force progress reporting")),
688 OPT_END()
690 struct remote *oldremote, *newremote;
691 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
692 old_remote_context = STRBUF_INIT;
693 struct string_list remote_branches = STRING_LIST_INIT_DUP;
694 struct rename_info rename;
695 int i, refs_renamed_nr = 0, refspec_updated = 0;
696 struct progress *progress = NULL;
698 argc = parse_options(argc, argv, NULL, options,
699 builtin_remote_rename_usage, 0);
701 if (argc != 2)
702 usage_with_options(builtin_remote_rename_usage, options);
704 rename.old_name = argv[0];
705 rename.new_name = argv[1];
706 rename.remote_branches = &remote_branches;
707 rename.symrefs_nr = 0;
709 oldremote = remote_get(rename.old_name);
710 if (!remote_is_configured(oldremote, 1)) {
711 error(_("No such remote: '%s'"), rename.old_name);
712 exit(2);
715 if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
716 return migrate_file(oldremote);
718 newremote = remote_get(rename.new_name);
719 if (remote_is_configured(newremote, 1)) {
720 error(_("remote %s already exists."), rename.new_name);
721 exit(3);
724 if (!valid_remote_name(rename.new_name))
725 die(_("'%s' is not a valid remote name"), rename.new_name);
727 strbuf_addf(&buf, "remote.%s", rename.old_name);
728 strbuf_addf(&buf2, "remote.%s", rename.new_name);
729 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
730 return error(_("Could not rename config section '%s' to '%s'"),
731 buf.buf, buf2.buf);
733 strbuf_reset(&buf);
734 strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
735 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
736 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
737 for (i = 0; i < oldremote->fetch.raw_nr; i++) {
738 char *ptr;
740 strbuf_reset(&buf2);
741 strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
742 ptr = strstr(buf2.buf, old_remote_context.buf);
743 if (ptr) {
744 refspec_updated = 1;
745 strbuf_splice(&buf2,
746 ptr-buf2.buf + strlen(":refs/remotes/"),
747 strlen(rename.old_name), rename.new_name,
748 strlen(rename.new_name));
749 } else
750 warning(_("Not updating non-default fetch refspec\n"
751 "\t%s\n"
752 "\tPlease update the configuration manually if necessary."),
753 buf2.buf);
755 git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
758 read_branches();
759 for (i = 0; i < branch_list.nr; i++) {
760 struct string_list_item *item = branch_list.items + i;
761 struct branch_info *info = item->util;
762 if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) {
763 strbuf_reset(&buf);
764 strbuf_addf(&buf, "branch.%s.remote", item->string);
765 git_config_set(buf.buf, rename.new_name);
767 if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) {
768 strbuf_reset(&buf);
769 strbuf_addf(&buf, "branch.%s.pushRemote", item->string);
770 git_config_set(buf.buf, rename.new_name);
774 if (!refspec_updated)
775 return 0;
778 * First remove symrefs, then rename the rest, finally create
779 * the new symrefs.
781 for_each_ref(read_remote_branches, &rename);
782 if (show_progress) {
784 * Count symrefs twice, since "renaming" them is done by
785 * deleting and recreating them in two separate passes.
787 progress = start_progress(_("Renaming remote references"),
788 rename.remote_branches->nr + rename.symrefs_nr);
790 for (i = 0; i < remote_branches.nr; i++) {
791 struct string_list_item *item = remote_branches.items + i;
792 struct strbuf referent = STRBUF_INIT;
794 if (refs_read_symbolic_ref(get_main_ref_store(the_repository), item->string,
795 &referent))
796 continue;
797 if (delete_ref(NULL, item->string, NULL, REF_NO_DEREF))
798 die(_("deleting '%s' failed"), item->string);
800 strbuf_release(&referent);
801 display_progress(progress, ++refs_renamed_nr);
803 for (i = 0; i < remote_branches.nr; i++) {
804 struct string_list_item *item = remote_branches.items + i;
806 if (item->util)
807 continue;
808 strbuf_reset(&buf);
809 strbuf_addstr(&buf, item->string);
810 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
811 rename.new_name, strlen(rename.new_name));
812 strbuf_reset(&buf2);
813 strbuf_addf(&buf2, "remote: renamed %s to %s",
814 item->string, buf.buf);
815 if (rename_ref(item->string, buf.buf, buf2.buf))
816 die(_("renaming '%s' failed"), item->string);
817 display_progress(progress, ++refs_renamed_nr);
819 for (i = 0; i < remote_branches.nr; i++) {
820 struct string_list_item *item = remote_branches.items + i;
822 if (!item->util)
823 continue;
824 strbuf_reset(&buf);
825 strbuf_addstr(&buf, item->string);
826 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
827 rename.new_name, strlen(rename.new_name));
828 strbuf_reset(&buf2);
829 strbuf_addstr(&buf2, item->util);
830 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name),
831 rename.new_name, strlen(rename.new_name));
832 strbuf_reset(&buf3);
833 strbuf_addf(&buf3, "remote: renamed %s to %s",
834 item->string, buf.buf);
835 if (create_symref(buf.buf, buf2.buf, buf3.buf))
836 die(_("creating '%s' failed"), buf.buf);
837 display_progress(progress, ++refs_renamed_nr);
839 stop_progress(&progress);
840 string_list_clear(&remote_branches, 1);
842 handle_push_default(rename.old_name, rename.new_name);
844 return 0;
847 static int rm(int argc, const char **argv)
849 struct option options[] = {
850 OPT_END()
852 struct remote *remote;
853 struct strbuf buf = STRBUF_INIT;
854 struct known_remotes known_remotes = { NULL, NULL };
855 struct string_list branches = STRING_LIST_INIT_DUP;
856 struct string_list skipped = STRING_LIST_INIT_DUP;
857 struct branches_for_remote cb_data;
858 int i, result;
860 memset(&cb_data, 0, sizeof(cb_data));
861 cb_data.branches = &branches;
862 cb_data.skipped = &skipped;
863 cb_data.keep = &known_remotes;
865 if (argc != 2)
866 usage_with_options(builtin_remote_rm_usage, options);
868 remote = remote_get(argv[1]);
869 if (!remote_is_configured(remote, 1)) {
870 error(_("No such remote: '%s'"), argv[1]);
871 exit(2);
874 known_remotes.to_delete = remote;
875 for_each_remote(add_known_remote, &known_remotes);
877 read_branches();
878 for (i = 0; i < branch_list.nr; i++) {
879 struct string_list_item *item = branch_list.items + i;
880 struct branch_info *info = item->util;
881 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
882 const char *keys[] = { "remote", "merge", NULL }, **k;
883 for (k = keys; *k; k++) {
884 strbuf_reset(&buf);
885 strbuf_addf(&buf, "branch.%s.%s",
886 item->string, *k);
887 result = git_config_set_gently(buf.buf, NULL);
888 if (result && result != CONFIG_NOTHING_SET)
889 die(_("could not unset '%s'"), buf.buf);
892 if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) {
893 strbuf_reset(&buf);
894 strbuf_addf(&buf, "branch.%s.pushremote", item->string);
895 result = git_config_set_gently(buf.buf, NULL);
896 if (result && result != CONFIG_NOTHING_SET)
897 die(_("could not unset '%s'"), buf.buf);
902 * We cannot just pass a function to for_each_ref() which deletes
903 * the branches one by one, since for_each_ref() relies on cached
904 * refs, which are invalidated when deleting a branch.
906 cb_data.remote = remote;
907 result = for_each_ref(add_branch_for_removal, &cb_data);
908 strbuf_release(&buf);
910 if (!result)
911 result = delete_refs("remote: remove", &branches, REF_NO_DEREF);
912 string_list_clear(&branches, 0);
914 if (skipped.nr) {
915 fprintf_ln(stderr,
916 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
917 "to delete it, use:",
918 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
919 "to delete them, use:",
920 skipped.nr));
921 for (i = 0; i < skipped.nr; i++)
922 fprintf(stderr, " git branch -d %s\n",
923 skipped.items[i].string);
925 string_list_clear(&skipped, 0);
927 if (!result) {
928 strbuf_addf(&buf, "remote.%s", remote->name);
929 if (git_config_rename_section(buf.buf, NULL) < 1)
930 return error(_("Could not remove config section '%s'"), buf.buf);
932 handle_push_default(remote->name, NULL);
935 return result;
938 static void clear_push_info(void *util, const char *string)
940 struct push_info *info = util;
941 free(info->dest);
942 free(info);
945 static void free_remote_ref_states(struct ref_states *states)
947 string_list_clear(&states->new_refs, 0);
948 string_list_clear(&states->skipped, 0);
949 string_list_clear(&states->stale, 1);
950 string_list_clear(&states->tracked, 0);
951 string_list_clear(&states->heads, 0);
952 string_list_clear_func(&states->push, clear_push_info);
955 static int append_ref_to_tracked_list(const char *refname,
956 const struct object_id *oid, int flags, void *cb_data)
958 struct ref_states *states = cb_data;
959 struct refspec_item refspec;
961 if (flags & REF_ISSYMREF)
962 return 0;
964 memset(&refspec, 0, sizeof(refspec));
965 refspec.dst = (char *)refname;
966 if (!remote_find_tracking(states->remote, &refspec))
967 string_list_append(&states->tracked, abbrev_branch(refspec.src));
969 return 0;
972 static int get_remote_ref_states(const char *name,
973 struct ref_states *states,
974 int query)
976 states->remote = remote_get(name);
977 if (!states->remote)
978 return error(_("No such remote: '%s'"), name);
980 read_branches();
982 if (query) {
983 struct transport *transport;
984 const struct ref *remote_refs;
986 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
987 states->remote->url[0] : NULL);
988 remote_refs = transport_get_remote_refs(transport, NULL);
990 states->queried = 1;
991 if (query & GET_REF_STATES)
992 get_ref_states(remote_refs, states);
993 if (query & GET_HEAD_NAMES)
994 get_head_names(remote_refs, states);
995 if (query & GET_PUSH_REF_STATES)
996 get_push_ref_states(remote_refs, states);
997 transport_disconnect(transport);
998 } else {
999 for_each_ref(append_ref_to_tracked_list, states);
1000 string_list_sort(&states->tracked);
1001 get_push_ref_states_noquery(states);
1004 return 0;
1007 struct show_info {
1008 struct string_list list;
1009 struct ref_states states;
1010 int width, width2;
1011 int any_rebase;
1014 #define SHOW_INFO_INIT { \
1015 .list = STRING_LIST_INIT_DUP, \
1016 .states = REF_STATES_INIT, \
1019 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
1021 struct show_info *info = cb_data;
1022 int n = strlen(item->string);
1023 if (n > info->width)
1024 info->width = n;
1025 string_list_insert(&info->list, item->string);
1026 return 0;
1029 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
1031 struct show_info *info = cb_data;
1032 struct ref_states *states = &info->states;
1033 const char *name = item->string;
1035 if (states->queried) {
1036 const char *fmt = "%s";
1037 const char *arg = "";
1038 if (string_list_has_string(&states->new_refs, name)) {
1039 fmt = _(" new (next fetch will store in remotes/%s)");
1040 arg = states->remote->name;
1041 } else if (string_list_has_string(&states->tracked, name))
1042 arg = _(" tracked");
1043 else if (string_list_has_string(&states->skipped, name))
1044 arg = _(" skipped");
1045 else if (string_list_has_string(&states->stale, name))
1046 arg = _(" stale (use 'git remote prune' to remove)");
1047 else
1048 arg = _(" ???");
1049 printf(" %-*s", info->width, name);
1050 printf(fmt, arg);
1051 printf("\n");
1052 } else
1053 printf(" %s\n", name);
1055 return 0;
1058 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
1060 struct show_info *show_info = cb_data;
1061 struct ref_states *states = &show_info->states;
1062 struct branch_info *branch_info = branch_item->util;
1063 struct string_list_item *item;
1064 int n;
1066 if (!branch_info->merge.nr || !branch_info->remote_name ||
1067 strcmp(states->remote->name, branch_info->remote_name))
1068 return 0;
1069 if ((n = strlen(branch_item->string)) > show_info->width)
1070 show_info->width = n;
1071 if (branch_info->rebase >= REBASE_TRUE)
1072 show_info->any_rebase = 1;
1074 item = string_list_insert(&show_info->list, branch_item->string);
1075 item->util = branch_info;
1077 return 0;
1080 static int show_local_info_item(struct string_list_item *item, void *cb_data)
1082 struct show_info *show_info = cb_data;
1083 struct branch_info *branch_info = item->util;
1084 struct string_list *merge = &branch_info->merge;
1085 int width = show_info->width + 4;
1086 int i;
1088 if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) {
1089 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1090 item->string);
1091 return 0;
1094 printf(" %-*s ", show_info->width, item->string);
1095 if (branch_info->rebase >= REBASE_TRUE) {
1096 const char *msg;
1097 if (branch_info->rebase == REBASE_INTERACTIVE)
1098 msg = _("rebases interactively onto remote %s");
1099 else if (branch_info->rebase == REBASE_MERGES)
1100 msg = _("rebases interactively (with merges) onto "
1101 "remote %s");
1102 else
1103 msg = _("rebases onto remote %s");
1104 printf_ln(msg, merge->items[0].string);
1105 return 0;
1106 } else if (show_info->any_rebase) {
1107 printf_ln(_(" merges with remote %s"), merge->items[0].string);
1108 width++;
1109 } else {
1110 printf_ln(_("merges with remote %s"), merge->items[0].string);
1112 for (i = 1; i < merge->nr; i++)
1113 printf(_("%-*s and with remote %s\n"), width, "",
1114 merge->items[i].string);
1116 return 0;
1119 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
1121 struct show_info *show_info = cb_data;
1122 struct push_info *push_info = push_item->util;
1123 struct string_list_item *item;
1124 int n;
1125 if ((n = strlen(push_item->string)) > show_info->width)
1126 show_info->width = n;
1127 if ((n = strlen(push_info->dest)) > show_info->width2)
1128 show_info->width2 = n;
1129 item = string_list_append(&show_info->list, push_item->string);
1130 item->util = push_item->util;
1131 return 0;
1135 * Sorting comparison for a string list that has push_info
1136 * structs in its util field
1138 static int cmp_string_with_push(const void *va, const void *vb)
1140 const struct string_list_item *a = va;
1141 const struct string_list_item *b = vb;
1142 const struct push_info *a_push = a->util;
1143 const struct push_info *b_push = b->util;
1144 int cmp = strcmp(a->string, b->string);
1145 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1148 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1150 struct show_info *show_info = cb_data;
1151 struct push_info *push_info = item->util;
1152 const char *src = item->string, *status = NULL;
1154 switch (push_info->status) {
1155 case PUSH_STATUS_CREATE:
1156 status = _("create");
1157 break;
1158 case PUSH_STATUS_DELETE:
1159 status = _("delete");
1160 src = _("(none)");
1161 break;
1162 case PUSH_STATUS_UPTODATE:
1163 status = _("up to date");
1164 break;
1165 case PUSH_STATUS_FASTFORWARD:
1166 status = _("fast-forwardable");
1167 break;
1168 case PUSH_STATUS_OUTOFDATE:
1169 status = _("local out of date");
1170 break;
1171 case PUSH_STATUS_NOTQUERIED:
1172 break;
1174 if (status) {
1175 if (push_info->forced)
1176 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1177 show_info->width2, push_info->dest, status);
1178 else
1179 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1180 show_info->width2, push_info->dest, status);
1181 } else {
1182 if (push_info->forced)
1183 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1184 push_info->dest);
1185 else
1186 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1187 push_info->dest);
1189 return 0;
1192 static int get_one_entry(struct remote *remote, void *priv)
1194 struct string_list *list = priv;
1195 struct strbuf remote_info_buf = STRBUF_INIT;
1196 const char **url;
1197 int i, url_nr;
1199 if (remote->url_nr > 0) {
1200 struct strbuf promisor_config = STRBUF_INIT;
1201 const char *partial_clone_filter = NULL;
1203 strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
1204 strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url[0]);
1205 if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
1206 strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
1208 strbuf_release(&promisor_config);
1209 string_list_append(list, remote->name)->util =
1210 strbuf_detach(&remote_info_buf, NULL);
1211 } else
1212 string_list_append(list, remote->name)->util = NULL;
1213 if (remote->pushurl_nr) {
1214 url = remote->pushurl;
1215 url_nr = remote->pushurl_nr;
1216 } else {
1217 url = remote->url;
1218 url_nr = remote->url_nr;
1220 for (i = 0; i < url_nr; i++)
1222 strbuf_addf(&remote_info_buf, "%s (push)", url[i]);
1223 string_list_append(list, remote->name)->util =
1224 strbuf_detach(&remote_info_buf, NULL);
1227 return 0;
1230 static int show_all(void)
1232 struct string_list list = STRING_LIST_INIT_NODUP;
1233 int result;
1235 list.strdup_strings = 1;
1236 result = for_each_remote(get_one_entry, &list);
1238 if (!result) {
1239 int i;
1241 string_list_sort(&list);
1242 for (i = 0; i < list.nr; i++) {
1243 struct string_list_item *item = list.items + i;
1244 if (verbose)
1245 printf("%s\t%s\n", item->string,
1246 item->util ? (const char *)item->util : "");
1247 else {
1248 if (i && !strcmp((item - 1)->string, item->string))
1249 continue;
1250 printf("%s\n", item->string);
1254 string_list_clear(&list, 1);
1255 return result;
1258 static int show(int argc, const char **argv)
1260 int no_query = 0, result = 0, query_flag = 0;
1261 struct option options[] = {
1262 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1263 OPT_END()
1265 struct show_info info = SHOW_INFO_INIT;
1267 argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1270 if (argc < 1)
1271 return show_all();
1273 if (!no_query)
1274 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1276 for (; argc; argc--, argv++) {
1277 int i;
1278 const char **url;
1279 int url_nr;
1281 get_remote_ref_states(*argv, &info.states, query_flag);
1283 printf_ln(_("* remote %s"), *argv);
1284 printf_ln(_(" Fetch URL: %s"), info.states.remote->url_nr > 0 ?
1285 info.states.remote->url[0] : _("(no URL)"));
1286 if (info.states.remote->pushurl_nr) {
1287 url = info.states.remote->pushurl;
1288 url_nr = info.states.remote->pushurl_nr;
1289 } else {
1290 url = info.states.remote->url;
1291 url_nr = info.states.remote->url_nr;
1293 for (i = 0; i < url_nr; i++)
1295 * TRANSLATORS: the colon ':' should align
1296 * with the one in " Fetch URL: %s"
1297 * translation.
1299 printf_ln(_(" Push URL: %s"), url[i]);
1300 if (!i)
1301 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1302 if (no_query)
1303 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1304 else if (!info.states.heads.nr)
1305 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1306 else if (info.states.heads.nr == 1)
1307 printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string);
1308 else {
1309 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1310 " may be one of the following):\n"));
1311 for (i = 0; i < info.states.heads.nr; i++)
1312 printf(" %s\n", info.states.heads.items[i].string);
1315 /* remote branch info */
1316 info.width = 0;
1317 for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info);
1318 for_each_string_list(&info.states.skipped, add_remote_to_show_info, &info);
1319 for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info);
1320 for_each_string_list(&info.states.stale, add_remote_to_show_info, &info);
1321 if (info.list.nr)
1322 printf_ln(Q_(" Remote branch:%s",
1323 " Remote branches:%s",
1324 info.list.nr),
1325 no_query ? _(" (status not queried)") : "");
1326 for_each_string_list(&info.list, show_remote_info_item, &info);
1327 string_list_clear(&info.list, 0);
1329 /* git pull info */
1330 info.width = 0;
1331 info.any_rebase = 0;
1332 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1333 if (info.list.nr)
1334 printf_ln(Q_(" Local branch configured for 'git pull':",
1335 " Local branches configured for 'git pull':",
1336 info.list.nr));
1337 for_each_string_list(&info.list, show_local_info_item, &info);
1338 string_list_clear(&info.list, 0);
1340 /* git push info */
1341 if (info.states.remote->mirror)
1342 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1344 info.width = info.width2 = 0;
1345 for_each_string_list(&info.states.push, add_push_to_show_info, &info);
1346 QSORT(info.list.items, info.list.nr, cmp_string_with_push);
1347 if (info.list.nr)
1348 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1349 " Local refs configured for 'git push'%s:",
1350 info.list.nr),
1351 no_query ? _(" (status not queried)") : "");
1352 for_each_string_list(&info.list, show_push_info_item, &info);
1353 string_list_clear(&info.list, 0);
1355 free_remote_ref_states(&info.states);
1358 return result;
1361 static int set_head(int argc, const char **argv)
1363 int i, opt_a = 0, opt_d = 0, result = 0;
1364 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1365 char *head_name = NULL;
1367 struct option options[] = {
1368 OPT_BOOL('a', "auto", &opt_a,
1369 N_("set refs/remotes/<name>/HEAD according to remote")),
1370 OPT_BOOL('d', "delete", &opt_d,
1371 N_("delete refs/remotes/<name>/HEAD")),
1372 OPT_END()
1374 argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1376 if (argc)
1377 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1379 if (!opt_a && !opt_d && argc == 2) {
1380 head_name = xstrdup(argv[1]);
1381 } else if (opt_a && !opt_d && argc == 1) {
1382 struct ref_states states = REF_STATES_INIT;
1383 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1384 if (!states.heads.nr)
1385 result |= error(_("Cannot determine remote HEAD"));
1386 else if (states.heads.nr > 1) {
1387 result |= error(_("Multiple remote HEAD branches. "
1388 "Please choose one explicitly with:"));
1389 for (i = 0; i < states.heads.nr; i++)
1390 fprintf(stderr, " git remote set-head %s %s\n",
1391 argv[0], states.heads.items[i].string);
1392 } else
1393 head_name = xstrdup(states.heads.items[0].string);
1394 free_remote_ref_states(&states);
1395 } else if (opt_d && !opt_a && argc == 1) {
1396 if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF))
1397 result |= error(_("Could not delete %s"), buf.buf);
1398 } else
1399 usage_with_options(builtin_remote_sethead_usage, options);
1401 if (head_name) {
1402 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1403 /* make sure it's valid */
1404 if (!ref_exists(buf2.buf))
1405 result |= error(_("Not a valid ref: %s"), buf2.buf);
1406 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1407 result |= error(_("Could not setup %s"), buf.buf);
1408 else if (opt_a)
1409 printf("%s/HEAD set to %s\n", argv[0], head_name);
1410 free(head_name);
1413 strbuf_release(&buf);
1414 strbuf_release(&buf2);
1415 return result;
1418 static int prune_remote(const char *remote, int dry_run)
1420 int result = 0;
1421 struct ref_states states = REF_STATES_INIT;
1422 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1423 struct string_list_item *item;
1424 const char *dangling_msg = dry_run
1425 ? _(" %s will become dangling!")
1426 : _(" %s has become dangling!");
1428 get_remote_ref_states(remote, &states, GET_REF_STATES);
1430 if (!states.stale.nr) {
1431 free_remote_ref_states(&states);
1432 return 0;
1435 printf_ln(_("Pruning %s"), remote);
1436 printf_ln(_("URL: %s"),
1437 states.remote->url_nr
1438 ? states.remote->url[0]
1439 : _("(no URL)"));
1441 for_each_string_list_item(item, &states.stale)
1442 string_list_append(&refs_to_prune, item->util);
1443 string_list_sort(&refs_to_prune);
1445 if (!dry_run)
1446 result |= delete_refs("remote: prune", &refs_to_prune, 0);
1448 for_each_string_list_item(item, &states.stale) {
1449 const char *refname = item->util;
1451 if (dry_run)
1452 printf_ln(_(" * [would prune] %s"),
1453 abbrev_ref(refname, "refs/remotes/"));
1454 else
1455 printf_ln(_(" * [pruned] %s"),
1456 abbrev_ref(refname, "refs/remotes/"));
1459 warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
1461 string_list_clear(&refs_to_prune, 0);
1462 free_remote_ref_states(&states);
1463 return result;
1466 static int prune(int argc, const char **argv)
1468 int dry_run = 0, result = 0;
1469 struct option options[] = {
1470 OPT__DRY_RUN(&dry_run, N_("dry run")),
1471 OPT_END()
1474 argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1477 if (argc < 1)
1478 usage_with_options(builtin_remote_prune_usage, options);
1480 for (; argc; argc--, argv++)
1481 result |= prune_remote(*argv, dry_run);
1483 return result;
1486 static int get_remote_default(const char *key, const char *value, void *priv)
1488 if (strcmp(key, "remotes.default") == 0) {
1489 int *found = priv;
1490 *found = 1;
1492 return 0;
1495 static int update(int argc, const char **argv)
1497 int i, prune = -1;
1498 struct option options[] = {
1499 OPT_BOOL('p', "prune", &prune,
1500 N_("prune remotes after fetching")),
1501 OPT_END()
1503 struct strvec fetch_argv = STRVEC_INIT;
1504 int default_defined = 0;
1505 int retval;
1507 argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1508 PARSE_OPT_KEEP_ARGV0);
1510 strvec_push(&fetch_argv, "fetch");
1512 if (prune != -1)
1513 strvec_push(&fetch_argv, prune ? "--prune" : "--no-prune");
1514 if (verbose)
1515 strvec_push(&fetch_argv, "-v");
1516 strvec_push(&fetch_argv, "--multiple");
1517 if (argc < 2)
1518 strvec_push(&fetch_argv, "default");
1519 for (i = 1; i < argc; i++)
1520 strvec_push(&fetch_argv, argv[i]);
1522 if (strcmp(fetch_argv.v[fetch_argv.nr-1], "default") == 0) {
1523 git_config(get_remote_default, &default_defined);
1524 if (!default_defined) {
1525 strvec_pop(&fetch_argv);
1526 strvec_push(&fetch_argv, "--all");
1530 retval = run_command_v_opt(fetch_argv.v, RUN_GIT_CMD);
1531 strvec_clear(&fetch_argv);
1532 return retval;
1535 static int remove_all_fetch_refspecs(const char *key)
1537 return git_config_set_multivar_gently(key, NULL, NULL,
1538 CONFIG_FLAGS_MULTI_REPLACE);
1541 static void add_branches(struct remote *remote, const char **branches,
1542 const char *key)
1544 const char *remotename = remote->name;
1545 int mirror = remote->mirror;
1546 struct strbuf refspec = STRBUF_INIT;
1548 for (; *branches; branches++)
1549 add_branch(key, *branches, remotename, mirror, &refspec);
1551 strbuf_release(&refspec);
1554 static int set_remote_branches(const char *remotename, const char **branches,
1555 int add_mode)
1557 struct strbuf key = STRBUF_INIT;
1558 struct remote *remote;
1560 strbuf_addf(&key, "remote.%s.fetch", remotename);
1562 remote = remote_get(remotename);
1563 if (!remote_is_configured(remote, 1)) {
1564 error(_("No such remote '%s'"), remotename);
1565 exit(2);
1568 if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
1569 strbuf_release(&key);
1570 return 1;
1572 add_branches(remote, branches, key.buf);
1574 strbuf_release(&key);
1575 return 0;
1578 static int set_branches(int argc, const char **argv)
1580 int add_mode = 0;
1581 struct option options[] = {
1582 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1583 OPT_END()
1586 argc = parse_options(argc, argv, NULL, options,
1587 builtin_remote_setbranches_usage, 0);
1588 if (argc == 0) {
1589 error(_("no remote specified"));
1590 usage_with_options(builtin_remote_setbranches_usage, options);
1592 argv[argc] = NULL;
1594 return set_remote_branches(argv[0], argv + 1, add_mode);
1597 static int get_url(int argc, const char **argv)
1599 int i, push_mode = 0, all_mode = 0;
1600 const char *remotename = NULL;
1601 struct remote *remote;
1602 const char **url;
1603 int url_nr;
1604 struct option options[] = {
1605 OPT_BOOL('\0', "push", &push_mode,
1606 N_("query push URLs rather than fetch URLs")),
1607 OPT_BOOL('\0', "all", &all_mode,
1608 N_("return all URLs")),
1609 OPT_END()
1611 argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0);
1613 if (argc != 1)
1614 usage_with_options(builtin_remote_geturl_usage, options);
1616 remotename = argv[0];
1618 remote = remote_get(remotename);
1619 if (!remote_is_configured(remote, 1)) {
1620 error(_("No such remote '%s'"), remotename);
1621 exit(2);
1624 url_nr = 0;
1625 if (push_mode) {
1626 url = remote->pushurl;
1627 url_nr = remote->pushurl_nr;
1629 /* else fetch mode */
1631 /* Use the fetch URL when no push URLs were found or requested. */
1632 if (!url_nr) {
1633 url = remote->url;
1634 url_nr = remote->url_nr;
1637 if (!url_nr)
1638 die(_("no URLs configured for remote '%s'"), remotename);
1640 if (all_mode) {
1641 for (i = 0; i < url_nr; i++)
1642 printf_ln("%s", url[i]);
1643 } else {
1644 printf_ln("%s", *url);
1647 return 0;
1650 static int set_url(int argc, const char **argv)
1652 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1653 int matches = 0, negative_matches = 0;
1654 const char *remotename = NULL;
1655 const char *newurl = NULL;
1656 const char *oldurl = NULL;
1657 struct remote *remote;
1658 regex_t old_regex;
1659 const char **urlset;
1660 int urlset_nr;
1661 struct strbuf name_buf = STRBUF_INIT;
1662 struct option options[] = {
1663 OPT_BOOL('\0', "push", &push_mode,
1664 N_("manipulate push URLs")),
1665 OPT_BOOL('\0', "add", &add_mode,
1666 N_("add URL")),
1667 OPT_BOOL('\0', "delete", &delete_mode,
1668 N_("delete URLs")),
1669 OPT_END()
1671 argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage,
1672 PARSE_OPT_KEEP_ARGV0);
1674 if (add_mode && delete_mode)
1675 die(_("--add --delete doesn't make sense"));
1677 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1678 usage_with_options(builtin_remote_seturl_usage, options);
1680 remotename = argv[1];
1681 newurl = argv[2];
1682 if (argc > 3)
1683 oldurl = argv[3];
1685 if (delete_mode)
1686 oldurl = newurl;
1688 remote = remote_get(remotename);
1689 if (!remote_is_configured(remote, 1)) {
1690 error(_("No such remote '%s'"), remotename);
1691 exit(2);
1694 if (push_mode) {
1695 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1696 urlset = remote->pushurl;
1697 urlset_nr = remote->pushurl_nr;
1698 } else {
1699 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1700 urlset = remote->url;
1701 urlset_nr = remote->url_nr;
1704 /* Special cases that add new entry. */
1705 if ((!oldurl && !delete_mode) || add_mode) {
1706 if (add_mode)
1707 git_config_set_multivar(name_buf.buf, newurl,
1708 "^$", 0);
1709 else
1710 git_config_set(name_buf.buf, newurl);
1711 goto out;
1714 /* Old URL specified. Demand that one matches. */
1715 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1716 die(_("Invalid old URL pattern: %s"), oldurl);
1718 for (i = 0; i < urlset_nr; i++)
1719 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1720 matches++;
1721 else
1722 negative_matches++;
1723 if (!delete_mode && !matches)
1724 die(_("No such URL found: %s"), oldurl);
1725 if (delete_mode && !negative_matches && !push_mode)
1726 die(_("Will not delete all non-push URLs"));
1728 regfree(&old_regex);
1730 if (!delete_mode)
1731 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1732 else
1733 git_config_set_multivar(name_buf.buf, NULL, oldurl,
1734 CONFIG_FLAGS_MULTI_REPLACE);
1735 out:
1736 strbuf_release(&name_buf);
1737 return 0;
1740 int cmd_remote(int argc, const char **argv, const char *prefix)
1742 struct option options[] = {
1743 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1744 OPT_END()
1746 int result;
1748 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1749 PARSE_OPT_STOP_AT_NON_OPTION);
1751 if (argc < 1)
1752 result = show_all();
1753 else if (!strcmp(argv[0], "add"))
1754 result = add(argc, argv);
1755 else if (!strcmp(argv[0], "rename"))
1756 result = mv(argc, argv);
1757 else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove"))
1758 result = rm(argc, argv);
1759 else if (!strcmp(argv[0], "set-head"))
1760 result = set_head(argc, argv);
1761 else if (!strcmp(argv[0], "set-branches"))
1762 result = set_branches(argc, argv);
1763 else if (!strcmp(argv[0], "get-url"))
1764 result = get_url(argc, argv);
1765 else if (!strcmp(argv[0], "set-url"))
1766 result = set_url(argc, argv);
1767 else if (!strcmp(argv[0], "show"))
1768 result = show(argc, argv);
1769 else if (!strcmp(argv[0], "prune"))
1770 result = prune(argc, argv);
1771 else if (!strcmp(argv[0], "update"))
1772 result = update(argc, argv);
1773 else {
1774 error(_("Unknown subcommand: %s"), argv[0]);
1775 usage_with_options(builtin_remote_usage, options);
1778 return result ? 1 : 0;