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