3 # Copyright (c) 2012-2020 Felipe Contreras
6 test_description
='test bash completion'
8 # The Bash completion scripts must not print anything to either stdout or
9 # stderr, which we try to verify. When tracing is enabled without support for
10 # BASH_XTRACEFD this assertion will fail, so we have to mark the test as
11 # untraceable with such ancient Bash versions.
12 test_untraceable
=UnfortunatelyYes
14 # Override environment and always use master for the default initial branch
15 # name for these tests, so that rev completion candidates are as expected.
16 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=master
17 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
19 TEST_PASSES_SANITIZE_LEAK
=true
28 # Be careful when updating these lists:
30 # (1) The build tree may have build artifact from different branch, or
31 # the user's $PATH may have a random executable that may begin
32 # with "git-check" that are not part of the subcommands this build
33 # will ship, e.g. "check-ignore". The tests for completion for
34 # subcommand names tests how "check" is expanded; we limit the
35 # possible candidates to "checkout" and "check-attr" to make sure
36 # "check-attr", which is known by the filter function as a
37 # subcommand to be thrown out, while excluding other random files
38 # that happen to begin with "check" to avoid letting them get in
41 # (2) A test makes sure that common subcommands are included in the
42 # completion for "git <TAB>", and a plumbing is excluded. "add",
43 # "rebase" and "ls-files" are listed for this.
45 GIT_TESTING_ALL_COMMAND_LIST
='add checkout check-attr rebase ls-files'
46 GIT_TESTING_PORCELAIN_COMMAND_LIST
='add checkout rebase'
48 .
"$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
50 # We don't need this function to actually join words or do anything special.
51 # Also, it's cleaner to avoid touching bash's internal completion variables.
52 # So let's override it with a minimal version for testing purposes.
53 _get_comp_words_by_ref
()
55 while [ $# -gt 0 ]; do
61 prev
=${_words[_cword-1]}
64 words
=("${_words[@]}")
77 printf '%s\n' "${COMPREPLY[*]}" > out
82 local -a COMPREPLY _words
85 test "${1: -1}" = ' ' && _words
[${#_words[@]}+1]=''
86 (( _cword
= ${#_words[@]} - 1 ))
87 __git_wrap__git_main
&& print_comp
90 # Test high-level completion
92 # 1: typed text so far (cur)
93 # 2: expected completion
98 printf '%s\n' "$2" >expected
100 sed -e 's/Z$//' |
sort >expected
102 run_completion
"$1" >"$TRASH_DIRECTORY"/bash-completion-output
2>&1 &&
103 sort out
>out_sorted
&&
104 test_cmp expected out_sorted
&&
105 test_must_be_empty
"$TRASH_DIRECTORY"/bash-completion-output
&&
106 rm "$TRASH_DIRECTORY"/bash-completion-output
110 # The first argument is the typed text so far (cur); the rest are
111 # passed to __gitcomp. Expected output comes is read from the
112 # standard input, like test_completion().
115 local -a COMPREPLY
&&
116 sed -e 's/Z$//' >expected
&&
121 test_cmp expected out
126 # 1: current word (cur)
127 # -: the rest are passed to __gitcomp_nl
130 local -a COMPREPLY
&&
131 sed -e 's/Z$//' >expected
&&
136 test_cmp expected out
139 invalid_variable_name
='${foo.bar}'
141 actual
="$TRASH_DIRECTORY/actual"
143 if test_have_prereq MINGW
150 test_expect_success
'setup for __git_find_repo_path/__gitdir tests' '
151 mkdir -p subdir/subsubdir &&
153 git init -b main otherrepo
156 test_expect_success
'__git_find_repo_path - from command line (through $__git_dir)' '
157 echo "$ROOT/otherrepo/.git" >expected &&
159 __git_dir="$ROOT/otherrepo/.git" &&
160 __git_find_repo_path &&
161 echo "$__git_repo_path" >"$actual"
163 test_cmp expected "$actual"
166 test_expect_success
'__git_find_repo_path - .git directory in cwd' '
167 echo ".git" >expected &&
169 __git_find_repo_path &&
170 echo "$__git_repo_path" >"$actual"
172 test_cmp expected "$actual"
175 test_expect_success
'__git_find_repo_path - .git directory in parent' '
176 echo "$ROOT/.git" >expected &&
178 cd subdir/subsubdir &&
179 __git_find_repo_path &&
180 echo "$__git_repo_path" >"$actual"
182 test_cmp expected "$actual"
185 test_expect_success
'__git_find_repo_path - cwd is a .git directory' '
186 echo "." >expected &&
189 __git_find_repo_path &&
190 echo "$__git_repo_path" >"$actual"
192 test_cmp expected "$actual"
195 test_expect_success
'__git_find_repo_path - parent is a .git directory' '
196 echo "$ROOT/.git" >expected &&
199 __git_find_repo_path &&
200 echo "$__git_repo_path" >"$actual"
202 test_cmp expected "$actual"
205 test_expect_success
'__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
206 echo "$ROOT/otherrepo/.git" >expected &&
208 GIT_DIR="$ROOT/otherrepo/.git" &&
210 __git_find_repo_path &&
211 echo "$__git_repo_path" >"$actual"
213 test_cmp expected "$actual"
216 test_expect_success
'__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
217 echo "$ROOT/otherrepo/.git" >expected &&
219 GIT_DIR="$ROOT/otherrepo/.git" &&
222 __git_find_repo_path &&
223 echo "$__git_repo_path" >"$actual"
225 test_cmp expected "$actual"
228 test_expect_success
'__git_find_repo_path - from command line while "git -C"' '
229 echo "$ROOT/.git" >expected &&
231 __git_dir="$ROOT/.git" &&
232 __git_C_args=(-C otherrepo) &&
233 __git_find_repo_path &&
234 echo "$__git_repo_path" >"$actual"
236 test_cmp expected "$actual"
239 test_expect_success
'__git_find_repo_path - relative dir from command line and "git -C"' '
240 echo "$ROOT/otherrepo/.git" >expected &&
243 __git_dir="otherrepo/.git" &&
244 __git_C_args=(-C ..) &&
245 __git_find_repo_path &&
246 echo "$__git_repo_path" >"$actual"
248 test_cmp expected "$actual"
251 test_expect_success
'__git_find_repo_path - $GIT_DIR set while "git -C"' '
252 echo "$ROOT/.git" >expected &&
254 GIT_DIR="$ROOT/.git" &&
256 __git_C_args=(-C otherrepo) &&
257 __git_find_repo_path &&
258 echo "$__git_repo_path" >"$actual"
260 test_cmp expected "$actual"
263 test_expect_success
'__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
264 echo "$ROOT/otherrepo/.git" >expected &&
267 GIT_DIR="otherrepo/.git" &&
269 __git_C_args=(-C ..) &&
270 __git_find_repo_path &&
271 echo "$__git_repo_path" >"$actual"
273 test_cmp expected "$actual"
276 test_expect_success
'__git_find_repo_path - "git -C" while .git directory in cwd' '
277 echo "$ROOT/otherrepo/.git" >expected &&
279 __git_C_args=(-C otherrepo) &&
280 __git_find_repo_path &&
281 echo "$__git_repo_path" >"$actual"
283 test_cmp expected "$actual"
286 test_expect_success
'__git_find_repo_path - "git -C" while cwd is a .git directory' '
287 echo "$ROOT/otherrepo/.git" >expected &&
290 __git_C_args=(-C .. -C otherrepo) &&
291 __git_find_repo_path &&
292 echo "$__git_repo_path" >"$actual"
294 test_cmp expected "$actual"
297 test_expect_success
'__git_find_repo_path - "git -C" while .git directory in parent' '
298 echo "$ROOT/otherrepo/.git" >expected &&
301 __git_C_args=(-C .. -C otherrepo) &&
302 __git_find_repo_path &&
303 echo "$__git_repo_path" >"$actual"
305 test_cmp expected "$actual"
308 test_expect_success
'__git_find_repo_path - non-existing path in "git -C"' '
310 __git_C_args=(-C non-existing) &&
311 test_must_fail __git_find_repo_path &&
312 printf "$__git_repo_path" >"$actual"
314 test_must_be_empty "$actual"
317 test_expect_success
'__git_find_repo_path - non-existing path in $__git_dir' '
319 __git_dir="non-existing" &&
320 test_must_fail __git_find_repo_path &&
321 printf "$__git_repo_path" >"$actual"
323 test_must_be_empty "$actual"
326 test_expect_success
'__git_find_repo_path - non-existing $GIT_DIR' '
328 GIT_DIR="$ROOT/non-existing" &&
330 test_must_fail __git_find_repo_path &&
331 printf "$__git_repo_path" >"$actual"
333 test_must_be_empty "$actual"
336 test_expect_success
'__git_find_repo_path - gitfile in cwd' '
337 echo "$ROOT/otherrepo/.git" >expected &&
338 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
339 test_when_finished "rm -f subdir/.git" &&
342 __git_find_repo_path &&
343 echo "$__git_repo_path" >"$actual"
345 test_cmp expected "$actual"
348 test_expect_success
'__git_find_repo_path - gitfile in parent' '
349 echo "$ROOT/otherrepo/.git" >expected &&
350 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
351 test_when_finished "rm -f subdir/.git" &&
353 cd subdir/subsubdir &&
354 __git_find_repo_path &&
355 echo "$__git_repo_path" >"$actual"
357 test_cmp expected "$actual"
360 test_expect_success SYMLINKS
'__git_find_repo_path - resulting path avoids symlinks' '
361 echo "$ROOT/otherrepo/.git" >expected &&
362 mkdir otherrepo/dir &&
363 test_when_finished "rm -rf otherrepo/dir" &&
364 ln -s otherrepo/dir link &&
365 test_when_finished "rm -f link" &&
368 __git_find_repo_path &&
369 echo "$__git_repo_path" >"$actual"
371 test_cmp expected "$actual"
374 test_expect_success
'__git_find_repo_path - not a git repository' '
377 GIT_CEILING_DIRECTORIES="$ROOT" &&
378 export GIT_CEILING_DIRECTORIES &&
379 test_must_fail __git_find_repo_path &&
380 printf "$__git_repo_path" >"$actual"
382 test_must_be_empty "$actual"
385 test_expect_success
'__gitdir - finds repo' '
386 echo "$ROOT/.git" >expected &&
388 cd subdir/subsubdir &&
391 test_cmp expected "$actual"
395 test_expect_success
'__gitdir - returns error when cannot find repo' '
397 __git_dir="non-existing" &&
398 test_must_fail __gitdir >"$actual"
400 test_must_be_empty "$actual"
403 test_expect_success
'__gitdir - repo as argument' '
404 echo "otherrepo/.git" >expected &&
406 __gitdir "otherrepo" >"$actual"
408 test_cmp expected "$actual"
411 test_expect_success
'__gitdir - remote as argument' '
412 echo "remote" >expected &&
414 __gitdir "remote" >"$actual"
416 test_cmp expected "$actual"
420 test_expect_success
'__git_dequote - plain unquoted word' '
421 __git_dequote unquoted-word &&
422 test unquoted-word = "$dequoted_word"
425 # input: b\a\c\k\'\\\"s\l\a\s\h\es
426 # expected: back'\"slashes
427 test_expect_success
'__git_dequote - backslash escaped' '
428 __git_dequote "b\a\c\k\\'\''\\\\\\\"s\l\a\s\h\es" &&
429 test "back'\''\\\"slashes" = "$dequoted_word"
432 # input: sin'gle\' '"quo'ted
433 # expected: single\ "quoted
434 test_expect_success
'__git_dequote - single quoted' '
435 __git_dequote "'"sin'gle\\\\' '\\\"quo'ted"'" &&
436 test '\''single\ "quoted'\'' = "$dequoted_word"
439 # input: dou"ble\\" "\"\quot"ed
440 # expected: double\ "\quoted
441 test_expect_success
'__git_dequote - double quoted' '
442 __git_dequote '\''dou"ble\\" "\"\quot"ed'\'' &&
443 test '\''double\ "\quoted'\'' = "$dequoted_word"
446 # input: 'open single quote
447 test_expect_success
'__git_dequote - open single quote' '
448 __git_dequote "'\''open single quote" &&
449 test "open single quote" = "$dequoted_word"
452 # input: "open double quote
453 test_expect_success
'__git_dequote - open double quote' '
454 __git_dequote "\"open double quote" &&
455 test "open double quote" = "$dequoted_word"
459 test_expect_success
'__gitcomp_direct - puts everything into COMPREPLY as-is' '
460 sed -e "s/Z$//g" >expected <<-EOF &&
461 with-trailing-space Z
462 without-trailing-spaceZ
465 $invalid_variable_name Z
468 cur=should_be_ignored &&
469 __gitcomp_direct "$(cat expected)" &&
472 test_cmp expected out
475 test_expect_success
'__gitcomp - trailing space - options' '
476 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
477 --reset-author" <<-EOF
484 test_expect_success
'__gitcomp - trailing space - config keys' '
485 test_gitcomp "br" "branch. branch.autosetupmerge
486 branch.autosetuprebase browser." <<-\EOF
488 branch.autosetupmerge Z
489 branch.autosetuprebase Z
494 test_expect_success
'__gitcomp - option parameter' '
495 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
502 test_expect_success
'__gitcomp - prefix' '
503 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
504 "branch.maint." "me" <<-\EOF
506 branch.maint.mergeoptions Z
510 test_expect_success
'__gitcomp - suffix' '
511 test_gitcomp "branch.me" "master maint next seen" "branch." \
518 test_expect_success
'__gitcomp - ignore optional negative options' '
519 test_gitcomp "--" "--abc --def --no-one -- --no-two" <<-\EOF
527 test_expect_success
'__gitcomp - ignore/narrow optional negative options' '
528 test_gitcomp "--a" "--abc --abcdef --no-one -- --no-two" <<-\EOF
534 test_expect_success
'__gitcomp - ignore/narrow optional negative options' '
535 test_gitcomp "--n" "--abc --def --no-one -- --no-two" <<-\EOF
541 test_expect_success
'__gitcomp - expand all negative options' '
542 test_gitcomp "--no-" "--abc --def --no-one -- --no-two" <<-\EOF
548 test_expect_success
'__gitcomp - expand/narrow all negative options' '
549 test_gitcomp "--no-o" "--abc --def --no-one -- --no-two" <<-\EOF
554 test_expect_success
'__gitcomp - equal skip' '
555 test_gitcomp "--option=" "--option=" <<-\EOF &&
558 test_gitcomp "option=" "option=" <<-\EOF
563 test_expect_success
'__gitcomp - doesnt fail because of invalid variable name' '
564 __gitcomp "$invalid_variable_name"
567 read -r -d "" refs
<<-\EOF
574 test_expect_success '__gitcomp_nl - trailing space
' '
575 test_gitcomp_nl
"m" "$refs" <<-EOF
581 test_expect_success '__gitcomp_nl
- prefix
' '
582 test_gitcomp_nl
"--fixup=m" "$refs" "--fixup=" "m" <<-EOF
588 test_expect_success '__gitcomp_nl
- suffix
' '
589 test_gitcomp_nl
"branch.ma" "$refs" "branch." "ma" "." <<-\EOF
595 test_expect_success '__gitcomp_nl - no suffix
' '
596 test_gitcomp_nl
"ma" "$refs" "" "ma" "" <<-\EOF
602 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name
' '
603 __gitcomp_nl
"$invalid_variable_name"
606 test_expect_success '__git_remotes
- list remotes from
$GIT_DIR/remotes and from config
file' '
607 cat >expect
<<-EOF &&
613 test_when_finished
"rm -rf .git/remotes" &&
614 mkdir
-p .git
/remotes
&&
615 >.git
/remotes
/remote_from_file_1
&&
616 >.git
/remotes
/remote_from_file_2
&&
617 test_when_finished
"git remote remove remote_in_config_1" &&
618 git remote add remote_in_config_1 git
://remote_1
&&
619 test_when_finished
"git remote remove remote_in_config_2" &&
620 git remote add remote_in_config_2 git
://remote_2
&&
622 __git_remotes
>actual
624 test_cmp expect actual
627 test_expect_success '__git_is_configured_remote
' '
628 test_when_finished
"git remote remove remote_1" &&
629 git remote add remote_1 git
://remote_1
&&
630 test_when_finished
"git remote remove remote_2" &&
631 git remote add remote_2 git
://remote_2
&&
633 __git_is_configured_remote remote_2
&&
634 test_must_fail __git_is_configured_remote non-existent
638 test_expect_success 'setup
for ref completion
' '
639 git commit
--allow-empty -m initial
&&
640 git branch
-M main
&&
641 git branch matching-branch
&&
642 git tag matching-tag
&&
645 git commit
--allow-empty -m initial
&&
646 git branch
-m main main-in-other
&&
647 git branch branch-in-other
&&
650 git remote add other
"$ROOT/otherrepo/.git" &&
651 git fetch
--no-tags other
&&
652 rm -f .git
/FETCH_HEAD
&&
656 test_expect_success '__git_refs
- simple
' '
657 cat >expected
<<-EOF &&
661 other/branch-in-other
667 __git_refs
>"$actual"
669 test_cmp expected
"$actual"
672 test_expect_success '__git_refs
- full refs
' '
673 cat >expected
<<-EOF &&
675 refs/heads/matching-branch
676 refs/remotes/other/branch-in-other
677 refs/remotes/other/main-in-other
678 refs/tags/matching-tag
682 __git_refs
>"$actual"
684 test_cmp expected
"$actual"
687 test_expect_success '__git_refs
- repo given on the
command line
' '
688 cat >expected
<<-EOF &&
695 __git_dir
="$ROOT/otherrepo/.git" &&
697 __git_refs
>"$actual"
699 test_cmp expected
"$actual"
702 test_expect_success '__git_refs
- remote on
local file system
' '
703 cat >expected
<<-EOF &&
711 __git_refs otherrepo
>"$actual"
713 test_cmp expected
"$actual"
716 test_expect_success '__git_refs
- remote on
local file system
- full refs
' '
717 cat >expected
<<-EOF &&
718 refs/heads/branch-in-other
719 refs/heads/main-in-other
720 refs/tags/tag-in-other
724 __git_refs otherrepo
>"$actual"
726 test_cmp expected
"$actual"
729 test_expect_success '__git_refs
- configured remote
' '
730 cat >expected
<<-EOF &&
737 __git_refs other
>"$actual"
739 test_cmp expected
"$actual"
742 test_expect_success '__git_refs
- configured remote
- full refs
' '
743 cat >expected
<<-EOF &&
745 refs/heads/branch-in-other
746 refs/heads/main-in-other
747 refs/tags/tag-in-other
751 __git_refs other
>"$actual"
753 test_cmp expected
"$actual"
756 test_expect_success '__git_refs
- configured remote
- repo given on the
command line
' '
757 cat >expected
<<-EOF &&
764 __git_dir
="$ROOT/.git" &&
766 __git_refs other
>"$actual"
768 test_cmp expected
"$actual"
771 test_expect_success '__git_refs
- configured remote
- full refs
- repo given on the
command line
' '
772 cat >expected
<<-EOF &&
774 refs/heads/branch-in-other
775 refs/heads/main-in-other
776 refs/tags/tag-in-other
780 __git_dir
="$ROOT/.git" &&
782 __git_refs other
>"$actual"
784 test_cmp expected
"$actual"
787 test_expect_success '__git_refs
- configured remote
- remote name matches a directory
' '
788 cat >expected
<<-EOF &&
794 test_when_finished
"rm -rf other" &&
797 __git_refs other
>"$actual"
799 test_cmp expected
"$actual"
802 test_expect_success '__git_refs
- URL remote
' '
803 cat >expected
<<-EOF &&
811 __git_refs
"file://$ROOT/otherrepo/.git" >"$actual"
813 test_cmp expected
"$actual"
816 test_expect_success '__git_refs
- URL remote
- full refs
' '
817 cat >expected
<<-EOF &&
819 refs/heads/branch-in-other
820 refs/heads/main-in-other
821 refs/tags/tag-in-other
825 __git_refs
"file://$ROOT/otherrepo/.git" >"$actual"
827 test_cmp expected
"$actual"
830 test_expect_success '__git_refs
- non-existing remote
' '
833 __git_refs non-existing
>"$actual"
835 test_must_be_empty
"$actual"
838 test_expect_success '__git_refs
- non-existing remote
- full refs
' '
841 __git_refs non-existing
>"$actual"
843 test_must_be_empty
"$actual"
846 test_expect_success '__git_refs
- non-existing URL remote
' '
849 __git_refs
"file://$ROOT/non-existing" >"$actual"
851 test_must_be_empty
"$actual"
854 test_expect_success '__git_refs
- non-existing URL remote
- full refs
' '
857 __git_refs
"file://$ROOT/non-existing" >"$actual"
859 test_must_be_empty
"$actual"
862 test_expect_success '__git_refs
- not
in a git repository
' '
864 GIT_CEILING_DIRECTORIES
="$ROOT" &&
865 export GIT_CEILING_DIRECTORIES
&&
868 __git_refs
>"$actual"
870 test_must_be_empty
"$actual"
873 test_expect_success '__git_refs
- unique remote branches
for git checkout DWIMery
' '
874 cat >expected
<<-EOF &&
879 other/branch-in-other
882 remote/branch-in-remote
888 for remote_ref
in refs
/remotes
/other
/ambiguous \
889 refs
/remotes
/remote
/ambiguous \
890 refs
/remotes
/remote
/branch-in-remote
892 git update-ref
$remote_ref main
&&
893 test_when_finished
"git update-ref -d $remote_ref" ||
return 1
897 __git_refs
"" 1 >"$actual"
899 test_cmp expected
"$actual"
902 test_expect_success '__git_refs
- after
--opt=' '
903 cat >expected
<<-EOF &&
907 other/branch-in-other
913 __git_refs
"" "" "" "" >"$actual"
915 test_cmp expected
"$actual"
918 test_expect_success '__git_refs
- after
--opt= - full refs
' '
919 cat >expected
<<-EOF &&
921 refs/heads/matching-branch
922 refs/remotes/other/branch-in-other
923 refs/remotes/other/main-in-other
924 refs/tags/matching-tag
928 __git_refs
"" "" "" refs
/ >"$actual"
930 test_cmp expected
"$actual"
933 test_expect_success '__git refs
- excluding refs
' '
934 cat >expected
<<-EOF &&
938 ^other/branch-in-other
944 __git_refs
>"$actual"
946 test_cmp expected
"$actual"
949 test_expect_success '__git refs
- excluding full refs
' '
950 cat >expected
<<-EOF &&
952 ^refs/heads/matching-branch
953 ^refs/remotes/other/branch-in-other
954 ^refs/remotes/other/main-in-other
955 ^refs/tags/matching-tag
959 __git_refs
>"$actual"
961 test_cmp expected
"$actual"
964 test_expect_success 'setup
for filtering matching refs
' '
965 git branch matching
/branch
&&
966 git tag matching
/tag
&&
967 git
-C otherrepo branch matching
/branch-in-other
&&
968 git fetch
--no-tags other
&&
969 rm -f .git
/FETCH_HEAD
972 test_expect_success '__git_refs
- do not filter refs unless told so
' '
973 cat >expected
<<-EOF &&
978 other/branch-in-other
980 other/matching/branch-in-other
986 __git_refs
>"$actual"
988 test_cmp expected
"$actual"
991 test_expect_success '__git_refs
- only matching refs
' '
992 cat >expected
<<-EOF &&
1000 __git_refs
"" "" "" "$cur" >"$actual"
1002 test_cmp expected
"$actual"
1005 test_expect_success '__git_refs
- only matching refs
- full refs
' '
1006 cat >expected
<<-EOF &&
1007 refs/heads/matching-branch
1008 refs/heads/matching/branch
1011 cur
=refs
/heads
/mat
&&
1012 __git_refs
"" "" "" "$cur" >"$actual"
1014 test_cmp expected
"$actual"
1017 test_expect_success '__git_refs
- only matching refs
- remote on
local file system
' '
1018 cat >expected
<<-EOF &&
1020 matching/branch-in-other
1024 __git_refs otherrepo
"" "" "$cur" >"$actual"
1026 test_cmp expected
"$actual"
1029 test_expect_success '__git_refs
- only matching refs
- configured remote
' '
1030 cat >expected
<<-EOF &&
1032 matching/branch-in-other
1036 __git_refs other
"" "" "$cur" >"$actual"
1038 test_cmp expected
"$actual"
1041 test_expect_success '__git_refs
- only matching refs
- remote
- full refs
' '
1042 cat >expected
<<-EOF &&
1043 refs/heads/main-in-other
1044 refs/heads/matching/branch-in-other
1047 cur
=refs
/heads
/ma
&&
1048 __git_refs other
"" "" "$cur" >"$actual"
1050 test_cmp expected
"$actual"
1053 test_expect_success '__git_refs
- only matching refs
- checkout DWIMery
' '
1054 cat >expected
<<-EOF &&
1059 matching/branch-in-other
1061 for remote_ref
in refs
/remotes
/other
/ambiguous \
1062 refs
/remotes
/remote
/ambiguous \
1063 refs
/remotes
/remote
/branch-in-remote
1065 git update-ref
$remote_ref main
&&
1066 test_when_finished
"git update-ref -d $remote_ref" ||
return 1
1070 __git_refs
"" 1 "" "$cur" >"$actual"
1072 test_cmp expected
"$actual"
1075 test_expect_success 'teardown after filtering matching refs
' '
1076 git branch
-d matching
/branch
&&
1077 git tag
-d matching
/tag
&&
1078 git update-ref
-d refs
/remotes
/other
/matching
/branch-in-other
&&
1079 git
-C otherrepo branch
-D matching
/branch-in-other
1082 test_expect_success '__git_refs
- for-each-ref format specifiers
in prefix
' '
1083 cat >expected
<<-EOF &&
1084 evil-%%-%42-%(refname)..main
1087 cur
="evil-%%-%42-%(refname)..mai" &&
1088 __git_refs
"" "" "evil-%%-%42-%(refname).." mai
>"$actual"
1090 test_cmp expected
"$actual"
1093 test_expect_success '__git_complete_refs
- simple
' '
1094 sed -e "s/Z$//" >expected
<<-EOF &&
1098 other/branch-in-other Z
1099 other/main-in-other Z
1104 __git_complete_refs
&&
1107 test_cmp expected out
1110 test_expect_success '__git_complete_refs
- matching
' '
1111 sed -e "s/Z$//" >expected
<<-EOF &&
1117 __git_complete_refs
&&
1120 test_cmp expected out
1123 test_expect_success '__git_complete_refs
- remote
' '
1124 sed -e "s/Z$//" >expected
<<-EOF &&
1131 __git_complete_refs
--remote=other
&&
1134 test_cmp expected out
1137 test_expect_success '__git_complete_refs
- track
' '
1138 sed -e "s/Z$//" >expected
<<-EOF &&
1142 other/branch-in-other Z
1143 other/main-in-other Z
1150 __git_complete_refs
--track &&
1153 test_cmp expected out
1156 test_expect_success '__git_complete_refs
- current word
' '
1157 sed -e "s/Z$//" >expected
<<-EOF &&
1162 cur
="--option=mat" &&
1163 __git_complete_refs
--cur="${cur#*=}" &&
1166 test_cmp expected out
1169 test_expect_success '__git_complete_refs
- prefix
' '
1170 sed -e "s/Z$//" >expected
<<-EOF &&
1171 v1.0..matching-branch Z
1172 v1.0..matching-tag Z
1176 __git_complete_refs
--pfx=v1.0..
--cur=mat
&&
1179 test_cmp expected out
1182 test_expect_success '__git_complete_refs
- suffix
' '
1183 cat >expected
<<-EOF &&
1187 other/branch-in-other.
1188 other/main-in-other.
1193 __git_complete_refs
--sfx=.
&&
1196 test_cmp expected out
1199 test_expect_success '__git_complete_fetch_refspecs
- simple
' '
1200 sed -e "s/Z$//" >expected
<<-EOF &&
1202 branch-in-other:branch-in-other Z
1203 main-in-other:main-in-other Z
1207 __git_complete_fetch_refspecs other
&&
1210 test_cmp expected out
1213 test_expect_success '__git_complete_fetch_refspecs
- matching
' '
1214 sed -e "s/Z$//" >expected
<<-EOF &&
1215 branch-in-other:branch-in-other Z
1219 __git_complete_fetch_refspecs other
"" br
&&
1222 test_cmp expected out
1225 test_expect_success '__git_complete_fetch_refspecs
- prefix
' '
1226 sed -e "s/Z$//" >expected
<<-EOF &&
1228 +branch-in-other:branch-in-other Z
1229 +main-in-other:main-in-other Z
1233 __git_complete_fetch_refspecs other
"+" "" &&
1236 test_cmp expected out
1239 test_expect_success '__git_complete_fetch_refspecs
- fully qualified
' '
1240 sed -e "s/Z$//" >expected
<<-EOF &&
1241 refs/heads/branch-in-other:refs/heads/branch-in-other Z
1242 refs/heads/main-in-other:refs/heads/main-in-other Z
1243 refs/tags/tag-in-other:refs/tags/tag-in-other Z
1247 __git_complete_fetch_refspecs other
"" refs
/ &&
1250 test_cmp expected out
1253 test_expect_success '__git_complete_fetch_refspecs
- fully qualified
& prefix
' '
1254 sed -e "s/Z$//" >expected
<<-EOF &&
1255 +refs/heads/branch-in-other:refs/heads/branch-in-other Z
1256 +refs/heads/main-in-other:refs/heads/main-in-other Z
1257 +refs/tags/tag-in-other:refs/tags/tag-in-other Z
1261 __git_complete_fetch_refspecs other
+ refs
/ &&
1264 test_cmp expected out
1267 test_expect_success '__git_complete_worktree_paths
' '
1268 test_when_finished
"git worktree remove other_wt" &&
1269 git worktree add
--orphan other_wt
&&
1270 run_completion
"git worktree remove " &&
1274 test_expect_success '__git_complete_worktree_paths
- not a git repository
' '
1277 GIT_CEILING_DIRECTORIES
="$ROOT" &&
1278 export GIT_CEILING_DIRECTORIES
&&
1279 test_completion
"git worktree remove " ""
1283 test_expect_success '__git_complete_worktree_paths with
-C' '
1284 test_when_finished
"git -C otherrepo worktree remove otherrepo_wt" &&
1285 git
-C otherrepo worktree add
--orphan otherrepo_wt
&&
1286 run_completion
"git -C otherrepo worktree remove " &&
1287 grep otherrepo_wt out
1290 test_expect_success 'git switch
- with no options
, complete
local branches and unique remote branch names
for DWIM logic
' '
1291 test_completion
"git switch " <<-\EOF
1299 test_expect_success 'git bisect - when not bisecting
, complete only replay and start subcommands
' '
1300 test_completion
"git bisect " <<-\EOF
1306 test_expect_success 'git bisect - complete options to start subcommand
' '
1307 test_completion
"git bisect start --" <<-\EOF
1317 test_expect_success 'setup for git-bisect tests requiring a repo' '
1318 git init git-bisect &&
1321 echo "initial contents" >file &&
1323 git commit -am "Initial commit" &&
1325 echo "new line" >>file &&
1326 git commit -am "First change" &&
1327 echo "another new line" >>file &&
1328 git commit -am "Second change" &&
1333 test_expect_success 'git bisect - start subcommand arguments before double-dash are completed as revs
' '
1336 test_completion
"git bisect start " <<-\EOF
1345 # Note that these arguments are <pathspec>s, which in practice the fallback
1346 # completion (not the git completion) later ends up completing as paths.
1347 test_expect_success 'git bisect - start subcommand arguments after double-dash are not completed
' '
1350 test_completion
"git bisect start final initial -- " ""
1354 test_expect_success 'setup
for git-bisect tests requiring ongoing bisection
' '
1357 git bisect start
--term-new=custom_new
--term-old=custom_old final initial
1361 test_expect_success 'git-bisect
- when bisecting all subcommands are candidates
' '
1364 test_completion
"git bisect " <<-\EOF
1384 test_expect_success 'git-bisect - options to terms subcommand are candidates
' '
1387 test_completion
"git bisect terms --" <<-\EOF
1396 test_expect_success 'git-bisect - git-log options to visualize subcommand are candidates
' '
1399 # The completion used for git-log and here does not complete
1400 # every git-log option, so rather than hope to stay in sync
1401 # with exactly what it does we will just spot-test here.
1402 test_completion
"git bisect visualize --sta" <<-\EOF &&
1405 test_completion "git bisect visualize --summar" <<-\
EOF
1411 test_expect_success 'git-bisect - view subcommand is not a candidate
' '
1414 test_completion
"git bisect vi" <<-\EOF
1420 test_expect_success 'git-bisect - existing view subcommand is recognized and enables completion of git-log options
' '
1423 # The completion used for git-log and here does not complete
1424 # every git-log option, so rather than hope to stay in sync
1425 # with exactly what it does we will just spot-test here.
1426 test_completion
"git bisect view --sta" <<-\EOF &&
1429 test_completion "git bisect view --summar" <<-\
EOF
1435 test_expect_success 'git checkout - completes refs and unique remote branches
for DWIM
' '
1436 test_completion
"git checkout " <<-\EOF
1443 other/branch-in-other Z
1444 other/main-in-other Z
1448 test_expect_success 'git switch - with
--no-guess, complete only
local branches
' '
1449 test_completion
"git switch --no-guess " <<-\EOF
1455 test_expect_success 'git switch - with GIT_COMPLETION_CHECKOUT_NO_GUESS
=1, complete only
local branches
' '
1456 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git switch " <<-\EOF
1462 test_expect_success 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS
=1, complete
local branches and unique remote names
for DWIM logic
' '
1463 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git switch --guess " <<-\EOF
1471 test_expect_success 'git switch - a later
--guess overrides previous
--no-guess, complete
local and remote unique branches
for DWIM
' '
1472 test_completion
"git switch --no-guess --guess " <<-\EOF
1480 test_expect_success 'git switch - a later
--no-guess overrides previous
--guess, complete only
local branches
' '
1481 test_completion
"git switch --guess --no-guess " <<-\EOF
1487 test_expect_success 'git checkout - with GIT_COMPLETION_NO_GUESS
=1 only completes refs
' '
1488 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git checkout " <<-\EOF
1493 other/branch-in-other Z
1494 other/main-in-other Z
1498 test_expect_success 'git checkout - --guess overrides GIT_COMPLETION_NO_GUESS
=1, complete refs and unique remote branches
for DWIM
' '
1499 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git checkout --guess " <<-\EOF
1506 other/branch-in-other Z
1507 other/main-in-other Z
1511 test_expect_success 'git checkout - with
--no-guess, only completes refs
' '
1512 test_completion
"git checkout --no-guess " <<-\EOF
1517 other/branch-in-other Z
1518 other/main-in-other Z
1522 test_expect_success 'git checkout - a later
--guess overrides previous
--no-guess, complete refs and unique remote branches
for DWIM
' '
1523 test_completion
"git checkout --no-guess --guess " <<-\EOF
1530 other/branch-in-other Z
1531 other/main-in-other Z
1535 test_expect_success 'git checkout - a later
--no-guess overrides previous
--guess, complete only refs
' '
1536 test_completion
"git checkout --guess --no-guess " <<-\EOF
1541 other/branch-in-other Z
1542 other/main-in-other Z
1546 test_expect_success 'git checkout - with checkout.guess
= false
, only completes refs
' '
1547 test_config checkout.guess false
&&
1548 test_completion
"git checkout " <<-\EOF
1553 other/branch-in-other Z
1554 other/main-in-other Z
1558 test_expect_success 'git checkout - with checkout.guess
= true
, completes refs and unique remote branches
for DWIM
' '
1559 test_config checkout.guess true
&&
1560 test_completion
"git checkout " <<-\EOF
1567 other/branch-in-other Z
1568 other/main-in-other Z
1572 test_expect_success 'git checkout - a later
--guess overrides previous checkout.guess
= false
, complete refs and unique remote branches
for DWIM
' '
1573 test_config checkout.guess false
&&
1574 test_completion
"git checkout --guess " <<-\EOF
1581 other/branch-in-other Z
1582 other/main-in-other Z
1586 test_expect_success 'git checkout - a later
--no-guess overrides previous checkout.guess
= true
, complete only refs
' '
1587 test_config checkout.guess true
&&
1588 test_completion
"git checkout --no-guess " <<-\EOF
1593 other/branch-in-other Z
1594 other/main-in-other Z
1598 test_expect_success 'git switch - with
--detach, complete all references
' '
1599 test_completion
"git switch --detach " <<-\EOF
1604 other/branch-in-other Z
1605 other/main-in-other Z
1609 test_expect_success 'git checkout - with
--detach, complete only references
' '
1610 test_completion
"git checkout --detach " <<-\EOF
1615 other/branch-in-other Z
1616 other/main-in-other Z
1620 test_expect_success 'setup sparse-checkout tests' '
1621 # set up sparse-checkout repo
1622 git init sparse-checkout &&
1624 cd sparse-checkout &&
1625 mkdir -p folder1/0/1 folder2/0 folder3 &&
1626 touch folder1/0/1/t.txt &&
1627 touch folder2/0/t.txt &&
1628 touch folder3/t.txt &&
1630 git commit -am "Initial commit"
1634 test_expect_success 'sparse-checkout completes subcommands' '
1635 test_completion "git sparse-checkout " <<-\
EOF
1645 test_expect_success 'cone mode sparse-checkout completes directory names' '
1646 # initialize sparse-checkout definitions
1647 git -C sparse-checkout sparse-checkout set --cone folder1/0 folder3 &&
1649 # test tab completion
1651 cd sparse-checkout &&
1652 test_completion "git sparse-checkout set f" <<-\EOF
1660 cd sparse-checkout &&
1661 test_completion "git sparse-checkout set folder1/" <<-\EOF
1667 cd sparse-checkout &&
1668 test_completion "git sparse-checkout set folder1/0/" <<-\EOF
1674 cd sparse-checkout/folder1 &&
1675 test_completion "git sparse-checkout add 0" <<-\EOF
1681 test_expect_success 'cone mode sparse-checkout completes directory names with spaces and accents' '
1682 # reset sparse-checkout
1683 git -C sparse-checkout sparse-checkout disable &&
1685 cd sparse-checkout &&
1686 mkdir "directory with spaces" &&
1687 mkdir "directory-with-áccent" &&
1688 >"directory with spaces/randomfile" &&
1689 >"directory-with-áccent/randomfile" &&
1691 git commit -m "Add directory with spaces and directory with accent" &&
1692 git sparse-checkout set --cone "directory with spaces" \
1693 "directory-with-áccent" &&
1694 test_completion "git sparse-checkout add dir" <<-\EOF &&
1695 directory with spaces/
1696 directory-with-áccent/
1698 rm -rf "directory with spaces" &&
1699 rm -rf "directory-with-áccent" &&
1701 git commit -m "Remove directory with spaces and directory with accent"
1705 # use FUNNYNAMES to avoid running on Windows, which doesn't permit tabs in paths
1706 test_expect_success FUNNYNAMES 'cone mode sparse-checkout completes directory names with tabs' '
1707 # reset sparse-checkout
1708 git -C sparse-checkout sparse-checkout disable &&
1710 cd sparse-checkout &&
1711 mkdir "$(printf "directory\twith\ttabs")" &&
1712 >"$(printf "directory\twith\ttabs")/randomfile" &&
1714 git commit -m "Add directory with tabs" &&
1715 git sparse-checkout set --cone \
1716 "$(printf "directory\twith\ttabs")" &&
1717 test_completion "git sparse-checkout add dir" <<-\EOF &&
1718 directory with tabs/
1720 rm -rf "$(printf "directory\twith\ttabs")" &&
1722 git commit -m "Remove directory with tabs"
1726 # use FUNNYNAMES to avoid running on Windows, and !CYGWIN for Cygwin, as neither permit backslashes in paths
1727 test_expect_success FUNNYNAMES,!CYGWIN 'cone mode sparse-checkout completes directory names with backslashes' '
1728 # reset sparse-checkout
1729 git -C sparse-checkout sparse-checkout disable &&
1731 cd sparse-checkout &&
1732 mkdir "directory\with\backslashes" &&
1733 >"directory\with\backslashes/randomfile" &&
1735 git commit -m "Add directory with backslashes" &&
1736 git sparse-checkout set --cone \
1737 "directory\with\backslashes" &&
1738 test_completion "git sparse-checkout add dir" <<-\EOF &&
1739 directory\with\backslashes/
1741 rm -rf "directory\with\backslashes" &&
1743 git commit -m "Remove directory with backslashes"
1747 test_expect_success 'non-cone mode sparse-checkout gives rooted paths' '
1748 # reset sparse-checkout repo to non-cone mode
1749 git -C sparse-checkout sparse-checkout disable &&
1750 git -C sparse-checkout sparse-checkout set --no-cone &&
1753 cd sparse-checkout &&
1754 # expected to be empty since we have not configured
1755 # custom completion for non-cone mode
1756 test_completion "git sparse-checkout set f" <<-\EOF
1757 /folder1/0/1/t.txt Z
1760 /folder1/out_sorted Z
1767 test_expect_success 'git sparse-checkout set --cone completes directory names' '
1768 git -C sparse-checkout sparse-checkout disable &&
1771 cd sparse-checkout &&
1772 test_completion "git sparse-checkout set --cone f" <<-\EOF
1780 test_expect_success 'git switch - with
-d, complete all references
' '
1781 test_completion
"git switch -d " <<-\EOF
1786 other/branch-in-other Z
1787 other/main-in-other Z
1791 test_expect_success 'git checkout - with
-d, complete only references
' '
1792 test_completion
"git checkout -d " <<-\EOF
1797 other/branch-in-other Z
1798 other/main-in-other Z
1802 test_expect_success 'git switch - with
--track, complete only remote branches
' '
1803 test_completion
"git switch --track " <<-\EOF &&
1804 other/branch-in-other Z
1805 other/main-in-other Z
1807 test_completion "git switch -t " <<-\
EOF
1808 other/branch-in-other Z
1809 other/main-in-other Z
1813 test_expect_success 'git checkout - with
--track, complete only remote branches
' '
1814 test_completion
"git checkout --track " <<-\EOF &&
1815 other/branch-in-other Z
1816 other/main-in-other Z
1818 test_completion "git checkout -t " <<-\
EOF
1819 other/branch-in-other Z
1820 other/main-in-other Z
1824 test_expect_success 'git switch - with
--no-track, complete only
local branch names
' '
1825 test_completion
"git switch --no-track " <<-\EOF
1831 test_expect_success 'git checkout - with
--no-track, complete only
local references
' '
1832 test_completion
"git checkout --no-track " <<-\EOF
1837 other/branch-in-other Z
1838 other/main-in-other Z
1842 test_expect_success 'git switch - with
-c, complete all references
' '
1843 test_completion
"git switch -c new-branch " <<-\EOF
1848 other/branch-in-other Z
1849 other/main-in-other Z
1853 test_expect_success 'git switch - with
-C, complete all references
' '
1854 test_completion
"git switch -C new-branch " <<-\EOF
1859 other/branch-in-other Z
1860 other/main-in-other Z
1864 test_expect_success 'git switch - with
-c and
--track, complete all references
' '
1865 test_completion
"git switch -c new-branch --track " <<-EOF
1870 other/branch-in-other Z
1871 other/main-in-other Z
1875 test_expect_success 'git switch
- with
-C and
--track, complete all references
' '
1876 test_completion
"git switch -C new-branch --track " <<-EOF
1881 other/branch-in-other Z
1882 other/main-in-other Z
1886 test_expect_success 'git switch
- with
-c and
--no-track, complete all references
' '
1887 test_completion
"git switch -c new-branch --no-track " <<-\EOF
1892 other/branch-in-other Z
1893 other/main-in-other Z
1897 test_expect_success 'git switch - with
-C and
--no-track, complete all references
' '
1898 test_completion
"git switch -C new-branch --no-track " <<-\EOF
1903 other/branch-in-other Z
1904 other/main-in-other Z
1908 test_expect_success 'git checkout - with
-b, complete all references
' '
1909 test_completion
"git checkout -b new-branch " <<-\EOF
1914 other/branch-in-other Z
1915 other/main-in-other Z
1919 test_expect_success 'git checkout - with
-B, complete all references
' '
1920 test_completion
"git checkout -B new-branch " <<-\EOF
1925 other/branch-in-other Z
1926 other/main-in-other Z
1930 test_expect_success 'git checkout - with
-b and
--track, complete all references
' '
1931 test_completion
"git checkout -b new-branch --track " <<-EOF
1936 other/branch-in-other Z
1937 other/main-in-other Z
1941 test_expect_success 'git checkout
- with
-B and
--track, complete all references
' '
1942 test_completion
"git checkout -B new-branch --track " <<-EOF
1947 other/branch-in-other Z
1948 other/main-in-other Z
1952 test_expect_success 'git checkout
- with
-b and
--no-track, complete all references
' '
1953 test_completion
"git checkout -b new-branch --no-track " <<-\EOF
1958 other/branch-in-other Z
1959 other/main-in-other Z
1963 test_expect_success 'git checkout - with
-B and
--no-track, complete all references
' '
1964 test_completion
"git checkout -B new-branch --no-track " <<-\EOF
1969 other/branch-in-other Z
1970 other/main-in-other Z
1974 test_expect_success 'git switch - for -c, complete
local branches and unique remote branches
' '
1975 test_completion
"git switch -c " <<-\EOF
1983 test_expect_success 'git switch - for -C, complete
local branches and unique remote branches
' '
1984 test_completion
"git switch -C " <<-\EOF
1992 test_expect_success 'git switch - for -c with
--no-guess, complete
local branches only
' '
1993 test_completion
"git switch --no-guess -c " <<-\EOF
1999 test_expect_success 'git switch - for -C with
--no-guess, complete
local branches only
' '
2000 test_completion
"git switch --no-guess -C " <<-\EOF
2006 test_expect_success 'git switch - for -c with
--no-track, complete
local branches only
' '
2007 test_completion
"git switch --no-track -c " <<-\EOF
2013 test_expect_success 'git switch - for -C with
--no-track, complete
local branches only
' '
2014 test_completion
"git switch --no-track -C " <<-\EOF
2020 test_expect_success 'git checkout - for -b, complete
local branches and unique remote branches
' '
2021 test_completion
"git checkout -b " <<-\EOF
2029 test_expect_success 'git checkout - for -B, complete
local branches and unique remote branches
' '
2030 test_completion
"git checkout -B " <<-\EOF
2038 test_expect_success 'git checkout - for -b with
--no-guess, complete
local branches only
' '
2039 test_completion
"git checkout --no-guess -b " <<-\EOF
2045 test_expect_success 'git checkout - for -B with
--no-guess, complete
local branches only
' '
2046 test_completion
"git checkout --no-guess -B " <<-\EOF
2052 test_expect_success 'git checkout - for -b with
--no-track, complete
local branches only
' '
2053 test_completion
"git checkout --no-track -b " <<-\EOF
2059 test_expect_success 'git checkout - for -B with
--no-track, complete
local branches only
' '
2060 test_completion
"git checkout --no-track -B " <<-\EOF
2066 test_expect_success 'git switch - with
--orphan completes
local branch names and unique remote branch names
' '
2067 test_completion
"git switch --orphan " <<-\EOF
2075 test_expect_success 'git switch - --orphan with branch already provided completes nothing
else' '
2076 test_completion
"git switch --orphan main " <<-\EOF
2081 test_expect_success 'git checkout - with
--orphan completes
local branch names and unique remote branch names
' '
2082 test_completion
"git checkout --orphan " <<-\EOF
2090 test_expect_success 'git checkout - --orphan with branch already provided completes
local refs
for a start-point
' '
2091 test_completion
"git checkout --orphan main " <<-\EOF
2096 other/branch-in-other Z
2097 other/main-in-other Z
2101 test_expect_success 'git restore completes modified files' '
2102 test_commit A a.file &&
2104 test_completion "git restore a." <<-\
EOF
2109 test_expect_success 'teardown after ref completion' '
2110 git branch -d matching-branch &&
2111 git tag -d matching-tag &&
2112 git remote remove other
2116 test_path_completion ()
2118 test $# = 2 || BUG "not 2 parameters to test_path_completion"
2120 local cur="$1" expected="$2"
2121 echo "$expected" >expected &&
2123 # In the following tests calling this function we only
2124 # care about how __git_complete_index_file() deals with
2125 # unusual characters in path names. By requesting only
2126 # untracked files we do not have to bother adding any
2127 # paths to the index in those tests.
2128 __git_complete_index_file --others &&
2131 test_cmp expected out
2134 test_expect_success 'setup for path completion tests' '
2138 touch simple-dir/simple-file \
2139 "spaces in dir/spaces in file" \
2140 "árvíztűrő/Сайн яваарай" &&
2141 if test_have_prereq !MINGW &&
2143 '$'separators\034in\035dir'' &&
2144 touch BS\\dir/DQ\"file \
2145 '$'separators\034in\035dir/sep\036in\037file''
2147 test_set_prereq FUNNIERNAMES
2149 rm -rf BS\\dir '$'separators\034in\035dir''
2153 test_expect_success '__git_complete_index_file - simple
' '
2154 test_path_completion simple simple-dir
&& # Bash is supposed to
2155 # add the trailing /.
2156 test_path_completion simple-dir
/simple simple-dir
/simple-file
2159 test_expect_success \
2160 '__git_complete_index_file
- escaped characters on cmdline
' '
2161 test_path_completion spac
"spaces in dir" && # Bash will turn this
2162 # into "spaces\ in\ dir"
2163 test_path_completion
"spaces\\ i" \
2165 test_path_completion
"spaces\\ in\\ dir/s" \
2166 "spaces in dir/spaces in file" &&
2167 test_path_completion
"spaces\\ in\\ dir/spaces\\ i" \
2168 "spaces in dir/spaces in file"
2171 test_expect_success \
2172 '__git_complete_index_file
- quoted characters on cmdline
' '
2173 # Testing with an opening but without a corresponding closing
2174 # double quote is important.
2175 test_path_completion
\"spac
"spaces in dir" &&
2176 test_path_completion
"\"spaces i" \
2178 test_path_completion
"\"spaces in dir/s" \
2179 "spaces in dir/spaces in file" &&
2180 test_path_completion
"\"spaces in dir/spaces i" \
2181 "spaces in dir/spaces in file"
2184 test_expect_success '__git_complete_index_file
- UTF-8
in ls-files output
' '
2185 test_path_completion á árvíztűrő
&&
2186 test_path_completion árvíztűrő
/С
"árvíztűrő/Сайн яваарай"
2189 test_expect_success FUNNIERNAMES \
2190 '__git_complete_index_file
- C-style escapes
in ls-files output
' '
2191 test_path_completion BS \
2193 test_path_completion BS
\\\\d \
2195 test_path_completion BS
\\\\dir
/DQ \
2197 test_path_completion BS
\\\\dir
/DQ
\\\"f \
2201 test_expect_success FUNNIERNAMES \
2202 '__git_complete_index_file
- \nnn
-escaped characters
in ls-files output
' '
2203 test_path_completion sep
'$'separators
\034in\035dir
'' &&
2204 test_path_completion
'$'separators
\034i
'' \
2205 '$'separators
\034in\035dir
'' &&
2206 test_path_completion
'$'separators
\034in\035dir
/sep
'' \
2207 '$'separators
\034in\035dir
/sep
\036in\037file'' &&
2208 test_path_completion
'$'separators
\034in\035dir
/sep
\036i
'' \
2209 '$'separators
\034in\035dir
/sep
\036in\037file''
2212 test_expect_success FUNNYNAMES \
2213 '__git_complete_index_file
- removing repeated quoted path components
' '
2214 test_when_finished
rm -r repeated-quoted
&&
2215 mkdir repeated-quoted
&& # A directory whose name in itself
2216 # would not be quoted ...
2217 >repeated-quoted
/0-file &&
2218 >repeated-quoted
/1\"file && # ... but here the file makes the
2219 # dirname quoted ...
2220 >repeated-quoted
/2-file &&
2221 >repeated-quoted
/3\"file && # ... and here, too.
2223 # Still, we shold only list the directory name only once.
2224 test_path_completion repeated repeated-quoted
2227 test_expect_success 'teardown after path completion tests
' '
2228 rm -rf simple-dir
"spaces in dir" árvíztűrő \
2229 BS
\\dir
'$'separators
\034in\035dir
''
2232 test_expect_success '__git_find_on_cmdline
- single match
' '
2233 echo list
>expect
&&
2235 words
=(git
command --opt list
) &&
2236 cword
=${#words[@]} &&
2238 __git_find_on_cmdline
"add list remove" >actual
2240 test_cmp expect actual
2243 test_expect_success '__git_find_on_cmdline
- multiple matches
' '
2244 echo remove
>expect
&&
2246 words
=(git
command -o --opt remove list add
) &&
2247 cword
=${#words[@]} &&
2249 __git_find_on_cmdline
"add list remove" >actual
2251 test_cmp expect actual
2254 test_expect_success '__git_find_on_cmdline
- no match
' '
2256 words
=(git
command --opt branch
) &&
2257 cword
=${#words[@]} &&
2259 __git_find_on_cmdline
"add list remove" >actual
2261 test_must_be_empty actual
2264 test_expect_success '__git_find_on_cmdline
- single match with index
' '
2265 echo "3 list" >expect
&&
2267 words
=(git
command --opt list
) &&
2268 cword
=${#words[@]} &&
2270 __git_find_on_cmdline
--show-idx "add list remove" >actual
2272 test_cmp expect actual
2275 test_expect_success '__git_find_on_cmdline
- multiple matches with index
' '
2276 echo "4 remove" >expect
&&
2278 words
=(git
command -o --opt remove list add
) &&
2279 cword
=${#words[@]} &&
2281 __git_find_on_cmdline
--show-idx "add list remove" >actual
2283 test_cmp expect actual
2286 test_expect_success '__git_find_on_cmdline
- no match with index
' '
2288 words
=(git
command --opt branch
) &&
2289 cword
=${#words[@]} &&
2291 __git_find_on_cmdline
--show-idx "add list remove" >actual
2293 test_must_be_empty actual
2296 test_expect_success '__git_find_on_cmdline
- ignores matches before
command with index
' '
2297 echo "6 remove" >expect
&&
2299 words
=(git
-C remove
command -o --opt remove list add
) &&
2300 cword
=${#words[@]} &&
2302 __git_find_on_cmdline
--show-idx "add list remove" >actual
2304 test_cmp expect actual
2307 test_expect_success '__git_get_config_variables
' '
2308 cat >expect
<<-EOF &&
2312 test_config interesting.name-1 good
&&
2313 test_config interesting.name-2 good
&&
2314 test_config subsection.interesting.name-3 bad
&&
2315 __git_get_config_variables interesting
>actual
&&
2316 test_cmp expect actual
2319 test_expect_success '__git_pretty_aliases
' '
2320 cat >expect
<<-EOF &&
2324 test_config pretty.author
"%an %ae" &&
2325 test_config pretty.
hash %H
&&
2326 __git_pretty_aliases
>actual
&&
2327 test_cmp expect actual
2330 test_expect_success 'basic
' '
2331 run_completion
"git " &&
2333 grep -q "^add \$" out
&&
2335 grep -q "^rebase \$" out
&&
2337 ! grep -q "^ls-files \$" out
&&
2339 run_completion
"git r" &&
2340 ! grep -q -v "^r" out
2343 test_expect_success 'double dash
"git" itself
' '
2344 test_completion
"git --" <<-\EOF
2357 --no-replace-objects Z
2362 test_expect_success 'double dash "git checkout"' '
2363 test_completion "git checkout --" <<-\
EOF
2373 --ignore-skip-worktree-bits Z
2374 --ignore-other-worktrees Z
2375 --recurse-submodules Z
2381 --pathspec-file-nul Z
2382 --pathspec-from-file=Z
2386 test_expect_success 'general options' '
2387 test_completion "git --ver" "--version " &&
2388 test_completion "git --hel" "--help " &&
2389 test_completion "git --exe" <<-\EOF &&
2393 test_completion "git --htm" "--html-path " &&
2394 test_completion "git --pag" "--paginate " &&
2395 test_completion "git --no-p" "--no-pager " &&
2396 test_completion "git --git" "--git-dir=" &&
2397 test_completion "git --wor" "--work-tree=" &&
2398 test_completion "git --nam" "--namespace=" &&
2399 test_completion "git --bar" "--bare " &&
2400 test_completion "git --inf" "--info-path " &&
2401 test_completion "git --no-r" "--no-replace-objects "
2404 test_expect_success 'general options plus command' '
2405 test_completion "git --version check" "checkout " &&
2406 test_completion "git --paginate check" "checkout " &&
2407 test_completion "git --git-dir=foo check" "checkout " &&
2408 test_completion "git --bare check" "checkout " &&
2409 test_completion "git --exec-path=foo check" "checkout " &&
2410 test_completion "git --html-path check" "checkout " &&
2411 test_completion "git --no-pager check" "checkout " &&
2412 test_completion "git --work-tree=foo check" "checkout " &&
2413 test_completion "git --namespace=foo check" "checkout " &&
2414 test_completion "git --paginate check" "checkout " &&
2415 test_completion "git --info-path check" "checkout " &&
2416 test_completion "git --no-replace-objects check" "checkout " &&
2417 test_completion "git --git-dir some/path check" "checkout " &&
2418 test_completion "git -c conf.var=value check" "checkout " &&
2419 test_completion "git -C some/path check" "checkout " &&
2420 test_completion "git --work-tree some/path check" "checkout " &&
2421 test_completion "git --namespace name/space check" "checkout "
2424 test_expect_success 'git --help completion' '
2425 test_completion "git --help ad" "add " &&
2426 test_completion "git --help core" "core-tutorial "
2429 test_expect_success 'completion.commands removes multiple commands' '
2430 test_config completion.commands "-cherry -mergetool" &&
2431 git --list-cmds=list-mainporcelain,list-complete,config >out &&
2432 ! grep -E "^(cherry|mergetool)$" out
2435 test_expect_success 'setup for integration tests' '
2436 echo content >file1 &&
2438 git add file1 file2 &&
2439 git commit -m one &&
2440 git branch mybranch &&
2444 test_expect_success 'checkout completes ref names' '
2445 test_completion "git checkout m" <<-\EOF
2452 test_expect_success 'checkout does not match ref names of a different case' '
2453 test_completion "git checkout M" ""
2456 test_expect_success 'checkout matches case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2458 GIT_COMPLETION_IGNORE_CASE=1 &&
2459 test_completion "git checkout M" <<-\EOF
2467 test_expect_success 'checkout completes pseudo refs' '
2468 test_completion "git checkout H" <<-\EOF
2473 test_expect_success 'checkout completes pseudo refs case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2475 GIT_COMPLETION_IGNORE_CASE=1 &&
2476 test_completion "git checkout h" <<-\EOF
2482 test_expect_success 'git -C <path> checkout uses the right repo' '
2483 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
2488 test_expect_success 'show completes all refs' '
2489 test_completion "git show m" <<-\EOF
2496 test_expect_success '<ref>: completes paths' '
2497 test_completion "git show mytag:f" <<-\EOF
2503 test_expect_success 'complete tree filename with spaces' '
2504 echo content >"name with spaces" &&
2505 git add "name with spaces" &&
2506 git commit -m spaces &&
2507 test_completion "git show HEAD:nam" <<-\EOF
2512 test_expect_success 'complete tree filename with metacharacters' '
2513 echo content >"name with \${meta}" &&
2514 git add "name with \${meta}" &&
2515 git commit -m meta &&
2516 test_completion "git show HEAD:nam" <<-\EOF
2522 test_expect_success 'symbolic-ref completes builtin options' '
2523 test_completion "git symbolic-ref --d" <<-\EOF
2528 test_expect_success 'symbolic-ref completes short ref names' '
2529 test_completion "git symbolic-ref foo m" <<-\EOF
2536 test_expect_success 'symbolic-ref completes full ref names' '
2537 test_completion "git symbolic-ref foo refs/" <<-\EOF
2539 refs/heads/mybranch Z
2545 test_expect_success PERL 'send-email' '
2546 test_completion "git send-email --cov" <<-\EOF &&
2547 --cover-from-description=Z
2550 test_completion "git send-email --val" <<-\EOF &&
2553 test_completion "git send-email ma" "main "
2556 test_expect_success 'complete files' '
2557 git init tmp && cd tmp &&
2558 test_when_finished "cd .. && rm -rf tmp" &&
2560 echo "expected" > .gitignore &&
2561 echo "out" >> .gitignore &&
2562 echo "out_sorted" >> .gitignore &&
2564 git add .gitignore &&
2565 test_completion "git commit " ".gitignore" &&
2567 git commit -m ignore &&
2570 test_completion "git add " "new" &&
2573 git commit -a -m new &&
2574 test_completion "git add " "" &&
2576 git mv new modified &&
2577 echo modify > modified &&
2578 test_completion "git add " "modified" &&
2580 mkdir -p some/deep &&
2581 touch some/deep/path &&
2582 test_completion "git add some/" "some/deep" &&
2583 git clean -f some &&
2587 : TODO .gitignore should not be here &&
2588 test_completion "git rm " <<-\EOF &&
2593 test_completion "git clean " "untracked" &&
2595 : TODO .gitignore should not be here &&
2596 test_completion "git mv " <<-\EOF &&
2602 touch dir/file-in-dir &&
2603 git add dir/file-in-dir &&
2604 git commit -m dir &&
2606 mkdir untracked-dir &&
2608 : TODO .gitignore should not be here &&
2609 test_completion "git mv modified " <<-\EOF &&
2617 test_completion "git commit " "modified" &&
2619 : TODO .gitignore should not be here &&
2620 test_completion "git ls-files " <<-\EOF &&
2627 test_completion "git add mom" "momified"
2630 test_expect_success "simple alias" '
2631 test_config alias.co checkout &&
2632 test_completion "git co m" <<-\EOF
2639 test_expect_success "recursive alias" '
2640 test_config alias.co checkout &&
2641 test_config alias.cod "co --detached" &&
2642 test_completion "git cod m" <<-\EOF
2649 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
2650 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
2651 test_completion "git co m" <<-\EOF
2658 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
2659 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
2660 test_completion "git co m" <<-\EOF
2667 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
2668 test_config alias.co "!f() { : git checkout ; if ... } f" &&
2669 test_completion "git co m" <<-\EOF
2676 test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd> ; ... }' '
2677 test_config alias.co "!f() { : checkout ; if ... } f" &&
2678 test_completion "git co m" <<-\EOF
2685 test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd>; ... }' '
2686 test_config alias.co "!f() { : checkout; if ... } f" &&
2687 test_completion "git co m" <<-\EOF
2694 test_expect_success 'completion without explicit _git_xxx function' '
2695 test_completion "git version --" <<-\EOF
2697 --no-build-options Z
2701 test_expect_failure 'complete with tilde expansion' '
2702 git init tmp && cd tmp &&
2703 test_when_finished "cd .. && rm -rf tmp" &&
2707 test_completion "git add ~/tmp/" "~/tmp/file"
2710 test_expect_success 'setup other remote for remote reference completion' '
2711 git remote add other otherrepo &&
2715 for flag in -d --delete
2717 test_expect_success "__git_complete_remote_or_refspec - push
$flag other
" '
2718 sed -e "s
/Z$
//" >expected <<-EOF &&
2722 words=(git push '$flag' other ma) &&
2723 cword=${#words[@]} cur=${words[cword-1]} &&
2725 __git_complete_remote_or_refspec &&
2728 test_cmp expected out
2731 test_expect_failure "__git_complete_remote_or_refspec
- push other
$flag" '
2732 sed -e "s
/Z$
//" >expected <<-EOF &&
2736 words=(git push other '$flag' ma) &&
2737 cword=${#words[@]} cur=${words[cword-1]} &&
2739 __git_complete_remote_or_refspec &&
2742 test_cmp expected out
2746 test_expect_success 'git config subcommand' '
2747 test_completion "git config
" <<-\EOF
2758 test_expect_success 'git config subcommand options' '
2759 test_completion "git config get
--show-" <<-\EOF
2766 test_expect_success 'git config get' '
2767 test_when_finished "rm -f cfgfile
" &&
2768 git config set --file cfgfile foo.bar baz &&
2769 test_completion "git config get
--file cfgfile foo.
" <<-\EOF
2774 test_expect_success 'git config set - section' '
2775 test_completion "git config
set br
" <<-\EOF
2781 test_expect_success 'git config set - section include, includeIf' '
2782 test_completion "git config
set inclu
" <<-\EOF
2788 test_expect_success 'git config set - variable name' '
2789 test_completion "git config
set log.d
" <<-\EOF
2796 test_expect_success 'git config set - variable name include' '
2797 test_completion "git config
set include.p
" <<-\EOF
2802 test_expect_success 'setup for git config submodule tests' '
2803 test_create_repo sub &&
2804 test_commit -C sub initial &&
2805 git submodule add ./sub
2808 test_expect_success 'git config set - variable name - submodule and __git_compute_first_level_config_vars_for_section' '
2809 test_completion "git config
set submodule.
" <<-\EOF
2811 submodule.alternateErrorStrategy Z
2812 submodule.alternateLocation Z
2813 submodule.fetchJobs Z
2814 submodule.propagateBranches Z
2820 test_expect_success 'git config set - variable name - __git_compute_second_level_config_vars_for_section' '
2821 test_completion "git config
set submodule.sub.
" <<-\EOF
2823 submodule.sub.update Z
2824 submodule.sub.branch Z
2825 submodule.sub.fetchRecurseSubmodules Z
2826 submodule.sub.ignore Z
2827 submodule.sub.active Z
2831 test_expect_success 'git config set - value' '
2832 test_completion "git config
set color.pager
" <<-\EOF
2838 test_expect_success 'git -c - section' '
2839 test_completion "git
-c br
" <<-\EOF
2845 test_expect_success 'git -c - variable name' '
2846 test_completion "git
-c log.d
" <<-\EOF
2853 test_expect_success 'git -c - value' '
2854 test_completion "git
-c color.pager
=" <<-\EOF
2860 test_expect_success 'git clone --config= - section' '
2861 test_completion "git clone
--config=br
" <<-\EOF
2867 test_expect_success 'git clone --config= - variable name' '
2868 test_completion "git clone
--config=log.d
" <<-\EOF
2875 test_expect_success 'git clone --config= - value' '
2876 test_completion "git clone
--config=color.pager
=" <<-\EOF
2882 test_expect_success 'git reflog show' '
2883 test_when_finished "git checkout
- && git branch
-d shown
" &&
2884 git checkout -b shown &&
2885 test_completion "git reflog sho
" <<-\EOF &&
2889 test_completion "git reflog show sho
" "shown
" &&
2890 test_completion "git reflog shown sho
" "shown
" &&
2891 test_completion "git reflog
--unt" "--until=" &&
2892 test_completion "git reflog show
--unt" "--until=" &&
2893 test_completion "git reflog shown
--unt" "--until="
2896 test_expect_success 'options with value' '
2897 test_completion "git merge
-X diff-algorithm
=" <<-\EOF
2902 test_expect_success 'sourcing the completion script clears cached commands' '
2904 __git_compute_all_commands &&
2905 test -n "$__git_all_commands" &&
2906 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2907 test -z "$__git_all_commands"
2911 test_expect_success 'sourcing the completion script clears cached merge strategies' '
2913 __git_compute_merge_strategies &&
2914 test -n "$__git_merge_strategies" &&
2915 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2916 test -z "$__git_merge_strategies"
2920 test_expect_success 'sourcing the completion script clears cached --options' '
2922 __gitcomp_builtin checkout &&
2923 test -n "$__gitcomp_builtin_checkout" &&
2924 __gitcomp_builtin notes_edit &&
2925 test -n "$__gitcomp_builtin_notes_edit" &&
2926 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2927 test -z "$__gitcomp_builtin_checkout" &&
2928 test -z "$__gitcomp_builtin_notes_edit"
2932 test_expect_success 'option aliases are not shown by default' '
2933 test_completion "git clone
--recurs" "--recurse-submodules "
2936 test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
2938 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2939 GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2940 test_completion "git clone
--recurs" <<-\EOF
2941 --recurse-submodules Z
2947 test_expect_success 'plumbing commands are excluded without GIT_COMPLETION_SHOW_ALL_COMMANDS' '
2949 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2950 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2952 # Just mainporcelain, not plumbing commands
2953 run_completion "git c
" &&
2954 grep checkout out &&
2959 test_expect_success 'all commands are shown with GIT_COMPLETION_SHOW_ALL_COMMANDS (also main non-builtin)' '
2961 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2962 GIT_COMPLETION_SHOW_ALL_COMMANDS=1 &&
2963 export GIT_COMPLETION_SHOW_ALL_COMMANDS &&
2964 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2966 # Both mainporcelain and plumbing commands
2967 run_completion "git c
" &&
2968 grep checkout out &&
2969 grep cat-file out &&
2971 # Check "gitk
", a "main
" command, but not a built-in + more plumbing
2972 run_completion "git g
" &&
2974 grep get-tar-commit-id out
2978 test_expect_success '__git_complete' '
2979 unset -f __git_wrap__git_main &&
2981 __git_complete foo __git_main &&
2982 __git_have_func __git_wrap__git_main &&
2983 unset -f __git_wrap__git_main &&
2985 __git_complete gf _git_fetch &&
2986 __git_have_func __git_wrap_git_fetch &&
2988 __git_complete foo git &&
2989 __git_have_func __git_wrap__git_main &&
2990 unset -f __git_wrap__git_main &&
2992 __git_complete gd git_diff &&
2993 __git_have_func __git_wrap_git_diff &&
2995 test_must_fail __git_complete ga missing
2998 test_expect_success '__git_pseudoref_exists' '
2999 test_when_finished "rm -rf repo
" &&
3003 sane_unset __git_repo_path &&
3005 # HEAD should exist, even if it points to an unborn branch.
3006 __git_pseudoref_exists HEAD >output 2>&1 &&
3007 test_must_be_empty output &&
3009 # HEAD points to an existing branch, so it should exist.
3011 __git_pseudoref_exists HEAD >output 2>&1 &&
3012 test_must_be_empty output &&
3014 # CHERRY_PICK_HEAD does not exist, so the existence check should fail.
3015 ! __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
3016 test_must_be_empty output &&
3018 # CHERRY_PICK_HEAD points to a commit, so it should exist.
3019 git update-ref CHERRY_PICK_HEAD A &&
3020 __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
3021 test_must_be_empty output