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
27 # Be careful when updating these lists:
29 # (1) The build tree may have build artifact from different branch, or
30 # the user's $PATH may have a random executable that may begin
31 # with "git-check" that are not part of the subcommands this build
32 # will ship, e.g. "check-ignore". The tests for completion for
33 # subcommand names tests how "check" is expanded; we limit the
34 # possible candidates to "checkout" and "check-attr" to make sure
35 # "check-attr", which is known by the filter function as a
36 # subcommand to be thrown out, while excluding other random files
37 # that happen to begin with "check" to avoid letting them get in
40 # (2) A test makes sure that common subcommands are included in the
41 # completion for "git <TAB>", and a plumbing is excluded. "add",
42 # "rebase" and "ls-files" are listed for this.
44 GIT_TESTING_ALL_COMMAND_LIST
='add checkout check-attr rebase ls-files'
45 GIT_TESTING_PORCELAIN_COMMAND_LIST
='add checkout rebase'
47 .
"$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
49 # We don't need this function to actually join words or do anything special.
50 # Also, it's cleaner to avoid touching bash's internal completion variables.
51 # So let's override it with a minimal version for testing purposes.
52 _get_comp_words_by_ref
()
54 while [ $# -gt 0 ]; do
60 prev
=${_words[_cword-1]}
63 words
=("${_words[@]}")
76 echo "${COMPREPLY[*]}" > out
81 local -a COMPREPLY _words
84 test "${1: -1}" = ' ' && _words
[${#_words[@]}+1]=''
85 (( _cword
= ${#_words[@]} - 1 ))
86 __git_wrap__git_main
&& print_comp
89 # Test high-level completion
91 # 1: typed text so far (cur)
92 # 2: expected completion
97 printf '%s\n' "$2" >expected
99 sed -e 's/Z$//' |
sort >expected
101 run_completion
"$1" >"$TRASH_DIRECTORY"/bash-completion-output
2>&1 &&
102 sort out
>out_sorted
&&
103 test_cmp expected out_sorted
&&
104 test_must_be_empty
"$TRASH_DIRECTORY"/bash-completion-output
&&
105 rm "$TRASH_DIRECTORY"/bash-completion-output
109 # The first argument is the typed text so far (cur); the rest are
110 # passed to __gitcomp. Expected output comes is read from the
111 # standard input, like test_completion().
114 local -a COMPREPLY
&&
115 sed -e 's/Z$//' >expected
&&
120 test_cmp expected out
125 # 1: current word (cur)
126 # -: the rest are passed to __gitcomp_nl
129 local -a COMPREPLY
&&
130 sed -e 's/Z$//' >expected
&&
135 test_cmp expected out
138 invalid_variable_name
='${foo.bar}'
140 actual
="$TRASH_DIRECTORY/actual"
142 if test_have_prereq MINGW
149 test_expect_success
'setup for __git_find_repo_path/__gitdir tests' '
150 mkdir -p subdir/subsubdir &&
152 git init -b main otherrepo
155 test_expect_success
'__git_find_repo_path - from command line (through $__git_dir)' '
156 echo "$ROOT/otherrepo/.git" >expected &&
158 __git_dir="$ROOT/otherrepo/.git" &&
159 __git_find_repo_path &&
160 echo "$__git_repo_path" >"$actual"
162 test_cmp expected "$actual"
165 test_expect_success
'__git_find_repo_path - .git directory in cwd' '
166 echo ".git" >expected &&
168 __git_find_repo_path &&
169 echo "$__git_repo_path" >"$actual"
171 test_cmp expected "$actual"
174 test_expect_success
'__git_find_repo_path - .git directory in parent' '
175 echo "$ROOT/.git" >expected &&
177 cd subdir/subsubdir &&
178 __git_find_repo_path &&
179 echo "$__git_repo_path" >"$actual"
181 test_cmp expected "$actual"
184 test_expect_success
'__git_find_repo_path - cwd is a .git directory' '
185 echo "." >expected &&
188 __git_find_repo_path &&
189 echo "$__git_repo_path" >"$actual"
191 test_cmp expected "$actual"
194 test_expect_success
'__git_find_repo_path - parent is a .git directory' '
195 echo "$ROOT/.git" >expected &&
198 __git_find_repo_path &&
199 echo "$__git_repo_path" >"$actual"
201 test_cmp expected "$actual"
204 test_expect_success
'__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
205 echo "$ROOT/otherrepo/.git" >expected &&
207 GIT_DIR="$ROOT/otherrepo/.git" &&
209 __git_find_repo_path &&
210 echo "$__git_repo_path" >"$actual"
212 test_cmp expected "$actual"
215 test_expect_success
'__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
216 echo "$ROOT/otherrepo/.git" >expected &&
218 GIT_DIR="$ROOT/otherrepo/.git" &&
221 __git_find_repo_path &&
222 echo "$__git_repo_path" >"$actual"
224 test_cmp expected "$actual"
227 test_expect_success
'__git_find_repo_path - from command line while "git -C"' '
228 echo "$ROOT/.git" >expected &&
230 __git_dir="$ROOT/.git" &&
231 __git_C_args=(-C otherrepo) &&
232 __git_find_repo_path &&
233 echo "$__git_repo_path" >"$actual"
235 test_cmp expected "$actual"
238 test_expect_success
'__git_find_repo_path - relative dir from command line and "git -C"' '
239 echo "$ROOT/otherrepo/.git" >expected &&
242 __git_dir="otherrepo/.git" &&
243 __git_C_args=(-C ..) &&
244 __git_find_repo_path &&
245 echo "$__git_repo_path" >"$actual"
247 test_cmp expected "$actual"
250 test_expect_success
'__git_find_repo_path - $GIT_DIR set while "git -C"' '
251 echo "$ROOT/.git" >expected &&
253 GIT_DIR="$ROOT/.git" &&
255 __git_C_args=(-C otherrepo) &&
256 __git_find_repo_path &&
257 echo "$__git_repo_path" >"$actual"
259 test_cmp expected "$actual"
262 test_expect_success
'__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
263 echo "$ROOT/otherrepo/.git" >expected &&
266 GIT_DIR="otherrepo/.git" &&
268 __git_C_args=(-C ..) &&
269 __git_find_repo_path &&
270 echo "$__git_repo_path" >"$actual"
272 test_cmp expected "$actual"
275 test_expect_success
'__git_find_repo_path - "git -C" while .git directory in cwd' '
276 echo "$ROOT/otherrepo/.git" >expected &&
278 __git_C_args=(-C otherrepo) &&
279 __git_find_repo_path &&
280 echo "$__git_repo_path" >"$actual"
282 test_cmp expected "$actual"
285 test_expect_success
'__git_find_repo_path - "git -C" while cwd is a .git directory' '
286 echo "$ROOT/otherrepo/.git" >expected &&
289 __git_C_args=(-C .. -C otherrepo) &&
290 __git_find_repo_path &&
291 echo "$__git_repo_path" >"$actual"
293 test_cmp expected "$actual"
296 test_expect_success
'__git_find_repo_path - "git -C" while .git directory in parent' '
297 echo "$ROOT/otherrepo/.git" >expected &&
300 __git_C_args=(-C .. -C otherrepo) &&
301 __git_find_repo_path &&
302 echo "$__git_repo_path" >"$actual"
304 test_cmp expected "$actual"
307 test_expect_success
'__git_find_repo_path - non-existing path in "git -C"' '
309 __git_C_args=(-C non-existing) &&
310 test_must_fail __git_find_repo_path &&
311 printf "$__git_repo_path" >"$actual"
313 test_must_be_empty "$actual"
316 test_expect_success
'__git_find_repo_path - non-existing path in $__git_dir' '
318 __git_dir="non-existing" &&
319 test_must_fail __git_find_repo_path &&
320 printf "$__git_repo_path" >"$actual"
322 test_must_be_empty "$actual"
325 test_expect_success
'__git_find_repo_path - non-existing $GIT_DIR' '
327 GIT_DIR="$ROOT/non-existing" &&
329 test_must_fail __git_find_repo_path &&
330 printf "$__git_repo_path" >"$actual"
332 test_must_be_empty "$actual"
335 test_expect_success
'__git_find_repo_path - gitfile in cwd' '
336 echo "$ROOT/otherrepo/.git" >expected &&
337 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
338 test_when_finished "rm -f subdir/.git" &&
341 __git_find_repo_path &&
342 echo "$__git_repo_path" >"$actual"
344 test_cmp expected "$actual"
347 test_expect_success
'__git_find_repo_path - gitfile in parent' '
348 echo "$ROOT/otherrepo/.git" >expected &&
349 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
350 test_when_finished "rm -f subdir/.git" &&
352 cd subdir/subsubdir &&
353 __git_find_repo_path &&
354 echo "$__git_repo_path" >"$actual"
356 test_cmp expected "$actual"
359 test_expect_success SYMLINKS
'__git_find_repo_path - resulting path avoids symlinks' '
360 echo "$ROOT/otherrepo/.git" >expected &&
361 mkdir otherrepo/dir &&
362 test_when_finished "rm -rf otherrepo/dir" &&
363 ln -s otherrepo/dir link &&
364 test_when_finished "rm -f link" &&
367 __git_find_repo_path &&
368 echo "$__git_repo_path" >"$actual"
370 test_cmp expected "$actual"
373 test_expect_success
'__git_find_repo_path - not a git repository' '
376 GIT_CEILING_DIRECTORIES="$ROOT" &&
377 export GIT_CEILING_DIRECTORIES &&
378 test_must_fail __git_find_repo_path &&
379 printf "$__git_repo_path" >"$actual"
381 test_must_be_empty "$actual"
384 test_expect_success
'__gitdir - finds repo' '
385 echo "$ROOT/.git" >expected &&
387 cd subdir/subsubdir &&
390 test_cmp expected "$actual"
394 test_expect_success
'__gitdir - returns error when cannot find repo' '
396 __git_dir="non-existing" &&
397 test_must_fail __gitdir >"$actual"
399 test_must_be_empty "$actual"
402 test_expect_success
'__gitdir - repo as argument' '
403 echo "otherrepo/.git" >expected &&
405 __gitdir "otherrepo" >"$actual"
407 test_cmp expected "$actual"
410 test_expect_success
'__gitdir - remote as argument' '
411 echo "remote" >expected &&
413 __gitdir "remote" >"$actual"
415 test_cmp expected "$actual"
419 test_expect_success
'__git_dequote - plain unquoted word' '
420 __git_dequote unquoted-word &&
421 test unquoted-word = "$dequoted_word"
424 # input: b\a\c\k\'\\\"s\l\a\s\h\es
425 # expected: back'\"slashes
426 test_expect_success
'__git_dequote - backslash escaped' '
427 __git_dequote "b\a\c\k\\'\''\\\\\\\"s\l\a\s\h\es" &&
428 test "back'\''\\\"slashes" = "$dequoted_word"
431 # input: sin'gle\' '"quo'ted
432 # expected: single\ "quoted
433 test_expect_success
'__git_dequote - single quoted' '
434 __git_dequote "'"sin'gle\\\\' '\\\"quo'ted"'" &&
435 test '\''single\ "quoted'\'' = "$dequoted_word"
438 # input: dou"ble\\" "\"\quot"ed
439 # expected: double\ "\quoted
440 test_expect_success
'__git_dequote - double quoted' '
441 __git_dequote '\''dou"ble\\" "\"\quot"ed'\'' &&
442 test '\''double\ "\quoted'\'' = "$dequoted_word"
445 # input: 'open single quote
446 test_expect_success
'__git_dequote - open single quote' '
447 __git_dequote "'\''open single quote" &&
448 test "open single quote" = "$dequoted_word"
451 # input: "open double quote
452 test_expect_success
'__git_dequote - open double quote' '
453 __git_dequote "\"open double quote" &&
454 test "open double quote" = "$dequoted_word"
458 test_expect_success
'__gitcomp_direct - puts everything into COMPREPLY as-is' '
459 sed -e "s/Z$//g" >expected <<-EOF &&
460 with-trailing-space Z
461 without-trailing-spaceZ
464 $invalid_variable_name Z
467 cur=should_be_ignored &&
468 __gitcomp_direct "$(cat expected)" &&
471 test_cmp expected out
474 test_expect_success
'__gitcomp - trailing space - options' '
475 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
476 --reset-author" <<-EOF
483 test_expect_success
'__gitcomp - trailing space - config keys' '
484 test_gitcomp "br" "branch. branch.autosetupmerge
485 branch.autosetuprebase browser." <<-\EOF
487 branch.autosetupmerge Z
488 branch.autosetuprebase Z
493 test_expect_success
'__gitcomp - option parameter' '
494 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
501 test_expect_success
'__gitcomp - prefix' '
502 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
503 "branch.maint." "me" <<-\EOF
505 branch.maint.mergeoptions Z
509 test_expect_success
'__gitcomp - suffix' '
510 test_gitcomp "branch.me" "master maint next seen" "branch." \
517 test_expect_success
'__gitcomp - ignore optional negative options' '
518 test_gitcomp "--" "--abc --def --no-one -- --no-two" <<-\EOF
526 test_expect_success
'__gitcomp - ignore/narrow optional negative options' '
527 test_gitcomp "--a" "--abc --abcdef --no-one -- --no-two" <<-\EOF
533 test_expect_success
'__gitcomp - ignore/narrow optional negative options' '
534 test_gitcomp "--n" "--abc --def --no-one -- --no-two" <<-\EOF
540 test_expect_success
'__gitcomp - expand all negative options' '
541 test_gitcomp "--no-" "--abc --def --no-one -- --no-two" <<-\EOF
547 test_expect_success
'__gitcomp - expand/narrow all negative options' '
548 test_gitcomp "--no-o" "--abc --def --no-one -- --no-two" <<-\EOF
553 test_expect_success
'__gitcomp - equal skip' '
554 test_gitcomp "--option=" "--option=" <<-\EOF &&
557 test_gitcomp "option=" "option=" <<-\EOF
562 test_expect_success
'__gitcomp - doesnt fail because of invalid variable name' '
563 __gitcomp "$invalid_variable_name"
566 read -r -d "" refs
<<-\EOF
573 test_expect_success '__gitcomp_nl - trailing space
' '
574 test_gitcomp_nl
"m" "$refs" <<-EOF
580 test_expect_success '__gitcomp_nl
- prefix
' '
581 test_gitcomp_nl
"--fixup=m" "$refs" "--fixup=" "m" <<-EOF
587 test_expect_success '__gitcomp_nl
- suffix
' '
588 test_gitcomp_nl
"branch.ma" "$refs" "branch." "ma" "." <<-\EOF
594 test_expect_success '__gitcomp_nl - no suffix
' '
595 test_gitcomp_nl
"ma" "$refs" "" "ma" "" <<-\EOF
601 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name
' '
602 __gitcomp_nl
"$invalid_variable_name"
605 test_expect_success '__git_remotes
- list remotes from
$GIT_DIR/remotes and from config
file' '
606 cat >expect
<<-EOF &&
612 test_when_finished
"rm -rf .git/remotes" &&
613 mkdir
-p .git
/remotes
&&
614 >.git
/remotes
/remote_from_file_1
&&
615 >.git
/remotes
/remote_from_file_2
&&
616 test_when_finished
"git remote remove remote_in_config_1" &&
617 git remote add remote_in_config_1 git
://remote_1
&&
618 test_when_finished
"git remote remove remote_in_config_2" &&
619 git remote add remote_in_config_2 git
://remote_2
&&
621 __git_remotes
>actual
623 test_cmp expect actual
626 test_expect_success '__git_is_configured_remote
' '
627 test_when_finished
"git remote remove remote_1" &&
628 git remote add remote_1 git
://remote_1
&&
629 test_when_finished
"git remote remove remote_2" &&
630 git remote add remote_2 git
://remote_2
&&
632 __git_is_configured_remote remote_2
&&
633 test_must_fail __git_is_configured_remote non-existent
637 test_expect_success 'setup
for ref completion
' '
638 git commit
--allow-empty -m initial
&&
639 git branch
-M main
&&
640 git branch matching-branch
&&
641 git tag matching-tag
&&
644 git commit
--allow-empty -m initial
&&
645 git branch
-m main main-in-other
&&
646 git branch branch-in-other
&&
649 git remote add other
"$ROOT/otherrepo/.git" &&
650 git fetch
--no-tags other
&&
651 rm -f .git
/FETCH_HEAD
&&
655 test_expect_success '__git_refs
- simple
' '
656 cat >expected
<<-EOF &&
660 other/branch-in-other
666 __git_refs
>"$actual"
668 test_cmp expected
"$actual"
671 test_expect_success '__git_refs
- full refs
' '
672 cat >expected
<<-EOF &&
674 refs/heads/matching-branch
675 refs/remotes/other/branch-in-other
676 refs/remotes/other/main-in-other
677 refs/tags/matching-tag
681 __git_refs
>"$actual"
683 test_cmp expected
"$actual"
686 test_expect_success '__git_refs
- repo given on the
command line
' '
687 cat >expected
<<-EOF &&
694 __git_dir
="$ROOT/otherrepo/.git" &&
696 __git_refs
>"$actual"
698 test_cmp expected
"$actual"
701 test_expect_success '__git_refs
- remote on
local file system
' '
702 cat >expected
<<-EOF &&
710 __git_refs otherrepo
>"$actual"
712 test_cmp expected
"$actual"
715 test_expect_success '__git_refs
- remote on
local file system
- full refs
' '
716 cat >expected
<<-EOF &&
717 refs/heads/branch-in-other
718 refs/heads/main-in-other
719 refs/tags/tag-in-other
723 __git_refs otherrepo
>"$actual"
725 test_cmp expected
"$actual"
728 test_expect_success '__git_refs
- configured remote
' '
729 cat >expected
<<-EOF &&
736 __git_refs other
>"$actual"
738 test_cmp expected
"$actual"
741 test_expect_success '__git_refs
- configured remote
- full refs
' '
742 cat >expected
<<-EOF &&
744 refs/heads/branch-in-other
745 refs/heads/main-in-other
746 refs/tags/tag-in-other
750 __git_refs other
>"$actual"
752 test_cmp expected
"$actual"
755 test_expect_success '__git_refs
- configured remote
- repo given on the
command line
' '
756 cat >expected
<<-EOF &&
763 __git_dir
="$ROOT/.git" &&
765 __git_refs other
>"$actual"
767 test_cmp expected
"$actual"
770 test_expect_success '__git_refs
- configured remote
- full refs
- repo given on the
command line
' '
771 cat >expected
<<-EOF &&
773 refs/heads/branch-in-other
774 refs/heads/main-in-other
775 refs/tags/tag-in-other
779 __git_dir
="$ROOT/.git" &&
781 __git_refs other
>"$actual"
783 test_cmp expected
"$actual"
786 test_expect_success '__git_refs
- configured remote
- remote name matches a directory
' '
787 cat >expected
<<-EOF &&
793 test_when_finished
"rm -rf other" &&
796 __git_refs other
>"$actual"
798 test_cmp expected
"$actual"
801 test_expect_success '__git_refs
- URL remote
' '
802 cat >expected
<<-EOF &&
810 __git_refs
"file://$ROOT/otherrepo/.git" >"$actual"
812 test_cmp expected
"$actual"
815 test_expect_success '__git_refs
- URL remote
- full refs
' '
816 cat >expected
<<-EOF &&
818 refs/heads/branch-in-other
819 refs/heads/main-in-other
820 refs/tags/tag-in-other
824 __git_refs
"file://$ROOT/otherrepo/.git" >"$actual"
826 test_cmp expected
"$actual"
829 test_expect_success '__git_refs
- non-existing remote
' '
832 __git_refs non-existing
>"$actual"
834 test_must_be_empty
"$actual"
837 test_expect_success '__git_refs
- non-existing remote
- full refs
' '
840 __git_refs non-existing
>"$actual"
842 test_must_be_empty
"$actual"
845 test_expect_success '__git_refs
- non-existing URL remote
' '
848 __git_refs
"file://$ROOT/non-existing" >"$actual"
850 test_must_be_empty
"$actual"
853 test_expect_success '__git_refs
- non-existing URL remote
- full refs
' '
856 __git_refs
"file://$ROOT/non-existing" >"$actual"
858 test_must_be_empty
"$actual"
861 test_expect_success '__git_refs
- not
in a git repository
' '
863 GIT_CEILING_DIRECTORIES
="$ROOT" &&
864 export GIT_CEILING_DIRECTORIES
&&
867 __git_refs
>"$actual"
869 test_must_be_empty
"$actual"
872 test_expect_success '__git_refs
- unique remote branches
for git checkout DWIMery
' '
873 cat >expected
<<-EOF &&
878 other/branch-in-other
881 remote/branch-in-remote
887 for remote_ref
in refs
/remotes
/other
/ambiguous \
888 refs
/remotes
/remote
/ambiguous \
889 refs
/remotes
/remote
/branch-in-remote
891 git update-ref
$remote_ref main
&&
892 test_when_finished
"git update-ref -d $remote_ref" ||
return 1
896 __git_refs
"" 1 >"$actual"
898 test_cmp expected
"$actual"
901 test_expect_success '__git_refs
- after
--opt=' '
902 cat >expected
<<-EOF &&
906 other/branch-in-other
912 __git_refs
"" "" "" "" >"$actual"
914 test_cmp expected
"$actual"
917 test_expect_success '__git_refs
- after
--opt= - full refs
' '
918 cat >expected
<<-EOF &&
920 refs/heads/matching-branch
921 refs/remotes/other/branch-in-other
922 refs/remotes/other/main-in-other
923 refs/tags/matching-tag
927 __git_refs
"" "" "" refs
/ >"$actual"
929 test_cmp expected
"$actual"
932 test_expect_success '__git refs
- excluding refs
' '
933 cat >expected
<<-EOF &&
937 ^other/branch-in-other
943 __git_refs
>"$actual"
945 test_cmp expected
"$actual"
948 test_expect_success '__git refs
- excluding full refs
' '
949 cat >expected
<<-EOF &&
951 ^refs/heads/matching-branch
952 ^refs/remotes/other/branch-in-other
953 ^refs/remotes/other/main-in-other
954 ^refs/tags/matching-tag
958 __git_refs
>"$actual"
960 test_cmp expected
"$actual"
963 test_expect_success 'setup
for filtering matching refs
' '
964 git branch matching
/branch
&&
965 git tag matching
/tag
&&
966 git
-C otherrepo branch matching
/branch-in-other
&&
967 git fetch
--no-tags other
&&
968 rm -f .git
/FETCH_HEAD
971 test_expect_success '__git_refs
- do not filter refs unless told so
' '
972 cat >expected
<<-EOF &&
977 other/branch-in-other
979 other/matching/branch-in-other
985 __git_refs
>"$actual"
987 test_cmp expected
"$actual"
990 test_expect_success '__git_refs
- only matching refs
' '
991 cat >expected
<<-EOF &&
999 __git_refs
"" "" "" "$cur" >"$actual"
1001 test_cmp expected
"$actual"
1004 test_expect_success '__git_refs
- only matching refs
- full refs
' '
1005 cat >expected
<<-EOF &&
1006 refs/heads/matching-branch
1007 refs/heads/matching/branch
1010 cur
=refs
/heads
/mat
&&
1011 __git_refs
"" "" "" "$cur" >"$actual"
1013 test_cmp expected
"$actual"
1016 test_expect_success '__git_refs
- only matching refs
- remote on
local file system
' '
1017 cat >expected
<<-EOF &&
1019 matching/branch-in-other
1023 __git_refs otherrepo
"" "" "$cur" >"$actual"
1025 test_cmp expected
"$actual"
1028 test_expect_success '__git_refs
- only matching refs
- configured remote
' '
1029 cat >expected
<<-EOF &&
1031 matching/branch-in-other
1035 __git_refs other
"" "" "$cur" >"$actual"
1037 test_cmp expected
"$actual"
1040 test_expect_success '__git_refs
- only matching refs
- remote
- full refs
' '
1041 cat >expected
<<-EOF &&
1042 refs/heads/main-in-other
1043 refs/heads/matching/branch-in-other
1046 cur
=refs
/heads
/ma
&&
1047 __git_refs other
"" "" "$cur" >"$actual"
1049 test_cmp expected
"$actual"
1052 test_expect_success '__git_refs
- only matching refs
- checkout DWIMery
' '
1053 cat >expected
<<-EOF &&
1058 matching/branch-in-other
1060 for remote_ref
in refs
/remotes
/other
/ambiguous \
1061 refs
/remotes
/remote
/ambiguous \
1062 refs
/remotes
/remote
/branch-in-remote
1064 git update-ref
$remote_ref main
&&
1065 test_when_finished
"git update-ref -d $remote_ref" ||
return 1
1069 __git_refs
"" 1 "" "$cur" >"$actual"
1071 test_cmp expected
"$actual"
1074 test_expect_success 'teardown after filtering matching refs
' '
1075 git branch
-d matching
/branch
&&
1076 git tag
-d matching
/tag
&&
1077 git update-ref
-d refs
/remotes
/other
/matching
/branch-in-other
&&
1078 git
-C otherrepo branch
-D matching
/branch-in-other
1081 test_expect_success '__git_refs
- for-each-ref format specifiers
in prefix
' '
1082 cat >expected
<<-EOF &&
1083 evil-%%-%42-%(refname)..main
1086 cur
="evil-%%-%42-%(refname)..mai" &&
1087 __git_refs
"" "" "evil-%%-%42-%(refname).." mai
>"$actual"
1089 test_cmp expected
"$actual"
1092 test_expect_success '__git_complete_refs
- simple
' '
1093 sed -e "s/Z$//" >expected
<<-EOF &&
1097 other/branch-in-other Z
1098 other/main-in-other Z
1103 __git_complete_refs
&&
1106 test_cmp expected out
1109 test_expect_success '__git_complete_refs
- matching
' '
1110 sed -e "s/Z$//" >expected
<<-EOF &&
1116 __git_complete_refs
&&
1119 test_cmp expected out
1122 test_expect_success '__git_complete_refs
- remote
' '
1123 sed -e "s/Z$//" >expected
<<-EOF &&
1130 __git_complete_refs
--remote=other
&&
1133 test_cmp expected out
1136 test_expect_success '__git_complete_refs
- track
' '
1137 sed -e "s/Z$//" >expected
<<-EOF &&
1141 other/branch-in-other Z
1142 other/main-in-other Z
1149 __git_complete_refs
--track &&
1152 test_cmp expected out
1155 test_expect_success '__git_complete_refs
- current word
' '
1156 sed -e "s/Z$//" >expected
<<-EOF &&
1161 cur
="--option=mat" &&
1162 __git_complete_refs
--cur="${cur#*=}" &&
1165 test_cmp expected out
1168 test_expect_success '__git_complete_refs
- prefix
' '
1169 sed -e "s/Z$//" >expected
<<-EOF &&
1170 v1.0..matching-branch Z
1171 v1.0..matching-tag Z
1175 __git_complete_refs
--pfx=v1.0..
--cur=mat
&&
1178 test_cmp expected out
1181 test_expect_success '__git_complete_refs
- suffix
' '
1182 cat >expected
<<-EOF &&
1186 other/branch-in-other.
1187 other/main-in-other.
1192 __git_complete_refs
--sfx=.
&&
1195 test_cmp expected out
1198 test_expect_success '__git_complete_fetch_refspecs
- simple
' '
1199 sed -e "s/Z$//" >expected
<<-EOF &&
1201 branch-in-other:branch-in-other Z
1202 main-in-other:main-in-other Z
1206 __git_complete_fetch_refspecs other
&&
1209 test_cmp expected out
1212 test_expect_success '__git_complete_fetch_refspecs
- matching
' '
1213 sed -e "s/Z$//" >expected
<<-EOF &&
1214 branch-in-other:branch-in-other Z
1218 __git_complete_fetch_refspecs other
"" br
&&
1221 test_cmp expected out
1224 test_expect_success '__git_complete_fetch_refspecs
- prefix
' '
1225 sed -e "s/Z$//" >expected
<<-EOF &&
1227 +branch-in-other:branch-in-other Z
1228 +main-in-other:main-in-other Z
1232 __git_complete_fetch_refspecs other
"+" "" &&
1235 test_cmp expected out
1238 test_expect_success '__git_complete_fetch_refspecs
- fully qualified
' '
1239 sed -e "s/Z$//" >expected
<<-EOF &&
1240 refs/heads/branch-in-other:refs/heads/branch-in-other Z
1241 refs/heads/main-in-other:refs/heads/main-in-other Z
1242 refs/tags/tag-in-other:refs/tags/tag-in-other Z
1246 __git_complete_fetch_refspecs other
"" refs
/ &&
1249 test_cmp expected out
1252 test_expect_success '__git_complete_fetch_refspecs
- fully qualified
& prefix
' '
1253 sed -e "s/Z$//" >expected
<<-EOF &&
1254 +refs/heads/branch-in-other:refs/heads/branch-in-other Z
1255 +refs/heads/main-in-other:refs/heads/main-in-other Z
1256 +refs/tags/tag-in-other:refs/tags/tag-in-other Z
1260 __git_complete_fetch_refspecs other
+ refs
/ &&
1263 test_cmp expected out
1266 test_expect_success '__git_complete_worktree_paths
' '
1267 test_when_finished
"git worktree remove other_wt" &&
1268 git worktree add
--orphan other_wt
&&
1269 run_completion
"git worktree remove " &&
1273 test_expect_success '__git_complete_worktree_paths
- not a git repository
' '
1276 GIT_CEILING_DIRECTORIES
="$ROOT" &&
1277 export GIT_CEILING_DIRECTORIES
&&
1278 test_completion
"git worktree remove " ""
1282 test_expect_success '__git_complete_worktree_paths with
-C' '
1283 test_when_finished
"git -C otherrepo worktree remove otherrepo_wt" &&
1284 git
-C otherrepo worktree add
--orphan otherrepo_wt
&&
1285 run_completion
"git -C otherrepo worktree remove " &&
1286 grep otherrepo_wt out
1289 test_expect_success 'git switch
- with no options
, complete
local branches and unique remote branch names
for DWIM logic
' '
1290 test_completion
"git switch " <<-\EOF
1298 test_expect_success 'git bisect - when not bisecting
, complete only replay and start subcommands
' '
1299 test_completion
"git bisect " <<-\EOF
1305 test_expect_success 'git bisect - complete options to start subcommand
' '
1306 test_completion
"git bisect start --" <<-\EOF
1316 test_expect_success 'setup for git-bisect tests requiring a repo' '
1317 git init git-bisect &&
1320 echo "initial contents" >file &&
1322 git commit -am "Initial commit" &&
1324 echo "new line" >>file &&
1325 git commit -am "First change" &&
1326 echo "another new line" >>file &&
1327 git commit -am "Second change" &&
1332 test_expect_success 'git bisect - start subcommand arguments before double-dash are completed as revs
' '
1335 test_completion
"git bisect start " <<-\EOF
1344 # Note that these arguments are <pathspec>s, which in practice the fallback
1345 # completion (not the git completion) later ends up completing as paths.
1346 test_expect_success 'git bisect - start subcommand arguments after double-dash are not completed
' '
1349 test_completion
"git bisect start final initial -- " ""
1353 test_expect_success 'setup
for git-bisect tests requiring ongoing bisection
' '
1356 git bisect start
--term-new=custom_new
--term-old=custom_old final initial
1360 test_expect_success 'git-bisect
- when bisecting all subcommands are candidates
' '
1363 test_completion
"git bisect " <<-\EOF
1383 test_expect_success 'git-bisect - options to terms subcommand are candidates
' '
1386 test_completion
"git bisect terms --" <<-\EOF
1395 test_expect_success 'git-bisect - git-log options to visualize subcommand are candidates
' '
1398 # The completion used for git-log and here does not complete
1399 # every git-log option, so rather than hope to stay in sync
1400 # with exactly what it does we will just spot-test here.
1401 test_completion
"git bisect visualize --sta" <<-\EOF &&
1404 test_completion "git bisect visualize --summar" <<-\
EOF
1410 test_expect_success 'git-bisect - view subcommand is not a candidate
' '
1413 test_completion
"git bisect vi" <<-\EOF
1419 test_expect_success 'git-bisect - existing view subcommand is recognized and enables completion of git-log options
' '
1422 # The completion used for git-log and here does not complete
1423 # every git-log option, so rather than hope to stay in sync
1424 # with exactly what it does we will just spot-test here.
1425 test_completion
"git bisect view --sta" <<-\EOF &&
1428 test_completion "git bisect view --summar" <<-\
EOF
1434 test_expect_success 'git checkout - completes refs and unique remote branches
for DWIM
' '
1435 test_completion
"git checkout " <<-\EOF
1442 other/branch-in-other Z
1443 other/main-in-other Z
1447 test_expect_success 'git switch - with
--no-guess, complete only
local branches
' '
1448 test_completion
"git switch --no-guess " <<-\EOF
1454 test_expect_success 'git switch - with GIT_COMPLETION_CHECKOUT_NO_GUESS
=1, complete only
local branches
' '
1455 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git switch " <<-\EOF
1461 test_expect_success 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS
=1, complete
local branches and unique remote names
for DWIM logic
' '
1462 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git switch --guess " <<-\EOF
1470 test_expect_success 'git switch - a later
--guess overrides previous
--no-guess, complete
local and remote unique branches
for DWIM
' '
1471 test_completion
"git switch --no-guess --guess " <<-\EOF
1479 test_expect_success 'git switch - a later
--no-guess overrides previous
--guess, complete only
local branches
' '
1480 test_completion
"git switch --guess --no-guess " <<-\EOF
1486 test_expect_success 'git checkout - with GIT_COMPLETION_NO_GUESS
=1 only completes refs
' '
1487 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git checkout " <<-\EOF
1492 other/branch-in-other Z
1493 other/main-in-other Z
1497 test_expect_success 'git checkout - --guess overrides GIT_COMPLETION_NO_GUESS
=1, complete refs and unique remote branches
for DWIM
' '
1498 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git checkout --guess " <<-\EOF
1505 other/branch-in-other Z
1506 other/main-in-other Z
1510 test_expect_success 'git checkout - with
--no-guess, only completes refs
' '
1511 test_completion
"git checkout --no-guess " <<-\EOF
1516 other/branch-in-other Z
1517 other/main-in-other Z
1521 test_expect_success 'git checkout - a later
--guess overrides previous
--no-guess, complete refs and unique remote branches
for DWIM
' '
1522 test_completion
"git checkout --no-guess --guess " <<-\EOF
1529 other/branch-in-other Z
1530 other/main-in-other Z
1534 test_expect_success 'git checkout - a later
--no-guess overrides previous
--guess, complete only refs
' '
1535 test_completion
"git checkout --guess --no-guess " <<-\EOF
1540 other/branch-in-other Z
1541 other/main-in-other Z
1545 test_expect_success 'git checkout - with checkout.guess
= false
, only completes refs
' '
1546 test_config checkout.guess false
&&
1547 test_completion
"git checkout " <<-\EOF
1552 other/branch-in-other Z
1553 other/main-in-other Z
1557 test_expect_success 'git checkout - with checkout.guess
= true
, completes refs and unique remote branches
for DWIM
' '
1558 test_config checkout.guess true
&&
1559 test_completion
"git checkout " <<-\EOF
1566 other/branch-in-other Z
1567 other/main-in-other Z
1571 test_expect_success 'git checkout - a later
--guess overrides previous checkout.guess
= false
, complete refs and unique remote branches
for DWIM
' '
1572 test_config checkout.guess false
&&
1573 test_completion
"git checkout --guess " <<-\EOF
1580 other/branch-in-other Z
1581 other/main-in-other Z
1585 test_expect_success 'git checkout - a later
--no-guess overrides previous checkout.guess
= true
, complete only refs
' '
1586 test_config checkout.guess true
&&
1587 test_completion
"git checkout --no-guess " <<-\EOF
1592 other/branch-in-other Z
1593 other/main-in-other Z
1597 test_expect_success 'git switch - with
--detach, complete all references
' '
1598 test_completion
"git switch --detach " <<-\EOF
1603 other/branch-in-other Z
1604 other/main-in-other Z
1608 test_expect_success 'git checkout - with
--detach, complete only references
' '
1609 test_completion
"git checkout --detach " <<-\EOF
1614 other/branch-in-other Z
1615 other/main-in-other Z
1619 test_expect_success 'setup sparse-checkout tests' '
1620 # set up sparse-checkout repo
1621 git init sparse-checkout &&
1623 cd sparse-checkout &&
1624 mkdir -p folder1/0/1 folder2/0 folder3 &&
1625 touch folder1/0/1/t.txt &&
1626 touch folder2/0/t.txt &&
1627 touch folder3/t.txt &&
1629 git commit -am "Initial commit"
1633 test_expect_success 'sparse-checkout completes subcommands' '
1634 test_completion "git sparse-checkout " <<-\
EOF
1644 test_expect_success 'cone mode sparse-checkout completes directory names' '
1645 # initialize sparse-checkout definitions
1646 git -C sparse-checkout sparse-checkout set --cone folder1/0 folder3 &&
1648 # test tab completion
1650 cd sparse-checkout &&
1651 test_completion "git sparse-checkout set f" <<-\EOF
1659 cd sparse-checkout &&
1660 test_completion "git sparse-checkout set folder1/" <<-\EOF
1666 cd sparse-checkout &&
1667 test_completion "git sparse-checkout set folder1/0/" <<-\EOF
1673 cd sparse-checkout/folder1 &&
1674 test_completion "git sparse-checkout add 0" <<-\EOF
1680 test_expect_success 'cone mode sparse-checkout completes directory names with spaces and accents' '
1681 # reset sparse-checkout
1682 git -C sparse-checkout sparse-checkout disable &&
1684 cd sparse-checkout &&
1685 mkdir "directory with spaces" &&
1686 mkdir "directory-with-áccent" &&
1687 >"directory with spaces/randomfile" &&
1688 >"directory-with-áccent/randomfile" &&
1690 git commit -m "Add directory with spaces and directory with accent" &&
1691 git sparse-checkout set --cone "directory with spaces" \
1692 "directory-with-áccent" &&
1693 test_completion "git sparse-checkout add dir" <<-\EOF &&
1694 directory with spaces/
1695 directory-with-áccent/
1697 rm -rf "directory with spaces" &&
1698 rm -rf "directory-with-áccent" &&
1700 git commit -m "Remove directory with spaces and directory with accent"
1704 # use FUNNYNAMES to avoid running on Windows, which doesn't permit tabs in paths
1705 test_expect_success FUNNYNAMES 'cone mode sparse-checkout completes directory names with tabs' '
1706 # reset sparse-checkout
1707 git -C sparse-checkout sparse-checkout disable &&
1709 cd sparse-checkout &&
1710 mkdir "$(printf "directory\twith\ttabs")" &&
1711 >"$(printf "directory\twith\ttabs")/randomfile" &&
1713 git commit -m "Add directory with tabs" &&
1714 git sparse-checkout set --cone \
1715 "$(printf "directory\twith\ttabs")" &&
1716 test_completion "git sparse-checkout add dir" <<-\EOF &&
1717 directory with tabs/
1719 rm -rf "$(printf "directory\twith\ttabs")" &&
1721 git commit -m "Remove directory with tabs"
1725 # use FUNNYNAMES to avoid running on Windows, and !CYGWIN for Cygwin, as neither permit backslashes in paths
1726 test_expect_success FUNNYNAMES,!CYGWIN 'cone mode sparse-checkout completes directory names with backslashes' '
1727 # reset sparse-checkout
1728 git -C sparse-checkout sparse-checkout disable &&
1730 cd sparse-checkout &&
1731 mkdir "directory\with\backslashes" &&
1732 >"directory\with\backslashes/randomfile" &&
1734 git commit -m "Add directory with backslashes" &&
1735 git sparse-checkout set --cone \
1736 "directory\with\backslashes" &&
1737 test_completion "git sparse-checkout add dir" <<-\EOF &&
1738 directory\with\backslashes/
1740 rm -rf "directory\with\backslashes" &&
1742 git commit -m "Remove directory with backslashes"
1746 test_expect_success 'non-cone mode sparse-checkout gives rooted paths' '
1747 # reset sparse-checkout repo to non-cone mode
1748 git -C sparse-checkout sparse-checkout disable &&
1749 git -C sparse-checkout sparse-checkout set --no-cone &&
1752 cd sparse-checkout &&
1753 # expected to be empty since we have not configured
1754 # custom completion for non-cone mode
1755 test_completion "git sparse-checkout set f" <<-\EOF
1756 /folder1/0/1/t.txt Z
1759 /folder1/out_sorted Z
1766 test_expect_success 'git sparse-checkout set --cone completes directory names' '
1767 git -C sparse-checkout sparse-checkout disable &&
1770 cd sparse-checkout &&
1771 test_completion "git sparse-checkout set --cone f" <<-\EOF
1779 test_expect_success 'git switch - with
-d, complete all references
' '
1780 test_completion
"git switch -d " <<-\EOF
1785 other/branch-in-other Z
1786 other/main-in-other Z
1790 test_expect_success 'git checkout - with
-d, complete only references
' '
1791 test_completion
"git checkout -d " <<-\EOF
1796 other/branch-in-other Z
1797 other/main-in-other Z
1801 test_expect_success 'git switch - with
--track, complete only remote branches
' '
1802 test_completion
"git switch --track " <<-\EOF &&
1803 other/branch-in-other Z
1804 other/main-in-other Z
1806 test_completion "git switch -t " <<-\
EOF
1807 other/branch-in-other Z
1808 other/main-in-other Z
1812 test_expect_success 'git checkout - with
--track, complete only remote branches
' '
1813 test_completion
"git checkout --track " <<-\EOF &&
1814 other/branch-in-other Z
1815 other/main-in-other Z
1817 test_completion "git checkout -t " <<-\
EOF
1818 other/branch-in-other Z
1819 other/main-in-other Z
1823 test_expect_success 'git switch - with
--no-track, complete only
local branch names
' '
1824 test_completion
"git switch --no-track " <<-\EOF
1830 test_expect_success 'git checkout - with
--no-track, complete only
local references
' '
1831 test_completion
"git checkout --no-track " <<-\EOF
1836 other/branch-in-other Z
1837 other/main-in-other Z
1841 test_expect_success 'git switch - with
-c, complete all references
' '
1842 test_completion
"git switch -c new-branch " <<-\EOF
1847 other/branch-in-other Z
1848 other/main-in-other Z
1852 test_expect_success 'git switch - with
-C, complete all references
' '
1853 test_completion
"git switch -C new-branch " <<-\EOF
1858 other/branch-in-other Z
1859 other/main-in-other Z
1863 test_expect_success 'git switch - with
-c and
--track, complete all references
' '
1864 test_completion
"git switch -c new-branch --track " <<-EOF
1869 other/branch-in-other Z
1870 other/main-in-other Z
1874 test_expect_success 'git switch
- with
-C and
--track, complete all references
' '
1875 test_completion
"git switch -C new-branch --track " <<-EOF
1880 other/branch-in-other Z
1881 other/main-in-other Z
1885 test_expect_success 'git switch
- with
-c and
--no-track, complete all references
' '
1886 test_completion
"git switch -c new-branch --no-track " <<-\EOF
1891 other/branch-in-other Z
1892 other/main-in-other Z
1896 test_expect_success 'git switch - with
-C and
--no-track, complete all references
' '
1897 test_completion
"git switch -C new-branch --no-track " <<-\EOF
1902 other/branch-in-other Z
1903 other/main-in-other Z
1907 test_expect_success 'git checkout - with
-b, complete all references
' '
1908 test_completion
"git checkout -b new-branch " <<-\EOF
1913 other/branch-in-other Z
1914 other/main-in-other Z
1918 test_expect_success 'git checkout - with
-B, complete all references
' '
1919 test_completion
"git checkout -B new-branch " <<-\EOF
1924 other/branch-in-other Z
1925 other/main-in-other Z
1929 test_expect_success 'git checkout - with
-b and
--track, complete all references
' '
1930 test_completion
"git checkout -b new-branch --track " <<-EOF
1935 other/branch-in-other Z
1936 other/main-in-other Z
1940 test_expect_success 'git checkout
- with
-B and
--track, complete all references
' '
1941 test_completion
"git checkout -B new-branch --track " <<-EOF
1946 other/branch-in-other Z
1947 other/main-in-other Z
1951 test_expect_success 'git checkout
- with
-b and
--no-track, complete all references
' '
1952 test_completion
"git checkout -b new-branch --no-track " <<-\EOF
1957 other/branch-in-other Z
1958 other/main-in-other Z
1962 test_expect_success 'git checkout - with
-B and
--no-track, complete all references
' '
1963 test_completion
"git checkout -B new-branch --no-track " <<-\EOF
1968 other/branch-in-other Z
1969 other/main-in-other Z
1973 test_expect_success 'git switch - for -c, complete
local branches and unique remote branches
' '
1974 test_completion
"git switch -c " <<-\EOF
1982 test_expect_success 'git switch - for -C, complete
local branches and unique remote branches
' '
1983 test_completion
"git switch -C " <<-\EOF
1991 test_expect_success 'git switch - for -c with
--no-guess, complete
local branches only
' '
1992 test_completion
"git switch --no-guess -c " <<-\EOF
1998 test_expect_success 'git switch - for -C with
--no-guess, complete
local branches only
' '
1999 test_completion
"git switch --no-guess -C " <<-\EOF
2005 test_expect_success 'git switch - for -c with
--no-track, complete
local branches only
' '
2006 test_completion
"git switch --no-track -c " <<-\EOF
2012 test_expect_success 'git switch - for -C with
--no-track, complete
local branches only
' '
2013 test_completion
"git switch --no-track -C " <<-\EOF
2019 test_expect_success 'git checkout - for -b, complete
local branches and unique remote branches
' '
2020 test_completion
"git checkout -b " <<-\EOF
2028 test_expect_success 'git checkout - for -B, complete
local branches and unique remote branches
' '
2029 test_completion
"git checkout -B " <<-\EOF
2037 test_expect_success 'git checkout - for -b with
--no-guess, complete
local branches only
' '
2038 test_completion
"git checkout --no-guess -b " <<-\EOF
2044 test_expect_success 'git checkout - for -B with
--no-guess, complete
local branches only
' '
2045 test_completion
"git checkout --no-guess -B " <<-\EOF
2051 test_expect_success 'git checkout - for -b with
--no-track, complete
local branches only
' '
2052 test_completion
"git checkout --no-track -b " <<-\EOF
2058 test_expect_success 'git checkout - for -B with
--no-track, complete
local branches only
' '
2059 test_completion
"git checkout --no-track -B " <<-\EOF
2065 test_expect_success 'git switch - with
--orphan completes
local branch names and unique remote branch names
' '
2066 test_completion
"git switch --orphan " <<-\EOF
2074 test_expect_success 'git switch - --orphan with branch already provided completes nothing
else' '
2075 test_completion
"git switch --orphan main " <<-\EOF
2080 test_expect_success 'git checkout - with
--orphan completes
local branch names and unique remote branch names
' '
2081 test_completion
"git checkout --orphan " <<-\EOF
2089 test_expect_success 'git checkout - --orphan with branch already provided completes
local refs
for a start-point
' '
2090 test_completion
"git checkout --orphan main " <<-\EOF
2095 other/branch-in-other Z
2096 other/main-in-other Z
2100 test_expect_success 'git restore completes modified files' '
2101 test_commit A a.file &&
2103 test_completion "git restore a." <<-\
EOF
2108 test_expect_success 'teardown after ref completion' '
2109 git branch -d matching-branch &&
2110 git tag -d matching-tag &&
2111 git remote remove other
2115 test_path_completion ()
2117 test $# = 2 || BUG "not 2 parameters to test_path_completion"
2119 local cur="$1" expected="$2"
2120 echo "$expected" >expected &&
2122 # In the following tests calling this function we only
2123 # care about how __git_complete_index_file() deals with
2124 # unusual characters in path names. By requesting only
2125 # untracked files we do not have to bother adding any
2126 # paths to the index in those tests.
2127 __git_complete_index_file --others &&
2130 test_cmp expected out
2133 test_expect_success 'setup for path completion tests' '
2137 touch simple-dir/simple-file \
2138 "spaces in dir/spaces in file" \
2139 "árvíztűrő/Сайн яваарай" &&
2140 if test_have_prereq !MINGW &&
2142 '$'separators\034in\035dir'' &&
2143 touch BS\\dir/DQ\"file \
2144 '$'separators\034in\035dir/sep\036in\037file''
2146 test_set_prereq FUNNIERNAMES
2148 rm -rf BS\\dir '$'separators\034in\035dir''
2152 test_expect_success '__git_complete_index_file - simple
' '
2153 test_path_completion simple simple-dir
&& # Bash is supposed to
2154 # add the trailing /.
2155 test_path_completion simple-dir
/simple simple-dir
/simple-file
2158 test_expect_success \
2159 '__git_complete_index_file
- escaped characters on cmdline
' '
2160 test_path_completion spac
"spaces in dir" && # Bash will turn this
2161 # into "spaces\ in\ dir"
2162 test_path_completion
"spaces\\ i" \
2164 test_path_completion
"spaces\\ in\\ dir/s" \
2165 "spaces in dir/spaces in file" &&
2166 test_path_completion
"spaces\\ in\\ dir/spaces\\ i" \
2167 "spaces in dir/spaces in file"
2170 test_expect_success \
2171 '__git_complete_index_file
- quoted characters on cmdline
' '
2172 # Testing with an opening but without a corresponding closing
2173 # double quote is important.
2174 test_path_completion
\"spac
"spaces in dir" &&
2175 test_path_completion
"\"spaces i" \
2177 test_path_completion
"\"spaces in dir/s" \
2178 "spaces in dir/spaces in file" &&
2179 test_path_completion
"\"spaces in dir/spaces i" \
2180 "spaces in dir/spaces in file"
2183 test_expect_success '__git_complete_index_file
- UTF-8
in ls-files output
' '
2184 test_path_completion á árvíztűrő
&&
2185 test_path_completion árvíztűrő
/С
"árvíztűrő/Сайн яваарай"
2188 test_expect_success FUNNIERNAMES \
2189 '__git_complete_index_file
- C-style escapes
in ls-files output
' '
2190 test_path_completion BS \
2192 test_path_completion BS
\\\\d \
2194 test_path_completion BS
\\\\dir
/DQ \
2196 test_path_completion BS
\\\\dir
/DQ
\\\"f \
2200 test_expect_success FUNNIERNAMES \
2201 '__git_complete_index_file
- \nnn
-escaped characters
in ls-files output
' '
2202 test_path_completion sep
'$'separators
\034in\035dir
'' &&
2203 test_path_completion
'$'separators
\034i
'' \
2204 '$'separators
\034in\035dir
'' &&
2205 test_path_completion
'$'separators
\034in\035dir
/sep
'' \
2206 '$'separators
\034in\035dir
/sep
\036in\037file'' &&
2207 test_path_completion
'$'separators
\034in\035dir
/sep
\036i
'' \
2208 '$'separators
\034in\035dir
/sep
\036in\037file''
2211 test_expect_success FUNNYNAMES \
2212 '__git_complete_index_file
- removing repeated quoted path components
' '
2213 test_when_finished
rm -r repeated-quoted
&&
2214 mkdir repeated-quoted
&& # A directory whose name in itself
2215 # would not be quoted ...
2216 >repeated-quoted
/0-file &&
2217 >repeated-quoted
/1\"file && # ... but here the file makes the
2218 # dirname quoted ...
2219 >repeated-quoted
/2-file &&
2220 >repeated-quoted
/3\"file && # ... and here, too.
2222 # Still, we shold only list the directory name only once.
2223 test_path_completion repeated repeated-quoted
2226 test_expect_success 'teardown after path completion tests
' '
2227 rm -rf simple-dir
"spaces in dir" árvíztűrő \
2228 BS
\\dir
'$'separators
\034in\035dir
''
2231 test_expect_success '__git_find_on_cmdline
- single match
' '
2232 echo list
>expect
&&
2234 words
=(git
command --opt list
) &&
2235 cword
=${#words[@]} &&
2237 __git_find_on_cmdline
"add list remove" >actual
2239 test_cmp expect actual
2242 test_expect_success '__git_find_on_cmdline
- multiple matches
' '
2243 echo remove
>expect
&&
2245 words
=(git
command -o --opt remove list add
) &&
2246 cword
=${#words[@]} &&
2248 __git_find_on_cmdline
"add list remove" >actual
2250 test_cmp expect actual
2253 test_expect_success '__git_find_on_cmdline
- no match
' '
2255 words
=(git
command --opt branch
) &&
2256 cword
=${#words[@]} &&
2258 __git_find_on_cmdline
"add list remove" >actual
2260 test_must_be_empty actual
2263 test_expect_success '__git_find_on_cmdline
- single match with index
' '
2264 echo "3 list" >expect
&&
2266 words
=(git
command --opt list
) &&
2267 cword
=${#words[@]} &&
2269 __git_find_on_cmdline
--show-idx "add list remove" >actual
2271 test_cmp expect actual
2274 test_expect_success '__git_find_on_cmdline
- multiple matches with index
' '
2275 echo "4 remove" >expect
&&
2277 words
=(git
command -o --opt remove list add
) &&
2278 cword
=${#words[@]} &&
2280 __git_find_on_cmdline
--show-idx "add list remove" >actual
2282 test_cmp expect actual
2285 test_expect_success '__git_find_on_cmdline
- no match with index
' '
2287 words
=(git
command --opt branch
) &&
2288 cword
=${#words[@]} &&
2290 __git_find_on_cmdline
--show-idx "add list remove" >actual
2292 test_must_be_empty actual
2295 test_expect_success '__git_find_on_cmdline
- ignores matches before
command with index
' '
2296 echo "6 remove" >expect
&&
2298 words
=(git
-C remove
command -o --opt remove list add
) &&
2299 cword
=${#words[@]} &&
2301 __git_find_on_cmdline
--show-idx "add list remove" >actual
2303 test_cmp expect actual
2306 test_expect_success '__git_get_config_variables
' '
2307 cat >expect
<<-EOF &&
2311 test_config interesting.name-1 good
&&
2312 test_config interesting.name-2 good
&&
2313 test_config subsection.interesting.name-3 bad
&&
2314 __git_get_config_variables interesting
>actual
&&
2315 test_cmp expect actual
2318 test_expect_success '__git_pretty_aliases
' '
2319 cat >expect
<<-EOF &&
2323 test_config pretty.author
"%an %ae" &&
2324 test_config pretty.
hash %H
&&
2325 __git_pretty_aliases
>actual
&&
2326 test_cmp expect actual
2329 test_expect_success 'basic
' '
2330 run_completion
"git " &&
2332 grep -q "^add \$" out
&&
2334 grep -q "^rebase \$" out
&&
2336 ! grep -q "^ls-files \$" out
&&
2338 run_completion
"git r" &&
2339 ! grep -q -v "^r" out
2342 test_expect_success 'double dash
"git" itself
' '
2343 test_completion
"git --" <<-\EOF
2356 --no-replace-objects Z
2361 test_expect_success 'double dash "git checkout"' '
2362 test_completion "git checkout --" <<-\
EOF
2372 --ignore-skip-worktree-bits Z
2373 --ignore-other-worktrees Z
2374 --recurse-submodules Z
2380 --pathspec-file-nul Z
2381 --pathspec-from-file=Z
2385 test_expect_success 'general options' '
2386 test_completion "git --ver" "--version " &&
2387 test_completion "git --hel" "--help " &&
2388 test_completion "git --exe" <<-\EOF &&
2392 test_completion "git --htm" "--html-path " &&
2393 test_completion "git --pag" "--paginate " &&
2394 test_completion "git --no-p" "--no-pager " &&
2395 test_completion "git --git" "--git-dir=" &&
2396 test_completion "git --wor" "--work-tree=" &&
2397 test_completion "git --nam" "--namespace=" &&
2398 test_completion "git --bar" "--bare " &&
2399 test_completion "git --inf" "--info-path " &&
2400 test_completion "git --no-r" "--no-replace-objects "
2403 test_expect_success 'general options plus command' '
2404 test_completion "git --version check" "checkout " &&
2405 test_completion "git --paginate check" "checkout " &&
2406 test_completion "git --git-dir=foo check" "checkout " &&
2407 test_completion "git --bare check" "checkout " &&
2408 test_completion "git --exec-path=foo check" "checkout " &&
2409 test_completion "git --html-path check" "checkout " &&
2410 test_completion "git --no-pager check" "checkout " &&
2411 test_completion "git --work-tree=foo check" "checkout " &&
2412 test_completion "git --namespace=foo check" "checkout " &&
2413 test_completion "git --paginate check" "checkout " &&
2414 test_completion "git --info-path check" "checkout " &&
2415 test_completion "git --no-replace-objects check" "checkout " &&
2416 test_completion "git --git-dir some/path check" "checkout " &&
2417 test_completion "git -c conf.var=value check" "checkout " &&
2418 test_completion "git -C some/path check" "checkout " &&
2419 test_completion "git --work-tree some/path check" "checkout " &&
2420 test_completion "git --namespace name/space check" "checkout "
2423 test_expect_success 'git --help completion' '
2424 test_completion "git --help ad" "add " &&
2425 test_completion "git --help core" "core-tutorial "
2428 test_expect_success 'completion.commands removes multiple commands' '
2429 test_config completion.commands "-cherry -mergetool" &&
2430 git --list-cmds=list-mainporcelain,list-complete,config >out &&
2431 ! grep -E "^(cherry|mergetool)$" out
2434 test_expect_success 'setup for integration tests' '
2435 echo content >file1 &&
2437 git add file1 file2 &&
2438 git commit -m one &&
2439 git branch mybranch &&
2443 test_expect_success 'checkout completes ref names' '
2444 test_completion "git checkout m" <<-\EOF
2451 test_expect_success 'checkout does not match ref names of a different case' '
2452 test_completion "git checkout M" ""
2455 test_expect_success 'checkout matches case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2457 GIT_COMPLETION_IGNORE_CASE=1 &&
2458 test_completion "git checkout M" <<-\EOF
2466 test_expect_success 'checkout completes pseudo refs' '
2467 test_completion "git checkout H" <<-\EOF
2472 test_expect_success 'checkout completes pseudo refs case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2474 GIT_COMPLETION_IGNORE_CASE=1 &&
2475 test_completion "git checkout h" <<-\EOF
2481 test_expect_success 'git -C <path> checkout uses the right repo' '
2482 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
2487 test_expect_success 'show completes all refs' '
2488 test_completion "git show m" <<-\EOF
2495 test_expect_success '<ref>: completes paths' '
2496 test_completion "git show mytag:f" <<-\EOF
2502 test_expect_success 'complete tree filename with spaces' '
2503 echo content >"name with spaces" &&
2504 git add "name with spaces" &&
2505 git commit -m spaces &&
2506 test_completion "git show HEAD:nam" <<-\EOF
2511 test_expect_success 'complete tree filename with metacharacters' '
2512 echo content >"name with \${meta}" &&
2513 git add "name with \${meta}" &&
2514 git commit -m meta &&
2515 test_completion "git show HEAD:nam" <<-\EOF
2521 test_expect_success PERL 'send-email' '
2522 test_completion "git send-email --cov" <<-\EOF &&
2523 --cover-from-description=Z
2526 test_completion "git send-email --val" <<-\EOF &&
2529 test_completion "git send-email ma" "main "
2532 test_expect_success 'complete files' '
2533 git init tmp && cd tmp &&
2534 test_when_finished "cd .. && rm -rf tmp" &&
2536 echo "expected" > .gitignore &&
2537 echo "out" >> .gitignore &&
2538 echo "out_sorted" >> .gitignore &&
2540 git add .gitignore &&
2541 test_completion "git commit " ".gitignore" &&
2543 git commit -m ignore &&
2546 test_completion "git add " "new" &&
2549 git commit -a -m new &&
2550 test_completion "git add " "" &&
2552 git mv new modified &&
2553 echo modify > modified &&
2554 test_completion "git add " "modified" &&
2556 mkdir -p some/deep &&
2557 touch some/deep/path &&
2558 test_completion "git add some/" "some/deep" &&
2559 git clean -f some &&
2563 : TODO .gitignore should not be here &&
2564 test_completion "git rm " <<-\EOF &&
2569 test_completion "git clean " "untracked" &&
2571 : TODO .gitignore should not be here &&
2572 test_completion "git mv " <<-\EOF &&
2578 touch dir/file-in-dir &&
2579 git add dir/file-in-dir &&
2580 git commit -m dir &&
2582 mkdir untracked-dir &&
2584 : TODO .gitignore should not be here &&
2585 test_completion "git mv modified " <<-\EOF &&
2593 test_completion "git commit " "modified" &&
2595 : TODO .gitignore should not be here &&
2596 test_completion "git ls-files " <<-\EOF &&
2603 test_completion "git add mom" "momified"
2606 test_expect_success "simple alias" '
2607 test_config alias.co checkout &&
2608 test_completion "git co m" <<-\EOF
2615 test_expect_success "recursive alias" '
2616 test_config alias.co checkout &&
2617 test_config alias.cod "co --detached" &&
2618 test_completion "git cod m" <<-\EOF
2625 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
2626 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
2627 test_completion "git co m" <<-\EOF
2634 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
2635 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
2636 test_completion "git co m" <<-\EOF
2643 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
2644 test_config alias.co "!f() { : git checkout ; if ... } f" &&
2645 test_completion "git co m" <<-\EOF
2652 test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd> ; ... }' '
2653 test_config alias.co "!f() { : checkout ; if ... } f" &&
2654 test_completion "git co m" <<-\EOF
2661 test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd>; ... }' '
2662 test_config alias.co "!f() { : checkout; if ... } f" &&
2663 test_completion "git co m" <<-\EOF
2670 test_expect_success 'completion without explicit _git_xxx function' '
2671 test_completion "git version --" <<-\EOF
2673 --no-build-options Z
2677 test_expect_failure 'complete with tilde expansion' '
2678 git init tmp && cd tmp &&
2679 test_when_finished "cd .. && rm -rf tmp" &&
2683 test_completion "git add ~/tmp/" "~/tmp/file"
2686 test_expect_success 'setup other remote for remote reference completion' '
2687 git remote add other otherrepo &&
2691 for flag in -d --delete
2693 test_expect_success "__git_complete_remote_or_refspec - push
$flag other
" '
2694 sed -e "s
/Z$
//" >expected <<-EOF &&
2698 words=(git push '$flag' other ma) &&
2699 cword=${#words[@]} cur=${words[cword-1]} &&
2701 __git_complete_remote_or_refspec &&
2704 test_cmp expected out
2707 test_expect_failure "__git_complete_remote_or_refspec
- push other
$flag" '
2708 sed -e "s
/Z$
//" >expected <<-EOF &&
2712 words=(git push other '$flag' ma) &&
2713 cword=${#words[@]} cur=${words[cword-1]} &&
2715 __git_complete_remote_or_refspec &&
2718 test_cmp expected out
2722 test_expect_success 'git config - section' '
2723 test_completion "git config br
" <<-\EOF
2729 test_expect_success 'git config - section include, includeIf' '
2730 test_completion "git config inclu
" <<-\EOF
2736 test_expect_success 'git config - variable name' '
2737 test_completion "git config log.d
" <<-\EOF
2744 test_expect_success 'git config - variable name include' '
2745 test_completion "git config include.p
" <<-\EOF
2750 test_expect_success 'setup for git config submodule tests' '
2751 test_create_repo sub &&
2752 test_commit -C sub initial &&
2753 git submodule add ./sub
2756 test_expect_success 'git config - variable name - submodule and __git_compute_first_level_config_vars_for_section' '
2757 test_completion "git config submodule.
" <<-\EOF
2759 submodule.alternateErrorStrategy Z
2760 submodule.alternateLocation Z
2761 submodule.fetchJobs Z
2762 submodule.propagateBranches Z
2768 test_expect_success 'git config - variable name - __git_compute_second_level_config_vars_for_section' '
2769 test_completion "git config submodule.sub.
" <<-\EOF
2771 submodule.sub.update Z
2772 submodule.sub.branch Z
2773 submodule.sub.fetchRecurseSubmodules Z
2774 submodule.sub.ignore Z
2775 submodule.sub.active Z
2779 test_expect_success 'git config - value' '
2780 test_completion "git config color.pager
" <<-\EOF
2786 test_expect_success 'git -c - section' '
2787 test_completion "git
-c br
" <<-\EOF
2793 test_expect_success 'git -c - variable name' '
2794 test_completion "git
-c log.d
" <<-\EOF
2801 test_expect_success 'git -c - value' '
2802 test_completion "git
-c color.pager
=" <<-\EOF
2808 test_expect_success 'git clone --config= - section' '
2809 test_completion "git clone
--config=br
" <<-\EOF
2815 test_expect_success 'git clone --config= - variable name' '
2816 test_completion "git clone
--config=log.d
" <<-\EOF
2823 test_expect_success 'git clone --config= - value' '
2824 test_completion "git clone
--config=color.pager
=" <<-\EOF
2830 test_expect_success 'git reflog show' '
2831 test_when_finished "git checkout
- && git branch
-d shown
" &&
2832 git checkout -b shown &&
2833 test_completion "git reflog sho
" <<-\EOF &&
2837 test_completion "git reflog show sho
" "shown
" &&
2838 test_completion "git reflog shown sho
" "shown
" &&
2839 test_completion "git reflog
--unt" "--until=" &&
2840 test_completion "git reflog show
--unt" "--until=" &&
2841 test_completion "git reflog shown
--unt" "--until="
2844 test_expect_success 'options with value' '
2845 test_completion "git merge
-X diff-algorithm
=" <<-\EOF
2850 test_expect_success 'sourcing the completion script clears cached commands' '
2852 __git_compute_all_commands &&
2853 test -n "$__git_all_commands" &&
2854 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2855 test -z "$__git_all_commands"
2859 test_expect_success 'sourcing the completion script clears cached merge strategies' '
2861 __git_compute_merge_strategies &&
2862 test -n "$__git_merge_strategies" &&
2863 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2864 test -z "$__git_merge_strategies"
2868 test_expect_success 'sourcing the completion script clears cached --options' '
2870 __gitcomp_builtin checkout &&
2871 test -n "$__gitcomp_builtin_checkout" &&
2872 __gitcomp_builtin notes_edit &&
2873 test -n "$__gitcomp_builtin_notes_edit" &&
2874 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2875 test -z "$__gitcomp_builtin_checkout" &&
2876 test -z "$__gitcomp_builtin_notes_edit"
2880 test_expect_success 'option aliases are not shown by default' '
2881 test_completion "git clone
--recurs" "--recurse-submodules "
2884 test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
2886 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2887 GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2888 test_completion "git clone
--recurs" <<-\EOF
2889 --recurse-submodules Z
2895 test_expect_success 'plumbing commands are excluded without GIT_COMPLETION_SHOW_ALL_COMMANDS' '
2897 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2898 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2900 # Just mainporcelain, not plumbing commands
2901 run_completion "git c
" &&
2902 grep checkout out &&
2907 test_expect_success 'all commands are shown with GIT_COMPLETION_SHOW_ALL_COMMANDS (also main non-builtin)' '
2909 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2910 GIT_COMPLETION_SHOW_ALL_COMMANDS=1 &&
2911 export GIT_COMPLETION_SHOW_ALL_COMMANDS &&
2912 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2914 # Both mainporcelain and plumbing commands
2915 run_completion "git c
" &&
2916 grep checkout out &&
2917 grep cat-file out &&
2919 # Check "gitk
", a "main
" command, but not a built-in + more plumbing
2920 run_completion "git g
" &&
2922 grep get-tar-commit-id out
2926 test_expect_success '__git_complete' '
2927 unset -f __git_wrap__git_main &&
2929 __git_complete foo __git_main &&
2930 __git_have_func __git_wrap__git_main &&
2931 unset -f __git_wrap__git_main &&
2933 __git_complete gf _git_fetch &&
2934 __git_have_func __git_wrap_git_fetch &&
2936 __git_complete foo git &&
2937 __git_have_func __git_wrap__git_main &&
2938 unset -f __git_wrap__git_main &&
2940 __git_complete gd git_diff &&
2941 __git_have_func __git_wrap_git_diff &&
2943 test_must_fail __git_complete ga missing
2946 test_expect_success '__git_pseudoref_exists' '
2947 test_when_finished "rm -rf repo
" &&
2951 sane_unset __git_repo_path &&
2953 # HEAD should exist, even if it points to an unborn branch.
2954 __git_pseudoref_exists HEAD >output 2>&1 &&
2955 test_must_be_empty output &&
2957 # HEAD points to an existing branch, so it should exist.
2959 __git_pseudoref_exists HEAD >output 2>&1 &&
2960 test_must_be_empty output &&
2962 # CHERRY_PICK_HEAD does not exist, so the existence check should fail.
2963 ! __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
2964 test_must_be_empty output &&
2966 # CHERRY_PICK_HEAD points to a commit, so it should exist.
2967 git update-ref CHERRY_PICK_HEAD A &&
2968 __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
2969 test_must_be_empty output