@matheusbsbs / matheus-bagliano / commits / 7d5ecd775d
Fork 0

completion: list paths and refs for 'git worktree add'

Complete paths after 'git worktree add <TAB>' and refs after 'git worktree add -b <TAB>' and 'git worktree add some/dir <TAB>'. Uncharacteristically for a Git command, 'git worktree add' takes a mandatory path parameter before a commit-ish as its optional last parameter. In addition, it has both standalone --options and options with a mandatory unstuck parameter ('-b <new-branch>'). Consequently, trying to complete refs for that last optional commit-ish parameter resulted in a more convoluted than usual completion function, but hopefully all the included comments will make it not too hard to digest. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Dec 19, 2019 at 16:09 UTC 7d5ecd775de33878f00055e0024a003ab4edfbce
1 file changed +36
contrib/completion/git-completion.bash
+36
@@ -3011,6 +3011,42 @@ _git_worktree ()
3011 *,--*)
3012 __gitcomp_builtin worktree_$subcommand
3013 ;;
3014 + add,*) # usage: git worktree add [<options>] <path> [<commit-ish>]
3015 + # Here we are not completing an --option, it's either the
3016 + # path or a ref.
3017 + case "$prev" in
3018 + -b|-B) # Complete refs for branch to be created/reseted.
3019 + __git_complete_refs
3020 + ;;
3021 + -*) # The previous word is an -o|--option without an
3022 + # unstuck argument: have to complete the path for
3023 + # the new worktree, so don't list anything, but let
3024 + # Bash fall back to filename completion.
3025 + ;;
3026 + *) # The previous word is not an --option, so it must
3027 + # be either the 'add' subcommand, the unstuck
3028 + # argument of an option (e.g. branch for -b|-B), or
3029 + # the path for the new worktree.
3030 + if [ $cword -eq $((subcommand_idx+1)) ]; then
3031 + # Right after the 'add' subcommand: have to
3032 + # complete the path, so fall back to Bash
3033 + # filename completion.
3034 + :
3035 + else
3036 + case "${words[cword-2]}" in
3037 + -b|-B) # After '-b <branch>': have to
3038 + # complete the path, so fall back
3039 + # to Bash filename completion.
3040 + ;;
3041 + *) # After the path: have to complete
3042 + # the ref to be checked out.
3043 + __git_complete_refs
3044 + ;;
3045 + esac
3046 + fi
3047 + ;;
3048 + esac
3049 + ;;
3050 lock,*|remove,*|unlock,*)
3051 __git_complete_worktree_paths
3052 ;;