completion: complete 'submodule.*' config variables
[git.git] / t / t9902-completion.sh
blob23d0e71324ccf5b81315fb0579c93cca3faee2ce
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 . ./lib-bash.sh
16 complete ()
18 # do nothing
19 return 0
22 # Be careful when updating these lists:
24 # (1) The build tree may have build artifact from different branch, or
25 # the user's $PATH may have a random executable that may begin
26 # with "git-check" that are not part of the subcommands this build
27 # will ship, e.g. "check-ignore". The tests for completion for
28 # subcommand names tests how "check" is expanded; we limit the
29 # possible candidates to "checkout" and "check-attr" to make sure
30 # "check-attr", which is known by the filter function as a
31 # subcommand to be thrown out, while excluding other random files
32 # that happen to begin with "check" to avoid letting them get in
33 # the way.
35 # (2) A test makes sure that common subcommands are included in the
36 # completion for "git <TAB>", and a plumbing is excluded. "add",
37 # "rebase" and "ls-files" are listed for this.
39 GIT_TESTING_ALL_COMMAND_LIST='add checkout check-attr rebase ls-files'
40 GIT_TESTING_PORCELAIN_COMMAND_LIST='add checkout rebase'
42 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
44 # We don't need this function to actually join words or do anything special.
45 # Also, it's cleaner to avoid touching bash's internal completion variables.
46 # So let's override it with a minimal version for testing purposes.
47 _get_comp_words_by_ref ()
49 while [ $# -gt 0 ]; do
50 case "$1" in
51 cur)
52 cur=${_words[_cword]}
54 prev)
55 prev=${_words[_cword-1]}
57 words)
58 words=("${_words[@]}")
60 cword)
61 cword=$_cword
63 esac
64 shift
65 done
68 print_comp ()
70 local IFS=$'\n'
71 echo "${COMPREPLY[*]}" > out
74 run_completion ()
76 local -a COMPREPLY _words
77 local _cword
78 _words=( $1 )
79 test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
80 (( _cword = ${#_words[@]} - 1 ))
81 __git_wrap__git_main && print_comp
84 # Test high-level completion
85 # Arguments are:
86 # 1: typed text so far (cur)
87 # 2: expected completion
88 test_completion ()
90 if test $# -gt 1
91 then
92 printf '%s\n' "$2" >expected
93 else
94 sed -e 's/Z$//' |sort >expected
95 fi &&
96 run_completion "$1" >"$TRASH_DIRECTORY"/bash-completion-output 2>&1 &&
97 sort out >out_sorted &&
98 test_cmp expected out_sorted &&
99 test_must_be_empty "$TRASH_DIRECTORY"/bash-completion-output &&
100 rm "$TRASH_DIRECTORY"/bash-completion-output
103 # Test __gitcomp.
104 # The first argument is the typed text so far (cur); the rest are
105 # passed to __gitcomp. Expected output comes is read from the
106 # standard input, like test_completion().
107 test_gitcomp ()
109 local -a COMPREPLY &&
110 sed -e 's/Z$//' >expected &&
111 local cur="$1" &&
112 shift &&
113 __gitcomp "$@" &&
114 print_comp &&
115 test_cmp expected out
118 # Test __gitcomp_nl
119 # Arguments are:
120 # 1: current word (cur)
121 # -: the rest are passed to __gitcomp_nl
122 test_gitcomp_nl ()
124 local -a COMPREPLY &&
125 sed -e 's/Z$//' >expected &&
126 local cur="$1" &&
127 shift &&
128 __gitcomp_nl "$@" &&
129 print_comp &&
130 test_cmp expected out
133 invalid_variable_name='${foo.bar}'
135 actual="$TRASH_DIRECTORY/actual"
137 if test_have_prereq MINGW
138 then
139 ROOT="$(pwd -W)"
140 else
141 ROOT="$(pwd)"
144 test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
145 mkdir -p subdir/subsubdir &&
146 mkdir -p non-repo &&
147 git init -b main otherrepo
150 test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '
151 echo "$ROOT/otherrepo/.git" >expected &&
153 __git_dir="$ROOT/otherrepo/.git" &&
154 __git_find_repo_path &&
155 echo "$__git_repo_path" >"$actual"
156 ) &&
157 test_cmp expected "$actual"
160 test_expect_success '__git_find_repo_path - .git directory in cwd' '
161 echo ".git" >expected &&
163 __git_find_repo_path &&
164 echo "$__git_repo_path" >"$actual"
165 ) &&
166 test_cmp expected "$actual"
169 test_expect_success '__git_find_repo_path - .git directory in parent' '
170 echo "$ROOT/.git" >expected &&
172 cd subdir/subsubdir &&
173 __git_find_repo_path &&
174 echo "$__git_repo_path" >"$actual"
175 ) &&
176 test_cmp expected "$actual"
179 test_expect_success '__git_find_repo_path - cwd is a .git directory' '
180 echo "." >expected &&
182 cd .git &&
183 __git_find_repo_path &&
184 echo "$__git_repo_path" >"$actual"
185 ) &&
186 test_cmp expected "$actual"
189 test_expect_success '__git_find_repo_path - parent is a .git directory' '
190 echo "$ROOT/.git" >expected &&
192 cd .git/objects &&
193 __git_find_repo_path &&
194 echo "$__git_repo_path" >"$actual"
195 ) &&
196 test_cmp expected "$actual"
199 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
200 echo "$ROOT/otherrepo/.git" >expected &&
202 GIT_DIR="$ROOT/otherrepo/.git" &&
203 export GIT_DIR &&
204 __git_find_repo_path &&
205 echo "$__git_repo_path" >"$actual"
206 ) &&
207 test_cmp expected "$actual"
210 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
211 echo "$ROOT/otherrepo/.git" >expected &&
213 GIT_DIR="$ROOT/otherrepo/.git" &&
214 export GIT_DIR &&
215 cd subdir &&
216 __git_find_repo_path &&
217 echo "$__git_repo_path" >"$actual"
218 ) &&
219 test_cmp expected "$actual"
222 test_expect_success '__git_find_repo_path - from command line while "git -C"' '
223 echo "$ROOT/.git" >expected &&
225 __git_dir="$ROOT/.git" &&
226 __git_C_args=(-C otherrepo) &&
227 __git_find_repo_path &&
228 echo "$__git_repo_path" >"$actual"
229 ) &&
230 test_cmp expected "$actual"
233 test_expect_success '__git_find_repo_path - relative dir from command line and "git -C"' '
234 echo "$ROOT/otherrepo/.git" >expected &&
236 cd subdir &&
237 __git_dir="otherrepo/.git" &&
238 __git_C_args=(-C ..) &&
239 __git_find_repo_path &&
240 echo "$__git_repo_path" >"$actual"
241 ) &&
242 test_cmp expected "$actual"
245 test_expect_success '__git_find_repo_path - $GIT_DIR set while "git -C"' '
246 echo "$ROOT/.git" >expected &&
248 GIT_DIR="$ROOT/.git" &&
249 export GIT_DIR &&
250 __git_C_args=(-C otherrepo) &&
251 __git_find_repo_path &&
252 echo "$__git_repo_path" >"$actual"
253 ) &&
254 test_cmp expected "$actual"
257 test_expect_success '__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
258 echo "$ROOT/otherrepo/.git" >expected &&
260 cd subdir &&
261 GIT_DIR="otherrepo/.git" &&
262 export GIT_DIR &&
263 __git_C_args=(-C ..) &&
264 __git_find_repo_path &&
265 echo "$__git_repo_path" >"$actual"
266 ) &&
267 test_cmp expected "$actual"
270 test_expect_success '__git_find_repo_path - "git -C" while .git directory in cwd' '
271 echo "$ROOT/otherrepo/.git" >expected &&
273 __git_C_args=(-C otherrepo) &&
274 __git_find_repo_path &&
275 echo "$__git_repo_path" >"$actual"
276 ) &&
277 test_cmp expected "$actual"
280 test_expect_success '__git_find_repo_path - "git -C" while cwd is a .git directory' '
281 echo "$ROOT/otherrepo/.git" >expected &&
283 cd .git &&
284 __git_C_args=(-C .. -C otherrepo) &&
285 __git_find_repo_path &&
286 echo "$__git_repo_path" >"$actual"
287 ) &&
288 test_cmp expected "$actual"
291 test_expect_success '__git_find_repo_path - "git -C" while .git directory in parent' '
292 echo "$ROOT/otherrepo/.git" >expected &&
294 cd subdir &&
295 __git_C_args=(-C .. -C otherrepo) &&
296 __git_find_repo_path &&
297 echo "$__git_repo_path" >"$actual"
298 ) &&
299 test_cmp expected "$actual"
302 test_expect_success '__git_find_repo_path - non-existing path in "git -C"' '
304 __git_C_args=(-C non-existing) &&
305 test_must_fail __git_find_repo_path &&
306 printf "$__git_repo_path" >"$actual"
307 ) &&
308 test_must_be_empty "$actual"
311 test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '
313 __git_dir="non-existing" &&
314 test_must_fail __git_find_repo_path &&
315 printf "$__git_repo_path" >"$actual"
316 ) &&
317 test_must_be_empty "$actual"
320 test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
322 GIT_DIR="$ROOT/non-existing" &&
323 export GIT_DIR &&
324 test_must_fail __git_find_repo_path &&
325 printf "$__git_repo_path" >"$actual"
326 ) &&
327 test_must_be_empty "$actual"
330 test_expect_success '__git_find_repo_path - gitfile in cwd' '
331 echo "$ROOT/otherrepo/.git" >expected &&
332 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
333 test_when_finished "rm -f subdir/.git" &&
335 cd subdir &&
336 __git_find_repo_path &&
337 echo "$__git_repo_path" >"$actual"
338 ) &&
339 test_cmp expected "$actual"
342 test_expect_success '__git_find_repo_path - gitfile in parent' '
343 echo "$ROOT/otherrepo/.git" >expected &&
344 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
345 test_when_finished "rm -f subdir/.git" &&
347 cd subdir/subsubdir &&
348 __git_find_repo_path &&
349 echo "$__git_repo_path" >"$actual"
350 ) &&
351 test_cmp expected "$actual"
354 test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '
355 echo "$ROOT/otherrepo/.git" >expected &&
356 mkdir otherrepo/dir &&
357 test_when_finished "rm -rf otherrepo/dir" &&
358 ln -s otherrepo/dir link &&
359 test_when_finished "rm -f link" &&
361 cd link &&
362 __git_find_repo_path &&
363 echo "$__git_repo_path" >"$actual"
364 ) &&
365 test_cmp expected "$actual"
368 test_expect_success '__git_find_repo_path - not a git repository' '
370 cd non-repo &&
371 GIT_CEILING_DIRECTORIES="$ROOT" &&
372 export GIT_CEILING_DIRECTORIES &&
373 test_must_fail __git_find_repo_path &&
374 printf "$__git_repo_path" >"$actual"
375 ) &&
376 test_must_be_empty "$actual"
379 test_expect_success '__gitdir - finds repo' '
380 echo "$ROOT/.git" >expected &&
382 cd subdir/subsubdir &&
383 __gitdir >"$actual"
384 ) &&
385 test_cmp expected "$actual"
389 test_expect_success '__gitdir - returns error when cannot find repo' '
391 __git_dir="non-existing" &&
392 test_must_fail __gitdir >"$actual"
393 ) &&
394 test_must_be_empty "$actual"
397 test_expect_success '__gitdir - repo as argument' '
398 echo "otherrepo/.git" >expected &&
400 __gitdir "otherrepo" >"$actual"
401 ) &&
402 test_cmp expected "$actual"
405 test_expect_success '__gitdir - remote as argument' '
406 echo "remote" >expected &&
408 __gitdir "remote" >"$actual"
409 ) &&
410 test_cmp expected "$actual"
414 test_expect_success '__git_dequote - plain unquoted word' '
415 __git_dequote unquoted-word &&
416 test unquoted-word = "$dequoted_word"
419 # input: b\a\c\k\'\\\"s\l\a\s\h\es
420 # expected: back'\"slashes
421 test_expect_success '__git_dequote - backslash escaped' '
422 __git_dequote "b\a\c\k\\'\''\\\\\\\"s\l\a\s\h\es" &&
423 test "back'\''\\\"slashes" = "$dequoted_word"
426 # input: sin'gle\' '"quo'ted
427 # expected: single\ "quoted
428 test_expect_success '__git_dequote - single quoted' '
429 __git_dequote "'"sin'gle\\\\' '\\\"quo'ted"'" &&
430 test '\''single\ "quoted'\'' = "$dequoted_word"
433 # input: dou"ble\\" "\"\quot"ed
434 # expected: double\ "\quoted
435 test_expect_success '__git_dequote - double quoted' '
436 __git_dequote '\''dou"ble\\" "\"\quot"ed'\'' &&
437 test '\''double\ "\quoted'\'' = "$dequoted_word"
440 # input: 'open single quote
441 test_expect_success '__git_dequote - open single quote' '
442 __git_dequote "'\''open single quote" &&
443 test "open single quote" = "$dequoted_word"
446 # input: "open double quote
447 test_expect_success '__git_dequote - open double quote' '
448 __git_dequote "\"open double quote" &&
449 test "open double quote" = "$dequoted_word"
453 test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' '
454 sed -e "s/Z$//g" >expected <<-EOF &&
455 with-trailing-space Z
456 without-trailing-spaceZ
457 --option Z
458 --option=Z
459 $invalid_variable_name Z
462 cur=should_be_ignored &&
463 __gitcomp_direct "$(cat expected)" &&
464 print_comp
465 ) &&
466 test_cmp expected out
469 test_expect_success '__gitcomp - trailing space - options' '
470 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
471 --reset-author" <<-EOF
472 --reuse-message=Z
473 --reedit-message=Z
474 --reset-author Z
478 test_expect_success '__gitcomp - trailing space - config keys' '
479 test_gitcomp "br" "branch. branch.autosetupmerge
480 branch.autosetuprebase browser." <<-\EOF
481 branch.Z
482 branch.autosetupmerge Z
483 branch.autosetuprebase Z
484 browser.Z
488 test_expect_success '__gitcomp - option parameter' '
489 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
490 "" "re" <<-\EOF
491 recursive Z
492 resolve Z
496 test_expect_success '__gitcomp - prefix' '
497 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
498 "branch.maint." "me" <<-\EOF
499 branch.maint.merge Z
500 branch.maint.mergeoptions Z
504 test_expect_success '__gitcomp - suffix' '
505 test_gitcomp "branch.me" "master maint next seen" "branch." \
506 "ma" "." <<-\EOF
507 branch.master.Z
508 branch.maint.Z
512 test_expect_success '__gitcomp - ignore optional negative options' '
513 test_gitcomp "--" "--abc --def --no-one -- --no-two" <<-\EOF
514 --abc Z
515 --def Z
516 --no-one Z
517 --no-... Z
521 test_expect_success '__gitcomp - ignore/narrow optional negative options' '
522 test_gitcomp "--a" "--abc --abcdef --no-one -- --no-two" <<-\EOF
523 --abc Z
524 --abcdef Z
528 test_expect_success '__gitcomp - ignore/narrow optional negative options' '
529 test_gitcomp "--n" "--abc --def --no-one -- --no-two" <<-\EOF
530 --no-one Z
531 --no-... Z
535 test_expect_success '__gitcomp - expand all negative options' '
536 test_gitcomp "--no-" "--abc --def --no-one -- --no-two" <<-\EOF
537 --no-one Z
538 --no-two Z
542 test_expect_success '__gitcomp - expand/narrow all negative options' '
543 test_gitcomp "--no-o" "--abc --def --no-one -- --no-two" <<-\EOF
544 --no-one Z
548 test_expect_success '__gitcomp - equal skip' '
549 test_gitcomp "--option=" "--option=" <<-\EOF &&
552 test_gitcomp "option=" "option=" <<-\EOF
557 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
558 __gitcomp "$invalid_variable_name"
561 read -r -d "" refs <<-\EOF
562 main
563 maint
564 next
565 seen
568 test_expect_success '__gitcomp_nl - trailing space' '
569 test_gitcomp_nl "m" "$refs" <<-EOF
570 main Z
571 maint Z
575 test_expect_success '__gitcomp_nl - prefix' '
576 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
577 --fixup=main Z
578 --fixup=maint Z
582 test_expect_success '__gitcomp_nl - suffix' '
583 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
584 branch.main.Z
585 branch.maint.Z
589 test_expect_success '__gitcomp_nl - no suffix' '
590 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
591 mainZ
592 maintZ
596 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
597 __gitcomp_nl "$invalid_variable_name"
600 test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
601 cat >expect <<-EOF &&
602 remote_from_file_1
603 remote_from_file_2
604 remote_in_config_1
605 remote_in_config_2
607 test_when_finished "rm -rf .git/remotes" &&
608 mkdir -p .git/remotes &&
609 >.git/remotes/remote_from_file_1 &&
610 >.git/remotes/remote_from_file_2 &&
611 test_when_finished "git remote remove remote_in_config_1" &&
612 git remote add remote_in_config_1 git://remote_1 &&
613 test_when_finished "git remote remove remote_in_config_2" &&
614 git remote add remote_in_config_2 git://remote_2 &&
616 __git_remotes >actual
617 ) &&
618 test_cmp expect actual
621 test_expect_success '__git_is_configured_remote' '
622 test_when_finished "git remote remove remote_1" &&
623 git remote add remote_1 git://remote_1 &&
624 test_when_finished "git remote remove remote_2" &&
625 git remote add remote_2 git://remote_2 &&
627 __git_is_configured_remote remote_2 &&
628 test_must_fail __git_is_configured_remote non-existent
632 test_expect_success 'setup for ref completion' '
633 git commit --allow-empty -m initial &&
634 git branch -M main &&
635 git branch matching-branch &&
636 git tag matching-tag &&
638 cd otherrepo &&
639 git commit --allow-empty -m initial &&
640 git branch -m main main-in-other &&
641 git branch branch-in-other &&
642 git tag tag-in-other
643 ) &&
644 git remote add other "$ROOT/otherrepo/.git" &&
645 git fetch --no-tags other &&
646 rm -f .git/FETCH_HEAD &&
647 git init thirdrepo
650 test_expect_success '__git_refs - simple' '
651 cat >expected <<-EOF &&
652 HEAD
653 main
654 matching-branch
655 other/branch-in-other
656 other/main-in-other
657 matching-tag
660 cur= &&
661 __git_refs >"$actual"
662 ) &&
663 test_cmp expected "$actual"
666 test_expect_success '__git_refs - full refs' '
667 cat >expected <<-EOF &&
668 refs/heads/main
669 refs/heads/matching-branch
670 refs/remotes/other/branch-in-other
671 refs/remotes/other/main-in-other
672 refs/tags/matching-tag
675 cur=refs/heads/ &&
676 __git_refs >"$actual"
677 ) &&
678 test_cmp expected "$actual"
681 test_expect_success '__git_refs - repo given on the command line' '
682 cat >expected <<-EOF &&
683 HEAD
684 branch-in-other
685 main-in-other
686 tag-in-other
689 __git_dir="$ROOT/otherrepo/.git" &&
690 cur= &&
691 __git_refs >"$actual"
692 ) &&
693 test_cmp expected "$actual"
696 test_expect_success '__git_refs - remote on local file system' '
697 cat >expected <<-EOF &&
698 HEAD
699 branch-in-other
700 main-in-other
701 tag-in-other
704 cur= &&
705 __git_refs otherrepo >"$actual"
706 ) &&
707 test_cmp expected "$actual"
710 test_expect_success '__git_refs - remote on local file system - full refs' '
711 cat >expected <<-EOF &&
712 refs/heads/branch-in-other
713 refs/heads/main-in-other
714 refs/tags/tag-in-other
717 cur=refs/ &&
718 __git_refs otherrepo >"$actual"
719 ) &&
720 test_cmp expected "$actual"
723 test_expect_success '__git_refs - configured remote' '
724 cat >expected <<-EOF &&
725 HEAD
726 branch-in-other
727 main-in-other
730 cur= &&
731 __git_refs other >"$actual"
732 ) &&
733 test_cmp expected "$actual"
736 test_expect_success '__git_refs - configured remote - full refs' '
737 cat >expected <<-EOF &&
738 HEAD
739 refs/heads/branch-in-other
740 refs/heads/main-in-other
741 refs/tags/tag-in-other
744 cur=refs/ &&
745 __git_refs other >"$actual"
746 ) &&
747 test_cmp expected "$actual"
750 test_expect_success '__git_refs - configured remote - repo given on the command line' '
751 cat >expected <<-EOF &&
752 HEAD
753 branch-in-other
754 main-in-other
757 cd thirdrepo &&
758 __git_dir="$ROOT/.git" &&
759 cur= &&
760 __git_refs other >"$actual"
761 ) &&
762 test_cmp expected "$actual"
765 test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
766 cat >expected <<-EOF &&
767 HEAD
768 refs/heads/branch-in-other
769 refs/heads/main-in-other
770 refs/tags/tag-in-other
773 cd thirdrepo &&
774 __git_dir="$ROOT/.git" &&
775 cur=refs/ &&
776 __git_refs other >"$actual"
777 ) &&
778 test_cmp expected "$actual"
781 test_expect_success '__git_refs - configured remote - remote name matches a directory' '
782 cat >expected <<-EOF &&
783 HEAD
784 branch-in-other
785 main-in-other
787 mkdir other &&
788 test_when_finished "rm -rf other" &&
790 cur= &&
791 __git_refs other >"$actual"
792 ) &&
793 test_cmp expected "$actual"
796 test_expect_success '__git_refs - URL remote' '
797 cat >expected <<-EOF &&
798 HEAD
799 branch-in-other
800 main-in-other
801 tag-in-other
804 cur= &&
805 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
806 ) &&
807 test_cmp expected "$actual"
810 test_expect_success '__git_refs - URL remote - full refs' '
811 cat >expected <<-EOF &&
812 HEAD
813 refs/heads/branch-in-other
814 refs/heads/main-in-other
815 refs/tags/tag-in-other
818 cur=refs/ &&
819 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
820 ) &&
821 test_cmp expected "$actual"
824 test_expect_success '__git_refs - non-existing remote' '
826 cur= &&
827 __git_refs non-existing >"$actual"
828 ) &&
829 test_must_be_empty "$actual"
832 test_expect_success '__git_refs - non-existing remote - full refs' '
834 cur=refs/ &&
835 __git_refs non-existing >"$actual"
836 ) &&
837 test_must_be_empty "$actual"
840 test_expect_success '__git_refs - non-existing URL remote' '
842 cur= &&
843 __git_refs "file://$ROOT/non-existing" >"$actual"
844 ) &&
845 test_must_be_empty "$actual"
848 test_expect_success '__git_refs - non-existing URL remote - full refs' '
850 cur=refs/ &&
851 __git_refs "file://$ROOT/non-existing" >"$actual"
852 ) &&
853 test_must_be_empty "$actual"
856 test_expect_success '__git_refs - not in a git repository' '
858 GIT_CEILING_DIRECTORIES="$ROOT" &&
859 export GIT_CEILING_DIRECTORIES &&
860 cd subdir &&
861 cur= &&
862 __git_refs >"$actual"
863 ) &&
864 test_must_be_empty "$actual"
867 test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
868 cat >expected <<-EOF &&
869 HEAD
870 main
871 matching-branch
872 other/ambiguous
873 other/branch-in-other
874 other/main-in-other
875 remote/ambiguous
876 remote/branch-in-remote
877 matching-tag
878 branch-in-other
879 branch-in-remote
880 main-in-other
882 for remote_ref in refs/remotes/other/ambiguous \
883 refs/remotes/remote/ambiguous \
884 refs/remotes/remote/branch-in-remote
886 git update-ref $remote_ref main &&
887 test_when_finished "git update-ref -d $remote_ref" || return 1
888 done &&
890 cur= &&
891 __git_refs "" 1 >"$actual"
892 ) &&
893 test_cmp expected "$actual"
896 test_expect_success '__git_refs - after --opt=' '
897 cat >expected <<-EOF &&
898 HEAD
899 main
900 matching-branch
901 other/branch-in-other
902 other/main-in-other
903 matching-tag
906 cur="--opt=" &&
907 __git_refs "" "" "" "" >"$actual"
908 ) &&
909 test_cmp expected "$actual"
912 test_expect_success '__git_refs - after --opt= - full refs' '
913 cat >expected <<-EOF &&
914 refs/heads/main
915 refs/heads/matching-branch
916 refs/remotes/other/branch-in-other
917 refs/remotes/other/main-in-other
918 refs/tags/matching-tag
921 cur="--opt=refs/" &&
922 __git_refs "" "" "" refs/ >"$actual"
923 ) &&
924 test_cmp expected "$actual"
927 test_expect_success '__git refs - excluding refs' '
928 cat >expected <<-EOF &&
929 ^HEAD
930 ^main
931 ^matching-branch
932 ^other/branch-in-other
933 ^other/main-in-other
934 ^matching-tag
937 cur=^ &&
938 __git_refs >"$actual"
939 ) &&
940 test_cmp expected "$actual"
943 test_expect_success '__git refs - excluding full refs' '
944 cat >expected <<-EOF &&
945 ^refs/heads/main
946 ^refs/heads/matching-branch
947 ^refs/remotes/other/branch-in-other
948 ^refs/remotes/other/main-in-other
949 ^refs/tags/matching-tag
952 cur=^refs/ &&
953 __git_refs >"$actual"
954 ) &&
955 test_cmp expected "$actual"
958 test_expect_success 'setup for filtering matching refs' '
959 git branch matching/branch &&
960 git tag matching/tag &&
961 git -C otherrepo branch matching/branch-in-other &&
962 git fetch --no-tags other &&
963 rm -f .git/FETCH_HEAD
966 test_expect_success '__git_refs - do not filter refs unless told so' '
967 cat >expected <<-EOF &&
968 HEAD
969 main
970 matching-branch
971 matching/branch
972 other/branch-in-other
973 other/main-in-other
974 other/matching/branch-in-other
975 matching-tag
976 matching/tag
979 cur=main &&
980 __git_refs >"$actual"
981 ) &&
982 test_cmp expected "$actual"
985 test_expect_success '__git_refs - only matching refs' '
986 cat >expected <<-EOF &&
987 matching-branch
988 matching/branch
989 matching-tag
990 matching/tag
993 cur=mat &&
994 __git_refs "" "" "" "$cur" >"$actual"
995 ) &&
996 test_cmp expected "$actual"
999 test_expect_success '__git_refs - only matching refs - full refs' '
1000 cat >expected <<-EOF &&
1001 refs/heads/matching-branch
1002 refs/heads/matching/branch
1005 cur=refs/heads/mat &&
1006 __git_refs "" "" "" "$cur" >"$actual"
1007 ) &&
1008 test_cmp expected "$actual"
1011 test_expect_success '__git_refs - only matching refs - remote on local file system' '
1012 cat >expected <<-EOF &&
1013 main-in-other
1014 matching/branch-in-other
1017 cur=ma &&
1018 __git_refs otherrepo "" "" "$cur" >"$actual"
1019 ) &&
1020 test_cmp expected "$actual"
1023 test_expect_success '__git_refs - only matching refs - configured remote' '
1024 cat >expected <<-EOF &&
1025 main-in-other
1026 matching/branch-in-other
1029 cur=ma &&
1030 __git_refs other "" "" "$cur" >"$actual"
1031 ) &&
1032 test_cmp expected "$actual"
1035 test_expect_success '__git_refs - only matching refs - remote - full refs' '
1036 cat >expected <<-EOF &&
1037 refs/heads/main-in-other
1038 refs/heads/matching/branch-in-other
1041 cur=refs/heads/ma &&
1042 __git_refs other "" "" "$cur" >"$actual"
1043 ) &&
1044 test_cmp expected "$actual"
1047 test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
1048 cat >expected <<-EOF &&
1049 matching-branch
1050 matching/branch
1051 matching-tag
1052 matching/tag
1053 matching/branch-in-other
1055 for remote_ref in refs/remotes/other/ambiguous \
1056 refs/remotes/remote/ambiguous \
1057 refs/remotes/remote/branch-in-remote
1059 git update-ref $remote_ref main &&
1060 test_when_finished "git update-ref -d $remote_ref" || return 1
1061 done &&
1063 cur=mat &&
1064 __git_refs "" 1 "" "$cur" >"$actual"
1065 ) &&
1066 test_cmp expected "$actual"
1069 test_expect_success 'teardown after filtering matching refs' '
1070 git branch -d matching/branch &&
1071 git tag -d matching/tag &&
1072 git update-ref -d refs/remotes/other/matching/branch-in-other &&
1073 git -C otherrepo branch -D matching/branch-in-other
1076 test_expect_success '__git_refs - for-each-ref format specifiers in prefix' '
1077 cat >expected <<-EOF &&
1078 evil-%%-%42-%(refname)..main
1081 cur="evil-%%-%42-%(refname)..mai" &&
1082 __git_refs "" "" "evil-%%-%42-%(refname).." mai >"$actual"
1083 ) &&
1084 test_cmp expected "$actual"
1087 test_expect_success '__git_complete_refs - simple' '
1088 sed -e "s/Z$//" >expected <<-EOF &&
1089 HEAD Z
1090 main Z
1091 matching-branch Z
1092 other/branch-in-other Z
1093 other/main-in-other Z
1094 matching-tag Z
1097 cur= &&
1098 __git_complete_refs &&
1099 print_comp
1100 ) &&
1101 test_cmp expected out
1104 test_expect_success '__git_complete_refs - matching' '
1105 sed -e "s/Z$//" >expected <<-EOF &&
1106 matching-branch Z
1107 matching-tag Z
1110 cur=mat &&
1111 __git_complete_refs &&
1112 print_comp
1113 ) &&
1114 test_cmp expected out
1117 test_expect_success '__git_complete_refs - remote' '
1118 sed -e "s/Z$//" >expected <<-EOF &&
1119 HEAD Z
1120 branch-in-other Z
1121 main-in-other Z
1124 cur= &&
1125 __git_complete_refs --remote=other &&
1126 print_comp
1127 ) &&
1128 test_cmp expected out
1131 test_expect_success '__git_complete_refs - track' '
1132 sed -e "s/Z$//" >expected <<-EOF &&
1133 HEAD Z
1134 main Z
1135 matching-branch Z
1136 other/branch-in-other Z
1137 other/main-in-other Z
1138 matching-tag Z
1139 branch-in-other Z
1140 main-in-other Z
1143 cur= &&
1144 __git_complete_refs --track &&
1145 print_comp
1146 ) &&
1147 test_cmp expected out
1150 test_expect_success '__git_complete_refs - current word' '
1151 sed -e "s/Z$//" >expected <<-EOF &&
1152 matching-branch Z
1153 matching-tag Z
1156 cur="--option=mat" &&
1157 __git_complete_refs --cur="${cur#*=}" &&
1158 print_comp
1159 ) &&
1160 test_cmp expected out
1163 test_expect_success '__git_complete_refs - prefix' '
1164 sed -e "s/Z$//" >expected <<-EOF &&
1165 v1.0..matching-branch Z
1166 v1.0..matching-tag Z
1169 cur=v1.0..mat &&
1170 __git_complete_refs --pfx=v1.0.. --cur=mat &&
1171 print_comp
1172 ) &&
1173 test_cmp expected out
1176 test_expect_success '__git_complete_refs - suffix' '
1177 cat >expected <<-EOF &&
1178 HEAD.
1179 main.
1180 matching-branch.
1181 other/branch-in-other.
1182 other/main-in-other.
1183 matching-tag.
1186 cur= &&
1187 __git_complete_refs --sfx=. &&
1188 print_comp
1189 ) &&
1190 test_cmp expected out
1193 test_expect_success '__git_complete_fetch_refspecs - simple' '
1194 sed -e "s/Z$//" >expected <<-EOF &&
1195 HEAD:HEAD Z
1196 branch-in-other:branch-in-other Z
1197 main-in-other:main-in-other Z
1200 cur= &&
1201 __git_complete_fetch_refspecs other &&
1202 print_comp
1203 ) &&
1204 test_cmp expected out
1207 test_expect_success '__git_complete_fetch_refspecs - matching' '
1208 sed -e "s/Z$//" >expected <<-EOF &&
1209 branch-in-other:branch-in-other Z
1212 cur=br &&
1213 __git_complete_fetch_refspecs other "" br &&
1214 print_comp
1215 ) &&
1216 test_cmp expected out
1219 test_expect_success '__git_complete_fetch_refspecs - prefix' '
1220 sed -e "s/Z$//" >expected <<-EOF &&
1221 +HEAD:HEAD Z
1222 +branch-in-other:branch-in-other Z
1223 +main-in-other:main-in-other Z
1226 cur="+" &&
1227 __git_complete_fetch_refspecs other "+" "" &&
1228 print_comp
1229 ) &&
1230 test_cmp expected out
1233 test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
1234 sed -e "s/Z$//" >expected <<-EOF &&
1235 refs/heads/branch-in-other:refs/heads/branch-in-other Z
1236 refs/heads/main-in-other:refs/heads/main-in-other Z
1237 refs/tags/tag-in-other:refs/tags/tag-in-other Z
1240 cur=refs/ &&
1241 __git_complete_fetch_refspecs other "" refs/ &&
1242 print_comp
1243 ) &&
1244 test_cmp expected out
1247 test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
1248 sed -e "s/Z$//" >expected <<-EOF &&
1249 +refs/heads/branch-in-other:refs/heads/branch-in-other Z
1250 +refs/heads/main-in-other:refs/heads/main-in-other Z
1251 +refs/tags/tag-in-other:refs/tags/tag-in-other Z
1254 cur=+refs/ &&
1255 __git_complete_fetch_refspecs other + refs/ &&
1256 print_comp
1257 ) &&
1258 test_cmp expected out
1261 test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
1262 test_completion "git switch " <<-\EOF
1263 branch-in-other Z
1264 main Z
1265 main-in-other Z
1266 matching-branch Z
1270 test_expect_success 'git checkout - completes refs and unique remote branches for DWIM' '
1271 test_completion "git checkout " <<-\EOF
1272 HEAD Z
1273 branch-in-other Z
1274 main Z
1275 main-in-other Z
1276 matching-branch Z
1277 matching-tag Z
1278 other/branch-in-other Z
1279 other/main-in-other Z
1283 test_expect_success 'git switch - with --no-guess, complete only local branches' '
1284 test_completion "git switch --no-guess " <<-\EOF
1285 main Z
1286 matching-branch Z
1290 test_expect_success 'git switch - with GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete only local branches' '
1291 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch " <<-\EOF
1292 main Z
1293 matching-branch Z
1297 test_expect_success 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete local branches and unique remote names for DWIM logic' '
1298 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch --guess " <<-\EOF
1299 branch-in-other Z
1300 main Z
1301 main-in-other Z
1302 matching-branch Z
1306 test_expect_success 'git switch - a later --guess overrides previous --no-guess, complete local and remote unique branches for DWIM' '
1307 test_completion "git switch --no-guess --guess " <<-\EOF
1308 branch-in-other Z
1309 main Z
1310 main-in-other Z
1311 matching-branch Z
1315 test_expect_success 'git switch - a later --no-guess overrides previous --guess, complete only local branches' '
1316 test_completion "git switch --guess --no-guess " <<-\EOF
1317 main Z
1318 matching-branch Z
1322 test_expect_success 'git checkout - with GIT_COMPLETION_NO_GUESS=1 only completes refs' '
1323 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout " <<-\EOF
1324 HEAD Z
1325 main Z
1326 matching-branch Z
1327 matching-tag Z
1328 other/branch-in-other Z
1329 other/main-in-other Z
1333 test_expect_success 'git checkout - --guess overrides GIT_COMPLETION_NO_GUESS=1, complete refs and unique remote branches for DWIM' '
1334 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout --guess " <<-\EOF
1335 HEAD Z
1336 branch-in-other Z
1337 main Z
1338 main-in-other Z
1339 matching-branch Z
1340 matching-tag Z
1341 other/branch-in-other Z
1342 other/main-in-other Z
1346 test_expect_success 'git checkout - with --no-guess, only completes refs' '
1347 test_completion "git checkout --no-guess " <<-\EOF
1348 HEAD Z
1349 main Z
1350 matching-branch Z
1351 matching-tag Z
1352 other/branch-in-other Z
1353 other/main-in-other Z
1357 test_expect_success 'git checkout - a later --guess overrides previous --no-guess, complete refs and unique remote branches for DWIM' '
1358 test_completion "git checkout --no-guess --guess " <<-\EOF
1359 HEAD Z
1360 branch-in-other Z
1361 main Z
1362 main-in-other Z
1363 matching-branch Z
1364 matching-tag Z
1365 other/branch-in-other Z
1366 other/main-in-other Z
1370 test_expect_success 'git checkout - a later --no-guess overrides previous --guess, complete only refs' '
1371 test_completion "git checkout --guess --no-guess " <<-\EOF
1372 HEAD Z
1373 main Z
1374 matching-branch Z
1375 matching-tag Z
1376 other/branch-in-other Z
1377 other/main-in-other Z
1381 test_expect_success 'git checkout - with checkout.guess = false, only completes refs' '
1382 test_config checkout.guess false &&
1383 test_completion "git checkout " <<-\EOF
1384 HEAD Z
1385 main Z
1386 matching-branch Z
1387 matching-tag Z
1388 other/branch-in-other Z
1389 other/main-in-other Z
1393 test_expect_success 'git checkout - with checkout.guess = true, completes refs and unique remote branches for DWIM' '
1394 test_config checkout.guess true &&
1395 test_completion "git checkout " <<-\EOF
1396 HEAD Z
1397 branch-in-other Z
1398 main Z
1399 main-in-other Z
1400 matching-branch Z
1401 matching-tag Z
1402 other/branch-in-other Z
1403 other/main-in-other Z
1407 test_expect_success 'git checkout - a later --guess overrides previous checkout.guess = false, complete refs and unique remote branches for DWIM' '
1408 test_config checkout.guess false &&
1409 test_completion "git checkout --guess " <<-\EOF
1410 HEAD Z
1411 branch-in-other Z
1412 main Z
1413 main-in-other Z
1414 matching-branch Z
1415 matching-tag Z
1416 other/branch-in-other Z
1417 other/main-in-other Z
1421 test_expect_success 'git checkout - a later --no-guess overrides previous checkout.guess = true, complete only refs' '
1422 test_config checkout.guess true &&
1423 test_completion "git checkout --no-guess " <<-\EOF
1424 HEAD Z
1425 main Z
1426 matching-branch Z
1427 matching-tag Z
1428 other/branch-in-other Z
1429 other/main-in-other Z
1433 test_expect_success 'git switch - with --detach, complete all references' '
1434 test_completion "git switch --detach " <<-\EOF
1435 HEAD Z
1436 main Z
1437 matching-branch Z
1438 matching-tag Z
1439 other/branch-in-other Z
1440 other/main-in-other Z
1444 test_expect_success 'git checkout - with --detach, complete only references' '
1445 test_completion "git checkout --detach " <<-\EOF
1446 HEAD Z
1447 main Z
1448 matching-branch Z
1449 matching-tag Z
1450 other/branch-in-other Z
1451 other/main-in-other Z
1455 test_expect_success 'setup sparse-checkout tests' '
1456 # set up sparse-checkout repo
1457 git init sparse-checkout &&
1459 cd sparse-checkout &&
1460 mkdir -p folder1/0/1 folder2/0 folder3 &&
1461 touch folder1/0/1/t.txt &&
1462 touch folder2/0/t.txt &&
1463 touch folder3/t.txt &&
1464 git add . &&
1465 git commit -am "Initial commit"
1469 test_expect_success 'sparse-checkout completes subcommands' '
1470 test_completion "git sparse-checkout " <<-\EOF
1471 list Z
1472 init Z
1473 set Z
1474 add Z
1475 reapply Z
1476 disable Z
1480 test_expect_success 'cone mode sparse-checkout completes directory names' '
1481 # initialize sparse-checkout definitions
1482 git -C sparse-checkout sparse-checkout set --cone folder1/0 folder3 &&
1484 # test tab completion
1486 cd sparse-checkout &&
1487 test_completion "git sparse-checkout set f" <<-\EOF
1488 folder1/
1489 folder2/
1490 folder3/
1492 ) &&
1495 cd sparse-checkout &&
1496 test_completion "git sparse-checkout set folder1/" <<-\EOF
1497 folder1/0/
1499 ) &&
1502 cd sparse-checkout &&
1503 test_completion "git sparse-checkout set folder1/0/" <<-\EOF
1504 folder1/0/1/
1506 ) &&
1509 cd sparse-checkout/folder1 &&
1510 test_completion "git sparse-checkout add 0" <<-\EOF
1516 test_expect_success 'cone mode sparse-checkout completes directory names with spaces and accents' '
1517 # reset sparse-checkout
1518 git -C sparse-checkout sparse-checkout disable &&
1520 cd sparse-checkout &&
1521 mkdir "directory with spaces" &&
1522 mkdir "directory-with-áccent" &&
1523 >"directory with spaces/randomfile" &&
1524 >"directory-with-áccent/randomfile" &&
1525 git add . &&
1526 git commit -m "Add directory with spaces and directory with accent" &&
1527 git sparse-checkout set --cone "directory with spaces" \
1528 "directory-with-áccent" &&
1529 test_completion "git sparse-checkout add dir" <<-\EOF &&
1530 directory with spaces/
1531 directory-with-áccent/
1533 rm -rf "directory with spaces" &&
1534 rm -rf "directory-with-áccent" &&
1535 git add . &&
1536 git commit -m "Remove directory with spaces and directory with accent"
1540 # use FUNNYNAMES to avoid running on Windows, which doesn't permit tabs in paths
1541 test_expect_success FUNNYNAMES 'cone mode sparse-checkout completes directory names with tabs' '
1542 # reset sparse-checkout
1543 git -C sparse-checkout sparse-checkout disable &&
1545 cd sparse-checkout &&
1546 mkdir "$(printf "directory\twith\ttabs")" &&
1547 >"$(printf "directory\twith\ttabs")/randomfile" &&
1548 git add . &&
1549 git commit -m "Add directory with tabs" &&
1550 git sparse-checkout set --cone \
1551 "$(printf "directory\twith\ttabs")" &&
1552 test_completion "git sparse-checkout add dir" <<-\EOF &&
1553 directory with tabs/
1555 rm -rf "$(printf "directory\twith\ttabs")" &&
1556 git add . &&
1557 git commit -m "Remove directory with tabs"
1561 # use FUNNYNAMES to avoid running on Windows, and !CYGWIN for Cygwin, as neither permit backslashes in paths
1562 test_expect_success FUNNYNAMES,!CYGWIN 'cone mode sparse-checkout completes directory names with backslashes' '
1563 # reset sparse-checkout
1564 git -C sparse-checkout sparse-checkout disable &&
1566 cd sparse-checkout &&
1567 mkdir "directory\with\backslashes" &&
1568 >"directory\with\backslashes/randomfile" &&
1569 git add . &&
1570 git commit -m "Add directory with backslashes" &&
1571 git sparse-checkout set --cone \
1572 "directory\with\backslashes" &&
1573 test_completion "git sparse-checkout add dir" <<-\EOF &&
1574 directory\with\backslashes/
1576 rm -rf "directory\with\backslashes" &&
1577 git add . &&
1578 git commit -m "Remove directory with backslashes"
1582 test_expect_success 'non-cone mode sparse-checkout gives rooted paths' '
1583 # reset sparse-checkout repo to non-cone mode
1584 git -C sparse-checkout sparse-checkout disable &&
1585 git -C sparse-checkout sparse-checkout set --no-cone &&
1588 cd sparse-checkout &&
1589 # expected to be empty since we have not configured
1590 # custom completion for non-cone mode
1591 test_completion "git sparse-checkout set f" <<-\EOF
1592 /folder1/0/1/t.txt Z
1593 /folder1/expected Z
1594 /folder1/out Z
1595 /folder1/out_sorted Z
1596 /folder2/0/t.txt Z
1597 /folder3/t.txt Z
1602 test_expect_success 'git sparse-checkout set --cone completes directory names' '
1603 git -C sparse-checkout sparse-checkout disable &&
1606 cd sparse-checkout &&
1607 test_completion "git sparse-checkout set --cone f" <<-\EOF
1608 folder1/
1609 folder2/
1610 folder3/
1615 test_expect_success 'git switch - with -d, complete all references' '
1616 test_completion "git switch -d " <<-\EOF
1617 HEAD Z
1618 main Z
1619 matching-branch Z
1620 matching-tag Z
1621 other/branch-in-other Z
1622 other/main-in-other Z
1626 test_expect_success 'git checkout - with -d, complete only references' '
1627 test_completion "git checkout -d " <<-\EOF
1628 HEAD Z
1629 main Z
1630 matching-branch Z
1631 matching-tag Z
1632 other/branch-in-other Z
1633 other/main-in-other Z
1637 test_expect_success 'git switch - with --track, complete only remote branches' '
1638 test_completion "git switch --track " <<-\EOF &&
1639 other/branch-in-other Z
1640 other/main-in-other Z
1642 test_completion "git switch -t " <<-\EOF
1643 other/branch-in-other Z
1644 other/main-in-other Z
1648 test_expect_success 'git checkout - with --track, complete only remote branches' '
1649 test_completion "git checkout --track " <<-\EOF &&
1650 other/branch-in-other Z
1651 other/main-in-other Z
1653 test_completion "git checkout -t " <<-\EOF
1654 other/branch-in-other Z
1655 other/main-in-other Z
1659 test_expect_success 'git switch - with --no-track, complete only local branch names' '
1660 test_completion "git switch --no-track " <<-\EOF
1661 main Z
1662 matching-branch Z
1666 test_expect_success 'git checkout - with --no-track, complete only local references' '
1667 test_completion "git checkout --no-track " <<-\EOF
1668 HEAD Z
1669 main Z
1670 matching-branch Z
1671 matching-tag Z
1672 other/branch-in-other Z
1673 other/main-in-other Z
1677 test_expect_success 'git switch - with -c, complete all references' '
1678 test_completion "git switch -c new-branch " <<-\EOF
1679 HEAD Z
1680 main Z
1681 matching-branch Z
1682 matching-tag Z
1683 other/branch-in-other Z
1684 other/main-in-other Z
1688 test_expect_success 'git switch - with -C, complete all references' '
1689 test_completion "git switch -C new-branch " <<-\EOF
1690 HEAD Z
1691 main Z
1692 matching-branch Z
1693 matching-tag Z
1694 other/branch-in-other Z
1695 other/main-in-other Z
1699 test_expect_success 'git switch - with -c and --track, complete all references' '
1700 test_completion "git switch -c new-branch --track " <<-EOF
1701 HEAD Z
1702 main Z
1703 matching-branch Z
1704 matching-tag Z
1705 other/branch-in-other Z
1706 other/main-in-other Z
1710 test_expect_success 'git switch - with -C and --track, complete all references' '
1711 test_completion "git switch -C new-branch --track " <<-EOF
1712 HEAD Z
1713 main Z
1714 matching-branch Z
1715 matching-tag Z
1716 other/branch-in-other Z
1717 other/main-in-other Z
1721 test_expect_success 'git switch - with -c and --no-track, complete all references' '
1722 test_completion "git switch -c new-branch --no-track " <<-\EOF
1723 HEAD Z
1724 main Z
1725 matching-branch Z
1726 matching-tag Z
1727 other/branch-in-other Z
1728 other/main-in-other Z
1732 test_expect_success 'git switch - with -C and --no-track, complete all references' '
1733 test_completion "git switch -C new-branch --no-track " <<-\EOF
1734 HEAD Z
1735 main Z
1736 matching-branch Z
1737 matching-tag Z
1738 other/branch-in-other Z
1739 other/main-in-other Z
1743 test_expect_success 'git checkout - with -b, complete all references' '
1744 test_completion "git checkout -b new-branch " <<-\EOF
1745 HEAD Z
1746 main Z
1747 matching-branch Z
1748 matching-tag Z
1749 other/branch-in-other Z
1750 other/main-in-other Z
1754 test_expect_success 'git checkout - with -B, complete all references' '
1755 test_completion "git checkout -B new-branch " <<-\EOF
1756 HEAD Z
1757 main Z
1758 matching-branch Z
1759 matching-tag Z
1760 other/branch-in-other Z
1761 other/main-in-other Z
1765 test_expect_success 'git checkout - with -b and --track, complete all references' '
1766 test_completion "git checkout -b new-branch --track " <<-EOF
1767 HEAD Z
1768 main Z
1769 matching-branch Z
1770 matching-tag Z
1771 other/branch-in-other Z
1772 other/main-in-other Z
1776 test_expect_success 'git checkout - with -B and --track, complete all references' '
1777 test_completion "git checkout -B new-branch --track " <<-EOF
1778 HEAD Z
1779 main Z
1780 matching-branch Z
1781 matching-tag Z
1782 other/branch-in-other Z
1783 other/main-in-other Z
1787 test_expect_success 'git checkout - with -b and --no-track, complete all references' '
1788 test_completion "git checkout -b new-branch --no-track " <<-\EOF
1789 HEAD Z
1790 main Z
1791 matching-branch Z
1792 matching-tag Z
1793 other/branch-in-other Z
1794 other/main-in-other Z
1798 test_expect_success 'git checkout - with -B and --no-track, complete all references' '
1799 test_completion "git checkout -B new-branch --no-track " <<-\EOF
1800 HEAD Z
1801 main Z
1802 matching-branch Z
1803 matching-tag Z
1804 other/branch-in-other Z
1805 other/main-in-other Z
1809 test_expect_success 'git switch - for -c, complete local branches and unique remote branches' '
1810 test_completion "git switch -c " <<-\EOF
1811 branch-in-other Z
1812 main Z
1813 main-in-other Z
1814 matching-branch Z
1818 test_expect_success 'git switch - for -C, complete local branches and unique remote branches' '
1819 test_completion "git switch -C " <<-\EOF
1820 branch-in-other Z
1821 main Z
1822 main-in-other Z
1823 matching-branch Z
1827 test_expect_success 'git switch - for -c with --no-guess, complete local branches only' '
1828 test_completion "git switch --no-guess -c " <<-\EOF
1829 main Z
1830 matching-branch Z
1834 test_expect_success 'git switch - for -C with --no-guess, complete local branches only' '
1835 test_completion "git switch --no-guess -C " <<-\EOF
1836 main Z
1837 matching-branch Z
1841 test_expect_success 'git switch - for -c with --no-track, complete local branches only' '
1842 test_completion "git switch --no-track -c " <<-\EOF
1843 main Z
1844 matching-branch Z
1848 test_expect_success 'git switch - for -C with --no-track, complete local branches only' '
1849 test_completion "git switch --no-track -C " <<-\EOF
1850 main Z
1851 matching-branch Z
1855 test_expect_success 'git checkout - for -b, complete local branches and unique remote branches' '
1856 test_completion "git checkout -b " <<-\EOF
1857 branch-in-other Z
1858 main Z
1859 main-in-other Z
1860 matching-branch Z
1864 test_expect_success 'git checkout - for -B, complete local branches and unique remote branches' '
1865 test_completion "git checkout -B " <<-\EOF
1866 branch-in-other Z
1867 main Z
1868 main-in-other Z
1869 matching-branch Z
1873 test_expect_success 'git checkout - for -b with --no-guess, complete local branches only' '
1874 test_completion "git checkout --no-guess -b " <<-\EOF
1875 main Z
1876 matching-branch Z
1880 test_expect_success 'git checkout - for -B with --no-guess, complete local branches only' '
1881 test_completion "git checkout --no-guess -B " <<-\EOF
1882 main Z
1883 matching-branch Z
1887 test_expect_success 'git checkout - for -b with --no-track, complete local branches only' '
1888 test_completion "git checkout --no-track -b " <<-\EOF
1889 main Z
1890 matching-branch Z
1894 test_expect_success 'git checkout - for -B with --no-track, complete local branches only' '
1895 test_completion "git checkout --no-track -B " <<-\EOF
1896 main Z
1897 matching-branch Z
1901 test_expect_success 'git switch - with --orphan completes local branch names and unique remote branch names' '
1902 test_completion "git switch --orphan " <<-\EOF
1903 branch-in-other Z
1904 main Z
1905 main-in-other Z
1906 matching-branch Z
1910 test_expect_success 'git switch - --orphan with branch already provided completes nothing else' '
1911 test_completion "git switch --orphan main " <<-\EOF
1916 test_expect_success 'git checkout - with --orphan completes local branch names and unique remote branch names' '
1917 test_completion "git checkout --orphan " <<-\EOF
1918 branch-in-other Z
1919 main Z
1920 main-in-other Z
1921 matching-branch Z
1925 test_expect_success 'git checkout - --orphan with branch already provided completes local refs for a start-point' '
1926 test_completion "git checkout --orphan main " <<-\EOF
1927 HEAD Z
1928 main Z
1929 matching-branch Z
1930 matching-tag Z
1931 other/branch-in-other Z
1932 other/main-in-other Z
1936 test_expect_success 'git restore completes modified files' '
1937 test_commit A a.file &&
1938 echo B >a.file &&
1939 test_completion "git restore a." <<-\EOF
1940 a.file
1944 test_expect_success 'teardown after ref completion' '
1945 git branch -d matching-branch &&
1946 git tag -d matching-tag &&
1947 git remote remove other
1951 test_path_completion ()
1953 test $# = 2 || BUG "not 2 parameters to test_path_completion"
1955 local cur="$1" expected="$2"
1956 echo "$expected" >expected &&
1958 # In the following tests calling this function we only
1959 # care about how __git_complete_index_file() deals with
1960 # unusual characters in path names. By requesting only
1961 # untracked files we do not have to bother adding any
1962 # paths to the index in those tests.
1963 __git_complete_index_file --others &&
1964 print_comp
1965 ) &&
1966 test_cmp expected out
1969 test_expect_success 'setup for path completion tests' '
1970 mkdir simple-dir \
1971 "spaces in dir" \
1972 árvíztűrő &&
1973 touch simple-dir/simple-file \
1974 "spaces in dir/spaces in file" \
1975 "árvíztűrő/Сайн яваарай" &&
1976 if test_have_prereq !MINGW &&
1977 mkdir BS\\dir \
1978 '$'separators\034in\035dir'' &&
1979 touch BS\\dir/DQ\"file \
1980 '$'separators\034in\035dir/sep\036in\037file''
1981 then
1982 test_set_prereq FUNNIERNAMES
1983 else
1984 rm -rf BS\\dir '$'separators\034in\035dir''
1988 test_expect_success '__git_complete_index_file - simple' '
1989 test_path_completion simple simple-dir && # Bash is supposed to
1990 # add the trailing /.
1991 test_path_completion simple-dir/simple simple-dir/simple-file
1994 test_expect_success \
1995 '__git_complete_index_file - escaped characters on cmdline' '
1996 test_path_completion spac "spaces in dir" && # Bash will turn this
1997 # into "spaces\ in\ dir"
1998 test_path_completion "spaces\\ i" \
1999 "spaces in dir" &&
2000 test_path_completion "spaces\\ in\\ dir/s" \
2001 "spaces in dir/spaces in file" &&
2002 test_path_completion "spaces\\ in\\ dir/spaces\\ i" \
2003 "spaces in dir/spaces in file"
2006 test_expect_success \
2007 '__git_complete_index_file - quoted characters on cmdline' '
2008 # Testing with an opening but without a corresponding closing
2009 # double quote is important.
2010 test_path_completion \"spac "spaces in dir" &&
2011 test_path_completion "\"spaces i" \
2012 "spaces in dir" &&
2013 test_path_completion "\"spaces in dir/s" \
2014 "spaces in dir/spaces in file" &&
2015 test_path_completion "\"spaces in dir/spaces i" \
2016 "spaces in dir/spaces in file"
2019 test_expect_success '__git_complete_index_file - UTF-8 in ls-files output' '
2020 test_path_completion á árvíztűrő &&
2021 test_path_completion árvíztűrő/С "árvíztűrő/Сайн яваарай"
2024 test_expect_success FUNNIERNAMES \
2025 '__git_complete_index_file - C-style escapes in ls-files output' '
2026 test_path_completion BS \
2027 BS\\dir &&
2028 test_path_completion BS\\\\d \
2029 BS\\dir &&
2030 test_path_completion BS\\\\dir/DQ \
2031 BS\\dir/DQ\"file &&
2032 test_path_completion BS\\\\dir/DQ\\\"f \
2033 BS\\dir/DQ\"file
2036 test_expect_success FUNNIERNAMES \
2037 '__git_complete_index_file - \nnn-escaped characters in ls-files output' '
2038 test_path_completion sep '$'separators\034in\035dir'' &&
2039 test_path_completion '$'separators\034i'' \
2040 '$'separators\034in\035dir'' &&
2041 test_path_completion '$'separators\034in\035dir/sep'' \
2042 '$'separators\034in\035dir/sep\036in\037file'' &&
2043 test_path_completion '$'separators\034in\035dir/sep\036i'' \
2044 '$'separators\034in\035dir/sep\036in\037file''
2047 test_expect_success FUNNYNAMES \
2048 '__git_complete_index_file - removing repeated quoted path components' '
2049 test_when_finished rm -r repeated-quoted &&
2050 mkdir repeated-quoted && # A directory whose name in itself
2051 # would not be quoted ...
2052 >repeated-quoted/0-file &&
2053 >repeated-quoted/1\"file && # ... but here the file makes the
2054 # dirname quoted ...
2055 >repeated-quoted/2-file &&
2056 >repeated-quoted/3\"file && # ... and here, too.
2058 # Still, we shold only list the directory name only once.
2059 test_path_completion repeated repeated-quoted
2062 test_expect_success 'teardown after path completion tests' '
2063 rm -rf simple-dir "spaces in dir" árvíztűrő \
2064 BS\\dir '$'separators\034in\035dir''
2067 test_expect_success '__git_find_on_cmdline - single match' '
2068 echo list >expect &&
2070 words=(git command --opt list) &&
2071 cword=${#words[@]} &&
2072 __git_cmd_idx=1 &&
2073 __git_find_on_cmdline "add list remove" >actual
2074 ) &&
2075 test_cmp expect actual
2078 test_expect_success '__git_find_on_cmdline - multiple matches' '
2079 echo remove >expect &&
2081 words=(git command -o --opt remove list add) &&
2082 cword=${#words[@]} &&
2083 __git_cmd_idx=1 &&
2084 __git_find_on_cmdline "add list remove" >actual
2085 ) &&
2086 test_cmp expect actual
2089 test_expect_success '__git_find_on_cmdline - no match' '
2091 words=(git command --opt branch) &&
2092 cword=${#words[@]} &&
2093 __git_cmd_idx=1 &&
2094 __git_find_on_cmdline "add list remove" >actual
2095 ) &&
2096 test_must_be_empty actual
2099 test_expect_success '__git_find_on_cmdline - single match with index' '
2100 echo "3 list" >expect &&
2102 words=(git command --opt list) &&
2103 cword=${#words[@]} &&
2104 __git_cmd_idx=1 &&
2105 __git_find_on_cmdline --show-idx "add list remove" >actual
2106 ) &&
2107 test_cmp expect actual
2110 test_expect_success '__git_find_on_cmdline - multiple matches with index' '
2111 echo "4 remove" >expect &&
2113 words=(git command -o --opt remove list add) &&
2114 cword=${#words[@]} &&
2115 __git_cmd_idx=1 &&
2116 __git_find_on_cmdline --show-idx "add list remove" >actual
2117 ) &&
2118 test_cmp expect actual
2121 test_expect_success '__git_find_on_cmdline - no match with index' '
2123 words=(git command --opt branch) &&
2124 cword=${#words[@]} &&
2125 __git_cmd_idx=1 &&
2126 __git_find_on_cmdline --show-idx "add list remove" >actual
2127 ) &&
2128 test_must_be_empty actual
2131 test_expect_success '__git_find_on_cmdline - ignores matches before command with index' '
2132 echo "6 remove" >expect &&
2134 words=(git -C remove command -o --opt remove list add) &&
2135 cword=${#words[@]} &&
2136 __git_cmd_idx=3 &&
2137 __git_find_on_cmdline --show-idx "add list remove" >actual
2138 ) &&
2139 test_cmp expect actual
2142 test_expect_success '__git_get_config_variables' '
2143 cat >expect <<-EOF &&
2144 name-1
2145 name-2
2147 test_config interesting.name-1 good &&
2148 test_config interesting.name-2 good &&
2149 test_config subsection.interesting.name-3 bad &&
2150 __git_get_config_variables interesting >actual &&
2151 test_cmp expect actual
2154 test_expect_success '__git_pretty_aliases' '
2155 cat >expect <<-EOF &&
2156 author
2157 hash
2159 test_config pretty.author "%an %ae" &&
2160 test_config pretty.hash %H &&
2161 __git_pretty_aliases >actual &&
2162 test_cmp expect actual
2165 test_expect_success 'basic' '
2166 run_completion "git " &&
2167 # built-in
2168 grep -q "^add \$" out &&
2169 # script
2170 grep -q "^rebase \$" out &&
2171 # plumbing
2172 ! grep -q "^ls-files \$" out &&
2174 run_completion "git r" &&
2175 ! grep -q -v "^r" out
2178 test_expect_success 'double dash "git" itself' '
2179 test_completion "git --" <<-\EOF
2180 --paginate Z
2181 --no-pager Z
2182 --git-dir=
2183 --bare Z
2184 --version Z
2185 --exec-path Z
2186 --exec-path=
2187 --html-path Z
2188 --man-path Z
2189 --info-path Z
2190 --work-tree=
2191 --namespace=
2192 --no-replace-objects Z
2193 --help Z
2197 test_expect_success 'double dash "git checkout"' '
2198 test_completion "git checkout --" <<-\EOF
2199 --quiet Z
2200 --detach Z
2201 --track Z
2202 --orphan=Z
2203 --ours Z
2204 --theirs Z
2205 --merge Z
2206 --conflict=Z
2207 --patch Z
2208 --ignore-skip-worktree-bits Z
2209 --ignore-other-worktrees Z
2210 --recurse-submodules Z
2211 --progress Z
2212 --guess Z
2213 --no-guess Z
2214 --no-... Z
2215 --overlay Z
2216 --pathspec-file-nul Z
2217 --pathspec-from-file=Z
2221 test_expect_success 'general options' '
2222 test_completion "git --ver" "--version " &&
2223 test_completion "git --hel" "--help " &&
2224 test_completion "git --exe" <<-\EOF &&
2225 --exec-path Z
2226 --exec-path=
2228 test_completion "git --htm" "--html-path " &&
2229 test_completion "git --pag" "--paginate " &&
2230 test_completion "git --no-p" "--no-pager " &&
2231 test_completion "git --git" "--git-dir=" &&
2232 test_completion "git --wor" "--work-tree=" &&
2233 test_completion "git --nam" "--namespace=" &&
2234 test_completion "git --bar" "--bare " &&
2235 test_completion "git --inf" "--info-path " &&
2236 test_completion "git --no-r" "--no-replace-objects "
2239 test_expect_success 'general options plus command' '
2240 test_completion "git --version check" "checkout " &&
2241 test_completion "git --paginate check" "checkout " &&
2242 test_completion "git --git-dir=foo check" "checkout " &&
2243 test_completion "git --bare check" "checkout " &&
2244 test_completion "git --exec-path=foo check" "checkout " &&
2245 test_completion "git --html-path check" "checkout " &&
2246 test_completion "git --no-pager check" "checkout " &&
2247 test_completion "git --work-tree=foo check" "checkout " &&
2248 test_completion "git --namespace=foo check" "checkout " &&
2249 test_completion "git --paginate check" "checkout " &&
2250 test_completion "git --info-path check" "checkout " &&
2251 test_completion "git --no-replace-objects check" "checkout " &&
2252 test_completion "git --git-dir some/path check" "checkout " &&
2253 test_completion "git -c conf.var=value check" "checkout " &&
2254 test_completion "git -C some/path check" "checkout " &&
2255 test_completion "git --work-tree some/path check" "checkout " &&
2256 test_completion "git --namespace name/space check" "checkout "
2259 test_expect_success 'git --help completion' '
2260 test_completion "git --help ad" "add " &&
2261 test_completion "git --help core" "core-tutorial "
2264 test_expect_success 'completion.commands removes multiple commands' '
2265 test_config completion.commands "-cherry -mergetool" &&
2266 git --list-cmds=list-mainporcelain,list-complete,config >out &&
2267 ! grep -E "^(cherry|mergetool)$" out
2270 test_expect_success 'setup for integration tests' '
2271 echo content >file1 &&
2272 echo more >file2 &&
2273 git add file1 file2 &&
2274 git commit -m one &&
2275 git branch mybranch &&
2276 git tag mytag
2279 test_expect_success 'checkout completes ref names' '
2280 test_completion "git checkout m" <<-\EOF
2281 main Z
2282 mybranch Z
2283 mytag Z
2287 test_expect_success 'checkout does not match ref names of a different case' '
2288 test_completion "git checkout M" ""
2291 test_expect_success 'checkout matches case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2293 GIT_COMPLETION_IGNORE_CASE=1 &&
2294 test_completion "git checkout M" <<-\EOF
2295 main Z
2296 mybranch Z
2297 mytag Z
2302 test_expect_success 'checkout completes pseudo refs' '
2303 test_completion "git checkout H" <<-\EOF
2304 HEAD Z
2308 test_expect_success 'checkout completes pseudo refs case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2310 GIT_COMPLETION_IGNORE_CASE=1 &&
2311 test_completion "git checkout h" <<-\EOF
2312 HEAD Z
2317 test_expect_success 'git -C <path> checkout uses the right repo' '
2318 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
2319 branch-in-other Z
2323 test_expect_success 'show completes all refs' '
2324 test_completion "git show m" <<-\EOF
2325 main Z
2326 mybranch Z
2327 mytag Z
2331 test_expect_success '<ref>: completes paths' '
2332 test_completion "git show mytag:f" <<-\EOF
2333 file1Z
2334 file2Z
2338 test_expect_success 'complete tree filename with spaces' '
2339 echo content >"name with spaces" &&
2340 git add "name with spaces" &&
2341 git commit -m spaces &&
2342 test_completion "git show HEAD:nam" <<-\EOF
2343 name with spacesZ
2347 test_expect_success 'complete tree filename with metacharacters' '
2348 echo content >"name with \${meta}" &&
2349 git add "name with \${meta}" &&
2350 git commit -m meta &&
2351 test_completion "git show HEAD:nam" <<-\EOF
2352 name with ${meta}Z
2353 name with spacesZ
2357 test_expect_success PERL 'send-email' '
2358 test_completion "git send-email --cov" <<-\EOF &&
2359 --cover-from-description=Z
2360 --cover-letter Z
2362 test_completion "git send-email --val" <<-\EOF &&
2363 --validate Z
2365 test_completion "git send-email ma" "main "
2368 test_expect_success 'complete files' '
2369 git init tmp && cd tmp &&
2370 test_when_finished "cd .. && rm -rf tmp" &&
2372 echo "expected" > .gitignore &&
2373 echo "out" >> .gitignore &&
2374 echo "out_sorted" >> .gitignore &&
2376 git add .gitignore &&
2377 test_completion "git commit " ".gitignore" &&
2379 git commit -m ignore &&
2381 touch new &&
2382 test_completion "git add " "new" &&
2384 git add new &&
2385 git commit -a -m new &&
2386 test_completion "git add " "" &&
2388 git mv new modified &&
2389 echo modify > modified &&
2390 test_completion "git add " "modified" &&
2392 mkdir -p some/deep &&
2393 touch some/deep/path &&
2394 test_completion "git add some/" "some/deep" &&
2395 git clean -f some &&
2397 touch untracked &&
2399 : TODO .gitignore should not be here &&
2400 test_completion "git rm " <<-\EOF &&
2401 .gitignore
2402 modified
2405 test_completion "git clean " "untracked" &&
2407 : TODO .gitignore should not be here &&
2408 test_completion "git mv " <<-\EOF &&
2409 .gitignore
2410 modified
2413 mkdir dir &&
2414 touch dir/file-in-dir &&
2415 git add dir/file-in-dir &&
2416 git commit -m dir &&
2418 mkdir untracked-dir &&
2420 : TODO .gitignore should not be here &&
2421 test_completion "git mv modified " <<-\EOF &&
2422 .gitignore
2424 modified
2425 untracked
2426 untracked-dir
2429 test_completion "git commit " "modified" &&
2431 : TODO .gitignore should not be here &&
2432 test_completion "git ls-files " <<-\EOF &&
2433 .gitignore
2435 modified
2438 touch momified &&
2439 test_completion "git add mom" "momified"
2442 test_expect_success "simple alias" '
2443 test_config alias.co checkout &&
2444 test_completion "git co m" <<-\EOF
2445 main Z
2446 mybranch Z
2447 mytag Z
2451 test_expect_success "recursive alias" '
2452 test_config alias.co checkout &&
2453 test_config alias.cod "co --detached" &&
2454 test_completion "git cod m" <<-\EOF
2455 main Z
2456 mybranch Z
2457 mytag Z
2461 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
2462 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
2463 test_completion "git co m" <<-\EOF
2464 main Z
2465 mybranch Z
2466 mytag Z
2470 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
2471 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
2472 test_completion "git co m" <<-\EOF
2473 main Z
2474 mybranch Z
2475 mytag Z
2479 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
2480 test_config alias.co "!f() { : git checkout ; if ... } f" &&
2481 test_completion "git co m" <<-\EOF
2482 main Z
2483 mybranch Z
2484 mytag Z
2488 test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd> ; ... }' '
2489 test_config alias.co "!f() { : checkout ; if ... } f" &&
2490 test_completion "git co m" <<-\EOF
2491 main Z
2492 mybranch Z
2493 mytag Z
2497 test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd>; ... }' '
2498 test_config alias.co "!f() { : checkout; if ... } f" &&
2499 test_completion "git co m" <<-\EOF
2500 main Z
2501 mybranch Z
2502 mytag Z
2506 test_expect_success 'completion without explicit _git_xxx function' '
2507 test_completion "git version --" <<-\EOF
2508 --build-options Z
2509 --no-build-options Z
2513 test_expect_failure 'complete with tilde expansion' '
2514 git init tmp && cd tmp &&
2515 test_when_finished "cd .. && rm -rf tmp" &&
2517 touch ~/tmp/file &&
2519 test_completion "git add ~/tmp/" "~/tmp/file"
2522 test_expect_success 'setup other remote for remote reference completion' '
2523 git remote add other otherrepo &&
2524 git fetch other
2527 for flag in -d --delete
2529 test_expect_success "__git_complete_remote_or_refspec - push $flag other" '
2530 sed -e "s/Z$//" >expected <<-EOF &&
2531 main-in-other Z
2534 words=(git push '$flag' other ma) &&
2535 cword=${#words[@]} cur=${words[cword-1]} &&
2536 __git_cmd_idx=1 &&
2537 __git_complete_remote_or_refspec &&
2538 print_comp
2539 ) &&
2540 test_cmp expected out
2543 test_expect_failure "__git_complete_remote_or_refspec - push other $flag" '
2544 sed -e "s/Z$//" >expected <<-EOF &&
2545 main-in-other Z
2548 words=(git push other '$flag' ma) &&
2549 cword=${#words[@]} cur=${words[cword-1]} &&
2550 __git_cmd_idx=1 &&
2551 __git_complete_remote_or_refspec &&
2552 print_comp
2553 ) &&
2554 test_cmp expected out
2556 done
2558 test_expect_success 'git config - section' '
2559 test_completion "git config br" <<-\EOF
2560 branch.Z
2561 browser.Z
2565 test_expect_success 'git config - section include, includeIf' '
2566 test_completion "git config inclu" <<-\EOF
2567 include.Z
2568 includeIf.Z
2572 test_expect_success 'git config - variable name' '
2573 test_completion "git config log.d" <<-\EOF
2574 log.date Z
2575 log.decorate Z
2576 log.diffMerges Z
2580 test_expect_success 'git config - variable name include' '
2581 test_completion "git config include.p" <<-\EOF
2582 include.path Z
2586 test_expect_success 'setup for git config submodule tests' '
2587 test_create_repo sub &&
2588 test_commit -C sub initial &&
2589 git submodule add ./sub
2592 test_expect_success 'git config - variable name - submodule' '
2593 test_completion "git config submodule." <<-\EOF
2594 submodule.active Z
2595 submodule.alternateErrorStrategy Z
2596 submodule.alternateLocation Z
2597 submodule.fetchJobs Z
2598 submodule.propagateBranches Z
2599 submodule.recurse Z
2600 submodule.sub.Z
2604 test_expect_success 'git config - variable name - submodule names' '
2605 test_completion "git config submodule.sub." <<-\EOF
2606 submodule.sub.url Z
2607 submodule.sub.update Z
2608 submodule.sub.branch Z
2609 submodule.sub.fetchRecurseSubmodules Z
2610 submodule.sub.ignore Z
2611 submodule.sub.active Z
2615 test_expect_success 'git config - value' '
2616 test_completion "git config color.pager " <<-\EOF
2617 false Z
2618 true Z
2622 test_expect_success 'git -c - section' '
2623 test_completion "git -c br" <<-\EOF
2624 branch.Z
2625 browser.Z
2629 test_expect_success 'git -c - variable name' '
2630 test_completion "git -c log.d" <<-\EOF
2631 log.date=Z
2632 log.decorate=Z
2633 log.diffMerges=Z
2637 test_expect_success 'git -c - value' '
2638 test_completion "git -c color.pager=" <<-\EOF
2639 false Z
2640 true Z
2644 test_expect_success 'git clone --config= - section' '
2645 test_completion "git clone --config=br" <<-\EOF
2646 branch.Z
2647 browser.Z
2651 test_expect_success 'git clone --config= - variable name' '
2652 test_completion "git clone --config=log.d" <<-\EOF
2653 log.date=Z
2654 log.decorate=Z
2655 log.diffMerges=Z
2659 test_expect_success 'git clone --config= - value' '
2660 test_completion "git clone --config=color.pager=" <<-\EOF
2661 false Z
2662 true Z
2666 test_expect_success 'options with value' '
2667 test_completion "git merge -X diff-algorithm=" <<-\EOF
2672 test_expect_success 'sourcing the completion script clears cached commands' '
2674 __git_compute_all_commands &&
2675 test -n "$__git_all_commands" &&
2676 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2677 test -z "$__git_all_commands"
2681 test_expect_success 'sourcing the completion script clears cached merge strategies' '
2683 __git_compute_merge_strategies &&
2684 test -n "$__git_merge_strategies" &&
2685 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2686 test -z "$__git_merge_strategies"
2690 test_expect_success 'sourcing the completion script clears cached --options' '
2692 __gitcomp_builtin checkout &&
2693 test -n "$__gitcomp_builtin_checkout" &&
2694 __gitcomp_builtin notes_edit &&
2695 test -n "$__gitcomp_builtin_notes_edit" &&
2696 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2697 test -z "$__gitcomp_builtin_checkout" &&
2698 test -z "$__gitcomp_builtin_notes_edit"
2702 test_expect_success 'option aliases are not shown by default' '
2703 test_completion "git clone --recurs" "--recurse-submodules "
2706 test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
2708 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2709 GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2710 test_completion "git clone --recurs" <<-\EOF
2711 --recurse-submodules Z
2712 --recursive Z
2717 test_expect_success 'plumbing commands are excluded without GIT_COMPLETION_SHOW_ALL_COMMANDS' '
2719 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2720 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2722 # Just mainporcelain, not plumbing commands
2723 run_completion "git c" &&
2724 grep checkout out &&
2725 ! grep cat-file out
2729 test_expect_success 'all commands are shown with GIT_COMPLETION_SHOW_ALL_COMMANDS (also main non-builtin)' '
2731 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2732 GIT_COMPLETION_SHOW_ALL_COMMANDS=1 &&
2733 export GIT_COMPLETION_SHOW_ALL_COMMANDS &&
2734 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2736 # Both mainporcelain and plumbing commands
2737 run_completion "git c" &&
2738 grep checkout out &&
2739 grep cat-file out &&
2741 # Check "gitk", a "main" command, but not a built-in + more plumbing
2742 run_completion "git g" &&
2743 grep gitk out &&
2744 grep get-tar-commit-id out
2748 test_expect_success '__git_complete' '
2749 unset -f __git_wrap__git_main &&
2751 __git_complete foo __git_main &&
2752 __git_have_func __git_wrap__git_main &&
2753 unset -f __git_wrap__git_main &&
2755 __git_complete gf _git_fetch &&
2756 __git_have_func __git_wrap_git_fetch &&
2758 __git_complete foo git &&
2759 __git_have_func __git_wrap__git_main &&
2760 unset -f __git_wrap__git_main &&
2762 __git_complete gd git_diff &&
2763 __git_have_func __git_wrap_git_diff &&
2765 test_must_fail __git_complete ga missing
2768 test_expect_success '__git_pseudoref_exists' '
2769 test_when_finished "rm -rf repo" &&
2770 git init repo &&
2772 cd repo &&
2773 sane_unset __git_repo_path &&
2775 # HEAD should exist, even if it points to an unborn branch.
2776 __git_pseudoref_exists HEAD >output 2>&1 &&
2777 test_must_be_empty output &&
2779 # HEAD points to an existing branch, so it should exist.
2780 test_commit A &&
2781 __git_pseudoref_exists HEAD >output 2>&1 &&
2782 test_must_be_empty output &&
2784 # CHERRY_PICK_HEAD does not exist, so the existence check should fail.
2785 ! __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
2786 test_must_be_empty output &&
2788 # CHERRY_PICK_HEAD points to a commit, so it should exist.
2789 git update-ref CHERRY_PICK_HEAD A &&
2790 __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
2791 test_must_be_empty output
2795 test_done