completion: let 'for-each-ref' and 'ls-remote' filter matching refs
[git/debian.git] / t / t9902-completion.sh
blobcc9e741f929784582c418d25e919fd39ad426bfa
1 #!/bin/sh
3 # Copyright (c) 2012 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 this list:
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 # "filter-branch" and "ls-files" are listed for this.
33 GIT_TESTING_COMMAND_COMPLETION='add checkout check-attr filter-branch ls-files'
35 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
37 # We don't need this function to actually join words or do anything special.
38 # Also, it's cleaner to avoid touching bash's internal completion variables.
39 # So let's override it with a minimal version for testing purposes.
40 _get_comp_words_by_ref ()
42 while [ $# -gt 0 ]; do
43 case "$1" in
44 cur)
45 cur=${_words[_cword]}
47 prev)
48 prev=${_words[_cword-1]}
50 words)
51 words=("${_words[@]}")
53 cword)
54 cword=$_cword
56 esac
57 shift
58 done
61 print_comp ()
63 local IFS=$'\n'
64 echo "${COMPREPLY[*]}" > out
67 run_completion ()
69 local -a COMPREPLY _words
70 local _cword
71 _words=( $1 )
72 test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
73 (( _cword = ${#_words[@]} - 1 ))
74 __git_wrap__git_main && print_comp
77 # Test high-level completion
78 # Arguments are:
79 # 1: typed text so far (cur)
80 # 2: expected completion
81 test_completion ()
83 if test $# -gt 1
84 then
85 printf '%s\n' "$2" >expected
86 else
87 sed -e 's/Z$//' >expected
88 fi &&
89 run_completion "$1" &&
90 test_cmp expected out
93 # Test __gitcomp.
94 # The first argument is the typed text so far (cur); the rest are
95 # passed to __gitcomp. Expected output comes is read from the
96 # standard input, like test_completion().
97 test_gitcomp ()
99 local -a COMPREPLY &&
100 sed -e 's/Z$//' >expected &&
101 local cur="$1" &&
102 shift &&
103 __gitcomp "$@" &&
104 print_comp &&
105 test_cmp expected out
108 # Test __gitcomp_nl
109 # Arguments are:
110 # 1: current word (cur)
111 # -: the rest are passed to __gitcomp_nl
112 test_gitcomp_nl ()
114 local -a COMPREPLY &&
115 sed -e 's/Z$//' >expected &&
116 local cur="$1" &&
117 shift &&
118 __gitcomp_nl "$@" &&
119 print_comp &&
120 test_cmp expected out
123 invalid_variable_name='${foo.bar}'
125 actual="$TRASH_DIRECTORY/actual"
127 if test_have_prereq MINGW
128 then
129 ROOT="$(pwd -W)"
130 else
131 ROOT="$(pwd)"
134 test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
135 mkdir -p subdir/subsubdir &&
136 mkdir -p non-repo &&
137 git init otherrepo
140 test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '
141 echo "$ROOT/otherrepo/.git" >expected &&
143 __git_dir="$ROOT/otherrepo/.git" &&
144 __git_find_repo_path &&
145 echo "$__git_repo_path" >"$actual"
146 ) &&
147 test_cmp expected "$actual"
150 test_expect_success '__git_find_repo_path - .git directory in cwd' '
151 echo ".git" >expected &&
153 __git_find_repo_path &&
154 echo "$__git_repo_path" >"$actual"
155 ) &&
156 test_cmp expected "$actual"
159 test_expect_success '__git_find_repo_path - .git directory in parent' '
160 echo "$ROOT/.git" >expected &&
162 cd subdir/subsubdir &&
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 - cwd is a .git directory' '
170 echo "." >expected &&
172 cd .git &&
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 - parent is a .git directory' '
180 echo "$ROOT/.git" >expected &&
182 cd .git/refs/heads &&
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 - $GIT_DIR set while .git directory in cwd' '
190 echo "$ROOT/otherrepo/.git" >expected &&
192 GIT_DIR="$ROOT/otherrepo/.git" &&
193 export GIT_DIR &&
194 __git_find_repo_path &&
195 echo "$__git_repo_path" >"$actual"
196 ) &&
197 test_cmp expected "$actual"
200 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
201 echo "$ROOT/otherrepo/.git" >expected &&
203 GIT_DIR="$ROOT/otherrepo/.git" &&
204 export GIT_DIR &&
205 cd subdir &&
206 __git_find_repo_path &&
207 echo "$__git_repo_path" >"$actual"
208 ) &&
209 test_cmp expected "$actual"
212 test_expect_success '__git_find_repo_path - from command line while "git -C"' '
213 echo "$ROOT/.git" >expected &&
215 __git_dir="$ROOT/.git" &&
216 __git_C_args=(-C otherrepo) &&
217 __git_find_repo_path &&
218 echo "$__git_repo_path" >"$actual"
219 ) &&
220 test_cmp expected "$actual"
223 test_expect_success '__git_find_repo_path - relative dir from command line and "git -C"' '
224 echo "$ROOT/otherrepo/.git" >expected &&
226 cd subdir &&
227 __git_dir="otherrepo/.git" &&
228 __git_C_args=(-C ..) &&
229 __git_find_repo_path &&
230 echo "$__git_repo_path" >"$actual"
231 ) &&
232 test_cmp expected "$actual"
235 test_expect_success '__git_find_repo_path - $GIT_DIR set while "git -C"' '
236 echo "$ROOT/.git" >expected &&
238 GIT_DIR="$ROOT/.git" &&
239 export GIT_DIR &&
240 __git_C_args=(-C otherrepo) &&
241 __git_find_repo_path &&
242 echo "$__git_repo_path" >"$actual"
243 ) &&
244 test_cmp expected "$actual"
247 test_expect_success '__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
248 echo "$ROOT/otherrepo/.git" >expected &&
250 cd subdir &&
251 GIT_DIR="otherrepo/.git" &&
252 export GIT_DIR &&
253 __git_C_args=(-C ..) &&
254 __git_find_repo_path &&
255 echo "$__git_repo_path" >"$actual"
256 ) &&
257 test_cmp expected "$actual"
260 test_expect_success '__git_find_repo_path - "git -C" while .git directory in cwd' '
261 echo "$ROOT/otherrepo/.git" >expected &&
263 __git_C_args=(-C otherrepo) &&
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 cwd is a .git directory' '
271 echo "$ROOT/otherrepo/.git" >expected &&
273 cd .git &&
274 __git_C_args=(-C .. -C otherrepo) &&
275 __git_find_repo_path &&
276 echo "$__git_repo_path" >"$actual"
277 ) &&
278 test_cmp expected "$actual"
281 test_expect_success '__git_find_repo_path - "git -C" while .git directory in parent' '
282 echo "$ROOT/otherrepo/.git" >expected &&
284 cd subdir &&
285 __git_C_args=(-C .. -C otherrepo) &&
286 __git_find_repo_path &&
287 echo "$__git_repo_path" >"$actual"
288 ) &&
289 test_cmp expected "$actual"
292 test_expect_success '__git_find_repo_path - non-existing path in "git -C"' '
294 __git_C_args=(-C non-existing) &&
295 test_must_fail __git_find_repo_path &&
296 printf "$__git_repo_path" >"$actual"
297 ) &&
298 test_must_be_empty "$actual"
301 test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '
303 __git_dir="non-existing" &&
304 test_must_fail __git_find_repo_path &&
305 printf "$__git_repo_path" >"$actual"
306 ) &&
307 test_must_be_empty "$actual"
310 test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
312 GIT_DIR="$ROOT/non-existing" &&
313 export GIT_DIR &&
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 - gitfile in cwd' '
321 echo "$ROOT/otherrepo/.git" >expected &&
322 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
323 test_when_finished "rm -f subdir/.git" &&
325 cd subdir &&
326 __git_find_repo_path &&
327 echo "$__git_repo_path" >"$actual"
328 ) &&
329 test_cmp expected "$actual"
332 test_expect_success '__git_find_repo_path - gitfile in parent' '
333 echo "$ROOT/otherrepo/.git" >expected &&
334 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
335 test_when_finished "rm -f subdir/.git" &&
337 cd subdir/subsubdir &&
338 __git_find_repo_path &&
339 echo "$__git_repo_path" >"$actual"
340 ) &&
341 test_cmp expected "$actual"
344 test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '
345 echo "$ROOT/otherrepo/.git" >expected &&
346 mkdir otherrepo/dir &&
347 test_when_finished "rm -rf otherrepo/dir" &&
348 ln -s otherrepo/dir link &&
349 test_when_finished "rm -f link" &&
351 cd link &&
352 __git_find_repo_path &&
353 echo "$__git_repo_path" >"$actual"
354 ) &&
355 test_cmp expected "$actual"
358 test_expect_success '__git_find_repo_path - not a git repository' '
360 cd non-repo &&
361 GIT_CEILING_DIRECTORIES="$ROOT" &&
362 export GIT_CEILING_DIRECTORIES &&
363 test_must_fail __git_find_repo_path &&
364 printf "$__git_repo_path" >"$actual"
365 ) &&
366 test_must_be_empty "$actual"
369 test_expect_success '__gitdir - finds repo' '
370 echo "$ROOT/.git" >expected &&
372 cd subdir/subsubdir &&
373 __gitdir >"$actual"
374 ) &&
375 test_cmp expected "$actual"
379 test_expect_success '__gitdir - returns error when cant find repo' '
381 __git_dir="non-existing" &&
382 test_must_fail __gitdir >"$actual"
383 ) &&
384 test_must_be_empty "$actual"
387 test_expect_success '__gitdir - repo as argument' '
388 echo "otherrepo/.git" >expected &&
390 __gitdir "otherrepo" >"$actual"
391 ) &&
392 test_cmp expected "$actual"
395 test_expect_success '__gitdir - remote as argument' '
396 echo "remote" >expected &&
398 __gitdir "remote" >"$actual"
399 ) &&
400 test_cmp expected "$actual"
403 test_expect_success '__gitcomp - trailing space - options' '
404 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
405 --reset-author" <<-EOF
406 --reuse-message=Z
407 --reedit-message=Z
408 --reset-author Z
412 test_expect_success '__gitcomp - trailing space - config keys' '
413 test_gitcomp "br" "branch. branch.autosetupmerge
414 branch.autosetuprebase browser." <<-\EOF
415 branch.Z
416 branch.autosetupmerge Z
417 branch.autosetuprebase Z
418 browser.Z
422 test_expect_success '__gitcomp - option parameter' '
423 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
424 "" "re" <<-\EOF
425 recursive Z
426 resolve Z
430 test_expect_success '__gitcomp - prefix' '
431 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
432 "branch.maint." "me" <<-\EOF
433 branch.maint.merge Z
434 branch.maint.mergeoptions Z
438 test_expect_success '__gitcomp - suffix' '
439 test_gitcomp "branch.me" "master maint next pu" "branch." \
440 "ma" "." <<-\EOF
441 branch.master.Z
442 branch.maint.Z
446 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
447 __gitcomp "$invalid_variable_name"
450 read -r -d "" refs <<-\EOF
451 maint
452 master
453 next
457 test_expect_success '__gitcomp_nl - trailing space' '
458 test_gitcomp_nl "m" "$refs" <<-EOF
459 maint Z
460 master Z
464 test_expect_success '__gitcomp_nl - prefix' '
465 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
466 --fixup=maint Z
467 --fixup=master Z
471 test_expect_success '__gitcomp_nl - suffix' '
472 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
473 branch.maint.Z
474 branch.master.Z
478 test_expect_success '__gitcomp_nl - no suffix' '
479 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
480 maintZ
481 masterZ
485 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
486 __gitcomp_nl "$invalid_variable_name"
489 test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
490 cat >expect <<-EOF &&
491 remote_from_file_1
492 remote_from_file_2
493 remote_in_config_1
494 remote_in_config_2
496 test_when_finished "rm -rf .git/remotes" &&
497 mkdir -p .git/remotes &&
498 >.git/remotes/remote_from_file_1 &&
499 >.git/remotes/remote_from_file_2 &&
500 test_when_finished "git remote remove remote_in_config_1" &&
501 git remote add remote_in_config_1 git://remote_1 &&
502 test_when_finished "git remote remove remote_in_config_2" &&
503 git remote add remote_in_config_2 git://remote_2 &&
505 __git_remotes >actual
506 ) &&
507 test_cmp expect actual
510 test_expect_success '__git_is_configured_remote' '
511 test_when_finished "git remote remove remote_1" &&
512 git remote add remote_1 git://remote_1 &&
513 test_when_finished "git remote remove remote_2" &&
514 git remote add remote_2 git://remote_2 &&
516 verbose __git_is_configured_remote remote_2 &&
517 test_must_fail __git_is_configured_remote non-existent
521 test_expect_success 'setup for ref completion' '
522 git commit --allow-empty -m initial &&
523 git branch matching-branch &&
524 git tag matching-tag &&
526 cd otherrepo &&
527 git commit --allow-empty -m initial &&
528 git branch -m master master-in-other &&
529 git branch branch-in-other &&
530 git tag tag-in-other
531 ) &&
532 git remote add other "$ROOT/otherrepo/.git" &&
533 git fetch --no-tags other &&
534 rm -f .git/FETCH_HEAD &&
535 git init thirdrepo
538 test_expect_success '__git_refs - simple' '
539 cat >expected <<-EOF &&
540 HEAD
541 master
542 matching-branch
543 other/branch-in-other
544 other/master-in-other
545 matching-tag
548 cur= &&
549 __git_refs >"$actual"
550 ) &&
551 test_cmp expected "$actual"
554 test_expect_success '__git_refs - full refs' '
555 cat >expected <<-EOF &&
556 refs/heads/master
557 refs/heads/matching-branch
558 refs/remotes/other/branch-in-other
559 refs/remotes/other/master-in-other
560 refs/tags/matching-tag
563 cur=refs/heads/ &&
564 __git_refs >"$actual"
565 ) &&
566 test_cmp expected "$actual"
569 test_expect_success '__git_refs - repo given on the command line' '
570 cat >expected <<-EOF &&
571 HEAD
572 branch-in-other
573 master-in-other
574 tag-in-other
577 __git_dir="$ROOT/otherrepo/.git" &&
578 cur= &&
579 __git_refs >"$actual"
580 ) &&
581 test_cmp expected "$actual"
584 test_expect_success '__git_refs - remote on local file system' '
585 cat >expected <<-EOF &&
586 HEAD
587 branch-in-other
588 master-in-other
589 tag-in-other
592 cur= &&
593 __git_refs otherrepo >"$actual"
594 ) &&
595 test_cmp expected "$actual"
598 test_expect_success '__git_refs - remote on local file system - full refs' '
599 cat >expected <<-EOF &&
600 refs/heads/branch-in-other
601 refs/heads/master-in-other
602 refs/tags/tag-in-other
605 cur=refs/ &&
606 __git_refs otherrepo >"$actual"
607 ) &&
608 test_cmp expected "$actual"
611 test_expect_success '__git_refs - configured remote' '
612 cat >expected <<-EOF &&
613 HEAD
614 branch-in-other
615 master-in-other
618 cur= &&
619 __git_refs other >"$actual"
620 ) &&
621 test_cmp expected "$actual"
624 test_expect_success '__git_refs - configured remote - full refs' '
625 cat >expected <<-EOF &&
626 HEAD
627 refs/heads/branch-in-other
628 refs/heads/master-in-other
629 refs/tags/tag-in-other
632 cur=refs/ &&
633 __git_refs other >"$actual"
634 ) &&
635 test_cmp expected "$actual"
638 test_expect_success '__git_refs - configured remote - repo given on the command line' '
639 cat >expected <<-EOF &&
640 HEAD
641 branch-in-other
642 master-in-other
645 cd thirdrepo &&
646 __git_dir="$ROOT/.git" &&
647 cur= &&
648 __git_refs other >"$actual"
649 ) &&
650 test_cmp expected "$actual"
653 test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
654 cat >expected <<-EOF &&
655 HEAD
656 refs/heads/branch-in-other
657 refs/heads/master-in-other
658 refs/tags/tag-in-other
661 cd thirdrepo &&
662 __git_dir="$ROOT/.git" &&
663 cur=refs/ &&
664 __git_refs other >"$actual"
665 ) &&
666 test_cmp expected "$actual"
669 test_expect_success '__git_refs - configured remote - remote name matches a directory' '
670 cat >expected <<-EOF &&
671 HEAD
672 branch-in-other
673 master-in-other
675 mkdir other &&
676 test_when_finished "rm -rf other" &&
678 cur= &&
679 __git_refs other >"$actual"
680 ) &&
681 test_cmp expected "$actual"
684 test_expect_success '__git_refs - URL remote' '
685 cat >expected <<-EOF &&
686 HEAD
687 branch-in-other
688 master-in-other
689 tag-in-other
692 cur= &&
693 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
694 ) &&
695 test_cmp expected "$actual"
698 test_expect_success '__git_refs - URL remote - full refs' '
699 cat >expected <<-EOF &&
700 HEAD
701 refs/heads/branch-in-other
702 refs/heads/master-in-other
703 refs/tags/tag-in-other
706 cur=refs/ &&
707 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
708 ) &&
709 test_cmp expected "$actual"
712 test_expect_success '__git_refs - non-existing remote' '
714 cur= &&
715 __git_refs non-existing >"$actual"
716 ) &&
717 test_must_be_empty "$actual"
720 test_expect_success '__git_refs - non-existing remote - full refs' '
722 cur=refs/ &&
723 __git_refs non-existing >"$actual"
724 ) &&
725 test_must_be_empty "$actual"
728 test_expect_success '__git_refs - non-existing URL remote' '
730 cur= &&
731 __git_refs "file://$ROOT/non-existing" >"$actual"
732 ) &&
733 test_must_be_empty "$actual"
736 test_expect_success '__git_refs - non-existing URL remote - full refs' '
738 cur=refs/ &&
739 __git_refs "file://$ROOT/non-existing" >"$actual"
740 ) &&
741 test_must_be_empty "$actual"
744 test_expect_success '__git_refs - not in a git repository' '
746 GIT_CEILING_DIRECTORIES="$ROOT" &&
747 export GIT_CEILING_DIRECTORIES &&
748 cd subdir &&
749 cur= &&
750 __git_refs >"$actual"
751 ) &&
752 test_must_be_empty "$actual"
755 test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
756 cat >expected <<-EOF &&
757 HEAD
758 master
759 matching-branch
760 other/ambiguous
761 other/branch-in-other
762 other/master-in-other
763 remote/ambiguous
764 remote/branch-in-remote
765 matching-tag
766 branch-in-other
767 branch-in-remote
768 master-in-other
770 for remote_ref in refs/remotes/other/ambiguous \
771 refs/remotes/remote/ambiguous \
772 refs/remotes/remote/branch-in-remote
774 git update-ref $remote_ref master &&
775 test_when_finished "git update-ref -d $remote_ref"
776 done &&
778 cur= &&
779 __git_refs "" 1 >"$actual"
780 ) &&
781 test_cmp expected "$actual"
784 test_expect_success '__git_refs - after --opt=' '
785 cat >expected <<-EOF &&
786 HEAD
787 master
788 matching-branch
789 other/branch-in-other
790 other/master-in-other
791 matching-tag
794 cur="--opt=" &&
795 __git_refs "" "" "" "" >"$actual"
796 ) &&
797 test_cmp expected "$actual"
800 test_expect_success '__git_refs - after --opt= - full refs' '
801 cat >expected <<-EOF &&
802 refs/heads/master
803 refs/heads/matching-branch
804 refs/remotes/other/branch-in-other
805 refs/remotes/other/master-in-other
806 refs/tags/matching-tag
809 cur="--opt=refs/" &&
810 __git_refs "" "" "" refs/ >"$actual"
811 ) &&
812 test_cmp expected "$actual"
815 test_expect_success '__git refs - exluding refs' '
816 cat >expected <<-EOF &&
817 ^HEAD
818 ^master
819 ^matching-branch
820 ^other/branch-in-other
821 ^other/master-in-other
822 ^matching-tag
825 cur=^ &&
826 __git_refs >"$actual"
827 ) &&
828 test_cmp expected "$actual"
831 test_expect_success '__git refs - exluding full refs' '
832 cat >expected <<-EOF &&
833 ^refs/heads/master
834 ^refs/heads/matching-branch
835 ^refs/remotes/other/branch-in-other
836 ^refs/remotes/other/master-in-other
837 ^refs/tags/matching-tag
840 cur=^refs/ &&
841 __git_refs >"$actual"
842 ) &&
843 test_cmp expected "$actual"
846 test_expect_success 'setup for filtering matching refs' '
847 git branch matching/branch &&
848 git tag matching/tag &&
849 git -C otherrepo branch matching/branch-in-other &&
850 git fetch --no-tags other &&
851 rm -f .git/FETCH_HEAD
854 test_expect_success '__git_refs - dont filter refs unless told so' '
855 cat >expected <<-EOF &&
856 HEAD
857 master
858 matching-branch
859 matching/branch
860 other/branch-in-other
861 other/master-in-other
862 other/matching/branch-in-other
863 matching-tag
864 matching/tag
867 cur=master &&
868 __git_refs >"$actual"
869 ) &&
870 test_cmp expected "$actual"
873 test_expect_success '__git_refs - only matching refs' '
874 cat >expected <<-EOF &&
875 matching-branch
876 matching/branch
877 matching-tag
878 matching/tag
881 cur=mat &&
882 __git_refs "" "" "" "$cur" >"$actual"
883 ) &&
884 test_cmp expected "$actual"
887 test_expect_success '__git_refs - only matching refs - full refs' '
888 cat >expected <<-EOF &&
889 refs/heads/matching-branch
890 refs/heads/matching/branch
893 cur=refs/heads/mat &&
894 __git_refs "" "" "" "$cur" >"$actual"
895 ) &&
896 test_cmp expected "$actual"
899 test_expect_success '__git_refs - only matching refs - remote on local file system' '
900 cat >expected <<-EOF &&
901 master-in-other
902 matching/branch-in-other
905 cur=ma &&
906 __git_refs otherrepo "" "" "$cur" >"$actual"
907 ) &&
908 test_cmp expected "$actual"
911 test_expect_success '__git_refs - only matching refs - configured remote' '
912 cat >expected <<-EOF &&
913 master-in-other
914 matching/branch-in-other
917 cur=ma &&
918 __git_refs other "" "" "$cur" >"$actual"
919 ) &&
920 test_cmp expected "$actual"
923 test_expect_success '__git_refs - only matching refs - remote - full refs' '
924 cat >expected <<-EOF &&
925 refs/heads/master-in-other
926 refs/heads/matching/branch-in-other
929 cur=refs/heads/ma &&
930 __git_refs other "" "" "$cur" >"$actual"
931 ) &&
932 test_cmp expected "$actual"
935 test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
936 cat >expected <<-EOF &&
937 matching-branch
938 matching/branch
939 matching-tag
940 matching/tag
941 matching/branch-in-other
943 for remote_ref in refs/remotes/other/ambiguous \
944 refs/remotes/remote/ambiguous \
945 refs/remotes/remote/branch-in-remote
947 git update-ref $remote_ref master &&
948 test_when_finished "git update-ref -d $remote_ref"
949 done &&
951 cur=mat &&
952 __git_refs "" 1 "" "$cur" >"$actual"
953 ) &&
954 test_cmp expected "$actual"
957 test_expect_success 'teardown after filtering matching refs' '
958 git branch -d matching/branch &&
959 git tag -d matching/tag &&
960 git update-ref -d refs/remotes/other/matching/branch-in-other &&
961 git -C otherrepo branch -D matching/branch-in-other
964 test_expect_success '__git_complete_refs - simple' '
965 sed -e "s/Z$//" >expected <<-EOF &&
966 HEAD Z
967 master Z
968 matching-branch Z
969 other/branch-in-other Z
970 other/master-in-other Z
971 matching-tag Z
974 cur= &&
975 __git_complete_refs &&
976 print_comp
977 ) &&
978 test_cmp expected out
981 test_expect_success '__git_complete_refs - matching' '
982 sed -e "s/Z$//" >expected <<-EOF &&
983 matching-branch Z
984 matching-tag Z
987 cur=mat &&
988 __git_complete_refs &&
989 print_comp
990 ) &&
991 test_cmp expected out
994 test_expect_success '__git_complete_refs - remote' '
995 sed -e "s/Z$//" >expected <<-EOF &&
996 HEAD Z
997 branch-in-other Z
998 master-in-other Z
1001 cur=
1002 __git_complete_refs --remote=other &&
1003 print_comp
1004 ) &&
1005 test_cmp expected out
1008 test_expect_success '__git_complete_refs - track' '
1009 sed -e "s/Z$//" >expected <<-EOF &&
1010 HEAD Z
1011 master Z
1012 matching-branch Z
1013 other/branch-in-other Z
1014 other/master-in-other Z
1015 matching-tag Z
1016 branch-in-other Z
1017 master-in-other Z
1020 cur=
1021 __git_complete_refs --track &&
1022 print_comp
1023 ) &&
1024 test_cmp expected out
1027 test_expect_success '__git_complete_refs - current word' '
1028 sed -e "s/Z$//" >expected <<-EOF &&
1029 matching-branch Z
1030 matching-tag Z
1033 cur="--option=mat" &&
1034 __git_complete_refs --cur="${cur#*=}" &&
1035 print_comp
1036 ) &&
1037 test_cmp expected out
1040 test_expect_success '__git_complete_refs - prefix' '
1041 sed -e "s/Z$//" >expected <<-EOF &&
1042 v1.0..matching-branch Z
1043 v1.0..matching-tag Z
1046 cur=v1.0..mat &&
1047 __git_complete_refs --pfx=v1.0.. --cur=mat &&
1048 print_comp
1049 ) &&
1050 test_cmp expected out
1053 test_expect_success '__git_complete_refs - suffix' '
1054 cat >expected <<-EOF &&
1055 HEAD.
1056 master.
1057 matching-branch.
1058 other/branch-in-other.
1059 other/master-in-other.
1060 matching-tag.
1063 cur= &&
1064 __git_complete_refs --sfx=. &&
1065 print_comp
1066 ) &&
1067 test_cmp expected out
1070 test_expect_success '__git_complete_fetch_refspecs - simple' '
1071 sed -e "s/Z$//" >expected <<-EOF &&
1072 HEAD:HEAD Z
1073 branch-in-other:branch-in-other Z
1074 master-in-other:master-in-other Z
1077 cur= &&
1078 __git_complete_fetch_refspecs other &&
1079 print_comp
1080 ) &&
1081 test_cmp expected out
1084 test_expect_success '__git_complete_fetch_refspecs - matching' '
1085 sed -e "s/Z$//" >expected <<-EOF &&
1086 branch-in-other:branch-in-other Z
1089 cur=br &&
1090 __git_complete_fetch_refspecs other "" br &&
1091 print_comp
1092 ) &&
1093 test_cmp expected out
1096 test_expect_success '__git_complete_fetch_refspecs - prefix' '
1097 sed -e "s/Z$//" >expected <<-EOF &&
1098 +HEAD:HEAD Z
1099 +branch-in-other:branch-in-other Z
1100 +master-in-other:master-in-other Z
1103 cur="+" &&
1104 __git_complete_fetch_refspecs other "+" "" &&
1105 print_comp
1106 ) &&
1107 test_cmp expected out
1110 test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
1111 sed -e "s/Z$//" >expected <<-EOF &&
1112 refs/heads/branch-in-other:refs/heads/branch-in-other Z
1113 refs/heads/master-in-other:refs/heads/master-in-other Z
1114 refs/tags/tag-in-other:refs/tags/tag-in-other Z
1117 cur=refs/ &&
1118 __git_complete_fetch_refspecs other "" refs/ &&
1119 print_comp
1120 ) &&
1121 test_cmp expected out
1124 test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
1125 sed -e "s/Z$//" >expected <<-EOF &&
1126 +refs/heads/branch-in-other:refs/heads/branch-in-other Z
1127 +refs/heads/master-in-other:refs/heads/master-in-other Z
1128 +refs/tags/tag-in-other:refs/tags/tag-in-other Z
1131 cur=+refs/ &&
1132 __git_complete_fetch_refspecs other + refs/ &&
1133 print_comp
1134 ) &&
1135 test_cmp expected out
1138 test_expect_success 'teardown after ref completion' '
1139 git branch -d matching-branch &&
1140 git tag -d matching-tag &&
1141 git remote remove other
1144 test_expect_success '__git_get_config_variables' '
1145 cat >expect <<-EOF &&
1146 name-1
1147 name-2
1149 test_config interesting.name-1 good &&
1150 test_config interesting.name-2 good &&
1151 test_config subsection.interesting.name-3 bad &&
1152 __git_get_config_variables interesting >actual &&
1153 test_cmp expect actual
1156 test_expect_success '__git_pretty_aliases' '
1157 cat >expect <<-EOF &&
1158 author
1159 hash
1161 test_config pretty.author "%an %ae" &&
1162 test_config pretty.hash %H &&
1163 __git_pretty_aliases >actual &&
1164 test_cmp expect actual
1167 test_expect_success '__git_aliases' '
1168 cat >expect <<-EOF &&
1172 test_config alias.ci commit &&
1173 test_config alias.co checkout &&
1174 __git_aliases >actual &&
1175 test_cmp expect actual
1178 test_expect_success 'basic' '
1179 run_completion "git " &&
1180 # built-in
1181 grep -q "^add \$" out &&
1182 # script
1183 grep -q "^filter-branch \$" out &&
1184 # plumbing
1185 ! grep -q "^ls-files \$" out &&
1187 run_completion "git f" &&
1188 ! grep -q -v "^f" out
1191 test_expect_success 'double dash "git" itself' '
1192 test_completion "git --" <<-\EOF
1193 --paginate Z
1194 --no-pager Z
1195 --git-dir=
1196 --bare Z
1197 --version Z
1198 --exec-path Z
1199 --exec-path=
1200 --html-path Z
1201 --man-path Z
1202 --info-path Z
1203 --work-tree=
1204 --namespace=
1205 --no-replace-objects Z
1206 --help Z
1210 test_expect_success 'double dash "git checkout"' '
1211 test_completion "git checkout --" <<-\EOF
1212 --quiet Z
1213 --ours Z
1214 --theirs Z
1215 --track Z
1216 --no-track Z
1217 --merge Z
1218 --conflict=
1219 --orphan Z
1220 --patch Z
1224 test_expect_success 'general options' '
1225 test_completion "git --ver" "--version " &&
1226 test_completion "git --hel" "--help " &&
1227 test_completion "git --exe" <<-\EOF &&
1228 --exec-path Z
1229 --exec-path=
1231 test_completion "git --htm" "--html-path " &&
1232 test_completion "git --pag" "--paginate " &&
1233 test_completion "git --no-p" "--no-pager " &&
1234 test_completion "git --git" "--git-dir=" &&
1235 test_completion "git --wor" "--work-tree=" &&
1236 test_completion "git --nam" "--namespace=" &&
1237 test_completion "git --bar" "--bare " &&
1238 test_completion "git --inf" "--info-path " &&
1239 test_completion "git --no-r" "--no-replace-objects "
1242 test_expect_success 'general options plus command' '
1243 test_completion "git --version check" "checkout " &&
1244 test_completion "git --paginate check" "checkout " &&
1245 test_completion "git --git-dir=foo check" "checkout " &&
1246 test_completion "git --bare check" "checkout " &&
1247 test_completion "git --exec-path=foo check" "checkout " &&
1248 test_completion "git --html-path check" "checkout " &&
1249 test_completion "git --no-pager check" "checkout " &&
1250 test_completion "git --work-tree=foo check" "checkout " &&
1251 test_completion "git --namespace=foo check" "checkout " &&
1252 test_completion "git --paginate check" "checkout " &&
1253 test_completion "git --info-path check" "checkout " &&
1254 test_completion "git --no-replace-objects check" "checkout " &&
1255 test_completion "git --git-dir some/path check" "checkout " &&
1256 test_completion "git -c conf.var=value check" "checkout " &&
1257 test_completion "git -C some/path check" "checkout " &&
1258 test_completion "git --work-tree some/path check" "checkout " &&
1259 test_completion "git --namespace name/space check" "checkout "
1262 test_expect_success 'git --help completion' '
1263 test_completion "git --help ad" "add " &&
1264 test_completion "git --help core" "core-tutorial "
1267 test_expect_success 'setup for integration tests' '
1268 echo content >file1 &&
1269 echo more >file2 &&
1270 git add file1 file2 &&
1271 git commit -m one &&
1272 git branch mybranch &&
1273 git tag mytag
1276 test_expect_success 'checkout completes ref names' '
1277 test_completion "git checkout m" <<-\EOF
1278 master Z
1279 mybranch Z
1280 mytag Z
1284 test_expect_success 'git -C <path> checkout uses the right repo' '
1285 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
1286 branch-in-other Z
1290 test_expect_success 'show completes all refs' '
1291 test_completion "git show m" <<-\EOF
1292 master Z
1293 mybranch Z
1294 mytag Z
1298 test_expect_success '<ref>: completes paths' '
1299 test_completion "git show mytag:f" <<-\EOF
1300 file1 Z
1301 file2 Z
1305 test_expect_success 'complete tree filename with spaces' '
1306 echo content >"name with spaces" &&
1307 git add "name with spaces" &&
1308 git commit -m spaces &&
1309 test_completion "git show HEAD:nam" <<-\EOF
1310 name with spaces Z
1314 test_expect_success 'complete tree filename with metacharacters' '
1315 echo content >"name with \${meta}" &&
1316 git add "name with \${meta}" &&
1317 git commit -m meta &&
1318 test_completion "git show HEAD:nam" <<-\EOF
1319 name with ${meta} Z
1320 name with spaces Z
1324 test_expect_success 'send-email' '
1325 test_completion "git send-email --cov" "--cover-letter " &&
1326 test_completion "git send-email ma" "master "
1329 test_expect_success 'complete files' '
1330 git init tmp && cd tmp &&
1331 test_when_finished "cd .. && rm -rf tmp" &&
1333 echo "expected" > .gitignore &&
1334 echo "out" >> .gitignore &&
1336 git add .gitignore &&
1337 test_completion "git commit " ".gitignore" &&
1339 git commit -m ignore &&
1341 touch new &&
1342 test_completion "git add " "new" &&
1344 git add new &&
1345 git commit -a -m new &&
1346 test_completion "git add " "" &&
1348 git mv new modified &&
1349 echo modify > modified &&
1350 test_completion "git add " "modified" &&
1352 touch untracked &&
1354 : TODO .gitignore should not be here &&
1355 test_completion "git rm " <<-\EOF &&
1356 .gitignore
1357 modified
1360 test_completion "git clean " "untracked" &&
1362 : TODO .gitignore should not be here &&
1363 test_completion "git mv " <<-\EOF &&
1364 .gitignore
1365 modified
1368 mkdir dir &&
1369 touch dir/file-in-dir &&
1370 git add dir/file-in-dir &&
1371 git commit -m dir &&
1373 mkdir untracked-dir &&
1375 : TODO .gitignore should not be here &&
1376 test_completion "git mv modified " <<-\EOF &&
1377 .gitignore
1379 modified
1380 untracked
1381 untracked-dir
1384 test_completion "git commit " "modified" &&
1386 : TODO .gitignore should not be here &&
1387 test_completion "git ls-files " <<-\EOF &&
1388 .gitignore
1390 modified
1393 touch momified &&
1394 test_completion "git add mom" "momified"
1397 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
1398 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
1399 test_completion "git co m" <<-\EOF
1400 master Z
1401 mybranch Z
1402 mytag Z
1406 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
1407 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
1408 test_completion "git co m" <<-\EOF
1409 master Z
1410 mybranch Z
1411 mytag Z
1415 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
1416 test_config alias.co "!f() { : git checkout ; if ... } f" &&
1417 test_completion "git co m" <<-\EOF
1418 master Z
1419 mybranch Z
1420 mytag Z
1424 test_expect_failure 'complete with tilde expansion' '
1425 git init tmp && cd tmp &&
1426 test_when_finished "cd .. && rm -rf tmp" &&
1428 touch ~/tmp/file &&
1430 test_completion "git add ~/tmp/" "~/tmp/file"
1433 test_done