contrib/coverage-diff: avoid redundant pipelines
[git/gitster.git] / t / t9902-completion.sh
blob569cf2310434358d268028ea8d26361727bf8652
1 #!/bin/sh
3 # Copyright (c) 2012-2020 Felipe Contreras
6 test_description='test bash completion'
8 # The Bash completion scripts must not print anything to either stdout or
9 # stderr, which we try to verify. When tracing is enabled without support for
10 # BASH_XTRACEFD this assertion will fail, so we have to mark the test as
11 # untraceable with such ancient Bash versions.
12 test_untraceable=UnfortunatelyYes
14 # Override environment and always use master for the default initial branch
15 # name for these tests, so that rev completion candidates are as expected.
16 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
17 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
19 . ./lib-bash.sh
21 complete ()
23 # do nothing
24 return 0
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
38 # the way.
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
55 case "$1" in
56 cur)
57 cur=${_words[_cword]}
59 prev)
60 prev=${_words[_cword-1]}
62 words)
63 words=("${_words[@]}")
65 cword)
66 cword=$_cword
68 esac
69 shift
70 done
73 print_comp ()
75 local IFS=$'\n'
76 echo "${COMPREPLY[*]}" > out
79 run_completion ()
81 local -a COMPREPLY _words
82 local _cword
83 _words=( $1 )
84 test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
85 (( _cword = ${#_words[@]} - 1 ))
86 __git_wrap__git_main && print_comp
89 # Test high-level completion
90 # Arguments are:
91 # 1: typed text so far (cur)
92 # 2: expected completion
93 test_completion ()
95 if test $# -gt 1
96 then
97 printf '%s\n' "$2" >expected
98 else
99 sed -e 's/Z$//' |sort >expected
100 fi &&
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
108 # Test __gitcomp.
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().
112 test_gitcomp ()
114 local -a COMPREPLY &&
115 sed -e 's/Z$//' >expected &&
116 local cur="$1" &&
117 shift &&
118 __gitcomp "$@" &&
119 print_comp &&
120 test_cmp expected out
123 # Test __gitcomp_nl
124 # Arguments are:
125 # 1: current word (cur)
126 # -: the rest are passed to __gitcomp_nl
127 test_gitcomp_nl ()
129 local -a COMPREPLY &&
130 sed -e 's/Z$//' >expected &&
131 local cur="$1" &&
132 shift &&
133 __gitcomp_nl "$@" &&
134 print_comp &&
135 test_cmp expected out
138 invalid_variable_name='${foo.bar}'
140 actual="$TRASH_DIRECTORY/actual"
142 if test_have_prereq MINGW
143 then
144 ROOT="$(pwd -W)"
145 else
146 ROOT="$(pwd)"
149 test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
150 mkdir -p subdir/subsubdir &&
151 mkdir -p non-repo &&
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"
161 ) &&
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"
170 ) &&
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"
180 ) &&
181 test_cmp expected "$actual"
184 test_expect_success '__git_find_repo_path - cwd is a .git directory' '
185 echo "." >expected &&
187 cd .git &&
188 __git_find_repo_path &&
189 echo "$__git_repo_path" >"$actual"
190 ) &&
191 test_cmp expected "$actual"
194 test_expect_success '__git_find_repo_path - parent is a .git directory' '
195 echo "$ROOT/.git" >expected &&
197 cd .git/objects &&
198 __git_find_repo_path &&
199 echo "$__git_repo_path" >"$actual"
200 ) &&
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" &&
208 export GIT_DIR &&
209 __git_find_repo_path &&
210 echo "$__git_repo_path" >"$actual"
211 ) &&
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" &&
219 export GIT_DIR &&
220 cd subdir &&
221 __git_find_repo_path &&
222 echo "$__git_repo_path" >"$actual"
223 ) &&
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"
234 ) &&
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 &&
241 cd subdir &&
242 __git_dir="otherrepo/.git" &&
243 __git_C_args=(-C ..) &&
244 __git_find_repo_path &&
245 echo "$__git_repo_path" >"$actual"
246 ) &&
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" &&
254 export GIT_DIR &&
255 __git_C_args=(-C otherrepo) &&
256 __git_find_repo_path &&
257 echo "$__git_repo_path" >"$actual"
258 ) &&
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 &&
265 cd subdir &&
266 GIT_DIR="otherrepo/.git" &&
267 export GIT_DIR &&
268 __git_C_args=(-C ..) &&
269 __git_find_repo_path &&
270 echo "$__git_repo_path" >"$actual"
271 ) &&
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"
281 ) &&
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 &&
288 cd .git &&
289 __git_C_args=(-C .. -C otherrepo) &&
290 __git_find_repo_path &&
291 echo "$__git_repo_path" >"$actual"
292 ) &&
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 &&
299 cd subdir &&
300 __git_C_args=(-C .. -C otherrepo) &&
301 __git_find_repo_path &&
302 echo "$__git_repo_path" >"$actual"
303 ) &&
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"
312 ) &&
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"
321 ) &&
322 test_must_be_empty "$actual"
325 test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
327 GIT_DIR="$ROOT/non-existing" &&
328 export GIT_DIR &&
329 test_must_fail __git_find_repo_path &&
330 printf "$__git_repo_path" >"$actual"
331 ) &&
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" &&
340 cd subdir &&
341 __git_find_repo_path &&
342 echo "$__git_repo_path" >"$actual"
343 ) &&
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"
355 ) &&
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" &&
366 cd link &&
367 __git_find_repo_path &&
368 echo "$__git_repo_path" >"$actual"
369 ) &&
370 test_cmp expected "$actual"
373 test_expect_success '__git_find_repo_path - not a git repository' '
375 cd non-repo &&
376 GIT_CEILING_DIRECTORIES="$ROOT" &&
377 export GIT_CEILING_DIRECTORIES &&
378 test_must_fail __git_find_repo_path &&
379 printf "$__git_repo_path" >"$actual"
380 ) &&
381 test_must_be_empty "$actual"
384 test_expect_success '__gitdir - finds repo' '
385 echo "$ROOT/.git" >expected &&
387 cd subdir/subsubdir &&
388 __gitdir >"$actual"
389 ) &&
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"
398 ) &&
399 test_must_be_empty "$actual"
402 test_expect_success '__gitdir - repo as argument' '
403 echo "otherrepo/.git" >expected &&
405 __gitdir "otherrepo" >"$actual"
406 ) &&
407 test_cmp expected "$actual"
410 test_expect_success '__gitdir - remote as argument' '
411 echo "remote" >expected &&
413 __gitdir "remote" >"$actual"
414 ) &&
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
462 --option Z
463 --option=Z
464 $invalid_variable_name Z
467 cur=should_be_ignored &&
468 __gitcomp_direct "$(cat expected)" &&
469 print_comp
470 ) &&
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
477 --reuse-message=Z
478 --reedit-message=Z
479 --reset-author Z
483 test_expect_success '__gitcomp - trailing space - config keys' '
484 test_gitcomp "br" "branch. branch.autosetupmerge
485 branch.autosetuprebase browser." <<-\EOF
486 branch.Z
487 branch.autosetupmerge Z
488 branch.autosetuprebase Z
489 browser.Z
493 test_expect_success '__gitcomp - option parameter' '
494 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
495 "" "re" <<-\EOF
496 recursive Z
497 resolve Z
501 test_expect_success '__gitcomp - prefix' '
502 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
503 "branch.maint." "me" <<-\EOF
504 branch.maint.merge Z
505 branch.maint.mergeoptions Z
509 test_expect_success '__gitcomp - suffix' '
510 test_gitcomp "branch.me" "master maint next seen" "branch." \
511 "ma" "." <<-\EOF
512 branch.master.Z
513 branch.maint.Z
517 test_expect_success '__gitcomp - ignore optional negative options' '
518 test_gitcomp "--" "--abc --def --no-one -- --no-two" <<-\EOF
519 --abc Z
520 --def Z
521 --no-one Z
522 --no-... Z
526 test_expect_success '__gitcomp - ignore/narrow optional negative options' '
527 test_gitcomp "--a" "--abc --abcdef --no-one -- --no-two" <<-\EOF
528 --abc Z
529 --abcdef Z
533 test_expect_success '__gitcomp - ignore/narrow optional negative options' '
534 test_gitcomp "--n" "--abc --def --no-one -- --no-two" <<-\EOF
535 --no-one Z
536 --no-... Z
540 test_expect_success '__gitcomp - expand all negative options' '
541 test_gitcomp "--no-" "--abc --def --no-one -- --no-two" <<-\EOF
542 --no-one Z
543 --no-two Z
547 test_expect_success '__gitcomp - expand/narrow all negative options' '
548 test_gitcomp "--no-o" "--abc --def --no-one -- --no-two" <<-\EOF
549 --no-one Z
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
567 main
568 maint
569 next
570 seen
573 test_expect_success '__gitcomp_nl - trailing space' '
574 test_gitcomp_nl "m" "$refs" <<-EOF
575 main Z
576 maint Z
580 test_expect_success '__gitcomp_nl - prefix' '
581 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
582 --fixup=main Z
583 --fixup=maint Z
587 test_expect_success '__gitcomp_nl - suffix' '
588 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
589 branch.main.Z
590 branch.maint.Z
594 test_expect_success '__gitcomp_nl - no suffix' '
595 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
596 mainZ
597 maintZ
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 &&
607 remote_from_file_1
608 remote_from_file_2
609 remote_in_config_1
610 remote_in_config_2
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
622 ) &&
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 &&
643 cd otherrepo &&
644 git commit --allow-empty -m initial &&
645 git branch -m main main-in-other &&
646 git branch branch-in-other &&
647 git tag tag-in-other
648 ) &&
649 git remote add other "$ROOT/otherrepo/.git" &&
650 git fetch --no-tags other &&
651 rm -f .git/FETCH_HEAD &&
652 git init thirdrepo
655 test_expect_success '__git_refs - simple' '
656 cat >expected <<-EOF &&
657 HEAD
658 main
659 matching-branch
660 other/branch-in-other
661 other/main-in-other
662 matching-tag
665 cur= &&
666 __git_refs >"$actual"
667 ) &&
668 test_cmp expected "$actual"
671 test_expect_success '__git_refs - full refs' '
672 cat >expected <<-EOF &&
673 refs/heads/main
674 refs/heads/matching-branch
675 refs/remotes/other/branch-in-other
676 refs/remotes/other/main-in-other
677 refs/tags/matching-tag
680 cur=refs/heads/ &&
681 __git_refs >"$actual"
682 ) &&
683 test_cmp expected "$actual"
686 test_expect_success '__git_refs - repo given on the command line' '
687 cat >expected <<-EOF &&
688 HEAD
689 branch-in-other
690 main-in-other
691 tag-in-other
694 __git_dir="$ROOT/otherrepo/.git" &&
695 cur= &&
696 __git_refs >"$actual"
697 ) &&
698 test_cmp expected "$actual"
701 test_expect_success '__git_refs - remote on local file system' '
702 cat >expected <<-EOF &&
703 HEAD
704 branch-in-other
705 main-in-other
706 tag-in-other
709 cur= &&
710 __git_refs otherrepo >"$actual"
711 ) &&
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
722 cur=refs/ &&
723 __git_refs otherrepo >"$actual"
724 ) &&
725 test_cmp expected "$actual"
728 test_expect_success '__git_refs - configured remote' '
729 cat >expected <<-EOF &&
730 HEAD
731 branch-in-other
732 main-in-other
735 cur= &&
736 __git_refs other >"$actual"
737 ) &&
738 test_cmp expected "$actual"
741 test_expect_success '__git_refs - configured remote - full refs' '
742 cat >expected <<-EOF &&
743 HEAD
744 refs/heads/branch-in-other
745 refs/heads/main-in-other
746 refs/tags/tag-in-other
749 cur=refs/ &&
750 __git_refs other >"$actual"
751 ) &&
752 test_cmp expected "$actual"
755 test_expect_success '__git_refs - configured remote - repo given on the command line' '
756 cat >expected <<-EOF &&
757 HEAD
758 branch-in-other
759 main-in-other
762 cd thirdrepo &&
763 __git_dir="$ROOT/.git" &&
764 cur= &&
765 __git_refs other >"$actual"
766 ) &&
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 &&
772 HEAD
773 refs/heads/branch-in-other
774 refs/heads/main-in-other
775 refs/tags/tag-in-other
778 cd thirdrepo &&
779 __git_dir="$ROOT/.git" &&
780 cur=refs/ &&
781 __git_refs other >"$actual"
782 ) &&
783 test_cmp expected "$actual"
786 test_expect_success '__git_refs - configured remote - remote name matches a directory' '
787 cat >expected <<-EOF &&
788 HEAD
789 branch-in-other
790 main-in-other
792 mkdir other &&
793 test_when_finished "rm -rf other" &&
795 cur= &&
796 __git_refs other >"$actual"
797 ) &&
798 test_cmp expected "$actual"
801 test_expect_success '__git_refs - URL remote' '
802 cat >expected <<-EOF &&
803 HEAD
804 branch-in-other
805 main-in-other
806 tag-in-other
809 cur= &&
810 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
811 ) &&
812 test_cmp expected "$actual"
815 test_expect_success '__git_refs - URL remote - full refs' '
816 cat >expected <<-EOF &&
817 HEAD
818 refs/heads/branch-in-other
819 refs/heads/main-in-other
820 refs/tags/tag-in-other
823 cur=refs/ &&
824 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
825 ) &&
826 test_cmp expected "$actual"
829 test_expect_success '__git_refs - non-existing remote' '
831 cur= &&
832 __git_refs non-existing >"$actual"
833 ) &&
834 test_must_be_empty "$actual"
837 test_expect_success '__git_refs - non-existing remote - full refs' '
839 cur=refs/ &&
840 __git_refs non-existing >"$actual"
841 ) &&
842 test_must_be_empty "$actual"
845 test_expect_success '__git_refs - non-existing URL remote' '
847 cur= &&
848 __git_refs "file://$ROOT/non-existing" >"$actual"
849 ) &&
850 test_must_be_empty "$actual"
853 test_expect_success '__git_refs - non-existing URL remote - full refs' '
855 cur=refs/ &&
856 __git_refs "file://$ROOT/non-existing" >"$actual"
857 ) &&
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 &&
865 cd subdir &&
866 cur= &&
867 __git_refs >"$actual"
868 ) &&
869 test_must_be_empty "$actual"
872 test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
873 cat >expected <<-EOF &&
874 HEAD
875 main
876 matching-branch
877 other/ambiguous
878 other/branch-in-other
879 other/main-in-other
880 remote/ambiguous
881 remote/branch-in-remote
882 matching-tag
883 branch-in-other
884 branch-in-remote
885 main-in-other
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
893 done &&
895 cur= &&
896 __git_refs "" 1 >"$actual"
897 ) &&
898 test_cmp expected "$actual"
901 test_expect_success '__git_refs - after --opt=' '
902 cat >expected <<-EOF &&
903 HEAD
904 main
905 matching-branch
906 other/branch-in-other
907 other/main-in-other
908 matching-tag
911 cur="--opt=" &&
912 __git_refs "" "" "" "" >"$actual"
913 ) &&
914 test_cmp expected "$actual"
917 test_expect_success '__git_refs - after --opt= - full refs' '
918 cat >expected <<-EOF &&
919 refs/heads/main
920 refs/heads/matching-branch
921 refs/remotes/other/branch-in-other
922 refs/remotes/other/main-in-other
923 refs/tags/matching-tag
926 cur="--opt=refs/" &&
927 __git_refs "" "" "" refs/ >"$actual"
928 ) &&
929 test_cmp expected "$actual"
932 test_expect_success '__git refs - excluding refs' '
933 cat >expected <<-EOF &&
934 ^HEAD
935 ^main
936 ^matching-branch
937 ^other/branch-in-other
938 ^other/main-in-other
939 ^matching-tag
942 cur=^ &&
943 __git_refs >"$actual"
944 ) &&
945 test_cmp expected "$actual"
948 test_expect_success '__git refs - excluding full refs' '
949 cat >expected <<-EOF &&
950 ^refs/heads/main
951 ^refs/heads/matching-branch
952 ^refs/remotes/other/branch-in-other
953 ^refs/remotes/other/main-in-other
954 ^refs/tags/matching-tag
957 cur=^refs/ &&
958 __git_refs >"$actual"
959 ) &&
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 &&
973 HEAD
974 main
975 matching-branch
976 matching/branch
977 other/branch-in-other
978 other/main-in-other
979 other/matching/branch-in-other
980 matching-tag
981 matching/tag
984 cur=main &&
985 __git_refs >"$actual"
986 ) &&
987 test_cmp expected "$actual"
990 test_expect_success '__git_refs - only matching refs' '
991 cat >expected <<-EOF &&
992 matching-branch
993 matching/branch
994 matching-tag
995 matching/tag
998 cur=mat &&
999 __git_refs "" "" "" "$cur" >"$actual"
1000 ) &&
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"
1012 ) &&
1013 test_cmp expected "$actual"
1016 test_expect_success '__git_refs - only matching refs - remote on local file system' '
1017 cat >expected <<-EOF &&
1018 main-in-other
1019 matching/branch-in-other
1022 cur=ma &&
1023 __git_refs otherrepo "" "" "$cur" >"$actual"
1024 ) &&
1025 test_cmp expected "$actual"
1028 test_expect_success '__git_refs - only matching refs - configured remote' '
1029 cat >expected <<-EOF &&
1030 main-in-other
1031 matching/branch-in-other
1034 cur=ma &&
1035 __git_refs other "" "" "$cur" >"$actual"
1036 ) &&
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"
1048 ) &&
1049 test_cmp expected "$actual"
1052 test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
1053 cat >expected <<-EOF &&
1054 matching-branch
1055 matching/branch
1056 matching-tag
1057 matching/tag
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
1066 done &&
1068 cur=mat &&
1069 __git_refs "" 1 "" "$cur" >"$actual"
1070 ) &&
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"
1088 ) &&
1089 test_cmp expected "$actual"
1092 test_expect_success '__git_complete_refs - simple' '
1093 sed -e "s/Z$//" >expected <<-EOF &&
1094 HEAD Z
1095 main Z
1096 matching-branch Z
1097 other/branch-in-other Z
1098 other/main-in-other Z
1099 matching-tag Z
1102 cur= &&
1103 __git_complete_refs &&
1104 print_comp
1105 ) &&
1106 test_cmp expected out
1109 test_expect_success '__git_complete_refs - matching' '
1110 sed -e "s/Z$//" >expected <<-EOF &&
1111 matching-branch Z
1112 matching-tag Z
1115 cur=mat &&
1116 __git_complete_refs &&
1117 print_comp
1118 ) &&
1119 test_cmp expected out
1122 test_expect_success '__git_complete_refs - remote' '
1123 sed -e "s/Z$//" >expected <<-EOF &&
1124 HEAD Z
1125 branch-in-other Z
1126 main-in-other Z
1129 cur= &&
1130 __git_complete_refs --remote=other &&
1131 print_comp
1132 ) &&
1133 test_cmp expected out
1136 test_expect_success '__git_complete_refs - track' '
1137 sed -e "s/Z$//" >expected <<-EOF &&
1138 HEAD Z
1139 main Z
1140 matching-branch Z
1141 other/branch-in-other Z
1142 other/main-in-other Z
1143 matching-tag Z
1144 branch-in-other Z
1145 main-in-other Z
1148 cur= &&
1149 __git_complete_refs --track &&
1150 print_comp
1151 ) &&
1152 test_cmp expected out
1155 test_expect_success '__git_complete_refs - current word' '
1156 sed -e "s/Z$//" >expected <<-EOF &&
1157 matching-branch Z
1158 matching-tag Z
1161 cur="--option=mat" &&
1162 __git_complete_refs --cur="${cur#*=}" &&
1163 print_comp
1164 ) &&
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
1174 cur=v1.0..mat &&
1175 __git_complete_refs --pfx=v1.0.. --cur=mat &&
1176 print_comp
1177 ) &&
1178 test_cmp expected out
1181 test_expect_success '__git_complete_refs - suffix' '
1182 cat >expected <<-EOF &&
1183 HEAD.
1184 main.
1185 matching-branch.
1186 other/branch-in-other.
1187 other/main-in-other.
1188 matching-tag.
1191 cur= &&
1192 __git_complete_refs --sfx=. &&
1193 print_comp
1194 ) &&
1195 test_cmp expected out
1198 test_expect_success '__git_complete_fetch_refspecs - simple' '
1199 sed -e "s/Z$//" >expected <<-EOF &&
1200 HEAD:HEAD Z
1201 branch-in-other:branch-in-other Z
1202 main-in-other:main-in-other Z
1205 cur= &&
1206 __git_complete_fetch_refspecs other &&
1207 print_comp
1208 ) &&
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
1217 cur=br &&
1218 __git_complete_fetch_refspecs other "" br &&
1219 print_comp
1220 ) &&
1221 test_cmp expected out
1224 test_expect_success '__git_complete_fetch_refspecs - prefix' '
1225 sed -e "s/Z$//" >expected <<-EOF &&
1226 +HEAD:HEAD Z
1227 +branch-in-other:branch-in-other Z
1228 +main-in-other:main-in-other Z
1231 cur="+" &&
1232 __git_complete_fetch_refspecs other "+" "" &&
1233 print_comp
1234 ) &&
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
1245 cur=refs/ &&
1246 __git_complete_fetch_refspecs other "" refs/ &&
1247 print_comp
1248 ) &&
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
1259 cur=+refs/ &&
1260 __git_complete_fetch_refspecs other + refs/ &&
1261 print_comp
1262 ) &&
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 " &&
1270 grep other_wt out
1273 test_expect_success '__git_complete_worktree_paths - not a git repository' '
1275 cd non-repo &&
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
1291 branch-in-other Z
1292 main Z
1293 main-in-other Z
1294 matching-branch Z
1298 test_expect_success 'git bisect - when not bisecting, complete only replay and start subcommands' '
1299 test_completion "git bisect " <<-\EOF
1300 replay Z
1301 start Z
1305 test_expect_success 'git bisect - complete options to start subcommand' '
1306 test_completion "git bisect start --" <<-\EOF
1307 --term-new Z
1308 --term-bad Z
1309 --term-old Z
1310 --term-good Z
1311 --no-checkout Z
1312 --first-parent Z
1316 test_expect_success 'setup for git-bisect tests requiring a repo' '
1317 git init git-bisect &&
1319 cd git-bisect &&
1320 echo "initial contents" >file &&
1321 git add file &&
1322 git commit -am "Initial commit" &&
1323 git tag initial &&
1324 echo "new line" >>file &&
1325 git commit -am "First change" &&
1326 echo "another new line" >>file &&
1327 git commit -am "Second change" &&
1328 git tag final
1332 test_expect_success 'git bisect - start subcommand arguments before double-dash are completed as revs' '
1334 cd git-bisect &&
1335 test_completion "git bisect start " <<-\EOF
1336 HEAD Z
1337 final Z
1338 initial Z
1339 master Z
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' '
1348 cd git-bisect &&
1349 test_completion "git bisect start final initial -- " ""
1353 test_expect_success 'setup for git-bisect tests requiring ongoing bisection' '
1355 cd git-bisect &&
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' '
1362 cd git-bisect &&
1363 test_completion "git bisect " <<-\EOF
1364 start Z
1365 bad Z
1366 custom_new Z
1367 custom_old Z
1368 new Z
1369 good Z
1370 old Z
1371 terms Z
1372 skip Z
1373 reset Z
1374 visualize Z
1375 replay Z
1376 log Z
1377 run Z
1378 help Z
1383 test_expect_success 'git-bisect - options to terms subcommand are candidates' '
1385 cd git-bisect &&
1386 test_completion "git bisect terms --" <<-\EOF
1387 --term-bad Z
1388 --term-good Z
1389 --term-new Z
1390 --term-old Z
1395 test_expect_success 'git-bisect - git-log options to visualize subcommand are candidates' '
1397 cd git-bisect &&
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 &&
1402 --stat Z
1404 test_completion "git bisect visualize --summar" <<-\EOF
1405 --summary Z
1410 test_expect_success 'git-bisect - view subcommand is not a candidate' '
1412 cd git-bisect &&
1413 test_completion "git bisect vi" <<-\EOF
1414 visualize Z
1419 test_expect_success 'git-bisect - existing view subcommand is recognized and enables completion of git-log options' '
1421 cd git-bisect &&
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 &&
1426 --stat Z
1428 test_completion "git bisect view --summar" <<-\EOF
1429 --summary Z
1434 test_expect_success 'git checkout - completes refs and unique remote branches for DWIM' '
1435 test_completion "git checkout " <<-\EOF
1436 HEAD Z
1437 branch-in-other Z
1438 main Z
1439 main-in-other Z
1440 matching-branch Z
1441 matching-tag Z
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
1449 main Z
1450 matching-branch Z
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
1456 main Z
1457 matching-branch Z
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
1463 branch-in-other Z
1464 main Z
1465 main-in-other Z
1466 matching-branch Z
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
1472 branch-in-other Z
1473 main Z
1474 main-in-other Z
1475 matching-branch Z
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
1481 main Z
1482 matching-branch Z
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
1488 HEAD Z
1489 main Z
1490 matching-branch Z
1491 matching-tag Z
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
1499 HEAD Z
1500 branch-in-other Z
1501 main Z
1502 main-in-other Z
1503 matching-branch Z
1504 matching-tag Z
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
1512 HEAD Z
1513 main Z
1514 matching-branch Z
1515 matching-tag Z
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
1523 HEAD Z
1524 branch-in-other Z
1525 main Z
1526 main-in-other Z
1527 matching-branch Z
1528 matching-tag Z
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
1536 HEAD Z
1537 main Z
1538 matching-branch Z
1539 matching-tag Z
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
1548 HEAD Z
1549 main Z
1550 matching-branch Z
1551 matching-tag Z
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
1560 HEAD Z
1561 branch-in-other Z
1562 main Z
1563 main-in-other Z
1564 matching-branch Z
1565 matching-tag Z
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
1574 HEAD Z
1575 branch-in-other Z
1576 main Z
1577 main-in-other Z
1578 matching-branch Z
1579 matching-tag Z
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
1588 HEAD Z
1589 main Z
1590 matching-branch Z
1591 matching-tag Z
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
1599 HEAD Z
1600 main Z
1601 matching-branch Z
1602 matching-tag Z
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
1610 HEAD Z
1611 main Z
1612 matching-branch Z
1613 matching-tag Z
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 &&
1628 git add . &&
1629 git commit -am "Initial commit"
1633 test_expect_success 'sparse-checkout completes subcommands' '
1634 test_completion "git sparse-checkout " <<-\EOF
1635 list Z
1636 init Z
1637 set Z
1638 add Z
1639 reapply Z
1640 disable Z
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
1652 folder1/
1653 folder2/
1654 folder3/
1656 ) &&
1659 cd sparse-checkout &&
1660 test_completion "git sparse-checkout set folder1/" <<-\EOF
1661 folder1/0/
1663 ) &&
1666 cd sparse-checkout &&
1667 test_completion "git sparse-checkout set folder1/0/" <<-\EOF
1668 folder1/0/1/
1670 ) &&
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" &&
1689 git add . &&
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" &&
1699 git add . &&
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" &&
1712 git add . &&
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")" &&
1720 git add . &&
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" &&
1733 git add . &&
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" &&
1741 git add . &&
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
1757 /folder1/expected Z
1758 /folder1/out Z
1759 /folder1/out_sorted Z
1760 /folder2/0/t.txt Z
1761 /folder3/t.txt 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
1772 folder1/
1773 folder2/
1774 folder3/
1779 test_expect_success 'git switch - with -d, complete all references' '
1780 test_completion "git switch -d " <<-\EOF
1781 HEAD Z
1782 main Z
1783 matching-branch Z
1784 matching-tag Z
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
1792 HEAD Z
1793 main Z
1794 matching-branch Z
1795 matching-tag Z
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
1825 main Z
1826 matching-branch Z
1830 test_expect_success 'git checkout - with --no-track, complete only local references' '
1831 test_completion "git checkout --no-track " <<-\EOF
1832 HEAD Z
1833 main Z
1834 matching-branch Z
1835 matching-tag Z
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
1843 HEAD Z
1844 main Z
1845 matching-branch Z
1846 matching-tag Z
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
1854 HEAD Z
1855 main Z
1856 matching-branch Z
1857 matching-tag Z
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
1865 HEAD Z
1866 main Z
1867 matching-branch Z
1868 matching-tag Z
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
1876 HEAD Z
1877 main Z
1878 matching-branch Z
1879 matching-tag Z
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
1887 HEAD Z
1888 main Z
1889 matching-branch Z
1890 matching-tag Z
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
1898 HEAD Z
1899 main Z
1900 matching-branch Z
1901 matching-tag Z
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
1909 HEAD Z
1910 main Z
1911 matching-branch Z
1912 matching-tag Z
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
1920 HEAD Z
1921 main Z
1922 matching-branch Z
1923 matching-tag Z
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
1931 HEAD Z
1932 main Z
1933 matching-branch Z
1934 matching-tag Z
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
1942 HEAD Z
1943 main Z
1944 matching-branch Z
1945 matching-tag Z
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
1953 HEAD Z
1954 main Z
1955 matching-branch Z
1956 matching-tag Z
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
1964 HEAD Z
1965 main Z
1966 matching-branch Z
1967 matching-tag Z
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
1975 branch-in-other Z
1976 main Z
1977 main-in-other Z
1978 matching-branch Z
1982 test_expect_success 'git switch - for -C, complete local branches and unique remote branches' '
1983 test_completion "git switch -C " <<-\EOF
1984 branch-in-other Z
1985 main Z
1986 main-in-other Z
1987 matching-branch Z
1991 test_expect_success 'git switch - for -c with --no-guess, complete local branches only' '
1992 test_completion "git switch --no-guess -c " <<-\EOF
1993 main Z
1994 matching-branch Z
1998 test_expect_success 'git switch - for -C with --no-guess, complete local branches only' '
1999 test_completion "git switch --no-guess -C " <<-\EOF
2000 main Z
2001 matching-branch Z
2005 test_expect_success 'git switch - for -c with --no-track, complete local branches only' '
2006 test_completion "git switch --no-track -c " <<-\EOF
2007 main Z
2008 matching-branch Z
2012 test_expect_success 'git switch - for -C with --no-track, complete local branches only' '
2013 test_completion "git switch --no-track -C " <<-\EOF
2014 main Z
2015 matching-branch Z
2019 test_expect_success 'git checkout - for -b, complete local branches and unique remote branches' '
2020 test_completion "git checkout -b " <<-\EOF
2021 branch-in-other Z
2022 main Z
2023 main-in-other Z
2024 matching-branch Z
2028 test_expect_success 'git checkout - for -B, complete local branches and unique remote branches' '
2029 test_completion "git checkout -B " <<-\EOF
2030 branch-in-other Z
2031 main Z
2032 main-in-other Z
2033 matching-branch Z
2037 test_expect_success 'git checkout - for -b with --no-guess, complete local branches only' '
2038 test_completion "git checkout --no-guess -b " <<-\EOF
2039 main Z
2040 matching-branch Z
2044 test_expect_success 'git checkout - for -B with --no-guess, complete local branches only' '
2045 test_completion "git checkout --no-guess -B " <<-\EOF
2046 main Z
2047 matching-branch Z
2051 test_expect_success 'git checkout - for -b with --no-track, complete local branches only' '
2052 test_completion "git checkout --no-track -b " <<-\EOF
2053 main Z
2054 matching-branch Z
2058 test_expect_success 'git checkout - for -B with --no-track, complete local branches only' '
2059 test_completion "git checkout --no-track -B " <<-\EOF
2060 main Z
2061 matching-branch Z
2065 test_expect_success 'git switch - with --orphan completes local branch names and unique remote branch names' '
2066 test_completion "git switch --orphan " <<-\EOF
2067 branch-in-other Z
2068 main Z
2069 main-in-other Z
2070 matching-branch Z
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
2082 branch-in-other Z
2083 main Z
2084 main-in-other Z
2085 matching-branch Z
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
2091 HEAD Z
2092 main Z
2093 matching-branch Z
2094 matching-tag Z
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 &&
2102 echo B >a.file &&
2103 test_completion "git restore a." <<-\EOF
2104 a.file
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 &&
2128 print_comp
2129 ) &&
2130 test_cmp expected out
2133 test_expect_success 'setup for path completion tests' '
2134 mkdir simple-dir \
2135 "spaces in dir" \
2136 árvíztűrő &&
2137 touch simple-dir/simple-file \
2138 "spaces in dir/spaces in file" \
2139 "árvíztűrő/Сайн яваарай" &&
2140 if test_have_prereq !MINGW &&
2141 mkdir BS\\dir \
2142 '$'separators\034in\035dir'' &&
2143 touch BS\\dir/DQ\"file \
2144 '$'separators\034in\035dir/sep\036in\037file''
2145 then
2146 test_set_prereq FUNNIERNAMES
2147 else
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" \
2163 "spaces in dir" &&
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" \
2176 "spaces in dir" &&
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 \
2191 BS\\dir &&
2192 test_path_completion BS\\\\d \
2193 BS\\dir &&
2194 test_path_completion BS\\\\dir/DQ \
2195 BS\\dir/DQ\"file &&
2196 test_path_completion BS\\\\dir/DQ\\\"f \
2197 BS\\dir/DQ\"file
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[@]} &&
2236 __git_cmd_idx=1 &&
2237 __git_find_on_cmdline "add list remove" >actual
2238 ) &&
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[@]} &&
2247 __git_cmd_idx=1 &&
2248 __git_find_on_cmdline "add list remove" >actual
2249 ) &&
2250 test_cmp expect actual
2253 test_expect_success '__git_find_on_cmdline - no match' '
2255 words=(git command --opt branch) &&
2256 cword=${#words[@]} &&
2257 __git_cmd_idx=1 &&
2258 __git_find_on_cmdline "add list remove" >actual
2259 ) &&
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[@]} &&
2268 __git_cmd_idx=1 &&
2269 __git_find_on_cmdline --show-idx "add list remove" >actual
2270 ) &&
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[@]} &&
2279 __git_cmd_idx=1 &&
2280 __git_find_on_cmdline --show-idx "add list remove" >actual
2281 ) &&
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[@]} &&
2289 __git_cmd_idx=1 &&
2290 __git_find_on_cmdline --show-idx "add list remove" >actual
2291 ) &&
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[@]} &&
2300 __git_cmd_idx=3 &&
2301 __git_find_on_cmdline --show-idx "add list remove" >actual
2302 ) &&
2303 test_cmp expect actual
2306 test_expect_success '__git_get_config_variables' '
2307 cat >expect <<-EOF &&
2308 name-1
2309 name-2
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 &&
2320 author
2321 hash
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 " &&
2331 # built-in
2332 grep -q "^add \$" out &&
2333 # script
2334 grep -q "^rebase \$" out &&
2335 # plumbing
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
2344 --paginate Z
2345 --no-pager Z
2346 --git-dir=
2347 --bare Z
2348 --version Z
2349 --exec-path Z
2350 --exec-path=
2351 --html-path Z
2352 --man-path Z
2353 --info-path Z
2354 --work-tree=
2355 --namespace=
2356 --no-replace-objects Z
2357 --help Z
2361 test_expect_success 'double dash "git checkout"' '
2362 test_completion "git checkout --" <<-\EOF
2363 --quiet Z
2364 --detach Z
2365 --track Z
2366 --orphan=Z
2367 --ours Z
2368 --theirs Z
2369 --merge Z
2370 --conflict=Z
2371 --patch Z
2372 --ignore-skip-worktree-bits Z
2373 --ignore-other-worktrees Z
2374 --recurse-submodules Z
2375 --progress Z
2376 --guess Z
2377 --no-guess Z
2378 --no-... Z
2379 --overlay 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 &&
2389 --exec-path Z
2390 --exec-path=
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 &&
2436 echo more >file2 &&
2437 git add file1 file2 &&
2438 git commit -m one &&
2439 git branch mybranch &&
2440 git tag mytag
2443 test_expect_success 'checkout completes ref names' '
2444 test_completion "git checkout m" <<-\EOF
2445 main Z
2446 mybranch Z
2447 mytag Z
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
2459 main Z
2460 mybranch Z
2461 mytag Z
2466 test_expect_success 'checkout completes pseudo refs' '
2467 test_completion "git checkout H" <<-\EOF
2468 HEAD Z
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
2476 HEAD Z
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
2483 branch-in-other Z
2487 test_expect_success 'show completes all refs' '
2488 test_completion "git show m" <<-\EOF
2489 main Z
2490 mybranch Z
2491 mytag Z
2495 test_expect_success '<ref>: completes paths' '
2496 test_completion "git show mytag:f" <<-\EOF
2497 file1Z
2498 file2Z
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
2507 name with spacesZ
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
2516 name with ${meta}Z
2517 name with spacesZ
2521 test_expect_success PERL 'send-email' '
2522 test_completion "git send-email --cov" <<-\EOF &&
2523 --cover-from-description=Z
2524 --cover-letter Z
2526 test_completion "git send-email --val" <<-\EOF &&
2527 --validate Z
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 &&
2545 touch new &&
2546 test_completion "git add " "new" &&
2548 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 &&
2561 touch untracked &&
2563 : TODO .gitignore should not be here &&
2564 test_completion "git rm " <<-\EOF &&
2565 .gitignore
2566 modified
2569 test_completion "git clean " "untracked" &&
2571 : TODO .gitignore should not be here &&
2572 test_completion "git mv " <<-\EOF &&
2573 .gitignore
2574 modified
2577 mkdir dir &&
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 &&
2586 .gitignore
2588 modified
2589 untracked
2590 untracked-dir
2593 test_completion "git commit " "modified" &&
2595 : TODO .gitignore should not be here &&
2596 test_completion "git ls-files " <<-\EOF &&
2597 .gitignore
2599 modified
2602 touch momified &&
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
2609 main Z
2610 mybranch Z
2611 mytag Z
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
2619 main Z
2620 mybranch Z
2621 mytag Z
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
2628 main Z
2629 mybranch Z
2630 mytag Z
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
2637 main Z
2638 mybranch Z
2639 mytag Z
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
2646 main Z
2647 mybranch Z
2648 mytag Z
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
2655 main Z
2656 mybranch Z
2657 mytag Z
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
2664 main Z
2665 mybranch Z
2666 mytag Z
2670 test_expect_success 'completion without explicit _git_xxx function' '
2671 test_completion "git version --" <<-\EOF
2672 --build-options Z
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" &&
2681 touch ~/tmp/file &&
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 &&
2688 git fetch other
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 &&
2695 main-in-other Z
2698 words=(git push '$flag' other ma) &&
2699 cword=${#words[@]} cur=${words[cword-1]} &&
2700 __git_cmd_idx=1 &&
2701 __git_complete_remote_or_refspec &&
2702 print_comp
2703 ) &&
2704 test_cmp expected out
2707 test_expect_failure "__git_complete_remote_or_refspec - push other $flag" '
2708 sed -e "s/Z$//" >expected <<-EOF &&
2709 main-in-other Z
2712 words=(git push other '$flag' ma) &&
2713 cword=${#words[@]} cur=${words[cword-1]} &&
2714 __git_cmd_idx=1 &&
2715 __git_complete_remote_or_refspec &&
2716 print_comp
2717 ) &&
2718 test_cmp expected out
2720 done
2722 test_expect_success 'git config - section' '
2723 test_completion "git config br" <<-\EOF
2724 branch.Z
2725 browser.Z
2729 test_expect_success 'git config - section include, includeIf' '
2730 test_completion "git config inclu" <<-\EOF
2731 include.Z
2732 includeIf.Z
2736 test_expect_success 'git config - variable name' '
2737 test_completion "git config log.d" <<-\EOF
2738 log.date Z
2739 log.decorate Z
2740 log.diffMerges Z
2744 test_expect_success 'git config - variable name include' '
2745 test_completion "git config include.p" <<-\EOF
2746 include.path Z
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
2758 submodule.active Z
2759 submodule.alternateErrorStrategy Z
2760 submodule.alternateLocation Z
2761 submodule.fetchJobs Z
2762 submodule.propagateBranches Z
2763 submodule.recurse Z
2764 submodule.sub.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
2770 submodule.sub.url Z
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
2781 false Z
2782 true Z
2786 test_expect_success 'git -c - section' '
2787 test_completion "git -c br" <<-\EOF
2788 branch.Z
2789 browser.Z
2793 test_expect_success 'git -c - variable name' '
2794 test_completion "git -c log.d" <<-\EOF
2795 log.date=Z
2796 log.decorate=Z
2797 log.diffMerges=Z
2801 test_expect_success 'git -c - value' '
2802 test_completion "git -c color.pager=" <<-\EOF
2803 false Z
2804 true Z
2808 test_expect_success 'git clone --config= - section' '
2809 test_completion "git clone --config=br" <<-\EOF
2810 branch.Z
2811 browser.Z
2815 test_expect_success 'git clone --config= - variable name' '
2816 test_completion "git clone --config=log.d" <<-\EOF
2817 log.date=Z
2818 log.decorate=Z
2819 log.diffMerges=Z
2823 test_expect_success 'git clone --config= - value' '
2824 test_completion "git clone --config=color.pager=" <<-\EOF
2825 false Z
2826 true Z
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 &&
2834 show Z
2835 shown Z
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
2890 --recursive 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 &&
2903 ! grep cat-file 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" &&
2921 grep gitk out &&
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" &&
2948 git init repo &&
2950 cd 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.
2958 test_commit A &&
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
2973 test_done