git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v6 02/10] help: make autocorrect handling reusable

From
JSJiamu Sun <39@barroit.sh>
Date
Apr 23, 2026, 01:37 UTC
Message-ID
<SY0P300MB080119BC1519FDE5DCD62088CE2A2@SY0P300MB0801.AUSP300.PROD.OUTLOOK.COM>
In-Reply-To
<SY0P300MB0801677A2A1E0FD38D06A841CE2A2@SY0P300MB0801.AUSP300.PROD.OUTLOOK.COM>

Move config parsing and prompt/delay handling into autocorrect.c and expose them in autocorrect.h. This makes autocorrect reusable regardless of which target links against it.

Signed-off-by: Jiamu Sun <39@barroit.sh>
---
 Makefile      |  1 +
 autocorrect.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++
 autocorrect.h | 16 ++++++++++++
 help.c        | 64 +++------------------------------------------
 meson.build   |  1 +
 5 files changed, 94 insertions(+), 60 deletions(-)
 create mode 100644 autocorrect.c
 create mode 100644 autocorrect.h
diff --git a/Makefile b/Makefile
index cedc234173e3..ced8a9b5abb4 100644
--- a/Makefile
+++ b/Makefile
@@ -1100,6 +1100,7 @@ LIB_OBJS += archive-tar.o
 LIB_OBJS += archive-zip.o
 LIB_OBJS += archive.o
 LIB_OBJS += attr.o
+LIB_OBJS += autocorrect.o
 LIB_OBJS += base85.o
 LIB_OBJS += bisect.o
 LIB_OBJS += blame.o
diff --git a/autocorrect.c b/autocorrect.c
new file mode 100644
index 000000000000..97145d3a53ce
--- /dev/null
+++ b/autocorrect.c
@@ -0,0 +1,72 @@
+#include "git-compat-util.h"
+#include "autocorrect.h"
+#include "config.h"
+#include "parse.h"
+#include "strbuf.h"
+#include "prompt.h"
+#include "gettext.h"
+
+static int parse_autocorrect(const char *value)
+{
+	switch (git_parse_maybe_bool_text(value)) {
+	case 1:
+		return AUTOCORRECT_IMMEDIATELY;
+	case 0:
+		return AUTOCORRECT_SHOW;
+	default: /* other random text */
+		break;
+	}
+
+	if (!strcmp(value, "prompt"))
+		return AUTOCORRECT_PROMPT;
+	if (!strcmp(value, "never"))
+		return AUTOCORRECT_NEVER;
+	if (!strcmp(value, "immediate"))
+		return AUTOCORRECT_IMMEDIATELY;
+	if (!strcmp(value, "show"))
+		return AUTOCORRECT_SHOW;
+
+	return 0;
+}
+
+void autocorrect_resolve_config(const char *var, const char *value,
+				const struct config_context *ctx, void *data)
+{
+	int *out = data;
+
+	if (!strcmp(var, "help.autocorrect")) {
+		int v = parse_autocorrect(value);
+
+		if (!v) {
+			v = git_config_int(var, value, ctx->kvi);
+			if (v < 0 || v == 1)
+				v = AUTOCORRECT_IMMEDIATELY;
+		}
+
+		*out = v;
+	}
+}
+
+void autocorrect_confirm(int autocorrect, const char *assumed)
+{
+	if (autocorrect == AUTOCORRECT_IMMEDIATELY) {
+		fprintf_ln(stderr,
+			   _("Continuing under the assumption that you meant '%s'."),
+			   assumed);
+	} 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'."),
+			   (float)autocorrect / 10.0, assumed);
+		sleep_millisec(autocorrect * 100);
+	}
+}
diff --git a/autocorrect.h b/autocorrect.h
new file mode 100644
index 000000000000..f5fadf9d9605
--- /dev/null
+++ b/autocorrect.h
@@ -0,0 +1,16 @@
+#ifndef AUTOCORRECT_H
+#define AUTOCORRECT_H
+
+#define AUTOCORRECT_SHOW (-4)
+#define AUTOCORRECT_PROMPT (-3)
+#define AUTOCORRECT_NEVER (-2)
+#define AUTOCORRECT_IMMEDIATELY (-1)
+
+struct config_context;
+
+void autocorrect_resolve_config(const char *var, const char *value,
+				const struct config_context *ctx, void *data);
+
+void autocorrect_confirm(int autocorrect, const char *assumed);
+
+#endif /* AUTOCORRECT_H */
diff --git a/help.c b/help.c
index 3e59d07c370b..ab619ed43c7a 100644
--- a/help.c
+++ b/help.c
@@ -22,6 +22,7 @@
 #include "repository.h"
 #include "alias.h"
 #include "utf8.h"
