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