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