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 show [-n] <name>",
17 "git remote prune [-n | --dry-run] <name>",
18 "git remote [-v | --verbose] update [group]",
22 #define GET_REF_STATES (1<<0)
23 #define GET_HEAD_NAMES (1<<1)
27 static int show_all(void);
29 static inline int postfixcmp(const char *string, const char *postfix)
31 int len1 = strlen(string), len2 = strlen(postfix);
34 return strcmp(string + len1 - len2, postfix);
37 static int opt_parse_track(const struct option *opt, const char *arg, int not)
39 struct string_list *list = opt->value;
41 string_list_clear(list, 0);
43 string_list_append(arg, list);
47 static int fetch_remote(const char *name)
49 const char *argv[] = { "fetch", name, NULL, NULL };
54 printf("Updating %s\n", name);
55 if (run_command_v_opt(argv, RUN_GIT_CMD))
56 return error("Could not fetch %s", name);
60 static int add(int argc, const char **argv)
62 int fetch = 0, mirror = 0;
63 struct string_list track = { NULL, 0, 0 };
64 const char *master = NULL;
65 struct remote *remote;
66 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
67 const char *name, *url;
70 struct option options[] = {
71 OPT_GROUP("add specific options"),
72 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
73 OPT_CALLBACK('t', "track", &track, "branch",
74 "branch(es) to track", opt_parse_track),
75 OPT_STRING('m', "master", &master, "branch", "master branch"),
76 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
80 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
83 usage_with_options(builtin_remote_usage, options);
88 remote = remote_get(name);
89 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
90 remote->fetch_refspec_nr))
91 die("remote %s already exists.", name);
93 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
94 if (!valid_fetch_refspec(buf2.buf))
95 die("'%s' is not a valid remote name", name);
97 strbuf_addf(&buf, "remote.%s.url", name);
98 if (git_config_set(buf.buf, url))
102 strbuf_addf(&buf, "remote.%s.fetch", name);
105 string_list_append("*", &track);
106 for (i = 0; i < track.nr; i++) {
107 struct string_list_item *item = track.items + i;
110 strbuf_addch(&buf2, '+');
112 strbuf_addf(&buf2, "refs/%s:refs/%s",
113 item->string, item->string);
115 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
116 item->string, name, item->string);
117 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
123 strbuf_addf(&buf, "remote.%s.mirror", name);
124 if (git_config_set(buf.buf, "true"))
128 if (fetch && fetch_remote(name))
133 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
136 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
138 if (create_symref(buf.buf, buf2.buf, "remote add"))
139 return error("Could not setup master '%s'", master);
142 strbuf_release(&buf);
143 strbuf_release(&buf2);
144 string_list_clear(&track, 0);
151 struct string_list merge;
155 static struct string_list branch_list;
157 static const char *abbrev_ref(const char *name, const char *prefix)
159 const char *abbrev = skip_prefix(name, prefix);
164 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
166 static int config_read_branches(const char *key, const char *value, void *cb)
168 if (!prefixcmp(key, "branch.")) {
169 const char *orig_key = key;
171 struct string_list_item *item;
172 struct branch_info *info;
173 enum { REMOTE, MERGE, REBASE } type;
176 if (!postfixcmp(key, ".remote")) {
177 name = xstrndup(key, strlen(key) - 7);
179 } else if (!postfixcmp(key, ".merge")) {
180 name = xstrndup(key, strlen(key) - 6);
182 } else if (!postfixcmp(key, ".rebase")) {
183 name = xstrndup(key, strlen(key) - 7);
188 item = string_list_insert(name, &branch_list);
191 item->util = xcalloc(sizeof(struct branch_info), 1);
193 if (type == REMOTE) {
194 if (info->remote_name)
195 warning("more than one %s", orig_key);
196 info->remote_name = xstrdup(value);
197 } else if (type == MERGE) {
198 char *space = strchr(value, ' ');
199 value = abbrev_branch(value);
202 merge = xstrndup(value, space - value);
203 string_list_append(merge, &info->merge);
204 value = abbrev_branch(space + 1);
205 space = strchr(value, ' ');
207 string_list_append(xstrdup(value), &info->merge);
209 info->rebase = git_config_bool(orig_key, value);
214 static void read_branches(void)
218 git_config(config_read_branches, NULL);
222 struct remote *remote;
223 struct string_list new, stale, tracked, heads;
227 static int handle_one_branch(const char *refname,
228 const unsigned char *sha1, int flags, void *cb_data)
230 struct ref_states *states = cb_data;
231 struct refspec refspec;
233 memset(&refspec, 0, sizeof(refspec));
234 refspec.dst = (char *)refname;
235 if (!remote_find_tracking(states->remote, &refspec)) {
236 struct string_list_item *item;
237 const char *name = abbrev_branch(refspec.src);
238 /* symbolic refs pointing nowhere were handled already */
239 if ((flags & REF_ISSYMREF) ||
240 string_list_has_string(&states->tracked, name) ||
241 string_list_has_string(&states->new, name))
243 item = string_list_append(name, &states->stale);
244 item->util = xstrdup(refname);
249 static int get_ref_states(const struct ref *remote_refs, struct ref_states *states)
251 struct ref *fetch_map = NULL, **tail = &fetch_map;
255 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
256 if (get_fetch_map(remote_refs, states->remote->fetch + i, &tail, 1))
257 die("Could not get fetch map for refspec %s",
258 states->remote->fetch_refspec[i]);
260 states->new.strdup_strings = states->tracked.strdup_strings = 1;
261 for (ref = fetch_map; ref; ref = ref->next) {
262 unsigned char sha1[20];
263 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
264 string_list_append(abbrev_branch(ref->name), &states->new);
266 string_list_append(abbrev_branch(ref->name), &states->tracked);
268 free_refs(fetch_map);
270 sort_string_list(&states->new);
271 sort_string_list(&states->tracked);
272 for_each_ref(handle_one_branch, states);
273 sort_string_list(&states->stale);
278 static int get_head_names(const struct ref *remote_refs, struct ref_states *states)
280 struct ref *ref, *matches;
281 struct ref *fetch_map = NULL, **fetch_map_tail = &fetch_map;
282 struct refspec refspec;
286 refspec.src = refspec.dst = "refs/heads/";
287 states->heads.strdup_strings = 1;
288 get_fetch_map(remote_refs, &refspec, &fetch_map_tail, 0);
289 matches = guess_remote_head(find_ref_by_name(remote_refs, "HEAD"),
291 for(ref = matches; ref; ref = ref->next)
292 string_list_append(abbrev_branch(ref->name), &states->heads);
294 free_refs(fetch_map);
300 struct known_remote {
301 struct known_remote *next;
302 struct remote *remote;
305 struct known_remotes {
306 struct remote *to_delete;
307 struct known_remote *list;
310 static int add_known_remote(struct remote *remote, void *cb_data)
312 struct known_remotes *all = cb_data;
313 struct known_remote *r;
315 if (!strcmp(all->to_delete->name, remote->name))
318 r = xmalloc(sizeof(*r));
325 struct branches_for_remote {
326 struct remote *remote;
327 struct string_list *branches, *skipped;
328 struct known_remotes *keep;
331 static int add_branch_for_removal(const char *refname,
332 const unsigned char *sha1, int flags, void *cb_data)
334 struct branches_for_remote *branches = cb_data;
335 struct refspec refspec;
336 struct string_list_item *item;
337 struct known_remote *kr;
339 memset(&refspec, 0, sizeof(refspec));
340 refspec.dst = (char *)refname;
341 if (remote_find_tracking(branches->remote, &refspec))
344 /* don't delete a branch if another remote also uses it */
345 for (kr = branches->keep->list; kr; kr = kr->next) {
346 memset(&refspec, 0, sizeof(refspec));
347 refspec.dst = (char *)refname;
348 if (!remote_find_tracking(kr->remote, &refspec))
352 /* don't delete non-remote refs */
353 if (prefixcmp(refname, "refs/remotes")) {
354 /* advise user how to delete local branches */
355 if (!prefixcmp(refname, "refs/heads/"))
356 string_list_append(abbrev_branch(refname),
358 /* silently skip over other non-remote refs */
362 /* make sure that symrefs are deleted */
363 if (flags & REF_ISSYMREF)
364 return unlink(git_path("%s", refname));
366 item = string_list_append(refname, branches->branches);
367 item->util = xmalloc(20);
368 hashcpy(item->util, sha1);
376 struct string_list *remote_branches;
379 static int read_remote_branches(const char *refname,
380 const unsigned char *sha1, int flags, void *cb_data)
382 struct rename_info *rename = cb_data;
383 struct strbuf buf = STRBUF_INIT;
384 struct string_list_item *item;
386 unsigned char orig_sha1[20];
389 strbuf_addf(&buf, "refs/remotes/%s", rename->old);
390 if(!prefixcmp(refname, buf.buf)) {
391 item = string_list_append(xstrdup(refname), rename->remote_branches);
392 symref = resolve_ref(refname, orig_sha1, 1, &flag);
393 if (flag & REF_ISSYMREF)
394 item->util = xstrdup(symref);
402 static int migrate_file(struct remote *remote)
404 struct strbuf buf = STRBUF_INIT;
408 strbuf_addf(&buf, "remote.%s.url", remote->name);
409 for (i = 0; i < remote->url_nr; i++)
410 if (git_config_set_multivar(buf.buf, remote->url[i], "^$", 0))
411 return error("Could not append '%s' to '%s'",
412 remote->url[i], buf.buf);
414 strbuf_addf(&buf, "remote.%s.push", remote->name);
415 for (i = 0; i < remote->push_refspec_nr; i++)
416 if (git_config_set_multivar(buf.buf, remote->push_refspec[i], "^$", 0))
417 return error("Could not append '%s' to '%s'",
418 remote->push_refspec[i], buf.buf);
420 strbuf_addf(&buf, "remote.%s.fetch", remote->name);
421 for (i = 0; i < remote->fetch_refspec_nr; i++)
422 if (git_config_set_multivar(buf.buf, remote->fetch_refspec[i], "^$", 0))
423 return error("Could not append '%s' to '%s'",
424 remote->fetch_refspec[i], buf.buf);
425 if (remote->origin == REMOTE_REMOTES)
426 path = git_path("remotes/%s", remote->name);
427 else if (remote->origin == REMOTE_BRANCHES)
428 path = git_path("branches/%s", remote->name);
429 if (path && unlink(path))
430 warning("failed to remove '%s'", path);
434 static int mv(int argc, const char **argv)
436 struct option options[] = {
439 struct remote *oldremote, *newremote;
440 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT, buf3 = STRBUF_INIT;
441 struct string_list remote_branches = { NULL, 0, 0, 0 };
442 struct rename_info rename;
446 usage_with_options(builtin_remote_usage, options);
448 rename.old = argv[1];
449 rename.new = argv[2];
450 rename.remote_branches = &remote_branches;
452 oldremote = remote_get(rename.old);
454 die("No such remote: %s", rename.old);
456 if (!strcmp(rename.old, rename.new) && oldremote->origin != REMOTE_CONFIG)
457 return migrate_file(oldremote);
459 newremote = remote_get(rename.new);
460 if (newremote && (newremote->url_nr > 1 || newremote->fetch_refspec_nr))
461 die("remote %s already exists.", rename.new);
463 strbuf_addf(&buf, "refs/heads/test:refs/remotes/%s/test", rename.new);
464 if (!valid_fetch_refspec(buf.buf))
465 die("'%s' is not a valid remote name", rename.new);
468 strbuf_addf(&buf, "remote.%s", rename.old);
469 strbuf_addf(&buf2, "remote.%s", rename.new);
470 if (git_config_rename_section(buf.buf, buf2.buf) < 1)
471 return error("Could not rename config section '%s' to '%s'",
475 strbuf_addf(&buf, "remote.%s.fetch", rename.new);
476 if (git_config_set_multivar(buf.buf, NULL, NULL, 1))
477 return error("Could not remove config section '%s'", buf.buf);
478 for (i = 0; i < oldremote->fetch_refspec_nr; i++) {
482 strbuf_addstr(&buf2, oldremote->fetch_refspec[i]);
483 ptr = strstr(buf2.buf, rename.old);
485 strbuf_splice(&buf2, ptr-buf2.buf, strlen(rename.old),
486 rename.new, strlen(rename.new));
487 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
488 return error("Could not append '%s'", buf.buf);
492 for (i = 0; i < branch_list.nr; i++) {
493 struct string_list_item *item = branch_list.items + i;
494 struct branch_info *info = item->util;
495 if (info->remote_name && !strcmp(info->remote_name, rename.old)) {
497 strbuf_addf(&buf, "branch.%s.remote", item->string);
498 if (git_config_set(buf.buf, rename.new)) {
499 return error("Could not set '%s'", buf.buf);
505 * First remove symrefs, then rename the rest, finally create
508 for_each_ref(read_remote_branches, &rename);
509 for (i = 0; i < remote_branches.nr; i++) {
510 struct string_list_item *item = remote_branches.items + i;
512 unsigned char sha1[20];
515 symref = resolve_ref(item->string, sha1, 1, &flag);
516 if (!(flag & REF_ISSYMREF))
518 if (delete_ref(item->string, NULL, REF_NODEREF))
519 die("deleting '%s' failed", item->string);
521 for (i = 0; i < remote_branches.nr; i++) {
522 struct string_list_item *item = remote_branches.items + i;
527 strbuf_addstr(&buf, item->string);
528 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
529 rename.new, strlen(rename.new));
531 strbuf_addf(&buf2, "remote: renamed %s to %s",
532 item->string, buf.buf);
533 if (rename_ref(item->string, buf.buf, buf2.buf))
534 die("renaming '%s' failed", item->string);
536 for (i = 0; i < remote_branches.nr; i++) {
537 struct string_list_item *item = remote_branches.items + i;
542 strbuf_addstr(&buf, item->string);
543 strbuf_splice(&buf, strlen("refs/remotes/"), strlen(rename.old),
544 rename.new, strlen(rename.new));
546 strbuf_addstr(&buf2, item->util);
547 strbuf_splice(&buf2, strlen("refs/remotes/"), strlen(rename.old),
548 rename.new, strlen(rename.new));
550 strbuf_addf(&buf3, "remote: renamed %s to %s",
551 item->string, buf.buf);
552 if (create_symref(buf.buf, buf2.buf, buf3.buf))
553 die("creating '%s' failed", buf.buf);
558 static int remove_branches(struct string_list *branches)
561 for (i = 0; i < branches->nr; i++) {
562 struct string_list_item *item = branches->items + i;
563 const char *refname = item->string;
564 unsigned char *sha1 = item->util;
566 if (delete_ref(refname, sha1, 0))
567 result |= error("Could not remove branch %s", refname);
572 static int rm(int argc, const char **argv)
574 struct option options[] = {
577 struct remote *remote;
578 struct strbuf buf = STRBUF_INIT;
579 struct known_remotes known_remotes = { NULL, NULL };
580 struct string_list branches = { NULL, 0, 0, 1 };
581 struct string_list skipped = { NULL, 0, 0, 1 };
582 struct branches_for_remote cb_data = {
583 NULL, &branches, &skipped, &known_remotes
588 usage_with_options(builtin_remote_usage, options);
590 remote = remote_get(argv[1]);
592 die("No such remote: %s", argv[1]);
594 known_remotes.to_delete = remote;
595 for_each_remote(add_known_remote, &known_remotes);
597 strbuf_addf(&buf, "remote.%s", remote->name);
598 if (git_config_rename_section(buf.buf, NULL) < 1)
599 return error("Could not remove config section '%s'", buf.buf);
602 for (i = 0; i < branch_list.nr; i++) {
603 struct string_list_item *item = branch_list.items + i;
604 struct branch_info *info = item->util;
605 if (info->remote_name && !strcmp(info->remote_name, remote->name)) {
606 const char *keys[] = { "remote", "merge", NULL }, **k;
607 for (k = keys; *k; k++) {
609 strbuf_addf(&buf, "branch.%s.%s",
611 if (git_config_set(buf.buf, NULL)) {
612 strbuf_release(&buf);
620 * We cannot just pass a function to for_each_ref() which deletes
621 * the branches one by one, since for_each_ref() relies on cached
622 * refs, which are invalidated when deleting a branch.
624 cb_data.remote = remote;
625 result = for_each_ref(add_branch_for_removal, &cb_data);
626 strbuf_release(&buf);
629 result = remove_branches(&branches);
630 string_list_clear(&branches, 1);
633 fprintf(stderr, skipped.nr == 1 ?
634 "Note: A non-remote branch was not removed; "
635 "to delete it, use:\n" :
636 "Note: Non-remote branches were not removed; "
637 "to delete them, use:\n");
638 for (i = 0; i < skipped.nr; i++)
639 fprintf(stderr, " git branch -d %s\n",
640 skipped.items[i].string);
642 string_list_clear(&skipped, 0);
647 static void free_remote_ref_states(struct ref_states *states)
649 string_list_clear(&states->new, 0);
650 string_list_clear(&states->stale, 0);
651 string_list_clear(&states->tracked, 0);
652 string_list_clear(&states->heads, 0);
655 static int append_ref_to_tracked_list(const char *refname,
656 const unsigned char *sha1, int flags, void *cb_data)
658 struct ref_states *states = cb_data;
659 struct refspec refspec;
661 if (flags & REF_ISSYMREF)
664 memset(&refspec, 0, sizeof(refspec));
665 refspec.dst = (char *)refname;
666 if (!remote_find_tracking(states->remote, &refspec))
667 string_list_append(abbrev_branch(refspec.src), &states->tracked);
672 static int get_remote_ref_states(const char *name,
673 struct ref_states *states,
676 struct transport *transport;
677 const struct ref *remote_refs;
679 states->remote = remote_get(name);
681 return error("No such remote: %s", name);
686 transport = transport_get(NULL, states->remote->url_nr > 0 ?
687 states->remote->url[0] : NULL);
688 remote_refs = transport_get_remote_refs(transport);
689 transport_disconnect(transport);
692 if (query & GET_REF_STATES)
693 get_ref_states(remote_refs, states);
694 if (query & GET_HEAD_NAMES)
695 get_head_names(remote_refs, states);
697 for_each_ref(append_ref_to_tracked_list, states);
698 sort_string_list(&states->tracked);
705 struct string_list *list;
706 struct ref_states *states;
711 int add_remote_to_show_info(struct string_list_item *item, void *cb_data)
713 struct show_info *info = cb_data;
714 int n = strlen(item->string);
717 string_list_insert(item->string, info->list);
721 int show_remote_info_item(struct string_list_item *item, void *cb_data)
723 struct show_info *info = cb_data;
724 struct ref_states *states = info->states;
725 const char *name = item->string;
727 if (states->queried) {
728 const char *fmt = "%s";
729 const char *arg = "";
730 if (string_list_has_string(&states->new, name)) {
731 fmt = " new (next fetch will store in remotes/%s)";
732 arg = states->remote->name;
733 } else if (string_list_has_string(&states->tracked, name))
735 else if (string_list_has_string(&states->stale, name))
736 arg = " stale (use 'git remote prune' to remove)";
739 printf(" %-*s", info->width, name);
743 printf(" %s\n", name);
748 int add_local_to_show_info(struct string_list_item *branch_item, void *cb_data)
750 struct show_info *show_info = cb_data;
751 struct ref_states *states = show_info->states;
752 struct branch_info *branch_info = branch_item->util;
753 struct string_list_item *item;
756 if (!branch_info->merge.nr || !branch_info->remote_name ||
757 strcmp(states->remote->name, branch_info->remote_name))
759 if ((n = strlen(branch_item->string)) > show_info->width)
760 show_info->width = n;
761 if (branch_info->rebase)
762 show_info->any_rebase = 1;
764 item = string_list_insert(branch_item->string, show_info->list);
765 item->util = branch_info;
770 int show_local_info_item(struct string_list_item *item, void *cb_data)
772 struct show_info *show_info = cb_data;
773 struct branch_info *branch_info = item->util;
774 struct string_list *merge = &branch_info->merge;
778 if (branch_info->rebase && branch_info->merge.nr > 1) {
779 error("invalid branch.%s.merge; cannot rebase onto > 1 branch",
784 printf(" %-*s ", show_info->width, item->string);
785 if (branch_info->rebase) {
786 printf("rebases onto remote %s\n", merge->items[0].string);
788 } else if (show_info->any_rebase) {
789 printf(" merges with remote %s\n", merge->items[0].string);
790 also = " and with remote";
792 printf("merges with remote %s\n", merge->items[0].string);
793 also = " and with remote";
795 for (i = 1; i < merge->nr; i++)
796 printf(" %-*s %s %s\n", show_info->width, "", also,
797 merge->items[i].string);
802 static int show(int argc, const char **argv)
804 int no_query = 0, result = 0, query_flag = 0;
805 struct option options[] = {
806 OPT_GROUP("show specific options"),
807 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
810 struct ref_states states;
811 struct string_list info_list = { NULL, 0, 0, 0 };
812 struct show_info info;
814 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
820 query_flag = (GET_REF_STATES | GET_HEAD_NAMES);
822 memset(&states, 0, sizeof(states));
823 memset(&info, 0, sizeof(info));
824 info.states = &states;
825 info.list = &info_list;
826 for (; argc; argc--, argv++) {
829 get_remote_ref_states(*argv, &states, query_flag);
831 printf("* remote %s\n URL: %s\n", *argv,
832 states.remote->url_nr > 0 ?
833 states.remote->url[0] : "(no URL)");
835 printf(" HEAD branch: (not queried)\n");
836 else if (!states.heads.nr)
837 printf(" HEAD branch: (unknown)\n");
838 else if (states.heads.nr == 1)
839 printf(" HEAD branch: %s\n", states.heads.items[0].string);
841 printf(" HEAD branch (remote HEAD is ambiguous,"
842 " may be one of the following):\n");
843 for (i = 0; i < states.heads.nr; i++)
844 printf(" %s\n", states.heads.items[i].string);
847 /* remote branch info */
849 for_each_string_list(add_remote_to_show_info, &states.new, &info);
850 for_each_string_list(add_remote_to_show_info, &states.tracked, &info);
851 for_each_string_list(add_remote_to_show_info, &states.stale, &info);
853 printf(" Remote branch%s:%s\n",
854 info.list->nr > 1 ? "es" : "",
855 no_query ? " (status not queried)" : "");
856 for_each_string_list(show_remote_info_item, info.list, &info);
857 string_list_clear(info.list, 0);
862 for_each_string_list(add_local_to_show_info, &branch_list, &info);
864 printf(" Local branch%s configured for 'git pull':\n",
865 info.list->nr > 1 ? "es" : "");
866 for_each_string_list(show_local_info_item, info.list, &info);
867 string_list_clear(info.list, 0);
870 if (states.remote->push_refspec_nr) {
871 printf(" Local branch%s pushed with 'git push'\n",
872 states.remote->push_refspec_nr > 1 ?
874 for (i = 0; i < states.remote->push_refspec_nr; i++) {
875 struct refspec *spec = states.remote->push + i;
876 printf(" %s%s%s%s\n",
877 spec->force ? "+" : "",
878 abbrev_branch(spec->src),
879 spec->dst ? ":" : "",
880 spec->dst ? abbrev_branch(spec->dst) : "");
884 free_remote_ref_states(&states);
890 static int set_head(int argc, const char **argv)
892 int i, opt_a = 0, opt_d = 0, result = 0;
893 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
894 char *head_name = NULL;
896 struct option options[] = {
897 OPT_GROUP("set-head specific options"),
898 OPT_BOOLEAN('a', "auto", &opt_a,
899 "set refs/remotes/<name>/HEAD according to remote"),
900 OPT_BOOLEAN('d', "delete", &opt_d,
901 "delete refs/remotes/<name>/HEAD"),
904 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
906 strbuf_addf(&buf, "refs/remotes/%s/HEAD", argv[0]);
908 if (!opt_a && !opt_d && argc == 2) {
909 head_name = xstrdup(argv[1]);
910 } else if (opt_a && !opt_d && argc == 1) {
911 struct ref_states states;
912 memset(&states, 0, sizeof(states));
913 get_remote_ref_states(argv[0], &states, GET_HEAD_NAMES);
914 if (!states.heads.nr)
915 result |= error("Cannot determine remote HEAD");
916 else if (states.heads.nr > 1) {
917 result |= error("Multiple remote HEAD branches. "
918 "Please choose one explicitly with:");
919 for (i = 0; i < states.heads.nr; i++)
920 fprintf(stderr, " git remote set-head %s %s\n",
921 argv[0], states.heads.items[i].string);
923 head_name = xstrdup(states.heads.items[0].string);
924 free_remote_ref_states(&states);
925 } else if (opt_d && !opt_a && argc == 1) {
926 if (delete_ref(buf.buf, NULL, REF_NODEREF))
927 result |= error("Could not delete %s", buf.buf);
929 usage_with_options(builtin_remote_usage, options);
932 unsigned char sha1[20];
933 strbuf_addf(&buf2, "refs/remotes/%s/%s", argv[0], head_name);
934 /* make sure it's valid */
935 if (!resolve_ref(buf2.buf, sha1, 1, NULL))
936 result |= error("Not a valid ref: %s", buf2.buf);
937 else if (create_symref(buf.buf, buf2.buf, "remote set-head"))
938 result |= error("Could not setup %s", buf.buf);
940 printf("%s/HEAD set to %s\n", argv[0], head_name);
944 strbuf_release(&buf);
945 strbuf_release(&buf2);
949 static int prune(int argc, const char **argv)
951 int dry_run = 0, result = 0;
952 struct option options[] = {
953 OPT_GROUP("prune specific options"),
954 OPT__DRY_RUN(&dry_run),
957 struct ref_states states;
958 const char *dangling_msg;
960 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
963 usage_with_options(builtin_remote_usage, options);
965 dangling_msg = (dry_run
966 ? " %s will become dangling!\n"
967 : " %s has become dangling!\n");
969 memset(&states, 0, sizeof(states));
970 for (; argc; argc--, argv++) {
973 get_remote_ref_states(*argv, &states, GET_REF_STATES);
975 if (states.stale.nr) {
976 printf("Pruning %s\n", *argv);
978 states.remote->url_nr
979 ? states.remote->url[0]
983 for (i = 0; i < states.stale.nr; i++) {
984 const char *refname = states.stale.items[i].util;
987 result |= delete_ref(refname, NULL, 0);
989 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
990 abbrev_ref(refname, "refs/remotes/"));
991 warn_dangling_symref(dangling_msg, refname);
994 free_remote_ref_states(&states);
1000 static int get_one_remote_for_update(struct remote *remote, void *priv)
1002 struct string_list *list = priv;
1003 if (!remote->skip_default_update)
1004 string_list_append(remote->name, list);
1008 struct remote_group {
1010 struct string_list *list;
1013 static int get_remote_group(const char *key, const char *value, void *cb)
1015 if (!prefixcmp(key, "remotes.") &&
1016 !strcmp(key + 8, remote_group.name)) {
1017 /* split list by white space */
1018 int space = strcspn(value, " \t\n");
1021 string_list_append(xstrndup(value, space),
1023 value += space + (value[space] != '\0');
1024 space = strcspn(value, " \t\n");
1031 static int update(int argc, const char **argv)
1034 struct string_list list = { NULL, 0, 0, 0 };
1035 static const char *default_argv[] = { NULL, "default", NULL };
1039 argv = default_argv;
1042 remote_group.list = &list;
1043 for (i = 1; i < argc; i++) {
1044 remote_group.name = argv[i];
1045 result = git_config(get_remote_group, NULL);
1048 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
1049 result = for_each_remote(get_one_remote_for_update, &list);
1051 for (i = 0; i < list.nr; i++)
1052 result |= fetch_remote(list.items[i].string);
1054 /* all names were strdup()ed or strndup()ed */
1055 list.strdup_strings = 1;
1056 string_list_clear(&list, 0);
1061 static int get_one_entry(struct remote *remote, void *priv)
1063 struct string_list *list = priv;
1065 if (remote->url_nr > 0) {
1068 for (i = 0; i < remote->url_nr; i++)
1069 string_list_append(remote->name, list)->util = (void *)remote->url[i];
1071 string_list_append(remote->name, list)->util = NULL;
1076 static int show_all(void)
1078 struct string_list list = { NULL, 0, 0 };
1079 int result = for_each_remote(get_one_entry, &list);
1084 sort_string_list(&list);
1085 for (i = 0; i < list.nr; i++) {
1086 struct string_list_item *item = list.items + i;
1088 printf("%s\t%s\n", item->string,
1089 item->util ? (const char *)item->util : "");
1091 if (i && !strcmp((item - 1)->string, item->string))
1093 printf("%s\n", item->string);
1100 int cmd_remote(int argc, const char **argv, const char *prefix)
1102 struct option options[] = {
1103 OPT__VERBOSE(&verbose),
1108 argc = parse_options(argc, argv, options, builtin_remote_usage,
1109 PARSE_OPT_STOP_AT_NON_OPTION);
1112 result = show_all();
1113 else if (!strcmp(argv[0], "add"))
1114 result = add(argc, argv);
1115 else if (!strcmp(argv[0], "rename"))
1116 result = mv(argc, argv);
1117 else if (!strcmp(argv[0], "rm"))
1118 result = rm(argc, argv);
1119 else if (!strcmp(argv[0], "set-head"))
1120 result = set_head(argc, argv);
1121 else if (!strcmp(argv[0], "show"))
1122 result = show(argc, argv);
1123 else if (!strcmp(argv[0], "prune"))
1124 result = prune(argc, argv);
1125 else if (!strcmp(argv[0], "update"))
1126 result = update(argc, argv);
1128 error("Unknown subcommand: %s", argv[0]);
1129 usage_with_options(builtin_remote_usage, options);
1132 return result ? 1 : 0;