@matheusbsbs / matheus-bagliano / commits / a8dfd5eac4
Fork 0

Make builtin-branch.c use parse_options.

Signed-off-by: Pierre Habouzit <madcoder@debian.org> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Pierre Habouzit committed Oct 7, 2007 at 18:26 UTC a8dfd5eac40143030d47da6fd87906990d7bf28c
1 file changed +52 -95
builtin-branch.c
+52 -95
@@ -11,9 +11,15 @@
11 #include "commit.h"
12 #include "builtin.h"
13 #include "remote.h"
14 -
15 -static const char builtin_branch_usage[] =
16 - "git-branch [-r] (-d | -D) <branchname> | [--track | --no-track] [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [--color | --no-color] [-r | -a] [-v [--abbrev=<length> | --no-abbrev]]";
14 +#include "parse-options.h"
15 +
16 +static const char * const builtin_branch_usage[] = {
17 + "git-branch [options] [-r | -a]",
18 + "git-branch [options] [-l] [-f] <branchname> [<start-point>]",
19 + "git-branch [options] [-r] (-d | -D) <branchname>",
20 + "git-branch [options] (-m | -M) [<oldbranch>] <newbranch>",
21 + NULL
22 +};
23
24 #define REF_UNKNOWN_TYPE 0x00
25 #define REF_LOCAL_BRANCH 0x01
@@ -505,93 +511,45 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
511 int rename = 0, force_rename = 0;
512 int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
513 int reflog = 0, track;
508 - int kinds = REF_LOCAL_BRANCH;
509 - int i;
514 + int kinds = REF_LOCAL_BRANCH, kind_remote = 0, kind_any = 0;
515 +
516 + struct option options[] = {
517 + OPT_GROUP("Generic options"),
518 + OPT__VERBOSE(&verbose),
519 + OPT_BOOLEAN( 0 , "track", &track, "set up tracking mode (see git-pull(1))"),
520 + OPT_BOOLEAN( 0 , "color", &branch_use_color, "use colored output"),
521 + OPT_BOOLEAN('r', NULL, &kind_remote, "act on remote-tracking branches"),
522 + OPT__ABBREV(&abbrev),
523 +
524 + OPT_GROUP("Specific git-branch actions:"),
525 + OPT_BOOLEAN('a', NULL, &kind_any, "list both remote-tracking and local branches"),
526 + OPT_BOOLEAN('d', NULL, &delete, "delete fully merged branch"),
527 + OPT_BOOLEAN('D', NULL, &force_delete, "delete branch (even if not merged)"),
528 + OPT_BOOLEAN('l', NULL, &reflog, "create the branch's reflog"),
529 + OPT_BOOLEAN('f', NULL, &force_create, "force creation (when already exists)"),
530 + OPT_BOOLEAN('m', NULL, &rename, "move/rename a branch and its reflog"),
531 + OPT_BOOLEAN('M', NULL, &force_rename, "move/rename a branch, even if target exists"),
532 + OPT_END(),
533 + };
534
535 git_config(git_branch_config);
536 track = branch_track;
513 -
514 - for (i = 1; i < argc; i++) {
515 - const char *arg = argv[i];
516 -
517 - if (arg[0] != '-')
518 - break;
519 - if (!strcmp(arg, "--")) {
520 - i++;
521 - break;
522 - }
523 - if (!strcmp(arg, "--track")) {
524 - track = 1;
525 - continue;
526 - }
527 - if (!strcmp(arg, "--no-track")) {
528 - track = 0;
529 - continue;
530 - }
531 - if (!strcmp(arg, "-d")) {
532 - delete = 1;
533 - continue;
534 - }
535 - if (!strcmp(arg, "-D")) {
536 - delete = 1;
537 - force_delete = 1;
538 - continue;
539 - }
540 - if (!strcmp(arg, "-f")) {
541 - force_create = 1;
542 - continue;
543 - }
544 - if (!strcmp(arg, "-m")) {
545 - rename = 1;
546 - continue;
547 - }
548 - if (!strcmp(arg, "-M")) {
549 - rename = 1;
550 - force_rename = 1;
551 - continue;
552 - }
553 - if (!strcmp(arg, "-r")) {
554 - kinds = REF_REMOTE_BRANCH;
555 - continue;
556 - }
557 - if (!strcmp(arg, "-a")) {
558 - kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
559 - continue;
560 - }
561 - if (!strcmp(arg, "-l")) {
562 - reflog = 1;
563 - continue;
564 - }
565 - if (!prefixcmp(arg, "--no-abbrev")) {
566 - abbrev = 0;
567 - continue;
568 - }
569 - if (!prefixcmp(arg, "--abbrev=")) {
570 - abbrev = strtoul(arg + 9, NULL, 10);
571 - if (abbrev < MINIMUM_ABBREV)
572 - abbrev = MINIMUM_ABBREV;
573 - else if (abbrev > 40)
574 - abbrev = 40;
575 - continue;
576 - }
577 - if (!strcmp(arg, "-v")) {
578 - verbose = 1;
579 - continue;
580 - }
581 - if (!strcmp(arg, "--color")) {
582 - branch_use_color = 1;
583 - continue;
584 - }
585 - if (!strcmp(arg, "--no-color")) {
586 - branch_use_color = 0;
587 - continue;
588 - }
589 - usage(builtin_branch_usage);
590 - }
537 + argc = parse_options(argc, argv, options, builtin_branch_usage, 0);
538 +
539 + delete |= force_delete;
540 + rename |= force_rename;
541 + if (kind_remote)
542 + kinds = REF_REMOTE_BRANCH;
543 + if (kind_any)
544 + kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
545 + if (abbrev && abbrev < MINIMUM_ABBREV)
546 + abbrev = MINIMUM_ABBREV;
547 + else if (abbrev > 40)
548 + abbrev = 40;
549
550 if ((delete && rename) || (delete && force_create) ||
551 (rename && force_create))
594 - usage(builtin_branch_usage);
552 + usage_with_options(builtin_branch_usage, options);
553
554 head = resolve_ref("HEAD", head_sha1, 0, NULL);
555 if (!head)
@@ -599,26 +557,25 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
557 head = xstrdup(head);
558 if (!strcmp(head, "HEAD")) {
559 detached = 1;
602 - }
603 - else {
560 + } else {
561 if (prefixcmp(head, "refs/heads/"))
562 die("HEAD not found below refs/heads!");
563 head += 11;
564 }
565
566 if (delete)
610 - return delete_branches(argc - i, argv + i, force_delete, kinds);
611 - else if (i == argc)
567 + return delete_branches(argc, argv, force_delete, kinds);
568 + else if (argc == 0)
569 print_ref_list(kinds, detached, verbose, abbrev);
613 - else if (rename && (i == argc - 1))
614 - rename_branch(head, argv[i], force_rename);
615 - else if (rename && (i == argc - 2))
616 - rename_branch(argv[i], argv[i + 1], force_rename);
617 - else if (i == argc - 1 || i == argc - 2)
618 - create_branch(argv[i], (i == argc - 2) ? argv[i+1] : head,
570 + else if (rename && (argc == 1))
571 + rename_branch(head, argv[0], force_rename);
572 + else if (rename && (argc == 2))
573 + rename_branch(argv[0], argv[1], force_rename);
574 + else if (argc <= 2)
575 + create_branch(argv[0], (argc == 2) ? argv[1] : head,
576 force_create, reflog, track);
577 else
621 - usage(builtin_branch_usage);
578 + usage_with_options(builtin_branch_usage, options);
579
580 return 0;
581 }