@matheusbsbs / matheus-bagliano / commits / 57343652a5
Fork 0

show-branch: migrate to parse-options API

Note that "-g" no longer uses an equals '=' sign for its optional arguments, but "--reflog" still does. This is normal behavior for parse options, as arguments to "-g" are put immediately after the option with no space. For example git show-branch -g=4 is now git show-branch -g4 Signed-off-by: Stephen Boyd <bebarino@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Stephen Boyd committed May 21, 2009 at 00:33 UTC 57343652a55bdc3e7f3cf8c6def94dac482c72e5
1 file changed +63 -62
builtin-show-branch.c
+63 -62
@@ -3,11 +3,13 @@
3 #include "refs.h"
4 #include "builtin.h"
5 #include "color.h"
6 +#include "parse-options.h"
7
7 -static const char show_branch_usage[] =
8 -"git show-branch [--sparse] [--current] [--all] [--remotes] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...] | --reflog[=n[,b]] <branch>";
9 -static const char show_branch_usage_reflog[] =
10 -"--reflog is incompatible with --all, --remotes, --independent or --merge-base";
8 +static const char* show_branch_usage[] = {
9 + "git show-branch [--sparse] [--current] [--all] [--remotes] [--topo-order] [--more=count | --list | --independent | --merge-base] [--topics] [--color] [<refs>...]",
10 + "--reflog[=n[,b]] [--list] [--color] <branch>",
11 + NULL
12 +};
13
14 static int showbranch_use_color = -1;
15 static char column_colors[][COLOR_MAXLEN] = {
@@ -601,18 +603,25 @@ static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
603 return 0;
604 }
605
604 -static void parse_reflog_param(const char *arg, int *cnt, const char **base)
606 +static int reflog = 0;
607 +
608 +static int parse_reflog_param(const struct option *opt, const char *arg,
609 + int unset)
610 {
611 char *ep;
607 - *cnt = strtoul(arg, &ep, 10);
612 + const char **base = (const char **)opt->value;
613 + if (!arg)
614 + arg = "";
615 + reflog = strtoul(arg, &ep, 10);
616 if (*ep == ',')
617 *base = ep + 1;
618 else if (*ep)
611 - die("unrecognized reflog param '%s'", arg);
619 + return error("unrecognized reflog param '%s'", arg);
620 else
621 *base = NULL;
614 - if (*cnt <= 0)
615 - *cnt = DEFAULT_REFLOG;
622 + if (reflog <= 0)
623 + reflog = DEFAULT_REFLOG;
624 + return 0;
625 }
626
627 int cmd_show_branch(int ac, const char **av, const char *prefix)
@@ -638,8 +647,44 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
647 int head_at = -1;
648 int topics = 0;
649 int dense = 1;
641 - int reflog = 0;
650 const char *reflog_base = NULL;
651 + struct option builtin_show_branch_options[] = {
652 + OPT_BOOLEAN('a', "all", &all_heads,
653 + "show remote-tracking and local branches"),
654 + OPT_BOOLEAN('r', "remotes", &all_remotes,
655 + "show remote-tracking branches"),
656 + OPT_BOOLEAN(0, "color", &showbranch_use_color,
657 + "color '*!+-' corresponding to the branch"),
658 + { OPTION_INTEGER, 0, "more", &extra, "n",
659 + "show <n> more commits after the common ancestor",
660 + PARSE_OPT_OPTARG | PARSE_OPT_LASTARG_DEFAULT,
661 + NULL, (intptr_t)1 },
662 + OPT_SET_INT(0, "list", &extra, "synonym to more=-1", -1),
663 + OPT_BOOLEAN(0, "no-name", &no_name, "suppress naming strings"),
664 + OPT_BOOLEAN(0, "current", &with_current_branch,
665 + "include the current branch"),
666 + OPT_BOOLEAN(0, "sha1-name", &sha1_name,
667 + "name commits with their object names"),
668 + OPT_BOOLEAN(0, "merge-base", &merge_base,
669 + "act like git merge-base -a"),
670 + OPT_BOOLEAN(0, "independent", &independent,
671 + "show refs unreachable from any other ref"),
672 + OPT_BOOLEAN(0, "topo-order", &lifo,
673 + "show commits in topological order"),
674 + OPT_BOOLEAN(0, "topics", &topics,
675 + "show only commits not on the first branch"),
676 + OPT_SET_INT(0, "sparse", &dense,
677 + "show merges reachable from only one tip", 0),
678 + OPT_SET_INT(0, "date-order", &lifo,
679 + "show commits where no parent comes before its "
680 + "children", 0),
681 + { OPTION_CALLBACK, 'g', "reflog", &reflog_base, "<n>[,<base>]",
682 + "show <n> most recent ref-log entries starting at "
683 + "base",
684 + PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
685 + parse_reflog_param },
686 + OPT_END()
687 + };
688
689 git_config(git_show_branch_config, NULL);
690
@@ -652,63 +697,18 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
697 av = default_arg - 1; /* ick; we would not address av[0] */
698 }
699
655 - while (1 < ac && av[1][0] == '-') {
656 - const char *arg = av[1];
657 - if (!strcmp(arg, "--")) {
658 - ac--; av++;
659 - break;
660 - }
661 - else if (!strcmp(arg, "--all") || !strcmp(arg, "-a"))
662 - all_heads = all_remotes = 1;
663 - else if (!strcmp(arg, "--remotes") || !strcmp(arg, "-r"))
664 - all_remotes = 1;
665 - else if (!strcmp(arg, "--more"))
666 - extra = 1;
667 - else if (!strcmp(arg, "--list"))
668 - extra = -1;
669 - else if (!strcmp(arg, "--no-name"))
670 - no_name = 1;
671 - else if (!strcmp(arg, "--current"))
672 - with_current_branch = 1;
673 - else if (!strcmp(arg, "--sha1-name"))
674 - sha1_name = 1;
675 - else if (!prefixcmp(arg, "--more="))
676 - extra = atoi(arg + 7);
677 - else if (!strcmp(arg, "--merge-base"))
678 - merge_base = 1;
679 - else if (!strcmp(arg, "--independent"))
680 - independent = 1;
681 - else if (!strcmp(arg, "--topo-order"))
682 - lifo = 1;
683 - else if (!strcmp(arg, "--topics"))
684 - topics = 1;
685 - else if (!strcmp(arg, "--sparse"))
686 - dense = 0;
687 - else if (!strcmp(arg, "--date-order"))
688 - lifo = 0;
689 - else if (!strcmp(arg, "--reflog") || !strcmp(arg, "-g")) {
690 - reflog = DEFAULT_REFLOG;
691 - }
692 - else if (!prefixcmp(arg, "--reflog="))
693 - parse_reflog_param(arg + 9, &reflog, &reflog_base);
694 - else if (!prefixcmp(arg, "-g="))
695 - parse_reflog_param(arg + 3, &reflog, &reflog_base);
696 - else if (!strcmp(arg, "--color"))
697 - showbranch_use_color = 1;
698 - else if (!strcmp(arg, "--no-color"))
699 - showbranch_use_color = 0;
700 - else
701 - usage(show_branch_usage);
702 - ac--; av++;
703 - }
704 - ac--; av++;
700 + ac = parse_options(ac, av, builtin_show_branch_options,
701 + show_branch_usage, PARSE_OPT_STOP_AT_NON_OPTION);
702 + if (all_heads)
703 + all_remotes = 1;
704
705 if (extra || reflog) {
706 /* "listing" mode is incompatible with
707 * independent nor merge-base modes.
708 */
709 if (independent || merge_base)
711 - usage(show_branch_usage);
710 + usage_with_options(show_branch_usage,
711 + builtin_show_branch_options);
712 if (reflog && ((0 < extra) || all_heads || all_remotes))
713 /*
714 * Asking for --more in reflog mode does not
@@ -716,7 +716,8 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
716 *
717 * Also --all and --remotes do not make sense either.
718 */
719 - usage(show_branch_usage_reflog);
719 + die("--reflog is incompatible with --all, --remotes, "
720 + "--independent or --merge-base");
721 }
722
723 /* If nothing is specified, show all branches by default */