]>
Commit | Line | Data |
---|---|---|
211c8968 JS |
1 | #include "cache.h" |
2 | #include "parse-options.h" | |
3 | #include "transport.h" | |
4 | #include "remote.h" | |
c455c87c | 5 | #include "string-list.h" |
211c8968 JS |
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 | ||
c4112bb6 JK |
22 | static int show_all(void); |
23 | ||
211c8968 JS |
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 | ||
211c8968 JS |
32 | static int opt_parse_track(const struct option *opt, const char *arg, int not) |
33 | { | |
c455c87c | 34 | struct string_list *list = opt->value; |
211c8968 | 35 | if (not) |
c455c87c | 36 | string_list_clear(list, 0); |
211c8968 | 37 | else |
c455c87c | 38 | string_list_append(arg, list); |
211c8968 JS |
39 | return 0; |
40 | } | |
41 | ||
42 | static int fetch_remote(const char *name) | |
43 | { | |
44 | const char *argv[] = { "fetch", name, NULL }; | |
3000658f | 45 | printf("Updating %s\n", name); |
211c8968 JS |
46 | if (run_command_v_opt(argv, RUN_GIT_CMD)) |
47 | return error("Could not fetch %s", name); | |
48 | return 0; | |
49 | } | |
50 | ||
51 | static int add(int argc, const char **argv) | |
52 | { | |
53 | int fetch = 0, mirror = 0; | |
c455c87c | 54 | struct string_list track = { NULL, 0, 0 }; |
211c8968 JS |
55 | const char *master = NULL; |
56 | struct remote *remote; | |
f285a2d7 | 57 | struct strbuf buf = STRBUF_INIT, buf2 = STRBUF_INIT; |
211c8968 JS |
58 | const char *name, *url; |
59 | int i; | |
60 | ||
61 | struct option options[] = { | |
62 | OPT_GROUP("add specific options"), | |
63 | OPT_BOOLEAN('f', "fetch", &fetch, "fetch the remote branches"), | |
64 | OPT_CALLBACK('t', "track", &track, "branch", | |
65 | "branch(es) to track", opt_parse_track), | |
66 | OPT_STRING('m', "master", &master, "branch", "master branch"), | |
67 | OPT_BOOLEAN(0, "mirror", &mirror, "no separate remotes"), | |
68 | OPT_END() | |
69 | }; | |
70 | ||
71 | argc = parse_options(argc, argv, options, builtin_remote_usage, 0); | |
72 | ||
73 | if (argc < 2) | |
74 | usage_with_options(builtin_remote_usage, options); | |
75 | ||
76 | name = argv[0]; | |
77 | url = argv[1]; | |
78 | ||
79 | remote = remote_get(name); | |
80 | if (remote && (remote->url_nr > 1 || strcmp(name, remote->url[0]) || | |
81 | remote->fetch_refspec_nr)) | |
82 | die("remote %s already exists.", name); | |
83 | ||
24b6177e JF |
84 | strbuf_addf(&buf2, "refs/heads/test:refs/remotes/%s/test", name); |
85 | if (!valid_fetch_refspec(buf2.buf)) | |
86 | die("'%s' is not a valid remote name", name); | |
87 | ||
211c8968 JS |
88 | strbuf_addf(&buf, "remote.%s.url", name); |
89 | if (git_config_set(buf.buf, url)) | |
90 | return 1; | |
91 | ||
24b6177e JF |
92 | strbuf_reset(&buf); |
93 | strbuf_addf(&buf, "remote.%s.fetch", name); | |
94 | ||
211c8968 | 95 | if (track.nr == 0) |
c455c87c | 96 | string_list_append("*", &track); |
211c8968 | 97 | for (i = 0; i < track.nr; i++) { |
c455c87c | 98 | struct string_list_item *item = track.items + i; |
211c8968 | 99 | |
211c8968 | 100 | strbuf_reset(&buf2); |
1ce89cc4 | 101 | strbuf_addch(&buf2, '+'); |
211c8968 JS |
102 | if (mirror) |
103 | strbuf_addf(&buf2, "refs/%s:refs/%s", | |
c455c87c | 104 | item->string, item->string); |
211c8968 JS |
105 | else |
106 | strbuf_addf(&buf2, "refs/heads/%s:refs/remotes/%s/%s", | |
c455c87c | 107 | item->string, name, item->string); |
211c8968 JS |
108 | if (git_config_set_multivar(buf.buf, buf2.buf, "^$", 0)) |
109 | return 1; | |
110 | } | |
111 | ||
84bb2dfd PB |
112 | if (mirror) { |
113 | strbuf_reset(&buf); | |
114 | strbuf_addf(&buf, "remote.%s.mirror", name); | |
bc699afc | 115 | if (git_config_set(buf.buf, "true")) |
84bb2dfd PB |
116 | return 1; |
117 | } | |
118 | ||
211c8968 JS |
119 | if (fetch && fetch_remote(name)) |
120 | return 1; | |
121 | ||
122 | if (master) { | |
123 | strbuf_reset(&buf); | |
124 | strbuf_addf(&buf, "refs/remotes/%s/HEAD", name); | |
125 | ||
126 | strbuf_reset(&buf2); | |
127 | strbuf_addf(&buf2, "refs/remotes/%s/%s", name, master); | |
128 | ||
129 | if (create_symref(buf.buf, buf2.buf, "remote add")) | |
130 | return error("Could not setup master '%s'", master); | |
131 | } | |
132 | ||
133 | strbuf_release(&buf); | |
134 | strbuf_release(&buf2); | |
c455c87c | 135 | string_list_clear(&track, 0); |
211c8968 JS |
136 | |
137 | return 0; | |
138 | } | |
139 | ||
140 | struct branch_info { | |
141 | char *remote; | |
c455c87c | 142 | struct string_list merge; |
211c8968 JS |
143 | }; |
144 | ||
c455c87c | 145 | static struct string_list branch_list; |
211c8968 | 146 | |
72972eb3 JH |
147 | static const char *abbrev_ref(const char *name, const char *prefix) |
148 | { | |
149 | const char *abbrev = skip_prefix(name, prefix); | |
150 | if (abbrev) | |
151 | return abbrev; | |
152 | return name; | |
153 | } | |
154 | #define abbrev_branch(name) abbrev_ref((name), "refs/heads/") | |
155 | ||
ef90d6d4 | 156 | static int config_read_branches(const char *key, const char *value, void *cb) |
211c8968 JS |
157 | { |
158 | if (!prefixcmp(key, "branch.")) { | |
159 | char *name; | |
c455c87c | 160 | struct string_list_item *item; |
211c8968 JS |
161 | struct branch_info *info; |
162 | enum { REMOTE, MERGE } type; | |
163 | ||
164 | key += 7; | |
165 | if (!postfixcmp(key, ".remote")) { | |
166 | name = xstrndup(key, strlen(key) - 7); | |
167 | type = REMOTE; | |
168 | } else if (!postfixcmp(key, ".merge")) { | |
169 | name = xstrndup(key, strlen(key) - 6); | |
170 | type = MERGE; | |
171 | } else | |
172 | return 0; | |
173 | ||
c455c87c | 174 | item = string_list_insert(name, &branch_list); |
211c8968 JS |
175 | |
176 | if (!item->util) | |
177 | item->util = xcalloc(sizeof(struct branch_info), 1); | |
178 | info = item->util; | |
179 | if (type == REMOTE) { | |
180 | if (info->remote) | |
181 | warning("more than one branch.%s", key); | |
182 | info->remote = xstrdup(value); | |
183 | } else { | |
184 | char *space = strchr(value, ' '); | |
72972eb3 | 185 | value = abbrev_branch(value); |
211c8968 JS |
186 | while (space) { |
187 | char *merge; | |
188 | merge = xstrndup(value, space - value); | |
c455c87c | 189 | string_list_append(merge, &info->merge); |
72972eb3 | 190 | value = abbrev_branch(space + 1); |
211c8968 JS |
191 | space = strchr(value, ' '); |
192 | } | |
c455c87c | 193 | string_list_append(xstrdup(value), &info->merge); |
211c8968 JS |
194 | } |
195 | } | |
196 | return 0; | |
197 | } | |
198 | ||
199 | static void read_branches(void) | |
200 | { | |
201 | if (branch_list.nr) | |
202 | return; | |
ef90d6d4 | 203 | git_config(config_read_branches, NULL); |
c455c87c | 204 | sort_string_list(&branch_list); |
211c8968 JS |
205 | } |
206 | ||
207 | struct ref_states { | |
208 | struct remote *remote; | |
c455c87c | 209 | struct string_list new, stale, tracked; |
211c8968 JS |
210 | }; |
211 | ||
212 | static 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)) { | |
c455c87c | 221 | struct string_list_item *item; |
72972eb3 | 222 | const char *name = abbrev_branch(refspec.src); |
740fdd27 JS |
223 | /* symbolic refs pointing nowhere were handled already */ |
224 | if ((flags & REF_ISSYMREF) || | |
c455c87c | 225 | unsorted_string_list_has_string(&states->tracked, |
740fdd27 | 226 | name) || |
c455c87c | 227 | unsorted_string_list_has_string(&states->new, |
211c8968 JS |
228 | name)) |
229 | return 0; | |
c455c87c | 230 | item = string_list_append(name, &states->stale); |
211c8968 JS |
231 | item->util = xstrdup(refname); |
232 | } | |
233 | return 0; | |
234 | } | |
235 | ||
236 | static 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 | ||
c455c87c | 246 | states->new.strdup_strings = states->tracked.strdup_strings = 1; |
211c8968 | 247 | for (ref = fetch_map; ref; ref = ref->next) { |
c455c87c | 248 | struct string_list *target = &states->tracked; |
211c8968 JS |
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 | } | |
c455c87c | 259 | string_list_append(abbrev_branch(ref->name), target)->util = util; |
211c8968 JS |
260 | } |
261 | free_refs(fetch_map); | |
262 | ||
211c8968 | 263 | for_each_ref(handle_one_branch, states); |
c455c87c | 264 | sort_string_list(&states->stale); |
211c8968 JS |
265 | |
266 | return 0; | |
267 | } | |
268 | ||
7ad2458f SP |
269 | struct known_remote { |
270 | struct known_remote *next; | |
271 | struct remote *remote; | |
272 | }; | |
273 | ||
274 | struct known_remotes { | |
275 | struct remote *to_delete; | |
276 | struct known_remote *list; | |
277 | }; | |
278 | ||
279 | static int add_known_remote(struct remote *remote, void *cb_data) | |
280 | { | |
281 | struct known_remotes *all = cb_data; | |
282 | struct known_remote *r; | |
283 | ||
284 | if (!strcmp(all->to_delete->name, remote->name)) | |
285 | return 0; | |
286 | ||
287 | r = xmalloc(sizeof(*r)); | |
288 | r->remote = remote; | |
289 | r->next = all->list; | |
290 | all->list = r; | |
291 | return 0; | |
292 | } | |
293 | ||
211c8968 | 294 | struct branches_for_remote { |
7ad2458f | 295 | struct remote *remote; |
c455c87c | 296 | struct string_list *branches; |
7ad2458f | 297 | struct known_remotes *keep; |
211c8968 JS |
298 | }; |
299 | ||
300 | static int add_branch_for_removal(const char *refname, | |
301 | const unsigned char *sha1, int flags, void *cb_data) | |
302 | { | |
303 | struct branches_for_remote *branches = cb_data; | |
7ad2458f | 304 | struct refspec refspec; |
c455c87c | 305 | struct string_list_item *item; |
7ad2458f | 306 | struct known_remote *kr; |
211c8968 | 307 | |
7ad2458f SP |
308 | memset(&refspec, 0, sizeof(refspec)); |
309 | refspec.dst = (char *)refname; | |
310 | if (remote_find_tracking(branches->remote, &refspec)) | |
311 | return 0; | |
312 | ||
313 | /* don't delete a branch if another remote also uses it */ | |
314 | for (kr = branches->keep->list; kr; kr = kr->next) { | |
315 | memset(&refspec, 0, sizeof(refspec)); | |
316 | refspec.dst = (char *)refname; | |
317 | if (!remote_find_tracking(kr->remote, &refspec)) | |
318 | return 0; | |
319 | } | |
3b9dcff5 | 320 | |
7ad2458f SP |
321 | /* make sure that symrefs are deleted */ |
322 | if (flags & REF_ISSYMREF) | |
323 | return unlink(git_path(refname)); | |
3b9dcff5 | 324 | |
c455c87c | 325 | item = string_list_append(refname, branches->branches); |
7ad2458f SP |
326 | item->util = xmalloc(20); |
327 | hashcpy(item->util, sha1); | |
211c8968 JS |
328 | |
329 | return 0; | |
330 | } | |
331 | ||
c455c87c | 332 | static int remove_branches(struct string_list *branches) |
211c8968 JS |
333 | { |
334 | int i, result = 0; | |
335 | for (i = 0; i < branches->nr; i++) { | |
c455c87c JS |
336 | struct string_list_item *item = branches->items + i; |
337 | const char *refname = item->string; | |
211c8968 JS |
338 | unsigned char *sha1 = item->util; |
339 | ||
eca35a25 | 340 | if (delete_ref(refname, sha1, 0)) |
211c8968 JS |
341 | result |= error("Could not remove branch %s", refname); |
342 | } | |
343 | return result; | |
344 | } | |
345 | ||
346 | static int rm(int argc, const char **argv) | |
347 | { | |
348 | struct option options[] = { | |
349 | OPT_END() | |
350 | }; | |
351 | struct remote *remote; | |
f285a2d7 | 352 | struct strbuf buf = STRBUF_INIT; |
7ad2458f | 353 | struct known_remotes known_remotes = { NULL, NULL }; |
c455c87c | 354 | struct string_list branches = { NULL, 0, 0, 1 }; |
7ad2458f | 355 | struct branches_for_remote cb_data = { NULL, &branches, &known_remotes }; |
211c8968 JS |
356 | int i; |
357 | ||
358 | if (argc != 2) | |
359 | usage_with_options(builtin_remote_usage, options); | |
360 | ||
361 | remote = remote_get(argv[1]); | |
362 | if (!remote) | |
363 | die("No such remote: %s", argv[1]); | |
364 | ||
7ad2458f SP |
365 | known_remotes.to_delete = remote; |
366 | for_each_remote(add_known_remote, &known_remotes); | |
367 | ||
211c8968 JS |
368 | strbuf_addf(&buf, "remote.%s", remote->name); |
369 | if (git_config_rename_section(buf.buf, NULL) < 1) | |
370 | return error("Could not remove config section '%s'", buf.buf); | |
371 | ||
372 | read_branches(); | |
373 | for (i = 0; i < branch_list.nr; i++) { | |
c455c87c | 374 | struct string_list_item *item = branch_list.items + i; |
211c8968 JS |
375 | struct branch_info *info = item->util; |
376 | if (info->remote && !strcmp(info->remote, remote->name)) { | |
377 | const char *keys[] = { "remote", "merge", NULL }, **k; | |
378 | for (k = keys; *k; k++) { | |
379 | strbuf_reset(&buf); | |
380 | strbuf_addf(&buf, "branch.%s.%s", | |
c455c87c | 381 | item->string, *k); |
211c8968 JS |
382 | if (git_config_set(buf.buf, NULL)) { |
383 | strbuf_release(&buf); | |
384 | return -1; | |
385 | } | |
386 | } | |
387 | } | |
388 | } | |
389 | ||
390 | /* | |
391 | * We cannot just pass a function to for_each_ref() which deletes | |
392 | * the branches one by one, since for_each_ref() relies on cached | |
393 | * refs, which are invalidated when deleting a branch. | |
394 | */ | |
7ad2458f | 395 | cb_data.remote = remote; |
211c8968 JS |
396 | i = for_each_ref(add_branch_for_removal, &cb_data); |
397 | strbuf_release(&buf); | |
398 | ||
399 | if (!i) | |
400 | i = remove_branches(&branches); | |
c455c87c | 401 | string_list_clear(&branches, 1); |
211c8968 JS |
402 | |
403 | return i; | |
404 | } | |
405 | ||
79bbc7fb JS |
406 | static void show_list(const char *title, struct string_list *list, |
407 | const char *extra_arg) | |
211c8968 JS |
408 | { |
409 | int i; | |
410 | ||
411 | if (!list->nr) | |
412 | return; | |
413 | ||
79bbc7fb | 414 | printf(title, list->nr > 1 ? "es" : "", extra_arg); |
211c8968 | 415 | printf("\n"); |
20244ea2 JS |
416 | for (i = 0; i < list->nr; i++) |
417 | printf(" %s\n", list->items[i].string); | |
211c8968 JS |
418 | } |
419 | ||
67a7e2d0 OM |
420 | static int get_remote_ref_states(const char *name, |
421 | struct ref_states *states, | |
422 | int query) | |
423 | { | |
424 | struct transport *transport; | |
425 | const struct ref *ref; | |
426 | ||
427 | states->remote = remote_get(name); | |
428 | if (!states->remote) | |
429 | return error("No such remote: %s", name); | |
430 | ||
431 | read_branches(); | |
432 | ||
433 | if (query) { | |
434 | transport = transport_get(NULL, states->remote->url_nr > 0 ? | |
435 | states->remote->url[0] : NULL); | |
436 | ref = transport_get_remote_refs(transport); | |
437 | transport_disconnect(transport); | |
438 | ||
439 | get_ref_states(ref, states); | |
440 | } | |
441 | ||
442 | return 0; | |
443 | } | |
444 | ||
e7d5a97d OM |
445 | static int append_ref_to_tracked_list(const char *refname, |
446 | const unsigned char *sha1, int flags, void *cb_data) | |
447 | { | |
448 | struct ref_states *states = cb_data; | |
449 | struct refspec refspec; | |
450 | ||
451 | memset(&refspec, 0, sizeof(refspec)); | |
452 | refspec.dst = (char *)refname; | |
72972eb3 | 453 | if (!remote_find_tracking(states->remote, &refspec)) |
c455c87c | 454 | string_list_append(abbrev_branch(refspec.src), &states->tracked); |
e7d5a97d OM |
455 | |
456 | return 0; | |
457 | } | |
458 | ||
67a7e2d0 | 459 | static int show(int argc, const char **argv) |
211c8968 | 460 | { |
0ecfcb3b | 461 | int no_query = 0, result = 0; |
211c8968 JS |
462 | struct option options[] = { |
463 | OPT_GROUP("show specific options"), | |
0ecfcb3b | 464 | OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"), |
211c8968 JS |
465 | OPT_END() |
466 | }; | |
467 | struct ref_states states; | |
468 | ||
469 | argc = parse_options(argc, argv, options, builtin_remote_usage, 0); | |
470 | ||
67a7e2d0 OM |
471 | if (argc < 1) |
472 | return show_all(); | |
211c8968 JS |
473 | |
474 | memset(&states, 0, sizeof(states)); | |
475 | for (; argc; argc--, argv++) { | |
0ecfcb3b | 476 | int i; |
211c8968 | 477 | |
67a7e2d0 | 478 | get_remote_ref_states(*argv, &states, !no_query); |
211c8968 JS |
479 | |
480 | printf("* remote %s\n URL: %s\n", *argv, | |
481 | states.remote->url_nr > 0 ? | |
482 | states.remote->url[0] : "(no URL)"); | |
483 | ||
484 | for (i = 0; i < branch_list.nr; i++) { | |
c455c87c | 485 | struct string_list_item *branch = branch_list.items + i; |
211c8968 JS |
486 | struct branch_info *info = branch->util; |
487 | int j; | |
488 | ||
489 | if (!info->merge.nr || strcmp(*argv, info->remote)) | |
490 | continue; | |
491 | printf(" Remote branch%s merged with 'git pull' " | |
492 | "while on branch %s\n ", | |
493 | info->merge.nr > 1 ? "es" : "", | |
c455c87c | 494 | branch->string); |
211c8968 | 495 | for (j = 0; j < info->merge.nr; j++) |
c455c87c | 496 | printf(" %s", info->merge.items[j].string); |
211c8968 JS |
497 | printf("\n"); |
498 | } | |
499 | ||
0ecfcb3b | 500 | if (!no_query) { |
79bbc7fb JS |
501 | show_list(" New remote branch%s (next fetch " |
502 | "will store in remotes/%s)", | |
503 | &states.new, states.remote->name); | |
0ecfcb3b | 504 | show_list(" Stale tracking branch%s (use 'git remote " |
79bbc7fb | 505 | "prune')", &states.stale, ""); |
0ecfcb3b | 506 | } |
211c8968 | 507 | |
e7d5a97d OM |
508 | if (no_query) |
509 | for_each_ref(append_ref_to_tracked_list, &states); | |
79bbc7fb | 510 | show_list(" Tracked remote branch%s", &states.tracked, ""); |
e7d5a97d | 511 | |
211c8968 | 512 | if (states.remote->push_refspec_nr) { |
20244ea2 | 513 | printf(" Local branch%s pushed with 'git push'\n", |
211c8968 JS |
514 | states.remote->push_refspec_nr > 1 ? |
515 | "es" : ""); | |
516 | for (i = 0; i < states.remote->push_refspec_nr; i++) { | |
517 | struct refspec *spec = states.remote->push + i; | |
20244ea2 JS |
518 | printf(" %s%s%s%s\n", |
519 | spec->force ? "+" : "", | |
72972eb3 JH |
520 | abbrev_branch(spec->src), |
521 | spec->dst ? ":" : "", | |
522 | spec->dst ? abbrev_branch(spec->dst) : ""); | |
211c8968 JS |
523 | } |
524 | } | |
67a7e2d0 OM |
525 | |
526 | /* NEEDSWORK: free remote */ | |
c455c87c JS |
527 | string_list_clear(&states.new, 0); |
528 | string_list_clear(&states.stale, 0); | |
529 | string_list_clear(&states.tracked, 0); | |
67a7e2d0 OM |
530 | } |
531 | ||
532 | return result; | |
533 | } | |
534 | ||
535 | static int prune(int argc, const char **argv) | |
536 | { | |
8d767927 | 537 | int dry_run = 0, result = 0; |
67a7e2d0 OM |
538 | struct option options[] = { |
539 | OPT_GROUP("prune specific options"), | |
8d767927 | 540 | OPT__DRY_RUN(&dry_run), |
67a7e2d0 OM |
541 | OPT_END() |
542 | }; | |
543 | struct ref_states states; | |
544 | ||
545 | argc = parse_options(argc, argv, options, builtin_remote_usage, 0); | |
546 | ||
547 | if (argc < 1) | |
548 | usage_with_options(builtin_remote_usage, options); | |
549 | ||
550 | memset(&states, 0, sizeof(states)); | |
551 | for (; argc; argc--, argv++) { | |
552 | int i; | |
553 | ||
8d767927 OM |
554 | get_remote_ref_states(*argv, &states, 1); |
555 | ||
8b7d4e73 JH |
556 | if (states.stale.nr) { |
557 | printf("Pruning %s\n", *argv); | |
8d767927 OM |
558 | printf("URL: %s\n", |
559 | states.remote->url_nr | |
560 | ? states.remote->url[0] | |
561 | : "(no URL)"); | |
8b7d4e73 | 562 | } |
67a7e2d0 OM |
563 | |
564 | for (i = 0; i < states.stale.nr; i++) { | |
565 | const char *refname = states.stale.items[i].util; | |
8d767927 OM |
566 | |
567 | if (!dry_run) | |
eca35a25 | 568 | result |= delete_ref(refname, NULL, 0); |
8d767927 OM |
569 | |
570 | printf(" * [%s] %s\n", dry_run ? "would prune" : "pruned", | |
72972eb3 | 571 | abbrev_ref(refname, "refs/remotes/")); |
67a7e2d0 OM |
572 | } |
573 | ||
211c8968 | 574 | /* NEEDSWORK: free remote */ |
c455c87c JS |
575 | string_list_clear(&states.new, 0); |
576 | string_list_clear(&states.stale, 0); | |
577 | string_list_clear(&states.tracked, 0); | |
211c8968 JS |
578 | } |
579 | ||
580 | return result; | |
581 | } | |
582 | ||
84521ed6 | 583 | static int get_one_remote_for_update(struct remote *remote, void *priv) |
211c8968 | 584 | { |
c455c87c | 585 | struct string_list *list = priv; |
211c8968 | 586 | if (!remote->skip_default_update) |
c455c87c | 587 | string_list_append(xstrdup(remote->name), list); |
84521ed6 JS |
588 | return 0; |
589 | } | |
590 | ||
591 | struct remote_group { | |
592 | const char *name; | |
c455c87c | 593 | struct string_list *list; |
84521ed6 JS |
594 | } remote_group; |
595 | ||
ef90d6d4 | 596 | static int get_remote_group(const char *key, const char *value, void *cb) |
84521ed6 JS |
597 | { |
598 | if (!prefixcmp(key, "remotes.") && | |
599 | !strcmp(key + 8, remote_group.name)) { | |
600 | /* split list by white space */ | |
601 | int space = strcspn(value, " \t\n"); | |
602 | while (*value) { | |
603 | if (space > 1) | |
c455c87c | 604 | string_list_append(xstrndup(value, space), |
84521ed6 JS |
605 | remote_group.list); |
606 | value += space + (value[space] != '\0'); | |
607 | space = strcspn(value, " \t\n"); | |
608 | } | |
609 | } | |
610 | ||
211c8968 JS |
611 | return 0; |
612 | } | |
613 | ||
614 | static int update(int argc, const char **argv) | |
615 | { | |
84521ed6 | 616 | int i, result = 0; |
c455c87c | 617 | struct string_list list = { NULL, 0, 0, 0 }; |
84521ed6 | 618 | static const char *default_argv[] = { NULL, "default", NULL }; |
211c8968 | 619 | |
84521ed6 JS |
620 | if (argc < 2) { |
621 | argc = 2; | |
622 | argv = default_argv; | |
623 | } | |
211c8968 | 624 | |
84521ed6 JS |
625 | remote_group.list = &list; |
626 | for (i = 1; i < argc; i++) { | |
627 | remote_group.name = argv[i]; | |
ef90d6d4 | 628 | result = git_config(get_remote_group, NULL); |
84521ed6 JS |
629 | } |
630 | ||
631 | if (!result && !list.nr && argc == 2 && !strcmp(argv[1], "default")) | |
632 | result = for_each_remote(get_one_remote_for_update, &list); | |
633 | ||
634 | for (i = 0; i < list.nr; i++) | |
c455c87c | 635 | result |= fetch_remote(list.items[i].string); |
84521ed6 JS |
636 | |
637 | /* all names were strdup()ed or strndup()ed */ | |
c455c87c JS |
638 | list.strdup_strings = 1; |
639 | string_list_clear(&list, 0); | |
84521ed6 JS |
640 | |
641 | return result; | |
211c8968 JS |
642 | } |
643 | ||
644 | static int get_one_entry(struct remote *remote, void *priv) | |
645 | { | |
c455c87c | 646 | struct string_list *list = priv; |
211c8968 | 647 | |
7d20e218 MG |
648 | if (remote->url_nr > 0) { |
649 | int i; | |
650 | ||
651 | for (i = 0; i < remote->url_nr; i++) | |
652 | string_list_append(remote->name, list)->util = (void *)remote->url[i]; | |
653 | } else | |
654 | string_list_append(remote->name, list)->util = NULL; | |
211c8968 JS |
655 | |
656 | return 0; | |
657 | } | |
658 | ||
659 | static int show_all(void) | |
660 | { | |
c455c87c | 661 | struct string_list list = { NULL, 0, 0 }; |
211c8968 JS |
662 | int result = for_each_remote(get_one_entry, &list); |
663 | ||
664 | if (!result) { | |
665 | int i; | |
666 | ||
c455c87c | 667 | sort_string_list(&list); |
211c8968 | 668 | for (i = 0; i < list.nr; i++) { |
c455c87c | 669 | struct string_list_item *item = list.items + i; |
7d20e218 MG |
670 | if (verbose) |
671 | printf("%s\t%s\n", item->string, | |
672 | item->util ? (const char *)item->util : ""); | |
673 | else { | |
674 | if (i && !strcmp((item - 1)->string, item->string)) | |
675 | continue; | |
676 | printf("%s\n", item->string); | |
677 | } | |
211c8968 JS |
678 | } |
679 | } | |
680 | return result; | |
681 | } | |
682 | ||
683 | int cmd_remote(int argc, const char **argv, const char *prefix) | |
684 | { | |
685 | struct option options[] = { | |
686 | OPT__VERBOSE(&verbose), | |
687 | OPT_END() | |
688 | }; | |
689 | int result; | |
690 | ||
691 | argc = parse_options(argc, argv, options, builtin_remote_usage, | |
692 | PARSE_OPT_STOP_AT_NON_OPTION); | |
693 | ||
694 | if (argc < 1) | |
695 | result = show_all(); | |
696 | else if (!strcmp(argv[0], "add")) | |
697 | result = add(argc, argv); | |
698 | else if (!strcmp(argv[0], "rm")) | |
699 | result = rm(argc, argv); | |
700 | else if (!strcmp(argv[0], "show")) | |
67a7e2d0 | 701 | result = show(argc, argv); |
211c8968 | 702 | else if (!strcmp(argv[0], "prune")) |
67a7e2d0 | 703 | result = prune(argc, argv); |
211c8968 JS |
704 | else if (!strcmp(argv[0], "update")) |
705 | result = update(argc, argv); | |
706 | else { | |
707 | error("Unknown subcommand: %s", argv[0]); | |
708 | usage_with_options(builtin_remote_usage, options); | |
709 | } | |
710 | ||
711 | return result ? 1 : 0; | |
712 | } |