3 # Copyright (c) 2012 Felipe Contreras
6 test_description
='test bash completion'
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
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
48 prev
=${_words[_cword-1]}
51 words
=("${_words[@]}")
64 echo "${COMPREPLY[*]}" > out
69 local -a COMPREPLY _words
72 test "${1: -1}" = ' ' && _words
[${#_words[@]}+1]=''
73 (( _cword
= ${#_words[@]} - 1 ))
74 __git_wrap__git_main
&& print_comp
77 # Test high-level completion
79 # 1: typed text so far (cur)
80 # 2: expected completion
85 printf '%s\n' "$2" >expected
87 sed -e 's/Z$//' >expected
89 run_completion
"$1" &&
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().
100 sed -e 's/Z$//' >expected
&&
105 test_cmp expected out
110 # 1: current word (cur)
111 # -: the rest are passed to __gitcomp_nl
114 local -a COMPREPLY
&&
115 sed -e 's/Z$//' >expected
&&
120 test_cmp expected out
123 invalid_variable_name
='${foo.bar}'
125 actual
="$TRASH_DIRECTORY/actual"
127 if test_have_prereq MINGW
134 test_expect_success
'setup for __gitdir tests' '
135 mkdir -p subdir/subsubdir &&
139 test_expect_success
'__gitdir - from command line (through $__git_dir)' '
140 echo "$ROOT/otherrepo/.git" >expected &&
142 __git_dir="$ROOT/otherrepo/.git" &&
145 test_cmp expected "$actual"
148 test_expect_success
'__gitdir - repo as argument' '
149 echo "otherrepo/.git" >expected &&
151 __gitdir "otherrepo" >"$actual"
153 test_cmp expected "$actual"
156 test_expect_success
'__gitdir - remote as argument' '
157 echo "remote" >expected &&
159 __gitdir "remote" >"$actual"
161 test_cmp expected "$actual"
164 test_expect_success
'__gitdir - .git directory in cwd' '
165 echo ".git" >expected &&
169 test_cmp expected "$actual"
172 test_expect_success
'__gitdir - .git directory in parent' '
173 echo "$ROOT/.git" >expected &&
175 cd subdir/subsubdir &&
178 test_cmp expected "$actual"
181 test_expect_success
'__gitdir - cwd is a .git directory' '
182 echo "." >expected &&
187 test_cmp expected "$actual"
190 test_expect_success
'__gitdir - parent is a .git directory' '
191 echo "$ROOT/.git" >expected &&
193 cd .git/refs/heads &&
196 test_cmp expected "$actual"
199 test_expect_success
'__gitdir - $GIT_DIR set while .git directory in cwd' '
200 echo "$ROOT/otherrepo/.git" >expected &&
202 GIT_DIR="$ROOT/otherrepo/.git" &&
206 test_cmp expected "$actual"
209 test_expect_success
'__gitdir - $GIT_DIR set while .git directory in parent' '
210 echo "$ROOT/otherrepo/.git" >expected &&
212 GIT_DIR="$ROOT/otherrepo/.git" &&
217 test_cmp expected "$actual"
220 test_expect_success
'__gitdir - from command line while "git -C"' '
221 echo "$ROOT/.git" >expected &&
223 __git_dir="$ROOT/.git" &&
224 __git_C_args=(-C otherrepo) &&
227 test_cmp expected "$actual"
230 test_expect_success
'__gitdir - relative dir from command line and "git -C"' '
231 echo "$ROOT/otherrepo/.git" >expected &&
234 __git_dir="otherrepo/.git" &&
235 __git_C_args=(-C ..) &&
238 test_cmp expected "$actual"
241 test_expect_success
'__gitdir - $GIT_DIR set while "git -C"' '
242 echo "$ROOT/.git" >expected &&
244 GIT_DIR="$ROOT/.git" &&
246 __git_C_args=(-C otherrepo) &&
249 test_cmp expected "$actual"
252 test_expect_success
'__gitdir - relative dir in $GIT_DIR and "git -C"' '
253 echo "$ROOT/otherrepo/.git" >expected &&
256 GIT_DIR="otherrepo/.git" &&
258 __git_C_args=(-C ..) &&
261 test_cmp expected "$actual"
264 test_expect_success
'__gitdir - "git -C" while .git directory in cwd' '
265 echo "$ROOT/otherrepo/.git" >expected &&
267 __git_C_args=(-C otherrepo) &&
270 test_cmp expected "$actual"
273 test_expect_success
'__gitdir - "git -C" while cwd is a .git directory' '
274 echo "$ROOT/otherrepo/.git" >expected &&
277 __git_C_args=(-C .. -C otherrepo) &&
280 test_cmp expected "$actual"
283 test_expect_success
'__gitdir - "git -C" while .git directory in parent' '
284 echo "$ROOT/otherrepo/.git" >expected &&
287 __git_C_args=(-C .. -C otherrepo) &&
290 test_cmp expected "$actual"
293 test_expect_success
'__gitdir - non-existing path in "git -C"' '
295 __git_C_args=(-C non-existing) &&
296 test_must_fail __gitdir >"$actual"
298 test_must_be_empty "$actual"
301 test_expect_success
'__gitdir - non-existing path in $__git_dir' '
303 __git_dir="non-existing" &&
304 test_must_fail __gitdir >"$actual"
306 test_must_be_empty "$actual"
309 test_expect_success
'__gitdir - non-existing $GIT_DIR' '
311 GIT_DIR="$ROOT/non-existing" &&
313 test_must_fail __gitdir >"$actual"
315 test_must_be_empty "$actual"
318 test_expect_success
'__gitdir - gitfile in cwd' '
319 echo "$ROOT/otherrepo/.git" >expected &&
320 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
321 test_when_finished "rm -f subdir/.git" &&
326 test_cmp expected "$actual"
329 test_expect_success
'__gitdir - gitfile in parent' '
330 echo "$ROOT/otherrepo/.git" >expected &&
331 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
332 test_when_finished "rm -f subdir/.git" &&
334 cd subdir/subsubdir &&
337 test_cmp expected "$actual"
340 test_expect_success SYMLINKS
'__gitdir - resulting path avoids symlinks' '
341 echo "$ROOT/otherrepo/.git" >expected &&
342 mkdir otherrepo/dir &&
343 test_when_finished "rm -rf otherrepo/dir" &&
344 ln -s otherrepo/dir link &&
345 test_when_finished "rm -f link" &&
350 test_cmp expected "$actual"
353 test_expect_success
'__gitdir - not a git repository' '
354 nongit test_must_fail __gitdir >"$actual" &&
355 test_must_be_empty "$actual"
358 test_expect_success
'__gitcomp - trailing space - options' '
359 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
360 --reset-author" <<-EOF
367 test_expect_success
'__gitcomp - trailing space - config keys' '
368 test_gitcomp "br" "branch. branch.autosetupmerge
369 branch.autosetuprebase browser." <<-\EOF
371 branch.autosetupmerge Z
372 branch.autosetuprebase Z
377 test_expect_success
'__gitcomp - option parameter' '
378 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
385 test_expect_success
'__gitcomp - prefix' '
386 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
387 "branch.maint." "me" <<-\EOF
389 branch.maint.mergeoptions Z
393 test_expect_success
'__gitcomp - suffix' '
394 test_gitcomp "branch.me" "master maint next pu" "branch." \
401 test_expect_success
'__gitcomp - doesnt fail because of invalid variable name' '
402 __gitcomp "$invalid_variable_name"
405 read -r -d "" refs
<<-\EOF
412 test_expect_success '__gitcomp_nl - trailing space
' '
413 test_gitcomp_nl
"m" "$refs" <<-EOF
419 test_expect_success '__gitcomp_nl
- prefix
' '
420 test_gitcomp_nl
"--fixup=m" "$refs" "--fixup=" "m" <<-EOF
426 test_expect_success '__gitcomp_nl
- suffix
' '
427 test_gitcomp_nl
"branch.ma" "$refs" "branch." "ma" "." <<-\EOF
433 test_expect_success '__gitcomp_nl - no suffix
' '
434 test_gitcomp_nl
"ma" "$refs" "" "ma" "" <<-\EOF
440 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name
' '
441 __gitcomp_nl
"$invalid_variable_name"
444 test_expect_success '__git_remotes
- list remotes from
$GIT_DIR/remotes and from config
file' '
445 cat >expect
<<-EOF &&
451 test_when_finished
"rm -rf .git/remotes" &&
452 mkdir
-p .git
/remotes
&&
453 >.git
/remotes
/remote_from_file_1
&&
454 >.git
/remotes
/remote_from_file_2
&&
455 test_when_finished
"git remote remove remote_in_config_1" &&
456 git remote add remote_in_config_1 git
://remote_1
&&
457 test_when_finished
"git remote remove remote_in_config_2" &&
458 git remote add remote_in_config_2 git
://remote_2
&&
460 __git_remotes
>actual
462 test_cmp expect actual
465 test_expect_success '__git_is_configured_remote
' '
466 test_when_finished
"git remote remove remote_1" &&
467 git remote add remote_1 git
://remote_1
&&
468 test_when_finished
"git remote remove remote_2" &&
469 git remote add remote_2 git
://remote_2
&&
471 verbose __git_is_configured_remote remote_2
&&
472 test_must_fail __git_is_configured_remote non-existent
476 test_expect_success 'setup
for ref completion
' '
477 git commit
--allow-empty -m initial
&&
478 git branch matching-branch
&&
479 git tag matching-tag
&&
482 git commit
--allow-empty -m initial
&&
483 git branch
-m master master-in-other
&&
484 git branch branch-in-other
&&
487 git remote add other
"$ROOT/otherrepo/.git" &&
488 git fetch
--no-tags other
&&
489 rm -f .git
/FETCH_HEAD
&&
493 test_expect_success '__git_refs
- simple
' '
494 cat >expected
<<-EOF &&
498 other/branch-in-other
499 other/master-in-other
504 __git_refs
>"$actual"
506 test_cmp expected
"$actual"
509 test_expect_success '__git_refs
- full refs
' '
510 cat >expected
<<-EOF &&
512 refs/heads/matching-branch
516 __git_refs
>"$actual"
518 test_cmp expected
"$actual"
521 test_expect_success '__git_refs
- repo given on the
command line
' '
522 cat >expected
<<-EOF &&
529 __git_dir
="$ROOT/otherrepo/.git" &&
531 __git_refs
>"$actual"
533 test_cmp expected
"$actual"
536 test_expect_success '__git_refs
- remote on
local file system
' '
537 cat >expected
<<-EOF &&
545 __git_refs otherrepo
>"$actual"
547 test_cmp expected
"$actual"
550 test_expect_success '__git_refs
- remote on
local file system
- full refs
' '
551 cat >expected
<<-EOF &&
552 refs/heads/branch-in-other
553 refs/heads/master-in-other
554 refs/tags/tag-in-other
558 __git_refs otherrepo
>"$actual"
560 test_cmp expected
"$actual"
563 test_expect_success '__git_refs
- configured remote
' '
564 cat >expected
<<-EOF &&
571 __git_refs other
>"$actual"
573 test_cmp expected
"$actual"
576 test_expect_success '__git_refs
- configured remote
- full refs
' '
577 cat >expected
<<-EOF &&
578 refs/heads/branch-in-other
579 refs/heads/master-in-other
580 refs/tags/tag-in-other
584 __git_refs other
>"$actual"
586 test_cmp expected
"$actual"
589 test_expect_success '__git_refs
- configured remote
- repo given on the
command line
' '
590 cat >expected
<<-EOF &&
597 __git_dir
="$ROOT/.git" &&
599 __git_refs other
>"$actual"
601 test_cmp expected
"$actual"
604 test_expect_success '__git_refs
- configured remote
- full refs
- repo given on the
command line
' '
605 cat >expected
<<-EOF &&
606 refs/heads/branch-in-other
607 refs/heads/master-in-other
608 refs/tags/tag-in-other
612 __git_dir
="$ROOT/.git" &&
614 __git_refs other
>"$actual"
616 test_cmp expected
"$actual"
619 test_expect_success '__git_refs
- configured remote
- remote name matches a directory
' '
620 cat >expected
<<-EOF &&
626 test_when_finished
"rm -rf other" &&
629 __git_refs other
>"$actual"
631 test_cmp expected
"$actual"
634 test_expect_success '__git_refs
- URL remote
' '
635 cat >expected
<<-EOF &&
643 __git_refs
"file://$ROOT/otherrepo/.git" >"$actual"
645 test_cmp expected
"$actual"
648 test_expect_success '__git_refs
- URL remote
- full refs
' '
649 cat >expected
<<-EOF &&
650 refs/heads/branch-in-other
651 refs/heads/master-in-other
652 refs/tags/tag-in-other
656 __git_refs
"file://$ROOT/otherrepo/.git" >"$actual"
658 test_cmp expected
"$actual"
661 test_expect_success '__git_refs
- non-existing remote
' '
664 __git_refs non-existing
>"$actual"
666 test_must_be_empty
"$actual"
669 test_expect_success '__git_refs
- non-existing remote
- full refs
' '
672 __git_refs non-existing
>"$actual"
674 test_must_be_empty
"$actual"
677 test_expect_success '__git_refs
- non-existing URL remote
' '
680 __git_refs
"file://$ROOT/non-existing" >"$actual"
682 test_must_be_empty
"$actual"
685 test_expect_success '__git_refs
- non-existing URL remote
- full refs
' '
688 __git_refs
"file://$ROOT/non-existing" >"$actual"
690 test_must_be_empty
"$actual"
693 test_expect_success '__git_refs
- not
in a git repository
' '
695 GIT_CEILING_DIRECTORIES
="$ROOT" &&
696 export GIT_CEILING_DIRECTORIES
&&
699 __git_refs
>"$actual"
701 test_must_be_empty
"$actual"
704 test_expect_success '__git_refs
- unique remote branches
for git checkout DWIMery
' '
705 cat >expected
<<-EOF &&
710 other/branch-in-other
711 other/master-in-other
713 remote/branch-in-remote
719 for remote_ref
in refs
/remotes
/other
/ambiguous \
720 refs
/remotes
/remote
/ambiguous \
721 refs
/remotes
/remote
/branch-in-remote
723 git update-ref
$remote_ref master
&&
724 test_when_finished
"git update-ref -d $remote_ref"
728 __git_refs
"" 1 >"$actual"
730 test_cmp expected
"$actual"
733 test_expect_success 'teardown after ref completion
' '
734 git branch
-d matching-branch
&&
735 git tag
-d matching-tag
&&
736 git remote remove other
739 test_expect_success '__git_get_config_variables
' '
740 cat >expect
<<-EOF &&
744 test_config interesting.name-1 good
&&
745 test_config interesting.name-2 good
&&
746 test_config subsection.interesting.name-3 bad
&&
747 __git_get_config_variables interesting
>actual
&&
748 test_cmp expect actual
751 test_expect_success '__git_pretty_aliases
' '
752 cat >expect
<<-EOF &&
756 test_config pretty.author
"%an %ae" &&
757 test_config pretty.
hash %H
&&
758 __git_pretty_aliases
>actual
&&
759 test_cmp expect actual
762 test_expect_success '__git_aliases
' '
763 cat >expect
<<-EOF &&
767 test_config
alias.ci commit
&&
768 test_config
alias.co checkout
&&
769 __git_aliases
>actual
&&
770 test_cmp expect actual
773 test_expect_success 'basic
' '
774 run_completion
"git " &&
776 grep -q "^add \$" out
&&
778 grep -q "^filter-branch \$" out
&&
780 ! grep -q "^ls-files \$" out
&&
782 run_completion
"git f" &&
783 ! grep -q -v "^f" out
786 test_expect_success 'double dash
"git" itself
' '
787 test_completion
"git --" <<-\EOF
800 --no-replace-objects Z
805 test_expect_success 'double dash "git checkout"' '
806 test_completion "git checkout --" <<-\
EOF
819 test_expect_success 'general options' '
820 test_completion "git --ver" "--version " &&
821 test_completion "git --hel" "--help " &&
822 test_completion "git --exe" <<-\EOF &&
826 test_completion "git --htm" "--html-path " &&
827 test_completion "git --pag" "--paginate " &&
828 test_completion "git --no-p" "--no-pager " &&
829 test_completion "git --git" "--git-dir=" &&
830 test_completion "git --wor" "--work-tree=" &&
831 test_completion "git --nam" "--namespace=" &&
832 test_completion "git --bar" "--bare " &&
833 test_completion "git --inf" "--info-path " &&
834 test_completion "git --no-r" "--no-replace-objects "
837 test_expect_success 'general options plus command' '
838 test_completion "git --version check" "checkout " &&
839 test_completion "git --paginate check" "checkout " &&
840 test_completion "git --git-dir=foo check" "checkout " &&
841 test_completion "git --bare check" "checkout " &&
842 test_completion "git --exec-path=foo check" "checkout " &&
843 test_completion "git --html-path check" "checkout " &&
844 test_completion "git --no-pager check" "checkout " &&
845 test_completion "git --work-tree=foo check" "checkout " &&
846 test_completion "git --namespace=foo check" "checkout " &&
847 test_completion "git --paginate check" "checkout " &&
848 test_completion "git --info-path check" "checkout " &&
849 test_completion "git --no-replace-objects check" "checkout " &&
850 test_completion "git --git-dir some/path check" "checkout " &&
851 test_completion "git -c conf.var=value check" "checkout " &&
852 test_completion "git -C some/path check" "checkout " &&
853 test_completion "git --work-tree some/path check" "checkout " &&
854 test_completion "git --namespace name/space check" "checkout "
857 test_expect_success 'git --help completion' '
858 test_completion "git --help ad" "add " &&
859 test_completion "git --help core" "core-tutorial "
862 test_expect_success 'setup for integration tests' '
863 echo content >file1 &&
865 git add file1 file2 &&
867 git branch mybranch &&
871 test_expect_success 'checkout completes ref names' '
872 test_completion "git checkout m" <<-\EOF
879 test_expect_success 'git -C <path> checkout uses the right repo' '
880 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
885 test_expect_success 'show completes all refs' '
886 test_completion "git show m" <<-\EOF
893 test_expect_success '<ref>: completes paths' '
894 test_completion "git show mytag:f" <<-\EOF
900 test_expect_success 'complete tree filename with spaces' '
901 echo content >"name with spaces" &&
902 git add "name with spaces" &&
903 git commit -m spaces &&
904 test_completion "git show HEAD:nam" <<-\EOF
909 test_expect_success 'complete tree filename with metacharacters' '
910 echo content >"name with \${meta}" &&
911 git add "name with \${meta}" &&
912 git commit -m meta &&
913 test_completion "git show HEAD:nam" <<-\EOF
919 test_expect_success 'send-email' '
920 test_completion "git send-email --cov" "--cover-letter " &&
921 test_completion "git send-email ma" "master "
924 test_expect_success 'complete files' '
925 git init tmp && cd tmp &&
926 test_when_finished "cd .. && rm -rf tmp" &&
928 echo "expected" > .gitignore &&
929 echo "out" >> .gitignore &&
931 git add .gitignore &&
932 test_completion "git commit " ".gitignore" &&
934 git commit -m ignore &&
937 test_completion "git add " "new" &&
940 git commit -a -m new &&
941 test_completion "git add " "" &&
943 git mv new modified &&
944 echo modify > modified &&
945 test_completion "git add " "modified" &&
949 : TODO .gitignore should not be here &&
950 test_completion "git rm " <<-\EOF &&
955 test_completion "git clean " "untracked" &&
957 : TODO .gitignore should not be here &&
958 test_completion "git mv " <<-\EOF &&
964 touch dir/file-in-dir &&
965 git add dir/file-in-dir &&
968 mkdir untracked-dir &&
970 : TODO .gitignore should not be here &&
971 test_completion "git mv modified " <<-\EOF &&
979 test_completion "git commit " "modified" &&
981 : TODO .gitignore should not be here &&
982 test_completion "git ls-files " <<-\EOF &&
989 test_completion "git add mom" "momified"
992 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
993 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
994 test_completion "git co m" <<-\EOF
1001 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
1002 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
1003 test_completion "git co m" <<-\EOF
1010 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
1011 test_config alias.co "!f() { : git checkout ; if ... } f" &&
1012 test_completion "git co m" <<-\EOF
1019 test_expect_failure 'complete with tilde expansion' '
1020 git init tmp && cd tmp &&
1021 test_when_finished "cd .. && rm -rf tmp" &&
1025 test_completion "git add ~/tmp/" "~/tmp/file"