Merge branch 'maint-1.5.4' into maint
[git.git] / builtin-remote.c
blob4149f3b3ce4bed091200fdff539e459aa39fb06d
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 inline int postfixcmp(const char *string, const char *postfix)
24 int len1 = strlen(string), len2 = strlen(postfix);
25 if (len1 < len2)
26 return 1;
27 return strcmp(string + len1 - len2, postfix);
30 static inline const char *skip_prefix(const char *name, const char *prefix)
32 return !name ? "" :
33 prefixcmp(name, prefix) ? name : name + strlen(prefix);
36 static int opt_parse_track(const struct option *opt, const char *arg, int not)
38 struct path_list *list = opt->value;
39 if (not)
40 path_list_clear(list, 0);
41 else
42 path_list_append(arg, list);
43 return 0;
46 static int fetch_remote(const char *name)
48 const char *argv[] = { "fetch", name, NULL };
49 printf("Updating %s\n", name);
50 if (run_command_v_opt(argv, RUN_GIT_CMD))
51 return error("Could not fetch %s", name);
52 return 0;
55 static int add(int argc, const char **argv)
57 int fetch = 0, mirror = 0;
58 struct path_list track = { NULL, 0, 0 };
59 const char *master = NULL;
60 struct remote *remote;
61 struct strbuf buf, buf2;
62 const char *name, *url;
63 int i;
65 struct option options[] = {
66 OPT_GROUP("add specific options"),
67 OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"),
68 OPT_CALLBACK('t', "track", &track, "branch",
69 "branch(es) to track", opt_parse_track),
70 OPT_STRING('m', "master", &master, "branch", "master branch"),
71 OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"),
72 OPT_END()
75 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
77 if (argc < 2)
78 usage_with_options(builtin_remote_usage, options);
80 name = argv[0];
81 url = argv[1];
83 remote = remote_get(name);
84 if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) ||
85 remote->fetch_refspec_nr))
86 die("remote %s already exists.", name);
88 strbuf_init(&buf, 0);
89 strbuf_init(&buf2, 0);
91 strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name);
92 if (!valid_fetch_refspec(buf2.buf))
93 die("'%s' is not a valid remote name", name);
95 strbuf_addf(&buf, "remote.%s.url", name);
96 if (git_config_set(buf.buf, url))
97 return 1;
99 strbuf_reset(&buf);
100 strbuf_addf(&buf, "remote.%s.fetch", name);
102 if (track.nr == 0)
103 path_list_append("*", &track);
104 for (i = 0; i < track.nr; i++) {
105 struct path_list_item *item = track.items + i;
107 strbuf_reset(&buf2);
108 strbuf_addch(&buf2, '+');
109 if (mirror)
110 strbuf_addf(&buf2, "refs/%s:refs/%s",
111 item->path, item->path);
112 else
113 strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
114 item->path, name, item->path);
115 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
116 return 1;
119 if (fetch && fetch_remote(name))
120 return 1;
122 if (master) {
123 strbuf_reset(&buf);
124 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
126 strbuf_reset(&buf2);
127 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
129 if (create_symref(buf.buf, buf2.buf, "remote add"))
130 return error("Could not setup master '%s'", master);
133 strbuf_release(&buf);
134 strbuf_release(&buf2);
135 path_list_clear(&track, 0);
137 return 0;
140 struct branch_info {
141 char *remote;
142 struct path_list merge;
145 static struct path_list branch_list;
147 static int config_read_branches(const char *key, const char *value)
149 if (!prefixcmp(key, "branch.")) {
150 char *name;
151 struct path_list_item *item;
152 struct branch_info *info;
153 enum { REMOTE, MERGE } type;
155 key += 7;
156 if (!postfixcmp(key, ".remote")) {
157 name = xstrndup(key, strlen(key) - 7);
158 type = REMOTE;
159 } else if (!postfixcmp(key, ".merge")) {
160 name = xstrndup(key, strlen(key) - 6);
161 type = MERGE;
162 } else
163 return 0;
165 item = path_list_insert(name, &branch_list);
167 if (!item->util)
168 item->util = xcalloc(sizeof(struct branch_info), 1);
169 info = item->util;
170 if (type == REMOTE) {
171 if (info->remote)
172 warning("more than one branch.%s", key);
173 info->remote = xstrdup(value);
174 } else {
175 char *space = strchr(value, ' ');
176 value = skip_prefix(value, "refs/heads/");
177 while (space) {
178 char *merge;
179 merge = xstrndup(value, space - value);
180 path_list_append(merge, &info->merge);
181 value = skip_prefix(space + 1, "refs/heads/");
182 space = strchr(value, ' ');
184 path_list_append(xstrdup(value), &info->merge);
187 return 0;
190 static void read_branches(void)
192 if (branch_list.nr)
193 return;
194 git_config(config_read_branches);
195 sort_path_list(&branch_list);
198 struct ref_states {
199 struct remote *remote;
200 struct strbuf remote_prefix;
201 struct path_list new, stale, tracked;
204 static int handle_one_branch(const char *refname,
205 const unsigned char *sha1, int flags, void *cb_data)
207 struct ref_states *states = cb_data;
208 struct refspec refspec;
210 memset(&refspec, 0, sizeof(refspec));
211 refspec.dst = (char *)refname;
212 if (!remote_find_tracking(states->remote, &refspec)) {
213 struct path_list_item *item;
214 const char *name = skip_prefix(refspec.src, "refs/heads/");
215 /* symbolic refs pointing nowhere were handled already */
216 if ((flags & REF_ISSYMREF) ||
217 unsorted_path_list_has_path(&states->tracked,
218 name) ||
219 unsorted_path_list_has_path(&states->new,
220 name))
221 return 0;
222 item = path_list_append(name, &states->stale);
223 item->util = xstrdup(refname);
225 return 0;
228 static int get_ref_states(const struct ref *ref, struct ref_states *states)
230 struct ref *fetch_map = NULL, **tail = &fetch_map;
231 int i;
233 for (i = 0; i < states->remote->fetch_refspec_nr; i++)
234 if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
235 die("Could not get fetch map for refspec %s",
236 states->remote->fetch_refspec[i]);
238 states->new.strdup_paths = states->tracked.strdup_paths = 1;
239 for (ref = fetch_map; ref; ref = ref->next) {
240 struct path_list *target = &states->tracked;
241 unsigned char sha1[20];
242 void *util = NULL;
244 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
245 target = &states->new;
246 else {
247 target = &states->tracked;
248 if (hashcmp(sha1, ref->new_sha1))
249 util = &states;
251 path_list_append(skip_prefix(ref->name, "refs/heads/"),
252 target)->util = util;
254 free_refs(fetch_map);
256 strbuf_addf(&states->remote_prefix,
257 "refs/remotes/%s/", states->remote->name);
258 for_each_ref(handle_one_branch, states);
259 sort_path_list(&states->stale);
261 return 0;
264 struct branches_for_remote {
265 const char *prefix;
266 struct path_list *branches;
269 static int add_branch_for_removal(const char *refname,
270 const unsigned char *sha1, int flags, void *cb_data)
272 struct branches_for_remote *branches = cb_data;
274 if (!prefixcmp(refname, branches->prefix)) {
275 struct path_list_item *item;
277 /* make sure that symrefs are deleted */
278 if (flags & REF_ISSYMREF)
279 return unlink(git_path(refname));
281 item = path_list_append(refname, branches->branches);
282 item->util = xmalloc(20);
283 hashcpy(item->util, sha1);
286 return 0;
289 static int remove_branches(struct path_list *branches)
291 int i, result = 0;
292 for (i = 0; i < branches->nr; i++) {
293 struct path_list_item *item = branches->items + i;
294 const char *refname = item->path;
295 unsigned char *sha1 = item->util;
297 if (delete_ref(refname, sha1))
298 result |= error("Could not remove branch %s", refname);
300 return result;
303 static int rm(int argc, const char **argv)
305 struct option options[] = {
306 OPT_END()
308 struct remote *remote;
309 struct strbuf buf;
310 struct path_list branches = { NULL, 0, 0, 1 };
311 struct branches_for_remote cb_data = { NULL, &branches };
312 int i;
314 if (argc != 2)
315 usage_with_options(builtin_remote_usage, options);
317 remote = remote_get(argv[1]);
318 if (!remote)
319 die("No such remote: %s", argv[1]);
321 strbuf_init(&buf, 0);
322 strbuf_addf(&buf, "remote.%s", remote->name);
323 if (git_config_rename_section(buf.buf, NULL) < 1)
324 return error("Could not remove config section '%s'", buf.buf);
326 read_branches();
327 for (i = 0; i < branch_list.nr; i++) {
328 struct path_list_item *item = branch_list.items + i;
329 struct branch_info *info = item->util;
330 if (info->remote && !strcmp(info->remote, remote->name)) {
331 const char *keys[] = { "remote", "merge", NULL }, **k;
332 for (k = keys; *k; k++) {
333 strbuf_reset(&buf);
334 strbuf_addf(&buf, "branch.%s.%s",
335 item->path, *k);
336 if (git_config_set(buf.buf, NULL)) {
337 strbuf_release(&buf);
338 return -1;
345 * We cannot just pass a function to for_each_ref() which deletes
346 * the branches one by one, since for_each_ref() relies on cached
347 * refs, which are invalidated when deleting a branch.
349 strbuf_reset(&buf);
350 strbuf_addf(&buf, "refs/remotes/%s/", remote->name);
351 cb_data.prefix = buf.buf;
352 i = for_each_ref(add_branch_for_removal, &cb_data);
353 strbuf_release(&buf);
355 if (!i)
356 i = remove_branches(&branches);
357 path_list_clear(&branches, 1);
359 return i;
362 static void show_list(const char *title, struct path_list *list)
364 int i;
366 if (!list->nr)
367 return;
369 printf(title, list->nr > 1 ? "es" : "");
370 printf("\n ");
371 for (i = 0; i < list->nr; i++)
372 printf("%s%s", i ? " " : "", list->items[i].path);
373 printf("\n");
376 static int show_or_prune(int argc, const char **argv, int prune)
378 int dry_run = 0, result = 0;
379 struct option options[] = {
380 OPT_GROUP("show specific options"),
381 OPT__DRY_RUN(&dry_run),
382 OPT_END()
384 struct ref_states states;
386 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
388 if (argc < 1)
389 usage_with_options(builtin_remote_usage, options);
391 memset(&states, 0, sizeof(states));
392 for (; argc; argc--, argv++) {
393 struct transport *transport;
394 const struct ref *ref;
395 struct strbuf buf;
396 int i, got_states;
398 states.remote = remote_get(*argv);
399 if (!states.remote)
400 return error("No such remote: %s", *argv);
401 transport = transport_get(NULL, states.remote->url_nr > 0 ?
402 states.remote->url[0] : NULL);
403 ref = transport_get_remote_refs(transport);
404 transport_disconnect(transport);
406 read_branches();
407 got_states = get_ref_states(ref, &states);
408 if (got_states)
409 result = error("Error getting local info for '%s'",
410 states.remote->name);
412 if (prune) {
413 struct strbuf buf;
414 int prefix_len;
416 strbuf_init(&buf, 0);
417 if (states.remote->fetch_refspec_nr == 1 &&
418 states.remote->fetch->pattern &&
419 !strcmp(states.remote->fetch->src,
420 states.remote->fetch->dst))
421 /* handle --mirror remote */
422 strbuf_addstr(&buf, "refs/heads/");
423 else
424 strbuf_addf(&buf, "refs/remotes/%s/", *argv);
425 prefix_len = buf.len;
427 for (i = 0; i < states.stale.nr; i++) {
428 strbuf_setlen(&buf, prefix_len);
429 strbuf_addstr(&buf, states.stale.items[i].path);
430 result |= delete_ref(buf.buf, NULL);
433 strbuf_release(&buf);
434 goto cleanup_states;
437 printf("* remote %s\n URL: %s\n", *argv,
438 states.remote->url_nr > 0 ?
439 states.remote->url[0] : "(no URL)");
441 for (i = 0; i < branch_list.nr; i++) {
442 struct path_list_item *branch = branch_list.items + i;
443 struct branch_info *info = branch->util;
444 int j;
446 if (!info->merge.nr || strcmp(*argv, info->remote))
447 continue;
448 printf(" Remote branch%s merged with 'git pull' "
449 "while on branch %s\n ",
450 info->merge.nr > 1 ? "es" : "",
451 branch->path);
452 for (j = 0; j < info->merge.nr; j++)
453 printf(" %s", info->merge.items[j].path);
454 printf("\n");
457 if (got_states)
458 continue;
459 strbuf_init(&buf, 0);
460 strbuf_addf(&buf, " New remote branch%%s (next fetch will "
461 "store in remotes/%s)", states.remote->name);
462 show_list(buf.buf, &states.new);
463 strbuf_release(&buf);
464 show_list(" Stale tracking branch%s (use 'git remote prune')",
465 &states.stale);
466 show_list(" Tracked remote branch%s",
467 &states.tracked);
469 if (states.remote->push_refspec_nr) {
470 printf(" Local branch%s pushed with 'git push'\n ",
471 states.remote->push_refspec_nr > 1 ?
472 "es" : "");
473 for (i = 0; i < states.remote->push_refspec_nr; i++) {
474 struct refspec *spec = states.remote->push + i;
475 printf(" %s%s%s%s", spec->force ? "+" : "",
476 skip_prefix(spec->src, "refs/heads/"),
477 spec->dst ? ":" : "",
478 skip_prefix(spec->dst, "refs/heads/"));
480 printf("\n");
482 cleanup_states:
483 /* NEEDSWORK: free remote */
484 path_list_clear(&states.new, 0);
485 path_list_clear(&states.stale, 0);
486 path_list_clear(&states.tracked, 0);
489 return result;
492 static int get_one_remote_for_update(struct remote *remote, void *priv)
494 struct path_list *list = priv;
495 if (!remote->skip_default_update)
496 path_list_append(xstrdup(remote->name), list);
497 return 0;
500 struct remote_group {
501 const char *name;
502 struct path_list *list;
503 } remote_group;
505 static int get_remote_group(const char *key, const char *value)
507 if (!prefixcmp(key, "remotes.") &&
508 !strcmp(key + 8, remote_group.name)) {
509 /* split list by white space */
510 int space = strcspn(value, " \t\n");
511 while (*value) {
512 if (space > 1)
513 path_list_append(xstrndup(value, space),
514 remote_group.list);
515 value += space + (value[space] != '\0');
516 space = strcspn(value, " \t\n");
520 return 0;
523 static int update(int argc, const char **argv)
525 int i, result = 0;
526 struct path_list list = { NULL, 0, 0, 0 };
527 static const char *default_argv[] = { NULL, "default", NULL };
529 if (argc < 2) {
530 argc = 2;
531 argv = default_argv;
534 remote_group.list = &list;
535 for (i = 1; i < argc; i++) {
536 remote_group.name = argv[i];
537 result = git_config(get_remote_group);
540 if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default"))
541 result = for_each_remote(get_one_remote_for_update, &list);
543 for (i = 0; i < list.nr; i++)
544 result |= fetch_remote(list.items[i].path);
546 /* all names were strdup()ed or strndup()ed */
547 list.strdup_paths = 1;
548 path_list_clear(&list, 0);
550 return result;
553 static int get_one_entry(struct remote *remote, void *priv)
555 struct path_list *list = priv;
557 path_list_append(remote->name, list)->util = remote->url_nr ?
558 (void *)remote->url[0] : NULL;
559 if (remote->url_nr > 1)
560 warning("Remote %s has more than one URL", remote->name);
562 return 0;
565 static int show_all(void)
567 struct path_list list = { NULL, 0, 0 };
568 int result = for_each_remote(get_one_entry, &list);
570 if (!result) {
571 int i;
573 sort_path_list(&list);
574 for (i = 0; i < list.nr; i++) {
575 struct path_list_item *item = list.items + i;
576 printf("%s%s%s\n", item->path,
577 verbose ? "\t" : "",
578 verbose && item->util ?
579 (const char *)item->util : "");
582 return result;
585 int cmd_remote(int argc, const char **argv, const char *prefix)
587 struct option options[] = {
588 OPT__VERBOSE(&verbose),
589 OPT_END()
591 int result;
593 argc = parse_options(argc, argv, options, builtin_remote_usage,
594 PARSE_OPT_STOP_AT_NON_OPTION);
596 if (argc < 1)
597 result = show_all();
598 else if (!strcmp(argv[0], "add"))
599 result = add(argc, argv);
600 else if (!strcmp(argv[0], "rm"))
601 result = rm(argc, argv);
602 else if (!strcmp(argv[0], "show"))
603 result = show_or_prune(argc, argv, 0);
604 else if (!strcmp(argv[0], "prune"))
605 result = show_or_prune(argc, argv, 1);
606 else if (!strcmp(argv[0], "update"))
607 result = update(argc, argv);
608 else {
609 error("Unknown subcommand: %s", argv[0]);
610 usage_with_options(builtin_remote_usage, options);
613 return result ? 1 : 0;