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 inline const char *skip_prefix(const char *name, const char *prefix)
35 prefixcmp(name, prefix) ? name : name + strlen(prefix);
38 static int opt_parse_track(const struct option *opt, const char *arg, int not)
40 struct path_list *list = opt->value;
42 path_list_clear(list, 0);
44 path_list_append(arg, list);
48 static int fetch_remote(const char *name)
50 const char *argv[] = { "fetch", name, NULL };
51 printf("Updating %s\n", name);
52 if (run_command_v_opt(argv, RUN_GIT_CMD))
53 return error("Could not fetch %s", name);
57 static int add(int argc, const char **argv)
59 int fetch = 0, mirror = 0;
60 struct path_list track = { NULL, 0, 0 };
61 const char *master = NULL;
62 struct remote *remote;
63 struct strbuf buf, buf2;
64 const char *name, *url;
67 struct option options[] = {
68 OPT_GROUP("add specific options"),
69 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
70 OPT_CALLBACK('t', "track", &track, "branch",
71 "branch(es) to track", opt_parse_track),
72 OPT_STRING('m', "master", &master, "branch", "master branch"),
73 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
77 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
80 usage_with_options(builtin_remote_usage, options);
85 remote = remote_get(name);
86 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
87 remote->fetch_refspec_nr))
88 die("remote %s already exists.", name);
91 strbuf_init(&buf2, 0);
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 path_list_append("*", &track);
106 for (i = 0; i < track.nr; i++) {
107 struct path_list_item *item = track.items + i;
110 strbuf_addch(&buf2, '+');
112 strbuf_addf(&buf2, "refs/%s:refs/%s",
113 item->path, item->path);
115 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
116 item->path, name, item->path);
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, "yes"))
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 path_list_clear(&track, 0);
151 struct path_list merge;
154 static struct path_list branch_list;
156 static int config_read_branches(const char *key, const char *value, void *cb)
158 if (!prefixcmp(key, "branch.")) {
160 struct path_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 = path_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 = skip_prefix(value, "refs/heads/");
188 merge = xstrndup(value, space - value);
189 path_list_append(merge, &info->merge);
190 value = 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 = skip_prefix(refspec.src, "refs/heads/");
223 /* symbolic refs pointing nowhere were handled already */
224 if ((flags & REF_ISSYMREF) ||
225 unsorted_path_list_has_path(&states->tracked,
227 unsorted_path_list_has_path(&states->new,
230 item = path_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_paths = states->tracked.strdup_paths = 1;
247 for (ref = fetch_map; ref; ref = ref->next) {
248 struct path_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 path_list_append(skip_prefix(ref->name, "refs/heads/"),
260 target)->util = util;
262 free_refs(fetch_map);
264 for_each_ref(handle_one_branch, states);
265 sort_path_list(&states->stale);
270 struct known_remote {
271 struct known_remote *next;
272 struct remote *remote;
275 struct known_remotes {
276 struct remote *to_delete;
277 struct known_remote *list;
280 static int add_known_remote(struct remote *remote, void *cb_data)
282 struct known_remotes *all = cb_data;
283 struct known_remote *r;
285 if (!strcmp(all->to_delete->name, remote->name))
288 r = xmalloc(sizeof(*r));
295 struct branches_for_remote {
296 struct remote *remote;
297 struct path_list *branches;
298 struct known_remotes *keep;
301 static int add_branch_for_removal(const char *refname,
302 const unsigned char *sha1, int flags, void *cb_data)
304 struct branches_for_remote *branches = cb_data;
305 struct refspec refspec;
306 struct path_list_item *item;
307 struct known_remote *kr;
309 memset(&refspec, 0, sizeof(refspec));
310 refspec.dst = (char *)refname;
311 if (remote_find_tracking(branches->remote, &refspec))
314 /* don't delete a branch if another remote also uses it */
315 for (kr = branches->keep->list; kr; kr = kr->next) {
316 memset(&refspec, 0, sizeof(refspec));
317 refspec.dst = (char *)refname;
318 if (!remote_find_tracking(kr->remote, &refspec))
322 /* make sure that symrefs are deleted */
323 if (flags & REF_ISSYMREF)
324 return unlink(git_path(refname));
326 item = path_list_append(refname, branches->branches);
327 item->util = xmalloc(20);
328 hashcpy(item->util, sha1);
333 static int remove_branches(struct path_list *branches)
336 for (i = 0; i < branches->nr; i++) {
337 struct path_list_item *item = branches->items + i;
338 const char *refname = item->path;
339 unsigned char *sha1 = item->util;
341 if (delete_ref(refname, sha1))
342 result |= error("Could not remove branch %s", refname);
347 static int rm(int argc, const char **argv)
349 struct option options[] = {
352 struct remote *remote;
354 struct known_remotes known_remotes = { NULL, NULL };
355 struct path_list branches = { NULL, 0, 0, 1 };
356 struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
360 usage_with_options(builtin_remote_usage, options);
362 remote = remote_get(argv[1]);
364 die("No such remote: %s", argv[1]);
366 known_remotes.to_delete = remote;
367 for_each_remote(add_known_remote, &known_remotes);
369 strbuf_init(&buf, 0);
370 strbuf_addf(&buf, "remote.%s", remote->name);
371 if (git_config_rename_section(buf.buf, NULL) < 1)
372 return error("Could not remove config section '%s'", buf.buf);
375 for (i = 0; i < branch_list.nr; i++) {
376 struct path_list_item *item = branch_list.items + i;
377 struct branch_info *info = item->util;
378 if (info->remote && !strcmp(info->remote, remote->name)) {
379 const char *keys[] = { "remote", "merge", NULL }, **k;
380 for (k = keys; *k; k++) {
382 strbuf_addf(&buf, "branch.%s.%s",
384 if (git_config_set(buf.buf, NULL)) {
385 strbuf_release(&buf);
393 * We cannot just pass a function to for_each_ref() which deletes
394 * the branches one by one, since for_each_ref() relies on cached
395 * refs, which are invalidated when deleting a branch.
397 cb_data.remote = remote;
398 i = for_each_ref(add_branch_for_removal, &cb_data);
399 strbuf_release(&buf);
402 i = remove_branches(&branches);
403 path_list_clear(&branches, 1);
408 static void show_list(const char *title, struct path_list *list)
415 printf(title, list->nr > 1 ? "es" : "");
417 for (i = 0; i < list->nr; i++)
418 printf("%s%s", i ? " " : "", list->items[i].path);
422 static int show_or_prune(int argc, const char **argv, int prune)
424 int dry_run = 0, result = 0;
425 struct option options[] = {
426 OPT_GROUP("show specific options"),
427 OPT__DRY_RUN(&dry_run),
430 struct ref_states states;
432 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
437 usage_with_options(builtin_remote_usage, options);
440 memset(&states, 0, sizeof(states));
441 for (; argc; argc--, argv++) {
442 struct transport *transport;
443 const struct ref *ref;
447 states.remote = remote_get(*argv);
449 return error("No such remote: %s", *argv);
450 transport = transport_get(NULL, states.remote->url_nr > 0 ?
451 states.remote->url[0] : NULL);
452 ref = transport_get_remote_refs(transport);
453 transport_disconnect(transport);
456 got_states = get_ref_states(ref, &states);
458 result = error("Error getting local info for '%s'",
459 states.remote->name);
462 for (i = 0; i < states.stale.nr; i++) {
463 const char *refname = states.stale.items[i].util;
464 result |= delete_ref(refname, NULL);
469 printf("* remote %s\n URL: %s\n", *argv,
470 states.remote->url_nr > 0 ?
471 states.remote->url[0] : "(no URL)");
473 for (i = 0; i < branch_list.nr; i++) {
474 struct path_list_item *branch = branch_list.items + i;
475 struct branch_info *info = branch->util;
478 if (!info->merge.nr || strcmp(*argv, info->remote))
480 printf(" Remote branch%s merged with 'git pull' "
481 "while on branch %s\n ",
482 info->merge.nr > 1 ? "es" : "",
484 for (j = 0; j < info->merge.nr; j++)
485 printf(" %s", info->merge.items[j].path);
491 strbuf_init(&buf, 0);
492 strbuf_addf(&buf, " New remote branch%%s (next fetch will "
493 "store in remotes/%s)", states.remote->name);
494 show_list(buf.buf, &states.new);
495 strbuf_release(&buf);
496 show_list(" Stale tracking branch%s (use 'git remote prune')",
498 show_list(" Tracked remote branch%s",
501 if (states.remote->push_refspec_nr) {
502 printf(" Local branch%s pushed with 'git push'\n ",
503 states.remote->push_refspec_nr > 1 ?
505 for (i = 0; i < states.remote->push_refspec_nr; i++) {
506 struct refspec *spec = states.remote->push + i;
507 printf(" %s%s%s%s", spec->force ? "+" : "",
508 skip_prefix(spec->src, "refs/heads/"),
509 spec->dst ? ":" : "",
510 skip_prefix(spec->dst, "refs/heads/"));
515 /* NEEDSWORK: free remote */
516 path_list_clear(&states.new, 0);
517 path_list_clear(&states.stale, 0);
518 path_list_clear(&states.tracked, 0);
524 static int get_one_remote_for_update(struct remote *remote, void *priv)
526 struct path_list *list = priv;
527 if (!remote->skip_default_update)
528 path_list_append(xstrdup(remote->name), list);
532 struct remote_group {
534 struct path_list *list;
537 static int get_remote_group(const char *key, const char *value, void *cb)
539 if (!prefixcmp(key, "remotes.") &&
540 !strcmp(key + 8, remote_group.name)) {
541 /* split list by white space */
542 int space = strcspn(value, " \t\n");
545 path_list_append(xstrndup(value, space),
547 value += space + (value[space] != '\0');
548 space = strcspn(value, " \t\n");
555 static int update(int argc, const char **argv)
558 struct path_list list = { NULL, 0, 0, 0 };
559 static const char *default_argv[] = { NULL, "default", NULL };
566 remote_group.list = &list;
567 for (i = 1; i < argc; i++) {
568 remote_group.name = argv[i];
569 result = git_config(get_remote_group, NULL);
572 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
573 result = for_each_remote(get_one_remote_for_update, &list);
575 for (i = 0; i < list.nr; i++)
576 result |= fetch_remote(list.items[i].path);
578 /* all names were strdup()ed or strndup()ed */
579 list.strdup_paths = 1;
580 path_list_clear(&list, 0);
585 static int get_one_entry(struct remote *remote, void *priv)
587 struct path_list *list = priv;
589 path_list_append(remote->name, list)->util = remote->url_nr ?
590 (void *)remote->url[0] : NULL;
591 if (remote->url_nr > 1)
592 warning("Remote %s has more than one URL", remote->name);
597 static int show_all(void)
599 struct path_list list = { NULL, 0, 0 };
600 int result = for_each_remote(get_one_entry, &list);
605 sort_path_list(&list);
606 for (i = 0; i < list.nr; i++) {
607 struct path_list_item *item = list.items + i;
608 printf("%s%s%s\n", item->path,
610 verbose && item->util ?
611 (const char *)item->util : "");
617 int cmd_remote(int argc, const char **argv, const char *prefix)
619 struct option options[] = {
620 OPT__VERBOSE(&verbose),
625 argc = parse_options(argc, argv, options, builtin_remote_usage,
626 PARSE_OPT_STOP_AT_NON_OPTION);
630 else if (!strcmp(argv[0], "add"))
631 result = add(argc, argv);
632 else if (!strcmp(argv[0], "rm"))
633 result = rm(argc, argv);
634 else if (!strcmp(argv[0], "show"))
635 result = show_or_prune(argc, argv, 0);
636 else if (!strcmp(argv[0], "prune"))
637 result = show_or_prune(argc, argv, 1);
638 else if (!strcmp(argv[0], "update"))
639 result = update(argc, argv);
641 error("Unknown subcommand: %s", argv[0]);
642 usage_with_options(builtin_remote_usage, options);
645 return result ? 1 : 0;