l10n: bg.po: Updated Bulgarian translation (5211t)
[git/debian.git] / builtin / remote.c
blob299c466116debac6b8c69658c5f499454496fdbd
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"
16 static const char * const builtin_remote_usage[] = {
17 N_("git remote [-v | --verbose]"),
18 N_("git remote add [-t <branch>] [-m <master>] [-f] [--tags | --no-tags] [--mirror=<fetch|push>] <name> <url>"),
19 N_("git remote rename <old> <new>"),
20 N_("git remote remove <name>"),
21 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
22 N_("git remote [-v | --verbose] show [-n] <name>"),
23 N_("git remote prune [-n | --dry-run] <name>"),
24 N_("git remote [-v | --verbose] update [-p | --prune] [(<group> | <remote>)...]"),
25 N_("git remote set-branches [--add] <name> <branch>..."),
26 N_("git remote get-url [--push] [--all] <name>"),
27 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
28 N_("git remote set-url --add <name> <newurl>"),
29 N_("git remote set-url --delete <name> <url>"),
30 NULL
33 static const char * const builtin_remote_add_usage[] = {
34 N_("git remote add [<options>] <name> <url>"),
35 NULL
38 static const char * const builtin_remote_rename_usage[] = {
39 N_("git remote rename <old> <new>"),
40 NULL
43 static const char * const builtin_remote_rm_usage[] = {
44 N_("git remote remove <name>"),
45 NULL
48 static const char * const builtin_remote_sethead_usage[] = {
49 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
50 NULL
53 static const char * const builtin_remote_setbranches_usage[] = {
54 N_("git remote set-branches <name> <branch>..."),
55 N_("git remote set-branches --add <name> <branch>..."),
56 NULL
59 static const char * const builtin_remote_show_usage[] = {
60 N_("git remote show [<options>] <name>"),
61 NULL
64 static const char * const builtin_remote_prune_usage[] = {
65 N_("git remote prune [<options>] <name>"),
66 NULL
69 static const char * const builtin_remote_update_usage[] = {
70 N_("git remote update [<options>] [<group> | <remote>]..."),
71 NULL
74 static const char * const builtin_remote_geturl_usage[] = {
75 N_("git remote get-url [--push] [--all] <name>"),
76 NULL
79 static const char * const builtin_remote_seturl_usage[] = {
80 N_("git remote set-url [--push] <name> <newurl> [<oldurl>]"),
81 N_("git remote set-url --add <name> <newurl>"),
82 N_("git remote set-url --delete <name> <url>"),
83 NULL
86 #define GET_REF_STATES (1<<0)
87 #define GET_HEAD_NAMES (1<<1)
88 #define GET_PUSH_REF_STATES (1<<2)
90 static int verbose;
92 static int fetch_remote(const char *name)
94 const char *argv[] = { "fetch", name, NULL, NULL };
95 if (verbose) {
96 argv[1] = "-v";
97 argv[2] = name;
99 printf_ln(_("Updating %s"), name);
100 if (run_command_v_opt(argv, RUN_GIT_CMD))
101 return error(_("Could not fetch %s"), name);
102 return 0;
105 enum {
106 TAGS_UNSET = 0,
107 TAGS_DEFAULT = 1,
108 TAGS_SET = 2
111 #define MIRROR_NONE 0
112 #define MIRROR_FETCH 1
113 #define MIRROR_PUSH 2
114 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
116 static void add_branch(const char *key, const char *branchname,
117 const char *remotename, int mirror, struct strbuf *tmp)
119 strbuf_reset(tmp);
120 strbuf_addch(tmp, '+');
121 if (mirror)
122 strbuf_addf(tmp, "refs/%s:refs/%s",
123 branchname, branchname);
124 else
125 strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
126 branchname, remotename, branchname);
127 git_config_set_multivar(key, tmp->buf, "^$", 0);
130 static const char mirror_advice[] =
131 N_("--mirror is dangerous and deprecated; please\n"
132 "\t use --mirror=fetch or --mirror=push instead");
134 static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
136 unsigned *mirror = opt->value;
137 if (not)
138 *mirror = MIRROR_NONE;
139 else if (!arg) {
140 warning("%s", _(mirror_advice));
141 *mirror = MIRROR_BOTH;
143 else if (!strcmp(arg, "fetch"))
144 *mirror = MIRROR_FETCH;
145 else if (!strcmp(arg, "push"))
146 *mirror = MIRROR_PUSH;
147 else
148 return error(_("unknown mirror argument: %s"), arg);
149 return 0;
152 static int add(int argc, const char **argv)
154 int fetch = 0, fetch_tags = TAGS_DEFAULT;
155 unsigned mirror = MIRROR_NONE;
156 struct string_list track = STRING_LIST_INIT_NODUP;
157 const char *master = NULL;
158 struct remote *remote;
159 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
160 const char *name, *url;
161 int i;
163 struct option options[] = {
164 OPT_BOOL('f', "fetch", &fetch, N_("fetch the remote branches")),
165 OPT_SET_INT(0, "tags", &fetch_tags,
166 N_("import all tags and associated objects when fetching"),
167 TAGS_SET),
168 OPT_SET_INT(0, NULL, &fetch_tags,
169 N_("or do not fetch any tag at all (--no-tags)"), TAGS_UNSET),
170 OPT_STRING_LIST('t', "track", &track, N_("branch"),
171 N_("branch(es) to track")),
172 OPT_STRING('m', "master", &master, N_("branch"), N_("master branch")),
173 OPT_CALLBACK_F(0, "mirror", &mirror, "(push|fetch)",
174 N_("set up remote as a mirror to push to or fetch from"),
175 PARSE_OPT_OPTARG | PARSE_OPT_COMP_ARG, parse_mirror_opt),
176 OPT_END()
179 argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage,
182 if (argc != 2)
183 usage_with_options(builtin_remote_add_usage, options);
185 if (mirror && master)
186 die(_("specifying a master branch makes no sense with --mirror"));
187 if (mirror && !(mirror & MIRROR_FETCH) && track.nr)
188 die(_("specifying branches to track makes sense only with fetch mirrors"));
190 name = argv[0];
191 url = argv[1];
193 remote = remote_get(name);
194 if (remote_is_configured(remote, 1)) {
195 error(_("remote %s already exists."), name);
196 exit(3);
199 if (!valid_remote_name(name))
200 die(_("'%s' is not a valid remote name"), name);
202 strbuf_addf(&buf, "remote.%s.url", name);
203 git_config_set(buf.buf, url);
205 if (!mirror || mirror & MIRROR_FETCH) {
206 strbuf_reset(&buf);
207 strbuf_addf(&buf, "remote.%s.fetch", name);
208 if (track.nr == 0)
209 string_list_append(&track, "*");
210 for (i = 0; i < track.nr; i++) {
211 add_branch(buf.buf, track.items[i].string,
212 name, mirror, &buf2);
216 if (mirror & MIRROR_PUSH) {
217 strbuf_reset(&buf);
218 strbuf_addf(&buf, "remote.%s.mirror", name);
219 git_config_set(buf.buf, "true");
222 if (fetch_tags != TAGS_DEFAULT) {
223 strbuf_reset(&buf);
224 strbuf_addf(&buf, "remote.%s.tagOpt", name);
225 git_config_set(buf.buf,
226 fetch_tags == TAGS_SET ? "--tags" : "--no-tags");
229 if (fetch && fetch_remote(name))
230 return 1;
232 if (master) {
233 strbuf_reset(&buf);
234 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
236 strbuf_reset(&buf2);
237 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
239 if (create_symref(buf.buf, buf2.buf, "remote add"))
240 return error(_("Could not setup master '%s'"), master);
243 strbuf_release(&buf);
244 strbuf_release(&buf2);
245 string_list_clear(&track, 0);
247 return 0;
250 struct branch_info {
251 char *remote_name;
252 struct string_list merge;
253 enum rebase_type rebase;
254 char *push_remote_name;
257 static struct string_list branch_list = STRING_LIST_INIT_NODUP;
259 static const char *abbrev_ref(const char *name, const char *prefix)
261 skip_prefix(name, prefix, &name);
262 return name;
264 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
266 static int config_read_branches(const char *key, const char *value, void *cb)
268 const char *orig_key = key;
269 char *name;
270 struct string_list_item *item;
271 struct branch_info *info;
272 enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type;
273 size_t key_len;
275 if (!starts_with(key, "branch."))
276 return 0;
278 key += strlen("branch.");
279 if (strip_suffix(key, ".remote", &key_len))
280 type = REMOTE;
281 else if (strip_suffix(key, ".merge", &key_len))
282 type = MERGE;
283 else if (strip_suffix(key, ".rebase", &key_len))
284 type = REBASE;
285 else if (strip_suffix(key, ".pushremote", &key_len))
286 type = PUSH_REMOTE;
287 else
288 return 0;
289 name = xmemdupz(key, key_len);
291 item = string_list_insert(&branch_list, name);
293 if (!item->util)
294 item->util = xcalloc(1, sizeof(struct branch_info));
295 info = item->util;
296 switch (type) {
297 case REMOTE:
298 if (info->remote_name)
299 warning(_("more than one %s"), orig_key);
300 info->remote_name = xstrdup(value);
301 break;
302 case MERGE: {
303 char *space = strchr(value, ' ');
304 value = abbrev_branch(value);
305 while (space) {
306 char *merge;
307 merge = xstrndup(value, space - value);
308 string_list_append(&info->merge, merge);
309 value = abbrev_branch(space + 1);
310 space = strchr(value, ' ');
312 string_list_append(&info->merge, xstrdup(value));
313 break;
315 case REBASE:
317 * Consider invalid values as false and check the
318 * truth value with >= REBASE_TRUE.
320 info->rebase = rebase_parse_value(value);
321 if (info->rebase == REBASE_INVALID)
322 warning(_("unhandled branch.%s.rebase=%s; assuming "
323 "'true'"), name, value);
324 break;
325 case PUSH_REMOTE:
326 if (info->push_remote_name)
327 warning(_("more than one %s"), orig_key);
328 info->push_remote_name = xstrdup(value);
329 break;
330 default:
331 BUG("unexpected type=%d", type);
334 return 0;
337 static void read_branches(void)
339 if (branch_list.nr)
340 return;
341 git_config(config_read_branches, NULL);
344 struct ref_states {
345 struct remote *remote;
346 struct string_list new_refs, stale, tracked, heads, push;
347 int queried;
350 #define REF_STATES_INIT { \
351 .new_refs = STRING_LIST_INIT_DUP, \
352 .stale = STRING_LIST_INIT_DUP, \
353 .tracked = STRING_LIST_INIT_DUP, \
354 .heads = STRING_LIST_INIT_DUP, \
355 .push = STRING_LIST_INIT_DUP, \
358 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
360 struct ref *fetch_map = NULL, **tail = &fetch_map;
361 struct ref *ref, *stale_refs;
362 int i;
364 for (i = 0; i < states->remote->fetch.nr; i++)
365 if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1))
366 die(_("Could not get fetch map for refspec %s"),
367 states->remote->fetch.raw[i]);
369 for (ref = fetch_map; ref; ref = ref->next) {
370 if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
371 string_list_append(&states->new_refs, abbrev_branch(ref->name));
372 else
373 string_list_append(&states->tracked, abbrev_branch(ref->name));
375 stale_refs = get_stale_heads(&states->remote->fetch, fetch_map);
376 for (ref = stale_refs; ref; ref = ref->next) {
377 struct string_list_item *item =
378 string_list_append(&states->stale, abbrev_branch(ref->name));
379 item->util = xstrdup(ref->name);
381 free_refs(stale_refs);
382 free_refs(fetch_map);
384 string_list_sort(&states->new_refs);
385 string_list_sort(&states->tracked);
386 string_list_sort(&states->stale);
388 return 0;
391 struct push_info {
392 char *dest;
393 int forced;
394 enum {
395 PUSH_STATUS_CREATE = 0,
396 PUSH_STATUS_DELETE,
397 PUSH_STATUS_UPTODATE,
398 PUSH_STATUS_FASTFORWARD,
399 PUSH_STATUS_OUTOFDATE,
400 PUSH_STATUS_NOTQUERIED
401 } status;
404 static int get_push_ref_states(const struct ref *remote_refs,
405 struct ref_states *states)
407 struct remote *remote = states->remote;
408 struct ref *ref, *local_refs, *push_map;
409 if (remote->mirror)
410 return 0;
412 local_refs = get_local_heads();
413 push_map = copy_ref_list(remote_refs);
415 match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE);
417 for (ref = push_map; ref; ref = ref->next) {
418 struct string_list_item *item;
419 struct push_info *info;
421 if (!ref->peer_ref)
422 continue;
423 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
425 item = string_list_append(&states->push,
426 abbrev_branch(ref->peer_ref->name));
427 item->util = xcalloc(1, sizeof(struct push_info));
428 info = item->util;
429 info->forced = ref->force;
430 info->dest = xstrdup(abbrev_branch(ref->name));
432 if (is_null_oid(&ref->new_oid)) {
433 info->status = PUSH_STATUS_DELETE;
434 } else if (oideq(&ref->old_oid, &ref->new_oid))
435 info->status = PUSH_STATUS_UPTODATE;
436 else if (is_null_oid(&ref->old_oid))
437 info->status = PUSH_STATUS_CREATE;
438 else if (has_object_file(&ref->old_oid) &&
439 ref_newer(&ref->new_oid, &ref->old_oid))
440 info->status = PUSH_STATUS_FASTFORWARD;
441 else
442 info->status = PUSH_STATUS_OUTOFDATE;
444 free_refs(local_refs);
445 free_refs(push_map);
446 return 0;
449 static int get_push_ref_states_noquery(struct ref_states *states)
451 int i;
452 struct remote *remote = states->remote;
453 struct string_list_item *item;
454 struct push_info *info;
456 if (remote->mirror)
457 return 0;
459 if (!remote->push.nr) {
460 item = string_list_append(&states->push, _("(matching)"));
461 info = item->util = xcalloc(1, sizeof(struct push_info));
462 info->status = PUSH_STATUS_NOTQUERIED;
463 info->dest = xstrdup(item->string);
465 for (i = 0; i < remote->push.nr; i++) {
466 const struct refspec_item *spec = &remote->push.items[i];
467 if (spec->matching)
468 item = string_list_append(&states->push, _("(matching)"));
469 else if (strlen(spec->src))
470 item = string_list_append(&states->push, spec->src);
471 else
472 item = string_list_append(&states->push, _("(delete)"));
474 info = item->util = xcalloc(1, sizeof(struct push_info));
475 info->forced = spec->force;
476 info->status = PUSH_STATUS_NOTQUERIED;
477 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
479 return 0;
482 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
484 struct ref *ref, *matches;
485 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
486 struct refspec_item refspec;
488 memset(&refspec, 0, sizeof(refspec));
489 refspec.force = 0;
490 refspec.pattern = 1;
491 refspec.src = refspec.dst = "refs/heads/*";
492 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
493 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
494 fetch_map, 1);
495 for (ref = matches; ref; ref = ref->next)
496 string_list_append(&states->heads, abbrev_branch(ref->name));
498 free_refs(fetch_map);
499 free_refs(matches);
501 return 0;
504 struct known_remote {
505 struct known_remote *next;
506 struct remote *remote;
509 struct known_remotes {
510 struct remote *to_delete;
511 struct known_remote *list;
514 static int add_known_remote(struct remote *remote, void *cb_data)
516 struct known_remotes *all = cb_data;
517 struct known_remote *r;
519 if (!strcmp(all->to_delete->name, remote->name))
520 return 0;
522 r = xmalloc(sizeof(*r));
523 r->remote = remote;
524 r->next = all->list;
525 all->list = r;
526 return 0;
529 struct branches_for_remote {
530 struct remote *remote;
531 struct string_list *branches, *skipped;
532 struct known_remotes *keep;
535 static int add_branch_for_removal(const char *refname,
536 const struct object_id *oid, int flags, void *cb_data)
538 struct branches_for_remote *branches = cb_data;
539 struct refspec_item refspec;
540 struct known_remote *kr;
542 memset(&refspec, 0, sizeof(refspec));
543 refspec.dst = (char *)refname;
544 if (remote_find_tracking(branches->remote, &refspec))
545 return 0;
547 /* don't delete a branch if another remote also uses it */
548 for (kr = branches->keep->list; kr; kr = kr->next) {
549 memset(&refspec, 0, sizeof(refspec));
550 refspec.dst = (char *)refname;
551 if (!remote_find_tracking(kr->remote, &refspec))
552 return 0;
555 /* don't delete non-remote-tracking refs */
556 if (!starts_with(refname, "refs/remotes/")) {
557 /* advise user how to delete local branches */
558 if (starts_with(refname, "refs/heads/"))
559 string_list_append(branches->skipped,
560 abbrev_branch(refname));
561 /* silently skip over other non-remote refs */
562 return 0;
565 string_list_append(branches->branches, refname);
567 return 0;
570 struct rename_info {
571 const char *old_name;
572 const char *new_name;
573 struct string_list *remote_branches;
576 static int read_remote_branches(const char *refname,
577 const struct object_id *oid, int flags, void *cb_data)
579 struct rename_info *rename = cb_data;
580 struct strbuf buf = STRBUF_INIT;
581 struct string_list_item *item;
582 int flag;
583 const char *symref;
585 strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name);
586 if (starts_with(refname, buf.buf)) {
587 item = string_list_append(rename->remote_branches, refname);
588 symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
589 NULL, &flag);
590 if (symref && (flag & REF_ISSYMREF))
591 item->util = xstrdup(symref);
592 else
593 item->util = NULL;
595 strbuf_release(&buf);
597 return 0;
600 static int migrate_file(struct remote *remote)
602 struct strbuf buf = STRBUF_INIT;
603 int i;
605 strbuf_addf(&buf, "remote.%s.url", remote->name);
606 for (i = 0; i < remote->url_nr; i++)
607 git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
608 strbuf_reset(&buf);
609 strbuf_addf(&buf, "remote.%s.push", remote->name);
610 for (i = 0; i < remote->push.raw_nr; i++)
611 git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0);
612 strbuf_reset(&buf);
613 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
614 for (i = 0; i < remote->fetch.raw_nr; i++)
615 git_config_set_multivar(buf.buf, remote->fetch.raw[i], "^$", 0);
616 if (remote->origin == REMOTE_REMOTES)
617 unlink_or_warn(git_path("remotes/%s", remote->name));
618 else if (remote->origin == REMOTE_BRANCHES)
619 unlink_or_warn(git_path("branches/%s", remote->name));
620 strbuf_release(&buf);
622 return 0;
625 struct push_default_info
627 const char *old_name;
628 enum config_scope scope;
629 struct strbuf origin;
630 int linenr;
633 static int config_read_push_default(const char *key, const char *value,
634 void *cb)
636 struct push_default_info* info = cb;
637 if (strcmp(key, "remote.pushdefault") ||
638 !value || strcmp(value, info->old_name))
639 return 0;
641 info->scope = current_config_scope();
642 strbuf_reset(&info->origin);
643 strbuf_addstr(&info->origin, current_config_name());
644 info->linenr = current_config_line();
646 return 0;
649 static void handle_push_default(const char* old_name, const char* new_name)
651 struct push_default_info push_default = {
652 old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 };
653 git_config(config_read_push_default, &push_default);
654 if (push_default.scope >= CONFIG_SCOPE_COMMAND)
655 ; /* pass */
656 else if (push_default.scope >= CONFIG_SCOPE_LOCAL) {
657 int result = git_config_set_gently("remote.pushDefault",
658 new_name);
659 if (new_name && result && result != CONFIG_NOTHING_SET)
660 die(_("could not set '%s'"), "remote.pushDefault");
661 else if (!new_name && result && result != CONFIG_NOTHING_SET)
662 die(_("could not unset '%s'"), "remote.pushDefault");
663 } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) {
664 /* warn */
665 warning(_("The %s configuration remote.pushDefault in:\n"
666 "\t%s:%d\n"
667 "now names the non-existent remote '%s'"),
668 config_scope_name(push_default.scope),
669 push_default.origin.buf, push_default.linenr,
670 old_name);
675 static int mv(int argc, const char **argv)
677 struct option options[] = {
678 OPT_END()
680 struct remote *oldremote, *newremote;
681 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
682 old_remote_context = STRBUF_INIT;
683 struct string_list remote_branches = STRING_LIST_INIT_DUP;
684 struct rename_info rename;
685 int i, refspec_updated = 0;
687 if (argc != 3)
688 usage_with_options(builtin_remote_rename_usage, options);
690 rename.old_name = argv[1];
691 rename.new_name = argv[2];
692 rename.remote_branches = &remote_branches;
694 oldremote = remote_get(rename.old_name);
695 if (!remote_is_configured(oldremote, 1)) {
696 error(_("No such remote: '%s'"), rename.old_name);
697 exit(2);
700 if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
701 return migrate_file(oldremote);
703 newremote = remote_get(rename.new_name);
704 if (remote_is_configured(newremote, 1)) {
705 error(_("remote %s already exists."), rename.new_name);
706 exit(3);
709 if (!valid_remote_name(rename.new_name))
710 die(_("'%s' is not a valid remote name"), rename.new_name);
712 strbuf_addf(&buf, "remote.%s", rename.old_name);
713 strbuf_addf(&buf2, "remote.%s", rename.new_name);
714 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
715 return error(_("Could not rename config section '%s' to '%s'"),
716 buf.buf, buf2.buf);
718 strbuf_reset(&buf);
719 strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
720 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
721 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
722 for (i = 0; i < oldremote->fetch.raw_nr; i++) {
723 char *ptr;
725 strbuf_reset(&buf2);
726 strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
727 ptr = strstr(buf2.buf, old_remote_context.buf);
728 if (ptr) {
729 refspec_updated = 1;
730 strbuf_splice(&buf2,
731 ptr-buf2.buf + strlen(":refs/remotes/"),
732 strlen(rename.old_name), rename.new_name,
733 strlen(rename.new_name));
734 } else
735 warning(_("Not updating non-default fetch refspec\n"
736 "\t%s\n"
737 "\tPlease update the configuration manually if necessary."),
738 buf2.buf);
740 git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
743 read_branches();
744 for (i = 0; i < branch_list.nr; i++) {
745 struct string_list_item *item = branch_list.items + i;
746 struct branch_info *info = item->util;
747 if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) {
748 strbuf_reset(&buf);
749 strbuf_addf(&buf, "branch.%s.remote", item->string);
750 git_config_set(buf.buf, rename.new_name);
752 if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) {
753 strbuf_reset(&buf);
754 strbuf_addf(&buf, "branch.%s.pushRemote", item->string);
755 git_config_set(buf.buf, rename.new_name);
759 if (!refspec_updated)
760 return 0;
763 * First remove symrefs, then rename the rest, finally create
764 * the new symrefs.
766 for_each_ref(read_remote_branches, &rename);
767 for (i = 0; i < remote_branches.nr; i++) {
768 struct string_list_item *item = remote_branches.items + i;
769 int flag = 0;
771 read_ref_full(item->string, RESOLVE_REF_READING, NULL, &flag);
772 if (!(flag & REF_ISSYMREF))
773 continue;
774 if (delete_ref(NULL, item->string, NULL, REF_NO_DEREF))
775 die(_("deleting '%s' failed"), item->string);
777 for (i = 0; i < remote_branches.nr; i++) {
778 struct string_list_item *item = remote_branches.items + i;
780 if (item->util)
781 continue;
782 strbuf_reset(&buf);
783 strbuf_addstr(&buf, item->string);
784 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
785 rename.new_name, strlen(rename.new_name));
786 strbuf_reset(&buf2);
787 strbuf_addf(&buf2, "remote: renamed %s to %s",
788 item->string, buf.buf);
789 if (rename_ref(item->string, buf.buf, buf2.buf))
790 die(_("renaming '%s' failed"), item->string);
792 for (i = 0; i < remote_branches.nr; i++) {
793 struct string_list_item *item = remote_branches.items + i;
795 if (!item->util)
796 continue;
797 strbuf_reset(&buf);
798 strbuf_addstr(&buf, item->string);
799 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
800 rename.new_name, strlen(rename.new_name));
801 strbuf_reset(&buf2);
802 strbuf_addstr(&buf2, item->util);
803 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name),
804 rename.new_name, strlen(rename.new_name));
805 strbuf_reset(&buf3);
806 strbuf_addf(&buf3, "remote: renamed %s to %s",
807 item->string, buf.buf);
808 if (create_symref(buf.buf, buf2.buf, buf3.buf))
809 die(_("creating '%s' failed"), buf.buf);
811 string_list_clear(&remote_branches, 1);
813 handle_push_default(rename.old_name, rename.new_name);
815 return 0;
818 static int rm(int argc, const char **argv)
820 struct option options[] = {
821 OPT_END()
823 struct remote *remote;
824 struct strbuf buf = STRBUF_INIT;
825 struct known_remotes known_remotes = { NULL, NULL };
826 struct string_list branches = STRING_LIST_INIT_DUP;
827 struct string_list skipped = STRING_LIST_INIT_DUP;
828 struct branches_for_remote cb_data;
829 int i, result;
831 memset(&cb_data, 0, sizeof(cb_data));
832 cb_data.branches = &branches;
833 cb_data.skipped = &skipped;
834 cb_data.keep = &known_remotes;
836 if (argc != 2)
837 usage_with_options(builtin_remote_rm_usage, options);
839 remote = remote_get(argv[1]);
840 if (!remote_is_configured(remote, 1)) {
841 error(_("No such remote: '%s'"), argv[1]);
842 exit(2);
845 known_remotes.to_delete = remote;
846 for_each_remote(add_known_remote, &known_remotes);
848 read_branches();
849 for (i = 0; i < branch_list.nr; i++) {
850 struct string_list_item *item = branch_list.items + i;
851 struct branch_info *info = item->util;
852 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
853 const char *keys[] = { "remote", "merge", NULL }, **k;
854 for (k = keys; *k; k++) {
855 strbuf_reset(&buf);
856 strbuf_addf(&buf, "branch.%s.%s",
857 item->string, *k);
858 result = git_config_set_gently(buf.buf, NULL);
859 if (result && result != CONFIG_NOTHING_SET)
860 die(_("could not unset '%s'"), buf.buf);
863 if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) {
864 strbuf_reset(&buf);
865 strbuf_addf(&buf, "branch.%s.pushremote", item->string);
866 result = git_config_set_gently(buf.buf, NULL);
867 if (result && result != CONFIG_NOTHING_SET)
868 die(_("could not unset '%s'"), buf.buf);
873 * We cannot just pass a function to for_each_ref() which deletes
874 * the branches one by one, since for_each_ref() relies on cached
875 * refs, which are invalidated when deleting a branch.
877 cb_data.remote = remote;
878 result = for_each_ref(add_branch_for_removal, &cb_data);
879 strbuf_release(&buf);
881 if (!result)
882 result = delete_refs("remote: remove", &branches, REF_NO_DEREF);
883 string_list_clear(&branches, 0);
885 if (skipped.nr) {
886 fprintf_ln(stderr,
887 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
888 "to delete it, use:",
889 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
890 "to delete them, use:",
891 skipped.nr));
892 for (i = 0; i < skipped.nr; i++)
893 fprintf(stderr, " git branch -d %s\n",
894 skipped.items[i].string);
896 string_list_clear(&skipped, 0);
898 if (!result) {
899 strbuf_addf(&buf, "remote.%s", remote->name);
900 if (git_config_rename_section(buf.buf, NULL) < 1)
901 return error(_("Could not remove config section '%s'"), buf.buf);
903 handle_push_default(remote->name, NULL);
906 return result;
909 static void clear_push_info(void *util, const char *string)
911 struct push_info *info = util;
912 free(info->dest);
913 free(info);
916 static void free_remote_ref_states(struct ref_states *states)
918 string_list_clear(&states->new_refs, 0);
919 string_list_clear(&states->stale, 1);
920 string_list_clear(&states->tracked, 0);
921 string_list_clear(&states->heads, 0);
922 string_list_clear_func(&states->push, clear_push_info);
925 static int append_ref_to_tracked_list(const char *refname,
926 const struct object_id *oid, int flags, void *cb_data)
928 struct ref_states *states = cb_data;
929 struct refspec_item refspec;
931 if (flags & REF_ISSYMREF)
932 return 0;
934 memset(&refspec, 0, sizeof(refspec));
935 refspec.dst = (char *)refname;
936 if (!remote_find_tracking(states->remote, &refspec))
937 string_list_append(&states->tracked, abbrev_branch(refspec.src));
939 return 0;
942 static int get_remote_ref_states(const char *name,
943 struct ref_states *states,
944 int query)
946 states->remote = remote_get(name);
947 if (!states->remote)
948 return error(_("No such remote: '%s'"), name);
950 read_branches();
952 if (query) {
953 struct transport *transport;
954 const struct ref *remote_refs;
956 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
957 states->remote->url[0] : NULL);
958 remote_refs = transport_get_remote_refs(transport, NULL);
960 states->queried = 1;
961 if (query & GET_REF_STATES)
962 get_ref_states(remote_refs, states);
963 if (query & GET_HEAD_NAMES)
964 get_head_names(remote_refs, states);
965 if (query & GET_PUSH_REF_STATES)
966 get_push_ref_states(remote_refs, states);
967 transport_disconnect(transport);
968 } else {
969 for_each_ref(append_ref_to_tracked_list, states);
970 string_list_sort(&states->tracked);
971 get_push_ref_states_noquery(states);
974 return 0;
977 struct show_info {
978 struct string_list list;
979 struct ref_states states;
980 int width, width2;
981 int any_rebase;
984 #define SHOW_INFO_INIT { \
985 .list = STRING_LIST_INIT_DUP, \
986 .states = REF_STATES_INIT, \
989 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
991 struct show_info *info = cb_data;
992 int n = strlen(item->string);
993 if (n > info->width)
994 info->width = n;
995 string_list_insert(&info->list, item->string);
996 return 0;
999 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
1001 struct show_info *info = cb_data;
1002 struct ref_states *states = &info->states;
1003 const char *name = item->string;
1005 if (states->queried) {
1006 const char *fmt = "%s";
1007 const char *arg = "";
1008 if (string_list_has_string(&states->new_refs, name)) {
1009 fmt = _(" new (next fetch will store in remotes/%s)");
1010 arg = states->remote->name;
1011 } else if (string_list_has_string(&states->tracked, name))
1012 arg = _(" tracked");
1013 else if (string_list_has_string(&states->stale, name))
1014 arg = _(" stale (use 'git remote prune' to remove)");
1015 else
1016 arg = _(" ???");
1017 printf(" %-*s", info->width, name);
1018 printf(fmt, arg);
1019 printf("\n");
1020 } else
1021 printf(" %s\n", name);
1023 return 0;
1026 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
1028 struct show_info *show_info = cb_data;
1029 struct ref_states *states = &show_info->states;
1030 struct branch_info *branch_info = branch_item->util;
1031 struct string_list_item *item;
1032 int n;
1034 if (!branch_info->merge.nr || !branch_info->remote_name ||
1035 strcmp(states->remote->name, branch_info->remote_name))
1036 return 0;
1037 if ((n = strlen(branch_item->string)) > show_info->width)
1038 show_info->width = n;
1039 if (branch_info->rebase >= REBASE_TRUE)
1040 show_info->any_rebase = 1;
1042 item = string_list_insert(&show_info->list, branch_item->string);
1043 item->util = branch_info;
1045 return 0;
1048 static int show_local_info_item(struct string_list_item *item, void *cb_data)
1050 struct show_info *show_info = cb_data;
1051 struct branch_info *branch_info = item->util;
1052 struct string_list *merge = &branch_info->merge;
1053 int width = show_info->width + 4;
1054 int i;
1056 if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) {
1057 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1058 item->string);
1059 return 0;
1062 printf(" %-*s ", show_info->width, item->string);
1063 if (branch_info->rebase >= REBASE_TRUE) {
1064 const char *msg;
1065 if (branch_info->rebase == REBASE_INTERACTIVE)
1066 msg = _("rebases interactively onto remote %s");
1067 else if (branch_info->rebase == REBASE_MERGES)
1068 msg = _("rebases interactively (with merges) onto "
1069 "remote %s");
1070 else
1071 msg = _("rebases onto remote %s");
1072 printf_ln(msg, merge->items[0].string);
1073 return 0;
1074 } else if (show_info->any_rebase) {
1075 printf_ln(_(" merges with remote %s"), merge->items[0].string);
1076 width++;
1077 } else {
1078 printf_ln(_("merges with remote %s"), merge->items[0].string);
1080 for (i = 1; i < merge->nr; i++)
1081 printf(_("%-*s and with remote %s\n"), width, "",
1082 merge->items[i].string);
1084 return 0;
1087 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
1089 struct show_info *show_info = cb_data;
1090 struct push_info *push_info = push_item->util;
1091 struct string_list_item *item;
1092 int n;
1093 if ((n = strlen(push_item->string)) > show_info->width)
1094 show_info->width = n;
1095 if ((n = strlen(push_info->dest)) > show_info->width2)
1096 show_info->width2 = n;
1097 item = string_list_append(&show_info->list, push_item->string);
1098 item->util = push_item->util;
1099 return 0;
1103 * Sorting comparison for a string list that has push_info
1104 * structs in its util field
1106 static int cmp_string_with_push(const void *va, const void *vb)
1108 const struct string_list_item *a = va;
1109 const struct string_list_item *b = vb;
1110 const struct push_info *a_push = a->util;
1111 const struct push_info *b_push = b->util;
1112 int cmp = strcmp(a->string, b->string);
1113 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1116 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1118 struct show_info *show_info = cb_data;
1119 struct push_info *push_info = item->util;
1120 const char *src = item->string, *status = NULL;
1122 switch (push_info->status) {
1123 case PUSH_STATUS_CREATE:
1124 status = _("create");
1125 break;
1126 case PUSH_STATUS_DELETE:
1127 status = _("delete");
1128 src = _("(none)");
1129 break;
1130 case PUSH_STATUS_UPTODATE:
1131 status = _("up to date");
1132 break;
1133 case PUSH_STATUS_FASTFORWARD:
1134 status = _("fast-forwardable");
1135 break;
1136 case PUSH_STATUS_OUTOFDATE:
1137 status = _("local out of date");
1138 break;
1139 case PUSH_STATUS_NOTQUERIED:
1140 break;
1142 if (status) {
1143 if (push_info->forced)
1144 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1145 show_info->width2, push_info->dest, status);
1146 else
1147 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1148 show_info->width2, push_info->dest, status);
1149 } else {
1150 if (push_info->forced)
1151 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1152 push_info->dest);
1153 else
1154 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1155 push_info->dest);
1157 return 0;
1160 static int get_one_entry(struct remote *remote, void *priv)
1162 struct string_list *list = priv;
1163 struct strbuf url_buf = STRBUF_INIT;
1164 const char **url;
1165 int i, url_nr;
1167 if (remote->url_nr > 0) {
1168 strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
1169 string_list_append(list, remote->name)->util =
1170 strbuf_detach(&url_buf, NULL);
1171 } else
1172 string_list_append(list, remote->name)->util = NULL;
1173 if (remote->pushurl_nr) {
1174 url = remote->pushurl;
1175 url_nr = remote->pushurl_nr;
1176 } else {
1177 url = remote->url;
1178 url_nr = remote->url_nr;
1180 for (i = 0; i < url_nr; i++)
1182 strbuf_addf(&url_buf, "%s (push)", url[i]);
1183 string_list_append(list, remote->name)->util =
1184 strbuf_detach(&url_buf, NULL);
1187 return 0;
1190 static int show_all(void)
1192 struct string_list list = STRING_LIST_INIT_NODUP;
1193 int result;
1195 list.strdup_strings = 1;
1196 result = for_each_remote(get_one_entry, &list);
1198 if (!result) {
1199 int i;
1201 string_list_sort(&list);
1202 for (i = 0; i < list.nr; i++) {
1203 struct string_list_item *item = list.items + i;
1204 if (verbose)
1205 printf("%s\t%s\n", item->string,
1206 item->util ? (const char *)item->util : "");
1207 else {
1208 if (i && !strcmp((item - 1)->string, item->string))
1209 continue;
1210 printf("%s\n", item->string);
1214 string_list_clear(&list, 1);
1215 return result;
1218 static int show(int argc, const char **argv)
1220 int no_query = 0, result = 0, query_flag = 0;
1221 struct option options[] = {
1222 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1223 OPT_END()
1225 struct show_info info = SHOW_INFO_INIT;
1227 argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1230 if (argc < 1)
1231 return show_all();
1233 if (!no_query)
1234 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1236 for (; argc; argc--, argv++) {
1237 int i;
1238 const char **url;
1239 int url_nr;
1241 get_remote_ref_states(*argv, &info.states, query_flag);
1243 printf_ln(_("* remote %s"), *argv);
1244 printf_ln(_(" Fetch URL: %s"), info.states.remote->url_nr > 0 ?
1245 info.states.remote->url[0] : _("(no URL)"));
1246 if (info.states.remote->pushurl_nr) {
1247 url = info.states.remote->pushurl;
1248 url_nr = info.states.remote->pushurl_nr;
1249 } else {
1250 url = info.states.remote->url;
1251 url_nr = info.states.remote->url_nr;
1253 for (i = 0; i < url_nr; i++)
1255 * TRANSLATORS: the colon ':' should align
1256 * with the one in " Fetch URL: %s"
1257 * translation.
1259 printf_ln(_(" Push URL: %s"), url[i]);
1260 if (!i)
1261 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1262 if (no_query)
1263 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1264 else if (!info.states.heads.nr)
1265 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1266 else if (info.states.heads.nr == 1)
1267 printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string);
1268 else {
1269 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1270 " may be one of the following):\n"));
1271 for (i = 0; i < info.states.heads.nr; i++)
1272 printf(" %s\n", info.states.heads.items[i].string);
1275 /* remote branch info */
1276 info.width = 0;
1277 for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info);
1278 for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info);
1279 for_each_string_list(&info.states.stale, add_remote_to_show_info, &info);
1280 if (info.list.nr)
1281 printf_ln(Q_(" Remote branch:%s",
1282 " Remote branches:%s",
1283 info.list.nr),
1284 no_query ? _(" (status not queried)") : "");
1285 for_each_string_list(&info.list, show_remote_info_item, &info);
1286 string_list_clear(&info.list, 0);
1288 /* git pull info */
1289 info.width = 0;
1290 info.any_rebase = 0;
1291 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1292 if (info.list.nr)
1293 printf_ln(Q_(" Local branch configured for 'git pull':",
1294 " Local branches configured for 'git pull':",
1295 info.list.nr));
1296 for_each_string_list(&info.list, show_local_info_item, &info);
1297 string_list_clear(&info.list, 0);
1299 /* git push info */
1300 if (info.states.remote->mirror)
1301 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1303 info.width = info.width2 = 0;
1304 for_each_string_list(&info.states.push, add_push_to_show_info, &info);
1305 QSORT(info.list.items, info.list.nr, cmp_string_with_push);
1306 if (info.list.nr)
1307 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1308 " Local refs configured for 'git push'%s:",
1309 info.list.nr),
1310 no_query ? _(" (status not queried)") : "");
1311 for_each_string_list(&info.list, show_push_info_item, &info);
1312 string_list_clear(&info.list, 0);
1314 free_remote_ref_states(&info.states);
1317 return result;
1320 static int set_head(int argc, const char **argv)
1322 int i, opt_a = 0, opt_d = 0, result = 0;
1323 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1324 char *head_name = NULL;
1326 struct option options[] = {
1327 OPT_BOOL('a', "auto", &opt_a,
1328 N_("set refs/remotes/<name>/HEAD according to remote")),
1329 OPT_BOOL('d', "delete", &opt_d,
1330 N_("delete refs/remotes/<name>/HEAD")),
1331 OPT_END()
1333 argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1335 if (argc)
1336 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1338 if (!opt_a && !opt_d && argc == 2) {
1339 head_name = xstrdup(argv[1]);
1340 } else if (opt_a && !opt_d && argc == 1) {
1341 struct ref_states states = REF_STATES_INIT;
1342 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1343 if (!states.heads.nr)
1344 result |= error(_("Cannot determine remote HEAD"));
1345 else if (states.heads.nr > 1) {
1346 result |= error(_("Multiple remote HEAD branches. "
1347 "Please choose one explicitly with:"));
1348 for (i = 0; i < states.heads.nr; i++)
1349 fprintf(stderr, " git remote set-head %s %s\n",
1350 argv[0], states.heads.items[i].string);
1351 } else
1352 head_name = xstrdup(states.heads.items[0].string);
1353 free_remote_ref_states(&states);
1354 } else if (opt_d && !opt_a && argc == 1) {
1355 if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF))
1356 result |= error(_("Could not delete %s"), buf.buf);
1357 } else
1358 usage_with_options(builtin_remote_sethead_usage, options);
1360 if (head_name) {
1361 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1362 /* make sure it's valid */
1363 if (!ref_exists(buf2.buf))
1364 result |= error(_("Not a valid ref: %s"), buf2.buf);
1365 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1366 result |= error(_("Could not setup %s"), buf.buf);
1367 else if (opt_a)
1368 printf("%s/HEAD set to %s\n", argv[0], head_name);
1369 free(head_name);
1372 strbuf_release(&buf);
1373 strbuf_release(&buf2);
1374 return result;
1377 static int prune_remote(const char *remote, int dry_run)
1379 int result = 0;
1380 struct ref_states states = REF_STATES_INIT;
1381 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1382 struct string_list_item *item;
1383 const char *dangling_msg = dry_run
1384 ? _(" %s will become dangling!")
1385 : _(" %s has become dangling!");
1387 get_remote_ref_states(remote, &states, GET_REF_STATES);
1389 if (!states.stale.nr) {
1390 free_remote_ref_states(&states);
1391 return 0;
1394 printf_ln(_("Pruning %s"), remote);
1395 printf_ln(_("URL: %s"),
1396 states.remote->url_nr
1397 ? states.remote->url[0]
1398 : _("(no URL)"));
1400 for_each_string_list_item(item, &states.stale)
1401 string_list_append(&refs_to_prune, item->util);
1402 string_list_sort(&refs_to_prune);
1404 if (!dry_run)
1405 result |= delete_refs("remote: prune", &refs_to_prune, 0);
1407 for_each_string_list_item(item, &states.stale) {
1408 const char *refname = item->util;
1410 if (dry_run)
1411 printf_ln(_(" * [would prune] %s"),
1412 abbrev_ref(refname, "refs/remotes/"));
1413 else
1414 printf_ln(_(" * [pruned] %s"),
1415 abbrev_ref(refname, "refs/remotes/"));
1418 warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
1420 string_list_clear(&refs_to_prune, 0);
1421 free_remote_ref_states(&states);
1422 return result;
1425 static int prune(int argc, const char **argv)
1427 int dry_run = 0, result = 0;
1428 struct option options[] = {
1429 OPT__DRY_RUN(&dry_run, N_("dry run")),
1430 OPT_END()
1433 argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1436 if (argc < 1)
1437 usage_with_options(builtin_remote_prune_usage, options);
1439 for (; argc; argc--, argv++)
1440 result |= prune_remote(*argv, dry_run);
1442 return result;
1445 static int get_remote_default(const char *key, const char *value, void *priv)
1447 if (strcmp(key, "remotes.default") == 0) {
1448 int *found = priv;
1449 *found = 1;
1451 return 0;
1454 static int update(int argc, const char **argv)
1456 int i, prune = -1;
1457 struct option options[] = {
1458 OPT_BOOL('p', "prune", &prune,
1459 N_("prune remotes after fetching")),
1460 OPT_END()
1462 struct strvec fetch_argv = STRVEC_INIT;
1463 int default_defined = 0;
1464 int retval;
1466 argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1467 PARSE_OPT_KEEP_ARGV0);
1469 strvec_push(&fetch_argv, "fetch");
1471 if (prune != -1)
1472 strvec_push(&fetch_argv, prune ? "--prune" : "--no-prune");
1473 if (verbose)
1474 strvec_push(&fetch_argv, "-v");
1475 strvec_push(&fetch_argv, "--multiple");
1476 if (argc < 2)
1477 strvec_push(&fetch_argv, "default");
1478 for (i = 1; i < argc; i++)
1479 strvec_push(&fetch_argv, argv[i]);
1481 if (strcmp(fetch_argv.v[fetch_argv.nr-1], "default") == 0) {
1482 git_config(get_remote_default, &default_defined);
1483 if (!default_defined) {
1484 strvec_pop(&fetch_argv);
1485 strvec_push(&fetch_argv, "--all");
1489 retval = run_command_v_opt(fetch_argv.v, RUN_GIT_CMD);
1490 strvec_clear(&fetch_argv);
1491 return retval;
1494 static int remove_all_fetch_refspecs(const char *key)
1496 return git_config_set_multivar_gently(key, NULL, NULL,
1497 CONFIG_FLAGS_MULTI_REPLACE);
1500 static void add_branches(struct remote *remote, const char **branches,
1501 const char *key)
1503 const char *remotename = remote->name;
1504 int mirror = remote->mirror;
1505 struct strbuf refspec = STRBUF_INIT;
1507 for (; *branches; branches++)
1508 add_branch(key, *branches, remotename, mirror, &refspec);
1510 strbuf_release(&refspec);
1513 static int set_remote_branches(const char *remotename, const char **branches,
1514 int add_mode)
1516 struct strbuf key = STRBUF_INIT;
1517 struct remote *remote;
1519 strbuf_addf(&key, "remote.%s.fetch", remotename);
1521 remote = remote_get(remotename);
1522 if (!remote_is_configured(remote, 1)) {
1523 error(_("No such remote '%s'"), remotename);
1524 exit(2);
1527 if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
1528 strbuf_release(&key);
1529 return 1;
1531 add_branches(remote, branches, key.buf);
1533 strbuf_release(&key);
1534 return 0;
1537 static int set_branches(int argc, const char **argv)
1539 int add_mode = 0;
1540 struct option options[] = {
1541 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1542 OPT_END()
1545 argc = parse_options(argc, argv, NULL, options,
1546 builtin_remote_setbranches_usage, 0);
1547 if (argc == 0) {
1548 error(_("no remote specified"));
1549 usage_with_options(builtin_remote_setbranches_usage, options);
1551 argv[argc] = NULL;
1553 return set_remote_branches(argv[0], argv + 1, add_mode);
1556 static int get_url(int argc, const char **argv)
1558 int i, push_mode = 0, all_mode = 0;
1559 const char *remotename = NULL;
1560 struct remote *remote;
1561 const char **url;
1562 int url_nr;
1563 struct option options[] = {
1564 OPT_BOOL('\0', "push", &push_mode,
1565 N_("query push URLs rather than fetch URLs")),
1566 OPT_BOOL('\0', "all", &all_mode,
1567 N_("return all URLs")),
1568 OPT_END()
1570 argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0);
1572 if (argc != 1)
1573 usage_with_options(builtin_remote_geturl_usage, options);
1575 remotename = argv[0];
1577 remote = remote_get(remotename);
1578 if (!remote_is_configured(remote, 1)) {
1579 error(_("No such remote '%s'"), remotename);
1580 exit(2);
1583 url_nr = 0;
1584 if (push_mode) {
1585 url = remote->pushurl;
1586 url_nr = remote->pushurl_nr;
1588 /* else fetch mode */
1590 /* Use the fetch URL when no push URLs were found or requested. */
1591 if (!url_nr) {
1592 url = remote->url;
1593 url_nr = remote->url_nr;
1596 if (!url_nr)
1597 die(_("no URLs configured for remote '%s'"), remotename);
1599 if (all_mode) {
1600 for (i = 0; i < url_nr; i++)
1601 printf_ln("%s", url[i]);
1602 } else {
1603 printf_ln("%s", *url);
1606 return 0;
1609 static int set_url(int argc, const char **argv)
1611 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1612 int matches = 0, negative_matches = 0;
1613 const char *remotename = NULL;
1614 const char *newurl = NULL;
1615 const char *oldurl = NULL;
1616 struct remote *remote;
1617 regex_t old_regex;
1618 const char **urlset;
1619 int urlset_nr;
1620 struct strbuf name_buf = STRBUF_INIT;
1621 struct option options[] = {
1622 OPT_BOOL('\0', "push", &push_mode,
1623 N_("manipulate push URLs")),
1624 OPT_BOOL('\0', "add", &add_mode,
1625 N_("add URL")),
1626 OPT_BOOL('\0', "delete", &delete_mode,
1627 N_("delete URLs")),
1628 OPT_END()
1630 argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage,
1631 PARSE_OPT_KEEP_ARGV0);
1633 if (add_mode && delete_mode)
1634 die(_("--add --delete doesn't make sense"));
1636 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1637 usage_with_options(builtin_remote_seturl_usage, options);
1639 remotename = argv[1];
1640 newurl = argv[2];
1641 if (argc > 3)
1642 oldurl = argv[3];
1644 if (delete_mode)
1645 oldurl = newurl;
1647 remote = remote_get(remotename);
1648 if (!remote_is_configured(remote, 1)) {
1649 error(_("No such remote '%s'"), remotename);
1650 exit(2);
1653 if (push_mode) {
1654 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1655 urlset = remote->pushurl;
1656 urlset_nr = remote->pushurl_nr;
1657 } else {
1658 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1659 urlset = remote->url;
1660 urlset_nr = remote->url_nr;
1663 /* Special cases that add new entry. */
1664 if ((!oldurl && !delete_mode) || add_mode) {
1665 if (add_mode)
1666 git_config_set_multivar(name_buf.buf, newurl,
1667 "^$", 0);
1668 else
1669 git_config_set(name_buf.buf, newurl);
1670 goto out;
1673 /* Old URL specified. Demand that one matches. */
1674 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1675 die(_("Invalid old URL pattern: %s"), oldurl);
1677 for (i = 0; i < urlset_nr; i++)
1678 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1679 matches++;
1680 else
1681 negative_matches++;
1682 if (!delete_mode && !matches)
1683 die(_("No such URL found: %s"), oldurl);
1684 if (delete_mode && !negative_matches && !push_mode)
1685 die(_("Will not delete all non-push URLs"));
1687 regfree(&old_regex);
1689 if (!delete_mode)
1690 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1691 else
1692 git_config_set_multivar(name_buf.buf, NULL, oldurl,
1693 CONFIG_FLAGS_MULTI_REPLACE);
1694 out:
1695 strbuf_release(&name_buf);
1696 return 0;
1699 int cmd_remote(int argc, const char **argv, const char *prefix)
1701 struct option options[] = {
1702 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1703 OPT_END()
1705 int result;
1707 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1708 PARSE_OPT_STOP_AT_NON_OPTION);
1710 if (argc < 1)
1711 result = show_all();
1712 else if (!strcmp(argv[0], "add"))
1713 result = add(argc, argv);
1714 else if (!strcmp(argv[0], "rename"))
1715 result = mv(argc, argv);
1716 else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove"))
1717 result = rm(argc, argv);
1718 else if (!strcmp(argv[0], "set-head"))
1719 result = set_head(argc, argv);
1720 else if (!strcmp(argv[0], "set-branches"))
1721 result = set_branches(argc, argv);
1722 else if (!strcmp(argv[0], "get-url"))
1723 result = get_url(argc, argv);
1724 else if (!strcmp(argv[0], "set-url"))
1725 result = set_url(argc, argv);
1726 else if (!strcmp(argv[0], "show"))
1727 result = show(argc, argv);
1728 else if (!strcmp(argv[0], "prune"))
1729 result = prune(argc, argv);
1730 else if (!strcmp(argv[0], "update"))
1731 result = update(argc, argv);
1732 else {
1733 error(_("Unknown subcommand: %s"), argv[0]);
1734 usage_with_options(builtin_remote_usage, options);
1737 return result ? 1 : 0;