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

Re: [PATCH v2 2/2] completion: add a GIT_COMPLETION_SHOW_ALL_COMMANDS

From
SZEDER Gábor <szeder.dev@gmail.com>
Date
Feb 6, 2022, 22:47 UTC
Message-ID
<20220206224740.GD1936@szeder.dev>
In-Reply-To
<xmqq8runiwow.fsf@gitster.g>
On Sun, Feb 06, 2022 at 12:09:35PM -0800, Junio C Hamano wrote:
Show 20 quoted lines
> SZEDER Gábor <szeder.dev@gmail.com> writes:
> 
> > To complete only rarely used plumbing commands in a non-intrusive way,
> > in my experience, it's best to first attempt to complete only
> > porcelains and aliases, and fall back to complete all commands,
> > plumbing included, only when no porcelains match the current word to
> > be completed.  E.g.:
> >
> >   $ git d<TAB>
> >   describe   diff   difftool
> >   $ git diff-<TAB>
> >   diff-files   diff-index   diff-tree
> 
> So after getting
> 
>     $ git diff<TAB>
>     diff difftool
> 
> you _have_ to know, if you are not happy with these two, that the
> next letter in the name of the command you forgot is a dash,

Yeah, it can only save a couple of keystrokes, but it's not really useful when you want to use it to jog your memory. I naively assumed that if you use plumbing, then you know what you are doing and which command you want to execute :)

Show 22 quoted lines
> to be
> able to say
> 
>     $ git diff-<TAB>
> 
> which is a bit unfortunate, but I agree that it is much nicer than
> getting all the plumbing when trying to complete "git d<TAB>".
> 
> I wonder if we can do better, and teach the completion logic an
> ability to say this: "I gave 'diff and difftool' after being asked
> for 'git diff<TAB>' and then the user is asking the same again
> without choosing either. Perhaps I should add less frequent one to
> the mix"?
> 
> I.e. the end-user session may look like
> 
>     $ git diff<TAB>
>     diff difftool
>     $ git diff<TAB>
>     diff difftool diff-files diff-index diff-tree
> 
> ?

Hrm, interesting, but dunno. When completing commands in __git_main() we could save the current word to be completed in a variable, and when completing commands the next time we could check whether the current word is still the same, and then include plumbing as well. However, when the current word can't be uniquely completed, then we have to press TAB twice to get the list of possible completion, so we have to preserve the last two current words, and only list plumbing when both match.

  ---   >8   ---
diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 377d6c5494..cda6b48c4e 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -3455,8 +3455,17 @@ __git_main ()
 			then
 				__gitcomp "$GIT_TESTING_PORCELAIN_COMMAND_LIST"
 			else
-				__gitcomp "$(__git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
+				echo >>/tmp/COMPLOG "__git_main() cur: '$cur' last: '$__git_last_command_cur' last2: '$__git_last_command_cur2'"
+				if test "$cur" = "$__git_last_command_cur" &&
+				   test "$cur" = "$__git_last_command_cur2"
+				then
+					__gitcomp "$(__git --list-cmds=main,others,nohelpers,alias,list-complete)"
+				else
+					__gitcomp "$(__git --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config)"
+				fi
 			fi
+			__git_last_command_cur2=$__git_last_command_cur
+			__git_last_command_cur=$cur
 			;;
 		esac
 		return

  ---   8<   ---

Superficial testing shows that it appears to work in common cases, but
we'll have to think it through when and how to clear these variables.
E.g.:

  $ git d<TAB><TAB>
  describe   diff   difftool
  # Oh, but I wanted to disable the pager
  $ git --no-p<TAB>
  # this completes the option uniquely
  $ git --no-pager d<TAB><TAB>
  daemon     diff         diff-index   diff-tree
  describe   diff-files   difftool

I think here it should list only porcelains, but because both those
last_cur variables still contain 'd', it lists plumbing as well.

And, of course, 'git <TAB><TAB>' in a newly started terminal should
list only porcelains, but it lists plumbing as well.
Previous: Junio C HamanoNext: Junio C Hamano
Message 16 of 17 in “Some sub-commands can't be completed by TAB key.”
  1. Hongyi ZhaoJan 22, 2022
  2. Johannes SixtJan 22, 2022
  3. Hongyi ZhaoJan 23, 2022
  4. Philippe BlainJan 23, 2022
  5. Junio C HamanoJan 23, 2022
  6. Hongyi ZhaoJan 24, 2022
  7. João Victor BonfimJan 24, 2022
  8. Junio C HamanoJan 25, 2022
  9. completion: add a GIT_COMPLETION_SHOW_ALL_COMMANDSÆvar Arnfjörð Bjarmason, Jan 25, 2022
  10. Junio C HamanoJan 26, 2022
  11. 0/2 completion: add a GIT_COMPLETION_SHOW_ALL_COMMANDSÆvar Arnfjörð Bjarmason, Feb 2, 2022
  12. 1/2 completion tests: re-source git-completion.bash in a subshellÆvar Arnfjörð Bjarmason, Feb 2, 2022
  13. 2/2 completion: add a GIT_COMPLETION_SHOW_ALL_COMMANDSÆvar Arnfjörð Bjarmason, Feb 2, 2022
  14. SZEDER GáborFeb 6, 2022
  15. Junio C HamanoFeb 6, 2022
  16. SZEDER GáborFeb 6, 2022
  17. Junio C HamanoFeb 7, 2022

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.