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