@matheusbsbs / matheus-bagliano / commits / 67a7e2d071
Fork 0

builtin-remote: split show_or_prune() in two separate functions

This allow us to add different features to each of them and keep the code simple at the same time. Also create a get_remote_ref_states() to avoid duplicated code. Signed-off-by: Olivier Marin <dkr@freesurf.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Olivier Marin committed Jun 10, 2008 at 16:51 UTC 67a7e2d07162dc470f7e3ae1889aad146e5b7917
1 file changed +67 -34
builtin-remote.c
+67 -34
@@ -419,7 +419,32 @@ static void show_list(const char *title, struct path_list *list)
419 printf("\n");
420 }
421
422 -static int show_or_prune(int argc, const char **argv, int prune)
422 +static int get_remote_ref_states(const char *name,
423 + struct ref_states *states,
424 + int query)
425 +{
426 + struct transport *transport;
427 + const struct ref *ref;
428 +
429 + states->remote = remote_get(name);
430 + if (!states->remote)
431 + return error("No such remote: %s", name);
432 +
433 + read_branches();
434 +
435 + if (query) {
436 + transport = transport_get(NULL, states->remote->url_nr > 0 ?
437 + states->remote->url[0] : NULL);
438 + ref = transport_get_remote_refs(transport);
439 + transport_disconnect(transport);
440 +
441 + get_ref_states(ref, states);
442 + }
443 +
444 + return 0;
445 +}
446 +
447 +static int show(int argc, const char **argv)
448 {
449 int no_query = 0, result = 0;
450 struct option options[] = {
@@ -431,42 +456,15 @@ static int show_or_prune(int argc, const char **argv, int prune)
456
457 argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
458
434 - if (argc < 1) {
435 - if (!prune)
436 - return show_all();
437 - usage_with_options(builtin_remote_usage, options);
438 - }
459 + if (argc < 1)
460 + return show_all();
461
462 memset(&states, 0, sizeof(states));
463 for (; argc; argc--, argv++) {
442 - struct transport *transport;
443 - const struct ref *ref;
464 struct strbuf buf;
465 int i;
466
447 - states.remote = remote_get(*argv);
448 - if (!states.remote)
449 - return error("No such remote: %s", *argv);
450 -
451 - read_branches();
452 -
453 - if (!no_query) {
454 - transport = transport_get(NULL,
455 - states.remote->url_nr > 0 ?
456 - states.remote->url[0] : NULL);
457 - ref = transport_get_remote_refs(transport);
458 - transport_disconnect(transport);
459 -
460 - get_ref_states(ref, &states);
461 - }
462 -
463 - if (prune) {
464 - for (i = 0; i < states.stale.nr; i++) {
465 - const char *refname = states.stale.items[i].util;
466 - result |= delete_ref(refname, NULL);
467 - }
468 - goto cleanup_states;
469 - }
467 + get_remote_ref_states(*argv, &states, !no_query);
468
469 printf("* remote %s\n URL: %s\n", *argv,
470 states.remote->url_nr > 0 ?
@@ -513,7 +511,42 @@ static int show_or_prune(int argc, const char **argv, int prune)
511 }
512 printf("\n");
513 }
516 -cleanup_states:
514 +
515 + /* NEEDSWORK: free remote */
516 + path_list_clear(&states.new, 0);
517 + path_list_clear(&states.stale, 0);
518 + path_list_clear(&states.tracked, 0);
519 + }
520 +
521 + return result;
522 +}
523 +
524 +static int prune(int argc, const char **argv)
525 +{
526 + int no_query = 0, result = 0;
527 + struct option options[] = {
528 + OPT_GROUP("prune specific options"),
529 + OPT_BOOLEAN('n', NULL, &no_query, "do not query remotes"),
530 + OPT_END()
531 + };
532 + struct ref_states states;
533 +
534 + argc = parse_options(argc, argv, options, builtin_remote_usage, 0);
535 +
536 + if (argc < 1)
537 + usage_with_options(builtin_remote_usage, options);
538 +
539 + memset(&states, 0, sizeof(states));
540 + for (; argc; argc--, argv++) {
541 + int i;
542 +
543 + get_remote_ref_states(*argv, &states, !no_query);
544 +
545 + for (i = 0; i < states.stale.nr; i++) {
546 + const char *refname = states.stale.items[i].util;
547 + result |= delete_ref(refname, NULL);
548 + }
549 +
550 /* NEEDSWORK: free remote */
551 path_list_clear(&states.new, 0);
552 path_list_clear(&states.stale, 0);
@@ -634,9 +667,9 @@ int cmd_remote(int argc, const char **argv, const char *prefix)
667 else if (!strcmp(argv[0], "rm"))
668 result = rm(argc, argv);
669 else if (!strcmp(argv[0], "show"))
637 - result = show_or_prune(argc, argv, 0);
670 + result = show(argc, argv);
671 else if (!strcmp(argv[0], "prune"))
639 - result = show_or_prune(argc, argv, 1);
672 + result = prune(argc, argv);
673 else if (!strcmp(argv[0], "update"))
674 result = update(argc, argv);
675 else {