Provide git_config with a callback-data parameter
[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 "path-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 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
38 static 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
48 static int fetch_remote(const char *name)
49 {
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);
54 return 0;
55 }
56
57 static 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
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
97 strbuf_addf(&buf, "remote.%s.url", name);
98 if (git_config_set(buf.buf, url))
99 return 1;
100
101 strbuf_reset(&buf);
102 strbuf_addf(&buf, "remote.%s.fetch", name);
103
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
109 strbuf_reset(&buf2);
110 strbuf_addch(&buf2, '+');
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
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
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
149 struct branch_info {
150 char *remote;
151 struct path_list merge;
152 };
153
154 static struct path_list branch_list;
155
156 static int config_read_branches(const char *key, const char *value, void *cb)
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
199 static void read_branches(void)
200 {
201 if (branch_list.nr)
202 return;
203 git_config(config_read_branches, NULL);
204 sort_path_list(&branch_list);
205 }
206
207 struct ref_states {
208 struct remote *remote;
209 struct strbuf remote_prefix;
210 struct path_list new, stale, tracked;
211 };
212
213 static int handle_one_branch(const char *refname,
214 const unsigned char *sha1, int flags, void *cb_data)
215 {
216 struct ref_states *states = cb_data;
217 struct refspec refspec;
218
219 memset(&refspec, 0, sizeof(refspec));
220 refspec.dst = (char *)refname;
221 if (!remote_find_tracking(states->remote, &refspec)) {
222 struct path_list_item *item;
223 const char *name = skip_prefix(refspec.src, "refs/heads/");
224 /* symbolic refs pointing nowhere were handled already */
225 if ((flags & REF_ISSYMREF) ||
226 unsorted_path_list_has_path(&states->tracked,
227 name) ||
228 unsorted_path_list_has_path(&states->new,
229 name))
230 return 0;
231 item = path_list_append(name, &states->stale);
232 item->util = xstrdup(refname);
233 }
234 return 0;
235 }
236
237 static int get_ref_states(const struct ref *ref, struct ref_states *states)
238 {
239 struct ref *fetch_map = NULL, **tail = &fetch_map;
240 int i;
241
242 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
243 if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
244 die("Could not get fetch map for refspec %s",
245 states->remote->fetch_refspec[i]);
246
247 states->new.strdup_paths = states->tracked.strdup_paths = 1;
248 for (ref = fetch_map; ref; ref = ref->next) {
249 struct path_list *target = &states->tracked;
250 unsigned char sha1[20];
251 void *util = NULL;
252
253 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
254 target = &states->new;
255 else {
256 target = &states->tracked;
257 if (hashcmp(sha1, ref->new_sha1))
258 util = &states;
259 }
260 path_list_append(skip_prefix(ref->name, "refs/heads/"),
261 target)->util = util;
262 }
263 free_refs(fetch_map);
264
265 strbuf_addf(&states->remote_prefix,
266 "refs/remotes/%s/", states->remote->name);
267 for_each_ref(handle_one_branch, states);
268 sort_path_list(&states->stale);
269
270 return 0;
271 }
272
273 struct branches_for_remote {
274 const char *prefix;
275 struct path_list *branches;
276 };
277
278 static int add_branch_for_removal(const char *refname,
279 const unsigned char *sha1, int flags, void *cb_data)
280 {
281 struct branches_for_remote *branches = cb_data;
282
283 if (!prefixcmp(refname, branches->prefix)) {
284 struct path_list_item *item;
285
286 /* make sure that symrefs are deleted */
287 if (flags & REF_ISSYMREF)
288 return unlink(git_path(refname));
289
290 item = path_list_append(refname, branches->branches);
291 item->util = xmalloc(20);
292 hashcpy(item->util, sha1);
293 }
294
295 return 0;
296 }
297
298 static int remove_branches(struct path_list *branches)
299 {
300 int i, result = 0;
301 for (i = 0; i < branches->nr; i++) {
302 struct path_list_item *item = branches->items + i;
303 const char *refname = item->path;
304 unsigned char *sha1 = item->util;
305
306 if (delete_ref(refname, sha1))
307 result |= error("Could not remove branch %s", refname);
308 }
309 return result;
310 }
311
312 static int rm(int argc, const char **argv)
313 {
314 struct option options[] = {
315 OPT_END()
316 };
317 struct remote *remote;
318 struct strbuf buf;
319 struct path_list branches = { NULL, 0, 0, 1 };
320 struct branches_for_remote cb_data = { NULL, &branches };
321 int i;
322
323 if (argc != 2)
324 usage_with_options(builtin_remote_usage, options);
325
326 remote = remote_get(argv[1]);
327 if (!remote)
328 die("No such remote: %s", argv[1]);
329
330 strbuf_init(&buf, 0);
331 strbuf_addf(&buf, "remote.%s", remote->name);
332 if (git_config_rename_section(buf.buf, NULL) < 1)
333 return error("Could not remove config section '%s'", buf.buf);
334
335 read_branches();
336 for (i = 0; i < branch_list.nr; i++) {
337 struct path_list_item *item = branch_list.items + i;
338 struct branch_info *info = item->util;
339 if (info->remote && !strcmp(info->remote, remote->name)) {
340 const char *keys[] = { "remote", "merge", NULL }, **k;
341 for (k = keys; *k; k++) {
342 strbuf_reset(&buf);
343 strbuf_addf(&buf, "branch.%s.%s",
344 item->path, *k);
345 if (git_config_set(buf.buf, NULL)) {
346 strbuf_release(&buf);
347 return -1;
348 }
349 }
350 }
351 }
352
353 /*
354 * We cannot just pass a function to for_each_ref() which deletes
355 * the branches one by one, since for_each_ref() relies on cached
356 * refs, which are invalidated when deleting a branch.
357 */
358 strbuf_reset(&buf);
359 strbuf_addf(&buf, "refs/remotes/%s/", remote->name);
360 cb_data.prefix = buf.buf;
361 i = for_each_ref(add_branch_for_removal, &cb_data);
362 strbuf_release(&buf);
363
364 if (!i)
365 i = remove_branches(&branches);
366 path_list_clear(&branches, 1);
367
368 return i;
369 }
370
371 static void show_list(const char *title, struct path_list *list)
372 {
373 int i;
374
375 if (!list->nr)
376 return;
377
378 printf(title, list->nr > 1 ? "es" : "");
379 printf("\n ");
380 for (i = 0; i < list->nr; i++)
381 printf("%s%s", i ? " " : "", list->items[i].path);
382 printf("\n");
383 }
384
385 static int show_or_prune(int argc, const char **argv, int prune)
386 {
387 int dry_run = 0, result = 0;
388 struct option options[] = {
389 OPT_GROUP("show specific options"),
390 OPT__DRY_RUN(&dry_run),
391 OPT_END()
392 };
393 struct ref_states states;
394
395 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
396
397 if (argc < 1) {
398 if (!prune)
399 return show_all();
400 usage_with_options(builtin_remote_usage, options);
401 }
402
403 memset(&states, 0, sizeof(states));
404 for (; argc; argc--, argv++) {
405 struct transport *transport;
406 const struct ref *ref;
407 struct strbuf buf;
408 int i, got_states;
409
410 states.remote = remote_get(*argv);
411 if (!states.remote)
412 return error("No such remote: %s", *argv);
413 transport = transport_get(NULL, states.remote->url_nr > 0 ?
414 states.remote->url[0] : NULL);
415 ref = transport_get_remote_refs(transport);
416 transport_disconnect(transport);
417
418 read_branches();
419 got_states = get_ref_states(ref, &states);
420 if (got_states)
421 result = error("Error getting local info for '%s'",
422 states.remote->name);
423
424 if (prune) {
425 struct strbuf buf;
426 int prefix_len;
427
428 strbuf_init(&buf, 0);
429 if (states.remote->fetch_refspec_nr == 1 &&
430 states.remote->fetch->pattern &&
431 !strcmp(states.remote->fetch->src,
432 states.remote->fetch->dst))
433 /* handle --mirror remote */
434 strbuf_addstr(&buf, "refs/heads/");
435 else
436 strbuf_addf(&buf, "refs/remotes/%s/", *argv);
437 prefix_len = buf.len;
438
439 for (i = 0; i < states.stale.nr; i++) {
440 strbuf_setlen(&buf, prefix_len);
441 strbuf_addstr(&buf, states.stale.items[i].path);
442 result |= delete_ref(buf.buf, NULL);
443 }
444
445 strbuf_release(&buf);
446 goto cleanup_states;
447 }
448
449 printf("* remote %s\n URL: %s\n", *argv,
450 states.remote->url_nr > 0 ?
451 states.remote->url[0] : "(no URL)");
452
453 for (i = 0; i < branch_list.nr; i++) {
454 struct path_list_item *branch = branch_list.items + i;
455 struct branch_info *info = branch->util;
456 int j;
457
458 if (!info->merge.nr || strcmp(*argv, info->remote))
459 continue;
460 printf(" Remote branch%s merged with 'git pull' "
461 "while on branch %s\n ",
462 info->merge.nr > 1 ? "es" : "",
463 branch->path);
464 for (j = 0; j < info->merge.nr; j++)
465 printf(" %s", info->merge.items[j].path);
466 printf("\n");
467 }
468
469 if (got_states)
470 continue;
471 strbuf_init(&buf, 0);
472 strbuf_addf(&buf, " New remote branch%%s (next fetch will "
473 "store in remotes/%s)", states.remote->name);
474 show_list(buf.buf, &states.new);
475 strbuf_release(&buf);
476 show_list(" Stale tracking branch%s (use 'git remote prune')",
477 &states.stale);
478 show_list(" Tracked remote branch%s",
479 &states.tracked);
480
481 if (states.remote->push_refspec_nr) {
482 printf(" Local branch%s pushed with 'git push'\n ",
483 states.remote->push_refspec_nr > 1 ?
484 "es" : "");
485 for (i = 0; i < states.remote->push_refspec_nr; i++) {
486 struct refspec *spec = states.remote->push + i;
487 printf(" %s%s%s%s", spec->force ? "+" : "",
488 skip_prefix(spec->src, "refs/heads/"),
489 spec->dst ? ":" : "",
490 skip_prefix(spec->dst, "refs/heads/"));
491 }
492 printf("\n");
493 }
494 cleanup_states:
495 /* NEEDSWORK: free remote */
496 path_list_clear(&states.new, 0);
497 path_list_clear(&states.stale, 0);
498 path_list_clear(&states.tracked, 0);
499 }
500
501 return result;
502 }
503
504 static int get_one_remote_for_update(struct remote *remote, void *priv)
505 {
506 struct path_list *list = priv;
507 if (!remote->skip_default_update)
508 path_list_append(xstrdup(remote->name), list);
509 return 0;
510 }
511
512 struct remote_group {
513 const char *name;
514 struct path_list *list;
515 } remote_group;
516
517 static int get_remote_group(const char *key, const char *value, void *cb)
518 {
519 if (!prefixcmp(key, "remotes.") &&
520 !strcmp(key + 8, remote_group.name)) {
521 /* split list by white space */
522 int space = strcspn(value, " \t\n");
523 while (*value) {
524 if (space > 1)
525 path_list_append(xstrndup(value, space),
526 remote_group.list);
527 value += space + (value[space] != '\0');
528 space = strcspn(value, " \t\n");
529 }
530 }
531
532 return 0;
533 }
534
535 static int update(int argc, const char **argv)
536 {
537 int i, result = 0;
538 struct path_list list = { NULL, 0, 0, 0 };
539 static const char *default_argv[] = { NULL, "default", NULL };
540
541 if (argc < 2) {
542 argc = 2;
543 argv = default_argv;
544 }
545
546 remote_group.list = &list;
547 for (i = 1; i < argc; i++) {
548 remote_group.name = argv[i];
549 result = git_config(get_remote_group, NULL);
550 }
551
552 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
553 result = for_each_remote(get_one_remote_for_update, &list);
554
555 for (i = 0; i < list.nr; i++)
556 result |= fetch_remote(list.items[i].path);
557
558 /* all names were strdup()ed or strndup()ed */
559 list.strdup_paths = 1;
560 path_list_clear(&list, 0);
561
562 return result;
563 }
564
565 static int get_one_entry(struct remote *remote, void *priv)
566 {
567 struct path_list *list = priv;
568
569 path_list_append(remote->name, list)->util = remote->url_nr ?
570 (void *)remote->url[0] : NULL;
571 if (remote->url_nr > 1)
572 warning("Remote %s has more than one URL", remote->name);
573
574 return 0;
575 }
576
577 static int show_all(void)
578 {
579 struct path_list list = { NULL, 0, 0 };
580 int result = for_each_remote(get_one_entry, &list);
581
582 if (!result) {
583 int i;
584
585 sort_path_list(&list);
586 for (i = 0; i < list.nr; i++) {
587 struct path_list_item *item = list.items + i;
588 printf("%s%s%s\n", item->path,
589 verbose ? "\t" : "",
590 verbose && item->util ?
591 (const char *)item->util : "");
592 }
593 }
594 return result;
595 }
596
597 int cmd_remote(int argc, const char **argv, const char *prefix)
598 {
599 struct option options[] = {
600 OPT__VERBOSE(&verbose),
601 OPT_END()
602 };
603 int result;
604
605 argc = parse_options(argc, argv, options, builtin_remote_usage,
606 PARSE_OPT_STOP_AT_NON_OPTION);
607
608 if (argc < 1)
609 result = show_all();
610 else if (!strcmp(argv[0], "add"))
611 result = add(argc, argv);
612 else if (!strcmp(argv[0], "rm"))
613 result = rm(argc, argv);
614 else if (!strcmp(argv[0], "show"))
615 result = show_or_prune(argc, argv, 0);
616 else if (!strcmp(argv[0], "prune"))
617 result = show_or_prune(argc, argv, 1);
618 else if (!strcmp(argv[0], "update"))
619 result = update(argc, argv);
620 else {
621 error("Unknown subcommand: %s", argv[0]);
622 usage_with_options(builtin_remote_usage, options);
623 }
624
625 return result ? 1 : 0;
626 }