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