2 #include "parse-options.h"
5 #include "string-list.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 string_list *list = opt->value;
36 string_list_clear(list, 0);
38 string_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 string_list track = { NULL, 0, 0 };
55 const char *master = NULL;
56 struct remote *remote;
57 struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT;
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);
84 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
85 if (!valid_fetch_refspec(buf2.buf))
86 die("'%s' is not a valid remote name", name);
88 strbuf_addf(&buf, "remote.%s.url", name);
89 if (git_config_set(buf.buf, url))
93 strbuf_addf(&buf, "remote.%s.fetch", name);
96 string_list_append("*", &track);
97 for (i = 0; i < track.nr; i++) {
98 struct string_list_item *item = track.items + i;
101 strbuf_addch(&buf2, '+');
103 strbuf_addf(&buf2, "refs/%s:refs/%s",
104 item->string, item->string);
106 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
107 item->string, name, item->string);
108 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
114 strbuf_addf(&buf, "remote.%s.mirror", name);
115 if (git_config_set(buf.buf, "true"))
119 if (fetch && fetch_remote(name))
124 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
127 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
129 if (create_symref(buf.buf, buf2.buf, "remote add"))
130 return error("Could not setup master '%s'", master);
133 strbuf_release(&buf);
134 strbuf_release(&buf2);
135 string_list_clear(&track, 0);
142 struct string_list merge;
145 static struct string_list branch_list;
147 static const char *abbrev_ref(const char *name, const char *prefix)
149 const char *abbrev = skip_prefix(name, prefix);
154 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
156 static int config_read_branches(const char *key, const char *value, void *cb)
158 if (!prefixcmp(key, "branch.")) {
160 struct string_list_item *item;
161 struct branch_info *info;
162 enum { REMOTE, MERGE } type;
165 if (!postfixcmp(key, ".remote")) {
166 name = xstrndup(key, strlen(key) - 7);
168 } else if (!postfixcmp(key, ".merge")) {
169 name = xstrndup(key, strlen(key) - 6);
174 item = string_list_insert(name, &branch_list);
177 item->util = xcalloc(sizeof(struct branch_info), 1);
179 if (type == REMOTE) {
181 warning("more than one branch.%s", key);
182 info->remote = xstrdup(value);
184 char *space = strchr(value, ' ');
185 value = abbrev_branch(value);
188 merge = xstrndup(value, space - value);
189 string_list_append(merge, &info->merge);
190 value = abbrev_branch(space + 1);
191 space = strchr(value, ' ');
193 string_list_append(xstrdup(value), &info->merge);
199 static void read_branches(void)
203 git_config(config_read_branches, NULL);
204 sort_string_list(&branch_list);
208 struct remote *remote;
209 struct string_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 string_list_item *item;
222 const char *name = abbrev_branch(refspec.src);
223 /* symbolic refs pointing nowhere were handled already */
224 if ((flags & REF_ISSYMREF) ||
225 unsorted_string_list_has_string(&states->tracked,
227 unsorted_string_list_has_string(&states->new,
230 item = string_list_append(name, &states->stale);
231 item->util = xstrdup(refname);
236 static int get_ref_states(const struct ref *ref, struct ref_states *states)
238 struct ref *fetch_map = NULL, **tail = &fetch_map;
241 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
242 if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
243 die("Could not get fetch map for refspec %s",
244 states->remote->fetch_refspec[i]);
246 states->new.strdup_strings = states->tracked.strdup_strings = 1;
247 for (ref = fetch_map; ref; ref = ref->next) {
248 struct string_list *target = &states->tracked;
249 unsigned char sha1[20];
252 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
253 target = &states->new;
255 target = &states->tracked;
256 if (hashcmp(sha1, ref->new_sha1))
259 string_list_append(abbrev_branch(ref->name), target)->util = util;
261 free_refs(fetch_map);
263 for_each_ref(handle_one_branch, states);
264 sort_string_list(&states->stale);
269 struct known_remote {
270 struct known_remote *next;
271 struct remote *remote;
274 struct known_remotes {
275 struct remote *to_delete;
276 struct known_remote *list;
279 static int add_known_remote(struct remote *remote, void *cb_data)
281 struct known_remotes *all = cb_data;
282 struct known_remote *r;
284 if (!strcmp(all->to_delete->name, remote->name))
287 r = xmalloc(sizeof(*r));
294 struct branches_for_remote {
295 struct remote *remote;
296 struct string_list *branches;
297 struct known_remotes *keep;
300 static int add_branch_for_removal(const char *refname,
301 const unsigned char *sha1, int flags, void *cb_data)
303 struct branches_for_remote *branches = cb_data;
304 struct refspec refspec;
305 struct string_list_item *item;
306 struct known_remote *kr;
308 memset(&refspec, 0, sizeof(refspec));
309 refspec.dst = (char *)refname;
310 if (remote_find_tracking(branches->remote, &refspec))
313 /* don't delete a branch if another remote also uses it */
314 for (kr = branches->keep->list; kr; kr = kr->next) {
315 memset(&refspec, 0, sizeof(refspec));
316 refspec.dst = (char *)refname;
317 if (!remote_find_tracking(kr->remote, &refspec))
321 /* make sure that symrefs are deleted */
322 if (flags & REF_ISSYMREF)
323 return unlink(git_path("%s", refname));
325 item = string_list_append(refname, branches->branches);
326 item->util = xmalloc(20);
327 hashcpy(item->util, sha1);
332 static int remove_branches(struct string_list *branches)
335 for (i = 0; i < branches->nr; i++) {
336 struct string_list_item *item = branches->items + i;
337 const char *refname = item->string;
338 unsigned char *sha1 = item->util;
340 if (delete_ref(refname, sha1, 0))
341 result |= error("Could not remove branch %s", refname);
346 static int rm(int argc, const char **argv)
348 struct option options[] = {
351 struct remote *remote;
352 struct strbuf buf = STRBUF_INIT;
353 struct known_remotes known_remotes = { NULL, NULL };
354 struct string_list branches = { NULL, 0, 0, 1 };
355 struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
359 usage_with_options(builtin_remote_usage, options);
361 remote = remote_get(argv[1]);
363 die("No such remote: %s", argv[1]);
365 known_remotes.to_delete = remote;
366 for_each_remote(add_known_remote, &known_remotes);
368 strbuf_addf(&buf, "remote.%s", remote->name);
369 if (git_config_rename_section(buf.buf, NULL) < 1)
370 return error("Could not remove config section '%s'", buf.buf);
373 for (i = 0; i < branch_list.nr; i++) {
374 struct string_list_item *item = branch_list.items + i;
375 struct branch_info *info = item->util;
376 if (info->remote && !strcmp(info->remote, remote->name)) {
377 const char *keys[] = { "remote", "merge", NULL }, **k;
378 for (k = keys; *k; k++) {
380 strbuf_addf(&buf, "branch.%s.%s",
382 if (git_config_set(buf.buf, NULL)) {
383 strbuf_release(&buf);
391 * We cannot just pass a function to for_each_ref() which deletes
392 * the branches one by one, since for_each_ref() relies on cached
393 * refs, which are invalidated when deleting a branch.
395 cb_data.remote = remote;
396 i = for_each_ref(add_branch_for_removal, &cb_data);
397 strbuf_release(&buf);
400 i = remove_branches(&branches);
401 string_list_clear(&branches, 1);
406 static void show_list(const char *title, struct string_list *list,
407 const char *extra_arg)
414 printf(title, list->nr > 1 ? "es" : "", extra_arg);
416 for (i = 0; i < list->nr; i++)
417 printf(" %s\n", list->items[i].string);
420 static int get_remote_ref_states(const char *name,
421 struct ref_states *states,
424 struct transport *transport;
425 const struct ref *ref;
427 states->remote = remote_get(name);
429 return error("No such remote: %s", name);
434 transport = transport_get(NULL, states->remote->url_nr > 0 ?
435 states->remote->url[0] : NULL);
436 ref = transport_get_remote_refs(transport);
437 transport_disconnect(transport);
439 get_ref_states(ref, states);
445 static int append_ref_to_tracked_list(const char *refname,
446 const unsigned char *sha1, int flags, void *cb_data)
448 struct ref_states *states = cb_data;
449 struct refspec refspec;
451 memset(&refspec, 0, sizeof(refspec));
452 refspec.dst = (char *)refname;
453 if (!remote_find_tracking(states->remote, &refspec))
454 string_list_append(abbrev_branch(refspec.src), &states->tracked);
459 static int show(int argc, const char **argv)
461 int no_query = 0, result = 0;
462 struct option options[] = {
463 OPT_GROUP("show specific options"),
464 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
467 struct ref_states states;
469 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
474 memset(&states, 0, sizeof(states));
475 for (; argc; argc--, argv++) {
478 get_remote_ref_states(*argv, &states, !no_query);
480 printf("* remote %s\n URL: %s\n", *argv,
481 states.remote->url_nr > 0 ?
482 states.remote->url[0] : "(no URL)");
484 for (i = 0; i < branch_list.nr; i++) {
485 struct string_list_item *branch = branch_list.items + i;
486 struct branch_info *info = branch->util;
489 if (!info->merge.nr || strcmp(*argv, info->remote))
491 printf(" Remote branch%s merged with 'git pull' "
492 "while on branch %s\n ",
493 info->merge.nr > 1 ? "es" : "",
495 for (j = 0; j < info->merge.nr; j++)
496 printf(" %s", info->merge.items[j].string);
501 show_list(" New remote branch%s (next fetch "
502 "will store in remotes/%s)",
503 &states.new, states.remote->name);
504 show_list(" Stale tracking branch%s (use 'git remote "
505 "prune')", &states.stale, "");
509 for_each_ref(append_ref_to_tracked_list, &states);
510 show_list(" Tracked remote branch%s", &states.tracked, "");
512 if (states.remote->push_refspec_nr) {
513 printf(" Local branch%s pushed with 'git push'\n",
514 states.remote->push_refspec_nr > 1 ?
516 for (i = 0; i < states.remote->push_refspec_nr; i++) {
517 struct refspec *spec = states.remote->push + i;
518 printf(" %s%s%s%s\n",
519 spec->force ? "+" : "",
520 abbrev_branch(spec->src),
521 spec->dst ? ":" : "",
522 spec->dst ? abbrev_branch(spec->dst) : "");
526 /* NEEDSWORK: free remote */
527 string_list_clear(&states.new, 0);
528 string_list_clear(&states.stale, 0);
529 string_list_clear(&states.tracked, 0);
535 static int prune(int argc, const char **argv)
537 int dry_run = 0, result = 0;
538 struct option options[] = {
539 OPT_GROUP("prune specific options"),
540 OPT__DRY_RUN(&dry_run),
543 struct ref_states states;
545 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
548 usage_with_options(builtin_remote_usage, options);
550 memset(&states, 0, sizeof(states));
551 for (; argc; argc--, argv++) {
554 get_remote_ref_states(*argv, &states, 1);
556 if (states.stale.nr) {
557 printf("Pruning %s\n", *argv);
559 states.remote->url_nr
560 ? states.remote->url[0]
564 for (i = 0; i < states.stale.nr; i++) {
565 const char *refname = states.stale.items[i].util;
568 result |= delete_ref(refname, NULL, 0);
570 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
571 abbrev_ref(refname, "refs/remotes/"));
574 /* NEEDSWORK: free remote */
575 string_list_clear(&states.new, 0);
576 string_list_clear(&states.stale, 0);
577 string_list_clear(&states.tracked, 0);
583 static int get_one_remote_for_update(struct remote *remote, void *priv)
585 struct string_list *list = priv;
586 if (!remote->skip_default_update)
587 string_list_append(xstrdup(remote->name), list);
591 struct remote_group {
593 struct string_list *list;
596 static int get_remote_group(const char *key, const char *value, void *cb)
598 if (!prefixcmp(key, "remotes.") &&
599 !strcmp(key + 8, remote_group.name)) {
600 /* split list by white space */
601 int space = strcspn(value, " \t\n");
604 string_list_append(xstrndup(value, space),
606 value += space + (value[space] != '\0');
607 space = strcspn(value, " \t\n");
614 static int update(int argc, const char **argv)
617 struct string_list list = { NULL, 0, 0, 0 };
618 static const char *default_argv[] = { NULL, "default", NULL };
625 remote_group.list = &list;
626 for (i = 1; i < argc; i++) {
627 remote_group.name = argv[i];
628 result = git_config(get_remote_group, NULL);
631 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
632 result = for_each_remote(get_one_remote_for_update, &list);
634 for (i = 0; i < list.nr; i++)
635 result |= fetch_remote(list.items[i].string);
637 /* all names were strdup()ed or strndup()ed */
638 list.strdup_strings = 1;
639 string_list_clear(&list, 0);
644 static int get_one_entry(struct remote *remote, void *priv)
646 struct string_list *list = priv;
648 if (remote->url_nr > 0) {
651 for (i = 0; i < remote->url_nr; i++)
652 string_list_append(remote->name, list)->util = (void *)remote->url[i];
654 string_list_append(remote->name, list)->util = NULL;
659 static int show_all(void)
661 struct string_list list = { NULL, 0, 0 };
662 int result = for_each_remote(get_one_entry, &list);
667 sort_string_list(&list);
668 for (i = 0; i < list.nr; i++) {
669 struct string_list_item *item = list.items + i;
671 printf("%s\t%s\n", item->string,
672 item->util ? (const char *)item->util : "");
674 if (i && !strcmp((item - 1)->string, item->string))
676 printf("%s\n", item->string);
683 int cmd_remote(int argc, const char **argv, const char *prefix)
685 struct option options[] = {
686 OPT__VERBOSE(&verbose),
691 argc = parse_options(argc, argv, options, builtin_remote_usage,
692 PARSE_OPT_STOP_AT_NON_OPTION);
696 else if (!strcmp(argv[0], "add"))
697 result = add(argc, argv);
698 else if (!strcmp(argv[0], "rm"))
699 result = rm(argc, argv);
700 else if (!strcmp(argv[0], "show"))
701 result = show(argc, argv);
702 else if (!strcmp(argv[0], "prune"))
703 result = prune(argc, argv);
704 else if (!strcmp(argv[0], "update"))
705 result = update(argc, argv);
707 error("Unknown subcommand: %s", argv[0]);
708 usage_with_options(builtin_remote_usage, options);
711 return result ? 1 : 0;