The seventh batch
[git.git] / t / t9902-completion.sh
blobcc6aa9f0cd36e3c39fa3ba93a450407fb89ce658
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 TEST_PASSES_SANITIZE_LEAK=true
20 . ./lib-bash.sh
22 complete ()
24 # do nothing
25 return 0
28 # Be careful when updating these lists:
30 # (1) The build tree may have build artifact from different branch, or
31 # the user's $PATH may have a random executable that may begin
32 # with "git-check" that are not part of the subcommands this build
33 # will ship, e.g. "check-ignore". The tests for completion for
34 # subcommand names tests how "check" is expanded; we limit the
35 # possible candidates to "checkout" and "check-attr" to make sure
36 # "check-attr", which is known by the filter function as a
37 # subcommand to be thrown out, while excluding other random files
38 # that happen to begin with "check" to avoid letting them get in
39 # the way.
41 # (2) A test makes sure that common subcommands are included in the
42 # completion for "git <TAB>", and a plumbing is excluded. "add",
43 # "rebase" and "ls-files" are listed for this.
45 GIT_TESTING_ALL_COMMAND_LIST='add checkout check-attr rebase ls-files'
46 GIT_TESTING_PORCELAIN_COMMAND_LIST='add checkout rebase'
48 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
50 # We don't need this function to actually join words or do anything special.
51 # Also, it's cleaner to avoid touching bash's internal completion variables.
52 # So let's override it with a minimal version for testing purposes.
53 _get_comp_words_by_ref ()
55 while [ $# -gt 0 ]; do
56 case "$1" in
57 cur)
58 cur=${_words[_cword]}
60 prev)
61 prev=${_words[_cword-1]}
63 words)
64 words=("${_words[@]}")
66 cword)
67 cword=$_cword
69 esac
70 shift
71 done
74 print_comp ()
76 local IFS=$'\n'
77 printf '%s\n' "${COMPREPLY[*]}" > out
80 run_completion ()
82 local -a COMPREPLY _words
83 local _cword
84 _words=( $1 )
85 test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
86 (( _cword = ${#_words[@]} - 1 ))
87 __git_wrap__git_main && print_comp
90 # Test high-level completion
91 # Arguments are:
92 # 1: typed text so far (cur)
93 # 2: expected completion
94 test_completion ()
96 if test $# -gt 1
97 then
98 printf '%s\n' "$2" >expected
99 else
100 sed -e 's/Z$//' |sort >expected
101 fi &&
102 run_completion "$1" >"$TRASH_DIRECTORY"/bash-completion-output 2>&1 &&
103 sort out >out_sorted &&
104 test_cmp expected out_sorted &&
105 test_must_be_empty "$TRASH_DIRECTORY"/bash-completion-output &&
106 rm "$TRASH_DIRECTORY"/bash-completion-output
109 # Test __gitcomp.
110 # The first argument is the typed text so far (cur); the rest are
111 # passed to __gitcomp. Expected output comes is read from the
112 # standard input, like test_completion().
113 test_gitcomp ()
115 local -a COMPREPLY &&
116 sed -e 's/Z$//' >expected &&
117 local cur="$1" &&
118 shift &&
119 __gitcomp "$@" &&
120 print_comp &&
121 test_cmp expected out
124 # Test __gitcomp_nl
125 # Arguments are:
126 # 1: current word (cur)
127 # -: the rest are passed to __gitcomp_nl
128 test_gitcomp_nl ()
130 local -a COMPREPLY &&
131 sed -e 's/Z$//' >expected &&
132 local cur="$1" &&
133 shift &&
134 __gitcomp_nl "$@" &&
135 print_comp &&
136 test_cmp expected out
139 invalid_variable_name='${foo.bar}'
141 actual="$TRASH_DIRECTORY/actual"
143 if test_have_prereq MINGW
144 then
145 ROOT="$(pwd -W)"
146 else
147 ROOT="$(pwd)"
150 test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
151 mkdir -p subdir/subsubdir &&
152 mkdir -p non-repo &&
153 git init -b main otherrepo
156 test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '
157 echo "$ROOT/otherrepo/.git" >expected &&
159 __git_dir="$ROOT/otherrepo/.git" &&
160 __git_find_repo_path &&
161 echo "$__git_repo_path" >"$actual"
162 ) &&
163 test_cmp expected "$actual"
166 test_expect_success '__git_find_repo_path - .git directory in cwd' '
167 echo ".git" >expected &&
169 __git_find_repo_path &&
170 echo "$__git_repo_path" >"$actual"
171 ) &&
172 test_cmp expected "$actual"
175 test_expect_success '__git_find_repo_path - .git directory in parent' '
176 echo "$ROOT/.git" >expected &&
178 cd subdir/subsubdir &&
179 __git_find_repo_path &&
180 echo "$__git_repo_path" >"$actual"
181 ) &&
182 test_cmp expected "$actual"
185 test_expect_success '__git_find_repo_path - cwd is a .git directory' '
186 echo "." >expected &&
188 cd .git &&
189 __git_find_repo_path &&
190 echo "$__git_repo_path" >"$actual"
191 ) &&
192 test_cmp expected "$actual"
195 test_expect_success '__git_find_repo_path - parent is a .git directory' '
196 echo "$ROOT/.git" >expected &&
198 cd .git/objects &&
199 __git_find_repo_path &&
200 echo "$__git_repo_path" >"$actual"
201 ) &&
202 test_cmp expected "$actual"
205 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
206 echo "$ROOT/otherrepo/.git" >expected &&
208 GIT_DIR="$ROOT/otherrepo/.git" &&
209 export GIT_DIR &&
210 __git_find_repo_path &&
211 echo "$__git_repo_path" >"$actual"
212 ) &&
213 test_cmp expected "$actual"
216 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
217 echo "$ROOT/otherrepo/.git" >expected &&
219 GIT_DIR="$ROOT/otherrepo/.git" &&
220 export GIT_DIR &&
221 cd subdir &&
222 __git_find_repo_path &&
223 echo "$__git_repo_path" >"$actual"
224 ) &&
225 test_cmp expected "$actual"
228 test_expect_success '__git_find_repo_path - from command line while "git -C"' '
229 echo "$ROOT/.git" >expected &&
231 __git_dir="$ROOT/.git" &&
232 __git_C_args=(-C otherrepo) &&
233 __git_find_repo_path &&
234 echo "$__git_repo_path" >"$actual"
235 ) &&
236 test_cmp expected "$actual"
239 test_expect_success '__git_find_repo_path - relative dir from command line and "git -C"' '
240 echo "$ROOT/otherrepo/.git" >expected &&
242 cd subdir &&
243 __git_dir="otherrepo/.git" &&
244 __git_C_args=(-C ..) &&
245 __git_find_repo_path &&
246 echo "$__git_repo_path" >"$actual"
247 ) &&
248 test_cmp expected "$actual"
251 test_expect_success '__git_find_repo_path - $GIT_DIR set while "git -C"' '
252 echo "$ROOT/.git" >expected &&
254 GIT_DIR="$ROOT/.git" &&
255 export GIT_DIR &&
256 __git_C_args=(-C otherrepo) &&
257 __git_find_repo_path &&
258 echo "$__git_repo_path" >"$actual"
259 ) &&
260 test_cmp expected "$actual"
263 test_expect_success '__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
264 echo "$ROOT/otherrepo/.git" >expected &&
266 cd subdir &&
267 GIT_DIR="otherrepo/.git" &&
268 export GIT_DIR &&
269 __git_C_args=(-C ..) &&
270 __git_find_repo_path &&
271 echo "$__git_repo_path" >"$actual"
272 ) &&
273 test_cmp expected "$actual"
276 test_expect_success '__git_find_repo_path - "git -C" while .git directory in cwd' '
277 echo "$ROOT/otherrepo/.git" >expected &&
279 __git_C_args=(-C otherrepo) &&
280 __git_find_repo_path &&
281 echo "$__git_repo_path" >"$actual"
282 ) &&
283 test_cmp expected "$actual"
286 test_expect_success '__git_find_repo_path - "git -C" while cwd is a .git directory' '
287 echo "$ROOT/otherrepo/.git" >expected &&
289 cd .git &&
290 __git_C_args=(-C .. -C otherrepo) &&
291 __git_find_repo_path &&
292 echo "$__git_repo_path" >"$actual"
293 ) &&
294 test_cmp expected "$actual"
297 test_expect_success '__git_find_repo_path - "git -C" while .git directory in parent' '
298 echo "$ROOT/otherrepo/.git" >expected &&
300 cd subdir &&
301 __git_C_args=(-C .. -C otherrepo) &&
302 __git_find_repo_path &&
303 echo "$__git_repo_path" >"$actual"
304 ) &&
305 test_cmp expected "$actual"
308 test_expect_success '__git_find_repo_path - non-existing path in "git -C"' '
310 __git_C_args=(-C non-existing) &&
311 test_must_fail __git_find_repo_path &&
312 printf "$__git_repo_path" >"$actual"
313 ) &&
314 test_must_be_empty "$actual"
317 test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '
319 __git_dir="non-existing" &&
320 test_must_fail __git_find_repo_path &&
321 printf "$__git_repo_path" >"$actual"
322 ) &&
323 test_must_be_empty "$actual"
326 test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
328 GIT_DIR="$ROOT/non-existing" &&
329 export GIT_DIR &&
330 test_must_fail __git_find_repo_path &&
331 printf "$__git_repo_path" >"$actual"
332 ) &&
333 test_must_be_empty "$actual"
336 test_expect_success '__git_find_repo_path - gitfile in cwd' '
337 echo "$ROOT/otherrepo/.git" >expected &&
338 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
339 test_when_finished "rm -f subdir/.git" &&
341 cd subdir &&
342 __git_find_repo_path &&
343 echo "$__git_repo_path" >"$actual"
344 ) &&
345 test_cmp expected "$actual"
348 test_expect_success '__git_find_repo_path - gitfile in parent' '
349 echo "$ROOT/otherrepo/.git" >expected &&
350 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
351 test_when_finished "rm -f subdir/.git" &&
353 cd subdir/subsubdir &&
354 __git_find_repo_path &&
355 echo "$__git_repo_path" >"$actual"
356 ) &&
357 test_cmp expected "$actual"
360 test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '
361 echo "$ROOT/otherrepo/.git" >expected &&
362 mkdir otherrepo/dir &&
363 test_when_finished "rm -rf otherrepo/dir" &&
364 ln -s otherrepo/dir link &&
365 test_when_finished "rm -f link" &&
367 cd link &&
368 __git_find_repo_path &&
369 echo "$__git_repo_path" >"$actual"
370 ) &&
371 test_cmp expected "$actual"
374 test_expect_success '__git_find_repo_path - not a git repository' '
376 cd non-repo &&
377 GIT_CEILING_DIRECTORIES="$ROOT" &&
378 export GIT_CEILING_DIRECTORIES &&
379 test_must_fail __git_find_repo_path &&
380 printf "$__git_repo_path" >"$actual"
381 ) &&
382 test_must_be_empty "$actual"
385 test_expect_success '__gitdir - finds repo' '
386 echo "$ROOT/.git" >expected &&
388 cd subdir/subsubdir &&
389 __gitdir >"$actual"
390 ) &&
391 test_cmp expected "$actual"
395 test_expect_success '__gitdir - returns error when cannot find repo' '
397 __git_dir="non-existing" &&
398 test_must_fail __gitdir >"$actual"
399 ) &&
400 test_must_be_empty "$actual"
403 test_expect_success '__gitdir - repo as argument' '
404 echo "otherrepo/.git" >expected &&
406 __gitdir "otherrepo" >"$actual"
407 ) &&
408 test_cmp expected "$actual"
411 test_expect_success '__gitdir - remote as argument' '
412 echo "remote" >expected &&
414 __gitdir "remote" >"$actual"
415 ) &&
416 test_cmp expected "$actual"
420 test_expect_success '__git_dequote - plain unquoted word' '
421 __git_dequote unquoted-word &&
422 test unquoted-word = "$dequoted_word"
425 # input: b\a\c\k\'\\\"s\l\a\s\h\es
426 # expected: back'\"slashes
427 test_expect_success '__git_dequote - backslash escaped' '
428 __git_dequote "b\a\c\k\\'\''\\\\\\\"s\l\a\s\h\es" &&
429 test "back'\''\\\"slashes" = "$dequoted_word"
432 # input: sin'gle\' '"quo'ted
433 # expected: single\ "quoted
434 test_expect_success '__git_dequote - single quoted' '
435 __git_dequote "'"sin'gle\\\\' '\\\"quo'ted"'" &&
436 test '\''single\ "quoted'\'' = "$dequoted_word"
439 # input: dou"ble\\" "\"\quot"ed
440 # expected: double\ "\quoted
441 test_expect_success '__git_dequote - double quoted' '
442 __git_dequote '\''dou"ble\\" "\"\quot"ed'\'' &&
443 test '\''double\ "\quoted'\'' = "$dequoted_word"
446 # input: 'open single quote
447 test_expect_success '__git_dequote - open single quote' '
448 __git_dequote "'\''open single quote" &&
449 test "open single quote" = "$dequoted_word"
452 # input: "open double quote
453 test_expect_success '__git_dequote - open double quote' '
454 __git_dequote "\"open double quote" &&
455 test "open double quote" = "$dequoted_word"
459 test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' '
460 sed -e "s/Z$//g" >expected <<-EOF &&
461 with-trailing-space Z
462 without-trailing-spaceZ
463 --option Z
464 --option=Z
465 $invalid_variable_name Z
468 cur=should_be_ignored &&
469 __gitcomp_direct "$(cat expected)" &&
470 print_comp
471 ) &&
472 test_cmp expected out
475 test_expect_success '__gitcomp - trailing space - options' '
476 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
477 --reset-author" <<-EOF
478 --reuse-message=Z
479 --reedit-message=Z
480 --reset-author Z
484 test_expect_success '__gitcomp - trailing space - config keys' '
485 test_gitcomp "br" "branch. branch.autosetupmerge
486 branch.autosetuprebase browser." <<-\EOF
487 branch.Z
488 branch.autosetupmerge Z
489 branch.autosetuprebase Z
490 browser.Z
494 test_expect_success '__gitcomp - option parameter' '
495 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
496 "" "re" <<-\EOF
497 recursive Z
498 resolve Z
502 test_expect_success '__gitcomp - prefix' '
503 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
504 "branch.maint." "me" <<-\EOF
505 branch.maint.merge Z
506 branch.maint.mergeoptions Z
510 test_expect_success '__gitcomp - suffix' '
511 test_gitcomp "branch.me" "master maint next seen" "branch." \
512 "ma" "." <<-\EOF
513 branch.master.Z
514 branch.maint.Z
518 test_expect_success '__gitcomp - ignore optional negative options' '
519 test_gitcomp "--" "--abc --def --no-one -- --no-two" <<-\EOF
520 --abc Z
521 --def Z
522 --no-one Z
523 --no-... Z
527 test_expect_success '__gitcomp - ignore/narrow optional negative options' '
528 test_gitcomp "--a" "--abc --abcdef --no-one -- --no-two" <<-\EOF
529 --abc Z
530 --abcdef Z
534 test_expect_success '__gitcomp - ignore/narrow optional negative options' '
535 test_gitcomp "--n" "--abc --def --no-one -- --no-two" <<-\EOF
536 --no-one Z
537 --no-... Z
541 test_expect_success '__gitcomp - expand all negative options' '
542 test_gitcomp "--no-" "--abc --def --no-one -- --no-two" <<-\EOF
543 --no-one Z
544 --no-two Z
548 test_expect_success '__gitcomp - expand/narrow all negative options' '
549 test_gitcomp "--no-o" "--abc --def --no-one -- --no-two" <<-\EOF
550 --no-one Z
554 test_expect_success '__gitcomp - equal skip' '
555 test_gitcomp "--option=" "--option=" <<-\EOF &&
558 test_gitcomp "option=" "option=" <<-\EOF
563 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
564 __gitcomp "$invalid_variable_name"
567 read -r -d "" refs <<-\EOF
568 main
569 maint
570 next
571 seen
574 test_expect_success '__gitcomp_nl - trailing space' '
575 test_gitcomp_nl "m" "$refs" <<-EOF
576 main Z
577 maint Z
581 test_expect_success '__gitcomp_nl - prefix' '
582 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
583 --fixup=main Z
584 --fixup=maint Z
588 test_expect_success '__gitcomp_nl - suffix' '
589 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
590 branch.main.Z
591 branch.maint.Z
595 test_expect_success '__gitcomp_nl - no suffix' '
596 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
597 mainZ
598 maintZ
602 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
603 __gitcomp_nl "$invalid_variable_name"
606 test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
607 cat >expect <<-EOF &&
608 remote_from_file_1
609 remote_from_file_2
610 remote_in_config_1
611 remote_in_config_2
613 test_when_finished "rm -rf .git/remotes" &&
614 mkdir -p .git/remotes &&
615 >.git/remotes/remote_from_file_1 &&
616 >.git/remotes/remote_from_file_2 &&
617 test_when_finished "git remote remove remote_in_config_1" &&
618 git remote add remote_in_config_1 git://remote_1 &&
619 test_when_finished "git remote remove remote_in_config_2" &&
620 git remote add remote_in_config_2 git://remote_2 &&
622 __git_remotes >actual
623 ) &&
624 test_cmp expect actual
627 test_expect_success '__git_is_configured_remote' '
628 test_when_finished "git remote remove remote_1" &&
629 git remote add remote_1 git://remote_1 &&
630 test_when_finished "git remote remove remote_2" &&
631 git remote add remote_2 git://remote_2 &&
633 __git_is_configured_remote remote_2 &&
634 test_must_fail __git_is_configured_remote non-existent
638 test_expect_success 'setup for ref completion' '
639 git commit --allow-empty -m initial &&
640 git branch -M main &&
641 git branch matching-branch &&
642 git tag matching-tag &&
644 cd otherrepo &&
645 git commit --allow-empty -m initial &&
646 git branch -m main main-in-other &&
647 git branch branch-in-other &&
648 git tag tag-in-other
649 ) &&
650 git remote add other "$ROOT/otherrepo/.git" &&
651 git fetch --no-tags other &&
652 rm -f .git/FETCH_HEAD &&
653 git init thirdrepo
656 test_expect_success '__git_refs - simple' '
657 cat >expected <<-EOF &&
658 HEAD
659 main
660 matching-branch
661 other/branch-in-other
662 other/main-in-other
663 matching-tag
666 cur= &&
667 __git_refs >"$actual"
668 ) &&
669 test_cmp expected "$actual"
672 test_expect_success '__git_refs - full refs' '
673 cat >expected <<-EOF &&
674 refs/heads/main
675 refs/heads/matching-branch
676 refs/remotes/other/branch-in-other
677 refs/remotes/other/main-in-other
678 refs/tags/matching-tag
681 cur=refs/heads/ &&
682 __git_refs >"$actual"
683 ) &&
684 test_cmp expected "$actual"
687 test_expect_success '__git_refs - repo given on the command line' '
688 cat >expected <<-EOF &&
689 HEAD
690 branch-in-other
691 main-in-other
692 tag-in-other
695 __git_dir="$ROOT/otherrepo/.git" &&
696 cur= &&
697 __git_refs >"$actual"
698 ) &&
699 test_cmp expected "$actual"
702 test_expect_success '__git_refs - remote on local file system' '
703 cat >expected <<-EOF &&
704 HEAD
705 branch-in-other
706 main-in-other
707 tag-in-other
710 cur= &&
711 __git_refs otherrepo >"$actual"
712 ) &&
713 test_cmp expected "$actual"
716 test_expect_success '__git_refs - remote on local file system - full refs' '
717 cat >expected <<-EOF &&
718 refs/heads/branch-in-other
719 refs/heads/main-in-other
720 refs/tags/tag-in-other
723 cur=refs/ &&
724 __git_refs otherrepo >"$actual"
725 ) &&
726 test_cmp expected "$actual"
729 test_expect_success '__git_refs - configured remote' '
730 cat >expected <<-EOF &&
731 HEAD
732 branch-in-other
733 main-in-other
736 cur= &&
737 __git_refs other >"$actual"
738 ) &&
739 test_cmp expected "$actual"
742 test_expect_success '__git_refs - configured remote - full refs' '
743 cat >expected <<-EOF &&
744 HEAD
745 refs/heads/branch-in-other
746 refs/heads/main-in-other
747 refs/tags/tag-in-other
750 cur=refs/ &&
751 __git_refs other >"$actual"
752 ) &&
753 test_cmp expected "$actual"
756 test_expect_success '__git_refs - configured remote - repo given on the command line' '
757 cat >expected <<-EOF &&
758 HEAD
759 branch-in-other
760 main-in-other
763 cd thirdrepo &&
764 __git_dir="$ROOT/.git" &&
765 cur= &&
766 __git_refs other >"$actual"
767 ) &&
768 test_cmp expected "$actual"
771 test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
772 cat >expected <<-EOF &&
773 HEAD
774 refs/heads/branch-in-other
775 refs/heads/main-in-other
776 refs/tags/tag-in-other
779 cd thirdrepo &&
780 __git_dir="$ROOT/.git" &&
781 cur=refs/ &&
782 __git_refs other >"$actual"
783 ) &&
784 test_cmp expected "$actual"
787 test_expect_success '__git_refs - configured remote - remote name matches a directory' '
788 cat >expected <<-EOF &&
789 HEAD
790 branch-in-other
791 main-in-other
793 mkdir other &&
794 test_when_finished "rm -rf other" &&
796 cur= &&
797 __git_refs other >"$actual"
798 ) &&
799 test_cmp expected "$actual"
802 test_expect_success '__git_refs - URL remote' '
803 cat >expected <<-EOF &&
804 HEAD
805 branch-in-other
806 main-in-other
807 tag-in-other
810 cur= &&
811 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
812 ) &&
813 test_cmp expected "$actual"
816 test_expect_success '__git_refs - URL remote - full refs' '
817 cat >expected <<-EOF &&
818 HEAD
819 refs/heads/branch-in-other
820 refs/heads/main-in-other
821 refs/tags/tag-in-other
824 cur=refs/ &&
825 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
826 ) &&
827 test_cmp expected "$actual"
830 test_expect_success '__git_refs - non-existing remote' '
832 cur= &&
833 __git_refs non-existing >"$actual"
834 ) &&
835 test_must_be_empty "$actual"
838 test_expect_success '__git_refs - non-existing remote - full refs' '
840 cur=refs/ &&
841 __git_refs non-existing >"$actual"
842 ) &&
843 test_must_be_empty "$actual"
846 test_expect_success '__git_refs - non-existing URL remote' '
848 cur= &&
849 __git_refs "file://$ROOT/non-existing" >"$actual"
850 ) &&
851 test_must_be_empty "$actual"
854 test_expect_success '__git_refs - non-existing URL remote - full refs' '
856 cur=refs/ &&
857 __git_refs "file://$ROOT/non-existing" >"$actual"
858 ) &&
859 test_must_be_empty "$actual"
862 test_expect_success '__git_refs - not in a git repository' '
864 GIT_CEILING_DIRECTORIES="$ROOT" &&
865 export GIT_CEILING_DIRECTORIES &&
866 cd subdir &&
867 cur= &&
868 __git_refs >"$actual"
869 ) &&
870 test_must_be_empty "$actual"
873 test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
874 cat >expected <<-EOF &&
875 HEAD
876 main
877 matching-branch
878 other/ambiguous
879 other/branch-in-other
880 other/main-in-other
881 remote/ambiguous
882 remote/branch-in-remote
883 matching-tag
884 branch-in-other
885 branch-in-remote
886 main-in-other
888 for remote_ref in refs/remotes/other/ambiguous \
889 refs/remotes/remote/ambiguous \
890 refs/remotes/remote/branch-in-remote
892 git update-ref $remote_ref main &&
893 test_when_finished "git update-ref -d $remote_ref" || return 1
894 done &&
896 cur= &&
897 __git_refs "" 1 >"$actual"
898 ) &&
899 test_cmp expected "$actual"
902 test_expect_success '__git_refs - after --opt=' '
903 cat >expected <<-EOF &&
904 HEAD
905 main
906 matching-branch
907 other/branch-in-other
908 other/main-in-other
909 matching-tag
912 cur="--opt=" &&
913 __git_refs "" "" "" "" >"$actual"
914 ) &&
915 test_cmp expected "$actual"
918 test_expect_success '__git_refs - after --opt= - full refs' '
919 cat >expected <<-EOF &&
920 refs/heads/main
921 refs/heads/matching-branch
922 refs/remotes/other/branch-in-other
923 refs/remotes/other/main-in-other
924 refs/tags/matching-tag
927 cur="--opt=refs/" &&
928 __git_refs "" "" "" refs/ >"$actual"
929 ) &&
930 test_cmp expected "$actual"
933 test_expect_success '__git refs - excluding refs' '
934 cat >expected <<-EOF &&
935 ^HEAD
936 ^main
937 ^matching-branch
938 ^other/branch-in-other
939 ^other/main-in-other
940 ^matching-tag
943 cur=^ &&
944 __git_refs >"$actual"
945 ) &&
946 test_cmp expected "$actual"
949 test_expect_success '__git refs - excluding full refs' '
950 cat >expected <<-EOF &&
951 ^refs/heads/main
952 ^refs/heads/matching-branch
953 ^refs/remotes/other/branch-in-other
954 ^refs/remotes/other/main-in-other
955 ^refs/tags/matching-tag
958 cur=^refs/ &&
959 __git_refs >"$actual"
960 ) &&
961 test_cmp expected "$actual"
964 test_expect_success 'setup for filtering matching refs' '
965 git branch matching/branch &&
966 git tag matching/tag &&
967 git -C otherrepo branch matching/branch-in-other &&
968 git fetch --no-tags other &&
969 rm -f .git/FETCH_HEAD
972 test_expect_success '__git_refs - do not filter refs unless told so' '
973 cat >expected <<-EOF &&
974 HEAD
975 main
976 matching-branch
977 matching/branch
978 other/branch-in-other
979 other/main-in-other
980 other/matching/branch-in-other
981 matching-tag
982 matching/tag
985 cur=main &&
986 __git_refs >"$actual"
987 ) &&
988 test_cmp expected "$actual"
991 test_expect_success '__git_refs - only matching refs' '
992 cat >expected <<-EOF &&
993 matching-branch
994 matching/branch
995 matching-tag
996 matching/tag
999 cur=mat &&
1000 __git_refs "" "" "" "$cur" >"$actual"
1001 ) &&
1002 test_cmp expected "$actual"
1005 test_expect_success '__git_refs - only matching refs - full refs' '
1006 cat >expected <<-EOF &&
1007 refs/heads/matching-branch
1008 refs/heads/matching/branch
1011 cur=refs/heads/mat &&
1012 __git_refs "" "" "" "$cur" >"$actual"
1013 ) &&
1014 test_cmp expected "$actual"
1017 test_expect_success '__git_refs - only matching refs - remote on local file system' '
1018 cat >expected <<-EOF &&
1019 main-in-other
1020 matching/branch-in-other
1023 cur=ma &&
1024 __git_refs otherrepo "" "" "$cur" >"$actual"
1025 ) &&
1026 test_cmp expected "$actual"
1029 test_expect_success '__git_refs - only matching refs - configured remote' '
1030 cat >expected <<-EOF &&
1031 main-in-other
1032 matching/branch-in-other
1035 cur=ma &&
1036 __git_refs other "" "" "$cur" >"$actual"
1037 ) &&
1038 test_cmp expected "$actual"
1041 test_expect_success '__git_refs - only matching refs - remote - full refs' '
1042 cat >expected <<-EOF &&
1043 refs/heads/main-in-other
1044 refs/heads/matching/branch-in-other
1047 cur=refs/heads/ma &&
1048 __git_refs other "" "" "$cur" >"$actual"
1049 ) &&
1050 test_cmp expected "$actual"
1053 test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
1054 cat >expected <<-EOF &&
1055 matching-branch
1056 matching/branch
1057 matching-tag
1058 matching/tag
1059 matching/branch-in-other
1061 for remote_ref in refs/remotes/other/ambiguous \
1062 refs/remotes/remote/ambiguous \
1063 refs/remotes/remote/branch-in-remote
1065 git update-ref $remote_ref main &&
1066 test_when_finished "git update-ref -d $remote_ref" || return 1
1067 done &&
1069 cur=mat &&
1070 __git_refs "" 1 "" "$cur" >"$actual"
1071 ) &&
1072 test_cmp expected "$actual"
1075 test_expect_success 'teardown after filtering matching refs' '
1076 git branch -d matching/branch &&
1077 git tag -d matching/tag &&
1078 git update-ref -d refs/remotes/other/matching/branch-in-other &&
1079 git -C otherrepo branch -D matching/branch-in-other
1082 test_expect_success '__git_refs - for-each-ref format specifiers in prefix' '
1083 cat >expected <<-EOF &&
1084 evil-%%-%42-%(refname)..main
1087 cur="evil-%%-%42-%(refname)..mai" &&
1088 __git_refs "" "" "evil-%%-%42-%(refname).." mai >"$actual"
1089 ) &&
1090 test_cmp expected "$actual"
1093 test_expect_success '__git_complete_refs - simple' '
1094 sed -e "s/Z$//" >expected <<-EOF &&
1095 HEAD Z
1096 main Z
1097 matching-branch Z
1098 other/branch-in-other Z
1099 other/main-in-other Z
1100 matching-tag Z
1103 cur= &&
1104 __git_complete_refs &&
1105 print_comp
1106 ) &&
1107 test_cmp expected out
1110 test_expect_success '__git_complete_refs - matching' '
1111 sed -e "s/Z$//" >expected <<-EOF &&
1112 matching-branch Z
1113 matching-tag Z
1116 cur=mat &&
1117 __git_complete_refs &&
1118 print_comp
1119 ) &&
1120 test_cmp expected out
1123 test_expect_success '__git_complete_refs - remote' '
1124 sed -e "s/Z$//" >expected <<-EOF &&
1125 HEAD Z
1126 branch-in-other Z
1127 main-in-other Z
1130 cur= &&
1131 __git_complete_refs --remote=other &&
1132 print_comp
1133 ) &&
1134 test_cmp expected out
1137 test_expect_success '__git_complete_refs - track' '
1138 sed -e "s/Z$//" >expected <<-EOF &&
1139 HEAD Z
1140 main Z
1141 matching-branch Z
1142 other/branch-in-other Z
1143 other/main-in-other Z
1144 matching-tag Z
1145 branch-in-other Z
1146 main-in-other Z
1149 cur= &&
1150 __git_complete_refs --track &&
1151 print_comp
1152 ) &&
1153 test_cmp expected out
1156 test_expect_success '__git_complete_refs - current word' '
1157 sed -e "s/Z$//" >expected <<-EOF &&
1158 matching-branch Z
1159 matching-tag Z
1162 cur="--option=mat" &&
1163 __git_complete_refs --cur="${cur#*=}" &&
1164 print_comp
1165 ) &&
1166 test_cmp expected out
1169 test_expect_success '__git_complete_refs - prefix' '
1170 sed -e "s/Z$//" >expected <<-EOF &&
1171 v1.0..matching-branch Z
1172 v1.0..matching-tag Z
1175 cur=v1.0..mat &&
1176 __git_complete_refs --pfx=v1.0.. --cur=mat &&
1177 print_comp
1178 ) &&
1179 test_cmp expected out
1182 test_expect_success '__git_complete_refs - suffix' '
1183 cat >expected <<-EOF &&
1184 HEAD.
1185 main.
1186 matching-branch.
1187 other/branch-in-other.
1188 other/main-in-other.
1189 matching-tag.
1192 cur= &&
1193 __git_complete_refs --sfx=. &&
1194 print_comp
1195 ) &&
1196 test_cmp expected out
1199 test_expect_success '__git_complete_fetch_refspecs - simple' '
1200 sed -e "s/Z$//" >expected <<-EOF &&
1201 HEAD:HEAD Z
1202 branch-in-other:branch-in-other Z
1203 main-in-other:main-in-other Z
1206 cur= &&
1207 __git_complete_fetch_refspecs other &&
1208 print_comp
1209 ) &&
1210 test_cmp expected out
1213 test_expect_success '__git_complete_fetch_refspecs - matching' '
1214 sed -e "s/Z$//" >expected <<-EOF &&
1215 branch-in-other:branch-in-other Z
1218 cur=br &&
1219 __git_complete_fetch_refspecs other "" br &&
1220 print_comp
1221 ) &&
1222 test_cmp expected out
1225 test_expect_success '__git_complete_fetch_refspecs - prefix' '
1226 sed -e "s/Z$//" >expected <<-EOF &&
1227 +HEAD:HEAD Z
1228 +branch-in-other:branch-in-other Z
1229 +main-in-other:main-in-other Z
1232 cur="+" &&
1233 __git_complete_fetch_refspecs other "+" "" &&
1234 print_comp
1235 ) &&
1236 test_cmp expected out
1239 test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
1240 sed -e "s/Z$//" >expected <<-EOF &&
1241 refs/heads/branch-in-other:refs/heads/branch-in-other Z
1242 refs/heads/main-in-other:refs/heads/main-in-other Z
1243 refs/tags/tag-in-other:refs/tags/tag-in-other Z
1246 cur=refs/ &&
1247 __git_complete_fetch_refspecs other "" refs/ &&
1248 print_comp
1249 ) &&
1250 test_cmp expected out
1253 test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
1254 sed -e "s/Z$//" >expected <<-EOF &&
1255 +refs/heads/branch-in-other:refs/heads/branch-in-other Z
1256 +refs/heads/main-in-other:refs/heads/main-in-other Z
1257 +refs/tags/tag-in-other:refs/tags/tag-in-other Z
1260 cur=+refs/ &&
1261 __git_complete_fetch_refspecs other + refs/ &&
1262 print_comp
1263 ) &&
1264 test_cmp expected out
1267 test_expect_success '__git_complete_worktree_paths' '
1268 test_when_finished "git worktree remove other_wt" &&
1269 git worktree add --orphan other_wt &&
1270 run_completion "git worktree remove " &&
1271 grep other_wt out
1274 test_expect_success '__git_complete_worktree_paths - not a git repository' '
1276 cd non-repo &&
1277 GIT_CEILING_DIRECTORIES="$ROOT" &&
1278 export GIT_CEILING_DIRECTORIES &&
1279 test_completion "git worktree remove " ""
1283 test_expect_success '__git_complete_worktree_paths with -C' '
1284 test_when_finished "git -C otherrepo worktree remove otherrepo_wt" &&
1285 git -C otherrepo worktree add --orphan otherrepo_wt &&
1286 run_completion "git -C otherrepo worktree remove " &&
1287 grep otherrepo_wt out
1290 test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
1291 test_completion "git switch " <<-\EOF
1292 branch-in-other Z
1293 main Z
1294 main-in-other Z
1295 matching-branch Z
1299 test_expect_success 'git bisect - when not bisecting, complete only replay and start subcommands' '
1300 test_completion "git bisect " <<-\EOF
1301 replay Z
1302 start Z
1306 test_expect_success 'git bisect - complete options to start subcommand' '
1307 test_completion "git bisect start --" <<-\EOF
1308 --term-new Z
1309 --term-bad Z
1310 --term-old Z
1311 --term-good Z
1312 --no-checkout Z
1313 --first-parent Z
1317 test_expect_success 'setup for git-bisect tests requiring a repo' '
1318 git init git-bisect &&
1320 cd git-bisect &&
1321 echo "initial contents" >file &&
1322 git add file &&
1323 git commit -am "Initial commit" &&
1324 git tag initial &&
1325 echo "new line" >>file &&
1326 git commit -am "First change" &&
1327 echo "another new line" >>file &&
1328 git commit -am "Second change" &&
1329 git tag final
1333 test_expect_success 'git bisect - start subcommand arguments before double-dash are completed as revs' '
1335 cd git-bisect &&
1336 test_completion "git bisect start " <<-\EOF
1337 HEAD Z
1338 final Z
1339 initial Z
1340 master Z
1345 # Note that these arguments are <pathspec>s, which in practice the fallback
1346 # completion (not the git completion) later ends up completing as paths.
1347 test_expect_success 'git bisect - start subcommand arguments after double-dash are not completed' '
1349 cd git-bisect &&
1350 test_completion "git bisect start final initial -- " ""
1354 test_expect_success 'setup for git-bisect tests requiring ongoing bisection' '
1356 cd git-bisect &&
1357 git bisect start --term-new=custom_new --term-old=custom_old final initial
1361 test_expect_success 'git-bisect - when bisecting all subcommands are candidates' '
1363 cd git-bisect &&
1364 test_completion "git bisect " <<-\EOF
1365 start Z
1366 bad Z
1367 custom_new Z
1368 custom_old Z
1369 new Z
1370 good Z
1371 old Z
1372 terms Z
1373 skip Z
1374 reset Z
1375 visualize Z
1376 replay Z
1377 log Z
1378 run Z
1379 help Z
1384 test_expect_success 'git-bisect - options to terms subcommand are candidates' '
1386 cd git-bisect &&
1387 test_completion "git bisect terms --" <<-\EOF
1388 --term-bad Z
1389 --term-good Z
1390 --term-new Z
1391 --term-old Z
1396 test_expect_success 'git-bisect - git-log options to visualize subcommand are candidates' '
1398 cd git-bisect &&
1399 # The completion used for git-log and here does not complete
1400 # every git-log option, so rather than hope to stay in sync
1401 # with exactly what it does we will just spot-test here.
1402 test_completion "git bisect visualize --sta" <<-\EOF &&
1403 --stat Z
1405 test_completion "git bisect visualize --summar" <<-\EOF
1406 --summary Z
1411 test_expect_success 'git-bisect - view subcommand is not a candidate' '
1413 cd git-bisect &&
1414 test_completion "git bisect vi" <<-\EOF
1415 visualize Z
1420 test_expect_success 'git-bisect - existing view subcommand is recognized and enables completion of git-log options' '
1422 cd git-bisect &&
1423 # The completion used for git-log and here does not complete
1424 # every git-log option, so rather than hope to stay in sync
1425 # with exactly what it does we will just spot-test here.
1426 test_completion "git bisect view --sta" <<-\EOF &&
1427 --stat Z
1429 test_completion "git bisect view --summar" <<-\EOF
1430 --summary Z
1435 test_expect_success 'git checkout - completes refs and unique remote branches for DWIM' '
1436 test_completion "git checkout " <<-\EOF
1437 HEAD Z
1438 branch-in-other Z
1439 main Z
1440 main-in-other Z
1441 matching-branch Z
1442 matching-tag Z
1443 other/branch-in-other Z
1444 other/main-in-other Z
1448 test_expect_success 'git switch - with --no-guess, complete only local branches' '
1449 test_completion "git switch --no-guess " <<-\EOF
1450 main Z
1451 matching-branch Z
1455 test_expect_success 'git switch - with GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete only local branches' '
1456 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch " <<-\EOF
1457 main Z
1458 matching-branch Z
1462 test_expect_success 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete local branches and unique remote names for DWIM logic' '
1463 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch --guess " <<-\EOF
1464 branch-in-other Z
1465 main Z
1466 main-in-other Z
1467 matching-branch Z
1471 test_expect_success 'git switch - a later --guess overrides previous --no-guess, complete local and remote unique branches for DWIM' '
1472 test_completion "git switch --no-guess --guess " <<-\EOF
1473 branch-in-other Z
1474 main Z
1475 main-in-other Z
1476 matching-branch Z
1480 test_expect_success 'git switch - a later --no-guess overrides previous --guess, complete only local branches' '
1481 test_completion "git switch --guess --no-guess " <<-\EOF
1482 main Z
1483 matching-branch Z
1487 test_expect_success 'git checkout - with GIT_COMPLETION_NO_GUESS=1 only completes refs' '
1488 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout " <<-\EOF
1489 HEAD Z
1490 main Z
1491 matching-branch Z
1492 matching-tag Z
1493 other/branch-in-other Z
1494 other/main-in-other Z
1498 test_expect_success 'git checkout - --guess overrides GIT_COMPLETION_NO_GUESS=1, complete refs and unique remote branches for DWIM' '
1499 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout --guess " <<-\EOF
1500 HEAD Z
1501 branch-in-other Z
1502 main Z
1503 main-in-other Z
1504 matching-branch Z
1505 matching-tag Z
1506 other/branch-in-other Z
1507 other/main-in-other Z
1511 test_expect_success 'git checkout - with --no-guess, only completes refs' '
1512 test_completion "git checkout --no-guess " <<-\EOF
1513 HEAD Z
1514 main Z
1515 matching-branch Z
1516 matching-tag Z
1517 other/branch-in-other Z
1518 other/main-in-other Z
1522 test_expect_success 'git checkout - a later --guess overrides previous --no-guess, complete refs and unique remote branches for DWIM' '
1523 test_completion "git checkout --no-guess --guess " <<-\EOF
1524 HEAD Z
1525 branch-in-other Z
1526 main Z
1527 main-in-other Z
1528 matching-branch Z
1529 matching-tag Z
1530 other/branch-in-other Z
1531 other/main-in-other Z
1535 test_expect_success 'git checkout - a later --no-guess overrides previous --guess, complete only refs' '
1536 test_completion "git checkout --guess --no-guess " <<-\EOF
1537 HEAD Z
1538 main Z
1539 matching-branch Z
1540 matching-tag Z
1541 other/branch-in-other Z
1542 other/main-in-other Z
1546 test_expect_success 'git checkout - with checkout.guess = false, only completes refs' '
1547 test_config checkout.guess false &&
1548 test_completion "git checkout " <<-\EOF
1549 HEAD Z
1550 main Z
1551 matching-branch Z
1552 matching-tag Z
1553 other/branch-in-other Z
1554 other/main-in-other Z
1558 test_expect_success 'git checkout - with checkout.guess = true, completes refs and unique remote branches for DWIM' '
1559 test_config checkout.guess true &&
1560 test_completion "git checkout " <<-\EOF
1561 HEAD Z
1562 branch-in-other Z
1563 main Z
1564 main-in-other Z
1565 matching-branch Z
1566 matching-tag Z
1567 other/branch-in-other Z
1568 other/main-in-other Z
1572 test_expect_success 'git checkout - a later --guess overrides previous checkout.guess = false, complete refs and unique remote branches for DWIM' '
1573 test_config checkout.guess false &&
1574 test_completion "git checkout --guess " <<-\EOF
1575 HEAD Z
1576 branch-in-other Z
1577 main Z
1578 main-in-other Z
1579 matching-branch Z
1580 matching-tag Z
1581 other/branch-in-other Z
1582 other/main-in-other Z
1586 test_expect_success 'git checkout - a later --no-guess overrides previous checkout.guess = true, complete only refs' '
1587 test_config checkout.guess true &&
1588 test_completion "git checkout --no-guess " <<-\EOF
1589 HEAD Z
1590 main Z
1591 matching-branch Z
1592 matching-tag Z
1593 other/branch-in-other Z
1594 other/main-in-other Z
1598 test_expect_success 'git switch - with --detach, complete all references' '
1599 test_completion "git switch --detach " <<-\EOF
1600 HEAD Z
1601 main Z
1602 matching-branch Z
1603 matching-tag Z
1604 other/branch-in-other Z
1605 other/main-in-other Z
1609 test_expect_success 'git checkout - with --detach, complete only references' '
1610 test_completion "git checkout --detach " <<-\EOF
1611 HEAD Z
1612 main Z
1613 matching-branch Z
1614 matching-tag Z
1615 other/branch-in-other Z
1616 other/main-in-other Z
1620 test_expect_success 'setup sparse-checkout tests' '
1621 # set up sparse-checkout repo
1622 git init sparse-checkout &&
1624 cd sparse-checkout &&
1625 mkdir -p folder1/0/1 folder2/0 folder3 &&
1626 touch folder1/0/1/t.txt &&
1627 touch folder2/0/t.txt &&
1628 touch folder3/t.txt &&
1629 git add . &&
1630 git commit -am "Initial commit"
1634 test_expect_success 'sparse-checkout completes subcommands' '
1635 test_completion "git sparse-checkout " <<-\EOF
1636 list Z
1637 init Z
1638 set Z
1639 add Z
1640 reapply Z
1641 disable Z
1645 test_expect_success 'cone mode sparse-checkout completes directory names' '
1646 # initialize sparse-checkout definitions
1647 git -C sparse-checkout sparse-checkout set --cone folder1/0 folder3 &&
1649 # test tab completion
1651 cd sparse-checkout &&
1652 test_completion "git sparse-checkout set f" <<-\EOF
1653 folder1/
1654 folder2/
1655 folder3/
1657 ) &&
1660 cd sparse-checkout &&
1661 test_completion "git sparse-checkout set folder1/" <<-\EOF
1662 folder1/0/
1664 ) &&
1667 cd sparse-checkout &&
1668 test_completion "git sparse-checkout set folder1/0/" <<-\EOF
1669 folder1/0/1/
1671 ) &&
1674 cd sparse-checkout/folder1 &&
1675 test_completion "git sparse-checkout add 0" <<-\EOF
1681 test_expect_success 'cone mode sparse-checkout completes directory names with spaces and accents' '
1682 # reset sparse-checkout
1683 git -C sparse-checkout sparse-checkout disable &&
1685 cd sparse-checkout &&
1686 mkdir "directory with spaces" &&
1687 mkdir "directory-with-áccent" &&
1688 >"directory with spaces/randomfile" &&
1689 >"directory-with-áccent/randomfile" &&
1690 git add . &&
1691 git commit -m "Add directory with spaces and directory with accent" &&
1692 git sparse-checkout set --cone "directory with spaces" \
1693 "directory-with-áccent" &&
1694 test_completion "git sparse-checkout add dir" <<-\EOF &&
1695 directory with spaces/
1696 directory-with-áccent/
1698 rm -rf "directory with spaces" &&
1699 rm -rf "directory-with-áccent" &&
1700 git add . &&
1701 git commit -m "Remove directory with spaces and directory with accent"
1705 # use FUNNYNAMES to avoid running on Windows, which doesn't permit tabs in paths
1706 test_expect_success FUNNYNAMES 'cone mode sparse-checkout completes directory names with tabs' '
1707 # reset sparse-checkout
1708 git -C sparse-checkout sparse-checkout disable &&
1710 cd sparse-checkout &&
1711 mkdir "$(printf "directory\twith\ttabs")" &&
1712 >"$(printf "directory\twith\ttabs")/randomfile" &&
1713 git add . &&
1714 git commit -m "Add directory with tabs" &&
1715 git sparse-checkout set --cone \
1716 "$(printf "directory\twith\ttabs")" &&
1717 test_completion "git sparse-checkout add dir" <<-\EOF &&
1718 directory with tabs/
1720 rm -rf "$(printf "directory\twith\ttabs")" &&
1721 git add . &&
1722 git commit -m "Remove directory with tabs"
1726 # use FUNNYNAMES to avoid running on Windows, and !CYGWIN for Cygwin, as neither permit backslashes in paths
1727 test_expect_success FUNNYNAMES,!CYGWIN 'cone mode sparse-checkout completes directory names with backslashes' '
1728 # reset sparse-checkout
1729 git -C sparse-checkout sparse-checkout disable &&
1731 cd sparse-checkout &&
1732 mkdir "directory\with\backslashes" &&
1733 >"directory\with\backslashes/randomfile" &&
1734 git add . &&
1735 git commit -m "Add directory with backslashes" &&
1736 git sparse-checkout set --cone \
1737 "directory\with\backslashes" &&
1738 test_completion "git sparse-checkout add dir" <<-\EOF &&
1739 directory\with\backslashes/
1741 rm -rf "directory\with\backslashes" &&
1742 git add . &&
1743 git commit -m "Remove directory with backslashes"
1747 test_expect_success 'non-cone mode sparse-checkout gives rooted paths' '
1748 # reset sparse-checkout repo to non-cone mode
1749 git -C sparse-checkout sparse-checkout disable &&
1750 git -C sparse-checkout sparse-checkout set --no-cone &&
1753 cd sparse-checkout &&
1754 # expected to be empty since we have not configured
1755 # custom completion for non-cone mode
1756 test_completion "git sparse-checkout set f" <<-\EOF
1757 /folder1/0/1/t.txt Z
1758 /folder1/expected Z
1759 /folder1/out Z
1760 /folder1/out_sorted Z
1761 /folder2/0/t.txt Z
1762 /folder3/t.txt Z
1767 test_expect_success 'git sparse-checkout set --cone completes directory names' '
1768 git -C sparse-checkout sparse-checkout disable &&
1771 cd sparse-checkout &&
1772 test_completion "git sparse-checkout set --cone f" <<-\EOF
1773 folder1/
1774 folder2/
1775 folder3/
1780 test_expect_success 'git switch - with -d, complete all references' '
1781 test_completion "git switch -d " <<-\EOF
1782 HEAD Z
1783 main Z
1784 matching-branch Z
1785 matching-tag Z
1786 other/branch-in-other Z
1787 other/main-in-other Z
1791 test_expect_success 'git checkout - with -d, complete only references' '
1792 test_completion "git checkout -d " <<-\EOF
1793 HEAD Z
1794 main Z
1795 matching-branch Z
1796 matching-tag Z
1797 other/branch-in-other Z
1798 other/main-in-other Z
1802 test_expect_success 'git switch - with --track, complete only remote branches' '
1803 test_completion "git switch --track " <<-\EOF &&
1804 other/branch-in-other Z
1805 other/main-in-other Z
1807 test_completion "git switch -t " <<-\EOF
1808 other/branch-in-other Z
1809 other/main-in-other Z
1813 test_expect_success 'git checkout - with --track, complete only remote branches' '
1814 test_completion "git checkout --track " <<-\EOF &&
1815 other/branch-in-other Z
1816 other/main-in-other Z
1818 test_completion "git checkout -t " <<-\EOF
1819 other/branch-in-other Z
1820 other/main-in-other Z
1824 test_expect_success 'git switch - with --no-track, complete only local branch names' '
1825 test_completion "git switch --no-track " <<-\EOF
1826 main Z
1827 matching-branch Z
1831 test_expect_success 'git checkout - with --no-track, complete only local references' '
1832 test_completion "git checkout --no-track " <<-\EOF
1833 HEAD Z
1834 main Z
1835 matching-branch Z
1836 matching-tag Z
1837 other/branch-in-other Z
1838 other/main-in-other Z
1842 test_expect_success 'git switch - with -c, complete all references' '
1843 test_completion "git switch -c new-branch " <<-\EOF
1844 HEAD Z
1845 main Z
1846 matching-branch Z
1847 matching-tag Z
1848 other/branch-in-other Z
1849 other/main-in-other Z
1853 test_expect_success 'git switch - with -C, complete all references' '
1854 test_completion "git switch -C new-branch " <<-\EOF
1855 HEAD Z
1856 main Z
1857 matching-branch Z
1858 matching-tag Z
1859 other/branch-in-other Z
1860 other/main-in-other Z
1864 test_expect_success 'git switch - with -c and --track, complete all references' '
1865 test_completion "git switch -c new-branch --track " <<-EOF
1866 HEAD Z
1867 main Z
1868 matching-branch Z
1869 matching-tag Z
1870 other/branch-in-other Z
1871 other/main-in-other Z
1875 test_expect_success 'git switch - with -C and --track, complete all references' '
1876 test_completion "git switch -C new-branch --track " <<-EOF
1877 HEAD Z
1878 main Z
1879 matching-branch Z
1880 matching-tag Z
1881 other/branch-in-other Z
1882 other/main-in-other Z
1886 test_expect_success 'git switch - with -c and --no-track, complete all references' '
1887 test_completion "git switch -c new-branch --no-track " <<-\EOF
1888 HEAD Z
1889 main Z
1890 matching-branch Z
1891 matching-tag Z
1892 other/branch-in-other Z
1893 other/main-in-other Z
1897 test_expect_success 'git switch - with -C and --no-track, complete all references' '
1898 test_completion "git switch -C new-branch --no-track " <<-\EOF
1899 HEAD Z
1900 main Z
1901 matching-branch Z
1902 matching-tag Z
1903 other/branch-in-other Z
1904 other/main-in-other Z
1908 test_expect_success 'git checkout - with -b, complete all references' '
1909 test_completion "git checkout -b new-branch " <<-\EOF
1910 HEAD Z
1911 main Z
1912 matching-branch Z
1913 matching-tag Z
1914 other/branch-in-other Z
1915 other/main-in-other Z
1919 test_expect_success 'git checkout - with -B, complete all references' '
1920 test_completion "git checkout -B new-branch " <<-\EOF
1921 HEAD Z
1922 main Z
1923 matching-branch Z
1924 matching-tag Z
1925 other/branch-in-other Z
1926 other/main-in-other Z
1930 test_expect_success 'git checkout - with -b and --track, complete all references' '
1931 test_completion "git checkout -b new-branch --track " <<-EOF
1932 HEAD Z
1933 main Z
1934 matching-branch Z
1935 matching-tag Z
1936 other/branch-in-other Z
1937 other/main-in-other Z
1941 test_expect_success 'git checkout - with -B and --track, complete all references' '
1942 test_completion "git checkout -B new-branch --track " <<-EOF
1943 HEAD Z
1944 main Z
1945 matching-branch Z
1946 matching-tag Z
1947 other/branch-in-other Z
1948 other/main-in-other Z
1952 test_expect_success 'git checkout - with -b and --no-track, complete all references' '
1953 test_completion "git checkout -b new-branch --no-track " <<-\EOF
1954 HEAD Z
1955 main Z
1956 matching-branch Z
1957 matching-tag Z
1958 other/branch-in-other Z
1959 other/main-in-other Z
1963 test_expect_success 'git checkout - with -B and --no-track, complete all references' '
1964 test_completion "git checkout -B new-branch --no-track " <<-\EOF
1965 HEAD Z
1966 main Z
1967 matching-branch Z
1968 matching-tag Z
1969 other/branch-in-other Z
1970 other/main-in-other Z
1974 test_expect_success 'git switch - for -c, complete local branches and unique remote branches' '
1975 test_completion "git switch -c " <<-\EOF
1976 branch-in-other Z
1977 main Z
1978 main-in-other Z
1979 matching-branch Z
1983 test_expect_success 'git switch - for -C, complete local branches and unique remote branches' '
1984 test_completion "git switch -C " <<-\EOF
1985 branch-in-other Z
1986 main Z
1987 main-in-other Z
1988 matching-branch Z
1992 test_expect_success 'git switch - for -c with --no-guess, complete local branches only' '
1993 test_completion "git switch --no-guess -c " <<-\EOF
1994 main Z
1995 matching-branch Z
1999 test_expect_success 'git switch - for -C with --no-guess, complete local branches only' '
2000 test_completion "git switch --no-guess -C " <<-\EOF
2001 main Z
2002 matching-branch Z
2006 test_expect_success 'git switch - for -c with --no-track, complete local branches only' '
2007 test_completion "git switch --no-track -c " <<-\EOF
2008 main Z
2009 matching-branch Z
2013 test_expect_success 'git switch - for -C with --no-track, complete local branches only' '
2014 test_completion "git switch --no-track -C " <<-\EOF
2015 main Z
2016 matching-branch Z
2020 test_expect_success 'git checkout - for -b, complete local branches and unique remote branches' '
2021 test_completion "git checkout -b " <<-\EOF
2022 branch-in-other Z
2023 main Z
2024 main-in-other Z
2025 matching-branch Z
2029 test_expect_success 'git checkout - for -B, complete local branches and unique remote branches' '
2030 test_completion "git checkout -B " <<-\EOF
2031 branch-in-other Z
2032 main Z
2033 main-in-other Z
2034 matching-branch Z
2038 test_expect_success 'git checkout - for -b with --no-guess, complete local branches only' '
2039 test_completion "git checkout --no-guess -b " <<-\EOF
2040 main Z
2041 matching-branch Z
2045 test_expect_success 'git checkout - for -B with --no-guess, complete local branches only' '
2046 test_completion "git checkout --no-guess -B " <<-\EOF
2047 main Z
2048 matching-branch Z
2052 test_expect_success 'git checkout - for -b with --no-track, complete local branches only' '
2053 test_completion "git checkout --no-track -b " <<-\EOF
2054 main Z
2055 matching-branch Z
2059 test_expect_success 'git checkout - for -B with --no-track, complete local branches only' '
2060 test_completion "git checkout --no-track -B " <<-\EOF
2061 main Z
2062 matching-branch Z
2066 test_expect_success 'git switch - with --orphan completes local branch names and unique remote branch names' '
2067 test_completion "git switch --orphan " <<-\EOF
2068 branch-in-other Z
2069 main Z
2070 main-in-other Z
2071 matching-branch Z
2075 test_expect_success 'git switch - --orphan with branch already provided completes nothing else' '
2076 test_completion "git switch --orphan main " <<-\EOF
2081 test_expect_success 'git checkout - with --orphan completes local branch names and unique remote branch names' '
2082 test_completion "git checkout --orphan " <<-\EOF
2083 branch-in-other Z
2084 main Z
2085 main-in-other Z
2086 matching-branch Z
2090 test_expect_success 'git checkout - --orphan with branch already provided completes local refs for a start-point' '
2091 test_completion "git checkout --orphan main " <<-\EOF
2092 HEAD Z
2093 main Z
2094 matching-branch Z
2095 matching-tag Z
2096 other/branch-in-other Z
2097 other/main-in-other Z
2101 test_expect_success 'git restore completes modified files' '
2102 test_commit A a.file &&
2103 echo B >a.file &&
2104 test_completion "git restore a." <<-\EOF
2105 a.file
2109 test_expect_success 'teardown after ref completion' '
2110 git branch -d matching-branch &&
2111 git tag -d matching-tag &&
2112 git remote remove other
2116 test_path_completion ()
2118 test $# = 2 || BUG "not 2 parameters to test_path_completion"
2120 local cur="$1" expected="$2"
2121 echo "$expected" >expected &&
2123 # In the following tests calling this function we only
2124 # care about how __git_complete_index_file() deals with
2125 # unusual characters in path names. By requesting only
2126 # untracked files we do not have to bother adding any
2127 # paths to the index in those tests.
2128 __git_complete_index_file --others &&
2129 print_comp
2130 ) &&
2131 test_cmp expected out
2134 test_expect_success 'setup for path completion tests' '
2135 mkdir simple-dir \
2136 "spaces in dir" \
2137 árvíztűrő &&
2138 touch simple-dir/simple-file \
2139 "spaces in dir/spaces in file" \
2140 "árvíztűrő/Сайн яваарай" &&
2141 if test_have_prereq !MINGW &&
2142 mkdir BS\\dir \
2143 '$'separators\034in\035dir'' &&
2144 touch BS\\dir/DQ\"file \
2145 '$'separators\034in\035dir/sep\036in\037file''
2146 then
2147 test_set_prereq FUNNIERNAMES
2148 else
2149 rm -rf BS\\dir '$'separators\034in\035dir''
2153 test_expect_success '__git_complete_index_file - simple' '
2154 test_path_completion simple simple-dir && # Bash is supposed to
2155 # add the trailing /.
2156 test_path_completion simple-dir/simple simple-dir/simple-file
2159 test_expect_success \
2160 '__git_complete_index_file - escaped characters on cmdline' '
2161 test_path_completion spac "spaces in dir" && # Bash will turn this
2162 # into "spaces\ in\ dir"
2163 test_path_completion "spaces\\ i" \
2164 "spaces in dir" &&
2165 test_path_completion "spaces\\ in\\ dir/s" \
2166 "spaces in dir/spaces in file" &&
2167 test_path_completion "spaces\\ in\\ dir/spaces\\ i" \
2168 "spaces in dir/spaces in file"
2171 test_expect_success \
2172 '__git_complete_index_file - quoted characters on cmdline' '
2173 # Testing with an opening but without a corresponding closing
2174 # double quote is important.
2175 test_path_completion \"spac "spaces in dir" &&
2176 test_path_completion "\"spaces i" \
2177 "spaces in dir" &&
2178 test_path_completion "\"spaces in dir/s" \
2179 "spaces in dir/spaces in file" &&
2180 test_path_completion "\"spaces in dir/spaces i" \
2181 "spaces in dir/spaces in file"
2184 test_expect_success '__git_complete_index_file - UTF-8 in ls-files output' '
2185 test_path_completion á árvíztűrő &&
2186 test_path_completion árvíztűrő/С "árvíztűrő/Сайн яваарай"
2189 test_expect_success FUNNIERNAMES \
2190 '__git_complete_index_file - C-style escapes in ls-files output' '
2191 test_path_completion BS \
2192 BS\\dir &&
2193 test_path_completion BS\\\\d \
2194 BS\\dir &&
2195 test_path_completion BS\\\\dir/DQ \
2196 BS\\dir/DQ\"file &&
2197 test_path_completion BS\\\\dir/DQ\\\"f \
2198 BS\\dir/DQ\"file
2201 test_expect_success FUNNIERNAMES \
2202 '__git_complete_index_file - \nnn-escaped characters in ls-files output' '
2203 test_path_completion sep '$'separators\034in\035dir'' &&
2204 test_path_completion '$'separators\034i'' \
2205 '$'separators\034in\035dir'' &&
2206 test_path_completion '$'separators\034in\035dir/sep'' \
2207 '$'separators\034in\035dir/sep\036in\037file'' &&
2208 test_path_completion '$'separators\034in\035dir/sep\036i'' \
2209 '$'separators\034in\035dir/sep\036in\037file''
2212 test_expect_success FUNNYNAMES \
2213 '__git_complete_index_file - removing repeated quoted path components' '
2214 test_when_finished rm -r repeated-quoted &&
2215 mkdir repeated-quoted && # A directory whose name in itself
2216 # would not be quoted ...
2217 >repeated-quoted/0-file &&
2218 >repeated-quoted/1\"file && # ... but here the file makes the
2219 # dirname quoted ...
2220 >repeated-quoted/2-file &&
2221 >repeated-quoted/3\"file && # ... and here, too.
2223 # Still, we shold only list the directory name only once.
2224 test_path_completion repeated repeated-quoted
2227 test_expect_success 'teardown after path completion tests' '
2228 rm -rf simple-dir "spaces in dir" árvíztűrő \
2229 BS\\dir '$'separators\034in\035dir''
2232 test_expect_success '__git_find_on_cmdline - single match' '
2233 echo list >expect &&
2235 words=(git command --opt list) &&
2236 cword=${#words[@]} &&
2237 __git_cmd_idx=1 &&
2238 __git_find_on_cmdline "add list remove" >actual
2239 ) &&
2240 test_cmp expect actual
2243 test_expect_success '__git_find_on_cmdline - multiple matches' '
2244 echo remove >expect &&
2246 words=(git command -o --opt remove list add) &&
2247 cword=${#words[@]} &&
2248 __git_cmd_idx=1 &&
2249 __git_find_on_cmdline "add list remove" >actual
2250 ) &&
2251 test_cmp expect actual
2254 test_expect_success '__git_find_on_cmdline - no match' '
2256 words=(git command --opt branch) &&
2257 cword=${#words[@]} &&
2258 __git_cmd_idx=1 &&
2259 __git_find_on_cmdline "add list remove" >actual
2260 ) &&
2261 test_must_be_empty actual
2264 test_expect_success '__git_find_on_cmdline - single match with index' '
2265 echo "3 list" >expect &&
2267 words=(git command --opt list) &&
2268 cword=${#words[@]} &&
2269 __git_cmd_idx=1 &&
2270 __git_find_on_cmdline --show-idx "add list remove" >actual
2271 ) &&
2272 test_cmp expect actual
2275 test_expect_success '__git_find_on_cmdline - multiple matches with index' '
2276 echo "4 remove" >expect &&
2278 words=(git command -o --opt remove list add) &&
2279 cword=${#words[@]} &&
2280 __git_cmd_idx=1 &&
2281 __git_find_on_cmdline --show-idx "add list remove" >actual
2282 ) &&
2283 test_cmp expect actual
2286 test_expect_success '__git_find_on_cmdline - no match with index' '
2288 words=(git command --opt branch) &&
2289 cword=${#words[@]} &&
2290 __git_cmd_idx=1 &&
2291 __git_find_on_cmdline --show-idx "add list remove" >actual
2292 ) &&
2293 test_must_be_empty actual
2296 test_expect_success '__git_find_on_cmdline - ignores matches before command with index' '
2297 echo "6 remove" >expect &&
2299 words=(git -C remove command -o --opt remove list add) &&
2300 cword=${#words[@]} &&
2301 __git_cmd_idx=3 &&
2302 __git_find_on_cmdline --show-idx "add list remove" >actual
2303 ) &&
2304 test_cmp expect actual
2307 test_expect_success '__git_get_config_variables' '
2308 cat >expect <<-EOF &&
2309 name-1
2310 name-2
2312 test_config interesting.name-1 good &&
2313 test_config interesting.name-2 good &&
2314 test_config subsection.interesting.name-3 bad &&
2315 __git_get_config_variables interesting >actual &&
2316 test_cmp expect actual
2319 test_expect_success '__git_pretty_aliases' '
2320 cat >expect <<-EOF &&
2321 author
2322 hash
2324 test_config pretty.author "%an %ae" &&
2325 test_config pretty.hash %H &&
2326 __git_pretty_aliases >actual &&
2327 test_cmp expect actual
2330 test_expect_success 'basic' '
2331 run_completion "git " &&
2332 # built-in
2333 grep -q "^add \$" out &&
2334 # script
2335 grep -q "^rebase \$" out &&
2336 # plumbing
2337 ! grep -q "^ls-files \$" out &&
2339 run_completion "git r" &&
2340 ! grep -q -v "^r" out
2343 test_expect_success 'double dash "git" itself' '
2344 test_completion "git --" <<-\EOF
2345 --paginate Z
2346 --no-pager Z
2347 --git-dir=
2348 --bare Z
2349 --version Z
2350 --exec-path Z
2351 --exec-path=
2352 --html-path Z
2353 --man-path Z
2354 --info-path Z
2355 --work-tree=
2356 --namespace=
2357 --no-replace-objects Z
2358 --help Z
2362 test_expect_success 'double dash "git checkout"' '
2363 test_completion "git checkout --" <<-\EOF
2364 --quiet Z
2365 --detach Z
2366 --track Z
2367 --orphan=Z
2368 --ours Z
2369 --theirs Z
2370 --merge Z
2371 --conflict=Z
2372 --patch Z
2373 --ignore-skip-worktree-bits Z
2374 --ignore-other-worktrees Z
2375 --recurse-submodules Z
2376 --progress Z
2377 --guess Z
2378 --no-guess Z
2379 --no-... Z
2380 --overlay Z
2381 --pathspec-file-nul Z
2382 --pathspec-from-file=Z
2386 test_expect_success 'general options' '
2387 test_completion "git --ver" "--version " &&
2388 test_completion "git --hel" "--help " &&
2389 test_completion "git --exe" <<-\EOF &&
2390 --exec-path Z
2391 --exec-path=
2393 test_completion "git --htm" "--html-path " &&
2394 test_completion "git --pag" "--paginate " &&
2395 test_completion "git --no-p" "--no-pager " &&
2396 test_completion "git --git" "--git-dir=" &&
2397 test_completion "git --wor" "--work-tree=" &&
2398 test_completion "git --nam" "--namespace=" &&
2399 test_completion "git --bar" "--bare " &&
2400 test_completion "git --inf" "--info-path " &&
2401 test_completion "git --no-r" "--no-replace-objects "
2404 test_expect_success 'general options plus command' '
2405 test_completion "git --version check" "checkout " &&
2406 test_completion "git --paginate check" "checkout " &&
2407 test_completion "git --git-dir=foo check" "checkout " &&
2408 test_completion "git --bare check" "checkout " &&
2409 test_completion "git --exec-path=foo check" "checkout " &&
2410 test_completion "git --html-path check" "checkout " &&
2411 test_completion "git --no-pager check" "checkout " &&
2412 test_completion "git --work-tree=foo check" "checkout " &&
2413 test_completion "git --namespace=foo check" "checkout " &&
2414 test_completion "git --paginate check" "checkout " &&
2415 test_completion "git --info-path check" "checkout " &&
2416 test_completion "git --no-replace-objects check" "checkout " &&
2417 test_completion "git --git-dir some/path check" "checkout " &&
2418 test_completion "git -c conf.var=value check" "checkout " &&
2419 test_completion "git -C some/path check" "checkout " &&
2420 test_completion "git --work-tree some/path check" "checkout " &&
2421 test_completion "git --namespace name/space check" "checkout "
2424 test_expect_success 'git --help completion' '
2425 test_completion "git --help ad" "add " &&
2426 test_completion "git --help core" "core-tutorial "
2429 test_expect_success 'completion.commands removes multiple commands' '
2430 test_config completion.commands "-cherry -mergetool" &&
2431 git --list-cmds=list-mainporcelain,list-complete,config >out &&
2432 ! grep -E "^(cherry|mergetool)$" out
2435 test_expect_success 'setup for integration tests' '
2436 echo content >file1 &&
2437 echo more >file2 &&
2438 git add file1 file2 &&
2439 git commit -m one &&
2440 git branch mybranch &&
2441 git tag mytag
2444 test_expect_success 'checkout completes ref names' '
2445 test_completion "git checkout m" <<-\EOF
2446 main Z
2447 mybranch Z
2448 mytag Z
2452 test_expect_success 'checkout does not match ref names of a different case' '
2453 test_completion "git checkout M" ""
2456 test_expect_success 'checkout matches case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2458 GIT_COMPLETION_IGNORE_CASE=1 &&
2459 test_completion "git checkout M" <<-\EOF
2460 main Z
2461 mybranch Z
2462 mytag Z
2467 test_expect_success 'checkout completes pseudo refs' '
2468 test_completion "git checkout H" <<-\EOF
2469 HEAD Z
2473 test_expect_success 'checkout completes pseudo refs case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2475 GIT_COMPLETION_IGNORE_CASE=1 &&
2476 test_completion "git checkout h" <<-\EOF
2477 HEAD Z
2482 test_expect_success 'git -C <path> checkout uses the right repo' '
2483 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
2484 branch-in-other Z
2488 test_expect_success 'show completes all refs' '
2489 test_completion "git show m" <<-\EOF
2490 main Z
2491 mybranch Z
2492 mytag Z
2496 test_expect_success '<ref>: completes paths' '
2497 test_completion "git show mytag:f" <<-\EOF
2498 file1Z
2499 file2Z
2503 test_expect_success 'complete tree filename with spaces' '
2504 echo content >"name with spaces" &&
2505 git add "name with spaces" &&
2506 git commit -m spaces &&
2507 test_completion "git show HEAD:nam" <<-\EOF
2508 name with spacesZ
2512 test_expect_success 'complete tree filename with metacharacters' '
2513 echo content >"name with \${meta}" &&
2514 git add "name with \${meta}" &&
2515 git commit -m meta &&
2516 test_completion "git show HEAD:nam" <<-\EOF
2517 name with ${meta}Z
2518 name with spacesZ
2522 test_expect_success 'symbolic-ref completes builtin options' '
2523 test_completion "git symbolic-ref --d" <<-\EOF
2524 --delete Z
2528 test_expect_success 'symbolic-ref completes short ref names' '
2529 test_completion "git symbolic-ref foo m" <<-\EOF
2530 main Z
2531 mybranch Z
2532 mytag Z
2536 test_expect_success 'symbolic-ref completes full ref names' '
2537 test_completion "git symbolic-ref foo refs/" <<-\EOF
2538 refs/heads/main Z
2539 refs/heads/mybranch Z
2540 refs/tags/mytag Z
2541 refs/tags/A Z
2545 test_expect_success PERL 'send-email' '
2546 test_completion "git send-email --cov" <<-\EOF &&
2547 --cover-from-description=Z
2548 --cover-letter Z
2550 test_completion "git send-email --val" <<-\EOF &&
2551 --validate Z
2553 test_completion "git send-email ma" "main "
2556 test_expect_success 'complete files' '
2557 git init tmp && cd tmp &&
2558 test_when_finished "cd .. && rm -rf tmp" &&
2560 echo "expected" > .gitignore &&
2561 echo "out" >> .gitignore &&
2562 echo "out_sorted" >> .gitignore &&
2564 git add .gitignore &&
2565 test_completion "git commit " ".gitignore" &&
2567 git commit -m ignore &&
2569 touch new &&
2570 test_completion "git add " "new" &&
2572 git add new &&
2573 git commit -a -m new &&
2574 test_completion "git add " "" &&
2576 git mv new modified &&
2577 echo modify > modified &&
2578 test_completion "git add " "modified" &&
2580 mkdir -p some/deep &&
2581 touch some/deep/path &&
2582 test_completion "git add some/" "some/deep" &&
2583 git clean -f some &&
2585 touch untracked &&
2587 : TODO .gitignore should not be here &&
2588 test_completion "git rm " <<-\EOF &&
2589 .gitignore
2590 modified
2593 test_completion "git clean " "untracked" &&
2595 : TODO .gitignore should not be here &&
2596 test_completion "git mv " <<-\EOF &&
2597 .gitignore
2598 modified
2601 mkdir dir &&
2602 touch dir/file-in-dir &&
2603 git add dir/file-in-dir &&
2604 git commit -m dir &&
2606 mkdir untracked-dir &&
2608 : TODO .gitignore should not be here &&
2609 test_completion "git mv modified " <<-\EOF &&
2610 .gitignore
2612 modified
2613 untracked
2614 untracked-dir
2617 test_completion "git commit " "modified" &&
2619 : TODO .gitignore should not be here &&
2620 test_completion "git ls-files " <<-\EOF &&
2621 .gitignore
2623 modified
2626 touch momified &&
2627 test_completion "git add mom" "momified"
2630 test_expect_success "simple alias" '
2631 test_config alias.co checkout &&
2632 test_completion "git co m" <<-\EOF
2633 main Z
2634 mybranch Z
2635 mytag Z
2639 test_expect_success "recursive alias" '
2640 test_config alias.co checkout &&
2641 test_config alias.cod "co --detached" &&
2642 test_completion "git cod m" <<-\EOF
2643 main Z
2644 mybranch Z
2645 mytag Z
2649 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
2650 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
2651 test_completion "git co m" <<-\EOF
2652 main Z
2653 mybranch Z
2654 mytag Z
2658 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
2659 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
2660 test_completion "git co m" <<-\EOF
2661 main Z
2662 mybranch Z
2663 mytag Z
2667 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
2668 test_config alias.co "!f() { : git checkout ; if ... } f" &&
2669 test_completion "git co m" <<-\EOF
2670 main Z
2671 mybranch Z
2672 mytag Z
2676 test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd> ; ... }' '
2677 test_config alias.co "!f() { : checkout ; if ... } f" &&
2678 test_completion "git co m" <<-\EOF
2679 main Z
2680 mybranch Z
2681 mytag Z
2685 test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd>; ... }' '
2686 test_config alias.co "!f() { : checkout; if ... } f" &&
2687 test_completion "git co m" <<-\EOF
2688 main Z
2689 mybranch Z
2690 mytag Z
2694 test_expect_success 'completion without explicit _git_xxx function' '
2695 test_completion "git version --" <<-\EOF
2696 --build-options Z
2697 --no-build-options Z
2701 test_expect_failure 'complete with tilde expansion' '
2702 git init tmp && cd tmp &&
2703 test_when_finished "cd .. && rm -rf tmp" &&
2705 touch ~/tmp/file &&
2707 test_completion "git add ~/tmp/" "~/tmp/file"
2710 test_expect_success 'setup other remote for remote reference completion' '
2711 git remote add other otherrepo &&
2712 git fetch other
2715 for flag in -d --delete
2717 test_expect_success "__git_complete_remote_or_refspec - push $flag other" '
2718 sed -e "s/Z$//" >expected <<-EOF &&
2719 main-in-other Z
2722 words=(git push '$flag' other ma) &&
2723 cword=${#words[@]} cur=${words[cword-1]} &&
2724 __git_cmd_idx=1 &&
2725 __git_complete_remote_or_refspec &&
2726 print_comp
2727 ) &&
2728 test_cmp expected out
2731 test_expect_failure "__git_complete_remote_or_refspec - push other $flag" '
2732 sed -e "s/Z$//" >expected <<-EOF &&
2733 main-in-other Z
2736 words=(git push other '$flag' ma) &&
2737 cword=${#words[@]} cur=${words[cword-1]} &&
2738 __git_cmd_idx=1 &&
2739 __git_complete_remote_or_refspec &&
2740 print_comp
2741 ) &&
2742 test_cmp expected out
2744 done
2746 test_expect_success 'git config subcommand' '
2747 test_completion "git config " <<-\EOF
2748 edit Z
2749 get Z
2750 list Z
2751 remove-section Z
2752 rename-section Z
2753 set Z
2754 unset Z
2758 test_expect_success 'git config subcommand options' '
2759 test_completion "git config get --show-" <<-\EOF
2760 --show-names Z
2761 --show-origin Z
2762 --show-scope Z
2766 test_expect_success 'git config get' '
2767 test_when_finished "rm -f cfgfile" &&
2768 git config set --file cfgfile foo.bar baz &&
2769 test_completion "git config get --file cfgfile foo." <<-\EOF
2770 foo.bar Z
2774 test_expect_success 'git config set - section' '
2775 test_completion "git config set br" <<-\EOF
2776 branch.Z
2777 browser.Z
2781 test_expect_success 'git config set - section include, includeIf' '
2782 test_completion "git config set inclu" <<-\EOF
2783 include.Z
2784 includeIf.Z
2788 test_expect_success 'git config set - variable name' '
2789 test_completion "git config set log.d" <<-\EOF
2790 log.date Z
2791 log.decorate Z
2792 log.diffMerges Z
2796 test_expect_success 'git config set - variable name include' '
2797 test_completion "git config set include.p" <<-\EOF
2798 include.path Z
2802 test_expect_success 'setup for git config submodule tests' '
2803 test_create_repo sub &&
2804 test_commit -C sub initial &&
2805 git submodule add ./sub
2808 test_expect_success 'git config set - variable name - submodule and __git_compute_first_level_config_vars_for_section' '
2809 test_completion "git config set submodule." <<-\EOF
2810 submodule.active Z
2811 submodule.alternateErrorStrategy Z
2812 submodule.alternateLocation Z
2813 submodule.fetchJobs Z
2814 submodule.propagateBranches Z
2815 submodule.recurse Z
2816 submodule.sub.Z
2820 test_expect_success 'git config set - variable name - __git_compute_second_level_config_vars_for_section' '
2821 test_completion "git config set submodule.sub." <<-\EOF
2822 submodule.sub.url Z
2823 submodule.sub.update Z
2824 submodule.sub.branch Z
2825 submodule.sub.fetchRecurseSubmodules Z
2826 submodule.sub.ignore Z
2827 submodule.sub.active Z
2831 test_expect_success 'git config set - value' '
2832 test_completion "git config set color.pager " <<-\EOF
2833 false Z
2834 true Z
2838 test_expect_success 'git -c - section' '
2839 test_completion "git -c br" <<-\EOF
2840 branch.Z
2841 browser.Z
2845 test_expect_success 'git -c - variable name' '
2846 test_completion "git -c log.d" <<-\EOF
2847 log.date=Z
2848 log.decorate=Z
2849 log.diffMerges=Z
2853 test_expect_success 'git -c - value' '
2854 test_completion "git -c color.pager=" <<-\EOF
2855 false Z
2856 true Z
2860 test_expect_success 'git clone --config= - section' '
2861 test_completion "git clone --config=br" <<-\EOF
2862 branch.Z
2863 browser.Z
2867 test_expect_success 'git clone --config= - variable name' '
2868 test_completion "git clone --config=log.d" <<-\EOF
2869 log.date=Z
2870 log.decorate=Z
2871 log.diffMerges=Z
2875 test_expect_success 'git clone --config= - value' '
2876 test_completion "git clone --config=color.pager=" <<-\EOF
2877 false Z
2878 true Z
2882 test_expect_success 'git reflog show' '
2883 test_when_finished "git checkout - && git branch -d shown" &&
2884 git checkout -b shown &&
2885 test_completion "git reflog sho" <<-\EOF &&
2886 show Z
2887 shown Z
2889 test_completion "git reflog show sho" "shown " &&
2890 test_completion "git reflog shown sho" "shown " &&
2891 test_completion "git reflog --unt" "--until=" &&
2892 test_completion "git reflog show --unt" "--until=" &&
2893 test_completion "git reflog shown --unt" "--until="
2896 test_expect_success 'options with value' '
2897 test_completion "git merge -X diff-algorithm=" <<-\EOF
2902 test_expect_success 'sourcing the completion script clears cached commands' '
2904 __git_compute_all_commands &&
2905 test -n "$__git_all_commands" &&
2906 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2907 test -z "$__git_all_commands"
2911 test_expect_success 'sourcing the completion script clears cached merge strategies' '
2913 __git_compute_merge_strategies &&
2914 test -n "$__git_merge_strategies" &&
2915 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2916 test -z "$__git_merge_strategies"
2920 test_expect_success 'sourcing the completion script clears cached --options' '
2922 __gitcomp_builtin checkout &&
2923 test -n "$__gitcomp_builtin_checkout" &&
2924 __gitcomp_builtin notes_edit &&
2925 test -n "$__gitcomp_builtin_notes_edit" &&
2926 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2927 test -z "$__gitcomp_builtin_checkout" &&
2928 test -z "$__gitcomp_builtin_notes_edit"
2932 test_expect_success 'option aliases are not shown by default' '
2933 test_completion "git clone --recurs" "--recurse-submodules "
2936 test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
2938 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2939 GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2940 test_completion "git clone --recurs" <<-\EOF
2941 --recurse-submodules Z
2942 --recursive Z
2947 test_expect_success 'plumbing commands are excluded without GIT_COMPLETION_SHOW_ALL_COMMANDS' '
2949 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2950 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2952 # Just mainporcelain, not plumbing commands
2953 run_completion "git c" &&
2954 grep checkout out &&
2955 ! grep cat-file out
2959 test_expect_success 'all commands are shown with GIT_COMPLETION_SHOW_ALL_COMMANDS (also main non-builtin)' '
2961 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2962 GIT_COMPLETION_SHOW_ALL_COMMANDS=1 &&
2963 export GIT_COMPLETION_SHOW_ALL_COMMANDS &&
2964 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2966 # Both mainporcelain and plumbing commands
2967 run_completion "git c" &&
2968 grep checkout out &&
2969 grep cat-file out &&
2971 # Check "gitk", a "main" command, but not a built-in + more plumbing
2972 run_completion "git g" &&
2973 grep gitk out &&
2974 grep get-tar-commit-id out
2978 test_expect_success '__git_complete' '
2979 unset -f __git_wrap__git_main &&
2981 __git_complete foo __git_main &&
2982 __git_have_func __git_wrap__git_main &&
2983 unset -f __git_wrap__git_main &&
2985 __git_complete gf _git_fetch &&
2986 __git_have_func __git_wrap_git_fetch &&
2988 __git_complete foo git &&
2989 __git_have_func __git_wrap__git_main &&
2990 unset -f __git_wrap__git_main &&
2992 __git_complete gd git_diff &&
2993 __git_have_func __git_wrap_git_diff &&
2995 test_must_fail __git_complete ga missing
2998 test_expect_success '__git_pseudoref_exists' '
2999 test_when_finished "rm -rf repo" &&
3000 git init repo &&
3002 cd repo &&
3003 sane_unset __git_repo_path &&
3005 # HEAD should exist, even if it points to an unborn branch.
3006 __git_pseudoref_exists HEAD >output 2>&1 &&
3007 test_must_be_empty output &&
3009 # HEAD points to an existing branch, so it should exist.
3010 test_commit A &&
3011 __git_pseudoref_exists HEAD >output 2>&1 &&
3012 test_must_be_empty output &&
3014 # CHERRY_PICK_HEAD does not exist, so the existence check should fail.
3015 ! __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
3016 test_must_be_empty output &&
3018 # CHERRY_PICK_HEAD points to a commit, so it should exist.
3019 git update-ref CHERRY_PICK_HEAD A &&
3020 __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
3021 test_must_be_empty output
3025 test_done