+#include "autocorrect.h"
 
 #ifndef NO_CURL
 #include "git-curl-compat.h" /* For LIBCURL_VERSION only */
@@ -541,34 +542,6 @@ struct help_unknown_cmd_config {
 	struct cmdnames aliases;
 };
 
-#define AUTOCORRECT_SHOW (-4)
-#define AUTOCORRECT_PROMPT (-3)
-#define AUTOCORRECT_NEVER (-2)
-#define AUTOCORRECT_IMMEDIATELY (-1)
-
-static int parse_autocorrect(const char *value)
-{
-	switch (git_parse_maybe_bool_text(value)) {
-		case 1:
-			return AUTOCORRECT_IMMEDIATELY;
-		case 0:
-			return AUTOCORRECT_SHOW;
-		default: /* other random text */
-			break;
-	}
-
-	if (!strcmp(value, "prompt"))
-		return AUTOCORRECT_PROMPT;
-	if (!strcmp(value, "never"))
-		return AUTOCORRECT_NEVER;
-	if (!strcmp(value, "immediate"))
-		return AUTOCORRECT_IMMEDIATELY;
-	if (!strcmp(value, "show"))
-		return AUTOCORRECT_SHOW;
-
-	return 0;
-}
-
 static int git_unknown_cmd_config(const char *var, const char *value,
 				  const struct config_context *ctx,
 				  void *cb)
