Code

Merge branch 'maint-1.5.4' into maint
[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"
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 };
20 static int verbose;
22 static inline int postfixcmp(const char *string, const char *postfix)
23 {
24         int len1 = strlen(string), len2 = strlen(postfix);
25         if (len1 < len2)
26                 return 1;
27         return strcmp(string + len1 - len2, postfix);
28 }
30 static inline const char *skip_prefix(const char *name, const char *prefix)
31 {
32         return !name ? "" :
33                 prefixcmp(name, prefix) ?  name : name + strlen(prefix);
34 }
36 static int opt_parse_track(const struct option *opt, const char *arg, int not)
37 {
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;
44 }
46 static int fetch_remote(const char *name)
47 {
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;
53 }
55 static int add(int argc, const char **argv)
56 {
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()
73         };
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                 if (mirror)
109                         strbuf_addf(&buf2, "refs/%s:refs/%s",
110                                         item->path, item->path);
111                 else
112                         strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
113                                         item->path, name, item->path);
114                 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
115                         return 1;
116         }
118         if (fetch && fetch_remote(name))
119                 return 1;
121         if (master) {
122                 strbuf_reset(&buf);
123                 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
125                 strbuf_reset(&buf2);
126                 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
128                 if (create_symref(buf.buf, buf2.buf, "remote add"))
129                         return error("Could not setup master '%s'", master);
130         }
132         strbuf_release(&buf);
133         strbuf_release(&buf2);
134         path_list_clear(&track, 0);
136         return 0;
139 struct branch_info {
140         char *remote;
141         struct path_list merge;
142 };
144 static struct path_list branch_list;
146 static int config_read_branches(const char *key, const char *value)
148         if (!prefixcmp(key, "branch.")) {
149                 char *name;
150                 struct path_list_item *item;
151                 struct branch_info *info;
152                 enum { REMOTE, MERGE } type;
154                 key += 7;
155                 if (!postfixcmp(key, ".remote")) {
156                         name = xstrndup(key, strlen(key) - 7);
157                         type = REMOTE;
158                 } else if (!postfixcmp(key, ".merge")) {
159                         name = xstrndup(key, strlen(key) - 6);
160                         type = MERGE;
161                 } else
162                         return 0;
164                 item = path_list_insert(name, &branch_list);
166                 if (!item->util)
167                         item->util = xcalloc(sizeof(struct branch_info), 1);
168                 info = item->util;
169                 if (type == REMOTE) {
170                         if (info->remote)
171                                 warning("more than one branch.%s", key);
172                         info->remote = xstrdup(value);
173                 } else {
174                         char *space = strchr(value, ' ');
175                         value = skip_prefix(value, "refs/heads/");
176                         while (space) {
177                                 char *merge;
178                                 merge = xstrndup(value, space - value);
179                                 path_list_append(merge, &info->merge);
180                                 value = skip_prefix(space + 1, "refs/heads/");
181                                 space = strchr(value, ' ');
182                         }
183                         path_list_append(xstrdup(value), &info->merge);
184                 }
185         }
186         return 0;
189 static void read_branches(void)
191         if (branch_list.nr)
192                 return;
193         git_config(config_read_branches);
194         sort_path_list(&branch_list);
197 struct ref_states {
198         struct remote *remote;
199         struct strbuf remote_prefix;
200         struct path_list new, stale, tracked;
201 };
203 static int handle_one_branch(const char *refname,
204         const unsigned char *sha1, int flags, void *cb_data)
206         struct ref_states *states = cb_data;
207         struct refspec refspec;
209         memset(&refspec, 0, sizeof(refspec));
210         refspec.dst = (char *)refname;
211         if (!remote_find_tracking(states->remote, &refspec)) {
212                 struct path_list_item *item;
213                 const char *name = skip_prefix(refspec.src, "refs/heads/");
214                 /* symbolic refs pointing nowhere were handled already */
215                 if ((flags & REF_ISSYMREF) ||
216                                 unsorted_path_list_has_path(&states->tracked,
217                                         name) ||
218                                 unsorted_path_list_has_path(&states->new,
219                                         name))
220                         return 0;
221                 item = path_list_append(name, &states->stale);
222                 item->util = xstrdup(refname);
223         }
224         return 0;
227 static int get_ref_states(const struct ref *ref, struct ref_states *states)
229         struct ref *fetch_map = NULL, **tail = &fetch_map;
230         int i;
232         for (i = 0; i < states->remote->fetch_refspec_nr; i++)
233                 if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
234                         die("Could not get fetch map for refspec %s",
235                                 states->remote->fetch_refspec[i]);
237         states->new.strdup_paths = states->tracked.strdup_paths = 1;
238         for (ref = fetch_map; ref; ref = ref->next) {
239                 struct path_list *target = &states->tracked;
240                 unsigned char sha1[20];
241                 void *util = NULL;
243                 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
244                         target = &states->new;
245                 else {
246                         target = &states->tracked;
247                         if (hashcmp(sha1, ref->new_sha1))
248                                 util = &states;
249                 }
250                 path_list_append(skip_prefix(ref->name, "refs/heads/"),
251                                 target)->util = util;
252         }
253         free_refs(fetch_map);
255         strbuf_addf(&states->remote_prefix,
256                 "refs/remotes/%s/", states->remote->name);
257         for_each_ref(handle_one_branch, states);
258         sort_path_list(&states->stale);
260         return 0;
263 struct branches_for_remote {
264         const char *prefix;
265         struct path_list *branches;
266 };
268 static int add_branch_for_removal(const char *refname,
269         const unsigned char *sha1, int flags, void *cb_data)
271         struct branches_for_remote *branches = cb_data;
273         if (!prefixcmp(refname, branches->prefix)) {
274                 struct path_list_item *item;
276                 /* make sure that symrefs are deleted */
277                 if (flags & REF_ISSYMREF)
278                         return unlink(git_path(refname));
280                 item = path_list_append(refname, branches->branches);
281                 item->util = xmalloc(20);
282                 hashcpy(item->util, sha1);
283         }
285         return 0;
288 static int remove_branches(struct path_list *branches)
290         int i, result = 0;
291         for (i = 0; i < branches->nr; i++) {
292                 struct path_list_item *item = branches->items + i;
293                 const char *refname = item->path;
294                 unsigned char *sha1 = item->util;
296                 if (delete_ref(refname, sha1))
297                         result |= error("Could not remove branch %s", refname);
298         }
299         return result;
302 static int rm(int argc, const char **argv)
304         struct option options[] = {
305                 OPT_END()
306         };
307         struct remote *remote;
308         struct strbuf buf;
309         struct path_list branches = { NULL, 0, 0, 1 };
310         struct branches_for_remote cb_data = { NULL, &branches };
311         int i;
313         if (argc != 2)
314                 usage_with_options(builtin_remote_usage, options);
316         remote = remote_get(argv[1]);
317         if (!remote)
318                 die("No such remote: %s", argv[1]);
320         strbuf_init(&buf, 0);
321         strbuf_addf(&buf, "remote.%s", remote->name);
322         if (git_config_rename_section(buf.buf, NULL) < 1)
323                 return error("Could not remove config section '%s'", buf.buf);
325         read_branches();
326         for (i = 0; i < branch_list.nr; i++) {
327                 struct path_list_item *item = branch_list.items + i;
328                 struct branch_info *info = item->util;
329                 if (info->remote && !strcmp(info->remote, remote->name)) {
330                         const char *keys[] = { "remote", "merge", NULL }, **k;
331                         for (k = keys; *k; k++) {
332                                 strbuf_reset(&buf);
333                                 strbuf_addf(&buf, "branch.%s.%s",
334                                                 item->path, *k);
335                                 if (git_config_set(buf.buf, NULL)) {
336                                         strbuf_release(&buf);
337                                         return -1;
338                                 }
339                         }
340                 }
341         }
343         /*
344          * We cannot just pass a function to for_each_ref() which deletes
345          * the branches one by one, since for_each_ref() relies on cached
346          * refs, which are invalidated when deleting a branch.
347          */
348         strbuf_reset(&buf);
349         strbuf_addf(&buf, "refs/remotes/%s/", remote->name);
350         cb_data.prefix = buf.buf;
351         i = for_each_ref(add_branch_for_removal, &cb_data);
352         strbuf_release(&buf);
354         if (!i)
355                 i = remove_branches(&branches);
356         path_list_clear(&branches, 1);
358         return i;
361 static void show_list(const char *title, struct path_list *list)
363         int i;
365         if (!list->nr)
366                 return;
368         printf(title, list->nr > 1 ? "es" : "");
369         printf("\n    ");
370         for (i = 0; i < list->nr; i++)
371                 printf("%s%s", i ? " " : "", list->items[i].path);
372         printf("\n");
375 static int show_or_prune(int argc, const char **argv, int prune)
377         int dry_run = 0, result = 0;
378         struct option options[] = {
379                 OPT_GROUP("show specific options"),
380                 OPT__DRY_RUN(&dry_run),
381                 OPT_END()
382         };
383         struct ref_states states;
385         argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
387         if (argc < 1)
388                 usage_with_options(builtin_remote_usage, options);
390         memset(&states, 0, sizeof(states));
391         for (; argc; argc--, argv++) {
392                 struct transport *transport;
393                 const struct ref *ref;
394                 struct strbuf buf;
395                 int i, got_states;
397                 states.remote = remote_get(*argv);
398                 if (!states.remote)
399                         return error("No such remote: %s", *argv);
400                 transport = transport_get(NULL, states.remote->url_nr > 0 ?
401                         states.remote->url[0] : NULL);
402                 ref = transport_get_remote_refs(transport);
403                 transport_disconnect(transport);
405                 read_branches();
406                 got_states = get_ref_states(ref, &states);
407                 if (got_states)
408                         result = error("Error getting local info for '%s'",
409                                         states.remote->name);
411                 if (prune) {
412                         struct strbuf buf;
413                         int prefix_len;
415                         strbuf_init(&buf, 0);
416                         if (states.remote->fetch_refspec_nr == 1 &&
417                                         states.remote->fetch->pattern &&
418                                         !strcmp(states.remote->fetch->src,
419                                                 states.remote->fetch->dst))
420                                 /* handle --mirror remote */
421                                 strbuf_addstr(&buf, "refs/heads/");
422                         else
423                                 strbuf_addf(&buf, "refs/remotes/%s/", *argv);
424                         prefix_len = buf.len;
426                         for (i = 0; i < states.stale.nr; i++) {
427                                 strbuf_setlen(&buf, prefix_len);
428                                 strbuf_addstr(&buf, states.stale.items[i].path);
429                                 result |= delete_ref(buf.buf, NULL);
430                         }
432                         strbuf_release(&buf);
433                         goto cleanup_states;
434                 }
436                 printf("* remote %s\n  URL: %s\n", *argv,
437                         states.remote->url_nr > 0 ?
438                                 states.remote->url[0] : "(no URL)");
440                 for (i = 0; i < branch_list.nr; i++) {
441                         struct path_list_item *branch = branch_list.items + i;
442                         struct branch_info *info = branch->util;
443                         int j;
445                         if (!info->merge.nr || strcmp(*argv, info->remote))
446                                 continue;
447                         printf("  Remote branch%s merged with 'git pull' "
448                                 "while on branch %s\n   ",
449                                 info->merge.nr > 1 ? "es" : "",
450                                 branch->path);
451                         for (j = 0; j < info->merge.nr; j++)
452                                 printf(" %s", info->merge.items[j].path);
453                         printf("\n");
454                 }
456                 if (got_states)
457                         continue;
458                 strbuf_init(&buf, 0);
459                 strbuf_addf(&buf, "  New remote branch%%s (next fetch will "
460                         "store in remotes/%s)", states.remote->name);
461                 show_list(buf.buf, &states.new);
462                 strbuf_release(&buf);
463                 show_list("  Stale tracking branch%s (use 'git remote prune')",
464                                 &states.stale);
465                 show_list("  Tracked remote branch%s",
466                                 &states.tracked);
468                 if (states.remote->push_refspec_nr) {
469                         printf("  Local branch%s pushed with 'git push'\n   ",
470                                 states.remote->push_refspec_nr > 1 ?
471                                         "es" : "");
472                         for (i = 0; i < states.remote->push_refspec_nr; i++) {
473                                 struct refspec *spec = states.remote->push + i;
474                                 printf(" %s%s%s%s", spec->force ? "+" : "",
475                                         skip_prefix(spec->src, "refs/heads/"),
476                                         spec->dst ? ":" : "",
477                                         skip_prefix(spec->dst, "refs/heads/"));
478                         }
479                         printf("\n");
480                 }
481 cleanup_states:
482                 /* NEEDSWORK: free remote */
483                 path_list_clear(&states.new, 0);
484                 path_list_clear(&states.stale, 0);
485                 path_list_clear(&states.tracked, 0);
486         }
488         return result;
491 static int get_one_remote_for_update(struct remote *remote, void *priv)
493         struct path_list *list = priv;
494         if (!remote->skip_default_update)
495                 path_list_append(xstrdup(remote->name), list);
496         return 0;
499 struct remote_group {
500         const char *name;
501         struct path_list *list;
502 } remote_group;
504 static int get_remote_group(const char *key, const char *value)
506         if (!prefixcmp(key, "remotes.") &&
507                         !strcmp(key + 8, remote_group.name)) {
508                 /* split list by white space */
509                 int space = strcspn(value, " \t\n");
510                 while (*value) {
511                         if (space > 1)
512                                 path_list_append(xstrndup(value, space),
513                                                 remote_group.list);
514                         value += space + (value[space] != '\0');
515                         space = strcspn(value, " \t\n");
516                 }
517         }
519         return 0;
522 static int update(int argc, const char **argv)
524         int i, result = 0;
525         struct path_list list = { NULL, 0, 0, 0 };
526         static const char *default_argv[] = { NULL, "default", NULL };
528         if (argc < 2) {
529                 argc = 2;
530                 argv = default_argv;
531         }
533         remote_group.list = &list;
534         for (i = 1; i < argc; i++) {
535                 remote_group.name = argv[i];
536                 result = git_config(get_remote_group);
537         }
539         if (!result && !list.nr  && argc == 2 && !strcmp(argv[1], "default"))
540                 result = for_each_remote(get_one_remote_for_update, &list);
542         for (i = 0; i < list.nr; i++)
543                 result |= fetch_remote(list.items[i].path);
545         /* all names were strdup()ed or strndup()ed */
546         list.strdup_paths = 1;
547         path_list_clear(&list, 0);
549         return result;
552 static int get_one_entry(struct remote *remote, void *priv)
554         struct path_list *list = priv;
556         path_list_append(remote->name, list)->util = remote->url_nr ?
557                 (void *)remote->url[0] : NULL;
558         if (remote->url_nr > 1)
559                 warning("Remote %s has more than one URL", remote->name);
561         return 0;
564 static int show_all(void)
566         struct path_list list = { NULL, 0, 0 };
567         int result = for_each_remote(get_one_entry, &list);
569         if (!result) {
570                 int i;
572                 sort_path_list(&list);
573                 for (i = 0; i < list.nr; i++) {
574                         struct path_list_item *item = list.items + i;
575                         printf("%s%s%s\n", item->path,
576                                 verbose ? "\t" : "",
577                                 verbose && item->util ?
578                                         (const char *)item->util : "");
579                 }
580         }
581         return result;
584 int cmd_remote(int argc, const char **argv, const char *prefix)
586         struct option options[] = {
587                 OPT__VERBOSE(&verbose),
588                 OPT_END()
589         };
590         int result;
592         argc = parse_options(argc, argv, options, builtin_remote_usage,
593                 PARSE_OPT_STOP_AT_NON_OPTION);
595         if (argc < 1)
596                 result = show_all();
597         else if (!strcmp(argv[0], "add"))
598                 result = add(argc, argv);
599         else if (!strcmp(argv[0], "rm"))
600                 result = rm(argc, argv);
601         else if (!strcmp(argv[0], "show"))
602                 result = show_or_prune(argc, argv, 0);
603         else if (!strcmp(argv[0], "prune"))
604                 result = show_or_prune(argc, argv, 1);
605         else if (!strcmp(argv[0], "update"))
606                 result = update(argc, argv);
607         else {
608                 error("Unknown subcommand: %s", argv[0]);
609                 usage_with_options(builtin_remote_usage, options);
610         }
612         return result ? 1 : 0;