t/README: Document GIT_TEST_INSTALLED and GIT_TEST_EXEC_PATH
[git/mingw.git] / builtin-remote.c
blob79166262182a1cb51839e20b8abcef8f6752b59f
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"
10 static 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 [-v | --verbose] show [-n] <name>",
17 "git remote prune [-n | --dry-run] <name>",
18 "git remote [-v | --verbose] update [-p | --prune] [group | remote]",
19 NULL
22 static const char * const builtin_remote_add_usage[] = {
23 "git remote add [<options>] <name> <url>",
24 NULL
27 static const char * const builtin_remote_rename_usage[] = {
28 "git remote rename <old> <new>",
29 NULL
32 static const char * const builtin_remote_rm_usage[] = {
33 "git remote rm <name>",
34 NULL
37 static const char * const builtin_remote_sethead_usage[] = {
38 "git remote set-head <name> (-a | -d | <branch>])",
39 NULL
42 static const char * const builtin_remote_show_usage[] = {
43 "git remote show [<options>] <name>",
44 NULL
47 static const char * const builtin_remote_prune_usage[] = {
48 "git remote prune [<options>] <name>",
49 NULL
52 static const char * const builtin_remote_update_usage[] = {
53 "git remote update [<options>] [<group> | <remote>]...",
54 NULL
57 #define GET_REF_STATES (1<<0)
58 #define GET_HEAD_NAMES (1<<1)
59 #define GET_PUSH_REF_STATES (1<<2)
61 static int verbose;
63 static int show_all(void);
64 static int prune_remote(const char *remote, int dry_run);
66 static inline int postfixcmp(const char *string, const char *postfix)
68 int len1 = strlen(string), len2 = strlen(postfix);
69 if (len1 < len2)
70 return 1;
71 return strcmp(string + len1 - len2, postfix);
74 static int opt_parse_track(const struct option *opt, const char *arg, int not)
76 struct string_list *list = opt->value;
77 if (not)
78 string_list_clear(list, 0);
79 else
80 string_list_append(arg, list);
81 return 0;
84 static int fetch_remote(const char *name)
86 const char *argv[] = { "fetch", name, NULL, NULL };
87 if (verbose) {
88 argv[1] = "-v";
89 argv[2] = name;
91 printf("Updating %s\n", name);
92 if (run_command_v_opt(argv, RUN_GIT_CMD))
93 return error("Could not fetch %s", name);
94 return 0;
97 static int add(int argc, const char **argv)
99 int fetch = 0, mirror = 0;
100 struct string_list track = { NULL, 0, 0 };
101 const char *master = NULL;
102 struct remote *remote;
103 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
104 const char *name, *url;
105 int i;
107 struct option options[] = {
108 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
109 OPT_CALLBACK('t', "track", &track, "branch",
110 "branch(es) to track", opt_parse_track),
111 OPT_STRING('m', "master", &master, "branch", "master branch"),
112 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
113 OPT_END()
116 argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage,
119 if (argc < 2)
120 usage_with_options(builtin_remote_add_usage, options);
122 name = argv[0];
123 url = argv[1];
125 remote = remote_get(name);
126 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
127 remote->fetch_refspec_nr))
128 die("remote %s already exists.", name);
130 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
131 if (!valid_fetch_refspec(buf2.buf))
132 die("'%s' is not a valid remote name", name);
134 strbuf_addf(&buf, "remote.%s.url", name);
135 if (git_config_set(buf.buf, url))
136 return 1;
138 strbuf_reset(&buf);
139 strbuf_addf(&buf, "remote.%s.fetch", name);
141 if (track.nr == 0)
142 string_list_append("*", &track);
143 for (i = 0; i < track.nr; i++) {
144 struct string_list_item *item = track.items + i;
146 strbuf_reset(&buf2);
147 strbuf_addch(&buf2, '+');
148 if (mirror)
149 strbuf_addf(&buf2, "refs/%s:refs/%s",
150 item->string, item->string);
151 else
152 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
153 item->string, name, item->string);
154 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
155 return 1;
158 if (mirror) {
159 strbuf_reset(&buf);
160 strbuf_addf(&buf, "remote.%s.mirror", name);
161 if (git_config_set(buf.buf, "true"))
162 return 1;
165 if (fetch && fetch_remote(name))
166 return 1;
168 if (master) {
169 strbuf_reset(&buf);
170 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
172 strbuf_reset(&buf2);
173 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
175 if (create_symref(buf.buf, buf2.buf, "remote add"))
176 return error("Could not setup master '%s'", master);
179 strbuf_release(&buf);
180 strbuf_release(&buf2);
181 string_list_clear(&track, 0);
183 return 0;
186 struct branch_info {
187 char *remote_name;
188 struct string_list merge;
189 int rebase;
192 static struct string_list branch_list;
194 static const char *abbrev_ref(const char *name, const char *prefix)
196 const char *abbrev = skip_prefix(name, prefix);
197 if (abbrev)
198 return abbrev;
199 return name;
201 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
203 static int config_read_branches(const char *key, const char *value, void *cb)
205 if (!prefixcmp(key, "branch.")) {
206 const char *orig_key = key;
207 char *name;
208 struct string_list_item *item;
209 struct branch_info *info;
210 enum { REMOTE, MERGE, REBASE } type;
212 key += 7;
213 if (!postfixcmp(key, ".remote")) {
214 name = xstrndup(key, strlen(key) - 7);
215 type = REMOTE;
216 } else if (!postfixcmp(key, ".merge")) {
217 name = xstrndup(key, strlen(key) - 6);
218 type = MERGE;
219 } else if (!postfixcmp(key, ".rebase")) {
220 name = xstrndup(key, strlen(key) - 7);
221 type = REBASE;
222 } else
223 return 0;
225 item = string_list_insert(name, &branch_list);
227 if (!item->util)
228 item->util = xcalloc(sizeof(struct branch_info), 1);
229 info = item->util;
230 if (type == REMOTE) {
231 if (info->remote_name)
232 warning("more than one %s", orig_key);
233 info->remote_name = xstrdup(value);
234 } else if (type == MERGE) {
235 char *space = strchr(value, ' ');
236 value = abbrev_branch(value);
237 while (space) {
238 char *merge;
239 merge = xstrndup(value, space - value);
240 string_list_append(merge, &info->merge);
241 value = abbrev_branch(space + 1);
242 space = strchr(value, ' ');
244 string_list_append(xstrdup(value), &info->merge);
245 } else
246 info->rebase = git_config_bool(orig_key, value);
248 return 0;
251 static void read_branches(void)
253 if (branch_list.nr)
254 return;
255 git_config(config_read_branches, NULL);
258 struct ref_states {
259 struct remote *remote;
260 struct string_list new, stale, tracked, heads, push;
261 int queried;
264 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
266 struct ref *fetch_map = NULL, **tail = &fetch_map;
267 struct ref *ref, *stale_refs;
268 int i;
270 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
271 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
272 die("Could not get fetch map for refspec %s",
273 states->remote->fetch_refspec[i]);
275 states->new.strdup_strings = states->tracked.strdup_strings = 1;
276 for (ref = fetch_map; ref; ref = ref->next) {
277 unsigned char sha1[20];
278 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
279 string_list_append(abbrev_branch(ref->name), &states->new);
280 else
281 string_list_append(abbrev_branch(ref->name), &states->tracked);
283 stale_refs = get_stale_heads(states->remote, fetch_map);
284 for (ref = stale_refs; ref; ref = ref->next) {
285 struct string_list_item *item =
286 string_list_append(abbrev_branch(ref->name), &states->stale);
287 item->util = xstrdup(ref->name);
289 free_refs(stale_refs);
290 free_refs(fetch_map);
292 sort_string_list(&states->new);
293 sort_string_list(&states->tracked);
294 sort_string_list(&states->stale);
296 return 0;
299 struct push_info {
300 char *dest;
301 int forced;
302 enum {
303 PUSH_STATUS_CREATE = 0,
304 PUSH_STATUS_DELETE,
305 PUSH_STATUS_UPTODATE,
306 PUSH_STATUS_FASTFORWARD,
307 PUSH_STATUS_OUTOFDATE,
308 PUSH_STATUS_NOTQUERIED,
309 } status;
312 static int get_push_ref_states(const struct ref *remote_refs,
313 struct ref_states *states)
315 struct remote *remote = states->remote;
316 struct ref *ref, *local_refs, *push_map;
317 if (remote->mirror)
318 return 0;
320 local_refs = get_local_heads();
321 push_map = copy_ref_list(remote_refs);
323 match_refs(local_refs, &push_map, remote->push_refspec_nr,
324 remote->push_refspec, MATCH_REFS_NONE);
326 states->push.strdup_strings = 1;
327 for (ref = push_map; ref; ref = ref->next) {
328 struct string_list_item *item;
329 struct push_info *info;
331 if (!ref->peer_ref)
332 continue;
333 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
335 item = string_list_append(abbrev_branch(ref->peer_ref->name),
336 &states->push);
337 item->util = xcalloc(sizeof(struct push_info), 1);
338 info = item->util;
339 info->forced = ref->force;
340 info->dest = xstrdup(abbrev_branch(ref->name));
342 if (is_null_sha1(ref->new_sha1)) {
343 info->status = PUSH_STATUS_DELETE;
344 } else if (!hashcmp(ref->old_sha1, ref->new_sha1))
345 info->status = PUSH_STATUS_UPTODATE;
346 else if (is_null_sha1(ref->old_sha1))
347 info->status = PUSH_STATUS_CREATE;
348 else if (has_sha1_file(ref->old_sha1) &&
349 ref_newer(ref->new_sha1, ref->old_sha1))
350 info->status = PUSH_STATUS_FASTFORWARD;
351 else
352 info->status = PUSH_STATUS_OUTOFDATE;
354 free_refs(local_refs);
355 free_refs(push_map);
356 return 0;
359 static int get_push_ref_states_noquery(struct ref_states *states)
361 int i;
362 struct remote *remote = states->remote;
363 struct string_list_item *item;
364 struct push_info *info;
366 if (remote->mirror)
367 return 0;
369 states->push.strdup_strings = 1;
370 if (!remote->push_refspec_nr) {
371 item = string_list_append("(matching)", &states->push);
372 info = item->util = xcalloc(sizeof(struct push_info), 1);
373 info->status = PUSH_STATUS_NOTQUERIED;
374 info->dest = xstrdup(item->string);
376 for (i = 0; i < remote->push_refspec_nr; i++) {
377 struct refspec *spec = remote->push + i;
378 if (spec->matching)
379 item = string_list_append("(matching)", &states->push);
380 else if (strlen(spec->src))
381 item = string_list_append(spec->src, &states->push);
382 else
383 item = string_list_append("(delete)", &states->push);
385 info = item->util = xcalloc(sizeof(struct push_info), 1);
386 info->forced = spec->force;
387 info->status = PUSH_STATUS_NOTQUERIED;
388 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
390 return 0;
393 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
395 struct ref *ref, *matches;
396 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
397 struct refspec refspec;
399 refspec.force = 0;
400 refspec.pattern = 1;
401 refspec.src = refspec.dst = "refs/heads/*";
402 states->heads.strdup_strings = 1;
403 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
404 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
405 fetch_map, 1);
406 for (ref = matches; ref; ref = ref->next)
407 string_list_append(abbrev_branch(ref->name), &states->heads);
409 free_refs(fetch_map);
410 free_refs(matches);
412 return 0;
415 struct known_remote {
416 struct known_remote *next;
417 struct remote *remote;
420 struct known_remotes {
421 struct remote *to_delete;
422 struct known_remote *list;
425 static int add_known_remote(struct remote *remote, void *cb_data)
427 struct known_remotes *all = cb_data;
428 struct known_remote *r;
430 if (!strcmp(all->to_delete->name, remote->name))
431 return 0;
433 r = xmalloc(sizeof(*r));
434 r->remote = remote;
435 r->next = all->list;
436 all->list = r;
437 return 0;
440 struct branches_for_remote {
441 struct remote *remote;
442 struct string_list *branches, *skipped;
443 struct known_remotes *keep;
446 static int add_branch_for_removal(const char *refname,
447 const unsigned char *sha1, int flags, void *cb_data)
449 struct branches_for_remote *branches = cb_data;
450 struct refspec refspec;
451 struct string_list_item *item;
452 struct known_remote *kr;
454 memset(&refspec, 0, sizeof(refspec));
455 refspec.dst = (char *)refname;
456 if (remote_find_tracking(branches->remote, &refspec))
457 return 0;
459 /* don't delete a branch if another remote also uses it */
460 for (kr = branches->keep->list; kr; kr = kr->next) {
461 memset(&refspec, 0, sizeof(refspec));
462 refspec.dst = (char *)refname;
463 if (!remote_find_tracking(kr->remote, &refspec))
464 return 0;
467 /* don't delete non-remote refs */
468 if (prefixcmp(refname, "refs/remotes")) {
469 /* advise user how to delete local branches */
470 if (!prefixcmp(refname, "refs/heads/"))
471 string_list_append(abbrev_branch(refname),
472 branches->skipped);
473 /* silently skip over other non-remote refs */
474 return 0;
477 /* make sure that symrefs are deleted */
478 if (flags & REF_ISSYMREF)
479 return unlink(git_path("%s", refname));
481 item = string_list_append(refname, branches->branches);
482 item->util = xmalloc(20);
483 hashcpy(item->util, sha1);
485 return 0;
488 struct rename_info {
489 const char *old;
490 const char *new;
491 struct string_list *remote_branches;
494 static int read_remote_branches(const char *refname,
495 const unsigned char *sha1, int flags, void *cb_data)
497 struct rename_info *rename = cb_data;
498 struct strbuf buf = STRBUF_INIT;
499 struct string_list_item *item;
500 int flag;
501 unsigned char orig_sha1[20];
502 const char *symref;
504 strbuf_addf(&buf, "refs/remotes/%s", rename->old);
505 if (!prefixcmp(refname, buf.buf)) {
506 item = string_list_append(xstrdup(refname), rename->remote_branches);
507 symref = resolve_ref(refname, orig_sha1, 1, &flag);
508 if (flag & REF_ISSYMREF)
509 item->util = xstrdup(symref);
510 else
511 item->util = NULL;
514 return 0;
517 static int migrate_file(struct remote *remote)
519 struct strbuf buf = STRBUF_INIT;
520 int i;
521 char *path = NULL;
523 strbuf_addf(&buf, "remote.%s.url", remote->name);
524 for (i = 0; i < remote->url_nr; i++)
525 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
526 return error("Could not append '%s' to '%s'",
527 remote->url[i], buf.buf);
528 strbuf_reset(&buf);
529 strbuf_addf(&buf, "remote.%s.push", remote->name);
530 for (i = 0; i < remote->push_refspec_nr; i++)
531 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
532 return error("Could not append '%s' to '%s'",
533 remote->push_refspec[i], buf.buf);
534 strbuf_reset(&buf);
535 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
536 for (i = 0; i < remote->fetch_refspec_nr; i++)
537 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
538 return error("Could not append '%s' to '%s'",
539 remote->fetch_refspec[i], buf.buf);
540 if (remote->origin == REMOTE_REMOTES)
541 path = git_path("remotes/%s", remote->name);
542 else if (remote->origin == REMOTE_BRANCHES)
543 path = git_path("branches/%s", remote->name);
544 if (path)
545 unlink_or_warn(path);
546 return 0;
549 static int mv(int argc, const char **argv)
551 struct option options[] = {
552 OPT_END()
554 struct remote *oldremote, *newremote;
555 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
556 struct string_list remote_branches = { NULL, 0, 0, 0 };
557 struct rename_info rename;
558 int i;
560 if (argc != 3)
561 usage_with_options(builtin_remote_rename_usage, options);
563 rename.old = argv[1];
564 rename.new = argv[2];
565 rename.remote_branches = &remote_branches;
567 oldremote = remote_get(rename.old);
568 if (!oldremote)
569 die("No such remote: %s", rename.old);
571 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
572 return migrate_file(oldremote);
574 newremote = remote_get(rename.new);
575 if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
576 die("remote %s already exists.", rename.new);
578 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
579 if (!valid_fetch_refspec(buf.buf))
580 die("'%s' is not a valid remote name", rename.new);
582 strbuf_reset(&buf);
583 strbuf_addf(&buf, "remote.%s", rename.old);
584 strbuf_addf(&buf2, "remote.%s", rename.new);
585 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
586 return error("Could not rename config section '%s' to '%s'",
587 buf.buf, buf2.buf);
589 strbuf_reset(&buf);
590 strbuf_addf(&buf, "remote.%s.fetch", rename.new);
591 if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
592 return error("Could not remove config section '%s'", buf.buf);
593 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
594 char *ptr;
596 strbuf_reset(&buf2);
597 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
598 ptr = strstr(buf2.buf, rename.old);
599 if (ptr)
600 strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
601 rename.new, strlen(rename.new));
602 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
603 return error("Could not append '%s'", buf.buf);
606 read_branches();
607 for (i = 0; i < branch_list.nr; i++) {
608 struct string_list_item *item = branch_list.items + i;
609 struct branch_info *info = item->util;
610 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
611 strbuf_reset(&buf);
612 strbuf_addf(&buf, "branch.%s.remote", item->string);
613 if (git_config_set(buf.buf, rename.new)) {
614 return error("Could not set '%s'", buf.buf);
620 * First remove symrefs, then rename the rest, finally create
621 * the new symrefs.
623 for_each_ref(read_remote_branches, &rename);
624 for (i = 0; i < remote_branches.nr; i++) {
625 struct string_list_item *item = remote_branches.items + i;
626 int flag = 0;
627 unsigned char sha1[20];
629 resolve_ref(item->string, sha1, 1, &flag);
630 if (!(flag & REF_ISSYMREF))
631 continue;
632 if (delete_ref(item->string, NULL, REF_NODEREF))
633 die("deleting '%s' failed", item->string);
635 for (i = 0; i < remote_branches.nr; i++) {
636 struct string_list_item *item = remote_branches.items + i;
638 if (item->util)
639 continue;
640 strbuf_reset(&buf);
641 strbuf_addstr(&buf, item->string);
642 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
643 rename.new, strlen(rename.new));
644 strbuf_reset(&buf2);
645 strbuf_addf(&buf2, "remote: renamed %s to %s",
646 item->string, buf.buf);
647 if (rename_ref(item->string, buf.buf, buf2.buf))
648 die("renaming '%s' failed", item->string);
650 for (i = 0; i < remote_branches.nr; i++) {
651 struct string_list_item *item = remote_branches.items + i;
653 if (!item->util)
654 continue;
655 strbuf_reset(&buf);
656 strbuf_addstr(&buf, item->string);
657 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
658 rename.new, strlen(rename.new));
659 strbuf_reset(&buf2);
660 strbuf_addstr(&buf2, item->util);
661 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
662 rename.new, strlen(rename.new));
663 strbuf_reset(&buf3);
664 strbuf_addf(&buf3, "remote: renamed %s to %s",
665 item->string, buf.buf);
666 if (create_symref(buf.buf, buf2.buf, buf3.buf))
667 die("creating '%s' failed", buf.buf);
669 return 0;
672 static int remove_branches(struct string_list *branches)
674 int i, result = 0;
675 for (i = 0; i < branches->nr; i++) {
676 struct string_list_item *item = branches->items + i;
677 const char *refname = item->string;
678 unsigned char *sha1 = item->util;
680 if (delete_ref(refname, sha1, 0))
681 result |= error("Could not remove branch %s", refname);
683 return result;
686 static int rm(int argc, const char **argv)
688 struct option options[] = {
689 OPT_END()
691 struct remote *remote;
692 struct strbuf buf = STRBUF_INIT;
693 struct known_remotes known_remotes = { NULL, NULL };
694 struct string_list branches = { NULL, 0, 0, 1 };
695 struct string_list skipped = { NULL, 0, 0, 1 };
696 struct branches_for_remote cb_data = {
697 NULL, &branches, &skipped, &known_remotes
699 int i, result;
701 if (argc != 2)
702 usage_with_options(builtin_remote_rm_usage, options);
704 remote = remote_get(argv[1]);
705 if (!remote)
706 die("No such remote: %s", argv[1]);
708 known_remotes.to_delete = remote;
709 for_each_remote(add_known_remote, &known_remotes);
711 strbuf_addf(&buf, "remote.%s", remote->name);
712 if (git_config_rename_section(buf.buf, NULL) < 1)
713 return error("Could not remove config section '%s'", buf.buf);
715 read_branches();
716 for (i = 0; i < branch_list.nr; i++) {
717 struct string_list_item *item = branch_list.items + i;
718 struct branch_info *info = item->util;
719 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
720 const char *keys[] = { "remote", "merge", NULL }, **k;
721 for (k = keys; *k; k++) {
722 strbuf_reset(&buf);
723 strbuf_addf(&buf, "branch.%s.%s",
724 item->string, *k);
725 if (git_config_set(buf.buf, NULL)) {
726 strbuf_release(&buf);
727 return -1;
734 * We cannot just pass a function to for_each_ref() which deletes
735 * the branches one by one, since for_each_ref() relies on cached
736 * refs, which are invalidated when deleting a branch.
738 cb_data.remote = remote;
739 result = for_each_ref(add_branch_for_removal, &cb_data);
740 strbuf_release(&buf);
742 if (!result)
743 result = remove_branches(&branches);
744 string_list_clear(&branches, 1);
746 if (skipped.nr) {
747 fprintf(stderr, skipped.nr == 1 ?
748 "Note: A non-remote branch was not removed; "
749 "to delete it, use:\n" :
750 "Note: Non-remote branches were not removed; "
751 "to delete them, use:\n");
752 for (i = 0; i < skipped.nr; i++)
753 fprintf(stderr, " git branch -d %s\n",
754 skipped.items[i].string);
756 string_list_clear(&skipped, 0);
758 return result;
761 static void clear_push_info(void *util, const char *string)
763 struct push_info *info = util;
764 free(info->dest);
765 free(info);
768 static void free_remote_ref_states(struct ref_states *states)
770 string_list_clear(&states->new, 0);
771 string_list_clear(&states->stale, 0);
772 string_list_clear(&states->tracked, 0);
773 string_list_clear(&states->heads, 0);
774 string_list_clear_func(&states->push, clear_push_info);
777 static int append_ref_to_tracked_list(const char *refname,
778 const unsigned char *sha1, int flags, void *cb_data)
780 struct ref_states *states = cb_data;
781 struct refspec refspec;
783 if (flags & REF_ISSYMREF)
784 return 0;
786 memset(&refspec, 0, sizeof(refspec));
787 refspec.dst = (char *)refname;
788 if (!remote_find_tracking(states->remote, &refspec))
789 string_list_append(abbrev_branch(refspec.src), &states->tracked);
791 return 0;
794 static int get_remote_ref_states(const char *name,
795 struct ref_states *states,
796 int query)
798 struct transport *transport;
799 const struct ref *remote_refs;
801 states->remote = remote_get(name);
802 if (!states->remote)
803 return error("No such remote: %s", name);
805 read_branches();
807 if (query) {
808 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
809 states->remote->url[0] : NULL);
810 remote_refs = transport_get_remote_refs(transport);
811 transport_disconnect(transport);
813 states->queried = 1;
814 if (query & GET_REF_STATES)
815 get_ref_states(remote_refs, states);
816 if (query & GET_HEAD_NAMES)
817 get_head_names(remote_refs, states);
818 if (query & GET_PUSH_REF_STATES)
819 get_push_ref_states(remote_refs, states);
820 } else {
821 for_each_ref(append_ref_to_tracked_list, states);
822 sort_string_list(&states->tracked);
823 get_push_ref_states_noquery(states);
826 return 0;
829 struct show_info {
830 struct string_list *list;
831 struct ref_states *states;
832 int width, width2;
833 int any_rebase;
836 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
838 struct show_info *info = cb_data;
839 int n = strlen(item->string);
840 if (n > info->width)
841 info->width = n;
842 string_list_insert(item->string, info->list);
843 return 0;
846 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
848 struct show_info *info = cb_data;
849 struct ref_states *states = info->states;
850 const char *name = item->string;
852 if (states->queried) {
853 const char *fmt = "%s";
854 const char *arg = "";
855 if (string_list_has_string(&states->new, name)) {
856 fmt = " new (next fetch will store in remotes/%s)";
857 arg = states->remote->name;
858 } else if (string_list_has_string(&states->tracked, name))
859 arg = " tracked";
860 else if (string_list_has_string(&states->stale, name))
861 arg = " stale (use 'git remote prune' to remove)";
862 else
863 arg = " ???";
864 printf(" %-*s", info->width, name);
865 printf(fmt, arg);
866 printf("\n");
867 } else
868 printf(" %s\n", name);
870 return 0;
873 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
875 struct show_info *show_info = cb_data;
876 struct ref_states *states = show_info->states;
877 struct branch_info *branch_info = branch_item->util;
878 struct string_list_item *item;
879 int n;
881 if (!branch_info->merge.nr || !branch_info->remote_name ||
882 strcmp(states->remote->name, branch_info->remote_name))
883 return 0;
884 if ((n = strlen(branch_item->string)) > show_info->width)
885 show_info->width = n;
886 if (branch_info->rebase)
887 show_info->any_rebase = 1;
889 item = string_list_insert(branch_item->string, show_info->list);
890 item->util = branch_info;
892 return 0;
895 static int show_local_info_item(struct string_list_item *item, void *cb_data)
897 struct show_info *show_info = cb_data;
898 struct branch_info *branch_info = item->util;
899 struct string_list *merge = &branch_info->merge;
900 const char *also;
901 int i;
903 if (branch_info->rebase && branch_info->merge.nr > 1) {
904 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
905 item->string);
906 return 0;
909 printf(" %-*s ", show_info->width, item->string);
910 if (branch_info->rebase) {
911 printf("rebases onto remote %s\n", merge->items[0].string);
912 return 0;
913 } else if (show_info->any_rebase) {
914 printf(" merges with remote %s\n", merge->items[0].string);
915 also = " and with remote";
916 } else {
917 printf("merges with remote %s\n", merge->items[0].string);
918 also = " and with remote";
920 for (i = 1; i < merge->nr; i++)
921 printf(" %-*s %s %s\n", show_info->width, "", also,
922 merge->items[i].string);
924 return 0;
927 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
929 struct show_info *show_info = cb_data;
930 struct push_info *push_info = push_item->util;
931 struct string_list_item *item;
932 int n;
933 if ((n = strlen(push_item->string)) > show_info->width)
934 show_info->width = n;
935 if ((n = strlen(push_info->dest)) > show_info->width2)
936 show_info->width2 = n;
937 item = string_list_append(push_item->string, show_info->list);
938 item->util = push_item->util;
939 return 0;
943 * Sorting comparison for a string list that has push_info
944 * structs in its util field
946 static int cmp_string_with_push(const void *va, const void *vb)
948 const struct string_list_item *a = va;
949 const struct string_list_item *b = vb;
950 const struct push_info *a_push = a->util;
951 const struct push_info *b_push = b->util;
952 int cmp = strcmp(a->string, b->string);
953 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
956 static int show_push_info_item(struct string_list_item *item, void *cb_data)
958 struct show_info *show_info = cb_data;
959 struct push_info *push_info = item->util;
960 char *src = item->string, *status = NULL;
962 switch (push_info->status) {
963 case PUSH_STATUS_CREATE:
964 status = "create";
965 break;
966 case PUSH_STATUS_DELETE:
967 status = "delete";
968 src = "(none)";
969 break;
970 case PUSH_STATUS_UPTODATE:
971 status = "up to date";
972 break;
973 case PUSH_STATUS_FASTFORWARD:
974 status = "fast-forwardable";
975 break;
976 case PUSH_STATUS_OUTOFDATE:
977 status = "local out of date";
978 break;
979 case PUSH_STATUS_NOTQUERIED:
980 break;
982 if (status)
983 printf(" %-*s %s to %-*s (%s)\n", show_info->width, src,
984 push_info->forced ? "forces" : "pushes",
985 show_info->width2, push_info->dest, status);
986 else
987 printf(" %-*s %s to %s\n", show_info->width, src,
988 push_info->forced ? "forces" : "pushes",
989 push_info->dest);
990 return 0;
993 static int show(int argc, const char **argv)
995 int no_query = 0, result = 0, query_flag = 0;
996 struct option options[] = {
997 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
998 OPT_END()
1000 struct ref_states states;
1001 struct string_list info_list = { NULL, 0, 0, 0 };
1002 struct show_info info;
1004 argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1007 if (argc < 1)
1008 return show_all();
1010 if (!no_query)
1011 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1013 memset(&states, 0, sizeof(states));
1014 memset(&info, 0, sizeof(info));
1015 info.states = &states;
1016 info.list = &info_list;
1017 for (; argc; argc--, argv++) {
1018 int i;
1019 const char **url;
1020 int url_nr;
1022 get_remote_ref_states(*argv, &states, query_flag);
1024 printf("* remote %s\n", *argv);
1025 printf(" Fetch URL: %s\n", states.remote->url_nr > 0 ?
1026 states.remote->url[0] : "(no URL)");
1027 if (states.remote->pushurl_nr) {
1028 url = states.remote->pushurl;
1029 url_nr = states.remote->pushurl_nr;
1030 } else {
1031 url = states.remote->url;
1032 url_nr = states.remote->url_nr;
1034 for (i=0; i < url_nr; i++)
1035 printf(" Push URL: %s\n", url[i]);
1036 if (!i)
1037 printf(" Push URL: %s\n", "(no URL)");
1038 if (no_query)
1039 printf(" HEAD branch: (not queried)\n");
1040 else if (!states.heads.nr)
1041 printf(" HEAD branch: (unknown)\n");
1042 else if (states.heads.nr == 1)
1043 printf(" HEAD branch: %s\n", states.heads.items[0].string);
1044 else {
1045 printf(" HEAD branch (remote HEAD is ambiguous,"
1046 " may be one of the following):\n");
1047 for (i = 0; i < states.heads.nr; i++)
1048 printf(" %s\n", states.heads.items[i].string);
1051 /* remote branch info */
1052 info.width = 0;
1053 for_each_string_list(add_remote_to_show_info, &states.new, &info);
1054 for_each_string_list(add_remote_to_show_info, &states.tracked, &info);
1055 for_each_string_list(add_remote_to_show_info, &states.stale, &info);
1056 if (info.list->nr)
1057 printf(" Remote branch%s:%s\n",
1058 info.list->nr > 1 ? "es" : "",
1059 no_query ? " (status not queried)" : "");
1060 for_each_string_list(show_remote_info_item, info.list, &info);
1061 string_list_clear(info.list, 0);
1063 /* git pull info */
1064 info.width = 0;
1065 info.any_rebase = 0;
1066 for_each_string_list(add_local_to_show_info, &branch_list, &info);
1067 if (info.list->nr)
1068 printf(" Local branch%s configured for 'git pull':\n",
1069 info.list->nr > 1 ? "es" : "");
1070 for_each_string_list(show_local_info_item, info.list, &info);
1071 string_list_clear(info.list, 0);
1073 /* git push info */
1074 if (states.remote->mirror)
1075 printf(" Local refs will be mirrored by 'git push'\n");
1077 info.width = info.width2 = 0;
1078 for_each_string_list(add_push_to_show_info, &states.push, &info);
1079 qsort(info.list->items, info.list->nr,
1080 sizeof(*info.list->items), cmp_string_with_push);
1081 if (info.list->nr)
1082 printf(" Local ref%s configured for 'git push'%s:\n",
1083 info.list->nr > 1 ? "s" : "",
1084 no_query ? " (status not queried)" : "");
1085 for_each_string_list(show_push_info_item, info.list, &info);
1086 string_list_clear(info.list, 0);
1088 free_remote_ref_states(&states);
1091 return result;
1094 static int set_head(int argc, const char **argv)
1096 int i, opt_a = 0, opt_d = 0, result = 0;
1097 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1098 char *head_name = NULL;
1100 struct option options[] = {
1101 OPT_BOOLEAN('a', "auto", &opt_a,
1102 "set refs/remotes/<name>/HEAD according to remote"),
1103 OPT_BOOLEAN('d', "delete", &opt_d,
1104 "delete refs/remotes/<name>/HEAD"),
1105 OPT_END()
1107 argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1109 if (argc)
1110 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1112 if (!opt_a && !opt_d && argc == 2) {
1113 head_name = xstrdup(argv[1]);
1114 } else if (opt_a && !opt_d && argc == 1) {
1115 struct ref_states states;
1116 memset(&states, 0, sizeof(states));
1117 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1118 if (!states.heads.nr)
1119 result |= error("Cannot determine remote HEAD");
1120 else if (states.heads.nr > 1) {
1121 result |= error("Multiple remote HEAD branches. "
1122 "Please choose one explicitly with:");
1123 for (i = 0; i < states.heads.nr; i++)
1124 fprintf(stderr, " git remote set-head %s %s\n",
1125 argv[0], states.heads.items[i].string);
1126 } else
1127 head_name = xstrdup(states.heads.items[0].string);
1128 free_remote_ref_states(&states);
1129 } else if (opt_d && !opt_a && argc == 1) {
1130 if (delete_ref(buf.buf, NULL, REF_NODEREF))
1131 result |= error("Could not delete %s", buf.buf);
1132 } else
1133 usage_with_options(builtin_remote_sethead_usage, options);
1135 if (head_name) {
1136 unsigned char sha1[20];
1137 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1138 /* make sure it's valid */
1139 if (!resolve_ref(buf2.buf, sha1, 1, NULL))
1140 result |= error("Not a valid ref: %s", buf2.buf);
1141 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1142 result |= error("Could not setup %s", buf.buf);
1143 if (opt_a)
1144 printf("%s/HEAD set to %s\n", argv[0], head_name);
1145 free(head_name);
1148 strbuf_release(&buf);
1149 strbuf_release(&buf2);
1150 return result;
1153 static int prune(int argc, const char **argv)
1155 int dry_run = 0, result = 0;
1156 struct option options[] = {
1157 OPT__DRY_RUN(&dry_run),
1158 OPT_END()
1161 argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1164 if (argc < 1)
1165 usage_with_options(builtin_remote_prune_usage, options);
1167 for (; argc; argc--, argv++)
1168 result |= prune_remote(*argv, dry_run);
1170 return result;
1173 static int prune_remote(const char *remote, int dry_run)
1175 int result = 0, i;
1176 struct ref_states states;
1177 const char *dangling_msg = dry_run
1178 ? " %s will become dangling!\n"
1179 : " %s has become dangling!\n";
1181 memset(&states, 0, sizeof(states));
1182 get_remote_ref_states(remote, &states, GET_REF_STATES);
1184 if (states.stale.nr) {
1185 printf("Pruning %s\n", remote);
1186 printf("URL: %s\n",
1187 states.remote->url_nr
1188 ? states.remote->url[0]
1189 : "(no URL)");
1192 for (i = 0; i < states.stale.nr; i++) {
1193 const char *refname = states.stale.items[i].util;
1195 if (!dry_run)
1196 result |= delete_ref(refname, NULL, 0);
1198 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
1199 abbrev_ref(refname, "refs/remotes/"));
1200 warn_dangling_symref(stdout, dangling_msg, refname);
1203 free_remote_ref_states(&states);
1204 return result;
1207 static int get_remote_default(const char *key, const char *value, void *priv)
1209 if (strcmp(key, "remotes.default") == 0) {
1210 int *found = priv;
1211 *found = 1;
1213 return 0;
1216 static int update(int argc, const char **argv)
1218 int i, prune = 0;
1219 struct option options[] = {
1220 OPT_BOOLEAN('p', "prune", &prune,
1221 "prune remotes after fetching"),
1222 OPT_END()
1224 const char **fetch_argv;
1225 int fetch_argc = 0;
1226 int default_defined = 0;
1228 fetch_argv = xmalloc(sizeof(char *) * (argc+5));
1230 argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1231 PARSE_OPT_KEEP_ARGV0);
1233 fetch_argv[fetch_argc++] = "fetch";
1235 if (prune)
1236 fetch_argv[fetch_argc++] = "--prune";
1237 if (verbose)
1238 fetch_argv[fetch_argc++] = "-v";
1239 if (argc < 2) {
1240 fetch_argv[fetch_argc++] = "default";
1241 } else {
1242 fetch_argv[fetch_argc++] = "--multiple";
1243 for (i = 1; i < argc; i++)
1244 fetch_argv[fetch_argc++] = argv[i];
1247 if (strcmp(fetch_argv[fetch_argc-1], "default") == 0) {
1248 git_config(get_remote_default, &default_defined);
1249 if (!default_defined)
1250 fetch_argv[fetch_argc-1] = "--all";
1253 fetch_argv[fetch_argc] = NULL;
1255 return run_command_v_opt(fetch_argv, RUN_GIT_CMD);
1258 static int get_one_entry(struct remote *remote, void *priv)
1260 struct string_list *list = priv;
1261 struct strbuf url_buf = STRBUF_INIT;
1262 const char **url;
1263 int i, url_nr;
1265 if (remote->url_nr > 0) {
1266 strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
1267 string_list_append(remote->name, list)->util =
1268 strbuf_detach(&url_buf, NULL);
1269 } else
1270 string_list_append(remote->name, list)->util = NULL;
1271 if (remote->pushurl_nr) {
1272 url = remote->pushurl;
1273 url_nr = remote->pushurl_nr;
1274 } else {
1275 url = remote->url;
1276 url_nr = remote->url_nr;
1278 for (i = 0; i < url_nr; i++)
1280 strbuf_addf(&url_buf, "%s (push)", url[i]);
1281 string_list_append(remote->name, list)->util =
1282 strbuf_detach(&url_buf, NULL);
1285 return 0;
1288 static int show_all(void)
1290 struct string_list list = { NULL, 0, 0 };
1291 int result;
1293 list.strdup_strings = 1;
1294 result = for_each_remote(get_one_entry, &list);
1296 if (!result) {
1297 int i;
1299 sort_string_list(&list);
1300 for (i = 0; i < list.nr; i++) {
1301 struct string_list_item *item = list.items + i;
1302 if (verbose)
1303 printf("%s\t%s\n", item->string,
1304 item->util ? (const char *)item->util : "");
1305 else {
1306 if (i && !strcmp((item - 1)->string, item->string))
1307 continue;
1308 printf("%s\n", item->string);
1312 string_list_clear(&list, 1);
1313 return result;
1316 int cmd_remote(int argc, const char **argv, const char *prefix)
1318 struct option options[] = {
1319 OPT_BOOLEAN('v', "verbose", &verbose, "be verbose; must be placed before a subcommand"),
1320 OPT_END()
1322 int result;
1324 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1325 PARSE_OPT_STOP_AT_NON_OPTION);
1327 if (argc < 1)
1328 result = show_all();
1329 else if (!strcmp(argv[0], "add"))
1330 result = add(argc, argv);
1331 else if (!strcmp(argv[0], "rename"))
1332 result = mv(argc, argv);
1333 else if (!strcmp(argv[0], "rm"))
1334 result = rm(argc, argv);
1335 else if (!strcmp(argv[0], "set-head"))
1336 result = set_head(argc, argv);
1337 else if (!strcmp(argv[0], "show"))
1338 result = show(argc, argv);
1339 else if (!strcmp(argv[0], "prune"))
1340 result = prune(argc, argv);
1341 else if (!strcmp(argv[0], "update"))
1342 result = update(argc, argv);
1343 else {
1344 error("Unknown subcommand: %s", argv[0]);
1345 usage_with_options(builtin_remote_usage, options);
1348 return result ? 1 : 0;