@matheusbsbs / matheus-bagliano / commits / f0e90716d4
Fork 0

Add help.autocorrect to enable/disable autocorrecting

It is off(0) by default, to avoid scaring people unless they asked to. If set to a non-0 value, wait for that amount of deciseconds before running the corrected command. Suggested by Junio, so he has a chance to hit Ctrl-C. Signed-off-by: Alex Riesen <raa.lkml@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Alex Riesen committed Aug 31, 2008 at 15:54 UTC f0e90716d47b429284702b75425a247c9fc41adb
2 files changed +27 -1
Documentation/config.txt
+9
@@ -790,6 +790,15 @@ help.format::
790 Values 'man', 'info', 'web' and 'html' are supported. 'man' is
791 the default. 'web' and 'html' are the same.
792
793 +help.autocorrect::
794 + Automatically correct and execute mistyped commands after
795 + waiting for the given number of deciseconds (0.1 sec). If more
796 + than one command can be deduced from the entered text, nothing
797 + will be executed. If the value of this option is negative,
798 + the corrected command will be executed immediately. If the
799 + value is 0 - the command will be just shown but not executed.
800 + This is the default.
801 +
802 http.proxy::
803 Override the HTTP proxy, normally configured using the 'http_proxy'
804 environment variable (see linkgit:curl[1]). This can be overridden
help.c
+18 -1
@@ -268,6 +268,16 @@ int is_in_cmdlist(struct cmdnames *c, const char *s)
268 return 0;
269 }
270
271 +static int autocorrect;
272 +
273 +static int git_unknown_cmd_config(const char *var, const char *value, void *cb)
274 +{
275 + if (!strcmp(var, "help.autocorrect"))
276 + autocorrect = git_config_int(var,value);
277 +
278 + return git_default_config(var, value, cb);
279 +}
280 +
281 static int levenshtein_compare(const void *p1, const void *p2)
282 {
283 const struct cmdname *const *c1 = p1, *const *c2 = p2;
@@ -285,6 +295,8 @@ const char *help_unknown_cmd(const char *cmd)
295 memset(&main_cmds, 0, sizeof(main_cmds));
296 memset(&other_cmds, 0, sizeof(main_cmds));
297
298 + git_config(git_unknown_cmd_config, NULL);
299 +
300 load_command_list("git-", &main_cmds, &other_cmds);
301
302 ALLOC_GROW(main_cmds.names, main_cmds.cnt + other_cmds.cnt,
@@ -309,7 +321,7 @@ const char *help_unknown_cmd(const char *cmd)
321 n = 1;
322 while (n < main_cmds.cnt && best_similarity == main_cmds.names[n]->len)
323 ++n;
312 - if (n == 1) {
324 + if (autocorrect && n == 1) {
325 const char *assumed = main_cmds.names[0]->name;
326 main_cmds.names[0] = NULL;
327 clean_cmdnames(&main_cmds);
@@ -317,6 +329,11 @@ const char *help_unknown_cmd(const char *cmd)
329 "which does not exist.\n"
330 "Continuing under the assumption that you meant '%s'\n",
331 cmd, assumed);
332 + if (autocorrect > 0) {
333 + fprintf(stderr, "in %0.1f seconds automatically...\n",
334 + (float)autocorrect/10.0);
335 + poll(NULL, 0, autocorrect * 100);
336 + }
337 return assumed;
338 }
339