2 #include "parse-options.h"
5 #include "string-list.h"
7 #include "run-command.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 "git remote set-branches [--add] <name> <branch>...",
20 "git remote set-url <name> <newurl> [<oldurl>]",
21 "git remote set-url --add <name> <newurl>",
22 "git remote set-url --delete <name> <url>",
26 static const char * const builtin_remote_add_usage[] = {
27 "git remote add [<options>] <name> <url>",
31 static const char * const builtin_remote_rename_usage[] = {
32 "git remote rename <old> <new>",
36 static const char * const builtin_remote_rm_usage[] = {
37 "git remote rm <name>",
41 static const char * const builtin_remote_sethead_usage[] = {
42 "git remote set-head <name> (-a | -d | <branch>])",
46 static const char * const builtin_remote_setbranches_usage[] = {
47 "git remote set-branches <name> <branch>...",
48 "git remote set-branches --add <name> <branch>...",
52 static const char * const builtin_remote_show_usage[] = {
53 "git remote show [<options>] <name>",
57 static const char * const builtin_remote_prune_usage[] = {
58 "git remote prune [<options>] <name>",
62 static const char * const builtin_remote_update_usage[] = {
63 "git remote update [<options>] [<group> | <remote>]...",
67 static const char * const builtin_remote_seturl_usage[] = {
68 "git remote set-url [--push] <name> <newurl> [<oldurl>]",
69 "git remote set-url --add <name> <newurl>",
70 "git remote set-url --delete <name> <url>",
74 #define GET_REF_STATES (1<<0)
75 #define GET_HEAD_NAMES (1<<1)
76 #define GET_PUSH_REF_STATES (1<<2)
80 static int show_all(void);
81 static int prune_remote(const char *remote, int dry_run);
83 static inline int postfixcmp(const char *string, const char *postfix)
85 int len1 = strlen(string), len2 = strlen(postfix);
88 return strcmp(string + len1 - len2, postfix);
91 static int opt_parse_track(const struct option *opt, const char *arg, int not)
93 struct string_list *list = opt->value;
95 string_list_clear(list, 0);
97 string_list_append(arg, list);
101 static int fetch_remote(const char *name)
103 const char *argv[] = { "fetch", name, NULL, NULL };
108 printf("Updating %s\n", name);
109 if (run_command_v_opt(argv, RUN_GIT_CMD))
110 return error("Could not fetch %s", name);
114 static int add_branch(const char *key, const char *branchname,
115 const char *remotename, int mirror, struct strbuf *tmp)
118 strbuf_addch(tmp, '+');
120 strbuf_addf(tmp, "refs/%s:refs/%s",
121 branchname, branchname);
123 strbuf_addf(tmp, "refs/heads/%s:refs/remotes/%s/%s",
124 branchname, remotename, branchname);
125 return git_config_set_multivar(key, tmp->buf, "^$", 0);
128 static int add(int argc, const char **argv)
130 int fetch = 0, mirror = 0;
131 struct string_list track = { NULL, 0, 0 };
132 const char *master = NULL;
133 struct remote *remote;
134 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
135 const char *name, *url;
138 struct option options[] = {
139 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
140 OPT_CALLBACK('t', "track", &track, "branch",
141 "branch(es) to track", opt_parse_track),
142 OPT_STRING('m', "master", &master, "branch", "master branch"),
143 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
147 argc = parse_options(argc, argv, NULL, options, builtin_remote_add_usage,
151 usage_with_options(builtin_remote_add_usage, options);
156 remote = remote_get(name);
157 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
158 remote->fetch_refspec_nr))
159 die("remote %s already exists.", name);
161 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
162 if (!valid_fetch_refspec(buf2.buf))
163 die("'%s' is not a valid remote name", name);
165 strbuf_addf(&buf, "remote.%s.url", name);
166 if (git_config_set(buf.buf, url))
170 strbuf_addf(&buf, "remote.%s.fetch", name);
173 string_list_append("*", &track);
174 for (i = 0; i < track.nr; i++) {
175 if (add_branch(buf.buf, track.items[i].string,
176 name, mirror, &buf2))
182 strbuf_addf(&buf, "remote.%s.mirror", name);
183 if (git_config_set(buf.buf, "true"))
187 if (fetch && fetch_remote(name))
192 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
195 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
197 if (create_symref(buf.buf, buf2.buf, "remote add"))
198 return error("Could not setup master '%s'", master);
201 strbuf_release(&buf);
202 strbuf_release(&buf2);
203 string_list_clear(&track, 0);
210 struct string_list merge;
214 static struct string_list branch_list;
216 static const char *abbrev_ref(const char *name, const char *prefix)
218 const char *abbrev = skip_prefix(name, prefix);
223 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
225 static int config_read_branches(const char *key, const char *value, void *cb)
227 if (!prefixcmp(key, "branch.")) {
228 const char *orig_key = key;
230 struct string_list_item *item;
231 struct branch_info *info;
232 enum { REMOTE, MERGE, REBASE } type;
235 if (!postfixcmp(key, ".remote")) {
236 name = xstrndup(key, strlen(key) - 7);
238 } else if (!postfixcmp(key, ".merge")) {
239 name = xstrndup(key, strlen(key) - 6);
241 } else if (!postfixcmp(key, ".rebase")) {
242 name = xstrndup(key, strlen(key) - 7);
247 item = string_list_insert(name, &branch_list);
250 item->util = xcalloc(sizeof(struct branch_info), 1);
252 if (type == REMOTE) {
253 if (info->remote_name)
254 warning("more than one %s", orig_key);
255 info->remote_name = xstrdup(value);
256 } else if (type == MERGE) {
257 char *space = strchr(value, ' ');
258 value = abbrev_branch(value);
261 merge = xstrndup(value, space - value);
262 string_list_append(merge, &info->merge);
263 value = abbrev_branch(space + 1);
264 space = strchr(value, ' ');
266 string_list_append(xstrdup(value), &info->merge);
268 info->rebase = git_config_bool(orig_key, value);
273 static void read_branches(void)
277 git_config(config_read_branches, NULL);
281 struct remote *remote;
282 struct string_list new, stale, tracked, heads, push;
286 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
288 struct ref *fetch_map = NULL, **tail = &fetch_map;
289 struct ref *ref, *stale_refs;
292 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
293 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
294 die("Could not get fetch map for refspec %s",
295 states->remote->fetch_refspec[i]);
297 states->new.strdup_strings = 1;
298 states->tracked.strdup_strings = 1;
299 states->stale.strdup_strings = 1;
300 for (ref = fetch_map; ref; ref = ref->next) {
301 unsigned char sha1[20];
302 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
303 string_list_append(abbrev_branch(ref->name), &states->new);
305 string_list_append(abbrev_branch(ref->name), &states->tracked);
307 stale_refs = get_stale_heads(states->remote, fetch_map);
308 for (ref = stale_refs; ref; ref = ref->next) {
309 struct string_list_item *item =
310 string_list_append(abbrev_branch(ref->name), &states->stale);
311 item->util = xstrdup(ref->name);
313 free_refs(stale_refs);
314 free_refs(fetch_map);
316 sort_string_list(&states->new);
317 sort_string_list(&states->tracked);
318 sort_string_list(&states->stale);
327 PUSH_STATUS_CREATE = 0,
329 PUSH_STATUS_UPTODATE,
330 PUSH_STATUS_FASTFORWARD,
331 PUSH_STATUS_OUTOFDATE,
332 PUSH_STATUS_NOTQUERIED,
336 static int get_push_ref_states(const struct ref *remote_refs,
337 struct ref_states *states)
339 struct remote *remote = states->remote;
340 struct ref *ref, *local_refs, *push_map;
344 local_refs = get_local_heads();
345 push_map = copy_ref_list(remote_refs);
347 match_refs(local_refs, &push_map, remote->push_refspec_nr,
348 remote->push_refspec, MATCH_REFS_NONE);
350 states->push.strdup_strings = 1;
351 for (ref = push_map; ref; ref = ref->next) {
352 struct string_list_item *item;
353 struct push_info *info;
357 hashcpy(ref->new_sha1, ref->peer_ref->new_sha1);
359 item = string_list_append(abbrev_branch(ref->peer_ref->name),
361 item->util = xcalloc(sizeof(struct push_info), 1);
363 info->forced = ref->force;
364 info->dest = xstrdup(abbrev_branch(ref->name));
366 if (is_null_sha1(ref->new_sha1)) {
367 info->status = PUSH_STATUS_DELETE;
368 } else if (!hashcmp(ref->old_sha1, ref->new_sha1))
369 info->status = PUSH_STATUS_UPTODATE;
370 else if (is_null_sha1(ref->old_sha1))
371 info->status = PUSH_STATUS_CREATE;
372 else if (has_sha1_file(ref->old_sha1) &&
373 ref_newer(ref->new_sha1, ref->old_sha1))
374 info->status = PUSH_STATUS_FASTFORWARD;
376 info->status = PUSH_STATUS_OUTOFDATE;
378 free_refs(local_refs);
383 static int get_push_ref_states_noquery(struct ref_states *states)
386 struct remote *remote = states->remote;
387 struct string_list_item *item;
388 struct push_info *info;
393 states->push.strdup_strings = 1;
394 if (!remote->push_refspec_nr) {
395 item = string_list_append("(matching)", &states->push);
396 info = item->util = xcalloc(sizeof(struct push_info), 1);
397 info->status = PUSH_STATUS_NOTQUERIED;
398 info->dest = xstrdup(item->string);
400 for (i = 0; i < remote->push_refspec_nr; i++) {
401 struct refspec *spec = remote->push + i;
403 item = string_list_append("(matching)", &states->push);
404 else if (strlen(spec->src))
405 item = string_list_append(spec->src, &states->push);
407 item = string_list_append("(delete)", &states->push);
409 info = item->util = xcalloc(sizeof(struct push_info), 1);
410 info->forced = spec->force;
411 info->status = PUSH_STATUS_NOTQUERIED;
412 info->dest = xstrdup(spec->dst ? spec->dst : item->string);
417 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
419 struct ref *ref, *matches;
420 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
421 struct refspec refspec;
425 refspec.src = refspec.dst = "refs/heads/*";
426 states->heads.strdup_strings = 1;
427 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
428 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
430 for (ref = matches; ref; ref = ref->next)
431 string_list_append(abbrev_branch(ref->name), &states->heads);
433 free_refs(fetch_map);
439 struct known_remote {
440 struct known_remote *next;
441 struct remote *remote;
444 struct known_remotes {
445 struct remote *to_delete;
446 struct known_remote *list;
449 static int add_known_remote(struct remote *remote, void *cb_data)
451 struct known_remotes *all = cb_data;
452 struct known_remote *r;
454 if (!strcmp(all->to_delete->name, remote->name))
457 r = xmalloc(sizeof(*r));
464 struct branches_for_remote {
465 struct remote *remote;
466 struct string_list *branches, *skipped;
467 struct known_remotes *keep;
470 static int add_branch_for_removal(const char *refname,
471 const unsigned char *sha1, int flags, void *cb_data)
473 struct branches_for_remote *branches = cb_data;
474 struct refspec refspec;
475 struct string_list_item *item;
476 struct known_remote *kr;
478 memset(&refspec, 0, sizeof(refspec));
479 refspec.dst = (char *)refname;
480 if (remote_find_tracking(branches->remote, &refspec))
483 /* don't delete a branch if another remote also uses it */
484 for (kr = branches->keep->list; kr; kr = kr->next) {
485 memset(&refspec, 0, sizeof(refspec));
486 refspec.dst = (char *)refname;
487 if (!remote_find_tracking(kr->remote, &refspec))
491 /* don't delete non-remote refs */
492 if (prefixcmp(refname, "refs/remotes")) {
493 /* advise user how to delete local branches */
494 if (!prefixcmp(refname, "refs/heads/"))
495 string_list_append(abbrev_branch(refname),
497 /* silently skip over other non-remote refs */
501 /* make sure that symrefs are deleted */
502 if (flags & REF_ISSYMREF)
503 return unlink(git_path("%s", refname));
505 item = string_list_append(refname, branches->branches);
506 item->util = xmalloc(20);
507 hashcpy(item->util, sha1);
515 struct string_list *remote_branches;
518 static int read_remote_branches(const char *refname,
519 const unsigned char *sha1, int flags, void *cb_data)
521 struct rename_info *rename = cb_data;
522 struct strbuf buf = STRBUF_INIT;
523 struct string_list_item *item;
525 unsigned char orig_sha1[20];
528 strbuf_addf(&buf, "refs/remotes/%s", rename->old);
529 if (!prefixcmp(refname, buf.buf)) {
530 item = string_list_append(xstrdup(refname), rename->remote_branches);
531 symref = resolve_ref(refname, orig_sha1, 1, &flag);
532 if (flag & REF_ISSYMREF)
533 item->util = xstrdup(symref);
541 static int migrate_file(struct remote *remote)
543 struct strbuf buf = STRBUF_INIT;
547 strbuf_addf(&buf, "remote.%s.url", remote->name);
548 for (i = 0; i < remote->url_nr; i++)
549 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
550 return error("Could not append '%s' to '%s'",
551 remote->url[i], buf.buf);
553 strbuf_addf(&buf, "remote.%s.push", remote->name);
554 for (i = 0; i < remote->push_refspec_nr; i++)
555 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
556 return error("Could not append '%s' to '%s'",
557 remote->push_refspec[i], buf.buf);
559 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
560 for (i = 0; i < remote->fetch_refspec_nr; i++)
561 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
562 return error("Could not append '%s' to '%s'",
563 remote->fetch_refspec[i], buf.buf);
564 if (remote->origin == REMOTE_REMOTES)
565 path = git_path("remotes/%s", remote->name);
566 else if (remote->origin == REMOTE_BRANCHES)
567 path = git_path("branches/%s", remote->name);
569 unlink_or_warn(path);
573 static int mv(int argc, const char **argv)
575 struct option options[] = {
578 struct remote *oldremote, *newremote;
579 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
580 struct string_list remote_branches = { NULL, 0, 0, 0 };
581 struct rename_info rename;
585 usage_with_options(builtin_remote_rename_usage, options);
587 rename.old = argv[1];
588 rename.new = argv[2];
589 rename.remote_branches = &remote_branches;
591 oldremote = remote_get(rename.old);
593 die("No such remote: %s", rename.old);
595 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
596 return migrate_file(oldremote);
598 newremote = remote_get(rename.new);
599 if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
600 die("remote %s already exists.", rename.new);
602 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
603 if (!valid_fetch_refspec(buf.buf))
604 die("'%s' is not a valid remote name", rename.new);
607 strbuf_addf(&buf, "remote.%s", rename.old);
608 strbuf_addf(&buf2, "remote.%s", rename.new);
609 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
610 return error("Could not rename config section '%s' to '%s'",
614 strbuf_addf(&buf, "remote.%s.fetch", rename.new);
615 if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
616 return error("Could not remove config section '%s'", buf.buf);
617 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
621 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
622 ptr = strstr(buf2.buf, rename.old);
624 strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
625 rename.new, strlen(rename.new));
626 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
627 return error("Could not append '%s'", buf.buf);
631 for (i = 0; i < branch_list.nr; i++) {
632 struct string_list_item *item = branch_list.items + i;
633 struct branch_info *info = item->util;
634 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
636 strbuf_addf(&buf, "branch.%s.remote", item->string);
637 if (git_config_set(buf.buf, rename.new)) {
638 return error("Could not set '%s'", buf.buf);
644 * First remove symrefs, then rename the rest, finally create
647 for_each_ref(read_remote_branches, &rename);
648 for (i = 0; i < remote_branches.nr; i++) {
649 struct string_list_item *item = remote_branches.items + i;
651 unsigned char sha1[20];
653 resolve_ref(item->string, sha1, 1, &flag);
654 if (!(flag & REF_ISSYMREF))
656 if (delete_ref(item->string, NULL, REF_NODEREF))
657 die("deleting '%s' failed", item->string);
659 for (i = 0; i < remote_branches.nr; i++) {
660 struct string_list_item *item = remote_branches.items + i;
665 strbuf_addstr(&buf, item->string);
666 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
667 rename.new, strlen(rename.new));
669 strbuf_addf(&buf2, "remote: renamed %s to %s",
670 item->string, buf.buf);
671 if (rename_ref(item->string, buf.buf, buf2.buf))
672 die("renaming '%s' failed", item->string);
674 for (i = 0; i < remote_branches.nr; i++) {
675 struct string_list_item *item = remote_branches.items + i;
680 strbuf_addstr(&buf, item->string);
681 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
682 rename.new, strlen(rename.new));
684 strbuf_addstr(&buf2, item->util);
685 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
686 rename.new, strlen(rename.new));
688 strbuf_addf(&buf3, "remote: renamed %s to %s",
689 item->string, buf.buf);
690 if (create_symref(buf.buf, buf2.buf, buf3.buf))
691 die("creating '%s' failed", buf.buf);
696 static int remove_branches(struct string_list *branches)
699 for (i = 0; i < branches->nr; i++) {
700 struct string_list_item *item = branches->items + i;
701 const char *refname = item->string;
702 unsigned char *sha1 = item->util;
704 if (delete_ref(refname, sha1, 0))
705 result |= error("Could not remove branch %s", refname);
710 static int rm(int argc, const char **argv)
712 struct option options[] = {
715 struct remote *remote;
716 struct strbuf buf = STRBUF_INIT;
717 struct known_remotes known_remotes = { NULL, NULL };
718 struct string_list branches = { NULL, 0, 0, 1 };
719 struct string_list skipped = { NULL, 0, 0, 1 };
720 struct branches_for_remote cb_data = {
721 NULL, &branches, &skipped, &known_remotes
726 usage_with_options(builtin_remote_rm_usage, options);
728 remote = remote_get(argv[1]);
730 die("No such remote: %s", argv[1]);
732 known_remotes.to_delete = remote;
733 for_each_remote(add_known_remote, &known_remotes);
735 strbuf_addf(&buf, "remote.%s", remote->name);
736 if (git_config_rename_section(buf.buf, NULL) < 1)
737 return error("Could not remove config section '%s'", buf.buf);
740 for (i = 0; i < branch_list.nr; i++) {
741 struct string_list_item *item = branch_list.items + i;
742 struct branch_info *info = item->util;
743 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
744 const char *keys[] = { "remote", "merge", NULL }, **k;
745 for (k = keys; *k; k++) {
747 strbuf_addf(&buf, "branch.%s.%s",
749 if (git_config_set(buf.buf, NULL)) {
750 strbuf_release(&buf);
758 * We cannot just pass a function to for_each_ref() which deletes
759 * the branches one by one, since for_each_ref() relies on cached
760 * refs, which are invalidated when deleting a branch.
762 cb_data.remote = remote;
763 result = for_each_ref(add_branch_for_removal, &cb_data);
764 strbuf_release(&buf);
767 result = remove_branches(&branches);
768 string_list_clear(&branches, 1);
771 fprintf(stderr, skipped.nr == 1 ?
772 "Note: A non-remote branch was not removed; "
773 "to delete it, use:\n" :
774 "Note: Non-remote branches were not removed; "
775 "to delete them, use:\n");
776 for (i = 0; i < skipped.nr; i++)
777 fprintf(stderr, " git branch -d %s\n",
778 skipped.items[i].string);
780 string_list_clear(&skipped, 0);
785 static void clear_push_info(void *util, const char *string)
787 struct push_info *info = util;
792 static void free_remote_ref_states(struct ref_states *states)
794 string_list_clear(&states->new, 0);
795 string_list_clear(&states->stale, 1);
796 string_list_clear(&states->tracked, 0);
797 string_list_clear(&states->heads, 0);
798 string_list_clear_func(&states->push, clear_push_info);
801 static int append_ref_to_tracked_list(const char *refname,
802 const unsigned char *sha1, int flags, void *cb_data)
804 struct ref_states *states = cb_data;
805 struct refspec refspec;
807 if (flags & REF_ISSYMREF)
810 memset(&refspec, 0, sizeof(refspec));
811 refspec.dst = (char *)refname;
812 if (!remote_find_tracking(states->remote, &refspec))
813 string_list_append(abbrev_branch(refspec.src), &states->tracked);
818 static int get_remote_ref_states(const char *name,
819 struct ref_states *states,
822 struct transport *transport;
823 const struct ref *remote_refs;
825 states->remote = remote_get(name);
827 return error("No such remote: %s", name);
832 transport = transport_get(states->remote, states->remote->url_nr > 0 ?
833 states->remote->url[0] : NULL);
834 remote_refs = transport_get_remote_refs(transport);
835 transport_disconnect(transport);
838 if (query & GET_REF_STATES)
839 get_ref_states(remote_refs, states);
840 if (query & GET_HEAD_NAMES)
841 get_head_names(remote_refs, states);
842 if (query & GET_PUSH_REF_STATES)
843 get_push_ref_states(remote_refs, states);
845 for_each_ref(append_ref_to_tracked_list, states);
846 sort_string_list(&states->tracked);
847 get_push_ref_states_noquery(states);
854 struct string_list *list;
855 struct ref_states *states;
860 static int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
862 struct show_info *info = cb_data;
863 int n = strlen(item->string);
866 string_list_insert(item->string, info->list);
870 static int show_remote_info_item(struct string_list_item *item, void *cb_data)
872 struct show_info *info = cb_data;
873 struct ref_states *states = info->states;
874 const char *name = item->string;
876 if (states->queried) {
877 const char *fmt = "%s";
878 const char *arg = "";
879 if (string_list_has_string(&states->new, name)) {
880 fmt = " new (next fetch will store in remotes/%s)";
881 arg = states->remote->name;
882 } else if (string_list_has_string(&states->tracked, name))
884 else if (string_list_has_string(&states->stale, name))
885 arg = " stale (use 'git remote prune' to remove)";
888 printf(" %-*s", info->width, name);
892 printf(" %s\n", name);
897 static int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
899 struct show_info *show_info = cb_data;
900 struct ref_states *states = show_info->states;
901 struct branch_info *branch_info = branch_item->util;
902 struct string_list_item *item;
905 if (!branch_info->merge.nr || !branch_info->remote_name ||
906 strcmp(states->remote->name, branch_info->remote_name))
908 if ((n = strlen(branch_item->string)) > show_info->width)
909 show_info->width = n;
910 if (branch_info->rebase)
911 show_info->any_rebase = 1;
913 item = string_list_insert(branch_item->string, show_info->list);
914 item->util = branch_info;
919 static int show_local_info_item(struct string_list_item *item, void *cb_data)
921 struct show_info *show_info = cb_data;
922 struct branch_info *branch_info = item->util;
923 struct string_list *merge = &branch_info->merge;
927 if (branch_info->rebase && branch_info->merge.nr > 1) {
928 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
933 printf(" %-*s ", show_info->width, item->string);
934 if (branch_info->rebase) {
935 printf("rebases onto remote %s\n", merge->items[0].string);
937 } else if (show_info->any_rebase) {
938 printf(" merges with remote %s\n", merge->items[0].string);
939 also = " and with remote";
941 printf("merges with remote %s\n", merge->items[0].string);
942 also = " and with remote";
944 for (i = 1; i < merge->nr; i++)
945 printf(" %-*s %s %s\n", show_info->width, "", also,
946 merge->items[i].string);
951 static int add_push_to_show_info(struct string_list_item *push_item, void *cb_data)
953 struct show_info *show_info = cb_data;
954 struct push_info *push_info = push_item->util;
955 struct string_list_item *item;
957 if ((n = strlen(push_item->string)) > show_info->width)
958 show_info->width = n;
959 if ((n = strlen(push_info->dest)) > show_info->width2)
960 show_info->width2 = n;
961 item = string_list_append(push_item->string, show_info->list);
962 item->util = push_item->util;
967 * Sorting comparison for a string list that has push_info
968 * structs in its util field
970 static int cmp_string_with_push(const void *va, const void *vb)
972 const struct string_list_item *a = va;
973 const struct string_list_item *b = vb;
974 const struct push_info *a_push = a->util;
975 const struct push_info *b_push = b->util;
976 int cmp = strcmp(a->string, b->string);
977 return cmp ? cmp : strcmp(a_push->dest, b_push->dest);
980 static int show_push_info_item(struct string_list_item *item, void *cb_data)
982 struct show_info *show_info = cb_data;
983 struct push_info *push_info = item->util;
984 char *src = item->string, *status = NULL;
986 switch (push_info->status) {
987 case PUSH_STATUS_CREATE:
990 case PUSH_STATUS_DELETE:
994 case PUSH_STATUS_UPTODATE:
995 status = "up to date";
997 case PUSH_STATUS_FASTFORWARD:
998 status = "fast-forwardable";
1000 case PUSH_STATUS_OUTOFDATE:
1001 status = "local out of date";
1003 case PUSH_STATUS_NOTQUERIED:
1007 printf(" %-*s %s to %-*s (%s)\n", show_info->width, src,
1008 push_info->forced ? "forces" : "pushes",
1009 show_info->width2, push_info->dest, status);
1011 printf(" %-*s %s to %s\n", show_info->width, src,
1012 push_info->forced ? "forces" : "pushes",
1017 static int show(int argc, const char **argv)
1019 int no_query = 0, result = 0, query_flag = 0;
1020 struct option options[] = {
1021 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
1024 struct ref_states states;
1025 struct string_list info_list = { NULL, 0, 0, 0 };
1026 struct show_info info;
1028 argc = parse_options(argc, argv, NULL, options, builtin_remote_show_usage,
1035 query_flag = (GET_REF_STATES | GET_HEAD_NAMES | GET_PUSH_REF_STATES);
1037 memset(&states, 0, sizeof(states));
1038 memset(&info, 0, sizeof(info));
1039 info.states = &states;
1040 info.list = &info_list;
1041 for (; argc; argc--, argv++) {
1046 get_remote_ref_states(*argv, &states, query_flag);
1048 printf("* remote %s\n", *argv);
1049 printf(" Fetch URL: %s\n", states.remote->url_nr > 0 ?
1050 states.remote->url[0] : "(no URL)");
1051 if (states.remote->pushurl_nr) {
1052 url = states.remote->pushurl;
1053 url_nr = states.remote->pushurl_nr;
1055 url = states.remote->url;
1056 url_nr = states.remote->url_nr;
1058 for (i=0; i < url_nr; i++)
1059 printf(" Push URL: %s\n", url[i]);
1061 printf(" Push URL: %s\n", "(no URL)");
1063 printf(" HEAD branch: (not queried)\n");
1064 else if (!states.heads.nr)
1065 printf(" HEAD branch: (unknown)\n");
1066 else if (states.heads.nr == 1)
1067 printf(" HEAD branch: %s\n", states.heads.items[0].string);
1069 printf(" HEAD branch (remote HEAD is ambiguous,"
1070 " may be one of the following):\n");
1071 for (i = 0; i < states.heads.nr; i++)
1072 printf(" %s\n", states.heads.items[i].string);
1075 /* remote branch info */
1077 for_each_string_list(add_remote_to_show_info, &states.new, &info);
1078 for_each_string_list(add_remote_to_show_info, &states.tracked, &info);
1079 for_each_string_list(add_remote_to_show_info, &states.stale, &info);
1081 printf(" Remote branch%s:%s\n",
1082 info.list->nr > 1 ? "es" : "",
1083 no_query ? " (status not queried)" : "");
1084 for_each_string_list(show_remote_info_item, info.list, &info);
1085 string_list_clear(info.list, 0);
1089 info.any_rebase = 0;
1090 for_each_string_list(add_local_to_show_info, &branch_list, &info);
1092 printf(" Local branch%s configured for 'git pull':\n",
1093 info.list->nr > 1 ? "es" : "");
1094 for_each_string_list(show_local_info_item, info.list, &info);
1095 string_list_clear(info.list, 0);
1098 if (states.remote->mirror)
1099 printf(" Local refs will be mirrored by 'git push'\n");
1101 info.width = info.width2 = 0;
1102 for_each_string_list(add_push_to_show_info, &states.push, &info);
1103 qsort(info.list->items, info.list->nr,
1104 sizeof(*info.list->items), cmp_string_with_push);
1106 printf(" Local ref%s configured for 'git push'%s:\n",
1107 info.list->nr > 1 ? "s" : "",
1108 no_query ? " (status not queried)" : "");
1109 for_each_string_list(show_push_info_item, info.list, &info);
1110 string_list_clear(info.list, 0);
1112 free_remote_ref_states(&states);
1118 static int set_head(int argc, const char **argv)
1120 int i, opt_a = 0, opt_d = 0, result = 0;
1121 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
1122 char *head_name = NULL;
1124 struct option options[] = {
1125 OPT_BOOLEAN('a', "auto", &opt_a,
1126 "set refs/remotes/<name>/HEAD according to remote"),
1127 OPT_BOOLEAN('d', "delete", &opt_d,
1128 "delete refs/remotes/<name>/HEAD"),
1131 argc = parse_options(argc, argv, NULL, options, builtin_remote_sethead_usage,
1134 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
1136 if (!opt_a && !opt_d && argc == 2) {
1137 head_name = xstrdup(argv[1]);
1138 } else if (opt_a && !opt_d && argc == 1) {
1139 struct ref_states states;
1140 memset(&states, 0, sizeof(states));
1141 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
1142 if (!states.heads.nr)
1143 result |= error("Cannot determine remote HEAD");
1144 else if (states.heads.nr > 1) {
1145 result |= error("Multiple remote HEAD branches. "
1146 "Please choose one explicitly with:");
1147 for (i = 0; i < states.heads.nr; i++)
1148 fprintf(stderr, " git remote set-head %s %s\n",
1149 argv[0], states.heads.items[i].string);
1151 head_name = xstrdup(states.heads.items[0].string);
1152 free_remote_ref_states(&states);
1153 } else if (opt_d && !opt_a && argc == 1) {
1154 if (delete_ref(buf.buf, NULL, REF_NODEREF))
1155 result |= error("Could not delete %s", buf.buf);
1157 usage_with_options(builtin_remote_sethead_usage, options);
1160 unsigned char sha1[20];
1161 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
1162 /* make sure it's valid */
1163 if (!resolve_ref(buf2.buf, sha1, 1, NULL))
1164 result |= error("Not a valid ref: %s", buf2.buf);
1165 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
1166 result |= error("Could not setup %s", buf.buf);
1168 printf("%s/HEAD set to %s\n", argv[0], head_name);
1172 strbuf_release(&buf);
1173 strbuf_release(&buf2);
1177 static int prune(int argc, const char **argv)
1179 int dry_run = 0, result = 0;
1180 struct option options[] = {
1181 OPT__DRY_RUN(&dry_run),
1185 argc = parse_options(argc, argv, NULL, options, builtin_remote_prune_usage,
1189 usage_with_options(builtin_remote_prune_usage, options);
1191 for (; argc; argc--, argv++)
1192 result |= prune_remote(*argv, dry_run);
1197 static int prune_remote(const char *remote, int dry_run)
1200 struct ref_states states;
1201 const char *dangling_msg = dry_run
1202 ? " %s will become dangling!\n"
1203 : " %s has become dangling!\n";
1205 memset(&states, 0, sizeof(states));
1206 get_remote_ref_states(remote, &states, GET_REF_STATES);
1208 if (states.stale.nr) {
1209 printf("Pruning %s\n", remote);
1211 states.remote->url_nr
1212 ? states.remote->url[0]
1216 for (i = 0; i < states.stale.nr; i++) {
1217 const char *refname = states.stale.items[i].util;
1220 result |= delete_ref(refname, NULL, 0);
1222 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
1223 abbrev_ref(refname, "refs/remotes/"));
1224 warn_dangling_symref(stdout, dangling_msg, refname);
1227 free_remote_ref_states(&states);
1231 static int get_remote_default(const char *key, const char *value, void *priv)
1233 if (strcmp(key, "remotes.default") == 0) {
1240 static int update(int argc, const char **argv)
1243 struct option options[] = {
1244 OPT_BOOLEAN('p', "prune", &prune,
1245 "prune remotes after fetching"),
1248 const char **fetch_argv;
1250 int default_defined = 0;
1252 fetch_argv = xmalloc(sizeof(char *) * (argc+5));
1254 argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1255 PARSE_OPT_KEEP_ARGV0);
1257 fetch_argv[fetch_argc++] = "fetch";
1260 fetch_argv[fetch_argc++] = "--prune";
1262 fetch_argv[fetch_argc++] = "-v";
1263 fetch_argv[fetch_argc++] = "--multiple";
1265 fetch_argv[fetch_argc++] = "default";
1266 for (i = 1; i < argc; i++)
1267 fetch_argv[fetch_argc++] = argv[i];
1269 if (strcmp(fetch_argv[fetch_argc-1], "default") == 0) {
1270 git_config(get_remote_default, &default_defined);
1271 if (!default_defined)
1272 fetch_argv[fetch_argc-1] = "--all";
1275 fetch_argv[fetch_argc] = NULL;
1277 return run_command_v_opt(fetch_argv, RUN_GIT_CMD);
1280 static int remove_all_fetch_refspecs(const char *remote, const char *key)
1282 return git_config_set_multivar(key, NULL, NULL, 1);
1285 static int add_branches(struct remote *remote, const char **branches,
1288 const char *remotename = remote->name;
1289 int mirror = remote->mirror;
1290 struct strbuf refspec = STRBUF_INIT;
1292 for (; *branches; branches++)
1293 if (add_branch(key, *branches, remotename, mirror, &refspec)) {
1294 strbuf_release(&refspec);
1298 strbuf_release(&refspec);
1302 static int set_remote_branches(const char *remotename, const char **branches,
1305 struct strbuf key = STRBUF_INIT;
1306 struct remote *remote;
1308 strbuf_addf(&key, "remote.%s.fetch", remotename);
1310 if (!remote_is_configured(remotename))
1311 die("No such remote '%s'", remotename);
1312 remote = remote_get(remotename);
1314 if (!add_mode && remove_all_fetch_refspecs(remotename, key.buf)) {
1315 strbuf_release(&key);
1318 if (add_branches(remote, branches, key.buf)) {
1319 strbuf_release(&key);
1323 strbuf_release(&key);
1327 static int set_branches(int argc, const char **argv)
1330 struct option options[] = {
1331 OPT_BOOLEAN('\0', "add", &add_mode, "add branch"),
1335 argc = parse_options(argc, argv, NULL, options,
1336 builtin_remote_setbranches_usage, 0);
1338 error("no remote specified");
1339 usage_with_options(builtin_remote_setbranches_usage, options);
1343 return set_remote_branches(argv[0], argv + 1, add_mode);
1346 static int set_url(int argc, const char **argv)
1348 int i, push_mode = 0, add_mode = 0, delete_mode = 0;
1349 int matches = 0, negative_matches = 0;
1350 const char *remotename = NULL;
1351 const char *newurl = NULL;
1352 const char *oldurl = NULL;
1353 struct remote *remote;
1355 const char **urlset;
1357 struct strbuf name_buf = STRBUF_INIT;
1358 struct option options[] = {
1359 OPT_BOOLEAN('\0', "push", &push_mode,
1360 "manipulate push URLs"),
1361 OPT_BOOLEAN('\0', "add", &add_mode,
1363 OPT_BOOLEAN('\0', "delete", &delete_mode,
1367 argc = parse_options(argc, argv, NULL, options, builtin_remote_update_usage,
1368 PARSE_OPT_KEEP_ARGV0);
1370 if (add_mode && delete_mode)
1371 die("--add --delete doesn't make sense");
1373 if (argc < 3 || argc > 4 || ((add_mode || delete_mode) && argc != 3))
1374 usage_with_options(builtin_remote_seturl_usage, options);
1376 remotename = argv[1];
1384 if (!remote_is_configured(remotename))
1385 die("No such remote '%s'", remotename);
1386 remote = remote_get(remotename);
1389 strbuf_addf(&name_buf, "remote.%s.pushurl", remotename);
1390 urlset = remote->pushurl;
1391 urlset_nr = remote->pushurl_nr;
1393 strbuf_addf(&name_buf, "remote.%s.url", remotename);
1394 urlset = remote->url;
1395 urlset_nr = remote->url_nr;
1398 /* Special cases that add new entry. */
1399 if ((!oldurl && !delete_mode) || add_mode) {
1401 git_config_set_multivar(name_buf.buf, newurl,
1404 git_config_set(name_buf.buf, newurl);
1405 strbuf_release(&name_buf);
1409 /* Old URL specified. Demand that one matches. */
1410 if (regcomp(&old_regex, oldurl, REG_EXTENDED))
1411 die("Invalid old URL pattern: %s", oldurl);
1413 for (i = 0; i < urlset_nr; i++)
1414 if (!regexec(&old_regex, urlset[i], 0, NULL, 0))
1418 if (!delete_mode && !matches)
1419 die("No such URL found: %s", oldurl);
1420 if (delete_mode && !negative_matches && !push_mode)
1421 die("Will not delete all non-push URLs");
1423 regfree(&old_regex);
1426 git_config_set_multivar(name_buf.buf, newurl, oldurl, 0);
1428 git_config_set_multivar(name_buf.buf, NULL, oldurl, 1);
1432 static int get_one_entry(struct remote *remote, void *priv)
1434 struct string_list *list = priv;
1435 struct strbuf url_buf = STRBUF_INIT;
1439 if (remote->url_nr > 0) {
1440 strbuf_addf(&url_buf, "%s (fetch)", remote->url[0]);
1441 string_list_append(remote->name, list)->util =
1442 strbuf_detach(&url_buf, NULL);
1444 string_list_append(remote->name, list)->util = NULL;
1445 if (remote->pushurl_nr) {
1446 url = remote->pushurl;
1447 url_nr = remote->pushurl_nr;
1450 url_nr = remote->url_nr;
1452 for (i = 0; i < url_nr; i++)
1454 strbuf_addf(&url_buf, "%s (push)", url[i]);
1455 string_list_append(remote->name, list)->util =
1456 strbuf_detach(&url_buf, NULL);
1462 static int show_all(void)
1464 struct string_list list = { NULL, 0, 0 };
1467 list.strdup_strings = 1;
1468 result = for_each_remote(get_one_entry, &list);
1473 sort_string_list(&list);
1474 for (i = 0; i < list.nr; i++) {
1475 struct string_list_item *item = list.items + i;
1477 printf("%s\t%s\n", item->string,
1478 item->util ? (const char *)item->util : "");
1480 if (i && !strcmp((item - 1)->string, item->string))
1482 printf("%s\n", item->string);
1486 string_list_clear(&list, 1);
1490 int cmd_remote(int argc, const char **argv, const char *prefix)
1492 struct option options[] = {
1493 OPT_BOOLEAN('v', "verbose", &verbose, "be verbose; must be placed before a subcommand"),
1498 argc = parse_options(argc, argv, prefix, options, builtin_remote_usage,
1499 PARSE_OPT_STOP_AT_NON_OPTION);
1502 result = show_all();
1503 else if (!strcmp(argv[0], "add"))
1504 result = add(argc, argv);
1505 else if (!strcmp(argv[0], "rename"))
1506 result = mv(argc, argv);
1507 else if (!strcmp(argv[0], "rm"))
1508 result = rm(argc, argv);
1509 else if (!strcmp(argv[0], "set-head"))
1510 result = set_head(argc, argv);
1511 else if (!strcmp(argv[0], "set-branches"))
1512 result = set_branches(argc, argv);
1513 else if (!strcmp(argv[0], "set-url"))
1514 result = set_url(argc, argv);
1515 else if (!strcmp(argv[0], "show"))
1516 result = show(argc, argv);
1517 else if (!strcmp(argv[0], "prune"))
1518 result = prune(argc, argv);
1519 else if (!strcmp(argv[0], "update"))
1520 result = update(argc, argv);
1522 error("Unknown subcommand: %s", argv[0]);
1523 usage_with_options(builtin_remote_usage, options);
1526 return result ? 1 : 0;