]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-remote.c
builtin-remote: split show_or_prune() in two separate functions
[thirdparty/git.git] / builtin-remote.c
CommitLineData
211c8968
JS
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"
9
10static 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
20static int verbose;
21
c4112bb6
JK
22static int show_all(void);
23
211c8968
JS
24static 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
32static inline const char *skip_prefix(const char *name, const char *prefix)
33{
34 return !name ? "" :
35 prefixcmp(name, prefix) ? name : name + strlen(prefix);
36}
37
38static int opt_parse_track(const struct option *opt, const char *arg, int not)
39{
40 struct path_list *list = opt->value;
41 if (not)
42 path_list_clear(list, 0);
43 else
44 path_list_append(arg, list);
45 return 0;
46}
47
48static int fetch_remote(const char *name)
49{
50 const char *argv[] = { "fetch", name, NULL };
3000658f 51 printf("Updating %s\n", name);
211c8968
JS
52 if (run_command_v_opt(argv, RUN_GIT_CMD))
53 return error("Could not fetch %s", name);
54 return 0;
55}
56
57static int add(int argc, const char **argv)
58{
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;
65 int i;
66
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"),
74 OPT_END()
75 };
76
77 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
78
79 if (argc < 2)
80 usage_with_options(builtin_remote_usage, options);
81
82 name = argv[0];
83 url = argv[1];
84
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);
89
90 strbuf_init(&buf, 0);
91 strbuf_init(&buf2, 0);
92
24b6177e
JF
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);
96
211c8968
JS
97 strbuf_addf(&buf, "remote.%s.url", name);
98 if (git_config_set(buf.buf, url))
99 return 1;
100
24b6177e
JF
101 strbuf_reset(&buf);
102 strbuf_addf(&buf, "remote.%s.fetch", name);
103
211c8968
JS
104 if (track.nr == 0)
105 path_list_append("*", &track);
106 for (i = 0; i < track.nr; i++) {
107 struct path_list_item *item = track.items + i;
108
211c8968 109 strbuf_reset(&buf2);
1ce89cc4 110 strbuf_addch(&buf2, '+');
211c8968
JS
111 if (mirror)
112 strbuf_addf(&buf2, "refs/%s:refs/%s",
113 item->path, item->path);
114 else
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))
118 return 1;
119 }
120
84bb2dfd
PB
121 if (mirror) {
122 strbuf_reset(&buf);
123 strbuf_addf(&buf, "remote.%s.mirror", name);
124 if (git_config_set(buf.buf, "yes"))
125 return 1;
126 }
127
211c8968
JS
128 if (fetch && fetch_remote(name))
129 return 1;
130
131 if (master) {
132 strbuf_reset(&buf);
133 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
134
135 strbuf_reset(&buf2);
136 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
137
138 if (create_symref(buf.buf, buf2.buf, "remote add"))
139 return error("Could not setup master '%s'", master);
140 }
141
142 strbuf_release(&buf);
143 strbuf_release(&buf2);
144 path_list_clear(&track, 0);
145
146 return 0;
147}
148
149struct branch_info {
150 char *remote;
151 struct path_list merge;
152};
153
154static struct path_list branch_list;
155
ef90d6d4 156static int config_read_branches(const char *key, const char *value, void *cb)
211c8968
JS
157{
158 if (!prefixcmp(key, "branch.")) {
159 char *name;
160 struct path_list_item *item;
161 struct branch_info *info;
162 enum { REMOTE, MERGE } type;
163
164 key += 7;
165 if (!postfixcmp(key, ".remote")) {
166 name = xstrndup(key, strlen(key) - 7);
167 type = REMOTE;
168 } else if (!postfixcmp(key, ".merge")) {
169 name = xstrndup(key, strlen(key) - 6);
170 type = MERGE;
171 } else
172 return 0;
173
174 item = path_list_insert(name, &branch_list);
175
176 if (!item->util)
177 item->util = xcalloc(sizeof(struct branch_info), 1);
178 info = item->util;
179 if (type == REMOTE) {
180 if (info->remote)
181 warning("more than one branch.%s", key);
182 info->remote = xstrdup(value);
183 } else {
184 char *space = strchr(value, ' ');
185 value = skip_prefix(value, "refs/heads/");
186 while (space) {
187 char *merge;
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, ' ');
192 }
193 path_list_append(xstrdup(value), &info->merge);
194 }
195 }
196 return 0;
197}
198
199static void read_branches(void)
200{
201 if (branch_list.nr)
202 return;
ef90d6d4 203 git_config(config_read_branches, NULL);
211c8968
JS
204 sort_path_list(&branch_list);
205}
206
207struct ref_states {
208 struct remote *remote;
211c8968
JS
209 struct path_list new, stale, tracked;
210};
211
212static int handle_one_branch(const char *refname,
213 const unsigned char *sha1, int flags, void *cb_data)
214{
215 struct ref_states *states = cb_data;
216 struct refspec refspec;
217
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/");
740fdd27
JS
223 /* symbolic refs pointing nowhere were handled already */
224 if ((flags & REF_ISSYMREF) ||
225 unsorted_path_list_has_path(&states->tracked,
226 name) ||
211c8968
JS
227 unsorted_path_list_has_path(&states->new,
228 name))
229 return 0;
230 item = path_list_append(name, &states->stale);
231 item->util = xstrdup(refname);
232 }
233 return 0;
234}
235
236static int get_ref_states(const struct ref *ref, struct ref_states *states)
237{
238 struct ref *fetch_map = NULL, **tail = &fetch_map;
239 int i;
240
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]);
245
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];
250 void *util = NULL;
251
252 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
253 target = &states->new;
254 else {
255 target = &states->tracked;
256 if (hashcmp(sha1, ref->new_sha1))
257 util = &states;
258 }
259 path_list_append(skip_prefix(ref->name, "refs/heads/"),
260 target)->util = util;
261 }
262 free_refs(fetch_map);
263
211c8968
JS
264 for_each_ref(handle_one_branch, states);
265 sort_path_list(&states->stale);
266
267 return 0;
268}
269
7ad2458f
SP
270struct known_remote {
271 struct known_remote *next;
272 struct remote *remote;
273};
274
275struct known_remotes {
276 struct remote *to_delete;
277 struct known_remote *list;
278};
279
280static int add_known_remote(struct remote *remote, void *cb_data)
281{
282 struct known_remotes *all = cb_data;
283 struct known_remote *r;
284
285 if (!strcmp(all->to_delete->name, remote->name))
286 return 0;
287
288 r = xmalloc(sizeof(*r));
289 r->remote = remote;
290 r->next = all->list;
291 all->list = r;
292 return 0;
293}
294
211c8968 295struct branches_for_remote {
7ad2458f 296 struct remote *remote;
211c8968 297 struct path_list *branches;
7ad2458f 298 struct known_remotes *keep;
211c8968
JS
299};
300
301static int add_branch_for_removal(const char *refname,
302 const unsigned char *sha1, int flags, void *cb_data)
303{
304 struct branches_for_remote *branches = cb_data;
7ad2458f
SP
305 struct refspec refspec;
306 struct path_list_item *item;
307 struct known_remote *kr;
211c8968 308
7ad2458f
SP
309 memset(&refspec, 0, sizeof(refspec));
310 refspec.dst = (char *)refname;
311 if (remote_find_tracking(branches->remote, &refspec))
312 return 0;
313
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))
319 return 0;
320 }
3b9dcff5 321
7ad2458f
SP
322 /* make sure that symrefs are deleted */
323 if (flags & REF_ISSYMREF)
324 return unlink(git_path(refname));
3b9dcff5 325
7ad2458f
SP
326 item = path_list_append(refname, branches->branches);
327 item->util = xmalloc(20);
328 hashcpy(item->util, sha1);
211c8968
JS
329
330 return 0;
331}
332
333static int remove_branches(struct path_list *branches)
334{
335 int i, result = 0;
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;
340
341 if (delete_ref(refname, sha1))
342 result |= error("Could not remove branch %s", refname);
343 }
344 return result;
345}
346
347static int rm(int argc, const char **argv)
348{
349 struct option options[] = {
350 OPT_END()
351 };
352 struct remote *remote;
353 struct strbuf buf;
7ad2458f 354 struct known_remotes known_remotes = { NULL, NULL };
211c8968 355 struct path_list branches = { NULL, 0, 0, 1 };
7ad2458f 356 struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
211c8968
JS
357 int i;
358
359 if (argc != 2)
360 usage_with_options(builtin_remote_usage, options);
361
362 remote = remote_get(argv[1]);
363 if (!remote)
364 die("No such remote: %s", argv[1]);
365
7ad2458f
SP
366 known_remotes.to_delete = remote;
367 for_each_remote(add_known_remote, &known_remotes);
368
211c8968
JS
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);
373
374 read_branches();
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++) {
381 strbuf_reset(&buf);
382 strbuf_addf(&buf, "branch.%s.%s",
383 item->path, *k);
384 if (git_config_set(buf.buf, NULL)) {
385 strbuf_release(&buf);
386 return -1;
387 }
388 }
389 }
390 }
391
392 /*
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.
396 */
7ad2458f 397 cb_data.remote = remote;
211c8968
JS
398 i = for_each_ref(add_branch_for_removal, &cb_data);
399 strbuf_release(&buf);
400
401 if (!i)
402 i = remove_branches(&branches);
403 path_list_clear(&branches, 1);
404
405 return i;
406}
407
408static void show_list(const char *title, struct path_list *list)
409{
410 int i;
411
412 if (!list->nr)
413 return;
414
415 printf(title, list->nr > 1 ? "es" : "");
416 printf("\n ");
417 for (i = 0; i < list->nr; i++)
418 printf("%s%s", i ? " " : "", list->items[i].path);
419 printf("\n");
420}
421
67a7e2d0
OM
422static int get_remote_ref_states(const char *name,
423 struct ref_states *states,
424 int query)
425{
426 struct transport *transport;
427 const struct ref *ref;
428
429 states->remote = remote_get(name);
430 if (!states->remote)
431 return error("No such remote: %s", name);
432
433 read_branches();
434
435 if (query) {
436 transport = transport_get(NULL, states->remote->url_nr > 0 ?
437 states->remote->url[0] : NULL);
438 ref = transport_get_remote_refs(transport);
439 transport_disconnect(transport);
440
441 get_ref_states(ref, states);
442 }
443
444 return 0;
445}
446
447static int show(int argc, const char **argv)
211c8968 448{
0ecfcb3b 449 int no_query = 0, result = 0;
211c8968
JS
450 struct option options[] = {
451 OPT_GROUP("show specific options"),
0ecfcb3b 452 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
211c8968
JS
453 OPT_END()
454 };
455 struct ref_states states;
456
457 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
458
67a7e2d0
OM
459 if (argc < 1)
460 return show_all();
211c8968
JS
461
462 memset(&states, 0, sizeof(states));
463 for (; argc; argc--, argv++) {
211c8968 464 struct strbuf buf;
0ecfcb3b 465 int i;
211c8968 466
67a7e2d0 467 get_remote_ref_states(*argv, &states, !no_query);
211c8968
JS
468
469 printf("* remote %s\n URL: %s\n", *argv,
470 states.remote->url_nr > 0 ?
471 states.remote->url[0] : "(no URL)");
472
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;
476 int j;
477
478 if (!info->merge.nr || strcmp(*argv, info->remote))
479 continue;
480 printf(" Remote branch%s merged with 'git pull' "
481 "while on branch %s\n ",
482 info->merge.nr > 1 ? "es" : "",
483 branch->path);
484 for (j = 0; j < info->merge.nr; j++)
485 printf(" %s", info->merge.items[j].path);
486 printf("\n");
487 }
488
0ecfcb3b
OM
489 if (!no_query) {
490 strbuf_init(&buf, 0);
491 strbuf_addf(&buf, " New remote branch%%s (next fetch "
492 "will store in remotes/%s)", states.remote->name);
493 show_list(buf.buf, &states.new);
494 strbuf_release(&buf);
495 show_list(" Stale tracking branch%s (use 'git remote "
496 "prune')", &states.stale);
497 show_list(" Tracked remote branch%s",
211c8968 498 &states.tracked);
0ecfcb3b 499 }
211c8968
JS
500
501 if (states.remote->push_refspec_nr) {
502 printf(" Local branch%s pushed with 'git push'\n ",
503 states.remote->push_refspec_nr > 1 ?
504 "es" : "");
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/"));
511 }
ec31b0ce 512 printf("\n");
211c8968 513 }
67a7e2d0
OM
514
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);
519 }
520
521 return result;
522}
523
524static int prune(int argc, const char **argv)
525{
526 int no_query = 0, result = 0;
527 struct option options[] = {
528 OPT_GROUP("prune specific options"),
529 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
530 OPT_END()
531 };
532 struct ref_states states;
533
534 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
535
536 if (argc < 1)
537 usage_with_options(builtin_remote_usage, options);
538
539 memset(&states, 0, sizeof(states));
540 for (; argc; argc--, argv++) {
541 int i;
542
543 get_remote_ref_states(*argv, &states, !no_query);
544
545 for (i = 0; i < states.stale.nr; i++) {
546 const char *refname = states.stale.items[i].util;
547 result |= delete_ref(refname, NULL);
548 }
549
211c8968
JS
550 /* NEEDSWORK: free remote */
551 path_list_clear(&states.new, 0);
552 path_list_clear(&states.stale, 0);
553 path_list_clear(&states.tracked, 0);
554 }
555
556 return result;
557}
558
84521ed6 559static int get_one_remote_for_update(struct remote *remote, void *priv)
211c8968 560{
84521ed6 561 struct path_list *list = priv;
211c8968 562 if (!remote->skip_default_update)
84521ed6
JS
563 path_list_append(xstrdup(remote->name), list);
564 return 0;
565}
566
567struct remote_group {
568 const char *name;
569 struct path_list *list;
570} remote_group;
571
ef90d6d4 572static int get_remote_group(const char *key, const char *value, void *cb)
84521ed6
JS
573{
574 if (!prefixcmp(key, "remotes.") &&
575 !strcmp(key + 8, remote_group.name)) {
576 /* split list by white space */
577 int space = strcspn(value, " \t\n");
578 while (*value) {
579 if (space > 1)
580 path_list_append(xstrndup(value, space),
581 remote_group.list);
582 value += space + (value[space] != '\0');
583 space = strcspn(value, " \t\n");
584 }
585 }
586
211c8968
JS
587 return 0;
588}
589
590static int update(int argc, const char **argv)
591{
84521ed6
JS
592 int i, result = 0;
593 struct path_list list = { NULL, 0, 0, 0 };
594 static const char *default_argv[] = { NULL, "default", NULL };
211c8968 595
84521ed6
JS
596 if (argc < 2) {
597 argc = 2;
598 argv = default_argv;
599 }
211c8968 600
84521ed6
JS
601 remote_group.list = &list;
602 for (i = 1; i < argc; i++) {
603 remote_group.name = argv[i];
ef90d6d4 604 result = git_config(get_remote_group, NULL);
84521ed6
JS
605 }
606
607 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
608 result = for_each_remote(get_one_remote_for_update, &list);
609
610 for (i = 0; i < list.nr; i++)
611 result |= fetch_remote(list.items[i].path);
612
613 /* all names were strdup()ed or strndup()ed */
614 list.strdup_paths = 1;
615 path_list_clear(&list, 0);
616
617 return result;
211c8968
JS
618}
619
620static int get_one_entry(struct remote *remote, void *priv)
621{
622 struct path_list *list = priv;
623
624 path_list_append(remote->name, list)->util = remote->url_nr ?
625 (void *)remote->url[0] : NULL;
626 if (remote->url_nr > 1)
627 warning("Remote %s has more than one URL", remote->name);
628
629 return 0;
630}
631
632static int show_all(void)
633{
634 struct path_list list = { NULL, 0, 0 };
635 int result = for_each_remote(get_one_entry, &list);
636
637 if (!result) {
638 int i;
639
640 sort_path_list(&list);
641 for (i = 0; i < list.nr; i++) {
642 struct path_list_item *item = list.items + i;
643 printf("%s%s%s\n", item->path,
644 verbose ? "\t" : "",
645 verbose && item->util ?
646 (const char *)item->util : "");
647 }
648 }
649 return result;
650}
651
652int cmd_remote(int argc, const char **argv, const char *prefix)
653{
654 struct option options[] = {
655 OPT__VERBOSE(&verbose),
656 OPT_END()
657 };
658 int result;
659
660 argc = parse_options(argc, argv, options, builtin_remote_usage,
661 PARSE_OPT_STOP_AT_NON_OPTION);
662
663 if (argc < 1)
664 result = show_all();
665 else if (!strcmp(argv[0], "add"))
666 result = add(argc, argv);
667 else if (!strcmp(argv[0], "rm"))
668 result = rm(argc, argv);
669 else if (!strcmp(argv[0], "show"))
67a7e2d0 670 result = show(argc, argv);
211c8968 671 else if (!strcmp(argv[0], "prune"))
67a7e2d0 672 result = prune(argc, argv);
211c8968
JS
673 else if (!strcmp(argv[0], "update"))
674 result = update(argc, argv);
675 else {
676 error("Unknown subcommand: %s", argv[0]);
677 usage_with_options(builtin_remote_usage, options);
678 }
679
680 return result ? 1 : 0;
681}