{"thread":{"id":"54655","subject":"[PATCH] help.c: add advice.correctTypos option","startedAt":"2020-11-16T19:01:13Z","lastAt":"2020-11-16T23:41:53Z","messageCount":2,"participants":["Drew DeVault","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"410034","messageId":"20201116185521.2276-1-sir@cmpwn.com","threadId":"54655","inReplyTo":null,"subject":"[PATCH] help.c: add advice.correctTypos option","fromName":"Drew DeVault","fromEmail":"sir@cmpwn.com","sentAt":"2020-11-16T18:55:21Z","receivedAt":"2020-11-16T19:01:13Z","isPatch":true,"sender":{"key":"sir@cmpwn.com","avatar":"https://avatars.githubusercontent.com/u/1310872?v=4"},"body":"This allows users to disable guessing the commands or options that they\nmeant to use.\n---\nQuestions:\n\n- Is advice.* the right namespace?\n- How should this interact with help.autocorrect?\n\n Documentation/config/advice.txt |  2 ++\n help.c                          | 19 ++++++++++++++++---\n 2 files changed, 18 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config/advice.txt b/Documentation/config/advice.txt\nindex acbd0c09aa..135d1345af 100644\n--- a/Documentation/config/advice.txt\n+++ b/Documentation/config/advice.txt\n@@ -119,4 +119,6 @@ advice.*::\n \taddEmptyPathspec::\n \t\tAdvice shown if a user runs the add command without providing\n \t\tthe pathspec parameter.\n+\tcorrectTypos::\n+\t\tDetect typos and suggest corrections.\n --\ndiff --git a/help.c b/help.c\nindex 919cbb9206..c35c4c99da 100644\n--- a/help.c\n+++ b/help.c\n@@ -515,10 +515,16 @@ static const char bad_interpreter_advice[] =\n \n const char *help_unknown_cmd(const char *cmd)\n {\n-\tint i, n, best_similarity = 0;\n+\tint i, n, best_similarity = 0, enable = 1;\n \tstruct cmdnames main_cmds, other_cmds;\n \tstruct cmdname_help *common_cmds;\n \n+\tgit_config_get_bool(\"advice.correctTypos\", &enable);\n+\tif (!enable) {\n+\t\tfprintf_ln(stderr, _(\"git: '%s' is not a git command. See 'git --help'.\"), cmd);\n+\t\texit(1);\n+\t}\n+\n \tmemset(&main_cmds, 0, sizeof(main_cmds));\n \tmemset(&other_cmds, 0, sizeof(other_cmds));\n \tmemset(&aliases, 0, sizeof(aliases));\n@@ -705,11 +711,18 @@ static struct string_list guess_refs(const char *ref)\n NORETURN void help_unknown_ref(const char *ref, const char *cmd,\n \t\t\t       const char *error)\n {\n-\tint i;\n-\tstruct string_list suggested_refs = guess_refs(ref);\n+\tint i, enable = 1;\n+\tstruct string_list suggested_refs;\n \n \tfprintf_ln(stderr, _(\"%s: %s - %s\"), cmd, ref, error);\n \n+\tgit_config_get_bool(\"advice.correctTypos\", &enable);\n+\tif (!enable) {\n+\t\texit(1);\n+\t}\n+\n+\tsuggested_refs = guess_refs(ref);\n+\n \tif (suggested_refs.nr > 0) {\n \t\tfprintf_ln(stderr,\n \t\t\t   Q_(\"\\nDid you mean this?\",\n-- \n2.29.2\n\n"},{"id":"410047","messageId":"xmqqlff07ttx.fsf@gitster.c.googlers.com","threadId":"54655","inReplyTo":"20201116185521.2276-1-sir@cmpwn.com","subject":"Re: [PATCH] help.c: add advice.correctTypos option","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-11-16T23:41:46Z","receivedAt":"2020-11-16T23:41:53Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Drew DeVault <sir@cmpwn.com> writes:\n\n> This allows users to disable guessing the commands or options that they\n> meant to use.\n> ---\n> Questions:\n>\n> - Is advice.* the right namespace?\n> - How should this interact with help.autocorrect?\n\nIf you are declining help.autocorrect altogether, do you need to\nstill invent a new and separate configuration variable?  Isn't a new\nvalue (e.g. 'never') given to help.autocorrect sufficient?\n\nSomething along this line (not even compile tested though)?\n\n help.c | 22 +++++++++++++++++++---\n 1 file changed, 19 insertions(+), 3 deletions(-)\n\ndiff --git c/help.c w/help.c\nindex 4fb93d5560..06f86152a6 100644\n--- c/help.c\n+++ w/help.c\n@@ -469,6 +469,8 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)\n \treturn 0;\n }\n \n+#define AUTOCORRECT_NEVER (-2)\n+#define AUTOCORRECT_IMMEDIATELY (-1)\n static int autocorrect;\n static struct cmdnames aliases;\n \n@@ -476,8 +478,19 @@ static int git_unknown_cmd_config(const char *var, const char *value, void *cb)\n {\n \tconst char *p;\n \n-\tif (!strcmp(var, \"help.autocorrect\"))\n-\t\tautocorrect = git_config_int(var,value);\n+\tif (!strcmp(var, \"help.autocorrect\")) {\n+\t\tif (!value)\n+\t\t\treturn config_error_nonbool(var);\n+\t\tif (!strcmp(value, \"never\"))\n+\t\t\tautocorrect = AUTOCORRECT_NEVER;\n+\t\telse {\n+\t\t\tint v = git_config_int(var,value);\n+\t\t\tif (v < 0)\n+\t\t\t\tautocorrect = AUTOCORRECT_IMMEDIATELY;\n+\t\t\telse\n+\t\t\t\tautocorrect = v;\n+\t\t}\n+\t}\n \t/* Also use aliases for command lookup */\n \tif (skip_prefix(var, \"alias.\", &p))\n \t\tadd_cmdname(&aliases, p, strlen(p));\n@@ -519,6 +532,9 @@ const char *help_unknown_cmd(const char *cmd)\n \tstruct cmdnames main_cmds, other_cmds;\n \tstruct cmdname_help *common_cmds;\n \n+\tif (autocorrect == AUTOCORRECT_NEVER)\n+\t\texit(1);\n+\n \tmemset(&main_cmds, 0, sizeof(main_cmds));\n \tmemset(&other_cmds, 0, sizeof(other_cmds));\n \tmemset(&aliases, 0, sizeof(aliases));\n@@ -594,7 +610,7 @@ const char *help_unknown_cmd(const char *cmd)\n \t\t\t   _(\"WARNING: You called a Git command named '%s', \"\n \t\t\t     \"which does not exist.\"),\n \t\t\t   cmd);\n-\t\tif (autocorrect < 0)\n+\t\tif (autocorrect == AUTOCORRECT_IMMEDIATELY)\n \t\t\tfprintf_ln(stderr,\n \t\t\t\t   _(\"Continuing under the assumption that \"\n \t\t\t\t     \"you meant '%s'.\"),\n"}]}