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