]> git.ipfire.org Git - thirdparty/git.git/blob - builtin/remote.c
Sync with Git 2.36.2
[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 remote_info_buf = STRBUF_INIT;
1189 const char **url;
1190 int i, url_nr;
1191
1192 if (remote->url_nr > 0) {
1193 struct strbuf promisor_config = STRBUF_INIT;
1194 const char *partial_clone_filter = NULL;
1195
1196 strbuf_addf(&promisor_config, "remote.%s.partialclonefilter", remote->name);
1197 strbuf_addf(&remote_info_buf, "%s (fetch)", remote->url[0]);
1198 if (!git_config_get_string_tmp(promisor_config.buf, &partial_clone_filter))
1199 strbuf_addf(&remote_info_buf, " [%s]", partial_clone_filter);
1200
1201 strbuf_release(&promisor_config);
1202 string_list_append(list, remote->name)->util =
1203 strbuf_detach(&remote_info_buf, NULL);
1204 } else
1205 string_list_append(list, remote->name)->util = NULL;
1206 if (remote->pushurl_nr) {
1207 url = remote->pushurl;
1208 url_nr = remote->pushurl_nr;
1209 } else {
1210 url = remote->url;
1211 url_nr = remote->url_nr;
1212 }
1213 for (i = 0; i < url_nr; i++)
1214 {
1215 strbuf_addf(&remote_info_buf, "%s (push)", url[i]);
1216 string_list_append(list, remote->name)->util =
1217 strbuf_detach(&remote_info_buf, NULL);
1218 }
1219
1220 return 0;
1221 }
1222
1223 static int show_all(void)
1224 {
1225 struct string_list list = STRING_LIST_INIT_NODUP;
1226 int result;
1227
1228 list.strdup_strings = 1;
1229 result = for_each_remote(get_one_entry, &list);
1230
1231 if (!result) {
1232 int i;
1233
1234 string_list_sort(&list);
1235 for (i = 0; i < list.nr; i++) {
1236 struct string_list_item *item = list.items + i;
1237 if (verbose)
1238 printf("%s\t%s\n", item->string,
1239 item->util ? (const char *)item->util : "");
1240 else {
1241 if (i && !strcmp((item - 1)->string, item->string))
1242 continue;
1243 printf("%s\n", item->string);
1244 }
1245 }
1246 }
1247 string_list_clear(&list, 1);
1248 return result;
1249 }
1250
1251 static int show(int argc, const char **argv)
1252 {
1253 int no_query = 0, result = 0, query_flag = 0;
1254 struct option options[] = {
1255 OPT_BOOL('n', NULL, &no_query, N_("do not query remotes")),
1256 OPT_END()
1257 };
1258 struct show_info info = SHOW_INFO_INIT;
1259
1260 argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1261 0);
1262
1263 if (argc < 1)
1264 return show_all();
1265
1266 if (!no_query)
1267 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1268
1269 for (; argc; argc--, argv++) {
1270 int i;
1271 const char **url;
1272 int url_nr;
1273
1274 get_remote_ref_states(*argv, &info.states, query_flag);
1275
1276 printf_ln(_("* remote %s"), *argv);
1277 printf_ln(_(" Fetch URL: %s"), info.states.remote->url_nr > 0 ?
1278 info.states.remote->url[0] : _("(no URL)"));
1279 if (info.states.remote->pushurl_nr) {
1280 url = info.states.remote->pushurl;
1281 url_nr = info.states.remote->pushurl_nr;
1282 } else {
1283 url = info.states.remote->url;
1284 url_nr = info.states.remote->url_nr;
1285 }
1286 for (i = 0; i < url_nr; i++)
1287 /*
1288 * TRANSLATORS: the colon ':' should align
1289 * with the one in " Fetch URL: %s"
1290 * translation.
1291 */
1292 printf_ln(_(" Push URL: %s"), url[i]);
1293 if (!i)
1294 printf_ln(_(" Push URL: %s"), _("(no URL)"));
1295 if (no_query)
1296 printf_ln(_(" HEAD branch: %s"), _("(not queried)"));
1297 else if (!info.states.heads.nr)
1298 printf_ln(_(" HEAD branch: %s"), _("(unknown)"));
1299 else if (info.states.heads.nr == 1)
1300 printf_ln(_(" HEAD branch: %s"), info.states.heads.items[0].string);
1301 else {
1302 printf(_(" HEAD branch (remote HEAD is ambiguous,"
1303 " may be one of the following):\n"));
1304 for (i = 0; i < info.states.heads.nr; i++)
1305 printf(" %s\n", info.states.heads.items[i].string);
1306 }
1307
1308 /* remote branch info */
1309 info.width = 0;
1310 for_each_string_list(&info.states.new_refs, add_remote_to_show_info, &info);
1311 for_each_string_list(&info.states.tracked, add_remote_to_show_info, &info);
1312 for_each_string_list(&info.states.stale, add_remote_to_show_info, &info);
1313 if (info.list.nr)
1314 printf_ln(Q_(" Remote branch:%s",
1315 " Remote branches:%s",
1316 info.list.nr),
1317 no_query ? _(" (status not queried)") : "");
1318 for_each_string_list(&info.list, show_remote_info_item, &info);
1319 string_list_clear(&info.list, 0);
1320
1321 /* git pull info */
1322 info.width = 0;
1323 info.any_rebase = 0;
1324 for_each_string_list(&branch_list, add_local_to_show_info, &info);
1325 if (info.list.nr)
1326 printf_ln(Q_(" Local branch configured for 'git pull':",
1327 " Local branches configured for 'git pull':",
1328 info.list.nr));
1329 for_each_string_list(&info.list, show_local_info_item, &info);
1330 string_list_clear(&info.list, 0);
1331
1332 /* git push info */
1333 if (info.states.remote->mirror)
1334 printf_ln(_(" Local refs will be mirrored by 'git push'"));
1335
1336 info.width = info.width2 = 0;
1337 for_each_string_list(&info.states.push, add_push_to_show_info, &info);
1338 QSORT(info.list.items, info.list.nr, cmp_string_with_push);
1339 if (info.list.nr)
1340 printf_ln(Q_(" Local ref configured for 'git push'%s:",
1341 " Local refs configured for 'git push'%s:",
1342 info.list.nr),
1343 no_query ? _(" (status not queried)") : "");
1344 for_each_string_list(&info.list, show_push_info_item, &info);
1345 string_list_clear(&info.list, 0);
1346
1347 free_remote_ref_states(&info.states);
1348 }
1349
1350 return result;
1351 }
1352
1353 static int set_head(int argc, const char **argv)
1354 {
1355 int i, opt_a = 0, opt_d = 0, result = 0;
1356 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1357 char *head_name = NULL;
1358
1359 struct option options[] = {
1360 OPT_BOOL('a', "auto", &opt_a,
1361 N_("set refs/remotes/<name>/HEAD according to remote")),
1362 OPT_BOOL('d', "delete", &opt_d,
1363 N_("delete refs/remotes/<name>/HEAD")),
1364 OPT_END()
1365 };
1366 argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1367 0);
1368 if (argc)
1369 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1370
1371 if (!opt_a && !opt_d && argc == 2) {
1372 head_name = xstrdup(argv[1]);
1373 } else if (opt_a && !opt_d && argc == 1) {
1374 struct ref_states states = REF_STATES_INIT;
1375 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1376 if (!states.heads.nr)
1377 result |= error(_("Cannot determine remote HEAD"));
1378 else if (states.heads.nr > 1) {
1379 result |= error(_("Multiple remote HEAD branches. "
1380 "Please choose one explicitly with:"));
1381 for (i = 0; i < states.heads.nr; i++)
1382 fprintf(stderr, " git remote set-head %s %s\n",
1383 argv[0], states.heads.items[i].string);
1384 } else
1385 head_name = xstrdup(states.heads.items[0].string);
1386 free_remote_ref_states(&states);
1387 } else if (opt_d && !opt_a && argc == 1) {
1388 if (delete_ref(NULL, buf.buf, NULL, REF_NO_DEREF))
1389 result |= error(_("Could not delete %s"), buf.buf);
1390 } else
1391 usage_with_options(builtin_remote_sethead_usage, options);
1392
1393 if (head_name) {
1394 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1395 /* make sure it's valid */
1396 if (!ref_exists(buf2.buf))
1397 result |= error(_("Not a valid ref: %s"), buf2.buf);
1398 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1399 result |= error(_("Could not setup %s"), buf.buf);
1400 else if (opt_a)
1401 printf("%s/HEAD set to %s\n", argv[0], head_name);
1402 free(head_name);
1403 }
1404
1405 strbuf_release(&buf);
1406 strbuf_release(&buf2);
1407 return result;
1408 }
1409
1410 static int prune_remote(const char *remote, int dry_run)
1411 {
1412 int result = 0;
1413 struct ref_states states = REF_STATES_INIT;
1414 struct string_list refs_to_prune = STRING_LIST_INIT_NODUP;
1415 struct string_list_item *item;
1416 const char *dangling_msg = dry_run
1417 ? _(" %s will become dangling!")
1418 : _(" %s has become dangling!");
1419
1420 get_remote_ref_states(remote, &states, GET_REF_STATES);
1421
1422 if (!states.stale.nr) {
1423 free_remote_ref_states(&states);
1424 return 0;
1425 }
1426
1427 printf_ln(_("Pruning %s"), remote);
1428 printf_ln(_("URL: %s"),
1429 states.remote->url_nr
1430 ? states.remote->url[0]
1431 : _("(no URL)"));
1432
1433 for_each_string_list_item(item, &states.stale)
1434 string_list_append(&refs_to_prune, item->util);
1435 string_list_sort(&refs_to_prune);
1436
1437 if (!dry_run)
1438 result |= delete_refs("remote: prune", &refs_to_prune, 0);
1439
1440 for_each_string_list_item(item, &states.stale) {
1441 const char *refname = item->util;
1442
1443 if (dry_run)
1444 printf_ln(_(" * [would prune] %s"),
1445 abbrev_ref(refname, "refs/remotes/"));
1446 else
1447 printf_ln(_(" * [pruned] %s"),
1448 abbrev_ref(refname, "refs/remotes/"));
1449 }
1450
1451 warn_dangling_symrefs(stdout, dangling_msg, &refs_to_prune);
1452
1453 string_list_clear(&refs_to_prune, 0);
1454 free_remote_ref_states(&states);
1455 return result;
1456 }
1457
1458 static int prune(int argc, const char **argv)
1459 {
1460 int dry_run = 0, result = 0;
1461 struct option options[] = {
1462 OPT__DRY_RUN(&dry_run, N_("dry run")),
1463 OPT_END()
1464 };
1465
1466 argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1467 0);
1468
1469 if (argc < 1)
1470 usage_with_options(builtin_remote_prune_usage, options);
1471
1472 for (; argc; argc--, argv++)
1473 result |= prune_remote(*argv, dry_run);
1474
1475 return result;
1476 }
1477
1478 static int get_remote_default(const char *key, const char *value, void *priv)
1479 {
1480 if (strcmp(key, "remotes.default") == 0) {
1481 int *found = priv;
1482 *found = 1;
1483 }
1484 return 0;
1485 }
1486
1487 static int update(int argc, const char **argv)
1488 {
1489 int i, prune = -1;
1490 struct option options[] = {
1491 OPT_BOOL('p', "prune", &prune,
1492 N_("prune remotes after fetching")),
1493 OPT_END()
1494 };
1495 struct strvec fetch_argv = STRVEC_INIT;
1496 int default_defined = 0;
1497 int retval;
1498
1499 argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1500 PARSE_OPT_KEEP_ARGV0);
1501
1502 strvec_push(&fetch_argv, "fetch");
1503
1504 if (prune != -1)
1505 strvec_push(&fetch_argv, prune ? "--prune" : "--no-prune");
1506 if (verbose)
1507 strvec_push(&fetch_argv, "-v");
1508 strvec_push(&fetch_argv, "--multiple");
1509 if (argc < 2)
1510 strvec_push(&fetch_argv, "default");
1511 for (i = 1; i < argc; i++)
1512 strvec_push(&fetch_argv, argv[i]);
1513
1514 if (strcmp(fetch_argv.v[fetch_argv.nr-1], "default") == 0) {
1515 git_config(get_remote_default, &default_defined);
1516 if (!default_defined) {
1517 strvec_pop(&fetch_argv);
1518 strvec_push(&fetch_argv, "--all");
1519 }
1520 }
1521
1522 retval = run_command_v_opt(fetch_argv.v, RUN_GIT_CMD);
1523 strvec_clear(&fetch_argv);
1524 return retval;
1525 }
1526
1527 static int remove_all_fetch_refspecs(const char *key)
1528 {
1529 return git_config_set_multivar_gently(key, NULL, NULL,
1530 CONFIG_FLAGS_MULTI_REPLACE);
1531 }
1532
1533 static void add_branches(struct remote *remote, const char **branches,
1534 const char *key)
1535 {
1536 const char *remotename = remote->name;
1537 int mirror = remote->mirror;
1538 struct strbuf refspec = STRBUF_INIT;
1539
1540 for (; *branches; branches++)
1541 add_branch(key, *branches, remotename, mirror, &refspec);
1542
1543 strbuf_release(&refspec);
1544 }
1545
1546 static int set_remote_branches(const char *remotename, const char **branches,
1547 int add_mode)
1548 {
1549 struct strbuf key = STRBUF_INIT;
1550 struct remote *remote;
1551
1552 strbuf_addf(&key, "remote.%s.fetch", remotename);
1553
1554 remote = remote_get(remotename);
1555 if (!remote_is_configured(remote, 1)) {
1556 error(_("No such remote '%s'"), remotename);
1557 exit(2);
1558 }
1559
1560 if (!add_mode && remove_all_fetch_refspecs(key.buf)) {
1561 strbuf_release(&key);
1562 return 1;
1563 }
1564 add_branches(remote, branches, key.buf);
1565
1566 strbuf_release(&key);
1567 return 0;
1568 }
1569
1570 static int set_branches(int argc, const char **argv)
1571 {
1572 int add_mode = 0;
1573 struct option options[] = {
1574 OPT_BOOL('\0', "add", &add_mode, N_("add branch")),
1575 OPT_END()
1576 };
1577
1578 argc = parse_options(argc, argv, NULL, options,
1579 builtin_remote_setbranches_usage, 0);
1580 if (argc == 0) {
1581 error(_("no remote specified"));
1582 usage_with_options(builtin_remote_setbranches_usage, options);
1583 }
1584 argv[argc] = NULL;
1585
1586 return set_remote_branches(argv[0], argv + 1, add_mode);
1587 }
1588
1589 static int get_url(int argc, const char **argv)
1590 {
1591 int i, push_mode = 0, all_mode = 0;
1592 const char *remotename = NULL;
1593 struct remote *remote;
1594 const char **url;
1595 int url_nr;
1596 struct option options[] = {
1597 OPT_BOOL('\0', "push", &push_mode,
1598 N_("query push URLs rather than fetch URLs")),
1599 OPT_BOOL('\0', "all", &all_mode,
1600 N_("return all URLs")),
1601 OPT_END()
1602 };
1603 argc = parse_options(argc, argv, NULL, options, builtin_remote_geturl_usage, 0);
1604
1605 if (argc != 1)
1606 usage_with_options(builtin_remote_geturl_usage, options);
1607
1608 remotename = argv[0];
1609
1610 remote = remote_get(remotename);
1611 if (!remote_is_configured(remote, 1)) {
1612 error(_("No such remote '%s'"), remotename);
1613 exit(2);
1614 }
1615
1616 url_nr = 0;
1617 if (push_mode) {
1618 url = remote->pushurl;
1619 url_nr = remote->pushurl_nr;
1620 }
1621 /* else fetch mode */
1622
1623 /* Use the fetch URL when no push URLs were found or requested. */
1624 if (!url_nr) {
1625 url = remote->url;
1626 url_nr = remote->url_nr;
1627 }
1628
1629 if (!url_nr)
1630 die(_("no URLs configured for remote '%s'"), remotename);
1631
1632 if (all_mode) {
1633 for (i = 0; i < url_nr; i++)
1634 printf_ln("%s", url[i]);
1635 } else {
1636 printf_ln("%s", *url);
1637 }
1638
1639 return 0;
1640 }
1641
1642 static int set_url(int argc, const char **argv)
1643 {
1644 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1645 int matches = 0, negative_matches = 0;
1646 const char *remotename = NULL;
1647 const char *newurl = NULL;
1648 const char *oldurl = NULL;
1649 struct remote *remote;
1650 regex_t old_regex;
1651 const char **urlset;
1652 int urlset_nr;
1653 struct strbuf name_buf = STRBUF_INIT;
1654 struct option options[] = {
1655 OPT_BOOL('\0', "push", &push_mode,
1656 N_("manipulate push URLs")),
1657 OPT_BOOL('\0', "add", &add_mode,
1658 N_("add URL")),
1659 OPT_BOOL('\0', "delete", &delete_mode,
1660 N_("delete URLs")),
1661 OPT_END()
1662 };
1663 argc = parse_options(argc, argv, NULL, options, builtin_remote_seturl_usage,
1664 PARSE_OPT_KEEP_ARGV0);
1665
1666 if (add_mode && delete_mode)
1667 die(_("--add --delete doesn't make sense"));
1668
1669 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1670 usage_with_options(builtin_remote_seturl_usage, options);
1671
1672 remotename = argv[1];
1673 newurl = argv[2];
1674 if (argc > 3)
1675 oldurl = argv[3];
1676
1677 if (delete_mode)
1678 oldurl = newurl;
1679
1680 remote = remote_get(remotename);
1681 if (!remote_is_configured(remote, 1)) {
1682 error(_("No such remote '%s'"), remotename);
1683 exit(2);
1684 }
1685
1686 if (push_mode) {
1687 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1688 urlset = remote->pushurl;
1689 urlset_nr = remote->pushurl_nr;
1690 } else {
1691 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1692 urlset = remote->url;
1693 urlset_nr = remote->url_nr;
1694 }
1695
1696 /* Special cases that add new entry. */
1697 if ((!oldurl && !delete_mode) || add_mode) {
1698 if (add_mode)
1699 git_config_set_multivar(name_buf.buf, newurl,
1700 "^$", 0);
1701 else
1702 git_config_set(name_buf.buf, newurl);
1703 goto out;
1704 }
1705
1706 /* Old URL specified. Demand that one matches. */
1707 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1708 die(_("Invalid old URL pattern: %s"), oldurl);
1709
1710 for (i = 0; i < urlset_nr; i++)
1711 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1712 matches++;
1713 else
1714 negative_matches++;
1715 if (!delete_mode && !matches)
1716 die(_("No such URL found: %s"), oldurl);
1717 if (delete_mode && !negative_matches && !push_mode)
1718 die(_("Will not delete all non-push URLs"));
1719
1720 regfree(&old_regex);
1721
1722 if (!delete_mode)
1723 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1724 else
1725 git_config_set_multivar(name_buf.buf, NULL, oldurl,
1726 CONFIG_FLAGS_MULTI_REPLACE);
1727 out:
1728 strbuf_release(&name_buf);
1729 return 0;
1730 }
1731
1732 int cmd_remote(int argc, const char **argv, const char *prefix)
1733 {
1734 struct option options[] = {
1735 OPT__VERBOSE(&verbose, N_("be verbose; must be placed before a subcommand")),
1736 OPT_END()
1737 };
1738 int result;
1739
1740 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1741 PARSE_OPT_STOP_AT_NON_OPTION);
1742
1743 if (argc < 1)
1744 result = show_all();
1745 else if (!strcmp(argv[0], "add"))
1746 result = add(argc, argv);
1747 else if (!strcmp(argv[0], "rename"))
1748 result = mv(argc, argv);
1749 else if (!strcmp(argv[0], "rm") || !strcmp(argv[0], "remove"))
1750 result = rm(argc, argv);
1751 else if (!strcmp(argv[0], "set-head"))
1752 result = set_head(argc, argv);
1753 else if (!strcmp(argv[0], "set-branches"))
1754 result = set_branches(argc, argv);
1755 else if (!strcmp(argv[0], "get-url"))
1756 result = get_url(argc, argv);
1757 else if (!strcmp(argv[0], "set-url"))
1758 result = set_url(argc, argv);
1759 else if (!strcmp(argv[0], "show"))
1760 result = show(argc, argv);
1761 else if (!strcmp(argv[0], "prune"))
1762 result = prune(argc, argv);
1763 else if (!strcmp(argv[0], "update"))
1764 result = update(argc, argv);
1765 else {
1766 error(_("Unknown subcommand: %s"), argv[0]);
1767 usage_with_options(builtin_remote_usage, options);
1768 }
1769
1770 return result ? 1 : 0;
1771 }