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 switch
- with no options
, complete
local branches and unique remote branch names
for DWIM logic
' '
1267 test_completion
"git switch " <<-\EOF
1275 test_expect_success 'git bisect - when not bisecting
, complete only replay and start subcommands
' '
1276 test_completion
"git bisect " <<-\EOF
1282 test_expect_success 'git bisect - complete options to start subcommand
' '
1283 test_completion
"git bisect start --" <<-\EOF
1293 test_expect_success 'setup for git-bisect tests requiring a repo' '
1294 git init git-bisect &&
1297 echo "initial contents" >file &&
1299 git commit -am "Initial commit" &&
1301 echo "new line" >>file &&
1302 git commit -am "First change" &&
1303 echo "another new line" >>file &&
1304 git commit -am "Second change" &&
1309 test_expect_success 'git bisect - start subcommand arguments before double-dash are completed as revs
' '
1312 test_completion
"git bisect start " <<-\EOF
1321 # Note that these arguments are <pathspec>s, which in practice the fallback
1322 # completion (not the git completion) later ends up completing as paths.
1323 test_expect_success 'git bisect - start subcommand arguments after double-dash are not completed
' '
1326 test_completion
"git bisect start final initial -- " ""
1330 test_expect_success 'setup
for git-bisect tests requiring ongoing bisection
' '
1333 git bisect start
--term-new=custom_new
--term-old=custom_old final initial
1337 test_expect_success 'git-bisect
- when bisecting all subcommands are candidates
' '
1340 test_completion
"git bisect " <<-\EOF
1360 test_expect_success 'git-bisect - options to terms subcommand are candidates
' '
1363 test_completion
"git bisect terms --" <<-\EOF
1372 test_expect_success 'git-bisect - git-log options to visualize subcommand are candidates
' '
1375 # The completion used for git-log and here does not complete
1376 # every git-log option, so rather than hope to stay in sync
1377 # with exactly what it does we will just spot-test here.
1378 test_completion
"git bisect visualize --sta" <<-\EOF &&
1381 test_completion "git bisect visualize --summar" <<-\
EOF
1387 test_expect_success 'git-bisect - view subcommand is not a candidate
' '
1390 test_completion
"git bisect vi" <<-\EOF
1396 test_expect_success 'git-bisect - existing view subcommand is recognized and enables completion of git-log options
' '
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 view --sta" <<-\EOF &&
1405 test_completion "git bisect view --summar" <<-\
EOF
1411 test_expect_success 'git checkout - completes refs and unique remote branches
for DWIM
' '
1412 test_completion
"git checkout " <<-\EOF
1419 other/branch-in-other Z
1420 other/main-in-other Z
1424 test_expect_success 'git switch - with
--no-guess, complete only
local branches
' '
1425 test_completion
"git switch --no-guess " <<-\EOF
1431 test_expect_success 'git switch - with GIT_COMPLETION_CHECKOUT_NO_GUESS
=1, complete only
local branches
' '
1432 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git switch " <<-\EOF
1438 test_expect_success 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS
=1, complete
local branches and unique remote names
for DWIM logic
' '
1439 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git switch --guess " <<-\EOF
1447 test_expect_success 'git switch - a later
--guess overrides previous
--no-guess, complete
local and remote unique branches
for DWIM
' '
1448 test_completion
"git switch --no-guess --guess " <<-\EOF
1456 test_expect_success 'git switch - a later
--no-guess overrides previous
--guess, complete only
local branches
' '
1457 test_completion
"git switch --guess --no-guess " <<-\EOF
1463 test_expect_success 'git checkout - with GIT_COMPLETION_NO_GUESS
=1 only completes refs
' '
1464 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git checkout " <<-\EOF
1469 other/branch-in-other Z
1470 other/main-in-other Z
1474 test_expect_success 'git checkout - --guess overrides GIT_COMPLETION_NO_GUESS
=1, complete refs and unique remote branches
for DWIM
' '
1475 GIT_COMPLETION_CHECKOUT_NO_GUESS
=1 test_completion
"git checkout --guess " <<-\EOF
1482 other/branch-in-other Z
1483 other/main-in-other Z
1487 test_expect_success 'git checkout - with
--no-guess, only completes refs
' '
1488 test_completion
"git checkout --no-guess " <<-\EOF
1493 other/branch-in-other Z
1494 other/main-in-other Z
1498 test_expect_success 'git checkout - a later
--guess overrides previous
--no-guess, complete refs and unique remote branches
for DWIM
' '
1499 test_completion
"git checkout --no-guess --guess " <<-\EOF
1506 other/branch-in-other Z
1507 other/main-in-other Z
1511 test_expect_success 'git checkout - a later
--no-guess overrides previous
--guess, complete only refs
' '
1512 test_completion
"git checkout --guess --no-guess " <<-\EOF
1517 other/branch-in-other Z
1518 other/main-in-other Z
1522 test_expect_success 'git checkout - with checkout.guess
= false
, only completes refs
' '
1523 test_config checkout.guess false
&&
1524 test_completion
"git checkout " <<-\EOF
1529 other/branch-in-other Z
1530 other/main-in-other Z
1534 test_expect_success 'git checkout - with checkout.guess
= true
, completes refs and unique remote branches
for DWIM
' '
1535 test_config checkout.guess true
&&
1536 test_completion
"git checkout " <<-\EOF
1543 other/branch-in-other Z
1544 other/main-in-other Z
1548 test_expect_success 'git checkout - a later
--guess overrides previous checkout.guess
= false
, complete refs and unique remote branches
for DWIM
' '
1549 test_config checkout.guess false
&&
1550 test_completion
"git checkout --guess " <<-\EOF
1557 other/branch-in-other Z
1558 other/main-in-other Z
1562 test_expect_success 'git checkout - a later
--no-guess overrides previous checkout.guess
= true
, complete only refs
' '
1563 test_config checkout.guess true
&&
1564 test_completion
"git checkout --no-guess " <<-\EOF
1569 other/branch-in-other Z
1570 other/main-in-other Z
1574 test_expect_success 'git switch - with
--detach, complete all references
' '
1575 test_completion
"git switch --detach " <<-\EOF
1580 other/branch-in-other Z
1581 other/main-in-other Z
1585 test_expect_success 'git checkout - with
--detach, complete only references
' '
1586 test_completion
"git checkout --detach " <<-\EOF
1591 other/branch-in-other Z
1592 other/main-in-other Z
1596 test_expect_success 'setup sparse-checkout tests' '
1597 # set up sparse-checkout repo
1598 git init sparse-checkout &&
1600 cd sparse-checkout &&
1601 mkdir -p folder1/0/1 folder2/0 folder3 &&
1602 touch folder1/0/1/t.txt &&
1603 touch folder2/0/t.txt &&
1604 touch folder3/t.txt &&
1606 git commit -am "Initial commit"
1610 test_expect_success 'sparse-checkout completes subcommands' '
1611 test_completion "git sparse-checkout " <<-\
EOF
1621 test_expect_success 'cone mode sparse-checkout completes directory names' '
1622 # initialize sparse-checkout definitions
1623 git -C sparse-checkout sparse-checkout set --cone folder1/0 folder3 &&
1625 # test tab completion
1627 cd sparse-checkout &&
1628 test_completion "git sparse-checkout set f" <<-\EOF
1636 cd sparse-checkout &&
1637 test_completion "git sparse-checkout set folder1/" <<-\EOF
1643 cd sparse-checkout &&
1644 test_completion "git sparse-checkout set folder1/0/" <<-\EOF
1650 cd sparse-checkout/folder1 &&
1651 test_completion "git sparse-checkout add 0" <<-\EOF
1657 test_expect_success 'cone mode sparse-checkout completes directory names with spaces and accents' '
1658 # reset sparse-checkout
1659 git -C sparse-checkout sparse-checkout disable &&
1661 cd sparse-checkout &&
1662 mkdir "directory with spaces" &&
1663 mkdir "directory-with-áccent" &&
1664 >"directory with spaces/randomfile" &&
1665 >"directory-with-áccent/randomfile" &&
1667 git commit -m "Add directory with spaces and directory with accent" &&
1668 git sparse-checkout set --cone "directory with spaces" \
1669 "directory-with-áccent" &&
1670 test_completion "git sparse-checkout add dir" <<-\EOF &&
1671 directory with spaces/
1672 directory-with-áccent/
1674 rm -rf "directory with spaces" &&
1675 rm -rf "directory-with-áccent" &&
1677 git commit -m "Remove directory with spaces and directory with accent"
1681 # use FUNNYNAMES to avoid running on Windows, which doesn't permit tabs in paths
1682 test_expect_success FUNNYNAMES 'cone mode sparse-checkout completes directory names with tabs' '
1683 # reset sparse-checkout
1684 git -C sparse-checkout sparse-checkout disable &&
1686 cd sparse-checkout &&
1687 mkdir "$(printf "directory\twith\ttabs")" &&
1688 >"$(printf "directory\twith\ttabs")/randomfile" &&
1690 git commit -m "Add directory with tabs" &&
1691 git sparse-checkout set --cone \
1692 "$(printf "directory\twith\ttabs")" &&
1693 test_completion "git sparse-checkout add dir" <<-\EOF &&
1694 directory with tabs/
1696 rm -rf "$(printf "directory\twith\ttabs")" &&
1698 git commit -m "Remove directory with tabs"
1702 # use FUNNYNAMES to avoid running on Windows, and !CYGWIN for Cygwin, as neither permit backslashes in paths
1703 test_expect_success FUNNYNAMES,!CYGWIN 'cone mode sparse-checkout completes directory names with backslashes' '
1704 # reset sparse-checkout
1705 git -C sparse-checkout sparse-checkout disable &&
1707 cd sparse-checkout &&
1708 mkdir "directory\with\backslashes" &&
1709 >"directory\with\backslashes/randomfile" &&
1711 git commit -m "Add directory with backslashes" &&
1712 git sparse-checkout set --cone \
1713 "directory\with\backslashes" &&
1714 test_completion "git sparse-checkout add dir" <<-\EOF &&
1715 directory\with\backslashes/
1717 rm -rf "directory\with\backslashes" &&
1719 git commit -m "Remove directory with backslashes"
1723 test_expect_success 'non-cone mode sparse-checkout gives rooted paths' '
1724 # reset sparse-checkout repo to non-cone mode
1725 git -C sparse-checkout sparse-checkout disable &&
1726 git -C sparse-checkout sparse-checkout set --no-cone &&
1729 cd sparse-checkout &&
1730 # expected to be empty since we have not configured
1731 # custom completion for non-cone mode
1732 test_completion "git sparse-checkout set f" <<-\EOF
1733 /folder1/0/1/t.txt Z
1736 /folder1/out_sorted Z
1743 test_expect_success 'git sparse-checkout set --cone completes directory names' '
1744 git -C sparse-checkout sparse-checkout disable &&
1747 cd sparse-checkout &&
1748 test_completion "git sparse-checkout set --cone f" <<-\EOF
1756 test_expect_success 'git switch - with
-d, complete all references
' '
1757 test_completion
"git switch -d " <<-\EOF
1762 other/branch-in-other Z
1763 other/main-in-other Z
1767 test_expect_success 'git checkout - with
-d, complete only references
' '
1768 test_completion
"git checkout -d " <<-\EOF
1773 other/branch-in-other Z
1774 other/main-in-other Z
1778 test_expect_success 'git switch - with
--track, complete only remote branches
' '
1779 test_completion
"git switch --track " <<-\EOF &&
1780 other/branch-in-other Z
1781 other/main-in-other Z
1783 test_completion "git switch -t " <<-\
EOF
1784 other/branch-in-other Z
1785 other/main-in-other Z
1789 test_expect_success 'git checkout - with
--track, complete only remote branches
' '
1790 test_completion
"git checkout --track " <<-\EOF &&
1791 other/branch-in-other Z
1792 other/main-in-other Z
1794 test_completion "git checkout -t " <<-\
EOF
1795 other/branch-in-other Z
1796 other/main-in-other Z
1800 test_expect_success 'git switch - with
--no-track, complete only
local branch names
' '
1801 test_completion
"git switch --no-track " <<-\EOF
1807 test_expect_success 'git checkout - with
--no-track, complete only
local references
' '
1808 test_completion
"git checkout --no-track " <<-\EOF
1813 other/branch-in-other Z
1814 other/main-in-other Z
1818 test_expect_success 'git switch - with
-c, complete all references
' '
1819 test_completion
"git switch -c new-branch " <<-\EOF
1824 other/branch-in-other Z
1825 other/main-in-other Z
1829 test_expect_success 'git switch - with
-C, complete all references
' '
1830 test_completion
"git switch -C new-branch " <<-\EOF
1835 other/branch-in-other Z
1836 other/main-in-other Z
1840 test_expect_success 'git switch - with
-c and
--track, complete all references
' '
1841 test_completion
"git switch -c new-branch --track " <<-EOF
1846 other/branch-in-other Z
1847 other/main-in-other Z
1851 test_expect_success 'git switch
- with
-C and
--track, complete all references
' '
1852 test_completion
"git switch -C new-branch --track " <<-EOF
1857 other/branch-in-other Z
1858 other/main-in-other Z
1862 test_expect_success 'git switch
- with
-c and
--no-track, complete all references
' '
1863 test_completion
"git switch -c new-branch --no-track " <<-\EOF
1868 other/branch-in-other Z
1869 other/main-in-other Z
1873 test_expect_success 'git switch - with
-C and
--no-track, complete all references
' '
1874 test_completion
"git switch -C new-branch --no-track " <<-\EOF
1879 other/branch-in-other Z
1880 other/main-in-other Z
1884 test_expect_success 'git checkout - with
-b, complete all references
' '
1885 test_completion
"git checkout -b new-branch " <<-\EOF
1890 other/branch-in-other Z
1891 other/main-in-other Z
1895 test_expect_success 'git checkout - with
-B, complete all references
' '
1896 test_completion
"git checkout -B new-branch " <<-\EOF
1901 other/branch-in-other Z
1902 other/main-in-other Z
1906 test_expect_success 'git checkout - with
-b and
--track, complete all references
' '
1907 test_completion
"git checkout -b new-branch --track " <<-EOF
1912 other/branch-in-other Z
1913 other/main-in-other Z
1917 test_expect_success 'git checkout
- with
-B and
--track, complete all references
' '
1918 test_completion
"git checkout -B new-branch --track " <<-EOF
1923 other/branch-in-other Z
1924 other/main-in-other Z
1928 test_expect_success 'git checkout
- with
-b and
--no-track, complete all references
' '
1929 test_completion
"git checkout -b new-branch --no-track " <<-\EOF
1934 other/branch-in-other Z
1935 other/main-in-other Z
1939 test_expect_success 'git checkout - with
-B and
--no-track, complete all references
' '
1940 test_completion
"git checkout -B new-branch --no-track " <<-\EOF
1945 other/branch-in-other Z
1946 other/main-in-other Z
1950 test_expect_success 'git switch - for -c, complete
local branches and unique remote branches
' '
1951 test_completion
"git switch -c " <<-\EOF
1959 test_expect_success 'git switch - for -C, complete
local branches and unique remote branches
' '
1960 test_completion
"git switch -C " <<-\EOF
1968 test_expect_success 'git switch - for -c with
--no-guess, complete
local branches only
' '
1969 test_completion
"git switch --no-guess -c " <<-\EOF
1975 test_expect_success 'git switch - for -C with
--no-guess, complete
local branches only
' '
1976 test_completion
"git switch --no-guess -C " <<-\EOF
1982 test_expect_success 'git switch - for -c with
--no-track, complete
local branches only
' '
1983 test_completion
"git switch --no-track -c " <<-\EOF
1989 test_expect_success 'git switch - for -C with
--no-track, complete
local branches only
' '
1990 test_completion
"git switch --no-track -C " <<-\EOF
1996 test_expect_success 'git checkout - for -b, complete
local branches and unique remote branches
' '
1997 test_completion
"git checkout -b " <<-\EOF
2005 test_expect_success 'git checkout - for -B, complete
local branches and unique remote branches
' '
2006 test_completion
"git checkout -B " <<-\EOF
2014 test_expect_success 'git checkout - for -b with
--no-guess, complete
local branches only
' '
2015 test_completion
"git checkout --no-guess -b " <<-\EOF
2021 test_expect_success 'git checkout - for -B with
--no-guess, complete
local branches only
' '
2022 test_completion
"git checkout --no-guess -B " <<-\EOF
2028 test_expect_success 'git checkout - for -b with
--no-track, complete
local branches only
' '
2029 test_completion
"git checkout --no-track -b " <<-\EOF
2035 test_expect_success 'git checkout - for -B with
--no-track, complete
local branches only
' '
2036 test_completion
"git checkout --no-track -B " <<-\EOF
2042 test_expect_success 'git switch - with
--orphan completes
local branch names and unique remote branch names
' '
2043 test_completion
"git switch --orphan " <<-\EOF
2051 test_expect_success 'git switch - --orphan with branch already provided completes nothing
else' '
2052 test_completion
"git switch --orphan main " <<-\EOF
2057 test_expect_success 'git checkout - with
--orphan completes
local branch names and unique remote branch names
' '
2058 test_completion
"git checkout --orphan " <<-\EOF
2066 test_expect_success 'git checkout - --orphan with branch already provided completes
local refs
for a start-point
' '
2067 test_completion
"git checkout --orphan main " <<-\EOF
2072 other/branch-in-other Z
2073 other/main-in-other Z
2077 test_expect_success 'git restore completes modified files' '
2078 test_commit A a.file &&
2080 test_completion "git restore a." <<-\
EOF
2085 test_expect_success 'teardown after ref completion' '
2086 git branch -d matching-branch &&
2087 git tag -d matching-tag &&
2088 git remote remove other
2092 test_path_completion ()
2094 test $# = 2 || BUG "not 2 parameters to test_path_completion"
2096 local cur="$1" expected="$2"
2097 echo "$expected" >expected &&
2099 # In the following tests calling this function we only
2100 # care about how __git_complete_index_file() deals with
2101 # unusual characters in path names. By requesting only
2102 # untracked files we do not have to bother adding any
2103 # paths to the index in those tests.
2104 __git_complete_index_file --others &&
2107 test_cmp expected out
2110 test_expect_success 'setup for path completion tests' '
2114 touch simple-dir/simple-file \
2115 "spaces in dir/spaces in file" \
2116 "árvíztűrő/Сайн яваарай" &&
2117 if test_have_prereq !MINGW &&
2119 '$'separators\034in\035dir'' &&
2120 touch BS\\dir/DQ\"file \
2121 '$'separators\034in\035dir/sep\036in\037file''
2123 test_set_prereq FUNNIERNAMES
2125 rm -rf BS\\dir '$'separators\034in\035dir''
2129 test_expect_success '__git_complete_index_file - simple
' '
2130 test_path_completion simple simple-dir
&& # Bash is supposed to
2131 # add the trailing /.
2132 test_path_completion simple-dir
/simple simple-dir
/simple-file
2135 test_expect_success \
2136 '__git_complete_index_file
- escaped characters on cmdline
' '
2137 test_path_completion spac
"spaces in dir" && # Bash will turn this
2138 # into "spaces\ in\ dir"
2139 test_path_completion
"spaces\\ i" \
2141 test_path_completion
"spaces\\ in\\ dir/s" \
2142 "spaces in dir/spaces in file" &&
2143 test_path_completion
"spaces\\ in\\ dir/spaces\\ i" \
2144 "spaces in dir/spaces in file"
2147 test_expect_success \
2148 '__git_complete_index_file
- quoted characters on cmdline
' '
2149 # Testing with an opening but without a corresponding closing
2150 # double quote is important.
2151 test_path_completion
\"spac
"spaces in dir" &&
2152 test_path_completion
"\"spaces i" \
2154 test_path_completion
"\"spaces in dir/s" \
2155 "spaces in dir/spaces in file" &&
2156 test_path_completion
"\"spaces in dir/spaces i" \
2157 "spaces in dir/spaces in file"
2160 test_expect_success '__git_complete_index_file
- UTF-8
in ls-files output
' '
2161 test_path_completion á árvíztűrő
&&
2162 test_path_completion árvíztűrő
/С
"árvíztűrő/Сайн яваарай"
2165 test_expect_success FUNNIERNAMES \
2166 '__git_complete_index_file
- C-style escapes
in ls-files output
' '
2167 test_path_completion BS \
2169 test_path_completion BS
\\\\d \
2171 test_path_completion BS
\\\\dir
/DQ \
2173 test_path_completion BS
\\\\dir
/DQ
\\\"f \
2177 test_expect_success FUNNIERNAMES \
2178 '__git_complete_index_file
- \nnn
-escaped characters
in ls-files output
' '
2179 test_path_completion sep
'$'separators
\034in\035dir
'' &&
2180 test_path_completion
'$'separators
\034i
'' \
2181 '$'separators
\034in\035dir
'' &&
2182 test_path_completion
'$'separators
\034in\035dir
/sep
'' \
2183 '$'separators
\034in\035dir
/sep
\036in\037file'' &&
2184 test_path_completion
'$'separators
\034in\035dir
/sep
\036i
'' \
2185 '$'separators
\034in\035dir
/sep
\036in\037file''
2188 test_expect_success FUNNYNAMES \
2189 '__git_complete_index_file
- removing repeated quoted path components
' '
2190 test_when_finished
rm -r repeated-quoted
&&
2191 mkdir repeated-quoted
&& # A directory whose name in itself
2192 # would not be quoted ...
2193 >repeated-quoted
/0-file &&
2194 >repeated-quoted
/1\"file && # ... but here the file makes the
2195 # dirname quoted ...
2196 >repeated-quoted
/2-file &&
2197 >repeated-quoted
/3\"file && # ... and here, too.
2199 # Still, we shold only list the directory name only once.
2200 test_path_completion repeated repeated-quoted
2203 test_expect_success 'teardown after path completion tests
' '
2204 rm -rf simple-dir
"spaces in dir" árvíztűrő \
2205 BS
\\dir
'$'separators
\034in\035dir
''
2208 test_expect_success '__git_find_on_cmdline
- single match
' '
2209 echo list
>expect
&&
2211 words
=(git
command --opt list
) &&
2212 cword
=${#words[@]} &&
2214 __git_find_on_cmdline
"add list remove" >actual
2216 test_cmp expect actual
2219 test_expect_success '__git_find_on_cmdline
- multiple matches
' '
2220 echo remove
>expect
&&
2222 words
=(git
command -o --opt remove list add
) &&
2223 cword
=${#words[@]} &&
2225 __git_find_on_cmdline
"add list remove" >actual
2227 test_cmp expect actual
2230 test_expect_success '__git_find_on_cmdline
- no match
' '
2232 words
=(git
command --opt branch
) &&
2233 cword
=${#words[@]} &&
2235 __git_find_on_cmdline
"add list remove" >actual
2237 test_must_be_empty actual
2240 test_expect_success '__git_find_on_cmdline
- single match with index
' '
2241 echo "3 list" >expect
&&
2243 words
=(git
command --opt list
) &&
2244 cword
=${#words[@]} &&
2246 __git_find_on_cmdline
--show-idx "add list remove" >actual
2248 test_cmp expect actual
2251 test_expect_success '__git_find_on_cmdline
- multiple matches with index
' '
2252 echo "4 remove" >expect
&&
2254 words
=(git
command -o --opt remove list add
) &&
2255 cword
=${#words[@]} &&
2257 __git_find_on_cmdline
--show-idx "add list remove" >actual
2259 test_cmp expect actual
2262 test_expect_success '__git_find_on_cmdline
- no match with index
' '
2264 words
=(git
command --opt branch
) &&
2265 cword
=${#words[@]} &&
2267 __git_find_on_cmdline
--show-idx "add list remove" >actual
2269 test_must_be_empty actual
2272 test_expect_success '__git_find_on_cmdline
- ignores matches before
command with index
' '
2273 echo "6 remove" >expect
&&
2275 words
=(git
-C remove
command -o --opt remove list add
) &&
2276 cword
=${#words[@]} &&
2278 __git_find_on_cmdline
--show-idx "add list remove" >actual
2280 test_cmp expect actual
2283 test_expect_success '__git_get_config_variables
' '
2284 cat >expect
<<-EOF &&
2288 test_config interesting.name-1 good
&&
2289 test_config interesting.name-2 good
&&
2290 test_config subsection.interesting.name-3 bad
&&
2291 __git_get_config_variables interesting
>actual
&&
2292 test_cmp expect actual
2295 test_expect_success '__git_pretty_aliases
' '
2296 cat >expect
<<-EOF &&
2300 test_config pretty.author
"%an %ae" &&
2301 test_config pretty.
hash %H
&&
2302 __git_pretty_aliases
>actual
&&
2303 test_cmp expect actual
2306 test_expect_success 'basic
' '
2307 run_completion
"git " &&
2309 grep -q "^add \$" out
&&
2311 grep -q "^rebase \$" out
&&
2313 ! grep -q "^ls-files \$" out
&&
2315 run_completion
"git r" &&
2316 ! grep -q -v "^r" out
2319 test_expect_success 'double dash
"git" itself
' '
2320 test_completion
"git --" <<-\EOF
2333 --no-replace-objects Z
2338 test_expect_success 'double dash "git checkout"' '
2339 test_completion "git checkout --" <<-\
EOF
2349 --ignore-skip-worktree-bits Z
2350 --ignore-other-worktrees Z
2351 --recurse-submodules Z
2357 --pathspec-file-nul Z
2358 --pathspec-from-file=Z
2362 test_expect_success 'general options' '
2363 test_completion "git --ver" "--version " &&
2364 test_completion "git --hel" "--help " &&
2365 test_completion "git --exe" <<-\EOF &&
2369 test_completion "git --htm" "--html-path " &&
2370 test_completion "git --pag" "--paginate " &&
2371 test_completion "git --no-p" "--no-pager " &&
2372 test_completion "git --git" "--git-dir=" &&
2373 test_completion "git --wor" "--work-tree=" &&
2374 test_completion "git --nam" "--namespace=" &&
2375 test_completion "git --bar" "--bare " &&
2376 test_completion "git --inf" "--info-path " &&
2377 test_completion "git --no-r" "--no-replace-objects "
2380 test_expect_success 'general options plus command' '
2381 test_completion "git --version check" "checkout " &&
2382 test_completion "git --paginate check" "checkout " &&
2383 test_completion "git --git-dir=foo check" "checkout " &&
2384 test_completion "git --bare check" "checkout " &&
2385 test_completion "git --exec-path=foo check" "checkout " &&
2386 test_completion "git --html-path check" "checkout " &&
2387 test_completion "git --no-pager check" "checkout " &&
2388 test_completion "git --work-tree=foo check" "checkout " &&
2389 test_completion "git --namespace=foo check" "checkout " &&
2390 test_completion "git --paginate check" "checkout " &&
2391 test_completion "git --info-path check" "checkout " &&
2392 test_completion "git --no-replace-objects check" "checkout " &&
2393 test_completion "git --git-dir some/path check" "checkout " &&
2394 test_completion "git -c conf.var=value check" "checkout " &&
2395 test_completion "git -C some/path check" "checkout " &&
2396 test_completion "git --work-tree some/path check" "checkout " &&
2397 test_completion "git --namespace name/space check" "checkout "
2400 test_expect_success 'git --help completion' '
2401 test_completion "git --help ad" "add " &&
2402 test_completion "git --help core" "core-tutorial "
2405 test_expect_success 'completion.commands removes multiple commands' '
2406 test_config completion.commands "-cherry -mergetool" &&
2407 git --list-cmds=list-mainporcelain,list-complete,config >out &&
2408 ! grep -E "^(cherry|mergetool)$" out
2411 test_expect_success 'setup for integration tests' '
2412 echo content >file1 &&
2414 git add file1 file2 &&
2415 git commit -m one &&
2416 git branch mybranch &&
2420 test_expect_success 'checkout completes ref names' '
2421 test_completion "git checkout m" <<-\EOF
2428 test_expect_success 'checkout does not match ref names of a different case' '
2429 test_completion "git checkout M" ""
2432 test_expect_success 'checkout matches case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2434 GIT_COMPLETION_IGNORE_CASE=1 &&
2435 test_completion "git checkout M" <<-\EOF
2443 test_expect_success 'checkout completes pseudo refs' '
2444 test_completion "git checkout H" <<-\EOF
2449 test_expect_success 'checkout completes pseudo refs case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2451 GIT_COMPLETION_IGNORE_CASE=1 &&
2452 test_completion "git checkout h" <<-\EOF
2458 test_expect_success 'git -C <path> checkout uses the right repo' '
2459 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
2464 test_expect_success 'show completes all refs' '
2465 test_completion "git show m" <<-\EOF
2472 test_expect_success '<ref>: completes paths' '
2473 test_completion "git show mytag:f" <<-\EOF
2479 test_expect_success 'complete tree filename with spaces' '
2480 echo content >"name with spaces" &&
2481 git add "name with spaces" &&
2482 git commit -m spaces &&
2483 test_completion "git show HEAD:nam" <<-\EOF
2488 test_expect_success 'complete tree filename with metacharacters' '
2489 echo content >"name with \${meta}" &&
2490 git add "name with \${meta}" &&
2491 git commit -m meta &&
2492 test_completion "git show HEAD:nam" <<-\EOF
2498 test_expect_success PERL 'send-email' '
2499 test_completion "git send-email --cov" <<-\EOF &&
2500 --cover-from-description=Z
2503 test_completion "git send-email --val" <<-\EOF &&
2506 test_completion "git send-email ma" "main "
2509 test_expect_success 'complete files' '
2510 git init tmp && cd tmp &&
2511 test_when_finished "cd .. && rm -rf tmp" &&
2513 echo "expected" > .gitignore &&
2514 echo "out" >> .gitignore &&
2515 echo "out_sorted" >> .gitignore &&
2517 git add .gitignore &&
2518 test_completion "git commit " ".gitignore" &&
2520 git commit -m ignore &&
2523 test_completion "git add " "new" &&
2526 git commit -a -m new &&
2527 test_completion "git add " "" &&
2529 git mv new modified &&
2530 echo modify > modified &&
2531 test_completion "git add " "modified" &&
2533 mkdir -p some/deep &&
2534 touch some/deep/path &&
2535 test_completion "git add some/" "some/deep" &&
2536 git clean -f some &&
2540 : TODO .gitignore should not be here &&
2541 test_completion "git rm " <<-\EOF &&
2546 test_completion "git clean " "untracked" &&
2548 : TODO .gitignore should not be here &&
2549 test_completion "git mv " <<-\EOF &&
2555 touch dir/file-in-dir &&
2556 git add dir/file-in-dir &&
2557 git commit -m dir &&
2559 mkdir untracked-dir &&
2561 : TODO .gitignore should not be here &&
2562 test_completion "git mv modified " <<-\EOF &&
2570 test_completion "git commit " "modified" &&
2572 : TODO .gitignore should not be here &&
2573 test_completion "git ls-files " <<-\EOF &&
2580 test_completion "git add mom" "momified"
2583 test_expect_success "simple alias" '
2584 test_config alias.co checkout &&
2585 test_completion "git co m" <<-\EOF
2592 test_expect_success "recursive alias" '
2593 test_config alias.co checkout &&
2594 test_config alias.cod "co --detached" &&
2595 test_completion "git cod m" <<-\EOF
2602 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
2603 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
2604 test_completion "git co m" <<-\EOF
2611 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
2612 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
2613 test_completion "git co m" <<-\EOF
2620 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
2621 test_config alias.co "!f() { : git checkout ; if ... } f" &&
2622 test_completion "git co m" <<-\EOF
2629 test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd> ; ... }' '
2630 test_config alias.co "!f() { : checkout ; if ... } f" &&
2631 test_completion "git co m" <<-\EOF
2638 test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd>; ... }' '
2639 test_config alias.co "!f() { : checkout; if ... } f" &&
2640 test_completion "git co m" <<-\EOF
2647 test_expect_success 'completion without explicit _git_xxx function' '
2648 test_completion "git version --" <<-\EOF
2650 --no-build-options Z
2654 test_expect_failure 'complete with tilde expansion' '
2655 git init tmp && cd tmp &&
2656 test_when_finished "cd .. && rm -rf tmp" &&
2660 test_completion "git add ~/tmp/" "~/tmp/file"
2663 test_expect_success 'setup other remote for remote reference completion' '
2664 git remote add other otherrepo &&
2668 for flag in -d --delete
2670 test_expect_success "__git_complete_remote_or_refspec - push
$flag other
" '
2671 sed -e "s
/Z$
//" >expected <<-EOF &&
2675 words=(git push '$flag' other ma) &&
2676 cword=${#words[@]} cur=${words[cword-1]} &&
2678 __git_complete_remote_or_refspec &&
2681 test_cmp expected out
2684 test_expect_failure "__git_complete_remote_or_refspec
- push other
$flag" '
2685 sed -e "s
/Z$
//" >expected <<-EOF &&
2689 words=(git push other '$flag' ma) &&
2690 cword=${#words[@]} cur=${words[cword-1]} &&
2692 __git_complete_remote_or_refspec &&
2695 test_cmp expected out
2699 test_expect_success 'git config - section' '
2700 test_completion "git config br
" <<-\EOF
2706 test_expect_success 'git config - section include, includeIf' '
2707 test_completion "git config inclu
" <<-\EOF
2713 test_expect_success 'git config - variable name' '
2714 test_completion "git config log.d
" <<-\EOF
2721 test_expect_success 'git config - variable name include' '
2722 test_completion "git config include.p
" <<-\EOF
2727 test_expect_success 'setup for git config submodule tests' '
2728 test_create_repo sub &&
2729 test_commit -C sub initial &&
2730 git submodule add ./sub
2733 test_expect_success 'git config - variable name - submodule and __git_compute_first_level_config_vars_for_section' '
2734 test_completion "git config submodule.
" <<-\EOF
2736 submodule.alternateErrorStrategy Z
2737 submodule.alternateLocation Z
2738 submodule.fetchJobs Z
2739 submodule.propagateBranches Z
2745 test_expect_success 'git config - variable name - __git_compute_second_level_config_vars_for_section' '
2746 test_completion "git config submodule.sub.
" <<-\EOF
2748 submodule.sub.update Z
2749 submodule.sub.branch Z
2750 submodule.sub.fetchRecurseSubmodules Z
2751 submodule.sub.ignore Z
2752 submodule.sub.active Z
2756 test_expect_success 'git config - value' '
2757 test_completion "git config color.pager
" <<-\EOF
2763 test_expect_success 'git -c - section' '
2764 test_completion "git
-c br
" <<-\EOF
2770 test_expect_success 'git -c - variable name' '
2771 test_completion "git
-c log.d
" <<-\EOF
2778 test_expect_success 'git -c - value' '
2779 test_completion "git
-c color.pager
=" <<-\EOF
2785 test_expect_success 'git clone --config= - section' '
2786 test_completion "git clone
--config=br
" <<-\EOF
2792 test_expect_success 'git clone --config= - variable name' '
2793 test_completion "git clone
--config=log.d
" <<-\EOF
2800 test_expect_success 'git clone --config= - value' '
2801 test_completion "git clone
--config=color.pager
=" <<-\EOF
2807 test_expect_success 'options with value' '
2808 test_completion "git merge
-X diff-algorithm
=" <<-\EOF
2813 test_expect_success 'sourcing the completion script clears cached commands' '
2815 __git_compute_all_commands &&
2816 test -n "$__git_all_commands" &&
2817 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2818 test -z "$__git_all_commands"
2822 test_expect_success 'sourcing the completion script clears cached merge strategies' '
2824 __git_compute_merge_strategies &&
2825 test -n "$__git_merge_strategies" &&
2826 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2827 test -z "$__git_merge_strategies"
2831 test_expect_success 'sourcing the completion script clears cached --options' '
2833 __gitcomp_builtin checkout &&
2834 test -n "$__gitcomp_builtin_checkout" &&
2835 __gitcomp_builtin notes_edit &&
2836 test -n "$__gitcomp_builtin_notes_edit" &&
2837 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2838 test -z "$__gitcomp_builtin_checkout" &&
2839 test -z "$__gitcomp_builtin_notes_edit"
2843 test_expect_success 'option aliases are not shown by default' '
2844 test_completion "git clone
--recurs" "--recurse-submodules "
2847 test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
2849 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2850 GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2851 test_completion "git clone
--recurs" <<-\EOF
2852 --recurse-submodules Z
2858 test_expect_success 'plumbing commands are excluded without GIT_COMPLETION_SHOW_ALL_COMMANDS' '
2860 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2861 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2863 # Just mainporcelain, not plumbing commands
2864 run_completion "git c
" &&
2865 grep checkout out &&
2870 test_expect_success 'all commands are shown with GIT_COMPLETION_SHOW_ALL_COMMANDS (also main non-builtin)' '
2872 . "$GIT_BUILD_DIR/contrib
/completion
/git-completion.bash
" &&
2873 GIT_COMPLETION_SHOW_ALL_COMMANDS=1 &&
2874 export GIT_COMPLETION_SHOW_ALL_COMMANDS &&
2875 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2877 # Both mainporcelain and plumbing commands
2878 run_completion "git c
" &&
2879 grep checkout out &&
2880 grep cat-file out &&
2882 # Check "gitk
", a "main
" command, but not a built-in + more plumbing
2883 run_completion "git g
" &&
2885 grep get-tar-commit-id out
2889 test_expect_success '__git_complete' '
2890 unset -f __git_wrap__git_main &&
2892 __git_complete foo __git_main &&
2893 __git_have_func __git_wrap__git_main &&
2894 unset -f __git_wrap__git_main &&
2896 __git_complete gf _git_fetch &&
2897 __git_have_func __git_wrap_git_fetch &&
2899 __git_complete foo git &&
2900 __git_have_func __git_wrap__git_main &&
2901 unset -f __git_wrap__git_main &&
2903 __git_complete gd git_diff &&
2904 __git_have_func __git_wrap_git_diff &&
2906 test_must_fail __git_complete ga missing
2909 test_expect_success '__git_pseudoref_exists' '
2910 test_when_finished "rm -rf repo
" &&
2914 sane_unset __git_repo_path &&
2916 # HEAD should exist, even if it points to an unborn branch.
2917 __git_pseudoref_exists HEAD >output 2>&1 &&
2918 test_must_be_empty output &&
2920 # HEAD points to an existing branch, so it should exist.
2922 __git_pseudoref_exists HEAD >output 2>&1 &&
2923 test_must_be_empty output &&
2925 # CHERRY_PICK_HEAD does not exist, so the existence check should fail.
2926 ! __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
2927 test_must_be_empty output &&
2929 # CHERRY_PICK_HEAD points to a commit, so it should exist.
2930 git update-ref CHERRY_PICK_HEAD A &&
2931 __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
2932 test_must_be_empty output