1#include "cache.h"
2#include "parse-options.h"
3#include "transport.h"
4#include "remote.h"
5#include "string-list.h"
6#include "strbuf.h"
7#include "run-command.h"
8#include "refs.h"
9
10static const char * const builtin_remote_usage[] = {
11 "git remote [-v | --verbose]",
12 "git remote add [-t <branch>] [-m <master>] [-f] [--mirror] <name> <url>",
13 "git remote rename <old> <new>",
14 "git remote rm <name>",
15 "git remote set-head <name> [-a | -d | <branch>]",
16 "git remote show [-n] <name>",
17 "git remote prune [-n | --dry-run] <name>",
18 "git remote [-v | --verbose] update [group]",
19 NULL
20};
21
22#define GET_REF_STATES (1<<0)
23#define GET_HEAD_NAMES (1<<1)
24#define GET_PUSH_REF_STATES (1<<2)
25
26static int verbose;
27
28static int show_all(void);
29
30static inline int postfixcmp(const char *string, const char *postfix)
31{
32 int len1 = strlen(string), len2 = strlen(postfix);
33 if (len1 < len2)
34 return 1;
35 return strcmp(string + len1 - len2, postfix);
36}
37
38static int opt_parse_track(const struct option *opt, const char *arg, int not)
39{
40 struct string_list *list = opt->value;
41 if (not)
42 string_list_clear(list, 0);
43 else
44 string_list_append(arg, list);
45 return 0;
46}
47
48static int fetch_remote(const char *name)
49{
50 const char *argv[] = { "fetch", name, NULL, NULL };
51 if (verbose) {
52 argv[1] = "-v";
53 argv[2] = name;
54 }
55 printf("Updating %s\n", name);
56 if (run_command_v_opt(argv, RUN_GIT_CMD))
57 return error("Could not fetch %s", name);
58 return 0;
59}
60
61static int add(int argc, const char **argv)
62{
63 int fetch = 0, mirror = 0;
64 struct string_list track = { NULL, 0, 0 };
65 const char *master = NULL;
66 struct remote *remote;
67 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
68 const char *name, *url;
69 int i;
70
71 struct option options[] = {
72 OPT_GROUP("add specific options"),
73 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
74 OPT_CALLBACK('t', "track", &track, "branch",
75 "branch(es) to track", opt_parse_track),
76 OPT_STRING('m', "master", &master, "branch", "master branch"),
77 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
78 OPT_END()
79 };
80
81 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
82
83 if (argc < 2)
84 usage_with_options(builtin_remote_usage, options);
85
86 name = argv[0];
87 url = argv[1];
88
89 remote = remote_get(name);
90 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
91 remote->fetch_refspec_nr))
92 die("remote %s already exists.", name);
93
94 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
95 if (!valid_fetch_refspec(buf2.buf))
96 die("'%s' is not a valid remote name", name);
97
98 strbuf_addf(&buf, "remote.%s.url", name);
99 if (git_config_set(buf.buf, url))
100 return 1;
101
102 strbuf_reset(&buf);
103 strbuf_addf(&buf, "remote.%s.fetch", name);
104
105 if (track.nr == 0)
106 string_list_append("*", &track);
107 for (i = 0; i < track.nr; i++) {
108 struct string_list_item *item = track.items + i;
109
110 strbuf_reset(&buf2);
111 strbuf_addch(&buf2, '+');
112 if (mirror)
113 strbuf_addf(&buf2, "refs/%s:refs/%s",
114 item->string, item->string);
115 else
116 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
117 item->string, name, item->string);
118 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
119 return 1;
120 }
121
122 if (mirror) {
123 strbuf_reset(&buf);
124 strbuf_addf(&buf, "remote.%s.mirror", name);
125 if (git_config_set(buf.buf, "true"))
126 return 1;
127 }
128
129 if (fetch && fetch_remote(name))
130 return 1;
131
132 if (master) {
133 strbuf_reset(&buf);
134 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
135
136 strbuf_reset(&buf2);
137 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
138
139 if (create_symref(buf.buf, buf2.buf, "remote add"))
140 return error("Could not setup master '%s'", master);
141 }
142
143 strbuf_release(&buf);
144 strbuf_release(&buf2);
145 string_list_clear(&track, 0);
146
147 return 0;
148}
149
150struct branch_info {
151 char *remote_name;
152 struct string_list merge;
153 int rebase;
154};
155
156static struct string_list branch_list;
157
158static const char *abbrev_ref(const char *name, const char *prefix)
159{
160 const char *abbrev = skip_prefix(name, prefix);
161 if (abbrev)
162 return abbrev;
163 return name;
164}
165#define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
166
167static int config_read_branches(const char *key, const char *value, void *cb)
168{
169 if (!prefixcmp(key, "branch.")) {
170 const char *orig_key = key;
171 char *name;
172 struct string_list_item *item;
173 struct branch_info *info;
174 enum { REMOTE, MERGE, REBASE } type;
175
176 key += 7;
177 if (!postfixcmp(key, ".remote")) {
178 name = xstrndup(key, strlen(key) - 7);
179 type = REMOTE;
180 } else if (!postfixcmp(key, ".merge")) {
181 name = xstrndup(key, strlen(key) - 6);
182 type = MERGE;
183 } else if (!postfixcmp(key, ".rebase")) {
184 name = xstrndup(key, strlen(key) - 7);
185 type = REBASE;
186 } else
187 return 0;
188
189 item = string_list_insert(name, &branch_list);
190
191 if (!item->util)
192 item->util = xcalloc(sizeof(struct branch_info), 1);
193 info = item->util;
194 if (type == REMOTE) {
195 if (info->remote_name)
196 warning("more than one %s", orig_key);
197 info->remote_name = xstrdup(value);
198 } else if (type == MERGE) {
199 char *space = strchr(value, ' ');
200 value = abbrev_branch(value);
201 while (space) {
202 char *merge;
203 merge = xstrndup(value, space - value);
204 string_list_append(merge, &info->merge);
205 value = abbrev_branch(space + 1);
206 space = strchr(value, ' ');
207 }
208 string_list_append(xstrdup(value), &info->merge);
209 } else
210 info->rebase = git_config_bool(orig_key, value);
211 }
212 return 0;
213}
214
215static void read_branches(void)
216{
217 if (branch_list.nr)
218 return;
219 git_config(config_read_branches, NULL);
220}
221
222struct ref_states {
223 struct remote *remote;
224 struct string_list new, stale, tracked, heads, push;
225 int queried;
226};
227
228static int handle_one_branch(const char *refname,
229 const unsigned char *sha1, int flags, void *cb_data)
230{
231 struct ref_states *states = cb_data;
232 struct refspec refspec;
233
234 memset(&refspec, 0, sizeof(refspec));
235 refspec.dst = (char *)refname;
236 if (!remote_find_tracking(states->remote, &refspec)) {
237 struct string_list_item *item;
238 const char *name = abbrev_branch(refspec.src);
239 /* symbolic refs pointing nowhere were handled already */
240 if ((flags & REF_ISSYMREF) ||
241 string_list_has_string(&states->tracked, name) ||
242 string_list_has_string(&states->new, name))
243 return 0;
244 item = string_list_append(name, &states->stale);
245 item->util = xstrdup(refname);
246 }
247 return 0;
248}
249
250static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
251{
252 struct ref *fetch_map = NULL, **tail = &fetch_map;
253 struct ref *ref;
254 int i;
255
256 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
257 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
258 die("Could not get fetch map for refspec %s",
259 states->remote->fetch_refspec[i]);
260
261 states->new.strdup_strings = states->tracked.strdup_strings = 1;
262 for (ref = fetch_map; ref; ref = ref->next) {
263 unsigned char sha1[20];
264 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
265 string_list_append(abbrev_branch(ref->name), &states->new);
266 else
267 string_list_append(abbrev_branch(ref->name), &states->tracked);
268 }
269 free_refs(fetch_map);
270
271 sort_string_list(&states->new);
272 sort_string_list(&states->tracked);
273 for_each_ref(handle_one_branch, states);
274 sort_string_list(&states->stale);
275
276 return 0;
277}
278
279struct push_info {
280 char *dest;
281 int forced;
282 enum {
283 PUSH_STATUS_CREATE = 0,
284 PUSH_STATUS_DELETE,
285 PUSH_STATUS_UPTODATE,
286 PUSH_STATUS_FASTFORWARD,
287 PUSH_STATUS_OUTOFDATE,
288 PUSH_STATUS_NOTQUERIED,
289 } status;
290};
291
292static int get_push_ref_states(const struct ref *remote_refs,
293 struct ref_states *states)
294{
295 struct remote *remote = states->remote;
296 struct ref *ref, *local_refs, *push_map, **push_tail;
297 if (remote->mirror)
298 return 0;
299
300 local_refs = get_local_heads();
301 ref = push_map = copy_ref_list(remote_refs);
302 while (ref->next)
303 ref = ref->next;
304 push_tail = &ref->next;
305
306 match_refs(local_refs, push_map, &push_tail, remote->push_refspec_nr,
307 remote->push_refspec, MATCH_REFS_NONE);
308
309 states->push.strdup_strings = 1;
310 for (ref = push_map; ref; ref = ref->next) {
311 struct string_list_item *item;
312 struct push_info *info;
313
314 if (!ref->peer_ref)
315 continue;
316 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
317
318 item = string_list_append(abbrev_branch(ref->peer_ref->name),
319 &states->push);
320 item->util = xcalloc(sizeof(struct push_info), 1);
321 info = item->util;
322 info->forced = ref->force;
323 info->dest = xstrdup(abbrev_branch(ref->name));
324
325 if (is_null_sha1(ref->new_sha1)) {
326 info->status = PUSH_STATUS_DELETE;
327 } else if (!hashcmp(ref->old_sha1, ref->new_sha1))
328 info->status = PUSH_STATUS_UPTODATE;
329 else if (is_null_sha1(ref->old_sha1))
330 info->status = PUSH_STATUS_CREATE;
331 else if (has_sha1_file(ref->old_sha1) &&
332 ref_newer(ref->new_sha1, ref->old_sha1))
333 info->status = PUSH_STATUS_FASTFORWARD;
334 else
335 info->status = PUSH_STATUS_OUTOFDATE;
336 }
337 free_refs(local_refs);
338 free_refs(push_map);
339 return 0;
340}
341
342static int get_push_ref_states_noquery(struct ref_states *states)
343{
344 int i;
345 struct remote *remote = states->remote;
346 struct string_list_item *item;
347 struct push_info *info;
348
349 if (remote->mirror)
350 return 0;
351
352 states->push.strdup_strings = 1;
353 if (!remote->push_refspec_nr) {
354 item = string_list_append("(matching)", &states->push);
355 info = item->util = xcalloc(sizeof(struct push_info), 1);
356 info->status = PUSH_STATUS_NOTQUERIED;
357 info->dest = xstrdup(item->string);
358 }
359 for (i = 0; i < remote->push_refspec_nr; i++) {
360 struct refspec *spec = remote->push + i;
361 if (spec->matching)
362 item = string_list_append("(matching)", &states->push);
363 else if (strlen(spec->src))
364 item = string_list_append(spec->src, &states->push);
365 else
366 item = string_list_append("(delete)", &states->push);
367
368 info = item->util = xcalloc(sizeof(struct push_info), 1);
369 info->forced = spec->force;
370 info->status = PUSH_STATUS_NOTQUERIED;
371 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
372 }
373 return 0;
374}
375
376static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
377{
378 struct ref *ref, *matches;
379 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
380 struct refspec refspec;
381
382 refspec.force = 0;
383 refspec.pattern = 1;
384 refspec.src = refspec.dst = "refs/heads/*";
385 states->heads.strdup_strings = 1;
386 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
387 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
388 fetch_map, 1);
389 for(ref = matches; ref; ref = ref->next)
390 string_list_append(abbrev_branch(ref->name), &states->heads);
391
392 free_refs(fetch_map);
393 free_refs(matches);
394
395 return 0;
396}
397
398struct known_remote {
399 struct known_remote *next;
400 struct remote *remote;
401};
402
403struct known_remotes {
404 struct remote *to_delete;
405 struct known_remote *list;
406};
407
408static int add_known_remote(struct remote *remote, void *cb_data)
409{
410 struct known_remotes *all = cb_data;
411 struct known_remote *r;
412
413 if (!strcmp(all->to_delete->name, remote->name))
414 return 0;
415
416 r = xmalloc(sizeof(*r));
417 r->remote = remote;
418 r->next = all->list;
419 all->list = r;
420 return 0;
421}
422
423struct branches_for_remote {
424 struct remote *remote;
425 struct string_list *branches, *skipped;
426 struct known_remotes *keep;
427};
428
429static int add_branch_for_removal(const char *refname,
430 const unsigned char *sha1, int flags, void *cb_data)
431{
432 struct branches_for_remote *branches = cb_data;
433 struct refspec refspec;
434 struct string_list_item *item;
435 struct known_remote *kr;
436
437 memset(&refspec, 0, sizeof(refspec));
438 refspec.dst = (char *)refname;
439 if (remote_find_tracking(branches->remote, &refspec))
440 return 0;
441
442 /* don't delete a branch if another remote also uses it */
443 for (kr = branches->keep->list; kr; kr = kr->next) {
444 memset(&refspec, 0, sizeof(refspec));
445 refspec.dst = (char *)refname;
446 if (!remote_find_tracking(kr->remote, &refspec))
447 return 0;
448 }
449
450 /* don't delete non-remote refs */
451 if (prefixcmp(refname, "refs/remotes")) {
452 /* advise user how to delete local branches */
453 if (!prefixcmp(refname, "refs/heads/"))
454 string_list_append(abbrev_branch(refname),
455 branches->skipped);
456 /* silently skip over other non-remote refs */
457 return 0;
458 }
459
460 /* make sure that symrefs are deleted */
461 if (flags & REF_ISSYMREF)
462 return unlink(git_path("%s", refname));
463
464 item = string_list_append(refname, branches->branches);
465 item->util = xmalloc(20);
466 hashcpy(item->util, sha1);
467
468 return 0;
469}
470
471struct rename_info {
472 const char *old;
473 const char *new;
474 struct string_list *remote_branches;
475};
476
477static int read_remote_branches(const char *refname,
478 const unsigned char *sha1, int flags, void *cb_data)
479{
480 struct rename_info *rename = cb_data;
481 struct strbuf buf = STRBUF_INIT;
482 struct string_list_item *item;
483 int flag;
484 unsigned char orig_sha1[20];
485 const char *symref;
486
487 strbuf_addf(&buf, "refs/remotes/%s", rename->old);
488 if(!prefixcmp(refname, buf.buf)) {
489 item = string_list_append(xstrdup(refname), rename->remote_branches);
490 symref = resolve_ref(refname, orig_sha1, 1, &flag);
491 if (flag & REF_ISSYMREF)
492 item->util = xstrdup(symref);
493 else
494 item->util = NULL;
495 }
496
497 return 0;
498}
499
500static int migrate_file(struct remote *remote)
501{
502 struct strbuf buf = STRBUF_INIT;
503 int i;
504 char *path = NULL;
505
506 strbuf_addf(&buf, "remote.%s.url", remote->name);
507 for (i = 0; i < remote->url_nr; i++)
508 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
509 return error("Could not append '%s' to '%s'",
510 remote->url[i], buf.buf);
511 strbuf_reset(&buf);
512 strbuf_addf(&buf, "remote.%s.push", remote->name);
513 for (i = 0; i < remote->push_refspec_nr; i++)
514 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
515 return error("Could not append '%s' to '%s'",
516 remote->push_refspec[i], buf.buf);
517 strbuf_reset(&buf);
518 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
519 for (i = 0; i < remote->fetch_refspec_nr; i++)
520 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
521 return error("Could not append '%s' to '%s'",
522 remote->fetch_refspec[i], buf.buf);
523 if (remote->origin == REMOTE_REMOTES)
524 path = git_path("remotes/%s", remote->name);
525 else if (remote->origin == REMOTE_BRANCHES)
526 path = git_path("branches/%s", remote->name);
527 if (path && unlink(path))
528 warning("failed to remove '%s'", path);
529 return 0;
530}
531
532static int mv(int argc, const char **argv)
533{
534 struct option options[] = {
535 OPT_END()
536 };
537 struct remote *oldremote, *newremote;
538 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
539 struct string_list remote_branches = { NULL, 0, 0, 0 };
540 struct rename_info rename;
541 int i;
542
543 if (argc != 3)
544 usage_with_options(builtin_remote_usage, options);
545
546 rename.old = argv[1];
547 rename.new = argv[2];
548 rename.remote_branches = &remote_branches;
549
550 oldremote = remote_get(rename.old);
551 if (!oldremote)
552 die("No such remote: %s", rename.old);
553
554 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
555 return migrate_file(oldremote);
556
557 newremote = remote_get(rename.new);
558 if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
559 die("remote %s already exists.", rename.new);
560
561 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
562 if (!valid_fetch_refspec(buf.buf))
563 die("'%s' is not a valid remote name", rename.new);
564
565 strbuf_reset(&buf);
566 strbuf_addf(&buf, "remote.%s", rename.old);
567 strbuf_addf(&buf2, "remote.%s", rename.new);
568 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
569 return error("Could not rename config section '%s' to '%s'",
570 buf.buf, buf2.buf);
571
572 strbuf_reset(&buf);
573 strbuf_addf(&buf, "remote.%s.fetch", rename.new);
574 if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
575 return error("Could not remove config section '%s'", buf.buf);
576 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
577 char *ptr;
578
579 strbuf_reset(&buf2);
580 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
581 ptr = strstr(buf2.buf, rename.old);
582 if (ptr)
583 strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
584 rename.new, strlen(rename.new));
585 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
586 return error("Could not append '%s'", buf.buf);
587 }
588
589 read_branches();
590 for (i = 0; i < branch_list.nr; i++) {
591 struct string_list_item *item = branch_list.items + i;
592 struct branch_info *info = item->util;
593 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
594 strbuf_reset(&buf);
595 strbuf_addf(&buf, "branch.%s.remote", item->string);
596 if (git_config_set(buf.buf, rename.new)) {
597 return error("Could not set '%s'", buf.buf);
598 }
599 }
600 }
601
602 /*
603 * First remove symrefs, then rename the rest, finally create
604 * the new symrefs.
605 */
606 for_each_ref(read_remote_branches, &rename);
607 for (i = 0; i < remote_branches.nr; i++) {
608 struct string_list_item *item = remote_branches.items + i;
609 int flag = 0;
610 unsigned char sha1[20];
611 const char *symref;
612
613 symref = resolve_ref(item->string, sha1, 1, &flag);
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
656static int remove_branches(struct string_list *branches)
657{
658 int i, result = 0;
659 for (i = 0; i < branches->nr; i++) {
660 struct string_list_item *item = branches->items + i;
661 const char *refname = item->string;
662 unsigned char *sha1 = item->util;
663
664 if (delete_ref(refname, sha1, 0))
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;
676 struct strbuf buf = STRBUF_INIT;
677 struct known_remotes known_remotes = { NULL, NULL };
678 struct string_list branches = { NULL, 0, 0, 1 };
679 struct string_list skipped = { NULL, 0, 0, 1 };
680 struct branches_for_remote cb_data = {
681 NULL, &branches, &skipped, &known_remotes
682 };
683 int i, result;
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
692 known_remotes.to_delete = remote;
693 for_each_remote(add_known_remote, &known_remotes);
694
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++) {
701 struct string_list_item *item = branch_list.items + i;
702 struct branch_info *info = item->util;
703 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
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",
708 item->string, *k);
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 */
722 cb_data.remote = remote;
723 result = for_each_ref(add_branch_for_removal, &cb_data);
724 strbuf_release(&buf);
725
726 if (!result)
727 result = remove_branches(&branches);
728 string_list_clear(&branches, 1);
729
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
742 return result;
743}
744
745void clear_push_info(void *util, const char *string)
746{
747 struct push_info *info = util;
748 free(info->dest);
749 free(info);
750}
751
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);
757 string_list_clear(&states->heads, 0);
758 string_list_clear_func(&states->push, clear_push_info);
759}
760
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
767 if (flags & REF_ISSYMREF)
768 return 0;
769
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;
776}
777
778static int get_remote_ref_states(const char *name,
779 struct ref_states *states,
780 int query)
781{
782 struct transport *transport;
783 const struct ref *remote_refs;
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);
794 remote_refs = transport_get_remote_refs(transport);
795 transport_disconnect(transport);
796
797 states->queried = 1;
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);
802 if (query & GET_PUSH_REF_STATES)
803 get_push_ref_states(remote_refs, states);
804 } else {
805 for_each_ref(append_ref_to_tracked_list, states);
806 sort_string_list(&states->tracked);
807 get_push_ref_states_noquery(states);
808 }
809
810 return 0;
811}
812
813struct show_info {
814 struct string_list *list;
815 struct ref_states *states;
816 int width, width2;
817 int any_rebase;
818};
819
820int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
821{
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}
829
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}
910
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
926int show_push_info_item(struct string_list_item *item, void *cb_data)
927{
928 struct show_info *show_info = cb_data;
929 struct push_info *push_info = item->util;
930 char *src = item->string, *status = NULL;
931
932 switch (push_info->status) {
933 case PUSH_STATUS_CREATE:
934 status = "create";
935 break;
936 case PUSH_STATUS_DELETE:
937 status = "delete";
938 src = "(none)";
939 break;
940 case PUSH_STATUS_UPTODATE:
941 status = "up to date";
942 break;
943 case PUSH_STATUS_FASTFORWARD:
944 status = "fast forwardable";
945 break;
946 case PUSH_STATUS_OUTOFDATE:
947 status = "local out of date";
948 break;
949 case PUSH_STATUS_NOTQUERIED:
950 break;
951 }
952 if (status)
953 printf(" %-*s %s to %-*s (%s)\n", show_info->width, src,
954 push_info->forced ? "forces" : "pushes",
955 show_info->width2, push_info->dest, status);
956 else
957 printf(" %-*s %s to %s\n", show_info->width, src,
958 push_info->forced ? "forces" : "pushes",
959 push_info->dest);
960 return 0;
961}
962
963static int show(int argc, const char **argv)
964{
965 int no_query = 0, result = 0, query_flag = 0;
966 struct option options[] = {
967 OPT_GROUP("show specific options"),
968 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
969 OPT_END()
970 };
971 struct ref_states states;
972 struct string_list info_list = { NULL, 0, 0, 0 };
973 struct show_info info;
974
975 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
976
977 if (argc < 1)
978 return show_all();
979
980 if (!no_query)
981 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
982
983 memset(&states, 0, sizeof(states));
984 memset(&info, 0, sizeof(info));
985 info.states = &states;
986 info.list = &info_list;
987 for (; argc; argc--, argv++) {
988 int i;
989
990 get_remote_ref_states(*argv, &states, query_flag);
991
992 printf("* remote %s\n URL: %s\n", *argv,
993 states.remote->url_nr > 0 ?
994 states.remote->url[0] : "(no URL)");
995 if (no_query)
996 printf(" HEAD branch: (not queried)\n");
997 else if (!states.heads.nr)
998 printf(" HEAD branch: (unknown)\n");
999 else if (states.heads.nr == 1)
1000 printf(" HEAD branch: %s\n", states.heads.items[0].string);
1001 else {
1002 printf(" HEAD branch (remote HEAD is ambiguous,"
1003 " may be one of the following):\n");
1004 for (i = 0; i < states.heads.nr; i++)
1005 printf(" %s\n", states.heads.items[i].string);
1006 }
1007
1008 /* remote branch info */
1009 info.width = 0;
1010 for_each_string_list(add_remote_to_show_info, &states.new, &info);
1011 for_each_string_list(add_remote_to_show_info, &states.tracked, &info);
1012 for_each_string_list(add_remote_to_show_info, &states.stale, &info);
1013 if (info.list->nr)
1014 printf(" Remote branch%s:%s\n",
1015 info.list->nr > 1 ? "es" : "",
1016 no_query ? " (status not queried)" : "");
1017 for_each_string_list(show_remote_info_item, info.list, &info);
1018 string_list_clear(info.list, 0);
1019
1020 /* git pull info */
1021 info.width = 0;
1022 info.any_rebase = 0;
1023 for_each_string_list(add_local_to_show_info, &branch_list, &info);
1024 if (info.list->nr)
1025 printf(" Local branch%s configured for 'git pull':\n",
1026 info.list->nr > 1 ? "es" : "");
1027 for_each_string_list(show_local_info_item, info.list, &info);
1028 string_list_clear(info.list, 0);
1029
1030 /* git push info */
1031 if (states.remote->mirror)
1032 printf(" Local refs will be mirrored by 'git push'\n");
1033
1034 info.width = info.width2 = 0;
1035 for_each_string_list(add_push_to_show_info, &states.push, &info);
1036 sort_string_list(info.list);
1037 if (info.list->nr)
1038 printf(" Local ref%s configured for 'git push'%s:\n",
1039 info.list->nr > 1 ? "s" : "",
1040 no_query ? " (status not queried)" : "");
1041 for_each_string_list(show_push_info_item, info.list, &info);
1042 string_list_clear(info.list, 0);
1043
1044 free_remote_ref_states(&states);
1045 }
1046
1047 return result;
1048}
1049
1050static int set_head(int argc, const char **argv)
1051{
1052 int i, opt_a = 0, opt_d = 0, result = 0;
1053 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1054 char *head_name = NULL;
1055
1056 struct option options[] = {
1057 OPT_GROUP("set-head specific options"),
1058 OPT_BOOLEAN('a', "auto", &opt_a,
1059 "set refs/remotes/<name>/HEAD according to remote"),
1060 OPT_BOOLEAN('d', "delete", &opt_d,
1061 "delete refs/remotes/<name>/HEAD"),
1062 OPT_END()
1063 };
1064 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
1065 if (argc)
1066 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1067
1068 if (!opt_a && !opt_d && argc == 2) {
1069 head_name = xstrdup(argv[1]);
1070 } else if (opt_a && !opt_d && argc == 1) {
1071 struct ref_states states;
1072 memset(&states, 0, sizeof(states));
1073 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1074 if (!states.heads.nr)
1075 result |= error("Cannot determine remote HEAD");
1076 else if (states.heads.nr > 1) {
1077 result |= error("Multiple remote HEAD branches. "
1078 "Please choose one explicitly with:");
1079 for (i = 0; i < states.heads.nr; i++)
1080 fprintf(stderr, " git remote set-head %s %s\n",
1081 argv[0], states.heads.items[i].string);
1082 } else
1083 head_name = xstrdup(states.heads.items[0].string);
1084 free_remote_ref_states(&states);
1085 } else if (opt_d && !opt_a && argc == 1) {
1086 if (delete_ref(buf.buf, NULL, REF_NODEREF))
1087 result |= error("Could not delete %s", buf.buf);
1088 } else
1089 usage_with_options(builtin_remote_usage, options);
1090
1091 if (head_name) {
1092 unsigned char sha1[20];
1093 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1094 /* make sure it's valid */
1095 if (!resolve_ref(buf2.buf, sha1, 1, NULL))
1096 result |= error("Not a valid ref: %s", buf2.buf);
1097 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1098 result |= error("Could not setup %s", buf.buf);
1099 if (opt_a)
1100 printf("%s/HEAD set to %s\n", argv[0], head_name);
1101 free(head_name);
1102 }
1103
1104 strbuf_release(&buf);
1105 strbuf_release(&buf2);
1106 return result;
1107}
1108
1109static int prune(int argc, const char **argv)
1110{
1111 int dry_run = 0, result = 0;
1112 struct option options[] = {
1113 OPT_GROUP("prune specific options"),
1114 OPT__DRY_RUN(&dry_run),
1115 OPT_END()
1116 };
1117 struct ref_states states;
1118 const char *dangling_msg;
1119
1120 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
1121
1122 if (argc < 1)
1123 usage_with_options(builtin_remote_usage, options);
1124
1125 dangling_msg = (dry_run
1126 ? " %s will become dangling!\n"
1127 : " %s has become dangling!\n");
1128
1129 memset(&states, 0, sizeof(states));
1130 for (; argc; argc--, argv++) {
1131 int i;
1132
1133 get_remote_ref_states(*argv, &states, GET_REF_STATES);
1134
1135 if (states.stale.nr) {
1136 printf("Pruning %s\n", *argv);
1137 printf("URL: %s\n",
1138 states.remote->url_nr
1139 ? states.remote->url[0]
1140 : "(no URL)");
1141 }
1142
1143 for (i = 0; i < states.stale.nr; i++) {
1144 const char *refname = states.stale.items[i].util;
1145
1146 if (!dry_run)
1147 result |= delete_ref(refname, NULL, 0);
1148
1149 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
1150 abbrev_ref(refname, "refs/remotes/"));
1151 warn_dangling_symref(dangling_msg, refname);
1152 }
1153
1154 free_remote_ref_states(&states);
1155 }
1156
1157 return result;
1158}
1159
1160static int get_one_remote_for_update(struct remote *remote, void *priv)
1161{
1162 struct string_list *list = priv;
1163 if (!remote->skip_default_update)
1164 string_list_append(remote->name, list);
1165 return 0;
1166}
1167
1168struct remote_group {
1169 const char *name;
1170 struct string_list *list;
1171} remote_group;
1172
1173static int get_remote_group(const char *key, const char *value, void *cb)
1174{
1175 if (!prefixcmp(key, "remotes.") &&
1176 !strcmp(key + 8, remote_group.name)) {
1177 /* split list by white space */
1178 int space = strcspn(value, " \t\n");
1179 while (*value) {
1180 if (space > 1)
1181 string_list_append(xstrndup(value, space),
1182 remote_group.list);
1183 value += space + (value[space] != '\0');
1184 space = strcspn(value, " \t\n");
1185 }
1186 }
1187
1188 return 0;
1189}
1190
1191static int update(int argc, const char **argv)
1192{
1193 int i, result = 0;
1194 struct string_list list = { NULL, 0, 0, 0 };
1195 static const char *default_argv[] = { NULL, "default", NULL };
1196
1197 if (argc < 2) {
1198 argc = 2;
1199 argv = default_argv;
1200 }
1201
1202 remote_group.list = &list;
1203 for (i = 1; i < argc; i++) {
1204 remote_group.name = argv[i];
1205 result = git_config(get_remote_group, NULL);
1206 }
1207
1208 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
1209 result = for_each_remote(get_one_remote_for_update, &list);
1210
1211 for (i = 0; i < list.nr; i++)
1212 result |= fetch_remote(list.items[i].string);
1213
1214 /* all names were strdup()ed or strndup()ed */
1215 list.strdup_strings = 1;
1216 string_list_clear(&list, 0);
1217
1218 return result;
1219}
1220
1221static int get_one_entry(struct remote *remote, void *priv)
1222{
1223 struct string_list *list = priv;
1224
1225 if (remote->url_nr > 0) {
1226 int i;
1227
1228 for (i = 0; i < remote->url_nr; i++)
1229 string_list_append(remote->name, list)->util = (void *)remote->url[i];
1230 } else
1231 string_list_append(remote->name, list)->util = NULL;
1232
1233 return 0;
1234}
1235
1236static int show_all(void)
1237{
1238 struct string_list list = { NULL, 0, 0 };
1239 int result = for_each_remote(get_one_entry, &list);
1240
1241 if (!result) {
1242 int i;
1243
1244 sort_string_list(&list);
1245 for (i = 0; i < list.nr; i++) {
1246 struct string_list_item *item = list.items + i;
1247 if (verbose)
1248 printf("%s\t%s\n", item->string,
1249 item->util ? (const char *)item->util : "");
1250 else {
1251 if (i && !strcmp((item - 1)->string, item->string))
1252 continue;
1253 printf("%s\n", item->string);
1254 }
1255 }
1256 }
1257 return result;
1258}
1259
1260int cmd_remote(int argc, const char **argv, const char *prefix)
1261{
1262 struct option options[] = {
1263 OPT__VERBOSE(&verbose),
1264 OPT_END()
1265 };
1266 int result;
1267
1268 argc = parse_options(argc, argv, options, builtin_remote_usage,
1269 PARSE_OPT_STOP_AT_NON_OPTION);
1270
1271 if (argc < 1)
1272 result = show_all();
1273 else if (!strcmp(argv[0], "add"))
1274 result = add(argc, argv);
1275 else if (!strcmp(argv[0], "rename"))
1276 result = mv(argc, argv);
1277 else if (!strcmp(argv[0], "rm"))
1278 result = rm(argc, argv);
1279 else if (!strcmp(argv[0], "set-head"))
1280 result = set_head(argc, argv);
1281 else if (!strcmp(argv[0], "show"))
1282 result = show(argc, argv);
1283 else if (!strcmp(argv[0], "prune"))
1284 result = prune(argc, argv);
1285 else if (!strcmp(argv[0], "update"))
1286 result = update(argc, argv);
1287 else {
1288 error("Unknown subcommand: %s", argv[0]);
1289 usage_with_options(builtin_remote_usage, options);
1290 }
1291
1292 return result ? 1 : 0;
1293}