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