{"thread":{"id":"39926","subject":"[RFC/PATCH] contrib: teach completion about git-worktree options and arguments","startedAt":"2015-07-23T20:49:10Z","lastAt":"2015-08-21T21:59:15Z","messageCount":6,"participants":["Eric Sunshine","Junio C Hamano","SZEDER Gábor"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"266686","messageId":"1437684550-21884-1-git-send-email-sunshine@sunshineco.com","threadId":"39926","inReplyTo":null,"subject":"[RFC/PATCH] contrib: teach completion about git-worktree options and arguments","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-07-23T20:49:10Z","receivedAt":"2015-07-23T20:49:10Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"Complete subcommands 'add' and 'prune', as well as their respective\noptions --force, --detach, --dry-run, --verbose, and --expire. Also\ncomplete 'refname' in \"git worktree add [-b <newbranch>] <path>\n<refname>\".\n\nSigned-off-by: Eric Sunshine <sunshine@sunshineco.com>\n---\n\nThis is RFC since this is my first foray into the Git completion script,\nand there are likely better and more idiomatic ways to accomplish this.\n\n contrib/completion/git-completion.bash | 32 ++++++++++++++++++++++++++++++++\n 1 file changed, 32 insertions(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex c97c648..07c34ef 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -2564,6 +2564,38 @@ _git_whatchanged ()\n \t_git_log\n }\n \n+_git_worktree ()\n+{\n+\tlocal subcommands='add prune'\n+\tlocal subcommand=\"$(__git_find_on_cmdline \"$subcommands\")\"\n+\tlocal c=2 n=0 refpos=2\n+\tif [ -z \"$subcommand\" ]; then\n+\t\t__gitcomp \"$subcommands\"\n+\telse\n+\t\tcase \"$subcommand,$cur\" in\n+\t\tadd,--*)\n+\t\t\t__gitcomp \"--force --detach\"\n+\t\t\t;;\n+\t\tadd,*)\n+\t\t\twhile [ $c -lt $cword ]; do\n+\t\t\t\tcase \"${words[c]}\" in\n+\t\t\t\t--*) ;;\n+\t\t\t\t-[bB]) ((refpos++)) ;;\n+\t\t\t\t*) ((n++)) ;;\n+\t\t\t\tesac\n+\t\t\t\t((c++))\n+\t\t\tdone\n+\t\t\tif [ $n -eq $refpos ]; then\n+\t\t\t\t__gitcomp_nl \"$(__git_refs)\"\n+\t\t\tfi\n+\t\t\t;;\n+\t\tprune,--*)\n+\t\t\t__gitcomp \"--dry-run --verbose --expire\"\n+\t\t\t;;\n+\t\tesac\n+\tfi\n+}\n+\n __git_main ()\n {\n \tlocal i c=1 command __git_dir\n-- \n2.5.0.rc3.407.g68aafd0\n"},{"id":"267569","messageId":"CAPig+cQhh7YCiirp4a1+R0bWh46JoFgVOVUCNidTWXK8uNL2Mg@mail.gmail.com","threadId":"39926","inReplyTo":"1437684550-21884-1-git-send-email-sunshine@sunshineco.com","subject":"Re: [RFC/PATCH] contrib: teach completion about git-worktree options and arguments","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-08-06T16:29:53Z","receivedAt":"2015-08-06T16:29:53Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Thu, Jul 23, 2015 at 4:49 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> Complete subcommands 'add' and 'prune', as well as their respective\n> options --force, --detach, --dry-run, --verbose, and --expire. Also\n> complete 'refname' in \"git worktree add [-b <newbranch>] <path>\n> <refname>\".\n\nPing[1]?\n\n[1]: http://article.gmane.org/gmane.comp.version-control.git/274526\n\n> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>\n> ---\n>\n> This is RFC since this is my first foray into the Git completion script,\n> and there are likely better and more idiomatic ways to accomplish this.\n>\n>  contrib/completion/git-completion.bash | 32 ++++++++++++++++++++++++++++++++\n>  1 file changed, 32 insertions(+)\n>\n> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n> index c97c648..07c34ef 100644\n> --- a/contrib/completion/git-completion.bash\n> +++ b/contrib/completion/git-completion.bash\n> @@ -2564,6 +2564,38 @@ _git_whatchanged ()\n>         _git_log\n>  }\n>\n> +_git_worktree ()\n> +{\n> +       local subcommands='add prune'\n> +       local subcommand=\"$(__git_find_on_cmdline \"$subcommands\")\"\n> +       local c=2 n=0 refpos=2\n> +       if [ -z \"$subcommand\" ]; then\n> +               __gitcomp \"$subcommands\"\n> +       else\n> +               case \"$subcommand,$cur\" in\n> +               add,--*)\n> +                       __gitcomp \"--force --detach\"\n> +                       ;;\n> +               add,*)\n> +                       while [ $c -lt $cword ]; do\n> +                               case \"${words[c]}\" in\n> +                               --*) ;;\n> +                               -[bB]) ((refpos++)) ;;\n> +                               *) ((n++)) ;;\n> +                               esac\n> +                               ((c++))\n> +                       done\n> +                       if [ $n -eq $refpos ]; then\n> +                               __gitcomp_nl \"$(__git_refs)\"\n> +                       fi\n> +                       ;;\n> +               prune,--*)\n> +                       __gitcomp \"--dry-run --verbose --expire\"\n> +                       ;;\n> +               esac\n> +       fi\n> +}\n> +\n>  __git_main ()\n>  {\n>         local i c=1 command __git_dir\n> --\n> 2.5.0.rc3.407.g68aafd0\n"},{"id":"268437","messageId":"xmqq37zc7e2o.fsf@gitster.dls.corp.google.com","threadId":"39926","inReplyTo":"CAPig+cQhh7YCiirp4a1+R0bWh46JoFgVOVUCNidTWXK8uNL2Mg@mail.gmail.com","subject":"Re: [RFC/PATCH] contrib: teach completion about git-worktree options and arguments","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2015-08-21T17:21:03Z","receivedAt":"2015-08-21T17:21:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Sunshine <sunshine@sunshineco.com> writes:\n\n> On Thu, Jul 23, 2015 at 4:49 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n>> Complete subcommands 'add' and 'prune', as well as their respective\n>> options --force, --detach, --dry-run, --verbose, and --expire. Also\n>> complete 'refname' in \"git worktree add [-b <newbranch>] <path>\n>> <refname>\".\n>\n> Ping[1]?\n>\n> [1]: http://article.gmane.org/gmane.comp.version-control.git/274526\n\nPing indeed?\n\n>\n>> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>\n>> ---\n>>\n>> This is RFC since this is my first foray into the Git completion script,\n>> and there are likely better and more idiomatic ways to accomplish this.\n>>\n>>  contrib/completion/git-completion.bash | 32 ++++++++++++++++++++++++++++++++\n>>  1 file changed, 32 insertions(+)\n>>\n>> diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\n>> index c97c648..07c34ef 100644\n>> --- a/contrib/completion/git-completion.bash\n>> +++ b/contrib/completion/git-completion.bash\n>> @@ -2564,6 +2564,38 @@ _git_whatchanged ()\n>>         _git_log\n>>  }\n>>\n>> +_git_worktree ()\n>> +{\n>> +       local subcommands='add prune'\n>> +       local subcommand=\"$(__git_find_on_cmdline \"$subcommands\")\"\n>> +       local c=2 n=0 refpos=2\n>> +       if [ -z \"$subcommand\" ]; then\n>> +               __gitcomp \"$subcommands\"\n>> +       else\n>> +               case \"$subcommand,$cur\" in\n>> +               add,--*)\n>> +                       __gitcomp \"--force --detach\"\n>> +                       ;;\n>> +               add,*)\n>> +                       while [ $c -lt $cword ]; do\n>> +                               case \"${words[c]}\" in\n>> +                               --*) ;;\n>> +                               -[bB]) ((refpos++)) ;;\n>> +                               *) ((n++)) ;;\n>> +                               esac\n>> +                               ((c++))\n>> +                       done\n>> +                       if [ $n -eq $refpos ]; then\n>> +                               __gitcomp_nl \"$(__git_refs)\"\n>> +                       fi\n>> +                       ;;\n>> +               prune,--*)\n>> +                       __gitcomp \"--dry-run --verbose --expire\"\n>> +                       ;;\n>> +               esac\n>> +       fi\n>> +}\n>> +\n>>  __git_main ()\n>>  {\n>>         local i c=1 command __git_dir\n>> --\n>> 2.5.0.rc3.407.g68aafd0\n"},{"id":"268454","messageId":"20150821224918.Horde.edB9u314lsP17FLUzwFsQA1@webmail.informatik.kit.edu","threadId":"39926","inReplyTo":"xmqq37zc7e2o.fsf@gitster.dls.corp.google.com","subject":"Re: [RFC/PATCH] contrib: teach completion about git-worktree options and arguments","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2015-08-21T20:49:18Z","receivedAt":"2015-08-21T20:49:18Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"\nQuoting Junio C Hamano <gitster@pobox.com>:\n\n> Eric Sunshine <sunshine@sunshineco.com> writes:\n>\n>> On Thu, Jul 23, 2015 at 4:49 PM, Eric Sunshine  \n>> <sunshine@sunshineco.com> wrote:\n>>> Complete subcommands 'add' and 'prune', as well as their respective\n>>> options --force, --detach, --dry-run, --verbose, and --expire. Also\n>>> complete 'refname' in \"git worktree add [-b <newbranch>] <path>\n>>> <refname>\".\n>>\n>> Ping[1]?\n>>\n>> [1]: http://article.gmane.org/gmane.comp.version-control.git/274526\n>\n> Ping indeed?\n>\n\nYeah, right, sorry.  Non-subscribed occasional gmane-reader here amidst\njob hunting, so thanks for the ping.  And the re-ping...\n\n>>> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>\n>>> ---\n>>>\n>>> This is RFC since this is my first foray into the Git completion script,\n>>> and there are likely better and more idiomatic ways to accomplish this.\n\nUsing __git_find_on_cmdline() to find subcommands and case\n\"$subcommand,$cur\" to limit the number of nested case statements is as\nidiomatic as you can get in the completion script.\n\nAnd I hear you, that \"<path> first, <refname> second\" syntax makes it way\ntoo complicated, especially since they can follow '-b <refname>'.\nI wrote a completion function for 'git worktree' as well, turns out a week\nor two before you posted this, but I never submitted it as it was way too\nconvoluted.  Anyway, will send it in reply to this, just for reference.\n\n>>>\n>>>  contrib/completion/git-completion.bash | 32  \n>>> ++++++++++++++++++++++++++++++++\n>>>  1 file changed, 32 insertions(+)\n>>>\n>>> diff --git a/contrib/completion/git-completion.bash  \n>>> b/contrib/completion/git-completion.bash\n>>> index c97c648..07c34ef 100644\n>>> --- a/contrib/completion/git-completion.bash\n>>> +++ b/contrib/completion/git-completion.bash\n>>> @@ -2564,6 +2564,38 @@ _git_whatchanged ()\n>>>         _git_log\n>>>  }\n>>>\n>>> +_git_worktree ()\n>>> +{\n>>> +       local subcommands='add prune'\n>>> +       local subcommand=\"$(__git_find_on_cmdline \"$subcommands\")\"\n>>> +       local c=2 n=0 refpos=2\n\nA more descriptive variable name for 'n' would be great.\n\n>>> +       if [ -z \"$subcommand\" ]; then\n>>> +               __gitcomp \"$subcommands\"\n>>> +       else\n>>> +               case \"$subcommand,$cur\" in\n>>> +               add,--*)\n>>> +                       __gitcomp \"--force --detach\"\n\nWe usually don't offer '--force', because that option must be\nhandled with care.\n\n>>> +                       ;;\n>>> +               add,*)\n>>> +                       while [ $c -lt $cword ]; do\n>>> +                               case \"${words[c]}\" in\n>>> +                               --*) ;;\n>>> +                               -[bB]) ((refpos++)) ;;\n>>> +                               *) ((n++)) ;;\n>>> +                               esac\n>>> +                               ((c++))\n>>> +                       done\n>>> +                       if [ $n -eq $refpos ]; then\n\nI suppose here you wanted to calculate where (i.e. at which word on\nthe command line) we should offer refs and fall back to bash builtin\nfilename completion otherwise.  It works well in the common cases,\nbut:\n\n   - it doesn't offer refs after -b or -B\n\n   - it gets fooled by options to the git command, e.g. 'git\n     --git-dir=.git worktree add <TAB>' offers refs instead of files,\n     'git --git-dir=.git worktree add ../some/path <TAB>' offers\n     refs, etc.\n\n>>> +                               __gitcomp_nl \"$(__git_refs)\"\n>>> +                       fi\n>>> +                       ;;\n>>> +               prune,--*)\n>>> +                       __gitcomp \"--dry-run --verbose --expire\"\n>>> +                       ;;\n>>> +               esac\n>>> +       fi\n>>> +}\n>>> +\n>>>  __git_main ()\n>>>  {\n>>>         local i c=1 command __git_dir\n>>> --\n>>> 2.5.0.rc3.407.g68aafd0\n"},{"id":"268455","messageId":"1440190256-21794-1-git-send-email-szeder@ira.uka.de","threadId":"39926","inReplyTo":"20150821224918.Horde.edB9u314lsP17FLUzwFsQA1@webmail.informatik.kit.edu","subject":"[PoC PATCH] completion: support 'git worktree'","fromName":"SZEDER Gábor","fromEmail":"szeder@ira.uka.de","sentAt":"2015-08-21T20:50:56Z","receivedAt":"2015-08-21T20:50:56Z","isPatch":true,"sender":{"key":"szeder.dev@gmail.com","avatar":"https://avatars.githubusercontent.com/u/116324?v=4"},"body":"Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>\n---\n\n> I wrote a completion function for 'git worktree' as well, turns out a week\n> or two before you posted this, but I never submitted it as it was way too\n> convoluted.  Anyway, will send it in reply to this, just for reference.\n\nAnd here it is.\nFrom the number of indentation levels and comment lines you can see why\nI haven't submitted this patch yet :)\n\nOTOH it offers refs for -b and -B, and there are only fairly narrow\ncorner cases when 'git --options' can fool it (but that's a general\nissue with __git_find_on_cmdline(), I wouldn't go into that).\n\n contrib/completion/git-completion.bash | 59 ++++++++++++++++++++++++++++++++++\n 1 file changed, 59 insertions(+)\n\ndiff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash\nindex c97c648d7e..20a17e2c50 100644\n--- a/contrib/completion/git-completion.bash\n+++ b/contrib/completion/git-completion.bash\n@@ -2564,6 +2564,65 @@ _git_whatchanged ()\n \t_git_log\n }\n \n+_git_worktree ()\n+{\n+\tlocal subcommand subcommand_idx sc c=1\n+\tlocal subcommands=\"add prune\"\n+\n+\twhile [ $c -lt $cword ] && [ -z \"$subcommand\" ]; do\n+\t\tfor sc in $subcommands; do\n+\t\t\tif [ \"$sc\" = \"${words[c]}\" ]; then\n+\t\t\t\tsubcommand=$sc\n+\t\t\t\tsubcommand_idx=$c\n+\t\t\t\tbreak\n+\t\t\tfi\n+\t\tdone\n+\t\t((c++))\n+\tdone\n+\n+\tcase \"$subcommand,$cur\" in\n+\t,*)\n+\t\t__gitcomp \"$subcommands\"\n+\t\t;;\n+\tadd,--*)\n+\t\t__gitcomp \"--detach\"\n+\t\t;;\n+\tadd,*)\n+\t\tcase \"$prev\" in\n+\t\t-b|-B)\n+\t\t\t__gitcomp_nl \"$(__git_refs)\"\n+\t\t\t;;\n+\t\t-*)\t# $prev is an option without argument: have to complete\n+\t\t\t# the path for the new worktree, fall back to bash\n+\t\t\t# filename completion\n+\t\t\t;;\n+\t\t*)\t# $prev is not an option, so it must be either the\n+\t\t\t# 'add' subcommand, an argument of an option (e.g.\n+\t\t\t# branch for -b|-B), or the path for the new worktree\n+\t\t\tif [ $cword -eq $((subcommand_idx+1)) ]; then\n+\t\t\t\t# right after the 'add' subcommand, have to\n+\t\t\t\t# complete the path\n+\t\t\t\t:\n+\t\t\telse\n+\t\t\t\tcase \"${words[cword-2]}\" in\n+\t\t\t\t-b|-B)\t# after '-b <branch>', have to complete\n+\t\t\t\t\t# the path\n+\t\t\t\t\t;;\n+\t\t\t\t*)\t# after the path, have to complete the\n+\t\t\t\t\t# branch to be checked out\n+\t\t\t\t\t__gitcomp_nl \"$(__git_refs)\"\n+\t\t\t\t\t;;\n+\t\t\t\tesac\n+\t\t\tfi\n+\t\t\t;;\n+\t\tesac\n+\t\t;;\n+\tprune,--*)\n+\t\t__gitcomp \"--dry-run --verbose --expire\"\n+\t\t;;\n+\tesac\n+}\n+\n __git_main ()\n {\n \tlocal i c=1 command __git_dir\n-- \n2.5.0.418.gdd37a9b\n"},{"id":"268459","messageId":"CAPig+cQ9Rema_n1xBJQNSv9i75sGT=RWu1AheWJFTBMa89DyFA@mail.gmail.com","threadId":"39926","inReplyTo":"20150821224918.Horde.edB9u314lsP17FLUzwFsQA1@webmail.informatik.kit.edu","subject":"Re: [RFC/PATCH] contrib: teach completion about git-worktree options and arguments","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2015-08-21T21:59:15Z","receivedAt":"2015-08-21T21:59:15Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Fri, Aug 21, 2015 at 4:49 PM, SZEDER Gábor <szeder@ira.uka.de> wrote:\n> Quoting Junio C Hamano <gitster@pobox.com>:\n>> Eric Sunshine <sunshine@sunshineco.com> writes:\n>>> On Thu, Jul 23, 2015 at 4:49 PM, Eric Sunshine <sunshine@sunshineco.com>\n>>> wrote:\n>>>> Complete subcommands 'add' and 'prune', as well as their respective\n>>>> options --force, --detach, --dry-run, --verbose, and --expire. Also\n>>>> complete 'refname' in \"git worktree add [-b <newbranch>] <path>\n>>>> <refname>\".\n>>>\n>>> Ping[1]?\n>>\n>> Ping indeed?\n>\n> Yeah, right, sorry.  Non-subscribed occasional gmane-reader here amidst\n> job hunting, so thanks for the ping.  And the re-ping...\n\nThanks for the review.\n\n>>>> +_git_worktree ()\n>>>> +{\n>>>> +       local subcommands='add prune'\n>>>> +       local subcommand=\"$(__git_find_on_cmdline \"$subcommands\")\"\n>>>> +       local c=2 n=0 refpos=2\n>\n> A more descriptive variable name for 'n' would be great.\n\nIndeed. I was planning on resubmitting with better variable names\n(even if I didn't get any feedback)...\n\n>>>> +       if [ -z \"$subcommand\" ]; then\n>>>> +               __gitcomp \"$subcommands\"\n>>>> +       else\n>>>> +               case \"$subcommand,$cur\" in\n>>>> +               add,--*)\n>>>> +                       __gitcomp \"--force --detach\"\n>\n> We usually don't offer '--force', because that option must be\n> handled with care.\n\nAs a person who never uses git-completion (or git-prompt), I wasn't\naware of that, so thanks for the heads-up. I only installed completion\n(and prompt) when I decided to work on this (and I don't think I've\nused tab-completion since then).\n\n>>>> +                       ;;\n>>>> +               add,*)\n>>>> +                       while [ $c -lt $cword ]; do\n>>>> +                               case \"${words[c]}\" in\n>>>> +                               --*) ;;\n>>>> +                               -[bB]) ((refpos++)) ;;\n>>>> +                               *) ((n++)) ;;\n>>>> +                               esac\n>>>> +                               ((c++))\n>>>> +                       done\n>>>> +                       if [ $n -eq $refpos ]; then\n>\n> I suppose here you wanted to calculate where (i.e. at which word on\n> the command line) we should offer refs and fall back to bash builtin\n> filename completion otherwise.  It works well in the common cases,\n> but:\n>\n>   - it doesn't offer refs after -b or -B\n\nThat was intentional since this is a new branch name, so it didn't\nseem sensible to offer completion of existing refs. On the other hand,\nit doesn't make much since to offer pathname completion either, but I\ndidn't see a better alternative. On reflection, I suppose ref\ncompletion can make sense if the new branch name is going to be\nsimilar to an existing one.\n\n>   - it gets fooled by options to the git command, e.g. 'git\n>     --git-dir=.git worktree add <TAB>' offers refs instead of files,\n>     'git --git-dir=.git worktree add ../some/path <TAB>' offers\n>     refs, etc.\n\nThis shortcoming could be addressed by computing relative to the\nsubcommand-index as your version does, correct?\n\nI don't have enough background with the (git-specific) completion\nfacility to be able to judge if this patch and approach has merit and\nought to be pursued further, or if it would be better to drop in favor\nof (some version of) your patch. I don't care strongly, and am fine\nwith dropping this patch if it's approach is suboptimal.\n\n>>>> +                               __gitcomp_nl \"$(__git_refs)\"\n>>>> +                       fi\n>>>> +                       ;;\n>>>> +               prune,--*)\n>>>> +                       __gitcomp \"--dry-run --verbose --expire\"\n>>>> +                       ;;\n>>>> +               esac\n>>>> +       fi\n>>>> +}\n>>>> +\n"}]}