Makefile: FreeBSD cannot do C99-or-below build
[git/debian.git] / t / t9902-completion.sh
blob98c628063288bca40f08664abf329340f05a28fa
1 #!/bin/sh
3 # Copyright (c) 2012-2020 Felipe Contreras
6 test_description='test bash completion'
8 . ./lib-bash.sh
10 complete ()
12 # do nothing
13 return 0
16 # Be careful when updating these lists:
18 # (1) The build tree may have build artifact from different branch, or
19 # the user's $PATH may have a random executable that may begin
20 # with "git-check" that are not part of the subcommands this build
21 # will ship, e.g. "check-ignore". The tests for completion for
22 # subcommand names tests how "check" is expanded; we limit the
23 # possible candidates to "checkout" and "check-attr" to make sure
24 # "check-attr", which is known by the filter function as a
25 # subcommand to be thrown out, while excluding other random files
26 # that happen to begin with "check" to avoid letting them get in
27 # the way.
29 # (2) A test makes sure that common subcommands are included in the
30 # completion for "git <TAB>", and a plumbing is excluded. "add",
31 # "rebase" and "ls-files" are listed for this.
33 GIT_TESTING_ALL_COMMAND_LIST='add checkout check-attr rebase ls-files'
34 GIT_TESTING_PORCELAIN_COMMAND_LIST='add checkout rebase'
36 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
38 # We don't need this function to actually join words or do anything special.
39 # Also, it's cleaner to avoid touching bash's internal completion variables.
40 # So let's override it with a minimal version for testing purposes.
41 _get_comp_words_by_ref ()
43 while [ $# -gt 0 ]; do
44 case "$1" in
45 cur)
46 cur=${_words[_cword]}
48 prev)
49 prev=${_words[_cword-1]}
51 words)
52 words=("${_words[@]}")
54 cword)
55 cword=$_cword
57 esac
58 shift
59 done
62 print_comp ()
64 local IFS=$'\n'
65 echo "${COMPREPLY[*]}" > out
68 run_completion ()
70 local -a COMPREPLY _words
71 local _cword
72 _words=( $1 )
73 test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
74 (( _cword = ${#_words[@]} - 1 ))
75 __git_wrap__git_main && print_comp
78 # Test high-level completion
79 # Arguments are:
80 # 1: typed text so far (cur)
81 # 2: expected completion
82 test_completion ()
84 if test $# -gt 1
85 then
86 printf '%s\n' "$2" >expected
87 else
88 sed -e 's/Z$//' |sort >expected
89 fi &&
90 run_completion "$1" &&
91 sort out >out_sorted &&
92 test_cmp expected out_sorted
95 # Test __gitcomp.
96 # The first argument is the typed text so far (cur); the rest are
97 # passed to __gitcomp. Expected output comes is read from the
98 # standard input, like test_completion().
99 test_gitcomp ()
101 local -a COMPREPLY &&
102 sed -e 's/Z$//' >expected &&
103 local cur="$1" &&
104 shift &&
105 __gitcomp "$@" &&
106 print_comp &&
107 test_cmp expected out
110 # Test __gitcomp_nl
111 # Arguments are:
112 # 1: current word (cur)
113 # -: the rest are passed to __gitcomp_nl
114 test_gitcomp_nl ()
116 local -a COMPREPLY &&
117 sed -e 's/Z$//' >expected &&
118 local cur="$1" &&
119 shift &&
120 __gitcomp_nl "$@" &&
121 print_comp &&
122 test_cmp expected out
125 invalid_variable_name='${foo.bar}'
127 actual="$TRASH_DIRECTORY/actual"
129 if test_have_prereq MINGW
130 then
131 ROOT="$(pwd -W)"
132 else
133 ROOT="$(pwd)"
136 test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
137 mkdir -p subdir/subsubdir &&
138 mkdir -p non-repo &&
139 git init -b main otherrepo
142 test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '
143 echo "$ROOT/otherrepo/.git" >expected &&
145 __git_dir="$ROOT/otherrepo/.git" &&
146 __git_find_repo_path &&
147 echo "$__git_repo_path" >"$actual"
148 ) &&
149 test_cmp expected "$actual"
152 test_expect_success '__git_find_repo_path - .git directory in cwd' '
153 echo ".git" >expected &&
155 __git_find_repo_path &&
156 echo "$__git_repo_path" >"$actual"
157 ) &&
158 test_cmp expected "$actual"
161 test_expect_success '__git_find_repo_path - .git directory in parent' '
162 echo "$ROOT/.git" >expected &&
164 cd subdir/subsubdir &&
165 __git_find_repo_path &&
166 echo "$__git_repo_path" >"$actual"
167 ) &&
168 test_cmp expected "$actual"
171 test_expect_success '__git_find_repo_path - cwd is a .git directory' '
172 echo "." >expected &&
174 cd .git &&
175 __git_find_repo_path &&
176 echo "$__git_repo_path" >"$actual"
177 ) &&
178 test_cmp expected "$actual"
181 test_expect_success '__git_find_repo_path - parent is a .git directory' '
182 echo "$ROOT/.git" >expected &&
184 cd .git/objects &&
185 __git_find_repo_path &&
186 echo "$__git_repo_path" >"$actual"
187 ) &&
188 test_cmp expected "$actual"
191 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
192 echo "$ROOT/otherrepo/.git" >expected &&
194 GIT_DIR="$ROOT/otherrepo/.git" &&
195 export GIT_DIR &&
196 __git_find_repo_path &&
197 echo "$__git_repo_path" >"$actual"
198 ) &&
199 test_cmp expected "$actual"
202 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
203 echo "$ROOT/otherrepo/.git" >expected &&
205 GIT_DIR="$ROOT/otherrepo/.git" &&
206 export GIT_DIR &&
207 cd subdir &&
208 __git_find_repo_path &&
209 echo "$__git_repo_path" >"$actual"
210 ) &&
211 test_cmp expected "$actual"
214 test_expect_success '__git_find_repo_path - from command line while "git -C"' '
215 echo "$ROOT/.git" >expected &&
217 __git_dir="$ROOT/.git" &&
218 __git_C_args=(-C otherrepo) &&
219 __git_find_repo_path &&
220 echo "$__git_repo_path" >"$actual"
221 ) &&
222 test_cmp expected "$actual"
225 test_expect_success '__git_find_repo_path - relative dir from command line and "git -C"' '
226 echo "$ROOT/otherrepo/.git" >expected &&
228 cd subdir &&
229 __git_dir="otherrepo/.git" &&
230 __git_C_args=(-C ..) &&
231 __git_find_repo_path &&
232 echo "$__git_repo_path" >"$actual"
233 ) &&
234 test_cmp expected "$actual"
237 test_expect_success '__git_find_repo_path - $GIT_DIR set while "git -C"' '
238 echo "$ROOT/.git" >expected &&
240 GIT_DIR="$ROOT/.git" &&
241 export GIT_DIR &&
242 __git_C_args=(-C otherrepo) &&
243 __git_find_repo_path &&
244 echo "$__git_repo_path" >"$actual"
245 ) &&
246 test_cmp expected "$actual"
249 test_expect_success '__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
250 echo "$ROOT/otherrepo/.git" >expected &&
252 cd subdir &&
253 GIT_DIR="otherrepo/.git" &&
254 export GIT_DIR &&
255 __git_C_args=(-C ..) &&
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 - "git -C" while .git directory in cwd' '
263 echo "$ROOT/otherrepo/.git" >expected &&
265 __git_C_args=(-C otherrepo) &&
266 __git_find_repo_path &&
267 echo "$__git_repo_path" >"$actual"
268 ) &&
269 test_cmp expected "$actual"
272 test_expect_success '__git_find_repo_path - "git -C" while cwd is a .git directory' '
273 echo "$ROOT/otherrepo/.git" >expected &&
275 cd .git &&
276 __git_C_args=(-C .. -C otherrepo) &&
277 __git_find_repo_path &&
278 echo "$__git_repo_path" >"$actual"
279 ) &&
280 test_cmp expected "$actual"
283 test_expect_success '__git_find_repo_path - "git -C" while .git directory in parent' '
284 echo "$ROOT/otherrepo/.git" >expected &&
286 cd subdir &&
287 __git_C_args=(-C .. -C otherrepo) &&
288 __git_find_repo_path &&
289 echo "$__git_repo_path" >"$actual"
290 ) &&
291 test_cmp expected "$actual"
294 test_expect_success '__git_find_repo_path - non-existing path in "git -C"' '
296 __git_C_args=(-C non-existing) &&
297 test_must_fail __git_find_repo_path &&
298 printf "$__git_repo_path" >"$actual"
299 ) &&
300 test_must_be_empty "$actual"
303 test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '
305 __git_dir="non-existing" &&
306 test_must_fail __git_find_repo_path &&
307 printf "$__git_repo_path" >"$actual"
308 ) &&
309 test_must_be_empty "$actual"
312 test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
314 GIT_DIR="$ROOT/non-existing" &&
315 export GIT_DIR &&
316 test_must_fail __git_find_repo_path &&
317 printf "$__git_repo_path" >"$actual"
318 ) &&
319 test_must_be_empty "$actual"
322 test_expect_success '__git_find_repo_path - gitfile in cwd' '
323 echo "$ROOT/otherrepo/.git" >expected &&
324 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
325 test_when_finished "rm -f subdir/.git" &&
327 cd subdir &&
328 __git_find_repo_path &&
329 echo "$__git_repo_path" >"$actual"
330 ) &&
331 test_cmp expected "$actual"
334 test_expect_success '__git_find_repo_path - gitfile in parent' '
335 echo "$ROOT/otherrepo/.git" >expected &&
336 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
337 test_when_finished "rm -f subdir/.git" &&
339 cd subdir/subsubdir &&
340 __git_find_repo_path &&
341 echo "$__git_repo_path" >"$actual"
342 ) &&
343 test_cmp expected "$actual"
346 test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '
347 echo "$ROOT/otherrepo/.git" >expected &&
348 mkdir otherrepo/dir &&
349 test_when_finished "rm -rf otherrepo/dir" &&
350 ln -s otherrepo/dir link &&
351 test_when_finished "rm -f link" &&
353 cd link &&
354 __git_find_repo_path &&
355 echo "$__git_repo_path" >"$actual"
356 ) &&
357 test_cmp expected "$actual"
360 test_expect_success '__git_find_repo_path - not a git repository' '
362 cd non-repo &&
363 GIT_CEILING_DIRECTORIES="$ROOT" &&
364 export GIT_CEILING_DIRECTORIES &&
365 test_must_fail __git_find_repo_path &&
366 printf "$__git_repo_path" >"$actual"
367 ) &&
368 test_must_be_empty "$actual"
371 test_expect_success '__gitdir - finds repo' '
372 echo "$ROOT/.git" >expected &&
374 cd subdir/subsubdir &&
375 __gitdir >"$actual"
376 ) &&
377 test_cmp expected "$actual"
381 test_expect_success '__gitdir - returns error when cannot find repo' '
383 __git_dir="non-existing" &&
384 test_must_fail __gitdir >"$actual"
385 ) &&
386 test_must_be_empty "$actual"
389 test_expect_success '__gitdir - repo as argument' '
390 echo "otherrepo/.git" >expected &&
392 __gitdir "otherrepo" >"$actual"
393 ) &&
394 test_cmp expected "$actual"
397 test_expect_success '__gitdir - remote as argument' '
398 echo "remote" >expected &&
400 __gitdir "remote" >"$actual"
401 ) &&
402 test_cmp expected "$actual"
406 test_expect_success '__git_dequote - plain unquoted word' '
407 __git_dequote unquoted-word &&
408 verbose test unquoted-word = "$dequoted_word"
411 # input: b\a\c\k\'\\\"s\l\a\s\h\es
412 # expected: back'\"slashes
413 test_expect_success '__git_dequote - backslash escaped' '
414 __git_dequote "b\a\c\k\\'\''\\\\\\\"s\l\a\s\h\es" &&
415 verbose test "back'\''\\\"slashes" = "$dequoted_word"
418 # input: sin'gle\' '"quo'ted
419 # expected: single\ "quoted
420 test_expect_success '__git_dequote - single quoted' '
421 __git_dequote "'"sin'gle\\\\' '\\\"quo'ted"'" &&
422 verbose test '\''single\ "quoted'\'' = "$dequoted_word"
425 # input: dou"ble\\" "\"\quot"ed
426 # expected: double\ "\quoted
427 test_expect_success '__git_dequote - double quoted' '
428 __git_dequote '\''dou"ble\\" "\"\quot"ed'\'' &&
429 verbose test '\''double\ "\quoted'\'' = "$dequoted_word"
432 # input: 'open single quote
433 test_expect_success '__git_dequote - open single quote' '
434 __git_dequote "'\''open single quote" &&
435 verbose test "open single quote" = "$dequoted_word"
438 # input: "open double quote
439 test_expect_success '__git_dequote - open double quote' '
440 __git_dequote "\"open double quote" &&
441 verbose test "open double quote" = "$dequoted_word"
445 test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' '
446 sed -e "s/Z$//g" >expected <<-EOF &&
447 with-trailing-space Z
448 without-trailing-spaceZ
449 --option Z
450 --option=Z
451 $invalid_variable_name Z
454 cur=should_be_ignored &&
455 __gitcomp_direct "$(cat expected)" &&
456 print_comp
457 ) &&
458 test_cmp expected out
461 test_expect_success '__gitcomp - trailing space - options' '
462 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
463 --reset-author" <<-EOF
464 --reuse-message=Z
465 --reedit-message=Z
466 --reset-author Z
470 test_expect_success '__gitcomp - trailing space - config keys' '
471 test_gitcomp "br" "branch. branch.autosetupmerge
472 branch.autosetuprebase browser." <<-\EOF
473 branch.Z
474 branch.autosetupmerge Z
475 branch.autosetuprebase Z
476 browser.Z
480 test_expect_success '__gitcomp - option parameter' '
481 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
482 "" "re" <<-\EOF
483 recursive Z
484 resolve Z
488 test_expect_success '__gitcomp - prefix' '
489 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
490 "branch.maint." "me" <<-\EOF
491 branch.maint.merge Z
492 branch.maint.mergeoptions Z
496 test_expect_success '__gitcomp - suffix' '
497 test_gitcomp "branch.me" "master maint next seen" "branch." \
498 "ma" "." <<-\EOF
499 branch.master.Z
500 branch.maint.Z
504 test_expect_success '__gitcomp - ignore optional negative options' '
505 test_gitcomp "--" "--abc --def --no-one -- --no-two" <<-\EOF
506 --abc Z
507 --def Z
508 --no-one Z
509 --no-... Z
513 test_expect_success '__gitcomp - ignore/narrow optional negative options' '
514 test_gitcomp "--a" "--abc --abcdef --no-one -- --no-two" <<-\EOF
515 --abc Z
516 --abcdef Z
520 test_expect_success '__gitcomp - ignore/narrow optional negative options' '
521 test_gitcomp "--n" "--abc --def --no-one -- --no-two" <<-\EOF
522 --no-one Z
523 --no-... Z
527 test_expect_success '__gitcomp - expand all negative options' '
528 test_gitcomp "--no-" "--abc --def --no-one -- --no-two" <<-\EOF
529 --no-one Z
530 --no-two Z
534 test_expect_success '__gitcomp - expand/narrow all negative options' '
535 test_gitcomp "--no-o" "--abc --def --no-one -- --no-two" <<-\EOF
536 --no-one Z
540 test_expect_success '__gitcomp - equal skip' '
541 test_gitcomp "--option=" "--option=" <<-\EOF &&
544 test_gitcomp "option=" "option=" <<-\EOF
549 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
550 __gitcomp "$invalid_variable_name"
553 read -r -d "" refs <<-\EOF
554 main
555 maint
556 next
557 seen
560 test_expect_success '__gitcomp_nl - trailing space' '
561 test_gitcomp_nl "m" "$refs" <<-EOF
562 main Z
563 maint Z
567 test_expect_success '__gitcomp_nl - prefix' '
568 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
569 --fixup=main Z
570 --fixup=maint Z
574 test_expect_success '__gitcomp_nl - suffix' '
575 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
576 branch.main.Z
577 branch.maint.Z
581 test_expect_success '__gitcomp_nl - no suffix' '
582 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
583 mainZ
584 maintZ
588 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
589 __gitcomp_nl "$invalid_variable_name"
592 test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
593 cat >expect <<-EOF &&
594 remote_from_file_1
595 remote_from_file_2
596 remote_in_config_1
597 remote_in_config_2
599 test_when_finished "rm -rf .git/remotes" &&
600 mkdir -p .git/remotes &&
601 >.git/remotes/remote_from_file_1 &&
602 >.git/remotes/remote_from_file_2 &&
603 test_when_finished "git remote remove remote_in_config_1" &&
604 git remote add remote_in_config_1 git://remote_1 &&
605 test_when_finished "git remote remove remote_in_config_2" &&
606 git remote add remote_in_config_2 git://remote_2 &&
608 __git_remotes >actual
609 ) &&
610 test_cmp expect actual
613 test_expect_success '__git_is_configured_remote' '
614 test_when_finished "git remote remove remote_1" &&
615 git remote add remote_1 git://remote_1 &&
616 test_when_finished "git remote remove remote_2" &&
617 git remote add remote_2 git://remote_2 &&
619 verbose __git_is_configured_remote remote_2 &&
620 test_must_fail __git_is_configured_remote non-existent
624 test_expect_success 'setup for ref completion' '
625 git commit --allow-empty -m initial &&
626 git branch -M main &&
627 git branch matching-branch &&
628 git tag matching-tag &&
630 cd otherrepo &&
631 git commit --allow-empty -m initial &&
632 git branch -m main main-in-other &&
633 git branch branch-in-other &&
634 git tag tag-in-other
635 ) &&
636 git remote add other "$ROOT/otherrepo/.git" &&
637 git fetch --no-tags other &&
638 rm -f .git/FETCH_HEAD &&
639 git init thirdrepo
642 test_expect_success '__git_refs - simple' '
643 cat >expected <<-EOF &&
644 HEAD
645 main
646 matching-branch
647 other/branch-in-other
648 other/main-in-other
649 matching-tag
652 cur= &&
653 __git_refs >"$actual"
654 ) &&
655 test_cmp expected "$actual"
658 test_expect_success '__git_refs - full refs' '
659 cat >expected <<-EOF &&
660 refs/heads/main
661 refs/heads/matching-branch
662 refs/remotes/other/branch-in-other
663 refs/remotes/other/main-in-other
664 refs/tags/matching-tag
667 cur=refs/heads/ &&
668 __git_refs >"$actual"
669 ) &&
670 test_cmp expected "$actual"
673 test_expect_success '__git_refs - repo given on the command line' '
674 cat >expected <<-EOF &&
675 HEAD
676 branch-in-other
677 main-in-other
678 tag-in-other
681 __git_dir="$ROOT/otherrepo/.git" &&
682 cur= &&
683 __git_refs >"$actual"
684 ) &&
685 test_cmp expected "$actual"
688 test_expect_success '__git_refs - remote on local file system' '
689 cat >expected <<-EOF &&
690 HEAD
691 branch-in-other
692 main-in-other
693 tag-in-other
696 cur= &&
697 __git_refs otherrepo >"$actual"
698 ) &&
699 test_cmp expected "$actual"
702 test_expect_success '__git_refs - remote on local file system - full refs' '
703 cat >expected <<-EOF &&
704 refs/heads/branch-in-other
705 refs/heads/main-in-other
706 refs/tags/tag-in-other
709 cur=refs/ &&
710 __git_refs otherrepo >"$actual"
711 ) &&
712 test_cmp expected "$actual"
715 test_expect_success '__git_refs - configured remote' '
716 cat >expected <<-EOF &&
717 HEAD
718 branch-in-other
719 main-in-other
722 cur= &&
723 __git_refs other >"$actual"
724 ) &&
725 test_cmp expected "$actual"
728 test_expect_success '__git_refs - configured remote - full refs' '
729 cat >expected <<-EOF &&
730 HEAD
731 refs/heads/branch-in-other
732 refs/heads/main-in-other
733 refs/tags/tag-in-other
736 cur=refs/ &&
737 __git_refs other >"$actual"
738 ) &&
739 test_cmp expected "$actual"
742 test_expect_success '__git_refs - configured remote - repo given on the command line' '
743 cat >expected <<-EOF &&
744 HEAD
745 branch-in-other
746 main-in-other
749 cd thirdrepo &&
750 __git_dir="$ROOT/.git" &&
751 cur= &&
752 __git_refs other >"$actual"
753 ) &&
754 test_cmp expected "$actual"
757 test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
758 cat >expected <<-EOF &&
759 HEAD
760 refs/heads/branch-in-other
761 refs/heads/main-in-other
762 refs/tags/tag-in-other
765 cd thirdrepo &&
766 __git_dir="$ROOT/.git" &&
767 cur=refs/ &&
768 __git_refs other >"$actual"
769 ) &&
770 test_cmp expected "$actual"
773 test_expect_success '__git_refs - configured remote - remote name matches a directory' '
774 cat >expected <<-EOF &&
775 HEAD
776 branch-in-other
777 main-in-other
779 mkdir other &&
780 test_when_finished "rm -rf other" &&
782 cur= &&
783 __git_refs other >"$actual"
784 ) &&
785 test_cmp expected "$actual"
788 test_expect_success '__git_refs - URL remote' '
789 cat >expected <<-EOF &&
790 HEAD
791 branch-in-other
792 main-in-other
793 tag-in-other
796 cur= &&
797 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
798 ) &&
799 test_cmp expected "$actual"
802 test_expect_success '__git_refs - URL remote - full refs' '
803 cat >expected <<-EOF &&
804 HEAD
805 refs/heads/branch-in-other
806 refs/heads/main-in-other
807 refs/tags/tag-in-other
810 cur=refs/ &&
811 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
812 ) &&
813 test_cmp expected "$actual"
816 test_expect_success '__git_refs - non-existing remote' '
818 cur= &&
819 __git_refs non-existing >"$actual"
820 ) &&
821 test_must_be_empty "$actual"
824 test_expect_success '__git_refs - non-existing remote - full refs' '
826 cur=refs/ &&
827 __git_refs non-existing >"$actual"
828 ) &&
829 test_must_be_empty "$actual"
832 test_expect_success '__git_refs - non-existing URL remote' '
834 cur= &&
835 __git_refs "file://$ROOT/non-existing" >"$actual"
836 ) &&
837 test_must_be_empty "$actual"
840 test_expect_success '__git_refs - non-existing URL remote - full refs' '
842 cur=refs/ &&
843 __git_refs "file://$ROOT/non-existing" >"$actual"
844 ) &&
845 test_must_be_empty "$actual"
848 test_expect_success '__git_refs - not in a git repository' '
850 GIT_CEILING_DIRECTORIES="$ROOT" &&
851 export GIT_CEILING_DIRECTORIES &&
852 cd subdir &&
853 cur= &&
854 __git_refs >"$actual"
855 ) &&
856 test_must_be_empty "$actual"
859 test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
860 cat >expected <<-EOF &&
861 HEAD
862 main
863 matching-branch
864 other/ambiguous
865 other/branch-in-other
866 other/main-in-other
867 remote/ambiguous
868 remote/branch-in-remote
869 matching-tag
870 branch-in-other
871 branch-in-remote
872 main-in-other
874 for remote_ref in refs/remotes/other/ambiguous \
875 refs/remotes/remote/ambiguous \
876 refs/remotes/remote/branch-in-remote
878 git update-ref $remote_ref main &&
879 test_when_finished "git update-ref -d $remote_ref" || return 1
880 done &&
882 cur= &&
883 __git_refs "" 1 >"$actual"
884 ) &&
885 test_cmp expected "$actual"
888 test_expect_success '__git_refs - after --opt=' '
889 cat >expected <<-EOF &&
890 HEAD
891 main
892 matching-branch
893 other/branch-in-other
894 other/main-in-other
895 matching-tag
898 cur="--opt=" &&
899 __git_refs "" "" "" "" >"$actual"
900 ) &&
901 test_cmp expected "$actual"
904 test_expect_success '__git_refs - after --opt= - full refs' '
905 cat >expected <<-EOF &&
906 refs/heads/main
907 refs/heads/matching-branch
908 refs/remotes/other/branch-in-other
909 refs/remotes/other/main-in-other
910 refs/tags/matching-tag
913 cur="--opt=refs/" &&
914 __git_refs "" "" "" refs/ >"$actual"
915 ) &&
916 test_cmp expected "$actual"
919 test_expect_success '__git refs - excluding refs' '
920 cat >expected <<-EOF &&
921 ^HEAD
922 ^main
923 ^matching-branch
924 ^other/branch-in-other
925 ^other/main-in-other
926 ^matching-tag
929 cur=^ &&
930 __git_refs >"$actual"
931 ) &&
932 test_cmp expected "$actual"
935 test_expect_success '__git refs - excluding full refs' '
936 cat >expected <<-EOF &&
937 ^refs/heads/main
938 ^refs/heads/matching-branch
939 ^refs/remotes/other/branch-in-other
940 ^refs/remotes/other/main-in-other
941 ^refs/tags/matching-tag
944 cur=^refs/ &&
945 __git_refs >"$actual"
946 ) &&
947 test_cmp expected "$actual"
950 test_expect_success 'setup for filtering matching refs' '
951 git branch matching/branch &&
952 git tag matching/tag &&
953 git -C otherrepo branch matching/branch-in-other &&
954 git fetch --no-tags other &&
955 rm -f .git/FETCH_HEAD
958 test_expect_success '__git_refs - do not filter refs unless told so' '
959 cat >expected <<-EOF &&
960 HEAD
961 main
962 matching-branch
963 matching/branch
964 other/branch-in-other
965 other/main-in-other
966 other/matching/branch-in-other
967 matching-tag
968 matching/tag
971 cur=main &&
972 __git_refs >"$actual"
973 ) &&
974 test_cmp expected "$actual"
977 test_expect_success '__git_refs - only matching refs' '
978 cat >expected <<-EOF &&
979 matching-branch
980 matching/branch
981 matching-tag
982 matching/tag
985 cur=mat &&
986 __git_refs "" "" "" "$cur" >"$actual"
987 ) &&
988 test_cmp expected "$actual"
991 test_expect_success '__git_refs - only matching refs - full refs' '
992 cat >expected <<-EOF &&
993 refs/heads/matching-branch
994 refs/heads/matching/branch
997 cur=refs/heads/mat &&
998 __git_refs "" "" "" "$cur" >"$actual"
999 ) &&
1000 test_cmp expected "$actual"
1003 test_expect_success '__git_refs - only matching refs - remote on local file system' '
1004 cat >expected <<-EOF &&
1005 main-in-other
1006 matching/branch-in-other
1009 cur=ma &&
1010 __git_refs otherrepo "" "" "$cur" >"$actual"
1011 ) &&
1012 test_cmp expected "$actual"
1015 test_expect_success '__git_refs - only matching refs - configured remote' '
1016 cat >expected <<-EOF &&
1017 main-in-other
1018 matching/branch-in-other
1021 cur=ma &&
1022 __git_refs other "" "" "$cur" >"$actual"
1023 ) &&
1024 test_cmp expected "$actual"
1027 test_expect_success '__git_refs - only matching refs - remote - full refs' '
1028 cat >expected <<-EOF &&
1029 refs/heads/main-in-other
1030 refs/heads/matching/branch-in-other
1033 cur=refs/heads/ma &&
1034 __git_refs other "" "" "$cur" >"$actual"
1035 ) &&
1036 test_cmp expected "$actual"
1039 test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
1040 cat >expected <<-EOF &&
1041 matching-branch
1042 matching/branch
1043 matching-tag
1044 matching/tag
1045 matching/branch-in-other
1047 for remote_ref in refs/remotes/other/ambiguous \
1048 refs/remotes/remote/ambiguous \
1049 refs/remotes/remote/branch-in-remote
1051 git update-ref $remote_ref main &&
1052 test_when_finished "git update-ref -d $remote_ref" || return 1
1053 done &&
1055 cur=mat &&
1056 __git_refs "" 1 "" "$cur" >"$actual"
1057 ) &&
1058 test_cmp expected "$actual"
1061 test_expect_success 'teardown after filtering matching refs' '
1062 git branch -d matching/branch &&
1063 git tag -d matching/tag &&
1064 git update-ref -d refs/remotes/other/matching/branch-in-other &&
1065 git -C otherrepo branch -D matching/branch-in-other
1068 test_expect_success '__git_refs - for-each-ref format specifiers in prefix' '
1069 cat >expected <<-EOF &&
1070 evil-%%-%42-%(refname)..main
1073 cur="evil-%%-%42-%(refname)..mai" &&
1074 __git_refs "" "" "evil-%%-%42-%(refname).." mai >"$actual"
1075 ) &&
1076 test_cmp expected "$actual"
1079 test_expect_success '__git_complete_refs - simple' '
1080 sed -e "s/Z$//" >expected <<-EOF &&
1081 HEAD Z
1082 main Z
1083 matching-branch Z
1084 other/branch-in-other Z
1085 other/main-in-other Z
1086 matching-tag Z
1089 cur= &&
1090 __git_complete_refs &&
1091 print_comp
1092 ) &&
1093 test_cmp expected out
1096 test_expect_success '__git_complete_refs - matching' '
1097 sed -e "s/Z$//" >expected <<-EOF &&
1098 matching-branch Z
1099 matching-tag Z
1102 cur=mat &&
1103 __git_complete_refs &&
1104 print_comp
1105 ) &&
1106 test_cmp expected out
1109 test_expect_success '__git_complete_refs - remote' '
1110 sed -e "s/Z$//" >expected <<-EOF &&
1111 HEAD Z
1112 branch-in-other Z
1113 main-in-other Z
1116 cur= &&
1117 __git_complete_refs --remote=other &&
1118 print_comp
1119 ) &&
1120 test_cmp expected out
1123 test_expect_success '__git_complete_refs - track' '
1124 sed -e "s/Z$//" >expected <<-EOF &&
1125 HEAD Z
1126 main Z
1127 matching-branch Z
1128 other/branch-in-other Z
1129 other/main-in-other Z
1130 matching-tag Z
1131 branch-in-other Z
1132 main-in-other Z
1135 cur= &&
1136 __git_complete_refs --track &&
1137 print_comp
1138 ) &&
1139 test_cmp expected out
1142 test_expect_success '__git_complete_refs - current word' '
1143 sed -e "s/Z$//" >expected <<-EOF &&
1144 matching-branch Z
1145 matching-tag Z
1148 cur="--option=mat" &&
1149 __git_complete_refs --cur="${cur#*=}" &&
1150 print_comp
1151 ) &&
1152 test_cmp expected out
1155 test_expect_success '__git_complete_refs - prefix' '
1156 sed -e "s/Z$//" >expected <<-EOF &&
1157 v1.0..matching-branch Z
1158 v1.0..matching-tag Z
1161 cur=v1.0..mat &&
1162 __git_complete_refs --pfx=v1.0.. --cur=mat &&
1163 print_comp
1164 ) &&
1165 test_cmp expected out
1168 test_expect_success '__git_complete_refs - suffix' '
1169 cat >expected <<-EOF &&
1170 HEAD.
1171 main.
1172 matching-branch.
1173 other/branch-in-other.
1174 other/main-in-other.
1175 matching-tag.
1178 cur= &&
1179 __git_complete_refs --sfx=. &&
1180 print_comp
1181 ) &&
1182 test_cmp expected out
1185 test_expect_success '__git_complete_fetch_refspecs - simple' '
1186 sed -e "s/Z$//" >expected <<-EOF &&
1187 HEAD:HEAD Z
1188 branch-in-other:branch-in-other Z
1189 main-in-other:main-in-other Z
1192 cur= &&
1193 __git_complete_fetch_refspecs other &&
1194 print_comp
1195 ) &&
1196 test_cmp expected out
1199 test_expect_success '__git_complete_fetch_refspecs - matching' '
1200 sed -e "s/Z$//" >expected <<-EOF &&
1201 branch-in-other:branch-in-other Z
1204 cur=br &&
1205 __git_complete_fetch_refspecs other "" br &&
1206 print_comp
1207 ) &&
1208 test_cmp expected out
1211 test_expect_success '__git_complete_fetch_refspecs - prefix' '
1212 sed -e "s/Z$//" >expected <<-EOF &&
1213 +HEAD:HEAD Z
1214 +branch-in-other:branch-in-other Z
1215 +main-in-other:main-in-other Z
1218 cur="+" &&
1219 __git_complete_fetch_refspecs other "+" "" &&
1220 print_comp
1221 ) &&
1222 test_cmp expected out
1225 test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
1226 sed -e "s/Z$//" >expected <<-EOF &&
1227 refs/heads/branch-in-other:refs/heads/branch-in-other Z
1228 refs/heads/main-in-other:refs/heads/main-in-other Z
1229 refs/tags/tag-in-other:refs/tags/tag-in-other Z
1232 cur=refs/ &&
1233 __git_complete_fetch_refspecs other "" refs/ &&
1234 print_comp
1235 ) &&
1236 test_cmp expected out
1239 test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
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 switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
1254 test_completion "git switch " <<-\EOF
1255 branch-in-other Z
1256 main Z
1257 main-in-other Z
1258 matching-branch Z
1262 test_expect_success 'git checkout - completes refs and unique remote branches for DWIM' '
1263 test_completion "git checkout " <<-\EOF
1264 HEAD Z
1265 branch-in-other Z
1266 main Z
1267 main-in-other Z
1268 matching-branch Z
1269 matching-tag Z
1270 other/branch-in-other Z
1271 other/main-in-other Z
1275 test_expect_success 'git switch - with --no-guess, complete only local branches' '
1276 test_completion "git switch --no-guess " <<-\EOF
1277 main Z
1278 matching-branch Z
1282 test_expect_success 'git switch - with GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete only local branches' '
1283 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch " <<-\EOF
1284 main Z
1285 matching-branch Z
1289 test_expect_success 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete local branches and unique remote names for DWIM logic' '
1290 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch --guess " <<-\EOF
1291 branch-in-other Z
1292 main Z
1293 main-in-other Z
1294 matching-branch Z
1298 test_expect_success 'git switch - a later --guess overrides previous --no-guess, complete local and remote unique branches for DWIM' '
1299 test_completion "git switch --no-guess --guess " <<-\EOF
1300 branch-in-other Z
1301 main Z
1302 main-in-other Z
1303 matching-branch Z
1307 test_expect_success 'git switch - a later --no-guess overrides previous --guess, complete only local branches' '
1308 test_completion "git switch --guess --no-guess " <<-\EOF
1309 main Z
1310 matching-branch Z
1314 test_expect_success 'git checkout - with GIT_COMPLETION_NO_GUESS=1 only completes refs' '
1315 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout " <<-\EOF
1316 HEAD Z
1317 main Z
1318 matching-branch Z
1319 matching-tag Z
1320 other/branch-in-other Z
1321 other/main-in-other Z
1325 test_expect_success 'git checkout - --guess overrides GIT_COMPLETION_NO_GUESS=1, complete refs and unique remote branches for DWIM' '
1326 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout --guess " <<-\EOF
1327 HEAD Z
1328 branch-in-other Z
1329 main Z
1330 main-in-other Z
1331 matching-branch Z
1332 matching-tag Z
1333 other/branch-in-other Z
1334 other/main-in-other Z
1338 test_expect_success 'git checkout - with --no-guess, only completes refs' '
1339 test_completion "git checkout --no-guess " <<-\EOF
1340 HEAD Z
1341 main Z
1342 matching-branch Z
1343 matching-tag Z
1344 other/branch-in-other Z
1345 other/main-in-other Z
1349 test_expect_success 'git checkout - a later --guess overrides previous --no-guess, complete refs and unique remote branches for DWIM' '
1350 test_completion "git checkout --no-guess --guess " <<-\EOF
1351 HEAD Z
1352 branch-in-other Z
1353 main Z
1354 main-in-other Z
1355 matching-branch Z
1356 matching-tag Z
1357 other/branch-in-other Z
1358 other/main-in-other Z
1362 test_expect_success 'git checkout - a later --no-guess overrides previous --guess, complete only refs' '
1363 test_completion "git checkout --guess --no-guess " <<-\EOF
1364 HEAD Z
1365 main Z
1366 matching-branch Z
1367 matching-tag Z
1368 other/branch-in-other Z
1369 other/main-in-other Z
1373 test_expect_success 'git checkout - with checkout.guess = false, only completes refs' '
1374 test_config checkout.guess false &&
1375 test_completion "git checkout " <<-\EOF
1376 HEAD Z
1377 main Z
1378 matching-branch Z
1379 matching-tag Z
1380 other/branch-in-other Z
1381 other/main-in-other Z
1385 test_expect_success 'git checkout - with checkout.guess = true, completes refs and unique remote branches for DWIM' '
1386 test_config checkout.guess true &&
1387 test_completion "git checkout " <<-\EOF
1388 HEAD Z
1389 branch-in-other Z
1390 main Z
1391 main-in-other Z
1392 matching-branch Z
1393 matching-tag Z
1394 other/branch-in-other Z
1395 other/main-in-other Z
1399 test_expect_success 'git checkout - a later --guess overrides previous checkout.guess = false, complete refs and unique remote branches for DWIM' '
1400 test_config checkout.guess false &&
1401 test_completion "git checkout --guess " <<-\EOF
1402 HEAD Z
1403 branch-in-other Z
1404 main Z
1405 main-in-other Z
1406 matching-branch Z
1407 matching-tag Z
1408 other/branch-in-other Z
1409 other/main-in-other Z
1413 test_expect_success 'git checkout - a later --no-guess overrides previous checkout.guess = true, complete only refs' '
1414 test_config checkout.guess true &&
1415 test_completion "git checkout --no-guess " <<-\EOF
1416 HEAD Z
1417 main Z
1418 matching-branch Z
1419 matching-tag Z
1420 other/branch-in-other Z
1421 other/main-in-other Z
1425 test_expect_success 'git switch - with --detach, complete all references' '
1426 test_completion "git switch --detach " <<-\EOF
1427 HEAD Z
1428 main Z
1429 matching-branch Z
1430 matching-tag Z
1431 other/branch-in-other Z
1432 other/main-in-other Z
1436 test_expect_success 'git checkout - with --detach, complete only references' '
1437 test_completion "git checkout --detach " <<-\EOF
1438 HEAD Z
1439 main 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 -d, complete all references' '
1448 test_completion "git switch -d " <<-\EOF
1449 HEAD Z
1450 main Z
1451 matching-branch Z
1452 matching-tag Z
1453 other/branch-in-other Z
1454 other/main-in-other Z
1458 test_expect_success 'git checkout - with -d, complete only references' '
1459 test_completion "git checkout -d " <<-\EOF
1460 HEAD Z
1461 main Z
1462 matching-branch Z
1463 matching-tag Z
1464 other/branch-in-other Z
1465 other/main-in-other Z
1469 test_expect_success 'git switch - with --track, complete only remote branches' '
1470 test_completion "git switch --track " <<-\EOF
1471 other/branch-in-other Z
1472 other/main-in-other Z
1476 test_expect_success 'git checkout - with --track, complete only remote branches' '
1477 test_completion "git checkout --track " <<-\EOF
1478 other/branch-in-other Z
1479 other/main-in-other Z
1483 test_expect_success 'git switch - with --no-track, complete only local branch names' '
1484 test_completion "git switch --no-track " <<-\EOF
1485 main Z
1486 matching-branch Z
1490 test_expect_success 'git checkout - with --no-track, complete only local references' '
1491 test_completion "git checkout --no-track " <<-\EOF
1492 HEAD Z
1493 main Z
1494 matching-branch Z
1495 matching-tag Z
1496 other/branch-in-other Z
1497 other/main-in-other Z
1501 test_expect_success 'git switch - with -c, complete all references' '
1502 test_completion "git switch -c new-branch " <<-\EOF
1503 HEAD Z
1504 main Z
1505 matching-branch Z
1506 matching-tag Z
1507 other/branch-in-other Z
1508 other/main-in-other Z
1512 test_expect_success 'git switch - with -C, complete all references' '
1513 test_completion "git switch -C new-branch " <<-\EOF
1514 HEAD Z
1515 main Z
1516 matching-branch Z
1517 matching-tag Z
1518 other/branch-in-other Z
1519 other/main-in-other Z
1523 test_expect_success 'git switch - with -c and --track, complete all references' '
1524 test_completion "git switch -c new-branch --track " <<-EOF
1525 HEAD Z
1526 main 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 switch - with -C and --track, complete all references' '
1535 test_completion "git switch -C new-branch --track " <<-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 switch - with -c and --no-track, complete all references' '
1546 test_completion "git switch -c new-branch --no-track " <<-\EOF
1547 HEAD Z
1548 main Z
1549 matching-branch Z
1550 matching-tag Z
1551 other/branch-in-other Z
1552 other/main-in-other Z
1556 test_expect_success 'git switch - with -C and --no-track, complete all references' '
1557 test_completion "git switch -C new-branch --no-track " <<-\EOF
1558 HEAD Z
1559 main Z
1560 matching-branch Z
1561 matching-tag Z
1562 other/branch-in-other Z
1563 other/main-in-other Z
1567 test_expect_success 'git checkout - with -b, complete all references' '
1568 test_completion "git checkout -b new-branch " <<-\EOF
1569 HEAD Z
1570 main Z
1571 matching-branch Z
1572 matching-tag Z
1573 other/branch-in-other Z
1574 other/main-in-other Z
1578 test_expect_success 'git checkout - with -B, complete all references' '
1579 test_completion "git checkout -B new-branch " <<-\EOF
1580 HEAD Z
1581 main Z
1582 matching-branch Z
1583 matching-tag Z
1584 other/branch-in-other Z
1585 other/main-in-other Z
1589 test_expect_success 'git checkout - with -b and --track, complete all references' '
1590 test_completion "git checkout -b new-branch --track " <<-EOF
1591 HEAD Z
1592 main Z
1593 matching-branch Z
1594 matching-tag Z
1595 other/branch-in-other Z
1596 other/main-in-other Z
1600 test_expect_success 'git checkout - with -B and --track, complete all references' '
1601 test_completion "git checkout -B new-branch --track " <<-EOF
1602 HEAD Z
1603 main Z
1604 matching-branch Z
1605 matching-tag Z
1606 other/branch-in-other Z
1607 other/main-in-other Z
1611 test_expect_success 'git checkout - with -b and --no-track, complete all references' '
1612 test_completion "git checkout -b new-branch --no-track " <<-\EOF
1613 HEAD Z
1614 main Z
1615 matching-branch Z
1616 matching-tag Z
1617 other/branch-in-other Z
1618 other/main-in-other Z
1622 test_expect_success 'git checkout - with -B and --no-track, complete all references' '
1623 test_completion "git checkout -B new-branch --no-track " <<-\EOF
1624 HEAD Z
1625 main Z
1626 matching-branch Z
1627 matching-tag Z
1628 other/branch-in-other Z
1629 other/main-in-other Z
1633 test_expect_success 'git switch - for -c, complete local branches and unique remote branches' '
1634 test_completion "git switch -c " <<-\EOF
1635 branch-in-other Z
1636 main Z
1637 main-in-other Z
1638 matching-branch Z
1642 test_expect_success 'git switch - for -C, complete local branches and unique remote branches' '
1643 test_completion "git switch -C " <<-\EOF
1644 branch-in-other Z
1645 main Z
1646 main-in-other Z
1647 matching-branch Z
1651 test_expect_success 'git switch - for -c with --no-guess, complete local branches only' '
1652 test_completion "git switch --no-guess -c " <<-\EOF
1653 main Z
1654 matching-branch Z
1658 test_expect_success 'git switch - for -C with --no-guess, complete local branches only' '
1659 test_completion "git switch --no-guess -C " <<-\EOF
1660 main Z
1661 matching-branch Z
1665 test_expect_success 'git switch - for -c with --no-track, complete local branches only' '
1666 test_completion "git switch --no-track -c " <<-\EOF
1667 main Z
1668 matching-branch Z
1672 test_expect_success 'git switch - for -C with --no-track, complete local branches only' '
1673 test_completion "git switch --no-track -C " <<-\EOF
1674 main Z
1675 matching-branch Z
1679 test_expect_success 'git checkout - for -b, complete local branches and unique remote branches' '
1680 test_completion "git checkout -b " <<-\EOF
1681 branch-in-other Z
1682 main Z
1683 main-in-other Z
1684 matching-branch Z
1688 test_expect_success 'git checkout - for -B, complete local branches and unique remote branches' '
1689 test_completion "git checkout -B " <<-\EOF
1690 branch-in-other Z
1691 main Z
1692 main-in-other Z
1693 matching-branch Z
1697 test_expect_success 'git checkout - for -b with --no-guess, complete local branches only' '
1698 test_completion "git checkout --no-guess -b " <<-\EOF
1699 main Z
1700 matching-branch Z
1704 test_expect_success 'git checkout - for -B with --no-guess, complete local branches only' '
1705 test_completion "git checkout --no-guess -B " <<-\EOF
1706 main Z
1707 matching-branch Z
1711 test_expect_success 'git checkout - for -b with --no-track, complete local branches only' '
1712 test_completion "git checkout --no-track -b " <<-\EOF
1713 main Z
1714 matching-branch Z
1718 test_expect_success 'git checkout - for -B with --no-track, complete local branches only' '
1719 test_completion "git checkout --no-track -B " <<-\EOF
1720 main Z
1721 matching-branch Z
1725 test_expect_success 'git switch - with --orphan completes local branch names and unique remote branch names' '
1726 test_completion "git switch --orphan " <<-\EOF
1727 branch-in-other Z
1728 main Z
1729 main-in-other Z
1730 matching-branch Z
1734 test_expect_success 'git switch - --orphan with branch already provided completes nothing else' '
1735 test_completion "git switch --orphan main " <<-\EOF
1740 test_expect_success 'git checkout - with --orphan completes local branch names and unique remote branch names' '
1741 test_completion "git checkout --orphan " <<-\EOF
1742 branch-in-other Z
1743 main Z
1744 main-in-other Z
1745 matching-branch Z
1749 test_expect_success 'git checkout - --orphan with branch already provided completes local refs for a start-point' '
1750 test_completion "git checkout --orphan main " <<-\EOF
1751 HEAD Z
1752 main Z
1753 matching-branch Z
1754 matching-tag Z
1755 other/branch-in-other Z
1756 other/main-in-other Z
1760 test_expect_success 'teardown after ref completion' '
1761 git branch -d matching-branch &&
1762 git tag -d matching-tag &&
1763 git remote remove other
1767 test_path_completion ()
1769 test $# = 2 || BUG "not 2 parameters to test_path_completion"
1771 local cur="$1" expected="$2"
1772 echo "$expected" >expected &&
1774 # In the following tests calling this function we only
1775 # care about how __git_complete_index_file() deals with
1776 # unusual characters in path names. By requesting only
1777 # untracked files we do not have to bother adding any
1778 # paths to the index in those tests.
1779 __git_complete_index_file --others &&
1780 print_comp
1781 ) &&
1782 test_cmp expected out
1785 test_expect_success 'setup for path completion tests' '
1786 mkdir simple-dir \
1787 "spaces in dir" \
1788 árvíztűrő &&
1789 touch simple-dir/simple-file \
1790 "spaces in dir/spaces in file" \
1791 "árvíztűrő/Сайн яваарай" &&
1792 if test_have_prereq !MINGW &&
1793 mkdir BS\\dir \
1794 '$'separators\034in\035dir'' &&
1795 touch BS\\dir/DQ\"file \
1796 '$'separators\034in\035dir/sep\036in\037file''
1797 then
1798 test_set_prereq FUNNIERNAMES
1799 else
1800 rm -rf BS\\dir '$'separators\034in\035dir''
1804 test_expect_success '__git_complete_index_file - simple' '
1805 test_path_completion simple simple-dir && # Bash is supposed to
1806 # add the trailing /.
1807 test_path_completion simple-dir/simple simple-dir/simple-file
1810 test_expect_success \
1811 '__git_complete_index_file - escaped characters on cmdline' '
1812 test_path_completion spac "spaces in dir" && # Bash will turn this
1813 # into "spaces\ in\ dir"
1814 test_path_completion "spaces\\ i" \
1815 "spaces in dir" &&
1816 test_path_completion "spaces\\ in\\ dir/s" \
1817 "spaces in dir/spaces in file" &&
1818 test_path_completion "spaces\\ in\\ dir/spaces\\ i" \
1819 "spaces in dir/spaces in file"
1822 test_expect_success \
1823 '__git_complete_index_file - quoted characters on cmdline' '
1824 # Testing with an opening but without a corresponding closing
1825 # double quote is important.
1826 test_path_completion \"spac "spaces in dir" &&
1827 test_path_completion "\"spaces i" \
1828 "spaces in dir" &&
1829 test_path_completion "\"spaces in dir/s" \
1830 "spaces in dir/spaces in file" &&
1831 test_path_completion "\"spaces in dir/spaces i" \
1832 "spaces in dir/spaces in file"
1835 test_expect_success '__git_complete_index_file - UTF-8 in ls-files output' '
1836 test_path_completion á árvíztűrő &&
1837 test_path_completion árvíztűrő/С "árvíztűrő/Сайн яваарай"
1840 test_expect_success FUNNIERNAMES \
1841 '__git_complete_index_file - C-style escapes in ls-files output' '
1842 test_path_completion BS \
1843 BS\\dir &&
1844 test_path_completion BS\\\\d \
1845 BS\\dir &&
1846 test_path_completion BS\\\\dir/DQ \
1847 BS\\dir/DQ\"file &&
1848 test_path_completion BS\\\\dir/DQ\\\"f \
1849 BS\\dir/DQ\"file
1852 test_expect_success FUNNIERNAMES \
1853 '__git_complete_index_file - \nnn-escaped characters in ls-files output' '
1854 test_path_completion sep '$'separators\034in\035dir'' &&
1855 test_path_completion '$'separators\034i'' \
1856 '$'separators\034in\035dir'' &&
1857 test_path_completion '$'separators\034in\035dir/sep'' \
1858 '$'separators\034in\035dir/sep\036in\037file'' &&
1859 test_path_completion '$'separators\034in\035dir/sep\036i'' \
1860 '$'separators\034in\035dir/sep\036in\037file''
1863 test_expect_success FUNNYNAMES \
1864 '__git_complete_index_file - removing repeated quoted path components' '
1865 test_when_finished rm -r repeated-quoted &&
1866 mkdir repeated-quoted && # A directory whose name in itself
1867 # would not be quoted ...
1868 >repeated-quoted/0-file &&
1869 >repeated-quoted/1\"file && # ... but here the file makes the
1870 # dirname quoted ...
1871 >repeated-quoted/2-file &&
1872 >repeated-quoted/3\"file && # ... and here, too.
1874 # Still, we shold only list the directory name only once.
1875 test_path_completion repeated repeated-quoted
1878 test_expect_success 'teardown after path completion tests' '
1879 rm -rf simple-dir "spaces in dir" árvíztűrő \
1880 BS\\dir '$'separators\034in\035dir''
1883 test_expect_success '__git_find_on_cmdline - single match' '
1884 echo list >expect &&
1886 words=(git command --opt list) &&
1887 cword=${#words[@]} &&
1888 __git_cmd_idx=1 &&
1889 __git_find_on_cmdline "add list remove" >actual
1890 ) &&
1891 test_cmp expect actual
1894 test_expect_success '__git_find_on_cmdline - multiple matches' '
1895 echo remove >expect &&
1897 words=(git command -o --opt remove list add) &&
1898 cword=${#words[@]} &&
1899 __git_cmd_idx=1 &&
1900 __git_find_on_cmdline "add list remove" >actual
1901 ) &&
1902 test_cmp expect actual
1905 test_expect_success '__git_find_on_cmdline - no match' '
1907 words=(git command --opt branch) &&
1908 cword=${#words[@]} &&
1909 __git_cmd_idx=1 &&
1910 __git_find_on_cmdline "add list remove" >actual
1911 ) &&
1912 test_must_be_empty actual
1915 test_expect_success '__git_find_on_cmdline - single match with index' '
1916 echo "3 list" >expect &&
1918 words=(git command --opt list) &&
1919 cword=${#words[@]} &&
1920 __git_cmd_idx=1 &&
1921 __git_find_on_cmdline --show-idx "add list remove" >actual
1922 ) &&
1923 test_cmp expect actual
1926 test_expect_success '__git_find_on_cmdline - multiple matches with index' '
1927 echo "4 remove" >expect &&
1929 words=(git command -o --opt remove list add) &&
1930 cword=${#words[@]} &&
1931 __git_cmd_idx=1 &&
1932 __git_find_on_cmdline --show-idx "add list remove" >actual
1933 ) &&
1934 test_cmp expect actual
1937 test_expect_success '__git_find_on_cmdline - no match with index' '
1939 words=(git command --opt branch) &&
1940 cword=${#words[@]} &&
1941 __git_cmd_idx=1 &&
1942 __git_find_on_cmdline --show-idx "add list remove" >actual
1943 ) &&
1944 test_must_be_empty actual
1947 test_expect_success '__git_find_on_cmdline - ignores matches before command with index' '
1948 echo "6 remove" >expect &&
1950 words=(git -C remove command -o --opt remove list add) &&
1951 cword=${#words[@]} &&
1952 __git_cmd_idx=3 &&
1953 __git_find_on_cmdline --show-idx "add list remove" >actual
1954 ) &&
1955 test_cmp expect actual
1958 test_expect_success '__git_get_config_variables' '
1959 cat >expect <<-EOF &&
1960 name-1
1961 name-2
1963 test_config interesting.name-1 good &&
1964 test_config interesting.name-2 good &&
1965 test_config subsection.interesting.name-3 bad &&
1966 __git_get_config_variables interesting >actual &&
1967 test_cmp expect actual
1970 test_expect_success '__git_pretty_aliases' '
1971 cat >expect <<-EOF &&
1972 author
1973 hash
1975 test_config pretty.author "%an %ae" &&
1976 test_config pretty.hash %H &&
1977 __git_pretty_aliases >actual &&
1978 test_cmp expect actual
1981 test_expect_success 'basic' '
1982 run_completion "git " &&
1983 # built-in
1984 grep -q "^add \$" out &&
1985 # script
1986 grep -q "^rebase \$" out &&
1987 # plumbing
1988 ! grep -q "^ls-files \$" out &&
1990 run_completion "git r" &&
1991 ! grep -q -v "^r" out
1994 test_expect_success 'double dash "git" itself' '
1995 test_completion "git --" <<-\EOF
1996 --paginate Z
1997 --no-pager Z
1998 --git-dir=
1999 --bare Z
2000 --version Z
2001 --exec-path Z
2002 --exec-path=
2003 --html-path Z
2004 --man-path Z
2005 --info-path Z
2006 --work-tree=
2007 --namespace=
2008 --no-replace-objects Z
2009 --help Z
2013 test_expect_success 'double dash "git checkout"' '
2014 test_completion "git checkout --" <<-\EOF
2015 --quiet Z
2016 --detach Z
2017 --track Z
2018 --orphan=Z
2019 --ours Z
2020 --theirs Z
2021 --merge Z
2022 --conflict=Z
2023 --patch Z
2024 --ignore-skip-worktree-bits Z
2025 --ignore-other-worktrees Z
2026 --recurse-submodules Z
2027 --progress Z
2028 --guess Z
2029 --no-guess Z
2030 --no-... Z
2031 --overlay Z
2032 --pathspec-file-nul Z
2033 --pathspec-from-file=Z
2037 test_expect_success 'general options' '
2038 test_completion "git --ver" "--version " &&
2039 test_completion "git --hel" "--help " &&
2040 test_completion "git --exe" <<-\EOF &&
2041 --exec-path Z
2042 --exec-path=
2044 test_completion "git --htm" "--html-path " &&
2045 test_completion "git --pag" "--paginate " &&
2046 test_completion "git --no-p" "--no-pager " &&
2047 test_completion "git --git" "--git-dir=" &&
2048 test_completion "git --wor" "--work-tree=" &&
2049 test_completion "git --nam" "--namespace=" &&
2050 test_completion "git --bar" "--bare " &&
2051 test_completion "git --inf" "--info-path " &&
2052 test_completion "git --no-r" "--no-replace-objects "
2055 test_expect_success 'general options plus command' '
2056 test_completion "git --version check" "checkout " &&
2057 test_completion "git --paginate check" "checkout " &&
2058 test_completion "git --git-dir=foo check" "checkout " &&
2059 test_completion "git --bare check" "checkout " &&
2060 test_completion "git --exec-path=foo check" "checkout " &&
2061 test_completion "git --html-path check" "checkout " &&
2062 test_completion "git --no-pager check" "checkout " &&
2063 test_completion "git --work-tree=foo check" "checkout " &&
2064 test_completion "git --namespace=foo check" "checkout " &&
2065 test_completion "git --paginate check" "checkout " &&
2066 test_completion "git --info-path check" "checkout " &&
2067 test_completion "git --no-replace-objects check" "checkout " &&
2068 test_completion "git --git-dir some/path check" "checkout " &&
2069 test_completion "git -c conf.var=value check" "checkout " &&
2070 test_completion "git -C some/path check" "checkout " &&
2071 test_completion "git --work-tree some/path check" "checkout " &&
2072 test_completion "git --namespace name/space check" "checkout "
2075 test_expect_success 'git --help completion' '
2076 test_completion "git --help ad" "add " &&
2077 test_completion "git --help core" "core-tutorial "
2080 test_expect_success 'completion.commands removes multiple commands' '
2081 test_config completion.commands "-cherry -mergetool" &&
2082 git --list-cmds=list-mainporcelain,list-complete,config >out &&
2083 ! grep -E "^(cherry|mergetool)$" out
2086 test_expect_success 'setup for integration tests' '
2087 echo content >file1 &&
2088 echo more >file2 &&
2089 git add file1 file2 &&
2090 git commit -m one &&
2091 git branch mybranch &&
2092 git tag mytag
2095 test_expect_success 'checkout completes ref names' '
2096 test_completion "git checkout m" <<-\EOF
2097 main Z
2098 mybranch Z
2099 mytag Z
2103 test_expect_success 'git -C <path> checkout uses the right repo' '
2104 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
2105 branch-in-other Z
2109 test_expect_success 'show completes all refs' '
2110 test_completion "git show m" <<-\EOF
2111 main Z
2112 mybranch Z
2113 mytag Z
2117 test_expect_success '<ref>: completes paths' '
2118 test_completion "git show mytag:f" <<-\EOF
2119 file1Z
2120 file2Z
2124 test_expect_success 'complete tree filename with spaces' '
2125 echo content >"name with spaces" &&
2126 git add "name with spaces" &&
2127 git commit -m spaces &&
2128 test_completion "git show HEAD:nam" <<-\EOF
2129 name with spacesZ
2133 test_expect_success 'complete tree filename with metacharacters' '
2134 echo content >"name with \${meta}" &&
2135 git add "name with \${meta}" &&
2136 git commit -m meta &&
2137 test_completion "git show HEAD:nam" <<-\EOF
2138 name with ${meta}Z
2139 name with spacesZ
2143 test_expect_success PERL 'send-email' '
2144 test_completion "git send-email --cov" <<-\EOF &&
2145 --cover-from-description=Z
2146 --cover-letter Z
2148 test_completion "git send-email --val" <<-\EOF &&
2149 --validate Z
2151 test_completion "git send-email ma" "main "
2154 test_expect_success 'complete files' '
2155 git init tmp && cd tmp &&
2156 test_when_finished "cd .. && rm -rf tmp" &&
2158 echo "expected" > .gitignore &&
2159 echo "out" >> .gitignore &&
2160 echo "out_sorted" >> .gitignore &&
2162 git add .gitignore &&
2163 test_completion "git commit " ".gitignore" &&
2165 git commit -m ignore &&
2167 touch new &&
2168 test_completion "git add " "new" &&
2170 git add new &&
2171 git commit -a -m new &&
2172 test_completion "git add " "" &&
2174 git mv new modified &&
2175 echo modify > modified &&
2176 test_completion "git add " "modified" &&
2178 mkdir -p some/deep &&
2179 touch some/deep/path &&
2180 test_completion "git add some/" "some/deep" &&
2181 git clean -f some &&
2183 touch untracked &&
2185 : TODO .gitignore should not be here &&
2186 test_completion "git rm " <<-\EOF &&
2187 .gitignore
2188 modified
2191 test_completion "git clean " "untracked" &&
2193 : TODO .gitignore should not be here &&
2194 test_completion "git mv " <<-\EOF &&
2195 .gitignore
2196 modified
2199 mkdir dir &&
2200 touch dir/file-in-dir &&
2201 git add dir/file-in-dir &&
2202 git commit -m dir &&
2204 mkdir untracked-dir &&
2206 : TODO .gitignore should not be here &&
2207 test_completion "git mv modified " <<-\EOF &&
2208 .gitignore
2210 modified
2211 untracked
2212 untracked-dir
2215 test_completion "git commit " "modified" &&
2217 : TODO .gitignore should not be here &&
2218 test_completion "git ls-files " <<-\EOF &&
2219 .gitignore
2221 modified
2224 touch momified &&
2225 test_completion "git add mom" "momified"
2228 test_expect_success "simple alias" '
2229 test_config alias.co checkout &&
2230 test_completion "git co m" <<-\EOF
2231 main Z
2232 mybranch Z
2233 mytag Z
2237 test_expect_success "recursive alias" '
2238 test_config alias.co checkout &&
2239 test_config alias.cod "co --detached" &&
2240 test_completion "git cod m" <<-\EOF
2241 main Z
2242 mybranch Z
2243 mytag Z
2247 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
2248 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
2249 test_completion "git co m" <<-\EOF
2250 main Z
2251 mybranch Z
2252 mytag Z
2256 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
2257 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
2258 test_completion "git co m" <<-\EOF
2259 main Z
2260 mybranch Z
2261 mytag Z
2265 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
2266 test_config alias.co "!f() { : git checkout ; if ... } f" &&
2267 test_completion "git co m" <<-\EOF
2268 main Z
2269 mybranch Z
2270 mytag Z
2274 test_expect_success 'completion without explicit _git_xxx function' '
2275 test_completion "git version --" <<-\EOF
2276 --build-options Z
2277 --no-build-options Z
2281 test_expect_failure 'complete with tilde expansion' '
2282 git init tmp && cd tmp &&
2283 test_when_finished "cd .. && rm -rf tmp" &&
2285 touch ~/tmp/file &&
2287 test_completion "git add ~/tmp/" "~/tmp/file"
2290 test_expect_success 'setup other remote for remote reference completion' '
2291 git remote add other otherrepo &&
2292 git fetch other
2295 for flag in -d --delete
2297 test_expect_success "__git_complete_remote_or_refspec - push $flag other" '
2298 sed -e "s/Z$//" >expected <<-EOF &&
2299 main-in-other Z
2302 words=(git push '$flag' other ma) &&
2303 cword=${#words[@]} cur=${words[cword-1]} &&
2304 __git_cmd_idx=1 &&
2305 __git_complete_remote_or_refspec &&
2306 print_comp
2307 ) &&
2308 test_cmp expected out
2311 test_expect_failure "__git_complete_remote_or_refspec - push other $flag" '
2312 sed -e "s/Z$//" >expected <<-EOF &&
2313 main-in-other Z
2316 words=(git push other '$flag' ma) &&
2317 cword=${#words[@]} cur=${words[cword-1]} &&
2318 __git_cmd_idx=1 &&
2319 __git_complete_remote_or_refspec &&
2320 print_comp
2321 ) &&
2322 test_cmp expected out
2324 done
2326 test_expect_success 'git config - section' '
2327 test_completion "git config br" <<-\EOF
2328 branch.Z
2329 browser.Z
2333 test_expect_success 'git config - variable name' '
2334 test_completion "git config log.d" <<-\EOF
2335 log.date Z
2336 log.decorate Z
2337 log.diffMerges Z
2341 test_expect_success 'git config - value' '
2342 test_completion "git config color.pager " <<-\EOF
2343 false Z
2344 true Z
2348 test_expect_success 'git -c - section' '
2349 test_completion "git -c br" <<-\EOF
2350 branch.Z
2351 browser.Z
2355 test_expect_success 'git -c - variable name' '
2356 test_completion "git -c log.d" <<-\EOF
2357 log.date=Z
2358 log.decorate=Z
2359 log.diffMerges=Z
2363 test_expect_success 'git -c - value' '
2364 test_completion "git -c color.pager=" <<-\EOF
2365 false Z
2366 true Z
2370 test_expect_success 'git clone --config= - section' '
2371 test_completion "git clone --config=br" <<-\EOF
2372 branch.Z
2373 browser.Z
2377 test_expect_success 'git clone --config= - variable name' '
2378 test_completion "git clone --config=log.d" <<-\EOF
2379 log.date=Z
2380 log.decorate=Z
2381 log.diffMerges=Z
2385 test_expect_success 'git clone --config= - value' '
2386 test_completion "git clone --config=color.pager=" <<-\EOF
2387 false Z
2388 true Z
2392 test_expect_success 'options with value' '
2393 test_completion "git merge -X diff-algorithm=" <<-\EOF
2398 test_expect_success 'sourcing the completion script clears cached commands' '
2399 __git_compute_all_commands &&
2400 verbose test -n "$__git_all_commands" &&
2401 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2402 verbose test -z "$__git_all_commands"
2405 test_expect_success 'sourcing the completion script clears cached merge strategies' '
2406 __git_compute_merge_strategies &&
2407 verbose test -n "$__git_merge_strategies" &&
2408 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2409 verbose test -z "$__git_merge_strategies"
2412 test_expect_success 'sourcing the completion script clears cached --options' '
2413 __gitcomp_builtin checkout &&
2414 verbose test -n "$__gitcomp_builtin_checkout" &&
2415 __gitcomp_builtin notes_edit &&
2416 verbose test -n "$__gitcomp_builtin_notes_edit" &&
2417 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2418 verbose test -z "$__gitcomp_builtin_checkout" &&
2419 verbose test -z "$__gitcomp_builtin_notes_edit"
2422 test_expect_success 'option aliases are not shown by default' '
2423 test_completion "git clone --recurs" "--recurse-submodules "
2426 test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
2427 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2428 GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2429 test_completion "git clone --recurs" <<-\EOF
2430 --recurse-submodules Z
2431 --recursive Z
2435 test_expect_success '__git_complete' '
2436 unset -f __git_wrap__git_main &&
2438 __git_complete foo __git_main &&
2439 __git_have_func __git_wrap__git_main &&
2440 unset -f __git_wrap__git_main &&
2442 __git_complete gf _git_fetch &&
2443 __git_have_func __git_wrap_git_fetch &&
2445 __git_complete foo git &&
2446 __git_have_func __git_wrap__git_main &&
2447 unset -f __git_wrap__git_main &&
2449 __git_complete gd git_diff &&
2450 __git_have_func __git_wrap_git_diff &&
2452 test_must_fail __git_complete ga missing
2455 test_done