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