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