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