{"thread":{"id":"56252","subject":"[PATCH] help.c: help.autocorrect=prompt waits for user action","startedAt":"2021-08-11T00:15:58Z","lastAt":"2021-09-08T00:18:19Z","messageCount":12,"participants":["Azeem Bande-Ali via GitGitGadget","Bagas Sanjaya","Junio C Hamano","Azeem Bande-Ali","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"432429","messageId":"pull.1012.git.1628640954160.gitgitgadget@gmail.com","threadId":"56252","inReplyTo":null,"subject":"[PATCH] help.c: help.autocorrect=prompt waits for user action","fromName":"Azeem Bande-Ali via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-08-11T00:15:53Z","receivedAt":"2021-08-11T00:15:58Z","isPatch":true,"sender":{"key":"me@azeemba.com","avatar":"https://avatars.githubusercontent.com/u/2160795?v=4"},"body":"From: Azeem Bande-Ali <me@azeemba.com>\n\nIf help.autocorrect is set to 'prompt', the user is prompted\nbefore the suggested action is executed.\n\nBased on original patch by David Barr\nhttps://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/\n\nSigned-off-by: Azeem Bande-Ali <me@azeemba.com>\n---\n    New config for help.autocorrect to prompt user before action\n    \n    Currently the prompt setting will wait X amount of time before taking an\n    action. Adding a new config that will instead prompt the user for a\n    confirmation before taking the action.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1012%2Fazeemba%2Fautoprompt-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1012/azeemba/autoprompt-v1\nPull-Request: https://github.com/gitgitgadget/git/pull/1012\n\n Documentation/config/help.txt | 20 +++++++++++++-------\n help.c                        | 17 ++++++++++++++++-\n 2 files changed, 29 insertions(+), 8 deletions(-)\n\ndiff --git a/Documentation/config/help.txt b/Documentation/config/help.txt\nindex 783a90a0f93..d36d98dd119 100644\n--- a/Documentation/config/help.txt\n+++ b/Documentation/config/help.txt\n@@ -9,13 +9,19 @@ help.format::\n \n help.autoCorrect::\n \tIf git detects typos and can identify exactly one valid command similar\n-\tto the error, git will automatically run the intended command after\n-\twaiting a duration of time defined by this configuration value in\n-\tdeciseconds (0.1 sec).  If this value is 0, the suggested corrections\n-\twill be shown, but not executed. If it is a negative integer, or\n-\t\"immediate\", the suggested command\n-\tis run immediately. If \"never\", suggestions are not shown at all. The\n-\tdefault value is zero.\n+\tto the error, git will try to suggest the correct command or even\n+\trun the intended command.\n+\tIf this config value is 0, then the suggested command will be shown.\n+\tIf it is positive, the suggested command will automatically\n+\trun after waiting for that many deciseconds (0.1 sec).\n+\tIf it is \"immediate\", the suggested command will be\n+\trun immediately.\n+\tIf it is \"prompt\", then the user will be shown the\n+\tsuggestion and will be prompted for confirmation before the command\n+\tis run.\n+\tIf it is \"never\", then no suggestion will be shown and no command\n+\twill be run.\n+\t0 is the default value for this config.\n \n help.htmlPath::\n \tSpecify the path where the HTML documentation resides. File system paths\ndiff --git a/help.c b/help.c\nindex 3c3bdec2135..079156e0421 100644\n--- a/help.c\n+++ b/help.c\n@@ -11,6 +11,7 @@\n #include \"version.h\"\n #include \"refs.h\"\n #include \"parse-options.h\"\n+#include \"prompt.h\"\n \n struct category_description {\n \tuint32_t category;\n@@ -472,6 +473,7 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)\n static int autocorrect;\n static struct cmdnames aliases;\n \n+#define AUTOCORRECT_PROMPT (-3)\n #define AUTOCORRECT_NEVER (-2)\n #define AUTOCORRECT_IMMEDIATELY (-1)\n \n@@ -486,6 +488,8 @@ static int git_unknown_cmd_config(const char *var, const char *value, void *cb)\n \t\t\tautocorrect = AUTOCORRECT_NEVER;\n \t\t} else if (!strcmp(value, \"immediate\")) {\n \t\t\tautocorrect = AUTOCORRECT_IMMEDIATELY;\n+\t\t} else if (!strcmp(value, \"prompt\")) {\n+\t\t\tautocorrect = AUTOCORRECT_PROMPT;\n \t\t} else {\n \t\t\tint v = git_config_int(var, value);\n \t\t\tautocorrect = (v < 0)\n@@ -618,7 +622,18 @@ const char *help_unknown_cmd(const char *cmd)\n \t\t\t\t   _(\"Continuing under the assumption that \"\n \t\t\t\t     \"you meant '%s'.\"),\n \t\t\t\t   assumed);\n-\t\telse {\n+\t\telse if (autocorrect == AUTOCORRECT_PROMPT) {\n+\t\t\tif (!isatty(STDIN_FILENO) | !isatty(STDERR_FILENO))\n+\t\t\t\texit(1);\n+\n+\t\t\tchar *answer;\n+\t\t\tfprintf_ln(stderr, _(\"Assuming you meant: '%s'.\"),\n+\t\t\t\t   assumed);\n+\t\t\tanswer = git_prompt(_(\"Continue? (y/N)\"), PROMPT_ECHO);\n+\t\t\tif (!(starts_with(answer, \"y\") ||\n+\t\t\t      starts_with(answer, \"Y\")))\n+\t\t\t\texit(1);\n+\t\t} else {\n \t\t\tfprintf_ln(stderr,\n \t\t\t\t   _(\"Continuing in %0.1f seconds, \"\n \t\t\t\t     \"assuming that you meant '%s'.\"),\n\nbase-commit: 2d755dfac9aadab25c3e025b849252b8c0a61465\n-- \ngitgitgadget\n"},{"id":"432577","messageId":"4f72cd3d-bcc7-a9b0-cc11-f0d7c5650365@gmail.com","threadId":"56252","inReplyTo":"pull.1012.git.1628640954160.gitgitgadget@gmail.com","subject":"Re: [PATCH] help.c: help.autocorrect=prompt waits for user action","fromName":"Bagas Sanjaya","fromEmail":"bagasdotme@gmail.com","sentAt":"2021-08-12T10:52:08Z","receivedAt":"2021-08-12T10:52:16Z","isPatch":true,"sender":{"key":"bagasdotme@gmail.com","avatar":"https://avatars.githubusercontent.com/u/40219486?v=4"},"body":"On 11/08/21 07.15, Azeem Bande-Ali via GitGitGadget wrote:\n>   help.autoCorrect::\n>   \tIf git detects typos and can identify exactly one valid command similar\n> -\tto the error, git will automatically run the intended command after\n> -\twaiting a duration of time defined by this configuration value in\n> -\tdeciseconds (0.1 sec).  If this value is 0, the suggested corrections\n> -\twill be shown, but not executed. If it is a negative integer, or\n> -\t\"immediate\", the suggested command\n> -\tis run immediately. If \"never\", suggestions are not shown at all. The\n> -\tdefault value is zero.\n> +\tto the error, git will try to suggest the correct command or even\n> +\trun the intended command.\n> +\tIf this config value is 0, then the suggested command will be shown.\n> +\tIf it is positive, the suggested command will automatically\n> +\trun after waiting for that many deciseconds (0.1 sec).\n> +\tIf it is \"immediate\", the suggested command will be\n> +\trun immediately.\n> +\tIf it is \"prompt\", then the user will be shown the\n> +\tsuggestion and will be prompted for confirmation before the command\n> +\tis run.\n> +\tIf it is \"never\", then no suggestion will be shown and no command\n> +\twill be run.\n> +\t0 is the default value for this config.\n\nI think it's better to use bullet lists, e.g.:\n\n```\nIf git detects typos and ...\n<skip>\n... git will try to suggest the correct command or even run the intended \ncommand. Possible values are:\n     - 0 (default): show the suggested command\n     - positive number: run the suggested command after specified \ndeciseconds (0.1 sec).\n     - \"immediate\": run the suggested command immediately\n     - \"prompt\": show the suggestion and prompt for confirmation to run \nthe command\n     - \"never\": don't show any suggestions nor run them\n```\n\n-- \nAn old man doll... just what I always wanted! - Clara\n"},{"id":"432594","messageId":"xmqqh7fu314a.fsf@gitster.g","threadId":"56252","inReplyTo":"pull.1012.git.1628640954160.gitgitgadget@gmail.com","subject":"Re: [PATCH] help.c: help.autocorrect=prompt waits for user action","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-08-12T19:37:57Z","receivedAt":"2021-08-12T19:38:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"> @@ -618,7 +622,18 @@ const char *help_unknown_cmd(const char *cmd)\n>  \t\t\t\t   _(\"Continuing under the assumption that \"\n>  \t\t\t\t     \"you meant '%s'.\"),\n>  \t\t\t\t   assumed);\n> -\t\telse {\n> +\t\telse if (autocorrect == AUTOCORRECT_PROMPT) {\n> +\t\t\tif (!isatty(STDIN_FILENO) | !isatty(STDERR_FILENO))\n\nDon't do bitwise or when you only care about boolean outcome.\n\n> +\t\t\t\texit(1);\n\nThis is not a new issue, but I think it is probably a bug in the\noriginal design of the help-unknown that we do not do this check\nmuch early in help_unknown_cmd(), to disable auto-correction in\na non-interactive session.  If we did so, we do not have to have\nthis check here. \n\nIf we cannot yet come to consensus that disabling autocorrection\nwhen running non-interactively is a good idea, at least we should be\nable to do so only for _PROMPT case, like the attached patch at the\nend.\n\n> +\t\t\tchar *answer;\n\nThis is decl-after-statement, but as I am suggesting to get rid of\nthe tty check done inside this block, it will become OK.\n\n> +\t\t\tfprintf_ln(stderr, _(\"Assuming you meant: '%s'.\"),\n> +\t\t\t\t   assumed);\n> +\t\t\tanswer = git_prompt(_(\"Continue? (y/N)\"), PROMPT_ECHO);\n\nHmph, the above does not look WRONG per-se, but I wonder if it is\neasier for the users to see a single line, e.g.\n\n\tstruct strbuf msg = STRBUF_INIT;\n\tstrbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"), assumed);\n        answer = git_prompt(msg.buf, PROMPT_ECHO);\n        strbuf_release(&msg);\n\nThanks.\n\n\ndiff --git i/help.c w/help.c\nindex e22ba1d246..3629cb8796 100644\n--- i/help.c\n+++ w/help.c\n@@ -540,6 +540,13 @@ const char *help_unknown_cmd(const char *cmd)\n \n \tread_early_config(git_unknown_cmd_config, NULL);\n \n+\t/*\n+\t * Disable autocorrection prompt in a non-interactive session\n+\t */\n+\tif ((autocorrect != AUTOCORRECT_PROMPT) &&\n+\t    (!isatty(0) || !isatty(2)))\n+\t\tautocorrect = AUTOCORRECT_NEVER;\n+\n \tif (autocorrect == AUTOCORRECT_NEVER) {\n \t\tfprintf_ln(stderr, _(\"git: '%s' is not a git command. See 'git --help'.\"), cmd);\n \t\texit(1);\n\n\n\n"},{"id":"432725","messageId":"CABye916n=EuKU3u2ybNtmT8Te9y0C__NsPR7Vdj9LKrf-y9coQ@mail.gmail.com","threadId":"56252","inReplyTo":"4f72cd3d-bcc7-a9b0-cc11-f0d7c5650365@gmail.com","subject":"Re: [PATCH] help.c: help.autocorrect=prompt waits for user action","fromName":"Azeem Bande-Ali","fromEmail":"me@azeemba.com","sentAt":"2021-08-14T02:57:39Z","receivedAt":"2021-08-14T02:58:16Z","isPatch":true,"sender":{"key":"me@azeemba.com","avatar":"https://avatars.githubusercontent.com/u/2160795?v=4"},"body":"On Thu, Aug 12, 2021 at 6:52 AM Bagas Sanjaya <bagasdotme@gmail.com> wrote:\n> I think it's better to use bullet lists, e.g.:\n>\n> ```\n> If git detects typos and ...\n> <skip>\n> ... git will try to suggest the correct command or even run the intended\n> command. Possible values are:\n>      - 0 (default): show the suggested command\n>      - positive number: run the suggested command after specified\n> deciseconds (0.1 sec).\n>      - \"immediate\": run the suggested command immediately\n>      - \"prompt\": show the suggestion and prompt for confirmation to run\n> the command\n>      - \"never\": don't show any suggestions nor run them\n> ```\n\nThanks for the feedback. I agree the bullets do look much better!\n"},{"id":"432726","messageId":"CABye9140=jnkERq8MLEcr_umW1+oaE5Wi39_iQnM3jevV3Hraw@mail.gmail.com","threadId":"56252","inReplyTo":"xmqqh7fu314a.fsf@gitster.g","subject":"Re: [PATCH] help.c: help.autocorrect=prompt waits for user action","fromName":"Azeem Bande-Ali","fromEmail":"me@azeemba.com","sentAt":"2021-08-14T03:07:26Z","receivedAt":"2021-08-14T03:07:57Z","isPatch":true,"sender":{"key":"me@azeemba.com","avatar":"https://avatars.githubusercontent.com/u/2160795?v=4"},"body":"On Thu, Aug 12, 2021 at 3:38 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> If we cannot yet come to consensus that disabling autocorrection\n> when running non-interactively is a good idea, at least we should be\n> able to do so only for _PROMPT case, like the attached patch at the\n> end.\n\nThanks for the suggestion. I think it makes sense to skip the autocorrection\ncompletely when the setting is \"prompt\" and the user is not running\ninteractively.\n\n> > +                     fprintf_ln(stderr, _(\"Assuming you meant: '%s'.\"),\n> > +                                assumed);\n> > +                     answer = git_prompt(_(\"Continue? (y/N)\"), PROMPT_ECHO);\n>\n> Hmph, the above does not look WRONG per-se, but I wonder if it is\n> easier for the users to see a single line, e.g.\n>\n>         struct strbuf msg = STRBUF_INIT;\n>         strbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"), assumed);\n>         answer = git_prompt(msg.buf, PROMPT_ECHO);\n>         strbuf_release(&msg);\n>\n\nI looked at the behavior for other settings and they\nall behave pretty compactly. So I think this suggested\nsolution does fit better.\n\n> +       /*\n> +        * Disable autocorrection prompt in a non-interactive session\n> +        */\n> +       if ((autocorrect != AUTOCORRECT_PROMPT) &&\n> +           (!isatty(0) || !isatty(2)))\n> +               autocorrect = AUTOCORRECT_NEVER;\n> +\n\nThat should be `(autocorrect == AUTOCORRECT_PROMPT)` right?\nSo we skip the autocorrect if the user is not running interactively\nand has the \"prompt\" setting.\n\nThanks for all the feedback! I will send out a patch incorporating\nthis feedback soon.\n"},{"id":"432727","messageId":"pull.1012.v2.git.1628917872724.gitgitgadget@gmail.com","threadId":"56252","inReplyTo":"pull.1012.git.1628640954160.gitgitgadget@gmail.com","subject":"[PATCH v2] help.c: help.autocorrect=prompt waits for user action","fromName":"Azeem Bande-Ali via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-08-14T05:11:12Z","receivedAt":"2021-08-14T05:13:24Z","isPatch":true,"sender":{"key":"me@azeemba.com","avatar":"https://avatars.githubusercontent.com/u/2160795?v=4"},"body":"From: Azeem Bande-Ali <me@azeemba.com>\n\nIf help.autocorrect is set to 'prompt', the user is prompted\nbefore the suggested action is executed.\n\nBased on original patch by David Barr\nhttps://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/\n\nSigned-off-by: Azeem Bande-Ali <me@azeemba.com>\n---\n    New config for help.autocorrect to prompt user before action\n    \n    Changes since v1:\n    \n     * Use bullet lists for documenting the various settings now supported\n       by help.autoCorrect\n     * Add a check for an interactive terminal early to bail if the user has\n       the \"prompt\" setting\n     * Make the output more compact so the user can see the suggestion and\n       the prompt in one line.\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1012%2Fazeemba%2Fautoprompt-v2\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1012/azeemba/autoprompt-v2\nPull-Request: https://github.com/gitgitgadget/git/pull/1012\n\nRange-diff vs v1:\n\n 1:  7f72819b805 ! 1:  5819b872356 help.c: help.autocorrect=prompt waits for user action\n     @@ Documentation/config/help.txt: help.format::\n      -\tis run immediately. If \"never\", suggestions are not shown at all. The\n      -\tdefault value is zero.\n      +\tto the error, git will try to suggest the correct command or even\n     -+\trun the intended command.\n     -+\tIf this config value is 0, then the suggested command will be shown.\n     -+\tIf it is positive, the suggested command will automatically\n     -+\trun after waiting for that many deciseconds (0.1 sec).\n     -+\tIf it is \"immediate\", the suggested command will be\n     -+\trun immediately.\n     -+\tIf it is \"prompt\", then the user will be shown the\n     -+\tsuggestion and will be prompted for confirmation before the command\n     -+\tis run.\n     -+\tIf it is \"never\", then no suggestion will be shown and no command\n     -+\twill be run.\n     -+\t0 is the default value for this config.\n     ++\trun the suggestion automatically. Possible config values are:\n     ++\t - 0 (default): show the suggested command.\n     ++\t - positive number: run the suggested command after specified\n     ++deciseconds (0.1 sec).\n     ++\t - \"immediate\": run the suggested command immediately.\n     ++\t - \"prompt\": show the suggestion and prompt for confirmation to run\n     ++the command.\n     ++\t - \"never\": don't run or show any suggested command.\n       \n       help.htmlPath::\n       \tSpecify the path where the HTML documentation resides. File system paths\n     @@ help.c: static int git_unknown_cmd_config(const char *var, const char *value, vo\n       \t\t\tint v = git_config_int(var, value);\n       \t\t\tautocorrect = (v < 0)\n      @@ help.c: const char *help_unknown_cmd(const char *cmd)\n     + \n     + \tread_early_config(git_unknown_cmd_config, NULL);\n     + \n     ++\t/*\n     ++\t * Disable autocorrection prompt in a non-interactive session\n     ++\t */\n     ++\tif ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))\n     ++\t\tautocorrect = AUTOCORRECT_NEVER;\n     ++\n     + \tif (autocorrect == AUTOCORRECT_NEVER) {\n     + \t\tfprintf_ln(stderr, _(\"git: '%s' is not a git command. See 'git --help'.\"), cmd);\n     + \t\texit(1);\n     +@@ help.c: const char *help_unknown_cmd(const char *cmd)\n       \t\t\t\t   _(\"Continuing under the assumption that \"\n       \t\t\t\t     \"you meant '%s'.\"),\n       \t\t\t\t   assumed);\n      -\t\telse {\n      +\t\telse if (autocorrect == AUTOCORRECT_PROMPT) {\n     -+\t\t\tif (!isatty(STDIN_FILENO) | !isatty(STDERR_FILENO))\n     -+\t\t\t\texit(1);\n     -+\n     -+\t\t\tchar *answer;\n     -+\t\t\tfprintf_ln(stderr, _(\"Assuming you meant: '%s'.\"),\n     -+\t\t\t\t   assumed);\n     -+\t\t\tanswer = git_prompt(_(\"Continue? (y/N)\"), PROMPT_ECHO);\n     ++\t\t\tchar* answer;\n     ++\t\t\tstruct strbuf msg = STRBUF_INIT;\n     ++\t\t\tstrbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"),\n     ++\t\t\t\t    assumed);\n     ++\t\t\tanswer = git_prompt(msg.buf, PROMPT_ECHO);\n     ++\t\t\tstrbuf_release(&msg);\n      +\t\t\tif (!(starts_with(answer, \"y\") ||\n      +\t\t\t      starts_with(answer, \"Y\")))\n      +\t\t\t\texit(1);\n\n\n Documentation/config/help.txt | 16 +++++++++-------\n help.c                        | 22 +++++++++++++++++++++-\n 2 files changed, 30 insertions(+), 8 deletions(-)\n\ndiff --git a/Documentation/config/help.txt b/Documentation/config/help.txt\nindex 783a90a0f93..610701f9a37 100644\n--- a/Documentation/config/help.txt\n+++ b/Documentation/config/help.txt\n@@ -9,13 +9,15 @@ help.format::\n \n help.autoCorrect::\n \tIf git detects typos and can identify exactly one valid command similar\n-\tto the error, git will automatically run the intended command after\n-\twaiting a duration of time defined by this configuration value in\n-\tdeciseconds (0.1 sec).  If this value is 0, the suggested corrections\n-\twill be shown, but not executed. If it is a negative integer, or\n-\t\"immediate\", the suggested command\n-\tis run immediately. If \"never\", suggestions are not shown at all. The\n-\tdefault value is zero.\n+\tto the error, git will try to suggest the correct command or even\n+\trun the suggestion automatically. Possible config values are:\n+\t - 0 (default): show the suggested command.\n+\t - positive number: run the suggested command after specified\n+deciseconds (0.1 sec).\n+\t - \"immediate\": run the suggested command immediately.\n+\t - \"prompt\": show the suggestion and prompt for confirmation to run\n+the command.\n+\t - \"never\": don't run or show any suggested command.\n \n help.htmlPath::\n \tSpecify the path where the HTML documentation resides. File system paths\ndiff --git a/help.c b/help.c\nindex 3c3bdec2135..22bb05e8e3d 100644\n--- a/help.c\n+++ b/help.c\n@@ -11,6 +11,7 @@\n #include \"version.h\"\n #include \"refs.h\"\n #include \"parse-options.h\"\n+#include \"prompt.h\"\n \n struct category_description {\n \tuint32_t category;\n@@ -472,6 +473,7 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)\n static int autocorrect;\n static struct cmdnames aliases;\n \n+#define AUTOCORRECT_PROMPT (-3)\n #define AUTOCORRECT_NEVER (-2)\n #define AUTOCORRECT_IMMEDIATELY (-1)\n \n@@ -486,6 +488,8 @@ static int git_unknown_cmd_config(const char *var, const char *value, void *cb)\n \t\t\tautocorrect = AUTOCORRECT_NEVER;\n \t\t} else if (!strcmp(value, \"immediate\")) {\n \t\t\tautocorrect = AUTOCORRECT_IMMEDIATELY;\n+\t\t} else if (!strcmp(value, \"prompt\")) {\n+\t\t\tautocorrect = AUTOCORRECT_PROMPT;\n \t\t} else {\n \t\t\tint v = git_config_int(var, value);\n \t\t\tautocorrect = (v < 0)\n@@ -539,6 +543,12 @@ const char *help_unknown_cmd(const char *cmd)\n \n \tread_early_config(git_unknown_cmd_config, NULL);\n \n+\t/*\n+\t * Disable autocorrection prompt in a non-interactive session\n+\t */\n+\tif ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))\n+\t\tautocorrect = AUTOCORRECT_NEVER;\n+\n \tif (autocorrect == AUTOCORRECT_NEVER) {\n \t\tfprintf_ln(stderr, _(\"git: '%s' is not a git command. See 'git --help'.\"), cmd);\n \t\texit(1);\n@@ -618,7 +628,17 @@ const char *help_unknown_cmd(const char *cmd)\n \t\t\t\t   _(\"Continuing under the assumption that \"\n \t\t\t\t     \"you meant '%s'.\"),\n \t\t\t\t   assumed);\n-\t\telse {\n+\t\telse if (autocorrect == AUTOCORRECT_PROMPT) {\n+\t\t\tchar* answer;\n+\t\t\tstruct strbuf msg = STRBUF_INIT;\n+\t\t\tstrbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"),\n+\t\t\t\t    assumed);\n+\t\t\tanswer = git_prompt(msg.buf, PROMPT_ECHO);\n+\t\t\tstrbuf_release(&msg);\n+\t\t\tif (!(starts_with(answer, \"y\") ||\n+\t\t\t      starts_with(answer, \"Y\")))\n+\t\t\t\texit(1);\n+\t\t} else {\n \t\t\tfprintf_ln(stderr,\n \t\t\t\t   _(\"Continuing in %0.1f seconds, \"\n \t\t\t\t     \"assuming that you meant '%s'.\"),\n\nbase-commit: 2d755dfac9aadab25c3e025b849252b8c0a61465\n-- \ngitgitgadget\n"},{"id":"432728","messageId":"c3b9aac8-992c-30de-c6a1-852b81ddecff@gmail.com","threadId":"56252","inReplyTo":"pull.1012.v2.git.1628917872724.gitgitgadget@gmail.com","subject":"Re: [PATCH v2] help.c: help.autocorrect=prompt waits for user action","fromName":"Bagas Sanjaya","fromEmail":"bagasdotme@gmail.com","sentAt":"2021-08-14T05:50:37Z","receivedAt":"2021-08-14T05:50:43Z","isPatch":true,"sender":{"key":"bagasdotme@gmail.com","avatar":"https://avatars.githubusercontent.com/u/40219486?v=4"},"body":"On 14/08/21 12.11, Azeem Bande-Ali via GitGitGadget wrote:\n>   help.autoCorrect::\n>   \tIf git detects typos and can identify exactly one valid command similar\n> -\tto the error, git will automatically run the intended command after\n> -\twaiting a duration of time defined by this configuration value in\n> -\tdeciseconds (0.1 sec).  If this value is 0, the suggested corrections\n> -\twill be shown, but not executed. If it is a negative integer, or\n> -\t\"immediate\", the suggested command\n> -\tis run immediately. If \"never\", suggestions are not shown at all. The\n> -\tdefault value is zero.\n> +\tto the error, git will try to suggest the correct command or even\n> +\trun the suggestion automatically. Possible config values are:\n> +\t - 0 (default): show the suggested command.\n> +\t - positive number: run the suggested command after specified\n> +deciseconds (0.1 sec).\n> +\t - \"immediate\": run the suggested command immediately.\n> +\t - \"prompt\": show the suggestion and prompt for confirmation to run\n> +the command.\n> +\t - \"never\": don't run or show any suggested command.\n\nLooks OK.\n\nReviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>\n\n-- \nAn old man doll... just what I always wanted! - Clara\n"},{"id":"432739","messageId":"xmqqmtpjubuv.fsf@gitster.g","threadId":"56252","inReplyTo":"pull.1012.v2.git.1628917872724.gitgitgadget@gmail.com","subject":"Re: [PATCH v2] help.c: help.autocorrect=prompt waits for user action","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-08-14T18:20:40Z","receivedAt":"2021-08-14T18:20:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Azeem Bande-Ali via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> @@ -618,7 +628,17 @@ const char *help_unknown_cmd(const char *cmd)\n>  \t\t\t\t   _(\"Continuing under the assumption that \"\n>  \t\t\t\t     \"you meant '%s'.\"),\n>  \t\t\t\t   assumed);\n> -\t\telse {\n> +\t\telse if (autocorrect == AUTOCORRECT_PROMPT) {\n> +\t\t\tchar* answer;\n\nSome people seem to make an asterisk stick to types like this, but\nin our codebase written in C, an asterisk sticks to the identifier\nthat it makes into a pointer, i.e.\n\n\t\t\tchar *answer;\n\nThis is because doing so differently would confuse novices with\nconstructs like this:\n\n\t\t\tint* a, b;\n\nwhere only 'a' is a pointer, and 'b' is not.\n\n> +\t\t\tstruct strbuf msg = STRBUF_INIT;\n> +\t\t\tstrbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"),\n> +\t\t\t\t    assumed);\n\nI think these should be kept on a single line for readability, i.e.\n\n\t\t\tstrbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"), assumed);\n\nas I see a fairly long line after this block already.\n\n\tfprintf_ln(stderr, _(\"git: '%s' is not a git command. See 'git --help'.\"), cmd);\n\n\nOther than these cosmetic bits, this round looks good to me,\nincluding the documentation update.\n\nThanks.\n"},{"id":"432740","messageId":"CABye917ALhCsfg_7JD72TxO0i2bhTc-0pJKKZwt8BVdiv1Qw4w@mail.gmail.com","threadId":"56252","inReplyTo":"xmqqmtpjubuv.fsf@gitster.g","subject":"Re: [PATCH v2] help.c: help.autocorrect=prompt waits for user action","fromName":"Azeem Bande-Ali","fromEmail":"me@azeemba.com","sentAt":"2021-08-14T18:40:55Z","receivedAt":"2021-08-14T18:41:27Z","isPatch":true,"sender":{"key":"me@azeemba.com","avatar":"https://avatars.githubusercontent.com/u/2160795?v=4"},"body":"On Sat, Aug 14, 2021 at 2:20 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> \"Azeem Bande-Ali via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > +                     char* answer;\n>\n> Some people seem to make an asterisk stick to types like this, but\n> in our codebase written in C, an asterisk sticks to the identifier\n> that it makes into a pointer, i.e.\n>\n>                         char *answer;\n>\nThanks! Will fix. I appreciate you providing the motivation for the\npolicy as well!\n\n> > +                     struct strbuf msg = STRBUF_INIT;\n> > +                     strbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"),\n> > +                                 assumed);\n>\n> I think these should be kept on a single line for readability, i.e.\n>\n>                         strbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"), assumed);\n>\n> as I see a fairly long line after this block already.\n>\n>         fprintf_ln(stderr, _(\"git: '%s' is not a git command. See 'git --help'.\"), cmd);\n\nThe line split was done by clang-format via `make style`. Is it okay\nto ignore the clang-format recommendation?\n(Incidentally `make style` would have also caught the first issue, I\nseem to have not run it after\nmaking a change in that line.)\n\n> Other than these cosmetic bits, this round looks good to me,\n> including the documentation update.\n\nGreat thanks! This is my first patch so I just wanted to confirm:\nshould I add a \"Reviewed-by:\"\nline to the commit with your name as well?\n"},{"id":"432757","messageId":"pull.1012.v3.git.1628992226139.gitgitgadget@gmail.com","threadId":"56252","inReplyTo":"pull.1012.v2.git.1628917872724.gitgitgadget@gmail.com","subject":"[PATCH v3] help.c: help.autocorrect=prompt waits for user action","fromName":"Azeem Bande-Ali via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-08-15T01:50:26Z","receivedAt":"2021-08-15T01:50:34Z","isPatch":true,"sender":{"key":"me@azeemba.com","avatar":"https://avatars.githubusercontent.com/u/2160795?v=4"},"body":"From: Azeem Bande-Ali <me@azeemba.com>\n\nIf help.autocorrect is set to 'prompt', the user is prompted\nbefore the suggested action is executed.\n\nBased on original patch by David Barr\nhttps://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/\n\nReviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>\nSigned-off-by: Azeem Bande-Ali <me@azeemba.com>\n---\n    New config for help.autocorrect to prompt user before action\n    \n    Only minor style changes have been made since v2:\n    \n     * Moved * to variable name instead of the type name\n     * Keep code in the same line for readability\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-1012%2Fazeemba%2Fautoprompt-v3\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1012/azeemba/autoprompt-v3\nPull-Request: https://github.com/gitgitgadget/git/pull/1012\n\nRange-diff vs v2:\n\n 1:  5819b872356 ! 1:  f3f38f0d141 help.c: help.autocorrect=prompt waits for user action\n     @@ Commit message\n          Based on original patch by David Barr\n          https://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/\n      \n     +    Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>\n          Signed-off-by: Azeem Bande-Ali <me@azeemba.com>\n      \n       ## Documentation/config/help.txt ##\n     @@ help.c: const char *help_unknown_cmd(const char *cmd)\n       \t\t\t\t   assumed);\n      -\t\telse {\n      +\t\telse if (autocorrect == AUTOCORRECT_PROMPT) {\n     -+\t\t\tchar* answer;\n     ++\t\t\tchar *answer;\n      +\t\t\tstruct strbuf msg = STRBUF_INIT;\n     -+\t\t\tstrbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"),\n     -+\t\t\t\t    assumed);\n     ++\t\t\tstrbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"), assumed);\n      +\t\t\tanswer = git_prompt(msg.buf, PROMPT_ECHO);\n      +\t\t\tstrbuf_release(&msg);\n      +\t\t\tif (!(starts_with(answer, \"y\") ||\n\n\n Documentation/config/help.txt | 16 +++++++++-------\n help.c                        | 21 ++++++++++++++++++++-\n 2 files changed, 29 insertions(+), 8 deletions(-)\n\ndiff --git a/Documentation/config/help.txt b/Documentation/config/help.txt\nindex 783a90a0f93..610701f9a37 100644\n--- a/Documentation/config/help.txt\n+++ b/Documentation/config/help.txt\n@@ -9,13 +9,15 @@ help.format::\n \n help.autoCorrect::\n \tIf git detects typos and can identify exactly one valid command similar\n-\tto the error, git will automatically run the intended command after\n-\twaiting a duration of time defined by this configuration value in\n-\tdeciseconds (0.1 sec).  If this value is 0, the suggested corrections\n-\twill be shown, but not executed. If it is a negative integer, or\n-\t\"immediate\", the suggested command\n-\tis run immediately. If \"never\", suggestions are not shown at all. The\n-\tdefault value is zero.\n+\tto the error, git will try to suggest the correct command or even\n+\trun the suggestion automatically. Possible config values are:\n+\t - 0 (default): show the suggested command.\n+\t - positive number: run the suggested command after specified\n+deciseconds (0.1 sec).\n+\t - \"immediate\": run the suggested command immediately.\n+\t - \"prompt\": show the suggestion and prompt for confirmation to run\n+the command.\n+\t - \"never\": don't run or show any suggested command.\n \n help.htmlPath::\n \tSpecify the path where the HTML documentation resides. File system paths\ndiff --git a/help.c b/help.c\nindex 3c3bdec2135..be2fa642415 100644\n--- a/help.c\n+++ b/help.c\n@@ -11,6 +11,7 @@\n #include \"version.h\"\n #include \"refs.h\"\n #include \"parse-options.h\"\n+#include \"prompt.h\"\n \n struct category_description {\n \tuint32_t category;\n@@ -472,6 +473,7 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)\n static int autocorrect;\n static struct cmdnames aliases;\n \n+#define AUTOCORRECT_PROMPT (-3)\n #define AUTOCORRECT_NEVER (-2)\n #define AUTOCORRECT_IMMEDIATELY (-1)\n \n@@ -486,6 +488,8 @@ static int git_unknown_cmd_config(const char *var, const char *value, void *cb)\n \t\t\tautocorrect = AUTOCORRECT_NEVER;\n \t\t} else if (!strcmp(value, \"immediate\")) {\n \t\t\tautocorrect = AUTOCORRECT_IMMEDIATELY;\n+\t\t} else if (!strcmp(value, \"prompt\")) {\n+\t\t\tautocorrect = AUTOCORRECT_PROMPT;\n \t\t} else {\n \t\t\tint v = git_config_int(var, value);\n \t\t\tautocorrect = (v < 0)\n@@ -539,6 +543,12 @@ const char *help_unknown_cmd(const char *cmd)\n \n \tread_early_config(git_unknown_cmd_config, NULL);\n \n+\t/*\n+\t * Disable autocorrection prompt in a non-interactive session\n+\t */\n+\tif ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))\n+\t\tautocorrect = AUTOCORRECT_NEVER;\n+\n \tif (autocorrect == AUTOCORRECT_NEVER) {\n \t\tfprintf_ln(stderr, _(\"git: '%s' is not a git command. See 'git --help'.\"), cmd);\n \t\texit(1);\n@@ -618,7 +628,16 @@ const char *help_unknown_cmd(const char *cmd)\n \t\t\t\t   _(\"Continuing under the assumption that \"\n \t\t\t\t     \"you meant '%s'.\"),\n \t\t\t\t   assumed);\n-\t\telse {\n+\t\telse if (autocorrect == AUTOCORRECT_PROMPT) {\n+\t\t\tchar *answer;\n+\t\t\tstruct strbuf msg = STRBUF_INIT;\n+\t\t\tstrbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"), assumed);\n+\t\t\tanswer = git_prompt(msg.buf, PROMPT_ECHO);\n+\t\t\tstrbuf_release(&msg);\n+\t\t\tif (!(starts_with(answer, \"y\") ||\n+\t\t\t      starts_with(answer, \"Y\")))\n+\t\t\t\texit(1);\n+\t\t} else {\n \t\t\tfprintf_ln(stderr,\n \t\t\t\t   _(\"Continuing in %0.1f seconds, \"\n \t\t\t\t     \"assuming that you meant '%s'.\"),\n\nbase-commit: 2d755dfac9aadab25c3e025b849252b8c0a61465\n-- \ngitgitgadget\n"},{"id":"432794","messageId":"nycvar.QRO.7.76.6.2108161427270.55@tvgsbejvaqbjf.bet","threadId":"56252","inReplyTo":"xmqqmtpjubuv.fsf@gitster.g","subject":"Re: [PATCH v2] help.c: help.autocorrect=prompt waits for user action","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2021-08-16T12:28:28Z","receivedAt":"2021-08-16T12:28:43Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi,\n\nOn Sat, 14 Aug 2021, Junio C Hamano wrote:\n\n> \"Azeem Bande-Ali via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n>\n> > @@ -618,7 +628,17 @@ const char *help_unknown_cmd(const char *cmd)\n> >  \t\t\t\t   _(\"Continuing under the assumption that \"\n> >  \t\t\t\t     \"you meant '%s'.\"),\n> >  \t\t\t\t   assumed);\n> > -\t\telse {\n> > +\t\telse if (autocorrect == AUTOCORRECT_PROMPT) {\n> > +\t\t\tchar* answer;\n>\n> Some people seem to make an asterisk stick to types like this, but\n> in our codebase written in C, an asterisk sticks to the identifier\n> that it makes into a pointer, i.e.\n>\n> \t\t\tchar *answer;\n>\n> This is because doing so differently would confuse novices with\n> constructs like this:\n>\n> \t\t\tint* a, b;\n>\n> where only 'a' is a pointer, and 'b' is not.\n>\n> > +\t\t\tstruct strbuf msg = STRBUF_INIT;\n> > +\t\t\tstrbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"),\n> > +\t\t\t\t    assumed);\n>\n> I think these should be kept on a single line for readability, i.e.\n>\n> \t\t\tstrbuf_addf(&msg, _(\"Run '%s' instead? (y/N)\"), assumed);\n\nIt might even make sense to use the `xstrfmt()` function instead:\n\n\t\t\tchar *msg = xstrfmt(_(\"Run '%s' instead? (y/N)\"), assumed);\n\t\t\t[...]\n\t\t\tfree(msg);\n\nCiao,\nDscho\n"},{"id":"434971","messageId":"xmqqee9z9ayf.fsf@gitster.g","threadId":"56252","inReplyTo":"pull.1012.v3.git.1628992226139.gitgitgadget@gmail.com","subject":"Re: [PATCH v3] help.c: help.autocorrect=prompt waits for user action","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-09-08T00:18:16Z","receivedAt":"2021-09-08T00:18:19Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"\"Azeem Bande-Ali via GitGitGadget\" <gitgitgadget@gmail.com> writes:\n\n> From: Azeem Bande-Ali <me@azeemba.com>\n>\n> If help.autocorrect is set to 'prompt', the user is prompted\n> before the suggested action is executed.\n>\n> Based on original patch by David Barr\n> https://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/\n>\n> Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>\n> Signed-off-by: Azeem Bande-Ali <me@azeemba.com>\n> ---\n>     New config for help.autocorrect to prompt user before action\n>     \n>     Only minor style changes have been made since v2:\n>     \n>      * Moved * to variable name instead of the type name\n>      * Keep code in the same line for readability\n\nI didn't see anybody comment on this round (and do not think I saw\nanything glaringly wrong).\n\nIs everybody happy with this version?  I am about to mark it for\n'next' in the next issue of \"What's cooking\" report, so please\nholler if I should wait.\n\nThanks.\n"}]}