git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH] help.c: help.autocorrect=prompt waits for user action
@ 2021-08-11  0:15 Azeem Bande-Ali via GitGitGadget
  2021-08-12 10:52 ` Bagas Sanjaya
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Azeem Bande-Ali via GitGitGadget @ 2021-08-11  0:15 UTC (permalink / raw)
  To: git; +Cc: David Barr, Azeem Bande-Ali, Azeem Bande-Ali

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/

Signed-off-by: Azeem Bande-Ali <me@azeemba.com>
---
    New config for help.autocorrect to prompt user before action
    
    Currently the prompt setting will wait X amount of time before taking an
    action. Adding a new config that will instead prompt the user for a
    confirmation before taking the action.

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

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

diff --git a/Documentation/config/help.txt b/Documentation/config/help.txt
index 783a90a0f93..d36d98dd119 100644
--- a/Documentation/config/help.txt
+++ b/Documentation/config/help.txt
@@ -9,13 +9,19 @@ 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 intended command.
+	If this config value is 0, then the suggested command will be shown.
+	If it is positive, the suggested command will automatically
+	run after waiting for that many deciseconds (0.1 sec).
+	If it is "immediate", the suggested command will be
+	run immediately.
+	If it is "prompt", then the user will be shown the
+	suggestion and will be prompted for confirmation before the command
+	is run.
+	If it is "never", then no suggestion will be shown and no command
+	will be run.
+	0 is the default value for this config.
 
 help.htmlPath::
 	Specify the path where the HTML documentation resides. File system paths
diff --git a/help.c b/help.c
index 3c3bdec2135..079156e0421 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)
@@ -618,7 +622,18 @@ const char *help_unknown_cmd(const char *cmd)
 				   _("Continuing under the assumption that "
 				     "you meant '%s'."),
 				   assumed);
-		else {
+		else if (autocorrect == AUTOCORRECT_PROMPT) {
+			if (!isatty(STDIN_FILENO) | !isatty(STDERR_FILENO))
+				exit(1);
+
+			char *answer;
+			fprintf_ln(stderr, _("Assuming you meant: '%s'."),
+				   assumed);
+			answer = git_prompt(_("Continue? (y/N)"), PROMPT_ECHO);
+			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

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2021-09-08  0:18 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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   ` [PATCH v3] " Azeem Bande-Ali via GitGitGadget
2021-09-08  0:18     ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).