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