]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/remote.c
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / builtin / remote.c
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"
16
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
32 };
33
34 static const char * const builtin_remote_add_usage[] = {
35 N_("git remote add [<options>] <name> <url>"),
36 NULL
37 };
38
39 static const char * const builtin_remote_rename_usage[] = {
40 N_("git remote rename [--[no-]progress] <old> <new>"),
41 NULL
42 };
43
44 static const char * const builtin_remote_rm_usage[] = {
45 N_("git remote remove <name>"),
46 NULL
47 };
48
49 static const char * const builtin_remote_sethead_usage[] = {
50 N_("git remote set-head <name> (-a | --auto | -d | --delete | <branch>)"),
51 NULL
52 };
53
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
58 };
59
60 static const char * const builtin_remote_show_usage[] = {
61 N_("git remote show [<options>] <name>"),
62 NULL
63 };
64
65 static const char * const builtin_remote_prune_usage[] = {
66 N_("git remote prune [<options>] <name>"),
67 NULL
68 };
69
70 static const char * const builtin_remote_update_usage[] = {
71 N_("git remote update [<options>] [<group> | <remote>]..."),
72 NULL
73 };
74
75 static const char * const builtin_remote_geturl_usage[] = {
76 N_("git remote get-url [--push] [--all] <name>"),
77 NULL
78 };
79
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
85 };
86
87 #define GET_REF_STATES (1<<0)
88 #define GET_HEAD_NAMES (1<<1)
89 #define GET_PUSH_REF_STATES (1<<2)
90
91 static int verbose;
92
93 static int fetch_remote(const char *name)
94 {
95 const char *argv[] = { "fetch", name, NULL, NULL };
96 if (verbose) {
97 argv[1] = "-v";
98 argv[2] = name;
99 }
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;
104 }
105
106 enum {
107 TAGS_UNSET = 0,
108 TAGS_DEFAULT = 1,
109 TAGS_SET = 2
110 };
111
112 #define MIRROR_NONE 0
113 #define MIRROR_FETCH 1
114 #define MIRROR_PUSH 2
115 #define MIRROR_BOTH (MIRROR_FETCH|MIRROR_PUSH)
116
117 static void add_branch(const char *key, const char *branchname,
118 const char *remotename, int mirror, struct strbuf *tmp)
119 {
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);
129 }
130
131 static const char mirror_advice[] =
132 N_("--mirror is dangerous and deprecated; please\n"
133 "\t use --mirror=fetch or --mirror=push instead");
134
135 static int parse_mirror_opt(const struct option *opt, const char *arg, int not)
136 {
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;
143 }
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;
151 }
152
153 static int add(int argc, const char **argv, const char *prefix)
154 {
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;
163
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()
178 };
179
180 argc = parse_options(argc, argv, prefix, options,
181 builtin_remote_add_usage, 0);
182
183 if (argc != 2)
184 usage_with_options(builtin_remote_add_usage, options);
185
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"));
190
191 name = argv[0];
192 url = argv[1];
193
194 remote = remote_get(name);
195 if (remote_is_configured(remote, 1)) {
196 error(_("remote %s already exists."), name);
197 exit(3);
198 }
199
200 if (!valid_remote_name(name))
201 die(_("'%s' is not a valid remote name"), name);
202
203 strbuf_addf(&buf, "remote.%s.url", name);
204 git_config_set(buf.buf, url);
205
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);
214 }
215 }
216
217 if (mirror & MIRROR_PUSH) {
218 strbuf_reset(&buf);
219 strbuf_addf(&buf, "remote.%s.mirror", name);
220 git_config_set(buf.buf, "true");
221 }
222
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");
228 }
229
230 if (fetch && fetch_remote(name))
231 return 1;
232
233 if (master) {
234 strbuf_reset(&buf);
235 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
236
237 strbuf_reset(&buf2);
238 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
239
240 if (create_symref(buf.buf, buf2.buf, "remote add"))
241 return error(_("Could not setup master '%s'"), master);
242 }
243
244 strbuf_release(&buf);
245 strbuf_release(&buf2);
246 string_list_clear(&track, 0);
247
248 return 0;
249 }
250
251 struct branch_info {
252 char *remote_name;
253 struct string_list merge;
254 enum rebase_type rebase;
255 char *push_remote_name;
256 };
257
258 static struct string_list branch_list = STRING_LIST_INIT_NODUP;
259
260 static const char *abbrev_ref(const char *name, const char *prefix)
261 {
262 skip_prefix(name, prefix, &name);
263 return name;
264 }
265 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
266
267 static int config_read_branches(const char *key, const char *value,
268 void *data UNUSED)
269 {
270 const char *orig_key = key;
271 char *name;
272 struct string_list_item *item;
273 struct branch_info *info;
274 enum { REMOTE, MERGE, REBASE, PUSH_REMOTE } type;
275 size_t key_len;
276
277 if (!starts_with(key, "branch."))
278 return 0;
279
280 key += strlen("branch.");
281 if (strip_suffix(key, ".remote", &key_len))
282 type = REMOTE;
283 else if (strip_suffix(key, ".merge", &key_len))
284 type = MERGE;
285 else if (strip_suffix(key, ".rebase", &key_len))
286 type = REBASE;
287 else if (strip_suffix(key, ".pushremote", &key_len))
288 type = PUSH_REMOTE;
289 else
290 return 0;
291 name = xmemdupz(key, key_len);
292
293 item = string_list_insert(&branch_list, name);
294
295 if (!item->util)
296 item->util = xcalloc(1, sizeof(struct branch_info));
297 info = item->util;
298 switch (type) {
299 case REMOTE:
300 if (info->remote_name)
301 warning(_("more than one %s"), orig_key);
302 info->remote_name = xstrdup(value);
303 break;
304 case MERGE: {
305 char *space = strchr(value, ' ');
306 value = abbrev_branch(value);
307 while (space) {
308 char *merge;
309 merge = xstrndup(value, space - value);
310 string_list_append(&info->merge, merge);
311 value = abbrev_branch(space + 1);
312 space = strchr(value, ' ');
313 }
314 string_list_append(&info->merge, xstrdup(value));
315 break;
316 }
317 case REBASE:
318 /*
319 * Consider invalid values as false and check the
320 * truth value with >= REBASE_TRUE.
321 */
322 info->rebase = rebase_parse_value(value);
323 if (info->rebase == REBASE_INVALID)
324 warning(_("unhandled branch.%s.rebase=%s; assuming "
325 "'true'"), name, value);
326 break;
327 case PUSH_REMOTE:
328 if (info->push_remote_name)
329 warning(_("more than one %s"), orig_key);
330 info->push_remote_name = xstrdup(value);
331 break;
332 default:
333 BUG("unexpected type=%d", type);
334 }
335
336 return 0;
337 }
338
339 static void read_branches(void)
340 {
341 if (branch_list.nr)
342 return;
343 git_config(config_read_branches, NULL);
344 }
345
346 struct ref_states {
347 struct remote *remote;
348 struct string_list new_refs, skipped, stale, tracked, heads, push;
349 int queried;
350 };
351
352 #define REF_STATES_INIT { \
353 .new_refs = STRING_LIST_INIT_DUP, \
354 .skipped = STRING_LIST_INIT_DUP, \
355 .stale = STRING_LIST_INIT_DUP, \
356 .tracked = STRING_LIST_INIT_DUP, \
357 .heads = STRING_LIST_INIT_DUP, \
358 .push = STRING_LIST_INIT_DUP, \
359 }
360
361 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
362 {
363 struct ref *fetch_map = NULL, **tail = &fetch_map;
364 struct ref *ref, *stale_refs;
365 int i;
366
367 for (i = 0; i < states->remote->fetch.nr; i++)
368 if (get_fetch_map(remote_refs, &states->remote->fetch.items[i], &tail, 1))
369 die(_("Could not get fetch map for refspec %s"),
370 states->remote->fetch.raw[i]);
371
372 for (ref = fetch_map; ref; ref = ref->next) {
373 if (omit_name_by_refspec(ref->name, &states->remote->fetch))
374 string_list_append(&states->skipped, abbrev_branch(ref->name));
375 else if (!ref->peer_ref || !ref_exists(ref->peer_ref->name))
376 string_list_append(&states->new_refs, abbrev_branch(ref->name));
377 else
378 string_list_append(&states->tracked, abbrev_branch(ref->name));
379 }
380 stale_refs = get_stale_heads(&states->remote->fetch, fetch_map);
381 for (ref = stale_refs; ref; ref = ref->next) {
382 struct string_list_item *item =
383 string_list_append(&states->stale, abbrev_branch(ref->name));
384 item->util = xstrdup(ref->name);
385 }
386 free_refs(stale_refs);
387 free_refs(fetch_map);
388
389 string_list_sort(&states->new_refs);
390 string_list_sort(&states->skipped);
391 string_list_sort(&states->tracked);
392 string_list_sort(&states->stale);
393
394 return 0;
395 }
396
397 struct push_info {
398 char *dest;
399 int forced;
400 enum {
401 PUSH_STATUS_CREATE = 0,
402 PUSH_STATUS_DELETE,
403 PUSH_STATUS_UPTODATE,
404 PUSH_STATUS_FASTFORWARD,
405 PUSH_STATUS_OUTOFDATE,
406 PUSH_STATUS_NOTQUERIED
407 } status;
408 };
409
410 static int get_push_ref_states(const struct ref *remote_refs,
411 struct ref_states *states)
412 {
413 struct remote *remote = states->remote;
414 struct ref *ref, *local_refs, *push_map;
415 if (remote->mirror)
416 return 0;
417
418 local_refs = get_local_heads();
419 push_map = copy_ref_list(remote_refs);
420
421 match_push_refs(local_refs, &push_map, &remote->push, MATCH_REFS_NONE);
422
423 for (ref = push_map; ref; ref = ref->next) {
424 struct string_list_item *item;
425 struct push_info *info;
426
427 if (!ref->peer_ref)
428 continue;
429 oidcpy(&ref->new_oid, &ref->peer_ref->new_oid);
430
431 item = string_list_append(&states->push,
432 abbrev_branch(ref->peer_ref->name));
433 item->util = xcalloc(1, sizeof(struct push_info));
434 info = item->util;
435 info->forced = ref->force;
436 info->dest = xstrdup(abbrev_branch(ref->name));
437
438 if (is_null_oid(&ref->new_oid)) {
439 info->status = PUSH_STATUS_DELETE;
440 } else if (oideq(&ref->old_oid, &ref->new_oid))
441 info->status = PUSH_STATUS_UPTODATE;
442 else if (is_null_oid(&ref->old_oid))
443 info->status = PUSH_STATUS_CREATE;
444 else if (has_object_file(&ref->old_oid) &&
445 ref_newer(&ref->new_oid, &ref->old_oid))
446 info->status = PUSH_STATUS_FASTFORWARD;
447 else
448 info->status = PUSH_STATUS_OUTOFDATE;
449 }
450 free_refs(local_refs);
451 free_refs(push_map);
452 return 0;
453 }
454
455 static int get_push_ref_states_noquery(struct ref_states *states)
456 {
457 int i;
458 struct remote *remote = states->remote;
459 struct string_list_item *item;
460 struct push_info *info;
461
462 if (remote->mirror)
463 return 0;
464
465 if (!remote->push.nr) {
466 item = string_list_append(&states->push, _("(matching)"));
467 info = item->util = xcalloc(1, sizeof(struct push_info));
468 info->status = PUSH_STATUS_NOTQUERIED;
469 info->dest = xstrdup(item->string);
470 }
471 for (i = 0; i < remote->push.nr; i++) {
472 const struct refspec_item *spec = &remote->push.items[i];
473 if (spec->matching)
474 item = string_list_append(&states->push, _("(matching)"));
475 else if (strlen(spec->src))
476 item = string_list_append(&states->push, spec->src);
477 else
478 item = string_list_append(&states->push, _("(delete)"));
479
480 info = item->util = xcalloc(1, sizeof(struct push_info));
481 info->forced = spec->force;
482 info->status = PUSH_STATUS_NOTQUERIED;
483 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
484 }
485 return 0;
486 }
487
488 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
489 {
490 struct ref *ref, *matches;
491 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
492 struct refspec_item refspec;
493
494 memset(&refspec, 0, sizeof(refspec));
495 refspec.force = 0;
496 refspec.pattern = 1;
497 refspec.src = refspec.dst = "refs/heads/*";
498 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
499 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
500 fetch_map, 1);
501 for (ref = matches; ref; ref = ref->next)
502 string_list_append(&states->heads, abbrev_branch(ref->name));
503
504 free_refs(fetch_map);
505 free_refs(matches);
506
507 return 0;
508 }
509
510 struct known_remote {
511 struct known_remote *next;
512 struct remote *remote;
513 };
514
515 struct known_remotes {
516 struct remote *to_delete;
517 struct known_remote *list;
518 };
519
520 static int add_known_remote(struct remote *remote, void *cb_data)
521 {
522 struct known_remotes *all = cb_data;
523 struct known_remote *r;
524
525 if (!strcmp(all->to_delete->name, remote->name))
526 return 0;
527
528 r = xmalloc(sizeof(*r));
529 r->remote = remote;
530 r->next = all->list;
531 all->list = r;
532 return 0;
533 }
534
535 struct branches_for_remote {
536 struct remote *remote;
537 struct string_list *branches, *skipped;
538 struct known_remotes *keep;
539 };
540
541 static int add_branch_for_removal(const char *refname,
542 const struct object_id *oid UNUSED,
543 int flags UNUSED, void *cb_data)
544 {
545 struct branches_for_remote *branches = cb_data;
546 struct refspec_item refspec;
547 struct known_remote *kr;
548
549 memset(&refspec, 0, sizeof(refspec));
550 refspec.dst = (char *)refname;
551 if (remote_find_tracking(branches->remote, &refspec))
552 return 0;
553
554 /* don't delete a branch if another remote also uses it */
555 for (kr = branches->keep->list; kr; kr = kr->next) {
556 memset(&refspec, 0, sizeof(refspec));
557 refspec.dst = (char *)refname;
558 if (!remote_find_tracking(kr->remote, &refspec))
559 return 0;
560 }
561
562 /* don't delete non-remote-tracking refs */
563 if (!starts_with(refname, "refs/remotes/")) {
564 /* advise user how to delete local branches */
565 if (starts_with(refname, "refs/heads/"))
566 string_list_append(branches->skipped,
567 abbrev_branch(refname));
568 /* silently skip over other non-remote refs */
569 return 0;
570 }
571
572 string_list_append(branches->branches, refname);
573
574 return 0;
575 }
576
577 struct rename_info {
578 const char *old_name;
579 const char *new_name;
580 struct string_list *remote_branches;
581 uint32_t symrefs_nr;
582 };
583
584 static int read_remote_branches(const char *refname,
585 const struct object_id *oid UNUSED,
586 int flags UNUSED, void *cb_data)
587 {
588 struct rename_info *rename = cb_data;
589 struct strbuf buf = STRBUF_INIT;
590 struct string_list_item *item;
591 int flag;
592 const char *symref;
593
594 strbuf_addf(&buf, "refs/remotes/%s/", rename->old_name);
595 if (starts_with(refname, buf.buf)) {
596 item = string_list_append(rename->remote_branches, refname);
597 symref = resolve_ref_unsafe(refname, RESOLVE_REF_READING,
598 NULL, &flag);
599 if (symref && (flag & REF_ISSYMREF)) {
600 item->util = xstrdup(symref);
601 rename->symrefs_nr++;
602 } else {
603 item->util = NULL;
604 }
605 }
606 strbuf_release(&buf);
607
608 return 0;
609 }
610
611 static int migrate_file(struct remote *remote)
612 {
613 struct strbuf buf = STRBUF_INIT;
614 int i;
615
616 strbuf_addf(&buf, "remote.%s.url", remote->name);
617 for (i = 0; i < remote->url_nr; i++)
618 git_config_set_multivar(buf.buf, remote->url[i], "^$", 0);
619 strbuf_reset(&buf);
620 strbuf_addf(&buf, "remote.%s.push", remote->name);
621 for (i = 0; i < remote->push.raw_nr; i++)
622 git_config_set_multivar(buf.buf, remote->push.raw[i], "^$", 0);
623 strbuf_reset(&buf);
624 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
625 for (i = 0; i < remote->fetch.raw_nr; i++)
626 git_config_set_multivar(buf.buf, remote->fetch.raw[i], "^$", 0);
627 if (remote->origin == REMOTE_REMOTES)
628 unlink_or_warn(git_path("remotes/%s", remote->name));
629 else if (remote->origin == REMOTE_BRANCHES)
630 unlink_or_warn(git_path("branches/%s", remote->name));
631 strbuf_release(&buf);
632
633 return 0;
634 }
635
636 struct push_default_info
637 {
638 const char *old_name;
639 enum config_scope scope;
640 struct strbuf origin;
641 int linenr;
642 };
643
644 static int config_read_push_default(const char *key, const char *value,
645 void *cb)
646 {
647 struct push_default_info* info = cb;
648 if (strcmp(key, "remote.pushdefault") ||
649 !value || strcmp(value, info->old_name))
650 return 0;
651
652 info->scope = current_config_scope();
653 strbuf_reset(&info->origin);
654 strbuf_addstr(&info->origin, current_config_name());
655 info->linenr = current_config_line();
656
657 return 0;
658 }
659
660 static void handle_push_default(const char* old_name, const char* new_name)
661 {
662 struct push_default_info push_default = {
663 old_name, CONFIG_SCOPE_UNKNOWN, STRBUF_INIT, -1 };
664 git_config(config_read_push_default, &push_default);
665 if (push_default.scope >= CONFIG_SCOPE_COMMAND)
666 ; /* pass */
667 else if (push_default.scope >= CONFIG_SCOPE_LOCAL) {
668 int result = git_config_set_gently("remote.pushDefault",
669 new_name);
670 if (new_name && result && result != CONFIG_NOTHING_SET)
671 die(_("could not set '%s'"), "remote.pushDefault");
672 else if (!new_name && result && result != CONFIG_NOTHING_SET)
673 die(_("could not unset '%s'"), "remote.pushDefault");
674 } else if (push_default.scope >= CONFIG_SCOPE_SYSTEM) {
675 /* warn */
676 warning(_("The %s configuration remote.pushDefault in:\n"
677 "\t%s:%d\n"
678 "now names the non-existent remote '%s'"),
679 config_scope_name(push_default.scope),
680 push_default.origin.buf, push_default.linenr,
681 old_name);
682 }
683 }
684
685
686 static int mv(int argc, const char **argv, const char *prefix)
687 {
688 int show_progress = isatty(2);
689 struct option options[] = {
690 OPT_BOOL(0, "progress", &show_progress, N_("force progress reporting")),
691 OPT_END()
692 };
693 struct remote *oldremote, *newremote;
694 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT,
695 old_remote_context = STRBUF_INIT;
696 struct string_list remote_branches = STRING_LIST_INIT_DUP;
697 struct rename_info rename;
698 int i, refs_renamed_nr = 0, refspec_updated = 0;
699 struct progress *progress = NULL;
700
701 argc = parse_options(argc, argv, prefix, options,
702 builtin_remote_rename_usage, 0);
703
704 if (argc != 2)
705 usage_with_options(builtin_remote_rename_usage, options);
706
707 rename.old_name = argv[0];
708 rename.new_name = argv[1];
709 rename.remote_branches = &remote_branches;
710 rename.symrefs_nr = 0;
711
712 oldremote = remote_get(rename.old_name);
713 if (!remote_is_configured(oldremote, 1)) {
714 error(_("No such remote: '%s'"), rename.old_name);
715 exit(2);
716 }
717
718 if (!strcmp(rename.old_name, rename.new_name) && oldremote->origin != REMOTE_CONFIG)
719 return migrate_file(oldremote);
720
721 newremote = remote_get(rename.new_name);
722 if (remote_is_configured(newremote, 1)) {
723 error(_("remote %s already exists."), rename.new_name);
724 exit(3);
725 }
726
727 if (!valid_remote_name(rename.new_name))
728 die(_("'%s' is not a valid remote name"), rename.new_name);
729
730 strbuf_addf(&buf, "remote.%s", rename.old_name);
731 strbuf_addf(&buf2, "remote.%s", rename.new_name);
732 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
733 return error(_("Could not rename config section '%s' to '%s'"),
734 buf.buf, buf2.buf);
735
736 strbuf_reset(&buf);
737 strbuf_addf(&buf, "remote.%s.fetch", rename.new_name);
738 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
739 strbuf_addf(&old_remote_context, ":refs/remotes/%s/", rename.old_name);
740 for (i = 0; i < oldremote->fetch.raw_nr; i++) {
741 char *ptr;
742
743 strbuf_reset(&buf2);
744 strbuf_addstr(&buf2, oldremote->fetch.raw[i]);
745 ptr = strstr(buf2.buf, old_remote_context.buf);
746 if (ptr) {
747 refspec_updated = 1;
748 strbuf_splice(&buf2,
749 ptr-buf2.buf + strlen(":refs/remotes/"),
750 strlen(rename.old_name), rename.new_name,
751 strlen(rename.new_name));
752 } else
753 warning(_("Not updating non-default fetch refspec\n"
754 "\t%s\n"
755 "\tPlease update the configuration manually if necessary."),
756 buf2.buf);
757
758 git_config_set_multivar(buf.buf, buf2.buf, "^$", 0);
759 }
760
761 read_branches();
762 for (i = 0; i < branch_list.nr; i++) {
763 struct string_list_item *item = branch_list.items + i;
764 struct branch_info *info = item->util;
765 if (info->remote_name && !strcmp(info->remote_name, rename.old_name)) {
766 strbuf_reset(&buf);
767 strbuf_addf(&buf, "branch.%s.remote", item->string);
768 git_config_set(buf.buf, rename.new_name);
769 }
770 if (info->push_remote_name && !strcmp(info->push_remote_name, rename.old_name)) {
771 strbuf_reset(&buf);
772 strbuf_addf(&buf, "branch.%s.pushRemote", item->string);
773 git_config_set(buf.buf, rename.new_name);
774 }
775 }
776
777 if (!refspec_updated)
778 return 0;
779
780 /*
781 * First remove symrefs, then rename the rest, finally create
782 * the new symrefs.
783 */
784 for_each_ref(read_remote_branches, &rename);
785 if (show_progress) {
786 /*
787 * Count symrefs twice, since "renaming" them is done by
788 * deleting and recreating them in two separate passes.
789 */
790 progress = start_progress(_("Renaming remote references"),
791 rename.remote_branches->nr + rename.symrefs_nr);
792 }
793 for (i = 0; i < remote_branches.nr; i++) {
794 struct string_list_item *item = remote_branches.items + i;
795 struct strbuf referent = STRBUF_INIT;
796
797 if (refs_read_symbolic_ref(get_main_ref_store(the_repository), item->string,
798 &referent))
799 continue;
800 if (delete_ref(NULL, item->string, NULL, REF_NO_DEREF))
801 die(_("deleting '%s' failed"), item->string);
802
803 strbuf_release(&referent);
804 display_progress(progress, ++refs_renamed_nr);
805 }
806 for (i = 0; i < remote_branches.nr; i++) {
807 struct string_list_item *item = remote_branches.items + i;
808
809 if (item->util)
810 continue;
811 strbuf_reset(&buf);
812 strbuf_addstr(&buf, item->string);
813 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
814 rename.new_name, strlen(rename.new_name));
815 strbuf_reset(&buf2);
816 strbuf_addf(&buf2, "remote: renamed %s to %s",
817 item->string, buf.buf);
818 if (rename_ref(item->string, buf.buf, buf2.buf))
819 die(_("renaming '%s' failed"), item->string);
820 display_progress(progress, ++refs_renamed_nr);
821 }
822 for (i = 0; i < remote_branches.nr; i++) {
823 struct string_list_item *item = remote_branches.items + i;
824
825 if (!item->util)
826 continue;
827 strbuf_reset(&buf);
828 strbuf_addstr(&buf, item->string);
829 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old_name),
830 rename.new_name, strlen(rename.new_name));
831 strbuf_reset(&buf2);
832 strbuf_addstr(&buf2, item->util);
833 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old_name),
834 rename.new_name, strlen(rename.new_name));
835 strbuf_reset(&buf3);
836 strbuf_addf(&buf3, "remote: renamed %s to %s",
837 item->string, buf.buf);
838 if (create_symref(buf.buf, buf2.buf, buf3.buf))
839 die(_("creating '%s' failed"), buf.buf);
840 display_progress(progress, ++refs_renamed_nr);
841 }
842 stop_progress(&progress);
843 string_list_clear(&remote_branches, 1);
844
845 handle_push_default(rename.old_name, rename.new_name);
846
847 return 0;
848 }
849
850 static int rm(int argc, const char **argv, const char *prefix)
851 {
852 struct option options[] = {
853 OPT_END()
854 };
855 struct remote *remote;
856 struct strbuf buf = STRBUF_INIT;
857 struct known_remotes known_remotes = { NULL, NULL };
858 struct string_list branches = STRING_LIST_INIT_DUP;
859 struct string_list skipped = STRING_LIST_INIT_DUP;
860 struct branches_for_remote cb_data;
861 int i, result;
862
863 memset(&cb_data, 0, sizeof(cb_data));
864 cb_data.branches = &branches;
865 cb_data.skipped = &skipped;
866 cb_data.keep = &known_remotes;
867
868 argc = parse_options(argc, argv, prefix, options,
869 builtin_remote_rm_usage, 0);
870 if (argc != 1)
871 usage_with_options(builtin_remote_rm_usage, options);
872
873 remote = remote_get(argv[0]);
874 if (!remote_is_configured(remote, 1)) {
875 error(_("No such remote: '%s'"), argv[0]);
876 exit(2);
877 }
878
879 known_remotes.to_delete = remote;
880 for_each_remote(add_known_remote, &known_remotes);
881
882 read_branches();
883 for (i = 0; i < branch_list.nr; i++) {
884 struct string_list_item *item = branch_list.items + i;
885 struct branch_info *info = item->util;
886 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
887 const char *keys[] = { "remote", "merge", NULL }, **k;
888 for (k = keys; *k; k++) {
889 strbuf_reset(&buf);
890 strbuf_addf(&buf, "branch.%s.%s",
891 item->string, *k);
892 result = git_config_set_gently(buf.buf, NULL);
893 if (result && result != CONFIG_NOTHING_SET)
894 die(_("could not unset '%s'"), buf.buf);
895 }
896 }
897 if (info->push_remote_name && !strcmp(info->push_remote_name, remote->name)) {
898 strbuf_reset(&buf);
899 strbuf_addf(&buf, "branch.%s.pushremote", item->string);
900 result = git_config_set_gently(buf.buf, NULL);
901 if (result && result != CONFIG_NOTHING_SET)
902 die(_("could not unset '%s'"), buf.buf);
903 }
904 }
905
906 /*
907 * We cannot just pass a function to for_each_ref() which deletes
908 * the branches one by one, since for_each_ref() relies on cached
909 * refs, which are invalidated when deleting a branch.
910 */
911 cb_data.remote = remote;
912 result = for_each_ref(add_branch_for_removal, &cb_data);
913 strbuf_release(&buf);
914
915 if (!result)
916 result = delete_refs("remote: remove", &branches, REF_NO_DEREF);
917 string_list_clear(&branches, 0);
918
919 if (skipped.nr) {
920 fprintf_ln(stderr,
921 Q_("Note: A branch outside the refs/remotes/ hierarchy was not removed;\n"
922 "to delete it, use:",
923 "Note: Some branches outside the refs/remotes/ hierarchy were not removed;\n"
924 "to delete them, use:",
925 skipped.nr));
926 for (i = 0; i < skipped.nr; i++)
927 fprintf(stderr, " git branch -d %s\n",
928 skipped.items[i].string);
929 }
930 string_list_clear(&skipped, 0);
931
932 if (!result) {
933 strbuf_addf(&buf, "remote.%s", remote->name);
934 if (git_config_rename_section(buf.buf, NULL) < 1)
935 return error(_("Could not remove config section '%s'"), buf.buf);
936
937 handle_push_default(remote->name, NULL);
938 }
939
940 return result;
941 }
942
943 static void clear_push_info(void *util, const char *string)
944 {
945 struct push_info *info = util;
946 free(info->dest);
947 free(info);
948 }
949
950 static void free_remote_ref_states(struct ref_states *states)
951 {
952 string_list_clear(&states->new_refs, 0);
953 string_list_clear(&states->skipped, 0);
954 string_list_clear(&states->stale, 1);
955 string_list_clear(&states->tracked, 0);
956 string_list_clear(&states->heads, 0);
957 string_list_clear_func(&states->push, clear_push_info);
958 }
959
960 static int append_ref_to_tracked_list(const char *refname,
961 const struct object_id *oid UNUSED,
962 int flags, void *cb_data)
963 {
964 struct ref_states *states = cb_data;
965 struct refspec_item refspec;
966
967 if (flags & REF_ISSYMREF)
968 return 0;
969
970 memset(&refspec, 0, sizeof(refspec));
971 refspec.dst = (char *)refname;
972 if (!remote_find_tracking(states->remote, &refspec))
973 string_list_append(&states->tracked, abbrev_branch(refspec.src));
974
975 return 0;
976 }
977
978 static int get_remote_ref_states(const char *name,
979 struct ref_states *states,
980 int query)
981 {
982 states->remote = remote_get(name);
983 if (!states->remote)
984 return error(_("No such remote: '%s'"), name);
985
986 read_branches();
987
988 if (query) {
989 struct transport *transport;
990 const struct ref *remote_refs;
991
992 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
993 states->remote->url[0] : NULL);
994 remote_refs = transport_get_remote_refs(transport, NULL);
995
996 states->queried = 1;
997 if (query & GET_REF_STATES)
998 get_ref_states(remote_refs, states);
999 if (query & GET_HEAD_NAMES)
1000 get_head_names(remote_refs, states);
1001 if (query & GET_PUSH_REF_STATES)
1002 get_push_ref_states(remote_refs, states);
1003 transport_disconnect(transport);
1004 } else {
1005 for_each_ref(append_ref_to_tracked_list, states);
1006 string_list_sort(&states->tracked);
1007 get_push_ref_states_noquery(states);
1008 }
1009
1010 return 0;
1011 }
1012
1013 struct show_info {
1014 struct string_list list;
1015 struct ref_states states;
1016 int width, width2;
1017 int any_rebase;
1018 };
1019
1020 #define SHOW_INFO_INIT { \
1021 .list = STRING_LIST_INIT_DUP, \
1022 .states = REF_STATES_INIT, \
1023 }
1024
1025 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
1026 {
1027 struct show_info *info = cb_data;
1028 int n = strlen(item->string);
1029 if (n > info->width)
1030 info->width = n;
1031 string_list_insert(&info->list, item->string);
1032 return 0;
1033 }
1034
1035 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
1036 {
1037 struct show_info *info = cb_data;
1038 struct ref_states *states = &info->states;
1039 const char *name = item->string;
1040
1041 if (states->queried) {
1042 const char *fmt = "%s";
1043 const char *arg = "";
1044 if (string_list_has_string(&states->new_refs, name)) {
1045 fmt = _(" new (next fetch will store in remotes/%s)");
1046 arg = states->remote->name;
1047 } else if (string_list_has_string(&states->tracked, name))
1048 arg = _(" tracked");
1049 else if (string_list_has_string(&states->skipped, name))
1050 arg = _(" skipped");
1051 else if (string_list_has_string(&states->stale, name))
1052 arg = _(" stale (use 'git remote prune' to remove)");
1053 else
1054 arg = _(" ???");
1055 printf(" %-*s", info->width, name);
1056 printf(fmt, arg);
1057 printf("\n");
1058 } else
1059 printf(" %s\n", name);
1060
1061 return 0;
1062 }
1063
1064 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
1065 {
1066 struct show_info *show_info = cb_data;
1067 struct ref_states *states = &show_info->states;
1068 struct branch_info *branch_info = branch_item->util;
1069 struct string_list_item *item;
1070 int n;
1071
1072 if (!branch_info->merge.nr || !branch_info->remote_name ||
1073 strcmp(states->remote->name, branch_info->remote_name))
1074 return 0;
1075 if ((n = strlen(branch_item->string)) > show_info->width)
1076 show_info->width = n;
1077 if (branch_info->rebase >= REBASE_TRUE)
1078 show_info->any_rebase = 1;
1079
1080 item = string_list_insert(&show_info->list, branch_item->string);
1081 item->util = branch_info;
1082
1083 return 0;
1084 }
1085
1086 static int show_local_info_item(struct string_list_item *item, void *cb_data)
1087 {
1088 struct show_info *show_info = cb_data;
1089 struct branch_info *branch_info = item->util;
1090 struct string_list *merge = &branch_info->merge;
1091 int width = show_info->width + 4;
1092 int i;
1093
1094 if (branch_info->rebase >= REBASE_TRUE && branch_info->merge.nr > 1) {
1095 error(_("invalid branch.%s.merge; cannot rebase onto > 1 branch"),
1096 item->string);
1097 return 0;
1098 }
1099
1100 printf(" %-*s ", show_info->width, item->string);
1101 if (branch_info->rebase >= REBASE_TRUE) {
1102 const char *msg;
1103 if (branch_info->rebase == REBASE_INTERACTIVE)
1104 msg = _("rebases interactively onto remote %s");
1105 else if (branch_info->rebase == REBASE_MERGES)
1106 msg = _("rebases interactively (with merges) onto "
1107 "remote %s");
1108 else
1109 msg = _("rebases onto remote %s");
1110 printf_ln(msg, merge->items[0].string);
1111 return 0;
1112 } else if (show_info->any_rebase) {
1113 printf_ln(_(" merges with remote %s"), merge->items[0].string);
1114 width++;
1115 } else {
1116 printf_ln(_("merges with remote %s"), merge->items[0].string);
1117 }
1118 for (i = 1; i < merge->nr; i++)
1119 printf(_("%-*s and with remote %s\n"), width, "",
1120 merge->items[i].string);
1121
1122 return 0;
1123 }
1124
1125 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
1126 {
1127 struct show_info *show_info = cb_data;
1128 struct push_info *push_info = push_item->util;
1129 struct string_list_item *item;
1130 int n;
1131 if ((n = strlen(push_item->string)) > show_info->width)
1132 show_info->width = n;
1133 if ((n = strlen(push_info->dest)) > show_info->width2)
1134 show_info->width2 = n;
1135 item = string_list_append(&show_info->list, push_item->string);
1136 item->util = push_item->util;
1137 return 0;
1138 }
1139
1140 /*
1141 * Sorting comparison for a string list that has push_info
1142 * structs in its util field
1143 */
1144 static int cmp_string_with_push(const void *va, const void *vb)
1145 {
1146 const struct string_list_item *a = va;
1147 const struct string_list_item *b = vb;
1148 const struct push_info *a_push = a->util;
1149 const struct push_info *b_push = b->util;
1150 int cmp = strcmp(a->string, b->string);
1151 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
1152 }
1153
1154 static int show_push_info_item(struct string_list_item *item, void *cb_data)
1155 {
1156 struct show_info *show_info = cb_data;
1157 struct push_info *push_info = item->util;
1158 const char *src = item->string, *status = NULL;
1159
1160 switch (push_info->status) {
1161 case PUSH_STATUS_CREATE:
1162 status = _("create");
1163 break;
1164 case PUSH_STATUS_DELETE:
1165 status = _("delete");
1166 src = _("(none)");
1167 break;
1168 case PUSH_STATUS_UPTODATE:
1169 status = _("up to date");
1170 break;
1171 case PUSH_STATUS_FASTFORWARD:
1172 status = _("fast-forwardable");
1173 break;
1174 case PUSH_STATUS_OUTOFDATE:
1175 status = _("local out of date");
1176 break;
1177 case PUSH_STATUS_NOTQUERIED:
1178 break;
1179 }
1180 if (status) {
1181 if (push_info->forced)
1182 printf_ln(_(" %-*s forces to %-*s (%s)"), show_info->width, src,
1183 show_info->width2, push_info->dest, status);
1184 else
1185 printf_ln(_(" %-*s pushes to %-*s (%s)"), show_info->width, src,
1186 show_info->width2, push_info->dest, status);
1187 } else {
1188 if (push_info->forced)
1189 printf_ln(_(" %-*s forces to %s"), show_info->width, src,
1190 push_info->dest);
1191 else
1192 printf_ln(_(" %-*s pushes to %s"), show_info->width, src,
1193 push_info->dest);
1194 }
1195 return 0;
1196 }
1197
1198 static int get_one_entry(struct remote *remote, void *priv)
1199 {
1200 struct string_list *list = priv;
1201 struct strbuf remote_info_buf = STRBUF_INIT;
1202 const char **url;
1203 int i, url_nr;
1204
1205 if (remote->url_nr > 0) {
1206 struct strbuf promisor_config = STRBUF_INIT;
1207 const char *partial_clone_filter = NULL;
1208
1209 strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
1210 strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url[0]);
1211 if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
1212 strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
1213
1214 strbuf_release(&promisor_config);
1215 string_list_append(list, remote->name)->util =
1216 strbuf_detach(&remote_info_buf, NULL);
1217 } else
1218 string_list_append(list, remote->name)->util = NULL;
1219 if (remote->pushurl_nr) {
1220 url = remote->pushurl;
1221 url_nr = remote->pushurl_nr;
1222 } else {
1223 url = remote->url;
1224 url_nr = remote->url_nr;
1225 }
1226 for (i = 0; i < url_nr; i++)
1227 {
1228 strbuf_addf(&remote_info_buf, "%s (push)", url[i]);
1229 string_list_append(list, remote->name)->util =
1230 strbuf_detach(&remote_info_buf, NULL);
1231 }
1232
1233 return 0;
1234 }
1235
1236 static int show_all(void)
1237 {
1238 struct string_list list = STRING_LIST_INIT_DUP;
1239 int result;
1240
1241 result = for_each_remote(get_one_entry, &list);
1242
1243 if (!result) {
1244 int i;
1245
1246 string_list_sort(&list);
1247 for (i = 0; i < list.nr; i++) {
1248 struct string_list_item *item = list.items + i;
1249 if (verbose)
1250 printf("%s\t%s\n", item->string,
1251 item->util ? (const char *)item->util : "");
1252 else {
1253 if (i && !strcmp((item - 1)->string, item->string))
1254 continue;
1255 printf("%s\n", item->string);
1256 }
1257 }
1258 }
1259 string_list_clear(&list, 1);
1260 return result;
1261 }
1262
1263 static int show(int argc, const char **argv, const char *prefix)
1264 {
1265 int no_query = 0, result = 0, query_flag = 0;
1266 struct option options[] = {
1267 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1268 OPT_END()
1269 };
1270 struct show_info info = SHOW_INFO_INIT;
1271
1272 argc = parse_options(argc, argv, prefix, options,
1273 builtin_remote_show_usage,
1274 0);
1275
1276 if (argc < 1)
1277 return show_all();
1278
1279 if (!no_query)
1280 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1281
1282 for (; argc; argc--, argv++) {
1283 int i;
1284 const char **url;
1285 int url_nr;
1286
1287 get_remote_ref_states(*argv, &info.states, query_flag);
1288
1289 printf_ln(_("* remote %s"), *argv);
1290 printf_ln(_(" Fetch URL: %s"), info.states.remote->url_nr > 0 ?
1291 info.states.remote->url[0] : _("(no URL)"));
1292 if (info.states.remote->pushurl_nr) {
1293 url = info.states.remote->pushurl;
1294 url_nr = info.states.remote->pushurl_nr;
1295 } else {
1296 url = info.states.remote->url;
1297 url_nr = info.states.remote->url_nr;
1298 }
1299 for (i = 0; i < url_nr; i++)
1300 /*
1301 * TRANSLATORS: the colon ':' should align
1302 * with the one in " Fetch URL: %s"
1303 * translation.
1304 */
1305 printf_ln(_(" Push URL: %s"), url[i]);
1306 if (!i)
1307 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1308 if (no_query)
1309 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1310 else if (!info.states.heads.nr)
1311 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1312 else if (info.states.heads.nr == 1)
1313 printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string);
1314 else {
1315 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1316 " may be one of the following):\n"));
1317 for (i = 0; i < info.states.heads.nr; i++)
1318 printf(" %s\n", info.states.heads.items[i].string);
1319 }
1320
1321 /* remote branch info */
1322 info.width = 0;
1323 for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info);
1324 for_each_string_list(&info.states.skipped, add_remote_to_show_info, &info);
1325 for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info);
1326 for_each_string_list(&info.states.stale, add_remote_to_show_info, &info);
1327 if (info.list.nr)
1328 printf_ln(Q_(" Remote branch:%s",
1329 " Remote branches:%s",
1330 info.list.nr),
1331 no_query ? _(" (status not queried)") : "");
1332 for_each_string_list(&info.list, show_remote_info_item, &info);
1333 string_list_clear(&info.list, 0);
1334
1335 /* git pull info */
1336 info.width = 0;
1337 info.any_rebase = 0;
1338 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1339 if (info.list.nr)
1340 printf_ln(Q_(" Local branch configured for 'git pull':",
1341 " Local branches configured for 'git pull':",
1342 info.list.nr));
1343 for_each_string_list(&info.list, show_local_info_item, &info);
1344 string_list_clear(&info.list, 0);
1345
1346 /* git push info */
1347 if (info.states.remote->mirror)
1348 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1349
1350 info.width = info.width2 = 0;
1351 for_each_string_list(&info.states.push, add_push_to_show_info, &info);
1352 QSORT(info.list.items, info.list.nr, cmp_string_with_push);
1353 if (info.list.nr)
1354 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1355 " Local refs configured for 'git push'%s:",
1356 info.list.nr),
1357 no_query ? _(" (status not queried)") : "");
1358 for_each_string_list(&info.list, show_push_info_item, &info);
1359 string_list_clear(&info.list, 0);
1360
1361 free_remote_ref_states(&info.states);
1362 }
1363
1364 return result;
1365 }
1366
1367 static int set_head(int argc, const char **argv, const char *prefix)
1368 {
1369 int i, opt_a = 0, opt_d = 0, result = 0;
1370 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1371 char *head_name = NULL;
1372
1373 struct option options[] = {
1374 OPT_BOOL('a', "auto", &opt_a,
1375 N_("set refs/remotes/<name>/HEAD according to remote")),
1376 OPT_BOOL('d', "delete", &opt_d,
1377 N_("delete refs/remotes/<name>/HEAD")),
1378 OPT_END()
1379 };
1380 argc = parse_options(argc, argv, prefix, options,
1381 builtin_remote_sethead_usage, 0);
1382 if (argc)
1383 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1384
1385 if (!opt_a && !opt_d && argc == 2) {
1386 head_name = xstrdup(argv[1]);
1387 } else if (opt_a && !opt_d && argc == 1) {
1388 struct ref_states states = REF_STATES_INIT;
1389 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1390 if (!states.heads.nr)
1391 result |= error(_("Cannot determine remote HEAD"));
1392 else if (states.heads.nr > 1) {
1393 result |= error(_("Multiple remote HEAD branches. "
1394 "Please choose one explicitly with:"));
1395 for (i = 0; i < states.heads.nr; i++)
1396 fprintf(stderr, " git remote set-head %s %s\n",
1397 argv[0], states.heads.items[i].string);
1398 } else
1399 head_name = xstrdup(states.heads.items[0].string);
1400 free_remote_ref_states(&states);
1401 } else if (opt_d && !opt_a && argc == 1) {
1402 if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF))
1403 result |= error(_("Could not delete %s"), buf.buf);
1404 } else
1405 usage_with_options(builtin_remote_sethead_usage, options);
1406
1407 if (head_name) {
1408 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1409 /* make sure it's valid */
1410 if (!ref_exists(buf2.buf))
1411 result |= error(_("Not a valid ref: %s"), buf2.buf);
1412 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1413 result |= error(_("Could not setup %s"), buf.buf);
1414 else if (opt_a)
1415 printf("%s/HEAD set to %s\n", argv[0], head_name);
1416 free(head_name);
1417 }
1418
1419 strbuf_release(&buf);
1420 strbuf_release(&buf2);
1421 return result;
1422 }
1423
1424 static int prune_remote(const char *remote, int dry_run)
1425 {
1426 int result = 0;
1427 struct ref_states states = REF_STATES_INIT;
1428 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1429 struct string_list_item *item;
1430 const char *dangling_msg = dry_run
1431 ? _(" %s will become dangling!")
1432 : _(" %s has become dangling!");
1433
1434 get_remote_ref_states(remote, &states, GET_REF_STATES);
1435
1436 if (!states.stale.nr) {
1437 free_remote_ref_states(&states);
1438 return 0;
1439 }
1440
1441 printf_ln(_("Pruning %s"), remote);
1442 printf_ln(_("URL: %s"),
1443 states.remote->url_nr
1444 ? states.remote->url[0]
1445 : _("(no URL)"));
1446
1447 for_each_string_list_item(item, &states.stale)
1448 string_list_append(&refs_to_prune, item->util);
1449 string_list_sort(&refs_to_prune);
1450
1451 if (!dry_run)
1452 result |= delete_refs("remote: prune", &refs_to_prune, 0);
1453
1454 for_each_string_list_item(item, &states.stale) {
1455 const char *refname = item->util;
1456
1457 if (dry_run)
1458 printf_ln(_(" * [would prune] %s"),
1459 abbrev_ref(refname, "refs/remotes/"));
1460 else
1461 printf_ln(_(" * [pruned] %s"),
1462 abbrev_ref(refname, "refs/remotes/"));
1463 }
1464
1465 warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
1466
1467 string_list_clear(&refs_to_prune, 0);
1468 free_remote_ref_states(&states);
1469 return result;
1470 }
1471
1472 static int prune(int argc, const char **argv, const char *prefix)
1473 {
1474 int dry_run = 0, result = 0;
1475 struct option options[] = {
1476 OPT__DRY_RUN(&dry_run, N_("dry run")),
1477 OPT_END()
1478 };
1479
1480 argc = parse_options(argc, argv, prefix, options,
1481 builtin_remote_prune_usage, 0);
1482
1483 if (argc < 1)
1484 usage_with_options(builtin_remote_prune_usage, options);
1485
1486 for (; argc; argc--, argv++)
1487 result |= prune_remote(*argv, dry_run);
1488
1489 return result;
1490 }
1491
1492 static int get_remote_default(const char *key, const char *value UNUSED, void *priv)
1493 {
1494 if (strcmp(key, "remotes.default") == 0) {
1495 int *found = priv;
1496 *found = 1;
1497 }
1498 return 0;
1499 }
1500
1501 static int update(int argc, const char **argv, const char *prefix)
1502 {
1503 int i, prune = -1;
1504 struct option options[] = {
1505 OPT_BOOL('p', "prune", &prune,
1506 N_("prune remotes after fetching")),
1507 OPT_END()
1508 };
1509 struct strvec fetch_argv = STRVEC_INIT;
1510 int default_defined = 0;
1511 int retval;
1512
1513 argc = parse_options(argc, argv, prefix, options,
1514 builtin_remote_update_usage,
1515 PARSE_OPT_KEEP_ARGV0);
1516
1517 strvec_push(&fetch_argv, "fetch");
1518
1519 if (prune != -1)
1520 strvec_push(&fetch_argv, prune ? "--prune" : "--no-prune");
1521 if (verbose)
1522 strvec_push(&fetch_argv, "-v");
1523 strvec_push(&fetch_argv, "--multiple");
1524 if (argc < 2)
1525 strvec_push(&fetch_argv, "default");
1526 for (i = 1; i < argc; i++)
1527 strvec_push(&fetch_argv, argv[i]);
1528
1529 if (strcmp(fetch_argv.v[fetch_argv.nr-1], "default") == 0) {
1530 git_config(get_remote_default, &default_defined);
1531 if (!default_defined) {
1532 strvec_pop(&fetch_argv);
1533 strvec_push(&fetch_argv, "--all");
1534 }
1535 }
1536
1537 retval = run_command_v_opt(fetch_argv.v, RUN_GIT_CMD);
1538 strvec_clear(&fetch_argv);
1539 return retval;
1540 }
1541
1542 static int remove_all_fetch_refspecs(const char *key)
1543 {
1544 return git_config_set_multivar_gently(key, NULL, NULL,
1545 CONFIG_FLAGS_MULTI_REPLACE);
1546 }
1547
1548 static void add_branches(struct remote *remote, const char **branches,
1549 const char *key)
1550 {
1551 const char *remotename = remote->name;
1552 int mirror = remote->mirror;
1553 struct strbuf refspec = STRBUF_INIT;
1554
1555 for (; *branches; branches++)
1556 add_branch(key, *branches, remotename, mirror, &refspec);
1557
1558 strbuf_release(&refspec);
1559 }
1560
1561 static int set_remote_branches(const char *remotename, const char **branches,
1562 int add_mode)
1563 {
1564 struct strbuf key = STRBUF_INIT;
1565 struct remote *remote;
1566
1567 strbuf_addf(&key, "remote.%s.fetch", remotename);
1568
1569 remote = remote_get(remotename);
1570 if (!remote_is_configured(remote, 1)) {
1571 error(_("No such remote '%s'"), remotename);
1572 exit(2);
1573 }
1574
1575 if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
1576 strbuf_release(&key);
1577 return 1;
1578 }
1579 add_branches(remote, branches, key.buf);
1580
1581 strbuf_release(&key);
1582 return 0;
1583 }
1584
1585 static int set_branches(int argc, const char **argv, const char *prefix)
1586 {
1587 int add_mode = 0;
1588 struct option options[] = {
1589 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1590 OPT_END()
1591 };
1592
1593 argc = parse_options(argc, argv, prefix, options,
1594 builtin_remote_setbranches_usage, 0);
1595 if (argc == 0) {
1596 error(_("no remote specified"));
1597 usage_with_options(builtin_remote_setbranches_usage, options);
1598 }
1599 argv[argc] = NULL;
1600
1601 return set_remote_branches(argv[0], argv + 1, add_mode);
1602 }
1603
1604 static int get_url(int argc, const char **argv, const char *prefix)
1605 {
1606 int i, push_mode = 0, all_mode = 0;
1607 const char *remotename = NULL;
1608 struct remote *remote;
1609 const char **url;
1610 int url_nr;
1611 struct option options[] = {
1612 OPT_BOOL('\0', "push", &push_mode,
1613 N_("query push URLs rather than fetch URLs")),
1614 OPT_BOOL('\0', "all", &all_mode,
1615 N_("return all URLs")),
1616 OPT_END()
1617 };
1618 argc = parse_options(argc, argv, prefix, options,
1619 builtin_remote_geturl_usage, 0);
1620
1621 if (argc != 1)
1622 usage_with_options(builtin_remote_geturl_usage, options);
1623
1624 remotename = argv[0];
1625
1626 remote = remote_get(remotename);
1627 if (!remote_is_configured(remote, 1)) {
1628 error(_("No such remote '%s'"), remotename);
1629 exit(2);
1630 }
1631
1632 url_nr = 0;
1633 if (push_mode) {
1634 url = remote->pushurl;
1635 url_nr = remote->pushurl_nr;
1636 }
1637 /* else fetch mode */
1638
1639 /* Use the fetch URL when no push URLs were found or requested. */
1640 if (!url_nr) {
1641 url = remote->url;
1642 url_nr = remote->url_nr;
1643 }
1644
1645 if (!url_nr)
1646 die(_("no URLs configured for remote '%s'"), remotename);
1647
1648 if (all_mode) {
1649 for (i = 0; i < url_nr; i++)
1650 printf_ln("%s", url[i]);
1651 } else {
1652 printf_ln("%s", *url);
1653 }
1654
1655 return 0;
1656 }
1657
1658 static int set_url(int argc, const char **argv, const char *prefix)
1659 {
1660 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1661 int matches = 0, negative_matches = 0;
1662 const char *remotename = NULL;
1663 const char *newurl = NULL;
1664 const char *oldurl = NULL;
1665 struct remote *remote;
1666 regex_t old_regex;
1667 const char **urlset;
1668 int urlset_nr;
1669 struct strbuf name_buf = STRBUF_INIT;
1670 struct option options[] = {
1671 OPT_BOOL('\0', "push", &push_mode,
1672 N_("manipulate push URLs")),
1673 OPT_BOOL('\0', "add", &add_mode,
1674 N_("add URL")),
1675 OPT_BOOL('\0', "delete", &delete_mode,
1676 N_("delete URLs")),
1677 OPT_END()
1678 };
1679 argc = parse_options(argc, argv, prefix, options,
1680 builtin_remote_seturl_usage,
1681 PARSE_OPT_KEEP_ARGV0);
1682
1683 if (add_mode && delete_mode)
1684 die(_("--add --delete doesn't make sense"));
1685
1686 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1687 usage_with_options(builtin_remote_seturl_usage, options);
1688
1689 remotename = argv[1];
1690 newurl = argv[2];
1691 if (argc > 3)
1692 oldurl = argv[3];
1693
1694 if (delete_mode)
1695 oldurl = newurl;
1696
1697 remote = remote_get(remotename);
1698 if (!remote_is_configured(remote, 1)) {
1699 error(_("No such remote '%s'"), remotename);
1700 exit(2);
1701 }
1702
1703 if (push_mode) {
1704 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1705 urlset = remote->pushurl;
1706 urlset_nr = remote->pushurl_nr;
1707 } else {
1708 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1709 urlset = remote->url;
1710 urlset_nr = remote->url_nr;
1711 }
1712
1713 /* Special cases that add new entry. */
1714 if ((!oldurl && !delete_mode) || add_mode) {
1715 if (add_mode)
1716 git_config_set_multivar(name_buf.buf, newurl,
1717 "^$", 0);
1718 else
1719 git_config_set(name_buf.buf, newurl);
1720 goto out;
1721 }
1722
1723 /* Old URL specified. Demand that one matches. */
1724 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1725 die(_("Invalid old URL pattern: %s"), oldurl);
1726
1727 for (i = 0; i < urlset_nr; i++)
1728 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1729 matches++;
1730 else
1731 negative_matches++;
1732 if (!delete_mode && !matches)
1733 die(_("No such URL found: %s"), oldurl);
1734 if (delete_mode && !negative_matches && !push_mode)
1735 die(_("Will not delete all non-push URLs"));
1736
1737 regfree(&old_regex);
1738
1739 if (!delete_mode)
1740 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1741 else
1742 git_config_set_multivar(name_buf.buf, NULL, oldurl,
1743 CONFIG_FLAGS_MULTI_REPLACE);
1744 out:
1745 strbuf_release(&name_buf);
1746 return 0;
1747 }
1748
1749 int cmd_remote(int argc, const char **argv, const char *prefix)
1750 {
1751 parse_opt_subcommand_fn *fn = NULL;
1752 struct option options[] = {
1753 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1754 OPT_SUBCOMMAND("add", &fn, add),
1755 OPT_SUBCOMMAND("rename", &fn, mv),
1756 OPT_SUBCOMMAND_F("rm", &fn, rm, PARSE_OPT_NOCOMPLETE),
1757 OPT_SUBCOMMAND("remove", &fn, rm),
1758 OPT_SUBCOMMAND("set-head", &fn, set_head),
1759 OPT_SUBCOMMAND("set-branches", &fn, set_branches),
1760 OPT_SUBCOMMAND("get-url", &fn, get_url),
1761 OPT_SUBCOMMAND("set-url", &fn, set_url),
1762 OPT_SUBCOMMAND("show", &fn, show),
1763 OPT_SUBCOMMAND("prune", &fn, prune),
1764 OPT_SUBCOMMAND("update", &fn, update),
1765 OPT_END()
1766 };
1767
1768 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1769 PARSE_OPT_SUBCOMMAND_OPTIONAL);
1770
1771 if (fn) {
1772 return !!fn(argc, argv, prefix);
1773 } else {
1774 if (argc) {
1775 error(_("unknown subcommand: `%s'"), argv[0]);
1776 usage_with_options(builtin_remote_usage, options);
1777 }
1778 return !!show_all();
1779 }
1780 }