t6021: add a new test for git-merge-resolve
[git/dscho.git] / builtin-remote.c
blob1491354a9d5d22546acde09021b0f666f6740257
1 #include "cache.h"
2 #include "parse-options.h"
3 #include "transport.h"
4 #include "remote.h"
5 #include "path-list.h"
6 #include "strbuf.h"
7 #include "run-command.h"
8 #include "refs.h"
10 static const char * const builtin_remote_usage[] = {
11 "git remote",
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]",
17 NULL
20 static int verbose;
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);
27 if (len1 < len2)
28 return 1;
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;
35 if (not)
36 path_list_clear(list, 0);
37 else
38 path_list_append(arg, list);
39 return 0;
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);
48 return 0;
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;
59 int i;
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"),
68 OPT_END()
71 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
73 if (argc < 2)
74 usage_with_options(builtin_remote_usage, options);
76 name = argv[0];
77 url = argv[1];
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_init(&buf, 0);
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))
93 return 1;
95 strbuf_reset(&buf);
96 strbuf_addf(&buf, "remote.%s.fetch", name);
98 if (track.nr == 0)
99 path_list_append("*", &track);
100 for (i = 0; i < track.nr; i++) {
101 struct path_list_item *item = track.items + i;
103 strbuf_reset(&buf2);
104 strbuf_addch(&buf2, '+');
105 if (mirror)
106 strbuf_addf(&buf2, "refs/%s:refs/%s",
107 item->path, item->path);
108 else
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))
112 return 1;
115 if (mirror) {
116 strbuf_reset(&buf);
117 strbuf_addf(&buf, "remote.%s.mirror", name);
118 if (git_config_set(buf.buf, "yes"))
119 return 1;
122 if (fetch && fetch_remote(name))
123 return 1;
125 if (master) {
126 strbuf_reset(&buf);
127 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
129 strbuf_reset(&buf2);
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);
140 return 0;
143 struct branch_info {
144 char *remote;
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.")) {
153 char *name;
154 struct path_list_item *item;
155 struct branch_info *info;
156 enum { REMOTE, MERGE } type;
158 key += 7;
159 if (!postfixcmp(key, ".remote")) {
160 name = xstrndup(key, strlen(key) - 7);
161 type = REMOTE;
162 } else if (!postfixcmp(key, ".merge")) {
163 name = xstrndup(key, strlen(key) - 6);
164 type = MERGE;
165 } else
166 return 0;
168 item = path_list_insert(name, &branch_list);
170 if (!item->util)
171 item->util = xcalloc(sizeof(struct branch_info), 1);
172 info = item->util;
173 if (type == REMOTE) {
174 if (info->remote)
175 warning("more than one branch.%s", key);
176 info->remote = xstrdup(value);
177 } else {
178 char *space = strchr(value, ' ');
179 const char *ptr = skip_prefix(value, "refs/heads/");
180 if (ptr)
181 value = ptr;
182 while (space) {
183 char *merge;
184 merge = xstrndup(value, space - value);
185 path_list_append(merge, &info->merge);
186 ptr = skip_prefix(space + 1, "refs/heads/");
187 if (ptr)
188 value = ptr;
189 else
190 value = space + 1;
191 space = strchr(value, ' ');
193 path_list_append(xstrdup(value), &info->merge);
196 return 0;
199 static void read_branches(void)
201 if (branch_list.nr)
202 return;
203 git_config(config_read_branches, NULL);
204 sort_path_list(&branch_list);
207 struct ref_states {
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/");
224 if (ptr)
225 name = ptr;
226 else
227 name = refspec.src;
228 /* symbolic refs pointing nowhere were handled already */
229 if ((flags & REF_ISSYMREF) ||
230 unsorted_path_list_has_path(&states->tracked,
231 name) ||
232 unsorted_path_list_has_path(&states->new,
233 name))
234 return 0;
235 item = path_list_append(name, &states->stale);
236 item->util = xstrdup(refname);
238 return 0;
241 static int get_ref_states(const struct ref *ref, struct ref_states *states)
243 struct ref *fetch_map = NULL, **tail = &fetch_map;
244 int i;
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];
255 void *util = NULL;
256 const char *ptr;
258 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
259 target = &states->new;
260 else {
261 target = &states->tracked;
262 if (hashcmp(sha1, ref->new_sha1))
263 util = &states;
265 ptr = skip_prefix(ref->name, "refs/heads/");
266 if (!ptr)
267 ptr = ref->name;
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);
275 return 0;
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))
294 return 0;
296 r = xmalloc(sizeof(*r));
297 r->remote = remote;
298 r->next = all->list;
299 all->list = r;
300 return 0;
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))
320 return 0;
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))
327 return 0;
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);
338 return 0;
341 static int remove_branches(struct path_list *branches)
343 int i, result = 0;
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);
352 return result;
355 static int rm(int argc, const char **argv)
357 struct option options[] = {
358 OPT_END()
360 struct remote *remote;
361 struct strbuf buf;
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 };
365 int i;
367 if (argc != 2)
368 usage_with_options(builtin_remote_usage, options);
370 remote = remote_get(argv[1]);
371 if (!remote)
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);
382 read_branches();
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++) {
389 strbuf_reset(&buf);
390 strbuf_addf(&buf, "branch.%s.%s",
391 item->path, *k);
392 if (git_config_set(buf.buf, NULL)) {
393 strbuf_release(&buf);
394 return -1;
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);
409 if (!i)
410 i = remove_branches(&branches);
411 path_list_clear(&branches, 1);
413 return i;
416 static void show_list(const char *title, struct path_list *list)
418 int i;
420 if (!list->nr)
421 return;
423 printf(title, list->nr > 1 ? "es" : "");
424 printf("\n ");
425 for (i = 0; i < list->nr; i++)
426 printf("%s%s", i ? " " : "", list->items[i].path);
427 printf("\n");
430 static int get_remote_ref_states(const char *name,
431 struct ref_states *states,
432 int query)
434 struct transport *transport;
435 const struct ref *ref;
437 states->remote = remote_get(name);
438 if (!states->remote)
439 return error("No such remote: %s", name);
441 read_branches();
443 if (query) {
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);
452 return 0;
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/"),
465 &states->tracked);
468 return 0;
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"),
477 OPT_END()
479 struct ref_states states;
481 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
483 if (argc < 1)
484 return show_all();
486 memset(&states, 0, sizeof(states));
487 for (; argc; argc--, argv++) {
488 struct strbuf buf;
489 int i;
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;
500 int j;
502 if (!info->merge.nr || strcmp(*argv, info->remote))
503 continue;
504 printf(" Remote branch%s merged with 'git pull' "
505 "while on branch %s\n ",
506 info->merge.nr > 1 ? "es" : "",
507 branch->path);
508 for (j = 0; j < info->merge.nr; j++)
509 printf(" %s", info->merge.items[j].path);
510 printf("\n");
513 if (!no_query) {
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);
523 if (no_query)
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 ?
530 "es" : "");
531 for (i = 0; i < states.remote->push_refspec_nr; i++) {
532 struct refspec *spec = states.remote->push + i;
533 const char *p = "", *q = "";
534 if (spec->src)
535 p = skip_prefix(spec->src, "refs/heads/");
536 if (spec->dst)
537 q = skip_prefix(spec->dst, "refs/heads/");
538 printf(" %s%s%s%s", spec->force ? "+" : "",
539 p ? p : spec->src,
540 spec->dst ? ":" : "",
541 q ? q : spec->dst);
543 printf("\n");
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);
552 return result;
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),
561 OPT_END()
563 struct ref_states states;
565 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
567 if (argc < 1)
568 usage_with_options(builtin_remote_usage, options);
570 memset(&states, 0, sizeof(states));
571 for (; argc; argc--, argv++) {
572 int i;
574 get_remote_ref_states(*argv, &states, 1);
576 if (states.stale.nr) {
577 printf("Pruning %s\n", *argv);
578 printf("URL: %s\n",
579 states.remote->url_nr
580 ? states.remote->url[0]
581 : "(no URL)");
584 for (i = 0; i < states.stale.nr; i++) {
585 const char *refname = states.stale.items[i].util;
587 if (!dry_run)
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);
600 return result;
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);
608 return 0;
611 struct remote_group {
612 const char *name;
613 struct path_list *list;
614 } remote_group;
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");
622 while (*value) {
623 if (space > 1)
624 path_list_append(xstrndup(value, space),
625 remote_group.list);
626 value += space + (value[space] != '\0');
627 space = strcspn(value, " \t\n");
631 return 0;
634 static int update(int argc, const char **argv)
636 int i, result = 0;
637 struct path_list list = { NULL, 0, 0, 0 };
638 static const char *default_argv[] = { NULL, "default", NULL };
640 if (argc < 2) {
641 argc = 2;
642 argv = default_argv;
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);
661 return result;
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);
673 return 0;
676 static int show_all(void)
678 struct path_list list = { NULL, 0, 0 };
679 int result = for_each_remote(get_one_entry, &list);
681 if (!result) {
682 int i;
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,
688 verbose ? "\t" : "",
689 verbose && item->util ?
690 (const char *)item->util : "");
693 return result;
696 int cmd_remote(int argc, const char **argv, const char *prefix)
698 struct option options[] = {
699 OPT__VERBOSE(&verbose),
700 OPT_END()
702 int result;
704 argc = parse_options(argc, argv, options, builtin_remote_usage,
705 PARSE_OPT_STOP_AT_NON_OPTION);
707 if (argc < 1)
708 result = show_all();
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);
719 else {
720 error("Unknown subcommand: %s", argv[0]);
721 usage_with_options(builtin_remote_usage, options);
724 return result ? 1 : 0;