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