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