]> asedeno.scripts.mit.edu Git - git.git/blob - builtin-remote.c
git-remote: show all remotes with "git remote show"
[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(&buf, "remote.%s.url", name);
94         if (git_config_set(buf.buf, url))
95                 return 1;
96
97         if (track.nr == 0)
98                 path_list_append("*", &track);
99         for (i = 0; i < track.nr; i++) {
100                 struct path_list_item *item = track.items + i;
101
102                 strbuf_reset(&buf);
103                 strbuf_addf(&buf, "remote.%s.fetch", name);
104
105                 strbuf_reset(&buf2);
106                 if (mirror)
107                         strbuf_addf(&buf2, "refs/%s:refs/%s",
108                                         item->path, item->path);
109                 else
110                         strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s",
111                                         item->path, name, item->path);
112                 if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0))
113                         return 1;
114         }
115
116         if (fetch && fetch_remote(name))
117                 return 1;
118
119         if (master) {
120                 strbuf_reset(&buf);
121                 strbuf_addf(&buf, "refs/remotes/%s/HEAD", name);
122
123                 strbuf_reset(&buf2);
124                 strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master);
125
126                 if (create_symref(buf.buf, buf2.buf, "remote add"))
127                         return error("Could not setup master '%s'", master);
128         }
129
130         strbuf_release(&buf);
131         strbuf_release(&buf2);
132         path_list_clear(&track, 0);
133
134         return 0;
135 }
136
137 struct branch_info {
138         char *remote;
139         struct path_list merge;
140 };
141
142 static struct path_list branch_list;
143
144 static int config_read_branches(const char *key, const char *value)
145 {
146         if (!prefixcmp(key, "branch.")) {
147                 char *name;
148                 struct path_list_item *item;
149                 struct branch_info *info;
150                 enum { REMOTE, MERGE } type;
151
152                 key += 7;
153                 if (!postfixcmp(key, ".remote")) {
154                         name = xstrndup(key, strlen(key) - 7);
155                         type = REMOTE;
156                 } else if (!postfixcmp(key, ".merge")) {
157                         name = xstrndup(key, strlen(key) - 6);
158                         type = MERGE;
159                 } else
160                         return 0;
161
162                 item = path_list_insert(name, &branch_list);
163
164                 if (!item->util)
165                         item->util = xcalloc(sizeof(struct branch_info), 1);
166                 info = item->util;
167                 if (type == REMOTE) {
168                         if (info->remote)
169                                 warning("more than one branch.%s", key);
170                         info->remote = xstrdup(value);
171                 } else {
172                         char *space = strchr(value, ' ');
173                         value = skip_prefix(value, "refs/heads/");
174                         while (space) {
175                                 char *merge;
176                                 merge = xstrndup(value, space - value);
177                                 path_list_append(merge, &info->merge);
178                                 value = skip_prefix(space + 1, "refs/heads/");
179                                 space = strchr(value, ' ');
180                         }
181                         path_list_append(xstrdup(value), &info->merge);
182                 }
183         }
184         return 0;
185 }
186
187 static void read_branches(void)
188 {
189         if (branch_list.nr)
190                 return;
191         git_config(config_read_branches);
192         sort_path_list(&branch_list);
193 }
194
195 struct ref_states {
196         struct remote *remote;
197         struct strbuf remote_prefix;
198         struct path_list new, stale, tracked;
199 };
200
201 static int handle_one_branch(const char *refname,
202         const unsigned char *sha1, int flags, void *cb_data)
203 {
204         struct ref_states *states = cb_data;
205         struct refspec refspec;
206
207         memset(&refspec, 0, sizeof(refspec));
208         refspec.dst = (char *)refname;
209         if (!remote_find_tracking(states->remote, &refspec)) {
210                 struct path_list_item *item;
211                 const char *name = skip_prefix(refspec.src, "refs/heads/");
212                 /* symbolic refs pointing nowhere were handled already */
213                 if ((flags & REF_ISSYMREF) ||
214                                 unsorted_path_list_has_path(&states->tracked,
215                                         name) ||
216                                 unsorted_path_list_has_path(&states->new,
217                                         name))
218                         return 0;
219                 item = path_list_append(name, &states->stale);
220                 item->util = xstrdup(refname);
221         }
222         return 0;
223 }
224
225 static int get_ref_states(const struct ref *ref, struct ref_states *states)
226 {
227         struct ref *fetch_map = NULL, **tail = &fetch_map;
228         int i;
229
230         for (i = 0; i < states->remote->fetch_refspec_nr; i++)
231                 if (get_fetch_map(ref, states->remote->fetch + i, &tail, 1))
232                         die("Could not get fetch map for refspec %s",
233                                 states->remote->fetch_refspec[i]);
234
235         states->new.strdup_paths = states->tracked.strdup_paths = 1;
236         for (ref = fetch_map; ref; ref = ref->next) {
237                 struct path_list *target = &states->tracked;
238                 unsigned char sha1[20];
239                 void *util = NULL;
240
241                 if (!ref->peer_ref || read_ref(ref->peer_ref->name, sha1))
242                         target = &states->new;
243                 else {
244                         target = &states->tracked;
245                         if (hashcmp(sha1, ref->new_sha1))
246                                 util = &states;
247                 }
248                 path_list_append(skip_prefix(ref->name, "refs/heads/"),
249                                 target)->util = util;
250         }
251         free_refs(fetch_map);
252
253         strbuf_addf(&states->remote_prefix,
254                 "refs/remotes/%s/", states->remote->name);
255         for_each_ref(handle_one_branch, states);
256         sort_path_list(&states->stale);
257
258         return 0;
259 }
260
261 struct branches_for_remote {
262         const char *prefix;
263         struct path_list *branches;
264 };
265
266 static int add_branch_for_removal(const char *refname,
267         const unsigned char *sha1, int flags, void *cb_data)
268 {
269         struct branches_for_remote *branches = cb_data;
270
271         if (!prefixcmp(refname, branches->prefix)) {
272                 struct path_list_item *item;
273
274                 /* make sure that symrefs are deleted */
275                 if (flags & REF_ISSYMREF)
276                         return unlink(git_path(refname));
277
278                 item = path_list_append(refname, branches->branches);
279                 item->util = xmalloc(20);
280                 hashcpy(item->util, sha1);
281         }
282
283         return 0;
284 }
285
286 static int remove_branches(struct path_list *branches)
287 {
288         int i, result = 0;
289         for (i = 0; i < branches->nr; i++) {
290                 struct path_list_item *item = branches->items + i;
291                 const char *refname = item->path;
292                 unsigned char *sha1 = item->util;
293
294                 if (delete_ref(refname, sha1))
295                         result |= error("Could not remove branch %s", refname);
296         }
297         return result;
298 }
299
300 static int rm(int argc, const char **argv)
301 {
302         struct option options[] = {
303                 OPT_END()
304         };
305         struct remote *remote;
306         struct strbuf buf;
307         struct path_list branches = { NULL, 0, 0, 1 };
308         struct branches_for_remote cb_data = { NULL, &branches };
309         int i;
310
311         if (argc != 2)
312                 usage_with_options(builtin_remote_usage, options);
313
314         remote = remote_get(argv[1]);
315         if (!remote)
316                 die("No such remote: %s", argv[1]);
317
318         strbuf_init(&buf, 0);
319         strbuf_addf(&buf, "remote.%s", remote->name);
320         if (git_config_rename_section(buf.buf, NULL) < 1)
321                 return error("Could not remove config section '%s'", buf.buf);
322
323         read_branches();
324         for (i = 0; i < branch_list.nr; i++) {
325                 struct path_list_item *item = branch_list.items + i;
326                 struct branch_info *info = item->util;
327                 if (info->remote && !strcmp(info->remote, remote->name)) {
328                         const char *keys[] = { "remote", "merge", NULL }, **k;
329                         for (k = keys; *k; k++) {
330                                 strbuf_reset(&buf);
331                                 strbuf_addf(&buf, "branch.%s.%s",
332                                                 item->path, *k);
333                                 if (git_config_set(buf.buf, NULL)) {
334                                         strbuf_release(&buf);
335                                         return -1;
336                                 }
337                         }
338                 }
339         }
340
341         /*
342          * We cannot just pass a function to for_each_ref() which deletes
343          * the branches one by one, since for_each_ref() relies on cached
344          * refs, which are invalidated when deleting a branch.
345          */
346         strbuf_reset(&buf);
347         strbuf_addf(&buf, "refs/remotes/%s/", remote->name);
348         cb_data.prefix = buf.buf;
349         i = for_each_ref(add_branch_for_removal, &cb_data);
350         strbuf_release(&buf);
351
352         if (!i)
353                 i = remove_branches(&branches);
354         path_list_clear(&branches, 1);
355
356         return i;
357 }
358
359 static void show_list(const char *title, struct path_list *list)
360 {
361         int i;
362
363         if (!list->nr)
364                 return;
365
366         printf(title, list->nr > 1 ? "es" : "");
367         printf("\n    ");
368         for (i = 0; i < list->nr; i++)
369                 printf("%s%s", i ? " " : "", list->items[i].path);
370         printf("\n");
371 }
372
373 static int show_or_prune(int argc, const char **argv, int prune)
374 {
375         int dry_run = 0, result = 0;
376         struct option options[] = {
377                 OPT_GROUP("show specific options"),
378                 OPT__DRY_RUN(&dry_run),
379                 OPT_END()
380         };
381         struct ref_states states;
382
383         argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
384
385         if (argc < 1) {
386                 if (!prune)
387                         return show_all();
388                 usage_with_options(builtin_remote_usage, options);
389         }
390
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;
397
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);
405
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);
411
412                 if (prune) {
413                         struct strbuf buf;
414                         int prefix_len;
415
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;
426
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);
431                         }
432
433                         strbuf_release(&buf);
434                         goto cleanup_states;
435                 }
436
437                 printf("* remote %s\n  URL: %s\n", *argv,
438                         states.remote->url_nr > 0 ?
439                                 states.remote->url[0] : "(no URL)");
440
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;
445
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");
455                 }
456
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);
468
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/"));
479                         }
480                         printf("\n");
481                 }
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);
487         }
488
489         return result;
490 }
491
492 static int get_one_remote_for_update(struct remote *remote, void *priv)
493 {
494         struct path_list *list = priv;
495         if (!remote->skip_default_update)
496                 path_list_append(xstrdup(remote->name), list);
497         return 0;
498 }
499
500 struct remote_group {
501         const char *name;
502         struct path_list *list;
503 } remote_group;
504
505 static int get_remote_group(const char *key, const char *value)
506 {
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");
517                 }
518         }
519
520         return 0;
521 }
522
523 static int update(int argc, const char **argv)
524 {
525         int i, result = 0;
526         struct path_list list = { NULL, 0, 0, 0 };
527         static const char *default_argv[] = { NULL, "default", NULL };
528
529         if (argc < 2) {
530                 argc = 2;
531                 argv = default_argv;
532         }
533
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);
538         }
539
540         if (!result && !list.nr  && argc == 2 && !strcmp(argv[1], "default"))
541                 result = for_each_remote(get_one_remote_for_update, &list);
542
543         for (i = 0; i < list.nr; i++)
544                 result |= fetch_remote(list.items[i].path);
545
546         /* all names were strdup()ed or strndup()ed */
547         list.strdup_paths = 1;
548         path_list_clear(&list, 0);
549
550         return result;
551 }
552
553 static int get_one_entry(struct remote *remote, void *priv)
554 {
555         struct path_list *list = priv;
556
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);
561
562         return 0;
563 }
564
565 static int show_all(void)
566 {
567         struct path_list list = { NULL, 0, 0 };
568         int result = for_each_remote(get_one_entry, &list);
569
570         if (!result) {
571                 int i;
572
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 : "");
580                 }
581         }
582         return result;
583 }
584
585 int cmd_remote(int argc, const char **argv, const char *prefix)
586 {
587         struct option options[] = {
588                 OPT__VERBOSE(&verbose),
589                 OPT_END()
590         };
591         int result;
592
593         argc = parse_options(argc, argv, options, builtin_remote_usage,
594                 PARSE_OPT_STOP_AT_NON_OPTION);
595
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);
611         }
612
613         return result ? 1 : 0;
614 }