]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-remote.c
Documentation/RelNotes-1.6.0.txt: Expand on the incompatible packfiles
[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
211c8968
JS
32static int opt_parse_track(const struct option *opt, const char *arg, int not)
33{
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;
40}
41
42static int fetch_remote(const char *name)
43{
44 const char *argv[] = { "fetch", name, NULL };
3000658f 45 printf("Updating %s\n", name);
211c8968
JS
46 if (run_command_v_opt(argv, RUN_GIT_CMD))
47 return error("Could not fetch %s", name);
48 return 0;
49}
50
51static int add(int argc, const char **argv)
52{
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;
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
24b6177e
JF
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
211c8968
JS
91 strbuf_addf(&buf, "remote.%s.url", name);
92 if (git_config_set(buf.buf, url))
93 return 1;
94
24b6177e
JF
95 strbuf_reset(&buf);
96 strbuf_addf(&buf, "remote.%s.fetch", name);
97
211c8968
JS
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;
102
211c8968 103 strbuf_reset(&buf2);
1ce89cc4 104 strbuf_addch(&buf2, '+');
211c8968
JS
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;
113 }
114
84bb2dfd
PB
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;
120 }
121
211c8968
JS
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 path_list_clear(&track, 0);
139
140 return 0;
141}
142
143struct branch_info {
144 char *remote;
145 struct path_list merge;
146};
147
148static struct path_list branch_list;
149
ef90d6d4 150static int config_read_branches(const char *key, const char *value, void *cb)
211c8968
JS
151{
152 if (!prefixcmp(key, "branch.")) {
153 char *name;
154 struct path_list_item *item;
155 struct branch_info *info;
156 enum { REMOTE, MERGE } type;
157
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;
167
168 item = path_list_insert(name, &branch_list);
169
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, ' ');
fbca5837
MV
179 const char *ptr = skip_prefix(value, "refs/heads/");
180 if (ptr)
181 value = ptr;
211c8968
JS
182 while (space) {
183 char *merge;
184 merge = xstrndup(value, space - value);
185 path_list_append(merge, &info->merge);
fbca5837
MV
186 ptr = skip_prefix(space + 1, "refs/heads/");
187 if (ptr)
188 value = ptr;
189 else
190 value = space + 1;
211c8968
JS
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;
fbca5837
MV
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;
740fdd27
JS
228 /* symbolic refs pointing nowhere were handled already */
229 if ((flags & REF_ISSYMREF) ||
230 unsorted_path_list_has_path(&states->tracked,
231 name) ||
211c8968
JS
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);
237 }
238 return 0;
239}
240
241static int get_ref_states(const struct ref *ref, struct ref_states *states)
242{
243 struct ref *fetch_map = NULL, **tail = &fetch_map;
244 int i;
245
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]);
250
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;
fbca5837 256 const char *ptr;
211c8968
JS
257
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;
264 }
fbca5837
MV
265 ptr = skip_prefix(ref->name, "refs/heads/");
266 if (!ptr)
267 ptr = ref->name;
268 path_list_append(ptr, target)->util = util;
211c8968
JS
269 }
270 free_refs(fetch_map);
271
211c8968
JS
272 for_each_ref(handle_one_branch, states);
273 sort_path_list(&states->stale);
274
275 return 0;
276}
277
7ad2458f
SP
278struct known_remote {
279 struct known_remote *next;
280 struct remote *remote;
281};
282
283struct known_remotes {
284 struct remote *to_delete;
285 struct known_remote *list;
286};
287
288static int add_known_remote(struct remote *remote, void *cb_data)
289{
290 struct known_remotes *all = cb_data;
291 struct known_remote *r;
292
293 if (!strcmp(all->to_delete->name, remote->name))
294 return 0;
295
296 r = xmalloc(sizeof(*r));
297 r->remote = remote;
298 r->next = all->list;
299 all->list = r;
300 return 0;
301}
302
211c8968 303struct branches_for_remote {
7ad2458f 304 struct remote *remote;
211c8968 305 struct path_list *branches;
7ad2458f 306 struct known_remotes *keep;
211c8968
JS
307};
308
309static int add_branch_for_removal(const char *refname,
310 const unsigned char *sha1, int flags, void *cb_data)
311{
312 struct branches_for_remote *branches = cb_data;
7ad2458f
SP
313 struct refspec refspec;
314 struct path_list_item *item;
315 struct known_remote *kr;
211c8968 316
7ad2458f
SP
317 memset(&refspec, 0, sizeof(refspec));
318 refspec.dst = (char *)refname;
319 if (remote_find_tracking(branches->remote, &refspec))
320 return 0;
321
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;
328 }
3b9dcff5 329
7ad2458f
SP
330 /* make sure that symrefs are deleted */
331 if (flags & REF_ISSYMREF)
332 return unlink(git_path(refname));
3b9dcff5 333
7ad2458f
SP
334 item = path_list_append(refname, branches->branches);
335 item->util = xmalloc(20);
336 hashcpy(item->util, sha1);
211c8968
JS
337
338 return 0;
339}
340
341static int remove_branches(struct path_list *branches)
342{
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;
348
349 if (delete_ref(refname, sha1))
350 result |= error("Could not remove branch %s", refname);
351 }
352 return result;
353}
354
355static int rm(int argc, const char **argv)
356{
357 struct option options[] = {
358 OPT_END()
359 };
360 struct remote *remote;
361 struct strbuf buf;
7ad2458f 362 struct known_remotes known_remotes = { NULL, NULL };
211c8968 363 struct path_list branches = { NULL, 0, 0, 1 };
7ad2458f 364 struct branches_for_remote cb_data = { NULL, &branches, &known_remotes };
211c8968
JS
365 int i;
366
367 if (argc != 2)
368 usage_with_options(builtin_remote_usage, options);
369
370 remote = remote_get(argv[1]);
371 if (!remote)
372 die("No such remote: %s", argv[1]);
373
7ad2458f
SP
374 known_remotes.to_delete = remote;
375 for_each_remote(add_known_remote, &known_remotes);
376
211c8968
JS
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);
381
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;
395 }
396 }
397 }
398 }
399
400 /*
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.
404 */
7ad2458f 405 cb_data.remote = remote;
211c8968
JS
406 i = for_each_ref(add_branch_for_removal, &cb_data);
407 strbuf_release(&buf);
408
409 if (!i)
410 i = remove_branches(&branches);
411 path_list_clear(&branches, 1);
412
413 return i;
414}
415
416static void show_list(const char *title, struct path_list *list)
417{
418 int i;
419
420 if (!list->nr)
421 return;
422
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");
428}
429
67a7e2d0
OM
430static int get_remote_ref_states(const char *name,
431 struct ref_states *states,
432 int query)
433{
434 struct transport *transport;
435 const struct ref *ref;
436
437 states->remote = remote_get(name);
438 if (!states->remote)
439 return error("No such remote: %s", name);
440
441 read_branches();
442
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);
448
449 get_ref_states(ref, states);
450 }
451
452 return 0;
453}
454
e7d5a97d
OM
455static int append_ref_to_tracked_list(const char *refname,
456 const unsigned char *sha1, int flags, void *cb_data)
457{
458 struct ref_states *states = cb_data;
459 struct refspec refspec;
460
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);
466 }
467
468 return 0;
469}
470
67a7e2d0 471static int show(int argc, const char **argv)
211c8968 472{
0ecfcb3b 473 int no_query = 0, result = 0;
211c8968
JS
474 struct option options[] = {
475 OPT_GROUP("show specific options"),
0ecfcb3b 476 OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
211c8968
JS
477 OPT_END()
478 };
479 struct ref_states states;
480
481 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
482
67a7e2d0
OM
483 if (argc < 1)
484 return show_all();
211c8968
JS
485
486 memset(&states, 0, sizeof(states));
487 for (; argc; argc--, argv++) {
211c8968 488 struct strbuf buf;
0ecfcb3b 489 int i;
211c8968 490
67a7e2d0 491 get_remote_ref_states(*argv, &states, !no_query);
211c8968
JS
492
493 printf("* remote %s\n URL: %s\n", *argv,
494 states.remote->url_nr > 0 ?
495 states.remote->url[0] : "(no URL)");
496
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;
501
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");
511 }
512
0ecfcb3b
OM
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);
0ecfcb3b 521 }
211c8968 522
e7d5a97d
OM
523 if (no_query)
524 for_each_ref(append_ref_to_tracked_list, &states);
525 show_list(" Tracked remote branch%s", &states.tracked);
526
211c8968
JS
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;
fbca5837
MV
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/");
211c8968 538 printf(" %s%s%s%s", spec->force ? "+" : "",
fbca5837 539 p ? p : spec->src,
211c8968 540 spec->dst ? ":" : "",
fbca5837 541 q ? q : spec->dst);
211c8968 542 }
ec31b0ce 543 printf("\n");
211c8968 544 }
67a7e2d0
OM
545
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);
550 }
551
552 return result;
553}
554
555static int prune(int argc, const char **argv)
556{
8d767927 557 int dry_run = 0, result = 0;
67a7e2d0
OM
558 struct option options[] = {
559 OPT_GROUP("prune specific options"),
8d767927 560 OPT__DRY_RUN(&dry_run),
67a7e2d0
OM
561 OPT_END()
562 };
563 struct ref_states states;
564
565 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
566
567 if (argc < 1)
568 usage_with_options(builtin_remote_usage, options);
569
570 memset(&states, 0, sizeof(states));
571 for (; argc; argc--, argv++) {
572 int i;
573
8d767927
OM
574 get_remote_ref_states(*argv, &states, 1);
575
8b7d4e73
JH
576 if (states.stale.nr) {
577 printf("Pruning %s\n", *argv);
8d767927
OM
578 printf("URL: %s\n",
579 states.remote->url_nr
580 ? states.remote->url[0]
581 : "(no URL)");
8b7d4e73 582 }
67a7e2d0
OM
583
584 for (i = 0; i < states.stale.nr; i++) {
585 const char *refname = states.stale.items[i].util;
8d767927
OM
586
587 if (!dry_run)
588 result |= delete_ref(refname, NULL);
589
590 printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned",
591 skip_prefix(refname, "refs/remotes/"));
67a7e2d0
OM
592 }
593
211c8968
JS
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);
598 }
599
600 return result;
601}
602
84521ed6 603static int get_one_remote_for_update(struct remote *remote, void *priv)
211c8968 604{
84521ed6 605 struct path_list *list = priv;
211c8968 606 if (!remote->skip_default_update)
84521ed6
JS
607 path_list_append(xstrdup(remote->name), list);
608 return 0;
609}
610
611struct remote_group {
612 const char *name;
613 struct path_list *list;
614} remote_group;
615
ef90d6d4 616static int get_remote_group(const char *key, const char *value, void *cb)
84521ed6
JS
617{
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");
628 }
629 }
630
211c8968
JS
631 return 0;
632}
633
634static int update(int argc, const char **argv)
635{
84521ed6
JS
636 int i, result = 0;
637 struct path_list list = { NULL, 0, 0, 0 };
638 static const char *default_argv[] = { NULL, "default", NULL };
211c8968 639
84521ed6
JS
640 if (argc < 2) {
641 argc = 2;
642 argv = default_argv;
643 }
211c8968 644
84521ed6
JS
645 remote_group.list = &list;
646 for (i = 1; i < argc; i++) {
647 remote_group.name = argv[i];
ef90d6d4 648 result = git_config(get_remote_group, NULL);
84521ed6
JS
649 }
650
651 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
652 result = for_each_remote(get_one_remote_for_update, &list);
653
654 for (i = 0; i < list.nr; i++)
655 result |= fetch_remote(list.items[i].path);
656
657 /* all names were strdup()ed or strndup()ed */
658 list.strdup_paths = 1;
659 path_list_clear(&list, 0);
660
661 return result;
211c8968
JS
662}
663
664static int get_one_entry(struct remote *remote, void *priv)
665{
666 struct path_list *list = priv;
667
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);
672
673 return 0;
674}
675
676static int show_all(void)
677{
678 struct path_list list = { NULL, 0, 0 };
679 int result = for_each_remote(get_one_entry, &list);
680
681 if (!result) {
682 int i;
683
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 : "");
691 }
692 }
693 return result;
694}
695
696int cmd_remote(int argc, const char **argv, const char *prefix)
697{
698 struct option options[] = {
699 OPT__VERBOSE(&verbose),
700 OPT_END()
701 };
702 int result;
703
704 argc = parse_options(argc, argv, options, builtin_remote_usage,
705 PARSE_OPT_STOP_AT_NON_OPTION);
706
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"))
67a7e2d0 714 result = show(argc, argv);
211c8968 715 else if (!strcmp(argv[0], "prune"))
67a7e2d0 716 result = prune(argc, argv);
211c8968
JS
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);
722 }
723
724 return result ? 1 : 0;
725}