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