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