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