开发者

How can I make git do the "did you mean" suggestion?

开发者 https://www.devze.com 2023-02-14 20:38 出处:网络
I type git puhs And git says: kristian@office:~/myrepo$ git puhs git: 开发者_StackOverflow\'puhs\' is not a git command. See \'git --help\'

I type

git puhs

And git says:

kristian@office:~/myrepo$ git puhs
git: 开发者_StackOverflow'puhs' is not a git command. See 'git --help'

Did you mean this?
      push

What is the config setting to make git just do the suggested command if it only has one suggestion ?


According to git-config(1), you want to set help.autocorrect appropriately. For example, git config --global help.autocorrect 5 will make it wait half a second before running the command so you can see the message first.


The autocorrect is nice, but my OCD-self needs a little more control over what's going on. So, I wrote a straightforward script that just chooses the first suggestion provided by git. You run the script after the failed command and use the built in bash history substitution "bang bang" syntax. Also, if you are typing something that could possibly have more than one command, this command lets you choose one other than the first option.

It would look something like this,

kristian@office:~/myrepo$ git puhs
git: 'puhs' is not a git command. See 'git --help'

Did you mean this?
      push

kristian@office:~/myrepo$ idid !!
Counting objects: 18, done.
Delta compression using up to 32 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (10/10), 1.17 KiB, done.
Total 10 (delta 6), reused 0 (delta 0)

Plus, it's fun to type anything with two exclamation points. So bonus for that.

Here's a gist with my script


As an alternative to help.autocorrect: if you make the same typos all the time, you can create aliases for them in your .gitconfig file

[alias]
    puhs = push

(I do this with shell aliases too, where I can never seem to type mkae^H^H^H^Hmake correctly.)


Also take a look at thefuck

It can correct typos, and also perform suggestions. Not just limited to git.


Is there a way to make git prompt before correcting, similar to zsh?
(e.g., "correct 'puhs' to 'push' [yn]?")

That was asked in 2015. 6 years later:

With Git 2.34 (Q4 2021), the logic for auto-correction of misspelt subcommands learned to go interactive when the help.autocorrect configuration variable is set to 'prompt'.

See commit dc66e3c (14 Aug 2021) by Azeem Bande-Ali (azeemba).
(Merged by Junio C Hamano -- gitster -- in commit 96ac07f, 10 Sep 2021)

help.c: help.autocorrect=prompt waits for user action

Signed-off-by: Azeem Bande-Ali

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

Based on original patch by David Barr (from... Sept. 2010!).

git config now includes in its man page:

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.

Note that, with Git 2.35 (Q1 2022), when proposing to run a command, the prompt has been updated to match what the others similar questions create.

It is no longer:

Run '%s' instead? (y/N)

But:

Run '%s' instead [y/N]? 

See commit 0fc8ed1 (15 Dec 2021) by Kashav Madan (kashav).
(Merged by Junio C Hamano -- gitster -- in commit a165484, 05 Jan 2022)

help: make auto-correction prompt more consistent

Signed-off-by: Kashav Madan

There are three callsites of git_prompt() helper function that ask a "yes/no" question to the end user, but one of them in help.c that asks if the suggested auto-correction is OK, which is given when the user makes a possible typo in a Git subcommand name, is formatted differently from the others.

Update the format string to make the prompt string look more consistent.

0

精彩评论

暂无评论...
验证码 换一张
取 消