Rename the test trash directory to contain spaces.
[git/bdonlan.git] / builtin-remote.c
blob93bb84e1d4c762c6c049276202ebc2320447e9c8
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 inline const char *skip_prefix(const char *name, const char *prefix)
34 return !name ? "" :
35 prefixcmp(name, prefix) ? name : name + strlen(prefix);
38 static int opt_parse_track(const struct option *opt, const char *arg, int not)
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;
48 static int fetch_remote(const char *name)
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;
57 static int add(int argc, const char **argv)
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;
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()
77 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
79 if (argc < 2)
80 usage_with_options(builtin_remote_usage, options);
82 name = argv[0];
83 url = argv[1];
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);
90 strbuf_init(&buf, 0);
91 strbuf_init(&buf2, 0);
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);
97 strbuf_addf(&buf, "remote.%s.url", name);
98 if (git_config_set(buf.buf, url))
99 return 1;
101 strbuf_reset(&buf);
102 strbuf_addf(&buf, "remote.%s.fetch", name);
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;
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;
121 if (fetch && fetch_remote(name))
122 return 1;
124 if (master) {
125 strbuf_reset(&buf);
126 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
128 strbuf_reset(&buf2);
129 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
131 if (create_symref(buf.buf, buf2.buf, "remote add"))
132 return error("Could not setup master '%s'", master);
135 strbuf_release(&buf);
136 strbuf_release(&buf2);
137 path_list_clear(&track, 0);
139 return 0;
142 struct branch_info {
143 char *remote;
144 struct path_list merge;
147 static struct path_list branch_list;
149 static int config_read_branches(const char *key, const char *value)
151 if (!prefixcmp(key, "branch.")) {
152 char *name;
153 struct path_list_item *item;
154 struct branch_info *info;
155 enum { REMOTE, MERGE } type;
157 key += 7;
158 if (!postfixcmp(key, ".remote")) {
159 name = xstrndup(key, strlen(key) - 7);
160 type = REMOTE;
161 } else if (!postfixcmp(key, ".merge")) {
162 name = xstrndup(key, strlen(key) - 6);
163 type = MERGE;
164 } else
165 return 0;
167 item = path_list_insert(name, &branch_list);
169 if (!item->util)
170 item->util = xcalloc(sizeof(struct branch_info), 1);
171 info = item->util;
172 if (type == REMOTE) {
173 if (info->remote)
174 warning("more than one branch.%s", key);
175 info->remote = xstrdup(value);
176 } else {
177 char *space = strchr(value, ' ');
178 value = skip_prefix(value, "refs/heads/");
179 while (space) {
180 char *merge;
181 merge = xstrndup(value, space - value);
182 path_list_append(merge, &info->merge);
183 value = skip_prefix(space + 1, "refs/heads/");
184 space = strchr(value, ' ');
186 path_list_append(xstrdup(value), &info->merge);
189 return 0;
192 static void read_branches(void)
194 if (branch_list.nr)
195 return;
196 git_config(config_read_branches);
197 sort_path_list(&branch_list);
200 struct ref_states {
201 struct remote *remote;
202 struct strbuf remote_prefix;
203 struct path_list new, stale, tracked;
206 static int handle_one_branch(const char *refname,
207 const unsigned char *sha1, int flags, void *cb_data)
209 struct ref_states *states = cb_data;
210 struct refspec refspec;
212 memset(&refspec, 0, sizeof(refspec));
213 refspec.dst = (char *)refname;
214 if (!remote_find_tracking(states->remote, &refspec)) {
215 struct path_list_item *item;
216 const char *name = skip_prefix(refspec.src, "refs/heads/");
217 /* symbolic refs pointing nowhere were handled already */
218 if ((flags & REF_ISSYMREF) ||
219 unsorted_path_list_has_path(&states->tracked,
220 name) ||
221 unsorted_path_list_has_path(&states->new,
222 name))
223 return 0;
224 item = path_list_append(name, &states->stale);
225 item->util = xstrdup(refname);
227 return 0;
230 static int get_ref_states(const struct ref *ref, struct ref_states *states)
232 struct ref *fetch_map = NULL, **tail = &fetch_map;
233 int i;
235 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
236 if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
237 die("Could not get fetch map for refspec %s",
238 states->remote->fetch_refspec[i]);
240 states->new.strdup_paths = states->tracked.strdup_paths = 1;
241 for (ref = fetch_map; ref; ref = ref->next) {
242 struct path_list *target = &states->tracked;
243 unsigned char sha1[20];
244 void *util = NULL;
246 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
247 target = &states->new;
248 else {
249 target = &states->tracked;
250 if (hashcmp(sha1, ref->new_sha1))
251 util = &states;
253 path_list_append(skip_prefix(ref->name, "refs/heads/"),
254 target)->util = util;
256 free_refs(fetch_map);
258 strbuf_addf(&states->remote_prefix,
259 "refs/remotes/%s/", states->remote->name);
260 for_each_ref(handle_one_branch, states);
261 sort_path_list(&states->stale);
263 return 0;
266 struct branches_for_remote {
267 const char *prefix;
268 struct path_list *branches;
271 static int add_branch_for_removal(const char *refname,
272 const unsigned char *sha1, int flags, void *cb_data)
274 struct branches_for_remote *branches = cb_data;
276 if (!prefixcmp(refname, branches->prefix)) {
277 struct path_list_item *item;
279 /* make sure that symrefs are deleted */
280 if (flags & REF_ISSYMREF)
281 return unlink(git_path(refname));
283 item = path_list_append(refname, branches->branches);
284 item->util = xmalloc(20);
285 hashcpy(item->util, sha1);
288 return 0;
291 static int remove_branches(struct path_list *branches)
293 int i, result = 0;
294 for (i = 0; i < branches->nr; i++) {
295 struct path_list_item *item = branches->items + i;
296 const char *refname = item->path;
297 unsigned char *sha1 = item->util;
299 if (delete_ref(refname, sha1))
300 result |= error("Could not remove branch %s", refname);
302 return result;
305 static int rm(int argc, const char **argv)
307 struct option options[] = {
308 OPT_END()
310 struct remote *remote;
311 struct strbuf buf;
312 struct path_list branches = { NULL, 0, 0, 1 };
313 struct branches_for_remote cb_data = { NULL, &branches };
314 int i;
316 if (argc != 2)
317 usage_with_options(builtin_remote_usage, options);
319 remote = remote_get(argv[1]);
320 if (!remote)
321 die("No such remote: %s", argv[1]);
323 strbuf_init(&buf, 0);
324 strbuf_addf(&buf, "remote.%s", remote->name);
325 if (git_config_rename_section(buf.buf, NULL) < 1)
326 return error("Could not remove config section '%s'", buf.buf);
328 read_branches();
329 for (i = 0; i < branch_list.nr; i++) {
330 struct path_list_item *item = branch_list.items + i;
331 struct branch_info *info = item->util;
332 if (info->remote && !strcmp(info->remote, remote->name)) {
333 const char *keys[] = { "remote", "merge", NULL }, **k;
334 for (k = keys; *k; k++) {
335 strbuf_reset(&buf);
336 strbuf_addf(&buf, "branch.%s.%s",
337 item->path, *k);
338 if (git_config_set(buf.buf, NULL)) {
339 strbuf_release(&buf);
340 return -1;
347 * We cannot just pass a function to for_each_ref() which deletes
348 * the branches one by one, since for_each_ref() relies on cached
349 * refs, which are invalidated when deleting a branch.
351 strbuf_reset(&buf);
352 strbuf_addf(&buf, "refs/remotes/%s/", remote->name);
353 cb_data.prefix = buf.buf;
354 i = for_each_ref(add_branch_for_removal, &cb_data);
355 strbuf_release(&buf);
357 if (!i)
358 i = remove_branches(&branches);
359 path_list_clear(&branches, 1);
361 return i;
364 static void show_list(const char *title, struct path_list *list)
366 int i;
368 if (!list->nr)
369 return;
371 printf(title, list->nr > 1 ? "es" : "");
372 printf("\n ");
373 for (i = 0; i < list->nr; i++)
374 printf("%s%s", i ? " " : "", list->items[i].path);
375 printf("\n");
378 static int show_or_prune(int argc, const char **argv, int prune)
380 int dry_run = 0, result = 0;
381 struct option options[] = {
382 OPT_GROUP("show specific options"),
383 OPT__DRY_RUN(&dry_run),
384 OPT_END()
386 struct ref_states states;
388 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
390 if (argc < 1) {
391 if (!prune)
392 return show_all();
393 usage_with_options(builtin_remote_usage, options);
396 memset(&states, 0, sizeof(states));
397 for (; argc; argc--, argv++) {
398 struct transport *transport;
399 const struct ref *ref;
400 struct strbuf buf;
401 int i, got_states;
403 states.remote = remote_get(*argv);
404 if (!states.remote)
405 return error("No such remote: %s", *argv);
406 transport = transport_get(NULL, states.remote->url_nr > 0 ?
407 states.remote->url[0] : NULL);
408 ref = transport_get_remote_refs(transport);
409 transport_disconnect(transport);
411 read_branches();
412 got_states = get_ref_states(ref, &states);
413 if (got_states)
414 result = error("Error getting local info for '%s'",
415 states.remote->name);
417 if (prune) {
418 struct strbuf buf;
419 int prefix_len;
421 strbuf_init(&buf, 0);
422 if (states.remote->fetch_refspec_nr == 1 &&
423 states.remote->fetch->pattern &&
424 !strcmp(states.remote->fetch->src,
425 states.remote->fetch->dst))
426 /* handle --mirror remote */
427 strbuf_addstr(&buf, "refs/heads/");
428 else
429 strbuf_addf(&buf, "refs/remotes/%s/", *argv);
430 prefix_len = buf.len;
432 for (i = 0; i < states.stale.nr; i++) {
433 strbuf_setlen(&buf, prefix_len);
434 strbuf_addstr(&buf, states.stale.items[i].path);
435 result |= delete_ref(buf.buf, NULL);
438 strbuf_release(&buf);
439 goto cleanup_states;
442 printf("* remote %s\n URL: %s\n", *argv,
443 states.remote->url_nr > 0 ?
444 states.remote->url[0] : "(no URL)");
446 for (i = 0; i < branch_list.nr; i++) {
447 struct path_list_item *branch = branch_list.items + i;
448 struct branch_info *info = branch->util;
449 int j;
451 if (!info->merge.nr || strcmp(*argv, info->remote))
452 continue;
453 printf(" Remote branch%s merged with 'git pull' "
454 "while on branch %s\n ",
455 info->merge.nr > 1 ? "es" : "",
456 branch->path);
457 for (j = 0; j < info->merge.nr; j++)
458 printf(" %s", info->merge.items[j].path);
459 printf("\n");
462 if (got_states)
463 continue;
464 strbuf_init(&buf, 0);
465 strbuf_addf(&buf, " New remote branch%%s (next fetch will "
466 "store in remotes/%s)", states.remote->name);
467 show_list(buf.buf, &states.new);
468 strbuf_release(&buf);
469 show_list(" Stale tracking branch%s (use 'git remote prune')",
470 &states.stale);
471 show_list(" Tracked remote branch%s",
472 &states.tracked);
474 if (states.remote->push_refspec_nr) {
475 printf(" Local branch%s pushed with 'git push'\n ",
476 states.remote->push_refspec_nr > 1 ?
477 "es" : "");
478 for (i = 0; i < states.remote->push_refspec_nr; i++) {
479 struct refspec *spec = states.remote->push + i;
480 printf(" %s%s%s%s", spec->force ? "+" : "",
481 skip_prefix(spec->src, "refs/heads/"),
482 spec->dst ? ":" : "",
483 skip_prefix(spec->dst, "refs/heads/"));
485 printf("\n");
487 cleanup_states:
488 /* NEEDSWORK: free remote */
489 path_list_clear(&states.new, 0);
490 path_list_clear(&states.stale, 0);
491 path_list_clear(&states.tracked, 0);
494 return result;
497 static int get_one_remote_for_update(struct remote *remote, void *priv)
499 struct path_list *list = priv;
500 if (!remote->skip_default_update)
501 path_list_append(xstrdup(remote->name), list);
502 return 0;
505 struct remote_group {
506 const char *name;
507 struct path_list *list;
508 } remote_group;
510 static int get_remote_group(const char *key, const char *value)
512 if (!prefixcmp(key, "remotes.") &&
513 !strcmp(key + 8, remote_group.name)) {
514 /* split list by white space */
515 int space = strcspn(value, " \t\n");
516 while (*value) {
517 if (space > 1)
518 path_list_append(xstrndup(value, space),
519 remote_group.list);
520 value += space + (value[space] != '\0');
521 space = strcspn(value, " \t\n");
525 return 0;
528 static int update(int argc, const char **argv)
530 int i, result = 0;
531 struct path_list list = { NULL, 0, 0, 0 };
532 static const char *default_argv[] = { NULL, "default", NULL };
534 if (argc < 2) {
535 argc = 2;
536 argv = default_argv;
539 remote_group.list = &list;
540 for (i = 1; i < argc; i++) {
541 remote_group.name = argv[i];
542 result = git_config(get_remote_group);
545 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
546 result = for_each_remote(get_one_remote_for_update, &list);
548 for (i = 0; i < list.nr; i++)
549 result |= fetch_remote(list.items[i].path);
551 /* all names were strdup()ed or strndup()ed */
552 list.strdup_paths = 1;
553 path_list_clear(&list, 0);
555 return result;
558 static int get_one_entry(struct remote *remote, void *priv)
560 struct path_list *list = priv;
562 path_list_append(remote->name, list)->util = remote->url_nr ?
563 (void *)remote->url[0] : NULL;
564 if (remote->url_nr > 1)
565 warning("Remote %s has more than one URL", remote->name);
567 return 0;
570 static int show_all(void)
572 struct path_list list = { NULL, 0, 0 };
573 int result = for_each_remote(get_one_entry, &list);
575 if (!result) {
576 int i;
578 sort_path_list(&list);
579 for (i = 0; i < list.nr; i++) {
580 struct path_list_item *item = list.items + i;
581 printf("%s%s%s\n", item->path,
582 verbose ? "\t" : "",
583 verbose && item->util ?
584 (const char *)item->util : "");
587 return result;
590 int cmd_remote(int argc, const char **argv, const char *prefix)
592 struct option options[] = {
593 OPT__VERBOSE(&verbose),
594 OPT_END()
596 int result;
598 argc = parse_options(argc, argv, options, builtin_remote_usage,
599 PARSE_OPT_STOP_AT_NON_OPTION);
601 if (argc < 1)
602 result = show_all();
603 else if (!strcmp(argv[0], "add"))
604 result = add(argc, argv);
605 else if (!strcmp(argv[0], "rm"))
606 result = rm(argc, argv);
607 else if (!strcmp(argv[0], "show"))
608 result = show_or_prune(argc, argv, 0);
609 else if (!strcmp(argv[0], "prune"))
610 result = show_or_prune(argc, argv, 1);
611 else if (!strcmp(argv[0], "update"))
612 result = update(argc, argv);
613 else {
614 error("Unknown subcommand: %s", argv[0]);
615 usage_with_options(builtin_remote_usage, options);
618 return result ? 1 : 0;