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