{"thread":{"id":"58545","subject":"Bash completion for git aliases containing nested subcommands","startedAt":"2022-10-03T11:46:00Z","lastAt":"2022-10-05T10:05:31Z","messageCount":7,"participants":["Tim Jaacks","SZEDER Gábor","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"464070","messageId":"em8d7ca854-51a3-4cb5-aaf9-30ed37b4194b@acer-switch-tim","threadId":"58545","inReplyTo":null,"subject":"Bash completion for git aliases containing nested subcommands","fromName":"Tim Jaacks","fromEmail":"timjaacks@posteo.de","sentAt":"2022-10-03T11:45:51Z","receivedAt":"2022-10-03T11:46:00Z","isPatch":false,"sender":{"key":"timjaacks@posteo.de","avatar":"https://gravatar.com/avatar/fba750ff838a695ba487ac36c9aa8f053ca8c5d064e0eff659da57774cb212d8?d=mp&s=160"},"body":"Hello everyone,\n\nI have set up the following alias in my .gitconfig file:\n\n[alias]\n     ss = stash show\n\nUnfortunately bash completion does not work correctly on this alias. \nWhen I type \"git ss <TAB>\", I get:\n\n$ git ss <TAB>\napply clear drop pop show\nbranch create list push\n\nWhich is obviously the completion for \"git stash\" instead of \"git stash \nshow\".\n\nWith the original command I get the list of available stashes:\n\n$ git stash show <TAB>\nstash@{0} stash@{1}\n\nIs there a way to get the completion on the alias behave like on the \noriginal command?\n\nI am on Ubuntu 20.04 and using the distro's default git completions.\n\nKind regards,\nTim\n\n"},{"id":"464075","messageId":"20221003142437.GB7659@szeder.dev","threadId":"58545","inReplyTo":"em8d7ca854-51a3-4cb5-aaf9-30ed37b4194b@acer-switch-tim","subject":"Re: Bash completion for git aliases containing nested subcommands","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2022-10-03T14:24:37Z","receivedAt":"2022-10-03T14:24:47Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Mon, Oct 03, 2022 at 11:45:51AM +0000, Tim Jaacks wrote:\n> Hello everyone,\n> \n> I have set up the following alias in my .gitconfig file:\n> \n> [alias]\n>     ss = stash show\n> \n> Unfortunately bash completion does not work correctly on this alias. When I\n> type \"git ss <TAB>\", I get:\n> \n> $ git ss <TAB>\n> apply clear drop pop show\n> branch create list push\n> \n> Which is obviously the completion for \"git stash\" instead of \"git stash\n> show\".\n> \n> With the original command I get the list of available stashes:\n> \n> $ git stash show <TAB>\n> stash@{0} stash@{1}\n> \n> Is there a way to get the completion on the alias behave like on the\n> original command?\n\nIn general: no.  Our Bash completion script is organized as one\n_git_cmd() function for each git supported command.  If a command has\nsubcommands, then its completion function looks for any of its\nsubcommands amond the words on the command line, and takes the\nappropriate action, which is usually executing a particular arm of a\ncase statement.  The two main issues are that in case of such an alias\nthere is no subcommand (\"show\") on the command line, and there is no\ndedicated function to handle only the completion for 'git stash show'.\n\nHowever, it is possible to make completion work for your particular\nalias by using our completion script's extension mechanism that allows\nusers to specify completion functions to their own git commands.  If\nyou type 'git foo <TAB>' and there is a _git_foo() function in your\nshell's environment, then the completion script will invoke that\nfunction to perform completion; this works not only for commands but\nfor aliases as well.  So for your alias you only need to \"borrow\" all\nthe \"show\"-subcommand-specific case arms from _git_stash() and place\nthem in a _git_ss() function, e.g. like this:\n\n_git_ss () {\n\tcase \"$cur\" in\n\t--*)\n\t\t__gitcomp_builtin stash_show \"$__git_diff_common_options\"\n\t\t;;\n\t*)\n\t\t__gitcomp_nl \"$(__git stash list | sed -n -e 's/:.*//p')\"\n\t\t;;\n\tesac\n}\n\nAdd it to your ~/.bashrc, or to a separate file that you source from\nyour .bashrc;  If you use bash-completion, then you don't even have to\ntouch you .bashrc: save that function to a file 'git-ss' (dash, not\nunderscore!) in one of the directories scanned by bash-completion\n($BASH_COMPLETION_USER_DIR, ~/.local/share/bash-completion/completions\nor its XDG_DATA_HOME-equivalent) and it will be auto-loaded as needed.\n\nThis approach should work just as well for any other similar \"command\nsubcommand\" alias that you might have; with the downside that you'll\nhave to write all these functions yourself.\n\n> I am on Ubuntu 20.04 and using the distro's default git completions.\n> \n> Kind regards,\n> Tim\n> \n"},{"id":"464076","messageId":"1839e676218.285a.8a94aeaa49923dfb9a7d55a303990d0a@posteo.de","threadId":"58545","inReplyTo":"1839e62f930.285a.8a94aeaa49923dfb9a7d55a303990d0a@posteo.de","subject":"Re: Bash completion for git aliases containing nested subcommands","fromName":"Tim Jaacks","fromEmail":"timjaacks@posteo.de","sentAt":"2022-10-03T15:11:59Z","receivedAt":"2022-10-03T15:12:08Z","isPatch":false,"sender":{"key":"timjaacks@posteo.de","avatar":"https://gravatar.com/avatar/fba750ff838a695ba487ac36c9aa8f053ca8c5d064e0eff659da57774cb212d8?d=mp&s=160"},"body":"Hi Gabor,\n\nthanks a lot for your detailed reply! I tried that and it works, thank you.\n\nJust another small follow-up question, out of curiosity: I noticed that zsh \nhandles the alias completion correctly out of the box (using the exact same \ngitconfig file). I don't have any dedicated zsh completion file, just \ninstalled zsh via apt. Do you know why and how zsh handles this differently?\n\nKind regards,\nTim\n\nAm 3. Oktober 2022 16:24:41 schrieb SZEDER Gábor <szeder.dev@gmail.com>:\n\n> On Mon, Oct 03, 2022 at 11:45:51AM +0000, Tim Jaacks wrote:\n>> Hello everyone,\n>>\n>> I have set up the following alias in my .gitconfig file:\n>>\n>> [alias]\n>> ss = stash show\n>>\n>> Unfortunately bash completion does not work correctly on this alias. When I\n>> type \"git ss <TAB>\", I get:\n>>\n>> $ git ss <TAB>\n>> apply clear drop pop show\n>> branch create list push\n>>\n>> Which is obviously the completion for \"git stash\" instead of \"git stash\n>> show\".\n>>\n>> With the original command I get the list of available stashes:\n>>\n>> $ git stash show <TAB>\n>> stash@{0} stash@{1}\n>>\n>> Is there a way to get the completion on the alias behave like on the\n>> original command?\n>\n> In general: no.  Our Bash completion script is organized as one\n> _git_cmd() function for each git supported command.  If a command has\n> subcommands, then its completion function looks for any of its\n> subcommands amond the words on the command line, and takes the\n> appropriate action, which is usually executing a particular arm of a\n> case statement.  The two main issues are that in case of such an alias\n> there is no subcommand (\"show\") on the command line, and there is no\n> dedicated function to handle only the completion for 'git stash show'.\n>\n> However, it is possible to make completion work for your particular\n> alias by using our completion script's extension mechanism that allows\n> users to specify completion functions to their own git commands.  If\n> you type 'git foo <TAB>' and there is a _git_foo() function in your\n> shell's environment, then the completion script will invoke that\n> function to perform completion; this works not only for commands but\n> for aliases as well.  So for your alias you only need to \"borrow\" all\n> the \"show\"-subcommand-specific case arms from _git_stash() and place\n> them in a _git_ss() function, e.g. like this:\n>\n> _git_ss () {\n> case \"$cur\" in\n> --*)\n> __gitcomp_builtin stash_show \"$__git_diff_common_options\"\n> ;;\n> *)\n> __gitcomp_nl \"$(__git stash list | sed -n -e 's/:.*//p')\"\n> ;;\n> esac\n> }\n>\n> Add it to your ~/.bashrc, or to a separate file that you source from\n> your .bashrc;  If you use bash-completion, then you don't even have to\n> touch you .bashrc: save that function to a file 'git-ss' (dash, not\n> underscore!) in one of the directories scanned by bash-completion\n> ($BASH_COMPLETION_USER_DIR, ~/.local/share/bash-completion/completions\n> or its XDG_DATA_HOME-equivalent) and it will be auto-loaded as needed.\n>\n> This approach should work just as well for any other similar \"command\n> subcommand\" alias that you might have; with the downside that you'll\n> have to write all these functions yourself.\n>\n>> I am on Ubuntu 20.04 and using the distro's default git completions.\n>>\n>> Kind regards,\n>> Tim\n\n\n\n"},{"id":"464078","messageId":"20221003154354.GC7659@szeder.dev","threadId":"58545","inReplyTo":"1839e62f930.285a.8a94aeaa49923dfb9a7d55a303990d0a@posteo.de","subject":"Re: Bash completion for git aliases containing nested subcommands","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2022-10-03T15:43:54Z","receivedAt":"2022-10-03T15:44:01Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Mon, Oct 03, 2022 at 03:07:10PM +0000, Tim Jaacks wrote:\n> Hi Gabor,\n> \n> thanks a lot for your detailed reply! I tried that and it works, thank you.\n> \n> Just another small follow-up question, out of curiosity: I noticed that zsh\n> handles the alias completion correctly out of the box (using the exact same\n> gitconfig file). I don't have any dedicated zsh completion file, just\n> installed zsh via apt. Do you know why and how zsh handles this differently?\n\nThere are not only one but two Zsh completion scripts for Git:\n\n  - Ours, which is essentially a wrapper around our Bash completion\n    script, so it shouldn't work for your alias, and it has to be\n    installed manually, so I doubt that this is what you tried.\n\n  - One that's shipped with Zsh, with all the bells and whistles that\n    Zsh's completion system offers.  It's not maintained by the Git\n    project, and I have no idea how it works, but I guess it works\n    sufficiently differently to make your use case work.\n\n"},{"id":"464101","messageId":"YzthDLdqeQx6bEcj@coredump.intra.peff.net","threadId":"58545","inReplyTo":"20221003142437.GB7659@szeder.dev","subject":"Re: Bash completion for git aliases containing nested subcommands","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2022-10-03T22:24:12Z","receivedAt":"2022-10-03T22:28:11Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Oct 03, 2022 at 04:24:37PM +0200, SZEDER Gábor wrote:\n\n> > Is there a way to get the completion on the alias behave like on the\n> > original command?\n> \n> In general: no.  Our Bash completion script is organized as one\n> _git_cmd() function for each git supported command.  If a command has\n> subcommands, then its completion function looks for any of its\n> subcommands amond the words on the command line, and takes the\n> appropriate action, which is usually executing a particular arm of a\n> case statement.  The two main issues are that in case of such an alias\n> there is no subcommand (\"show\") on the command line, and there is no\n> dedicated function to handle only the completion for 'git stash show'.\n\nIt feels like this ought to be able to work, for the same reason that\n\"git stash show <TAB>\" works. In the non-aliased case, we call into\n_git_stash(), and it sees that \"show\" is already there on the command\nline. But in the aliased case, we know \"show\" is part of the alias but\nthrow away that information completely, and never feed it to\n_git_stash() at all.\n\nI think we could do something like the patch below, though I suspect\nthere are some dragons with more complicated aliases. I wonder if\n__git_aliased_command() needs to be more careful with distinguishing\npure-git-command aliases from the complexity of \"!\" aliases. Or maybe\nthe alias stuff is all best-effort enough that this doesn't make\nanything worse.\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex ba5c395d2d..f68bfcbf05 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -1148,10 +1148,13 @@ __git_aliased_command ()\n \t\tcur=\n \n \t\tfor word in $cmdline; do\n+\t\t\tif test -n \"$cur\"; then\n+\t\t\t\texpansion_words+=(\"$word\")\n+\t\t\t\tcontinue\n+\t\t\tfi\n \t\t\tcase \"$word\" in\n \t\t\t\\!gitk|gitk)\n \t\t\t\tcur=\"gitk\"\n-\t\t\t\tbreak\n \t\t\t\t;;\n \t\t\t\\!*)\t: shell command alias ;;\n \t\t\t-*)\t: option ;;\n@@ -1163,14 +1166,13 @@ __git_aliased_command ()\n \t\t\t\\'*)\t: skip opening quote after sh -c ;;\n \t\t\t*)\n \t\t\t\tcur=\"$word\"\n-\t\t\t\tbreak\n \t\t\tesac\n \t\tdone\n \tdone\n \n \tcur=$last\n \tif [[ \"$cur\" != \"$1\" ]]; then\n-\t\techo \"$cur\"\n+\t\texpansion=$cur\n \tfi\n }\n \n@@ -3507,9 +3509,13 @@ __git_main ()\n \n \t__git_complete_command \"$command\" && return\n \n-\tlocal expansion=$(__git_aliased_command \"$command\")\n+\t# __git_aliased_command now writes to these\n+\tlocal expansion\n+\tlocal expansion_words\n+\t__git_aliased_command \"$command\"\n \tif [ -n \"$expansion\" ]; then\n-\t\twords[1]=$expansion\n+\t\twords=(\"${words[0]}\" \"$expansion\" \"${expansion_words[@]}\" \"${words[@]:2}\")\n+\t\tcword=$((cword + ${#expansion_words[@]}))\n \t\t__git_complete_command \"$expansion\"\n \tfi\n }\n\nBy the way, you'll notice that the splice into \"words\" happens right\nat words[1]. That matches the earlier code that just touches words[1].\nBut I suspect that isn't right. If we're completing \"git -p foo\", for\nexample, then the command is at word[2]. I don't know if this causes any\nbugs, since we get to the right completion function based on $expansion,\nnot any value in $words. But presumably it should be __git_cmd_idx?\n\n-Peff\n"},{"id":"464119","messageId":"em34822e40-c62f-43aa-bc9b-2a87e9033ee8@acer-switch-tim","threadId":"58545","inReplyTo":"YzthDLdqeQx6bEcj@coredump.intra.peff.net","subject":"Re: Bash completion for git aliases containing nested subcommands","fromName":"Tim Jaacks","fromEmail":"timjaacks@posteo.de","sentAt":"2022-10-04T11:21:11Z","receivedAt":"2022-10-04T11:21:36Z","isPatch":false,"sender":{"key":"timjaacks@posteo.de","avatar":"https://gravatar.com/avatar/fba750ff838a695ba487ac36c9aa8f053ca8c5d064e0eff659da57774cb212d8?d=mp&s=160"},"body":"Hi Peff,\n\nthanks a lot for your reply.\n\n > It feels like this ought to be able to work, for the same reason that\n > \"git stash show <TAB>\" works. In the non-aliased case, we call into\n > _git_stash(), and it sees that \"show\" is already there on the command\n > line. But in the aliased case, we know \"show\" is part of the alias but\n > throw away that information completely, and never feed it to\n > _git_stash() at all.\n\nI actually got the same impression when looking into the \n__git_aliased_command implementation. I just wasn't familiar enough with \nthe code to actually come up with a solution. So thanks for your patch, \nthat makes my use-case work indeed! Do you think there's a chance to get \nthis upstream?\n\nBest wishes,\nTim\n\n"},{"id":"464221","messageId":"20221005100518.GA1718@szeder.dev","threadId":"58545","inReplyTo":"YzthDLdqeQx6bEcj@coredump.intra.peff.net","subject":"Re: Bash completion for git aliases containing nested subcommands","fromName":"SZEDER Gábor","fromEmail":"szeder.dev@gmail.com","sentAt":"2022-10-05T10:05:18Z","receivedAt":"2022-10-05T10:05:31Z","isPatch":false,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"On Mon, Oct 03, 2022 at 06:24:12PM -0400, Jeff King wrote:\n> On Mon, Oct 03, 2022 at 04:24:37PM +0200, SZEDER Gábor wrote:\n> \n> > > Is there a way to get the completion on the alias behave like on the\n> > > original command?\n> > \n> > In general: no.  Our Bash completion script is organized as one\n> > _git_cmd() function for each git supported command.  If a command has\n> > subcommands, then its completion function looks for any of its\n> > subcommands amond the words on the command line, and takes the\n> > appropriate action, which is usually executing a particular arm of a\n> > case statement.  The two main issues are that in case of such an alias\n> > there is no subcommand (\"show\") on the command line, and there is no\n> > dedicated function to handle only the completion for 'git stash show'.\n> \n> It feels like this ought to be able to work, for the same reason that\n> \"git stash show <TAB>\" works. In the non-aliased case, we call into\n> _git_stash(), and it sees that \"show\" is already there on the command\n> line. But in the aliased case, we know \"show\" is part of the alias but\n> throw away that information completely, and never feed it to\n> _git_stash() at all.\n> \n> I think we could do something like the patch below, though I suspect\n> there are some dragons with more complicated aliases. I wonder if\n> __git_aliased_command() needs to be more careful with distinguishing\n> pure-git-command aliases from the complexity of \"!\" aliases. Or maybe\n> the alias stuff is all best-effort enough that this doesn't make\n> anything worse.\n> \n> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> index ba5c395d2d..f68bfcbf05 100644\n> --- a/contrib/completion/git-completion.bash\n> +++ b/contrib/completion/git-completion.bash\n> @@ -1148,10 +1148,13 @@ __git_aliased_command ()\n>  \t\tcur=\n>  \n>  \t\tfor word in $cmdline; do\n> +\t\t\tif test -n \"$cur\"; then\n> +\t\t\t\texpansion_words+=(\"$word\")\n> +\t\t\t\tcontinue\n> +\t\t\tfi\n>  \t\t\tcase \"$word\" in\n>  \t\t\t\\!gitk|gitk)\n>  \t\t\t\tcur=\"gitk\"\n> -\t\t\t\tbreak\n>  \t\t\t\t;;\n>  \t\t\t\\!*)\t: shell command alias ;;\n>  \t\t\t-*)\t: option ;;\n> @@ -1163,14 +1166,13 @@ __git_aliased_command ()\n>  \t\t\t\\'*)\t: skip opening quote after sh -c ;;\n>  \t\t\t*)\n>  \t\t\t\tcur=\"$word\"\n> -\t\t\t\tbreak\n>  \t\t\tesac\n>  \t\tdone\n>  \tdone\n>  \n>  \tcur=$last\n>  \tif [[ \"$cur\" != \"$1\" ]]; then\n> -\t\techo \"$cur\"\n> +\t\texpansion=$cur\n>  \tfi\n>  }\n>  \n> @@ -3507,9 +3509,13 @@ __git_main ()\n>  \n>  \t__git_complete_command \"$command\" && return\n>  \n> -\tlocal expansion=$(__git_aliased_command \"$command\")\n> +\t# __git_aliased_command now writes to these\n> +\tlocal expansion\n> +\tlocal expansion_words\n> +\t__git_aliased_command \"$command\"\n>  \tif [ -n \"$expansion\" ]; then\n> -\t\twords[1]=$expansion\n> +\t\twords=(\"${words[0]}\" \"$expansion\" \"${expansion_words[@]}\" \"${words[@]:2}\")\n> +\t\tcword=$((cword + ${#expansion_words[@]}))\n>  \t\t__git_complete_command \"$expansion\"\n>  \tfi\n>  }\n\nOh, that's an interesting idea.  I think it could help other cases as\nwell, e.g. in case of an alias like 'pg = push github', after 'git pg\n<TAB>' the completion script would list remotes, but we'd actually\nneed refspecs.\n\nI like the idea of inserting the expansion of git aliases into the\nwords array, as if they were present on the command line, and as far\nas I can tell it's safe to do so, after all it's pretty much matches\nwhat git itself does when expanding such an alias.  However, I'm\nworried about doing the same for shell aliases, because those can\nexpand to just about anything, including e.g. some filtering pipe\nstages after the aliased git command, that will be added to\n$expanded_words, and could cause all kinds of confusion in various\ncompletion functions.\n\n> By the way, you'll notice that the splice into \"words\" happens right\n> at words[1]. That matches the earlier code that just touches words[1].\n> But I suspect that isn't right. If we're completing \"git -p foo\", for\n> example, then the command is at word[2]. I don't know if this causes any\n> bugs, since we get to the right completion function based on $expansion,\n> not any value in $words. But presumably it should be __git_cmd_idx?\n\nYeah, that should definitely be words[__git_cmd_idx]=...\n\nAs far as dispatching to the right _git_cmd() function, it doesn't\nmatter whether we overwrite the $words at index 1 or $__git_cmd_idx;\nbut for that we wouldn't have to overwrite words[1] at all.\n\nHowever, there is (AFAIK only) one completion helper function\n__git_complete_remote_or_refspec() which does what its name suggests,\nbut behaves slightly differently whether it's invoked for 'git push',\n'pull', 'fetch', or 'remote'.  Alas, this function doesn't have a\nparameter that its caller could use to tell how to behave, but it\ndecides what to do by looking at the word on the command line\ncontaining the git command, i.e. ${words[__git_cmd_idx]}.  So I guess\nwith a simple alias like 'p = push' completing 'git -c var=val p\norigin <TAB>' would misbehave, because that 'words[1]=...' would\noverwrite '-c', so the completion function would see the words 'git\npush var=val p origin', but inside __git_complete_remote_or_refspec()\nthat ${words[__git_cmd_idx]} would be 'p', and without seeing one of\nthe supported commands that function won't list refspecs.\n\n\nOne \"dragon with more complicated aliases\" might be that\n__git_aliased_command() doesn't know about git's --options, in\nparticular which --options take a mandatory argument.  Yesterday I\nlearned that, quoting the description of 'alias.*' from git-config\nmanpage, \"the first word of an alias does not necessarily have to be a\ncommand.  It can be a command-line option that will be passed into the\ninvocation of git.\"  Now, if I understand you patch correctly, then it\nwould only add those words to $expansion_words that follow the aliased\ncommand, e.g. with an alias like 'pg = -c var=val push github' when\ncompleting 'git pg <TAB>', then $words would contain 'git push\ngithub'.  On one hand this is good, because $__git_cmd_idx would index\nthe right element of the $words array even after inserting the\nexpansion of the alias.  OTOH, it can be bad, because with an alias\nlike 'pg = -C path push github' __git_aliased_command() would believe\nthat the command is \"path\", not \"push\".\n\nAnother dragon might be the support for recursive aliases: will the\norder of words in $expansion_words match the order that git uses when\nexpanding aliases recursively?\n\n"}]}