Merge branch 'ld/sparse-index-bash-completion'

The command line completion (in contrib/) learns to complete
arguments to give to "git sparse-checkout" command.

* ld/sparse-index-bash-completion:
  completion: handle unusual characters for sparse-checkout
  completion: improve sparse-checkout cone mode directory completion
  completion: address sparse-checkout issues
This commit is contained in:
Junio C Hamano 2022-02-16 15:14:26 -08:00
Родитель b80121027d 48803821b1
Коммит 9d2f9a6188
2 изменённых файлов: 174 добавлений и 8 удалений

Просмотреть файл

@ -2986,9 +2986,37 @@ _git_show_branch ()
__git_complete_revlist
}
__gitcomp_directories ()
{
local _tmp_dir _tmp_completions _found=0
# Get the directory of the current token; this differs from dirname
# in that it keeps up to the final trailing slash. If no slash found
# that's fine too.
[[ "$cur" =~ .*/ ]]
_tmp_dir=$BASH_REMATCH
# Find possible directory completions, adding trailing '/' characters,
# de-quoting, and handling unusual characters.
while IFS= read -r -d $'\0' c ; do
# If there are directory completions, find ones that start
# with "$cur", the current token, and put those in COMPREPLY
if [[ $c == "$cur"* ]]; then
COMPREPLY+=("$c/")
_found=1
fi
done < <(git ls-tree -z -d --name-only HEAD $_tmp_dir)
if [[ $_found == 0 ]] && [[ "$cur" =~ /$ ]]; then
# No possible further completions any deeper, so assume we're at
# a leaf directory and just consider it complete
__gitcomp_direct_append "$cur "
fi
}
_git_sparse_checkout ()
{
local subcommands="list init set disable"
local subcommands="list init set disable add reapply"
local subcommand="$(__git_find_on_cmdline "$subcommands")"
if [ -z "$subcommand" ]; then
__gitcomp "$subcommands"
@ -2996,14 +3024,14 @@ _git_sparse_checkout ()
fi
case "$subcommand,$cur" in
init,--*)
__gitcomp "--cone"
;;
set,--*)
__gitcomp "--stdin"
;;
*)
*,--*)
__gitcomp_builtin sparse-checkout_$subcommand "" "--"
;;
set,*|add,*)
if [ "$(__git config core.sparseCheckoutCone)" == "true" ] ||
[ -n "$(__git_find_on_cmdline --cone)" ]; then
__gitcomp_directories
fi
esac
}

Просмотреть файл

@ -1444,6 +1444,144 @@ test_expect_success 'git checkout - with --detach, complete only references' '
EOF
'
test_expect_success 'setup sparse-checkout tests' '
# set up sparse-checkout repo
git init sparse-checkout &&
(
cd sparse-checkout &&
mkdir -p folder1/0/1 folder2/0 folder3 &&
touch folder1/0/1/t.txt &&
touch folder2/0/t.txt &&
touch folder3/t.txt &&
git add . &&
git commit -am "Initial commit"
)
'
test_expect_success 'sparse-checkout completes subcommands' '
test_completion "git sparse-checkout " <<-\EOF
list Z
init Z
set Z
add Z
reapply Z
disable Z
EOF
'
test_expect_success 'cone mode sparse-checkout completes directory names' '
# initialize sparse-checkout definitions
git -C sparse-checkout sparse-checkout set --cone folder1/0 folder3 &&
# test tab completion
(
cd sparse-checkout &&
test_completion "git sparse-checkout set f" <<-\EOF
folder1/
folder2/
folder3/
EOF
) &&
(
cd sparse-checkout &&
test_completion "git sparse-checkout set folder1/" <<-\EOF
folder1/0/
EOF
) &&
(
cd sparse-checkout &&
test_completion "git sparse-checkout set folder1/0/" <<-\EOF
folder1/0/1/
EOF
) &&
(
cd sparse-checkout/folder1 &&
test_completion "git sparse-checkout add 0" <<-\EOF
0/
EOF
)
'
test_expect_success 'cone mode sparse-checkout completes directory names with spaces and accents' '
# reset sparse-checkout
git -C sparse-checkout sparse-checkout disable &&
(
cd sparse-checkout &&
mkdir "directory with spaces" &&
mkdir "directory-with-áccent" &&
>"directory with spaces/randomfile" &&
>"directory-with-áccent/randomfile" &&
git add . &&
git commit -m "Add directory with spaces and directory with accent" &&
git sparse-checkout set --cone "directory with spaces" \
"directory-with-áccent" &&
test_completion "git sparse-checkout add dir" <<-\EOF &&
directory with spaces/
directory-with-áccent/
EOF
rm -rf "directory with spaces" &&
rm -rf "directory-with-áccent" &&
git add . &&
git commit -m "Remove directory with spaces and directory with accent"
)
'
# use FUNNYNAMES to avoid running on Windows, which doesn't permit backslashes or tabs in paths
test_expect_success FUNNYNAMES 'cone mode sparse-checkout completes directory names with backslashes and tabs' '
# reset sparse-checkout
git -C sparse-checkout sparse-checkout disable &&
(
cd sparse-checkout &&
mkdir "directory\with\backslashes" &&
mkdir "$(printf "directory\twith\ttabs")" &&
>"directory\with\backslashes/randomfile" &&
>"$(printf "directory\twith\ttabs")/randomfile" &&
git add . &&
git commit -m "Add directory with backslashes and directory with tabs" &&
git sparse-checkout set --cone "directory\with\backslashes" \
"$(printf "directory\twith\ttabs")" &&
test_completion "git sparse-checkout add dir" <<-\EOF &&
directory\with\backslashes/
directory with tabs/
EOF
rm -rf "directory\with\backslashes" &&
rm -rf "$(printf "directory\twith\ttabs")" &&
git add . &&
git commit -m "Remove directory with backslashes and directory with tabs"
)
'
test_expect_success 'non-cone mode sparse-checkout uses bash completion' '
# reset sparse-checkout repo to non-cone mode
git -C sparse-checkout sparse-checkout disable &&
git -C sparse-checkout sparse-checkout set --no-cone &&
(
cd sparse-checkout &&
# expected to be empty since we have not configured
# custom completion for non-cone mode
test_completion "git sparse-checkout set f" <<-\EOF
EOF
)
'
test_expect_success 'git sparse-checkout set --cone completes directory names' '
git -C sparse-checkout sparse-checkout disable &&
(
cd sparse-checkout &&
test_completion "git sparse-checkout set --cone f" <<-\EOF
folder1/
folder2/
folder3/
EOF
)
'
test_expect_success 'git switch - with -d, complete all references' '
test_completion "git switch -d " <<-\EOF
HEAD Z