builtin-remote: make rm operation safer in mirrored repository
[git/git.git] / builtin-remote.c
1 #include "cache.h"
2 #include "parse-options.h"
3 #include "transport.h"
4 #include "remote.h"
5 #include "string-list.h"
6 #include "strbuf.h"
7 #include "run-command.h"
8 #include "refs.h"
9
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
18 };
19
20 static int verbose;
21
22 static int show_all(void);
23
24 static inline int postfixcmp(const char *string, const char *postfix)
25 {
26 int len1 = strlen(string), len2 = strlen(postfix);
27 if (len1 < len2)
28 return 1;
29 return strcmp(string + len1 - len2, postfix);
30 }
31
32 static int opt_parse_track(const struct option *opt, const char *arg, int not)
33 {
34 struct string_list *list = opt->value;
35 if (not)
36 string_list_clear(list, 0);
37 else
38 string_list_append(arg, list);
39 return 0;
40 }
41
42 static int fetch_remote(const char *name)
43 {
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;
49 }
50
51 static int add(int argc, const char **argv)
52 {
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, buf2;
58 const char *name, *url;
59 int i;
60
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()
69 };
70
71 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
72
73 if (argc < 2)
74 usage_with_options(builtin_remote_usage, options);
75
76 name = argv[0];
77 url = argv[1];
78
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);
83
84 strbuf_init(&buf, 0);
85 strbuf_init(&buf2, 0);
86
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);
90
91 strbuf_addf(&buf, "remote.%s.url", name);
92 if (git_config_set(buf.buf, url))
93 return 1;
94
95 strbuf_reset(&buf);
96 strbuf_addf(&buf, "remote.%s.fetch", name);
97
98 if (track.nr == 0)
99 string_list_append("*", &track);
100 for (i = 0; i < track.nr; i++) {
101 struct string_list_item *item = track.items + i;
102
103 strbuf_reset(&buf2);
104 strbuf_addch(&buf2, '+');
105 if (mirror)
106 strbuf_addf(&buf2, "refs/%s:refs/%s",
107 item->string, item->string);
108 else
109 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
110 item->string, name, item->string);
111 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
112 return 1;
113 }
114
115 if (mirror) {
116 strbuf_reset(&buf);
117 strbuf_addf(&buf, "remote.%s.mirror", name);
118 if (git_config_set(buf.buf, "true"))
119 return 1;
120 }
121
122 if (fetch && fetch_remote(name))
123 return 1;
124
125 if (master) {
126 strbuf_reset(&buf);
127 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
128
129 strbuf_reset(&buf2);
130 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
131
132 if (create_symref(buf.buf, buf2.buf, "remote add"))
133 return error("Could not setup master '%s'", master);
134 }
135
136 strbuf_release(&buf);
137 strbuf_release(&buf2);
138 string_list_clear(&track, 0);
139
140 return 0;
141 }
142
143 struct branch_info {
144 char *remote;
145 struct string_list merge;
146 };
147
148 static struct string_list branch_list;
149
150 static const char *abbrev_ref(const char *name, const char *prefix)
151 {
152 const char *abbrev = skip_prefix(name, prefix);
153 if (abbrev)
154 return abbrev;
155 return name;
156 }
157 #define abbrev_branch(name) abbrev_ref((name), "refs/heads/")
158
159 static int config_read_branches(const char *key, const char *value, void *cb)
160 {
161 if (!prefixcmp(key, "branch.")) {
162 char *name;
163 struct string_list_item *item;
164 struct branch_info *info;
165 enum { REMOTE, MERGE } type;
166
167 key += 7;
168 if (!postfixcmp(key, ".remote")) {
169 name = xstrndup(key, strlen(key) - 7);
170 type = REMOTE;
171 } else if (!postfixcmp(key, ".merge")) {
172 name = xstrndup(key, strlen(key) - 6);
173 type = MERGE;
174 } else
175 return 0;
176
177 item = string_list_insert(name, &branch_list);
178
179 if (!item->util)
180 item->util = xcalloc(sizeof(struct branch_info), 1);
181 info = item->util;
182 if (type == REMOTE) {
183 if (info->remote)
184 warning("more than one branch.%s", key);
185 info->remote = xstrdup(value);
186 } else {
187 char *space = strchr(value, ' ');
188 value = abbrev_branch(value);
189 while (space) {
190 char *merge;
191 merge = xstrndup(value, space - value);
192 string_list_append(merge, &info->merge);
193 value = abbrev_branch(space + 1);
194 space = strchr(value, ' ');
195 }
196 string_list_append(xstrdup(value), &info->merge);
197 }
198 }
199 return 0;
200 }
201
202 static void read_branches(void)
203 {
204 if (branch_list.nr)
205 return;
206 git_config(config_read_branches, NULL);
207 sort_string_list(&branch_list);
208 }
209
210 struct ref_states {
211 struct remote *remote;
212 struct string_list new, stale, tracked;
213 };
214
215 static int handle_one_branch(const char *refname,
216 const unsigned char *sha1, int flags, void *cb_data)
217 {
218 struct ref_states *states = cb_data;
219 struct refspec refspec;
220
221 memset(&refspec, 0, sizeof(refspec));
222 refspec.dst = (char *)refname;
223 if (!remote_find_tracking(states->remote, &refspec)) {
224 struct string_list_item *item;
225 const char *name = abbrev_branch(refspec.src);
226 /* symbolic refs pointing nowhere were handled already */
227 if ((flags & REF_ISSYMREF) ||
228 unsorted_string_list_has_string(&states->tracked,
229 name) ||
230 unsorted_string_list_has_string(&states->new,
231 name))
232 return 0;
233 item = string_list_append(name, &states->stale);
234 item->util = xstrdup(refname);
235 }
236 return 0;
237 }
238
239 static int get_ref_states(const struct ref *ref, struct ref_states *states)
240 {
241 struct ref *fetch_map = NULL, **tail = &fetch_map;
242 int i;
243
244 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
245 if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
246 die("Could not get fetch map for refspec %s",
247 states->remote->fetch_refspec[i]);
248
249 states->new.strdup_strings = states->tracked.strdup_strings = 1;
250 for (ref = fetch_map; ref; ref = ref->next) {
251 struct string_list *target = &states->tracked;
252 unsigned char sha1[20];
253 void *util = NULL;
254
255 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
256 target = &states->new;
257 else {
258 target = &states->tracked;
259 if (hashcmp(sha1, ref->new_sha1))
260 util = &states;
261 }
262 string_list_append(abbrev_branch(ref->name), target)->util = util;
263 }
264 free_refs(fetch_map);
265
266 for_each_ref(handle_one_branch, states);
267 sort_string_list(&states->stale);
268
269 return 0;
270 }
271
272 struct known_remote {
273 struct known_remote *next;
274 struct remote *remote;
275 };
276
277 struct known_remotes {
278 struct remote *to_delete;
279 struct known_remote *list;
280 };
281
282 static int add_known_remote(struct remote *remote, void *cb_data)
283 {
284 struct known_remotes *all = cb_data;
285 struct known_remote *r;
286
287 if (!strcmp(all->to_delete->name, remote->name))
288 return 0;
289
290 r = xmalloc(sizeof(*r));
291 r->remote = remote;
292 r->next = all->list;
293 all->list = r;
294 return 0;
295 }
296
297 struct branches_for_remote {
298 struct remote *remote;
299 struct string_list *branches, *skipped;
300 struct known_remotes *keep;
301 };
302
303 static int add_branch_for_removal(const char *refname,
304 const unsigned char *sha1, int flags, void *cb_data)
305 {
306 struct branches_for_remote *branches = cb_data;
307 struct refspec refspec;
308 struct string_list_item *item;
309 struct known_remote *kr;
310
311 memset(&refspec, 0, sizeof(refspec));
312 refspec.dst = (char *)refname;
313 if (remote_find_tracking(branches->remote, &refspec))
314 return 0;
315
316 /* don't delete a branch if another remote also uses it */
317 for (kr = branches->keep->list; kr; kr = kr->next) {
318 memset(&refspec, 0, sizeof(refspec));
319 refspec.dst = (char *)refname;
320 if (!remote_find_tracking(kr->remote, &refspec))
321 return 0;
322 }
323
324 /* don't delete non-remote refs */
325 if (prefixcmp(refname, "refs/remotes")) {
326 /* advise user how to delete local branches */
327 if (!prefixcmp(refname, "refs/heads/"))
328 string_list_append(abbrev_branch(refname),
329 branches->skipped);
330 /* silently skip over other non-remote refs */
331 return 0;
332 }
333
334 /* make sure that symrefs are deleted */
335 if (flags & REF_ISSYMREF)
336 return unlink(git_path("%s", refname));
337
338 item = string_list_append(refname, branches->branches);
339 item->util = xmalloc(20);
340 hashcpy(item->util, sha1);
341
342 return 0;
343 }
344
345 static int remove_branches(struct string_list *branches)
346 {
347 int i, result = 0;
348 for (i = 0; i < branches->nr; i++) {
349 struct string_list_item *item = branches->items + i;
350 const char *refname = item->string;
351 unsigned char *sha1 = item->util;
352
353 if (delete_ref(refname, sha1, 0))
354 result |= error("Could not remove branch %s", refname);
355 }
356 return result;
357 }
358
359 static int rm(int argc, const char **argv)
360 {
361 struct option options[] = {
362 OPT_END()
363 };
364 struct remote *remote;
365 struct strbuf buf;
366 struct known_remotes known_remotes = { NULL, NULL };
367 struct string_list branches = { NULL, 0, 0, 1 };
368 struct string_list skipped = { NULL, 0, 0, 1 };
369 struct branches_for_remote cb_data = {
370 NULL, &branches, &skipped, &known_remotes
371 };
372 int i, result;
373
374 if (argc != 2)
375 usage_with_options(builtin_remote_usage, options);
376
377 remote = remote_get(argv[1]);
378 if (!remote)
379 die("No such remote: %s", argv[1]);
380
381 known_remotes.to_delete = remote;
382 for_each_remote(add_known_remote, &known_remotes);
383
384 strbuf_init(&buf, 0);
385 strbuf_addf(&buf, "remote.%s", remote->name);
386 if (git_config_rename_section(buf.buf, NULL) < 1)
387 return error("Could not remove config section '%s'", buf.buf);
388
389 read_branches();
390 for (i = 0; i < branch_list.nr; i++) {
391 struct string_list_item *item = branch_list.items + i;
392 struct branch_info *info = item->util;
393 if (info->remote && !strcmp(info->remote, remote->name)) {
394 const char *keys[] = { "remote", "merge", NULL }, **k;
395 for (k = keys; *k; k++) {
396 strbuf_reset(&buf);
397 strbuf_addf(&buf, "branch.%s.%s",
398 item->string, *k);
399 if (git_config_set(buf.buf, NULL)) {
400 strbuf_release(&buf);
401 return -1;
402 }
403 }
404 }
405 }
406
407 /*
408 * We cannot just pass a function to for_each_ref() which deletes
409 * the branches one by one, since for_each_ref() relies on cached
410 * refs, which are invalidated when deleting a branch.
411 */
412 cb_data.remote = remote;
413 result = for_each_ref(add_branch_for_removal, &cb_data);
414 strbuf_release(&buf);
415
416 if (!result)
417 result = remove_branches(&branches);
418 string_list_clear(&branches, 1);
419
420 if (skipped.nr) {
421 fprintf(stderr, skipped.nr == 1 ?
422 "Note: A non-remote branch was not removed; "
423 "to delete it, use:\n" :
424 "Note: Non-remote branches were not removed; "
425 "to delete them, use:\n");
426 for (i = 0; i < skipped.nr; i++)
427 fprintf(stderr, " git branch -d %s\n",
428 skipped.items[i].string);
429 }
430 string_list_clear(&skipped, 0);
431
432 return result;
433 }
434
435 static void show_list(const char *title, struct string_list *list,
436 const char *extra_arg)
437 {
438 int i;
439
440 if (!list->nr)
441 return;
442
443 printf(title, list->nr > 1 ? "es" : "", extra_arg);
444 printf("\n ");
445 for (i = 0; i < list->nr; i++)
446 printf("%s%s", i ? " " : "", list->items[i].string);
447 printf("\n");
448 }
449
450 static int get_remote_ref_states(const char *name,
451 struct ref_states *states,
452 int query)
453 {
454 struct transport *transport;
455 const struct ref *ref;
456
457 states->remote = remote_get(name);
458 if (!states->remote)
459 return error("No such remote: %s", name);
460
461 read_branches();
462
463 if (query) {
464 transport = transport_get(NULL, states->remote->url_nr > 0 ?
465 states->remote->url[0] : NULL);
466 ref = transport_get_remote_refs(transport);
467 transport_disconnect(transport);
468
469 get_ref_states(ref, states);
470 }
471
472 return 0;
473 }
474
475 static int append_ref_to_tracked_list(const char *refname,
476 const unsigned char *sha1, int flags, void *cb_data)
477 {
478 struct ref_states *states = cb_data;
479 struct refspec refspec;
480
481 memset(&refspec, 0, sizeof(refspec));
482 refspec.dst = (char *)refname;
483 if (!remote_find_tracking(states->remote, &refspec))
484 string_list_append(abbrev_branch(refspec.src), &states->tracked);
485
486 return 0;
487 }
488
489 static int show(int argc, const char **argv)
490 {
491 int no_query = 0, result = 0;
492 struct option options[] = {
493 OPT_GROUP("show specific options"),
494 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
495 OPT_END()
496 };
497 struct ref_states states;
498
499 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
500
501 if (argc < 1)
502 return show_all();
503
504 memset(&states, 0, sizeof(states));
505 for (; argc; argc--, argv++) {
506 int i;
507
508 get_remote_ref_states(*argv, &states, !no_query);
509
510 printf("* remote %s\n URL: %s\n", *argv,
511 states.remote->url_nr > 0 ?
512 states.remote->url[0] : "(no URL)");
513
514 for (i = 0; i < branch_list.nr; i++) {
515 struct string_list_item *branch = branch_list.items + i;
516 struct branch_info *info = branch->util;
517 int j;
518
519 if (!info->merge.nr || strcmp(*argv, info->remote))
520 continue;
521 printf(" Remote branch%s merged with 'git pull' "
522 "while on branch %s\n ",
523 info->merge.nr > 1 ? "es" : "",
524 branch->string);
525 for (j = 0; j < info->merge.nr; j++)
526 printf(" %s", info->merge.items[j].string);
527 printf("\n");
528 }
529
530 if (!no_query) {
531 show_list(" New remote branch%s (next fetch "
532 "will store in remotes/%s)",
533 &states.new, states.remote->name);
534 show_list(" Stale tracking branch%s (use 'git remote "
535 "prune')", &states.stale, "");
536 }
537
538 if (no_query)
539 for_each_ref(append_ref_to_tracked_list, &states);
540 show_list(" Tracked remote branch%s", &states.tracked, "");
541
542 if (states.remote->push_refspec_nr) {
543 printf(" Local branch%s pushed with 'git push'\n ",
544 states.remote->push_refspec_nr > 1 ?
545 "es" : "");
546 for (i = 0; i < states.remote->push_refspec_nr; i++) {
547 struct refspec *spec = states.remote->push + i;
548 printf(" %s%s%s%s", spec->force ? "+" : "",
549 abbrev_branch(spec->src),
550 spec->dst ? ":" : "",
551 spec->dst ? abbrev_branch(spec->dst) : "");
552 }
553 printf("\n");
554 }
555
556 /* NEEDSWORK: free remote */
557 string_list_clear(&states.new, 0);
558 string_list_clear(&states.stale, 0);
559 string_list_clear(&states.tracked, 0);
560 }
561
562 return result;
563 }
564
565 static int prune(int argc, const char **argv)
566 {
567 int dry_run = 0, result = 0;
568 struct option options[] = {
569 OPT_GROUP("prune specific options"),
570 OPT__DRY_RUN(&dry_run),
571 OPT_END()
572 };
573 struct ref_states states;
574
575 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
576
577 if (argc < 1)
578 usage_with_options(builtin_remote_usage, options);
579
580 memset(&states, 0, sizeof(states));
581 for (; argc; argc--, argv++) {
582 int i;
583
584 get_remote_ref_states(*argv, &states, 1);
585
586 if (states.stale.nr) {
587 printf("Pruning %s\n", *argv);
588 printf("URL: %s\n",
589 states.remote->url_nr
590 ? states.remote->url[0]
591 : "(no URL)");
592 }
593
594 for (i = 0; i < states.stale.nr; i++) {
595 const char *refname = states.stale.items[i].util;
596
597 if (!dry_run)
598 result |= delete_ref(refname, NULL, 0);
599
600 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
601 abbrev_ref(refname, "refs/remotes/"));
602 }
603
604 /* NEEDSWORK: free remote */
605 string_list_clear(&states.new, 0);
606 string_list_clear(&states.stale, 0);
607 string_list_clear(&states.tracked, 0);
608 }
609
610 return result;
611 }
612
613 static int get_one_remote_for_update(struct remote *remote, void *priv)
614 {
615 struct string_list *list = priv;
616 if (!remote->skip_default_update)
617 string_list_append(xstrdup(remote->name), list);
618 return 0;
619 }
620
621 struct remote_group {
622 const char *name;
623 struct string_list *list;
624 } remote_group;
625
626 static int get_remote_group(const char *key, const char *value, void *cb)
627 {
628 if (!prefixcmp(key, "remotes.") &&
629 !strcmp(key + 8, remote_group.name)) {
630 /* split list by white space */
631 int space = strcspn(value, " \t\n");
632 while (*value) {
633 if (space > 1)
634 string_list_append(xstrndup(value, space),
635 remote_group.list);
636 value += space + (value[space] != '\0');
637 space = strcspn(value, " \t\n");
638 }
639 }
640
641 return 0;
642 }
643
644 static int update(int argc, const char **argv)
645 {
646 int i, result = 0;
647 struct string_list list = { NULL, 0, 0, 0 };
648 static const char *default_argv[] = { NULL, "default", NULL };
649
650 if (argc < 2) {
651 argc = 2;
652 argv = default_argv;
653 }
654
655 remote_group.list = &list;
656 for (i = 1; i < argc; i++) {
657 remote_group.name = argv[i];
658 result = git_config(get_remote_group, NULL);
659 }
660
661 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
662 result = for_each_remote(get_one_remote_for_update, &list);
663
664 for (i = 0; i < list.nr; i++)
665 result |= fetch_remote(list.items[i].string);
666
667 /* all names were strdup()ed or strndup()ed */
668 list.strdup_strings = 1;
669 string_list_clear(&list, 0);
670
671 return result;
672 }
673
674 static int get_one_entry(struct remote *remote, void *priv)
675 {
676 struct string_list *list = priv;
677
678 if (remote->url_nr > 0) {
679 int i;
680
681 for (i = 0; i < remote->url_nr; i++)
682 string_list_append(remote->name, list)->util = (void *)remote->url[i];
683 } else
684 string_list_append(remote->name, list)->util = NULL;
685
686 return 0;
687 }
688
689 static int show_all(void)
690 {
691 struct string_list list = { NULL, 0, 0 };
692 int result = for_each_remote(get_one_entry, &list);
693
694 if (!result) {
695 int i;
696
697 sort_string_list(&list);
698 for (i = 0; i < list.nr; i++) {
699 struct string_list_item *item = list.items + i;
700 if (verbose)
701 printf("%s\t%s\n", item->string,
702 item->util ? (const char *)item->util : "");
703 else {
704 if (i && !strcmp((item - 1)->string, item->string))
705 continue;
706 printf("%s\n", item->string);
707 }
708 }
709 }
710 return result;
711 }
712
713 int cmd_remote(int argc, const char **argv, const char *prefix)
714 {
715 struct option options[] = {
716 OPT__VERBOSE(&verbose),
717 OPT_END()
718 };
719 int result;
720
721 argc = parse_options(argc, argv, options, builtin_remote_usage,
722 PARSE_OPT_STOP_AT_NON_OPTION);
723
724 if (argc < 1)
725 result = show_all();
726 else if (!strcmp(argv[0], "add"))
727 result = add(argc, argv);
728 else if (!strcmp(argv[0], "rm"))
729 result = rm(argc, argv);
730 else if (!strcmp(argv[0], "show"))
731 result = show(argc, argv);
732 else if (!strcmp(argv[0], "prune"))
733 result = prune(argc, argv);
734 else if (!strcmp(argv[0], "update"))
735 result = update(argc, argv);
736 else {
737 error("Unknown subcommand: %s", argv[0]);
738 usage_with_options(builtin_remote_usage, options);
739 }
740
741 return result ? 1 : 0;
742 }