Skip to content

Commit 7d5ecd7

Browse files
szedergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 3027e4f commit 7d5ecd7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

contrib/completion/git-completion.bash

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3011,6 +3011,42 @@ _git_worktree ()
30113011
*,--*)
30123012
__gitcomp_builtin worktree_$subcommand
30133013
;;
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+
;;
30143050
lock,*|remove,*|unlock,*)
30153051
__git_complete_worktree_paths
30163052
;;

0 commit comments

Comments
 (0)