All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Azeem Bande-Ali via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: David Barr <b@rr-dav.id.au>, Bagas Sanjaya <bagasdotme@gmail.com>,
	Azeem Bande-Ali <me@azeemba.com>,
	Azeem Bande-Ali <A.BandeAli@gmail.com>,
	Azeem Bande-Ali <me@azeemba.com>
Subject: [PATCH v3] help.c: help.autocorrect=prompt waits for user action
Date: Sun, 15 Aug 2021 01:50:26 +0000	[thread overview]
Message-ID: <pull.1012.v3.git.1628992226139.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1012.v2.git.1628917872724.gitgitgadget@gmail.com>

From: Azeem Bande-Ali <me@azeemba.com>

If help.autocorrect is set to 'prompt', the user is prompted
before the suggested action is executed.

Based on original patch by David Barr
https://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/

Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>
Signed-off-by: Azeem Bande-Ali <me@azeemba.com>
---
    New config for help.autocorrect to prompt user before action
    
    Only minor style changes have been made since v2:
    
     * Moved * to variable name instead of the type name
     * Keep code in the same line for readability

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1012%2Fazeemba%2Fautoprompt-v3
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1012/azeemba/autoprompt-v3
Pull-Request: https://github.com/gitgitgadget/git/pull/1012

Range-diff vs v2:

 1:  5819b872356 ! 1:  f3f38f0d141 help.c: help.autocorrect=prompt waits for user action
     @@ Commit message
          Based on original patch by David Barr
          https://lore.kernel.org/git/1283758030-13345-1-git-send-email-david.barr@cordelta.com/
      
     +    Reviewed-by: Bagas Sanjaya <bagasdotme@gmail.com>
          Signed-off-by: Azeem Bande-Ali <me@azeemba.com>
      
       ## Documentation/config/help.txt ##
     @@ help.c: const char *help_unknown_cmd(const char *cmd)
       				   assumed);
      -		else {
      +		else if (autocorrect == AUTOCORRECT_PROMPT) {
     -+			char* answer;
     ++			char *answer;
      +			struct strbuf msg = STRBUF_INIT;
     -+			strbuf_addf(&msg, _("Run '%s' instead? (y/N)"),
     -+				    assumed);
     ++			strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), assumed);
      +			answer = git_prompt(msg.buf, PROMPT_ECHO);
      +			strbuf_release(&msg);
      +			if (!(starts_with(answer, "y") ||


 Documentation/config/help.txt | 16 +++++++++-------
 help.c                        | 21 ++++++++++++++++++++-
 2 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/Documentation/config/help.txt b/Documentation/config/help.txt
index 783a90a0f93..610701f9a37 100644
--- a/Documentation/config/help.txt
+++ b/Documentation/config/help.txt
@@ -9,13 +9,15 @@ help.format::
 
 help.autoCorrect::
 	If git detects typos and can identify exactly one valid command similar
-	to the error, git will automatically run the intended command after
-	waiting a duration of time defined by this configuration value in
-	deciseconds (0.1 sec).  If this value is 0, the suggested corrections
-	will be shown, but not executed. If it is a negative integer, or
-	"immediate", the suggested command
-	is run immediately. If "never", suggestions are not shown at all. The
-	default value is zero.
+	to the error, git will try to suggest the correct command or even
+	run the suggestion automatically. Possible config values are:
+	 - 0 (default): show the suggested command.
+	 - positive number: run the suggested command after specified
+deciseconds (0.1 sec).
+	 - "immediate": run the suggested command immediately.
+	 - "prompt": show the suggestion and prompt for confirmation to run
+the command.
+	 - "never": don't run or show any suggested command.
 
 help.htmlPath::
 	Specify the path where the HTML documentation resides. File system paths
diff --git a/help.c b/help.c
index 3c3bdec2135..be2fa642415 100644
--- a/help.c
+++ b/help.c
@@ -11,6 +11,7 @@
 #include "version.h"
 #include "refs.h"
 #include "parse-options.h"
+#include "prompt.h"
 
 struct category_description {
 	uint32_t category;
@@ -472,6 +473,7 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
 static int autocorrect;
 static struct cmdnames aliases;
 
+#define AUTOCORRECT_PROMPT (-3)
 #define AUTOCORRECT_NEVER (-2)
 #define AUTOCORRECT_IMMEDIATELY (-1)
 
@@ -486,6 +488,8 @@ static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
 			autocorrect = AUTOCORRECT_NEVER;
 		} else if (!strcmp(value, "immediate")) {
 			autocorrect = AUTOCORRECT_IMMEDIATELY;
+		} else if (!strcmp(value, "prompt")) {
+			autocorrect = AUTOCORRECT_PROMPT;
 		} else {
 			int v = git_config_int(var, value);
 			autocorrect = (v < 0)
@@ -539,6 +543,12 @@ const char *help_unknown_cmd(const char *cmd)
 
 	read_early_config(git_unknown_cmd_config, NULL);
 
+	/*
+	 * Disable autocorrection prompt in a non-interactive session
+	 */
+	if ((autocorrect == AUTOCORRECT_PROMPT) && (!isatty(0) || !isatty(2)))
+		autocorrect = AUTOCORRECT_NEVER;
+
 	if (autocorrect == AUTOCORRECT_NEVER) {
 		fprintf_ln(stderr, _("git: '%s' is not a git command. See 'git --help'."), cmd);
 		exit(1);
@@ -618,7 +628,16 @@ const char *help_unknown_cmd(const char *cmd)
 				   _("Continuing under the assumption that "
 				     "you meant '%s'."),
 				   assumed);
-		else {
+		else if (autocorrect == AUTOCORRECT_PROMPT) {
+			char *answer;
+			struct strbuf msg = STRBUF_INIT;
+			strbuf_addf(&msg, _("Run '%s' instead? (y/N)"), assumed);
+			answer = git_prompt(msg.buf, PROMPT_ECHO);
+			strbuf_release(&msg);
+			if (!(starts_with(answer, "y") ||
+			      starts_with(answer, "Y")))
+				exit(1);
+		} else {
 			fprintf_ln(stderr,
 				   _("Continuing in %0.1f seconds, "
 				     "assuming that you meant '%s'."),

base-commit: 2d755dfac9aadab25c3e025b849252b8c0a61465
-- 
gitgitgadget

  parent reply	other threads:[~2021-08-15  1:50 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-11  0:15 [PATCH] help.c: help.autocorrect=prompt waits for user action Azeem Bande-Ali via GitGitGadget
2021-08-12 10:52 ` Bagas Sanjaya
2021-08-14  2:57   ` Azeem Bande-Ali
2021-08-12 19:37 ` Junio C Hamano
2021-08-14  3:07   ` Azeem Bande-Ali
2021-08-14  5:11 ` [PATCH v2] " Azeem Bande-Ali via GitGitGadget
2021-08-14  5:50   ` Bagas Sanjaya
2021-08-14 18:20   ` Junio C Hamano
2021-08-14 18:40     ` Azeem Bande-Ali
2021-08-16 12:28     ` Johannes Schindelin
2021-08-15  1:50   ` Azeem Bande-Ali via GitGitGadget [this message]
2021-09-08  0:18     ` [PATCH v3] " Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=pull.1012.v3.git.1628992226139.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=A.BandeAli@gmail.com \
    --cc=b@rr-dav.id.au \
    --cc=bagasdotme@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=me@azeemba.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.