@@ -577,17 +550,7 @@ static int git_unknown_cmd_config(const char *var, const char *value,
 	const char *subsection, *key;
 	size_t subsection_len;
 
-	if (!strcmp(var, "help.autocorrect")) {
-		int v = parse_autocorrect(value);
-
-		if (!v) {
-			v = git_config_int(var, value, ctx->kvi);
-			if (v < 0 || v == 1)
-				v = AUTOCORRECT_IMMEDIATELY;
-		}
-
-		cfg->autocorrect = v;
-	}
+	autocorrect_resolve_config(var, value, ctx, &cfg->autocorrect);
 
 	/* Also use aliases for command lookup */
 	if (!parse_config_key(var, "alias", &subsection, &subsection_len,
@@ -724,27 +687,8 @@ char *help_unknown_cmd(const char *cmd)
 			   _("WARNING: You called a Git command named '%s', "
 			     "which does not exist."),
 			   cmd);
-		if (cfg.autocorrect == AUTOCORRECT_IMMEDIATELY)
-			fprintf_ln(stderr,
-				   _("Continuing under the assumption that "
-				     "you meant '%s'."),
-				   assumed);
-		else if (cfg.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'."),
-				   (float)cfg.autocorrect/10.0, assumed);
-			sleep_millisec(cfg.autocorrect * 100);
-		}
+
+		autocorrect_confirm(cfg.autocorrect, assumed);
 
 		cmdnames_release(&cfg.aliases);
 		cmdnames_release(&main_cmds);
diff --git a/meson.build b/meson.build
index 11488623bfd8..be20d507826d 100644
--- a/meson.build
+++ b/meson.build
@@ -290,6 +290,7 @@ libgit_sources = [
   'archive-zip.c',
   'archive.c',
   'attr.c',
+  'autocorrect.c',
   'base85.c',
   'bisect.c',
   'blame.c',
-- 
2.54.0
Previous: Jiamu SunNext: Jiamu Sun
Message 85 of 96 in “parseopt: add subcommand autocorrection”
  1. 0/5 parseopt: add subcommand autocorrectionJiamu Sun, Mar 8, 2026
  2. 1/5 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Mar 8, 2026
  3. Junio C HamanoMar 8, 2026
  4. Jiamu SunMar 9, 2026
  5. 2/5 help: refactor command autocorrection handlingJiamu Sun, Mar 8, 2026
  6. Junio C HamanoMar 8, 2026
  7. Jiamu SunMar 9, 2026
  8. 3/5 parseopt: autocorrect mistyped subcommandsJiamu Sun, Mar 8, 2026
  9. Junio C HamanoMar 9, 2026
  10. Jiamu SunMar 9, 2026
  11. 4/5 parseopt: enable subcommand autocorrect for remote and notesJiamu Sun, Mar 8, 2026
  12. 5/5 help: add tests for subcommand autocorrectionJiamu Sun, Mar 8, 2026
  13. Aaron PlattnerMar 11, 2026
  14. Jiamu SunMar 11, 2026
  15. Junio C HamanoMar 12, 2026
  16. Junio C HamanoMar 8, 2026
  17. Jiamu SunMar 8, 2026
  18. 0/5 parseopt: add subcommand autocorrectionJiamu Sun, Mar 8, 2026
  19. 1/5 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Mar 8, 2026
  20. 2/5 help: refactor command autocorrection handlingJiamu Sun, Mar 8, 2026
  21. 3/5 parseopt: autocorrect mistyped subcommandsJiamu Sun, Mar 8, 2026
  22. 4/5 parseopt: enable subcommand autocorrect for remote and notesJiamu Sun, Mar 8, 2026
  23. 5/5 help: add tests for subcommand autocorrectionJiamu Sun, Mar 8, 2026
  24. 0/8 parseopt: add subcommand autocorrectionJiamu Sun, Mar 10, 2026
  25. 1/8 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Mar 10, 2026
  26. Karthik NayakMar 10, 2026
  27. Jiamu SunMar 11, 2026
  28. Junio C HamanoMar 11, 2026
  29. Junio C HamanoMar 11, 2026
  30. Jiamu SunMar 11, 2026
  31. 2/8 help: make autocorrect handling reusableJiamu Sun, Mar 10, 2026
  32. Karthik NayakMar 10, 2026
  33. Junio C HamanoMar 10, 2026
  34. Jiamu SunMar 11, 2026
  35. Jiamu SunMar 11, 2026
  36. 3/8 help: move tty check for autocorrection to autocorrect.cJiamu Sun, Mar 10, 2026
  37. Karthik NayakMar 10, 2026
  38. Jiamu SunMar 11, 2026
  39. Jiamu SunMar 12, 2026
  40. 4/8 autocorrect: rename AUTOCORRECT_SHOW to AUTOCORRECT_HINTONLYJiamu Sun, Mar 10, 2026
  41. Karthik NayakMar 10, 2026
  42. Jiamu SunMar 11, 2026
  43. 5/8 autocorrect: provide config resolution APIJiamu Sun, Mar 10, 2026
  44. Karthik NayakMar 10, 2026
  45. 6/8 parseopt: autocorrect mistyped subcommandsJiamu Sun, Mar 10, 2026
  46. Junio C HamanoMar 10, 2026
  47. Jiamu SunMar 11, 2026
  48. Jiamu SunMar 11, 2026
  49. Junio C HamanoMar 12, 2026
  50. Jiamu SunMar 12, 2026
  51. 7/8 parseopt: enable subcommand autocorrection for git-remote and git-notesJiamu Sun, Mar 10, 2026
  52. 8/8 help: add tests for subcommand autocorrectionJiamu Sun, Mar 10, 2026
  53. Junio C HamanoMar 11, 2026
  54. Jiamu SunMar 11, 2026
  55. 00/10 parseopt: add subcommand autocorrectionJiamu Sun, Mar 16, 2026
  56. 01/10 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Mar 16, 2026
  57. 02/10 help: make autocorrect handling reusableJiamu Sun, Mar 16, 2026
  58. 03/10 help: move tty check for autocorrection to autocorrect.cJiamu Sun, Mar 16, 2026
  59. 04/10 autocorrect: use mode and delay instead of magic numbersJiamu Sun, Mar 16, 2026
  60. 05/10 autocorrect: rename AUTOCORRECT_SHOW to AUTOCORRECT_HINTJiamu Sun, Mar 16, 2026
  61. 06/10 autocorrect: provide config resolution APIJiamu Sun, Mar 16, 2026
  62. 07/10 parseopt: autocorrect mistyped subcommandsJiamu Sun, Mar 16, 2026
  63. Junio C HamanoMar 16, 2026
  64. Jiamu SunMar 17, 2026
  65. Junio C HamanoApr 15, 2026
  66. Jiamu SunApr 16, 2026
  67. 08/10 parseopt: enable subcommand autocorrection for git-remote and git-notesJiamu Sun, Mar 16, 2026
  68. 09/10 parseopt: add tests for subcommand autocorrectionJiamu Sun, Mar 16, 2026
  69. 10/10 doc: document autocorrect APIJiamu Sun, Mar 16, 2026
  70. 00/10 parseopt: add subcommand autocorrectionJiamu Sun, Apr 22, 2026
  71. 02/10 help: make autocorrect handling reusableJiamu Sun, Apr 22, 2026
  72. 01/10 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Apr 22, 2026
  73. 03/10 help: move tty check for autocorrection to autocorrect.cJiamu Sun, Apr 22, 2026
  74. 04/10 autocorrect: use mode and delay instead of magic numbersJiamu Sun, Apr 22, 2026
  75. 05/10 autocorrect: rename AUTOCORRECT_SHOW to AUTOCORRECT_HINTJiamu Sun, Apr 22, 2026
  76. 06/10 autocorrect: provide config resolution APIJiamu Sun, Apr 22, 2026
  77. 07/10 parseopt: autocorrect mistyped subcommandsJiamu Sun, Apr 22, 2026
  78. 08/10 parseopt: enable subcommand autocorrection for git-remote and git-notesJiamu Sun, Apr 22, 2026
  79. 09/10 parseopt: add tests for subcommand autocorrectionJiamu Sun, Apr 22, 2026
  80. 10/10 doc: document autocorrect APIJiamu Sun, Apr 22, 2026
  81. Junio C HamanoApr 23, 2026
  82. Jiamu SunApr 23, 2026
  83. 00/10 parseopt: add subcommand autocorrectionJiamu Sun, Apr 23, 2026
  84. 01/10 parseopt: extract subcommand handling from parse_options_step()Jiamu Sun, Apr 23, 2026
  85. 02/10 help: make autocorrect handling reusableJiamu Sun, Apr 23, 2026
  86. 03/10 help: move tty check for autocorrection to autocorrect.cJiamu Sun, Apr 23, 2026
  87. 04/10 autocorrect: use mode and delay instead of magic numbersJiamu Sun, Apr 23, 2026
  88. 06/10 autocorrect: provide config resolution APIJiamu Sun, Apr 23, 2026
  89. 10/10 doc: document autocorrect APIJiamu Sun, Apr 23, 2026
  90. 05/10 autocorrect: rename AUTOCORRECT_SHOW to AUTOCORRECT_HINTJiamu Sun, Apr 23, 2026
  91. 07/10 parseopt: autocorrect mistyped subcommandsJiamu Sun, Apr 23, 2026
  92. 08/10 parseopt: enable subcommand autocorrection for git-remote and git-notesJiamu Sun, Apr 23, 2026
  93. 09/10 parseopt: add tests for subcommand autocorrectionJiamu Sun, Apr 23, 2026
  94. Junio C HamanoMay 11, 2026
  95. Jiamu SunMay 15, 2026
  96. Junio C HamanoJun 6, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.