|
22 | 22 | #include "repository.h" |
23 | 23 | #include "alias.h" |
24 | 24 | #include "utf8.h" |
| 25 | +#include "autocorrect.h" |
25 | 26 |
|
26 | 27 | #ifndef NO_CURL |
27 | 28 | #include "git-curl-compat.h" /* For LIBCURL_VERSION only */ |
@@ -536,70 +537,23 @@ int is_in_cmdlist(struct cmdnames *c, const char *s) |
536 | 537 | return 0; |
537 | 538 | } |
538 | 539 |
|
539 | | -struct help_unknown_cmd_config { |
540 | | - int autocorrect; |
541 | | - struct cmdnames aliases; |
542 | | -}; |
543 | | - |
544 | | -#define AUTOCORRECT_SHOW (-4) |
545 | | -#define AUTOCORRECT_PROMPT (-3) |
546 | | -#define AUTOCORRECT_NEVER (-2) |
547 | | -#define AUTOCORRECT_IMMEDIATELY (-1) |
548 | | - |
549 | | -static int parse_autocorrect(const char *value) |
| 540 | +static int resolve_aliases(const char *var, const char *value UNUSED, |
| 541 | + const struct config_context *ctx UNUSED, void *data) |
550 | 542 | { |
551 | | - switch (git_parse_maybe_bool_text(value)) { |
552 | | - case 1: |
553 | | - return AUTOCORRECT_IMMEDIATELY; |
554 | | - case 0: |
555 | | - return AUTOCORRECT_SHOW; |
556 | | - default: /* other random text */ |
557 | | - break; |
558 | | - } |
559 | | - |
560 | | - if (!strcmp(value, "prompt")) |
561 | | - return AUTOCORRECT_PROMPT; |
562 | | - if (!strcmp(value, "never")) |
563 | | - return AUTOCORRECT_NEVER; |
564 | | - if (!strcmp(value, "immediate")) |
565 | | - return AUTOCORRECT_IMMEDIATELY; |
566 | | - if (!strcmp(value, "show")) |
567 | | - return AUTOCORRECT_SHOW; |
568 | | - |
569 | | - return 0; |
570 | | -} |
571 | | - |
572 | | -static int git_unknown_cmd_config(const char *var, const char *value, |
573 | | - const struct config_context *ctx, |
574 | | - void *cb) |
575 | | -{ |
576 | | - struct help_unknown_cmd_config *cfg = cb; |
| 543 | + struct cmdnames *aliases = data; |
577 | 544 | const char *subsection, *key; |
578 | 545 | size_t subsection_len; |
579 | 546 |
|
580 | | - if (!strcmp(var, "help.autocorrect")) { |
581 | | - int v = parse_autocorrect(value); |
582 | | - |
583 | | - if (!v) { |
584 | | - v = git_config_int(var, value, ctx->kvi); |
585 | | - if (v < 0 || v == 1) |
586 | | - v = AUTOCORRECT_IMMEDIATELY; |
587 | | - } |
588 | | - |
589 | | - cfg->autocorrect = v; |
590 | | - } |
591 | | - |
592 | | - /* Also use aliases for command lookup */ |
593 | 547 | if (!parse_config_key(var, "alias", &subsection, &subsection_len, |
594 | 548 | &key)) { |
595 | 549 | if (subsection) { |
596 | 550 | /* [alias "name"] command = value */ |
597 | 551 | if (!strcmp(key, "command")) |
598 | | - add_cmdname(&cfg->aliases, subsection, |
| 552 | + add_cmdname(aliases, subsection, |
599 | 553 | subsection_len); |
600 | 554 | } else { |
601 | 555 | /* alias.name = value */ |
602 | | - add_cmdname(&cfg->aliases, key, strlen(key)); |
| 556 | + add_cmdname(aliases, key, strlen(key)); |
603 | 557 | } |
604 | 558 | } |
605 | 559 |
|
@@ -636,28 +590,26 @@ static const char bad_interpreter_advice[] = |
636 | 590 |
|
637 | 591 | char *help_unknown_cmd(const char *cmd) |
638 | 592 | { |
639 | | - struct help_unknown_cmd_config cfg = { 0 }; |
| 593 | + struct cmdnames aliases = { 0 }; |
| 594 | + struct autocorrect autocorrect = { 0 }; |
640 | 595 | int i, n, best_similarity = 0; |
641 | 596 | struct cmdnames main_cmds = { 0 }; |
642 | 597 | struct cmdnames other_cmds = { 0 }; |
643 | 598 | struct cmdname_help *common_cmds; |
644 | 599 |
|
645 | | - read_early_config(the_repository, git_unknown_cmd_config, &cfg); |
646 | | - |
647 | | - /* |
648 | | - * Disable autocorrection prompt in a non-interactive session |
649 | | - */ |
650 | | - if ((cfg.autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2))) |
651 | | - cfg.autocorrect = AUTOCORRECT_NEVER; |
| 600 | + autocorrect_resolve(&autocorrect); |
652 | 601 |
|
653 | | - if (cfg.autocorrect == AUTOCORRECT_NEVER) { |
| 602 | + if (autocorrect.mode == AUTOCORRECT_NEVER) { |
654 | 603 | fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd); |
655 | 604 | exit(1); |
656 | 605 | } |
657 | 606 |
|
658 | 607 | load_command_list("git-", &main_cmds, &other_cmds); |
659 | 608 |
|
660 | | - add_cmd_list(&main_cmds, &cfg.aliases); |
| 609 | + /* Also use aliases for command lookup */ |
| 610 | + read_early_config(the_repository, resolve_aliases, &aliases); |
| 611 | + |
| 612 | + add_cmd_list(&main_cmds, &aliases); |
661 | 613 | add_cmd_list(&main_cmds, &other_cmds); |
662 | 614 | QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare); |
663 | 615 | uniq(&main_cmds); |
@@ -716,37 +668,18 @@ char *help_unknown_cmd(const char *cmd) |
716 | 668 | n++) |
717 | 669 | ; /* still counting */ |
718 | 670 | } |
719 | | - if (cfg.autocorrect && cfg.autocorrect != AUTOCORRECT_SHOW && n == 1 && |
| 671 | + |
| 672 | + if (autocorrect.mode != AUTOCORRECT_HINT && n == 1 && |
720 | 673 | SIMILAR_ENOUGH(best_similarity)) { |
721 | 674 | char *assumed = xstrdup(main_cmds.names[0]->name); |
722 | 675 |
|
723 | 676 | fprintf_ln(stderr, |
724 | | - _("WARNING: You called a Git command named '%s', " |
725 | | - "which does not exist."), |
| 677 | + _("WARNING: You called a Git command named '%s', which does not exist."), |
726 | 678 | cmd); |
727 | | - if (cfg.autocorrect == AUTOCORRECT_IMMEDIATELY) |
728 | | - fprintf_ln(stderr, |
729 | | - _("Continuing under the assumption that " |
730 | | - "you meant '%s'."), |
731 | | - assumed); |
732 | | - else if (cfg.autocorrect == AUTOCORRECT_PROMPT) { |
733 | | - char *answer; |
734 | | - struct strbuf msg = STRBUF_INIT; |
735 | | - strbuf_addf(&msg, _("Run '%s' instead [y/N]? "), assumed); |
736 | | - answer = git_prompt(msg.buf, PROMPT_ECHO); |
737 | | - strbuf_release(&msg); |
738 | | - if (!(starts_with(answer, "y") || |
739 | | - starts_with(answer, "Y"))) |
740 | | - exit(1); |
741 | | - } else { |
742 | | - fprintf_ln(stderr, |
743 | | - _("Continuing in %0.1f seconds, " |
744 | | - "assuming that you meant '%s'."), |
745 | | - (float)cfg.autocorrect/10.0, assumed); |
746 | | - sleep_millisec(cfg.autocorrect * 100); |
747 | | - } |
748 | 679 |
|
749 | | - cmdnames_release(&cfg.aliases); |
| 680 | + autocorrect_confirm(&autocorrect, assumed); |
| 681 | + |
| 682 | + cmdnames_release(&aliases); |
750 | 683 | cmdnames_release(&main_cmds); |
751 | 684 | cmdnames_release(&other_cmds); |
752 | 685 | return assumed; |
|
0 commit comments