2 #include "parse-options.h"
7 #include "run-command.h"
10 static const char * const builtin_remote_usage[] = {
12 "git remote add <name> <url>",
13 "git remote rm <name>",
14 "git remote show <name>",
15 "git remote prune <name>",
16 "git remote update [group]",
22 static int show_all(void);
24 static inline int postfixcmp(const char *string, const char *postfix)
26 int len1 = strlen(string), len2 = strlen(postfix);
29 return strcmp(string + len1 - len2, postfix);
32 static int opt_parse_track(const struct option *opt, const char *arg, int not)
34 struct path_list *list = opt->value;
36 path_list_clear(list, 0);
38 path_list_append(arg, list);
42 static int fetch_remote(const char *name)
44 const char *argv[] = { "fetch", name, NULL };
45 printf("Updating %s\n", name);
46 if (run_command_v_opt(argv, RUN_GIT_CMD))
47 return error("Could not fetch %s", name);
51 static int add(int argc, const char **argv)
53 int fetch = 0, mirror = 0;
54 struct path_list track = { NULL, 0, 0 };
55 const char *master = NULL;
56 struct remote *remote;
57 struct strbuf buf, buf2;
58 const char *name, *url;
61 struct option options[] = {
62 OPT_GROUP("add specific options"),
63 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
64 OPT_CALLBACK('t', "track", &track, "branch",
65 "branch(es) to track", opt_parse_track),
66 OPT_STRING('m', "master", &master, "branch", "master branch"),
67 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
71 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
74 usage_with_options(builtin_remote_usage, options);
79 remote = remote_get(name);
80 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
81 remote->fetch_refspec_nr))
82 die("remote %s already exists.", name);
85 strbuf_init(&buf2, 0);
87 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
88 if (!valid_fetch_refspec(buf2.buf))
89 die("'%s' is not a valid remote name", name);
91 strbuf_addf(&buf, "remote.%s.url", name);
92 if (git_config_set(buf.buf, url))
96 strbuf_addf(&buf, "remote.%s.fetch", name);
99 path_list_append("*", &track);
100 for (i = 0; i < track.nr; i++) {
101 struct path_list_item *item = track.items + i;
104 strbuf_addch(&buf2, '+');
106 strbuf_addf(&buf2, "refs/%s:refs/%s",
107 item->path, item->path);
109 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
110 item->path, name, item->path);
111 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
117 strbuf_addf(&buf, "remote.%s.mirror", name);
118 if (git_config_set(buf.buf, "yes"))
122 if (fetch && fetch_remote(name))
127 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
130 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
132 if (create_symref(buf.buf, buf2.buf, "remote add"))
133 return error("Could not setup master '%s'", master);
136 strbuf_release(&buf);
137 strbuf_release(&buf2);
138 path_list_clear(&track, 0);
145 struct path_list merge;
148 static struct path_list branch_list;
150 static int config_read_branches(const char *key, const char *value, void *cb)
152 if (!prefixcmp(key, "branch.")) {
154 struct path_list_item *item;
155 struct branch_info *info;
156 enum { REMOTE, MERGE } type;
159 if (!postfixcmp(key, ".remote")) {
160 name = xstrndup(key, strlen(key) - 7);
162 } else if (!postfixcmp(key, ".merge")) {
163 name = xstrndup(key, strlen(key) - 6);
168 item = path_list_insert(name, &branch_list);
171 item->util = xcalloc(sizeof(struct branch_info), 1);
173 if (type == REMOTE) {
175 warning("more than one branch.%s", key);
176 info->remote = xstrdup(value);
178 char *space = strchr(value, ' ');
179 const char *ptr = skip_prefix(value, "refs/heads/");
184 merge = xstrndup(value, space - value);
185 path_list_append(merge, &info->merge);
186 ptr = skip_prefix(space + 1, "refs/heads/");
191 space = strchr(value, ' ');
193 path_list_append(xstrdup(value), &info->merge);
199 static void read_branches(void)
203 git_config(config_read_branches, NULL);
204 sort_path_list(&branch_list);
208 struct remote *remote;
209 struct path_list new, stale, tracked;
212 static int handle_one_branch(const char *refname,
213 const unsigned char *sha1, int flags, void *cb_data)
215 struct ref_states *states = cb_data;
216 struct refspec refspec;
218 memset(&refspec, 0, sizeof(refspec));
219 refspec.dst = (char *)refname;
220 if (!remote_find_tracking(states->remote, &refspec)) {
221 struct path_list_item *item;
222 const char *name, *ptr;
223 ptr = skip_prefix(refspec.src, "refs/heads/");
228 /* symbolic refs pointing nowhere were handled already */
229 if ((flags & REF_ISSYMREF) ||
230 unsorted_path_list_has_path(&states->tracked,
232 unsorted_path_list_has_path(&states->new,
235 item = path_list_append(name, &states->stale);
236 item->util = xstrdup(refname);
241 static int get_ref_states(const struct ref *ref, struct ref_states *states)
243 struct ref *fetch_map = NULL, **tail = &fetch_map;
246 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
247 if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
248 die("Could not get fetch map for refspec %s",
249 states->remote->fetch_refspec[i]);
251 states->new.strdup_paths = states->tracked.strdup_paths = 1;
252 for (ref = fetch_map; ref; ref = ref->next) {
253 struct path_list *target = &states->tracked;
254 unsigned char sha1[20];
258 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
259 target = &states->new;
261 target = &states->tracked;
262 if (hashcmp(sha1, ref->new_sha1))
265 ptr = skip_prefix(ref->name, "refs/heads/");
268 path_list_append(ptr, target)->util = util;
270 free_refs(fetch_map);
272 for_each_ref(handle_one_branch, states);
273 sort_path_list(&states->stale);
278 struct known_remote {
279 struct known_remote *next;
280 struct remote *remote;
283 struct known_remotes {
284 struct remote *to_delete;
285 struct known_remote *list;
288 static int add_known_remote(struct remote *remote, void *cb_data)
290 struct known_remotes *all = cb_data;
291 struct known_remote *r;
293 if (!strcmp(all->to_delete->name, remote->name))
296 r = xmalloc(sizeof(*r));
303 struct branches_for_remote {
304 struct remote *remote;
305 struct path_list *branches;
306 struct known_remotes *keep;
309 static int add_branch_for_removal(const char *refname,
310 const unsigned char *sha1, int flags, void *cb_data)
312 struct branches_for_remote *branches = cb_data;
313 struct refspec refspec;
314 struct path_list_item *item;
315 struct known_remote *kr;
317 memset(&refspec, 0, sizeof(refspec));
318 refspec.dst = (char *)refname;
319 if (remote_find_tracking(branches->remote, &refspec))
322 /* don't delete a branch if another remote also uses it */
323 for (kr = branches->keep->list; kr; kr = kr->next) {
324 memset(&refspec, 0, sizeof(refspec));
325 refspec.dst = (char *)refname;
326 if (!remote_find_tracking(kr->remote, &refspec))
330 /* make sure that symrefs are deleted */
331 if (flags & REF_ISSYMREF)
332 return unlink(git_path(refname));
334 item = path_list_append(refname, branches->branches);
335 item->util = xmalloc(20);
336 hashcpy(item->util, sha1);
341 static int remove_branches(struct path_list *branches)
344 for (i = 0; i < branches->nr; i++) {
345 struct path_list_item *item = branches->items + i;
346 const char *refname = item->path;
347 unsigned char *sha1 = item->util;
349 if (delete_ref(refname, sha1))
350 result |= error("Could not remove branch %s", refname);
355 static int rm(int argc, const char **argv)
357 struct option options[] = {
360 struct remote *remote;
362 struct known_remotes known_remotes = { NULL, NULL };
363 struct path_list branches = { NULL, 0, 0, 1 };
364 struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
368 usage_with_options(builtin_remote_usage, options);
370 remote = remote_get(argv[1]);
372 die("No such remote: %s", argv[1]);
374 known_remotes.to_delete = remote;
375 for_each_remote(add_known_remote, &known_remotes);
377 strbuf_init(&buf, 0);
378 strbuf_addf(&buf, "remote.%s", remote->name);
379 if (git_config_rename_section(buf.buf, NULL) < 1)
380 return error("Could not remove config section '%s'", buf.buf);
383 for (i = 0; i < branch_list.nr; i++) {
384 struct path_list_item *item = branch_list.items + i;
385 struct branch_info *info = item->util;
386 if (info->remote && !strcmp(info->remote, remote->name)) {
387 const char *keys[] = { "remote", "merge", NULL }, **k;
388 for (k = keys; *k; k++) {
390 strbuf_addf(&buf, "branch.%s.%s",
392 if (git_config_set(buf.buf, NULL)) {
393 strbuf_release(&buf);
401 * We cannot just pass a function to for_each_ref() which deletes
402 * the branches one by one, since for_each_ref() relies on cached
403 * refs, which are invalidated when deleting a branch.
405 cb_data.remote = remote;
406 i = for_each_ref(add_branch_for_removal, &cb_data);
407 strbuf_release(&buf);
410 i = remove_branches(&branches);
411 path_list_clear(&branches, 1);
416 static void show_list(const char *title, struct path_list *list)
423 printf(title, list->nr > 1 ? "es" : "");
425 for (i = 0; i < list->nr; i++)
426 printf("%s%s", i ? " " : "", list->items[i].path);
430 static int get_remote_ref_states(const char *name,
431 struct ref_states *states,
434 struct transport *transport;
435 const struct ref *ref;
437 states->remote = remote_get(name);
439 return error("No such remote: %s", name);
444 transport = transport_get(NULL, states->remote->url_nr > 0 ?
445 states->remote->url[0] : NULL);
446 ref = transport_get_remote_refs(transport);
447 transport_disconnect(transport);
449 get_ref_states(ref, states);
455 static int append_ref_to_tracked_list(const char *refname,
456 const unsigned char *sha1, int flags, void *cb_data)
458 struct ref_states *states = cb_data;
459 struct refspec refspec;
461 memset(&refspec, 0, sizeof(refspec));
462 refspec.dst = (char *)refname;
463 if (!remote_find_tracking(states->remote, &refspec)) {
464 path_list_append(skip_prefix(refspec.src, "refs/heads/"),
471 static int show(int argc, const char **argv)
473 int no_query = 0, result = 0;
474 struct option options[] = {
475 OPT_GROUP("show specific options"),
476 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
479 struct ref_states states;
481 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
486 memset(&states, 0, sizeof(states));
487 for (; argc; argc--, argv++) {
491 get_remote_ref_states(*argv, &states, !no_query);
493 printf("* remote %s\n URL: %s\n", *argv,
494 states.remote->url_nr > 0 ?
495 states.remote->url[0] : "(no URL)");
497 for (i = 0; i < branch_list.nr; i++) {
498 struct path_list_item *branch = branch_list.items + i;
499 struct branch_info *info = branch->util;
502 if (!info->merge.nr || strcmp(*argv, info->remote))
504 printf(" Remote branch%s merged with 'git pull' "
505 "while on branch %s\n ",
506 info->merge.nr > 1 ? "es" : "",
508 for (j = 0; j < info->merge.nr; j++)
509 printf(" %s", info->merge.items[j].path);
514 strbuf_init(&buf, 0);
515 strbuf_addf(&buf, " New remote branch%%s (next fetch "
516 "will store in remotes/%s)", states.remote->name);
517 show_list(buf.buf, &states.new);
518 strbuf_release(&buf);
519 show_list(" Stale tracking branch%s (use 'git remote "
520 "prune')", &states.stale);
524 for_each_ref(append_ref_to_tracked_list, &states);
525 show_list(" Tracked remote branch%s", &states.tracked);
527 if (states.remote->push_refspec_nr) {
528 printf(" Local branch%s pushed with 'git push'\n ",
529 states.remote->push_refspec_nr > 1 ?
531 for (i = 0; i < states.remote->push_refspec_nr; i++) {
532 struct refspec *spec = states.remote->push + i;
533 const char *p = "", *q = "";
535 p = skip_prefix(spec->src, "refs/heads/");
537 q = skip_prefix(spec->dst, "refs/heads/");
538 printf(" %s%s%s%s", spec->force ? "+" : "",
540 spec->dst ? ":" : "",
546 /* NEEDSWORK: free remote */
547 path_list_clear(&states.new, 0);
548 path_list_clear(&states.stale, 0);
549 path_list_clear(&states.tracked, 0);
555 static int prune(int argc, const char **argv)
557 int dry_run = 0, result = 0;
558 struct option options[] = {
559 OPT_GROUP("prune specific options"),
560 OPT__DRY_RUN(&dry_run),
563 struct ref_states states;
565 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
568 usage_with_options(builtin_remote_usage, options);
570 memset(&states, 0, sizeof(states));
571 for (; argc; argc--, argv++) {
574 get_remote_ref_states(*argv, &states, 1);
576 if (states.stale.nr) {
577 printf("Pruning %s\n", *argv);
579 states.remote->url_nr
580 ? states.remote->url[0]
584 for (i = 0; i < states.stale.nr; i++) {
585 const char *refname = states.stale.items[i].util;
588 result |= delete_ref(refname, NULL);
590 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
591 skip_prefix(refname, "refs/remotes/"));
594 /* NEEDSWORK: free remote */
595 path_list_clear(&states.new, 0);
596 path_list_clear(&states.stale, 0);
597 path_list_clear(&states.tracked, 0);
603 static int get_one_remote_for_update(struct remote *remote, void *priv)
605 struct path_list *list = priv;
606 if (!remote->skip_default_update)
607 path_list_append(xstrdup(remote->name), list);
611 struct remote_group {
613 struct path_list *list;
616 static int get_remote_group(const char *key, const char *value, void *cb)
618 if (!prefixcmp(key, "remotes.") &&
619 !strcmp(key + 8, remote_group.name)) {
620 /* split list by white space */
621 int space = strcspn(value, " \t\n");
624 path_list_append(xstrndup(value, space),
626 value += space + (value[space] != '\0');
627 space = strcspn(value, " \t\n");
634 static int update(int argc, const char **argv)
637 struct path_list list = { NULL, 0, 0, 0 };
638 static const char *default_argv[] = { NULL, "default", NULL };
645 remote_group.list = &list;
646 for (i = 1; i < argc; i++) {
647 remote_group.name = argv[i];
648 result = git_config(get_remote_group, NULL);
651 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
652 result = for_each_remote(get_one_remote_for_update, &list);
654 for (i = 0; i < list.nr; i++)
655 result |= fetch_remote(list.items[i].path);
657 /* all names were strdup()ed or strndup()ed */
658 list.strdup_paths = 1;
659 path_list_clear(&list, 0);
664 static int get_one_entry(struct remote *remote, void *priv)
666 struct path_list *list = priv;
668 path_list_append(remote->name, list)->util = remote->url_nr ?
669 (void *)remote->url[0] : NULL;
670 if (remote->url_nr > 1)
671 warning("Remote %s has more than one URL", remote->name);
676 static int show_all(void)
678 struct path_list list = { NULL, 0, 0 };
679 int result = for_each_remote(get_one_entry, &list);
684 sort_path_list(&list);
685 for (i = 0; i < list.nr; i++) {
686 struct path_list_item *item = list.items + i;
687 printf("%s%s%s\n", item->path,
689 verbose && item->util ?
690 (const char *)item->util : "");
696 int cmd_remote(int argc, const char **argv, const char *prefix)
698 struct option options[] = {
699 OPT__VERBOSE(&verbose),
704 argc = parse_options(argc, argv, options, builtin_remote_usage,
705 PARSE_OPT_STOP_AT_NON_OPTION);
709 else if (!strcmp(argv[0], "add"))
710 result = add(argc, argv);
711 else if (!strcmp(argv[0], "rm"))
712 result = rm(argc, argv);
713 else if (!strcmp(argv[0], "show"))
714 result = show(argc, argv);
715 else if (!strcmp(argv[0], "prune"))
716 result = prune(argc, argv);
717 else if (!strcmp(argv[0], "update"))
718 result = update(argc, argv);
720 error("Unknown subcommand: %s", argv[0]);
721 usage_with_options(builtin_remote_usage, options);
724 return result ? 1 : 0;