Signed-off-by: SZEDER Gábor <[email protected]>
---

> I wrote a completion function for 'git worktree' as well, turns out a week
> or two before you posted this, but I never submitted it as it was way too
> convoluted.  Anyway, will send it in reply to this, just for reference.

And here it is.
>From the number of indentation levels and comment lines you can see why
I haven't submitted this patch yet :)

OTOH it offers refs for -b and -B, and there are only fairly narrow
corner cases when 'git --options' can fool it (but that's a general
issue with __git_find_on_cmdline(), I wouldn't go into that).

 contrib/completion/git-completion.bash | 59 ++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/contrib/completion/git-completion.bash 
b/contrib/completion/git-completion.bash
index c97c648d7e..20a17e2c50 100644
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -2564,6 +2564,65 @@ _git_whatchanged ()
        _git_log
 }
 
+_git_worktree ()
+{
+       local subcommand subcommand_idx sc c=1
+       local subcommands="add prune"
+
+       while [ $c -lt $cword ] && [ -z "$subcommand" ]; do
+               for sc in $subcommands; do
+                       if [ "$sc" = "${words[c]}" ]; then
+                               subcommand=$sc
+                               subcommand_idx=$c
+                               break
+                       fi
+               done
+               ((c++))
+       done
+
+       case "$subcommand,$cur" in
+       ,*)
+               __gitcomp "$subcommands"
+               ;;
+       add,--*)
+               __gitcomp "--detach"
+               ;;
+       add,*)
+               case "$prev" in
+               -b|-B)
+                       __gitcomp_nl "$(__git_refs)"
+                       ;;
+               -*)     # $prev is an option without argument: have to complete
+                       # the path for the new worktree, fall back to bash
+                       # filename completion
+                       ;;
+               *)      # $prev is not an option, so it must be either the
+                       # 'add' subcommand, an argument of an option (e.g.
+                       # branch for -b|-B), or the path for the new worktree
+                       if [ $cword -eq $((subcommand_idx+1)) ]; then
+                               # right after the 'add' subcommand, have to
+                               # complete the path
+                               :
+                       else
+                               case "${words[cword-2]}" in
+                               -b|-B)  # after '-b <branch>', have to complete
+                                       # the path
+                                       ;;
+                               *)      # after the path, have to complete the
+                                       # branch to be checked out
+                                       __gitcomp_nl "$(__git_refs)"
+                                       ;;
+                               esac
+                       fi
+                       ;;
+               esac
+               ;;
+       prune,--*)
+               __gitcomp "--dry-run --verbose --expire"
+               ;;
+       esac
+}
+
 __git_main ()
 {
        local i c=1 command __git_dir
-- 
2.5.0.418.gdd37a9b

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to