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 &&
150 __gitdir "otherrepo" >"$actual" &&
151 test_cmp expected "$actual"
154 test_expect_success
'__gitdir - remote as argument' '
155 echo "remote" >expected &&
156 __gitdir "remote" >"$actual" &&
157 test_cmp expected "$actual"
160 test_expect_success
'__gitdir - .git directory in cwd' '
161 echo ".git" >expected &&
162 __gitdir >"$actual" &&
163 test_cmp expected "$actual"
166 test_expect_success
'__gitdir - .git directory in parent' '
167 echo "$ROOT/.git" >expected &&
169 cd subdir/subsubdir &&
172 test_cmp expected "$actual"
175 test_expect_success
'__gitdir - cwd is a .git directory' '
176 echo "." >expected &&
181 test_cmp expected "$actual"
184 test_expect_success
'__gitdir - parent is a .git directory' '
185 echo "$ROOT/.git" >expected &&
187 cd .git/refs/heads &&
190 test_cmp expected "$actual"
193 test_expect_success
'__gitdir - $GIT_DIR set while .git directory in cwd' '
194 echo "$ROOT/otherrepo/.git" >expected &&
196 GIT_DIR="$ROOT/otherrepo/.git" &&
200 test_cmp expected "$actual"
203 test_expect_success
'__gitdir - $GIT_DIR set while .git directory in parent' '
204 echo "$ROOT/otherrepo/.git" >expected &&
206 GIT_DIR="$ROOT/otherrepo/.git" &&
211 test_cmp expected "$actual"
214 test_expect_success
'__gitdir - from command line while "git -C"' '
215 echo "$ROOT/.git" >expected &&
217 __git_dir="$ROOT/.git" &&
218 __git_C_args=(-C otherrepo) &&
221 test_cmp expected "$actual"
224 test_expect_success
'__gitdir - relative dir from command line and "git -C"' '
225 echo "$ROOT/otherrepo/.git" >expected &&
228 __git_dir="otherrepo/.git" &&
229 __git_C_args=(-C ..) &&
232 test_cmp expected "$actual"
235 test_expect_success
'__gitdir - $GIT_DIR set while "git -C"' '
236 echo "$ROOT/.git" >expected &&
238 GIT_DIR="$ROOT/.git" &&
240 __git_C_args=(-C otherrepo) &&
243 test_cmp expected "$actual"
246 test_expect_success
'__gitdir - relative dir in $GIT_DIR and "git -C"' '
247 echo "$ROOT/otherrepo/.git" >expected &&
250 GIT_DIR="otherrepo/.git" &&
252 __git_C_args=(-C ..) &&
255 test_cmp expected "$actual"
258 test_expect_success
'__gitdir - "git -C" while .git directory in cwd' '
259 echo "$ROOT/otherrepo/.git" >expected &&
261 __git_C_args=(-C otherrepo) &&
264 test_cmp expected "$actual"
267 test_expect_success
'__gitdir - "git -C" while cwd is a .git directory' '
268 echo "$ROOT/otherrepo/.git" >expected &&
271 __git_C_args=(-C .. -C otherrepo) &&
274 test_cmp expected "$actual"
277 test_expect_success
'__gitdir - "git -C" while .git directory in parent' '
278 echo "$ROOT/otherrepo/.git" >expected &&
281 __git_C_args=(-C .. -C otherrepo) &&
284 test_cmp expected "$actual"
287 test_expect_success
'__gitdir - non-existing path in "git -C"' '
289 __git_C_args=(-C non-existing) &&
290 test_must_fail __gitdir >"$actual"
292 test_must_be_empty "$actual"
295 test_expect_success
'__gitdir - non-existing path in $__git_dir' '
297 __git_dir="non-existing" &&
298 test_must_fail __gitdir >"$actual"
300 test_must_be_empty "$actual"
303 test_expect_success
'__gitdir - non-existing $GIT_DIR' '
305 GIT_DIR="$ROOT/non-existing" &&
307 test_must_fail __gitdir >"$actual"
309 test_must_be_empty "$actual"
312 test_expect_success
'__gitdir - gitfile in cwd' '
313 echo "$ROOT/otherrepo/.git" >expected &&
314 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
315 test_when_finished "rm -f subdir/.git" &&
320 test_cmp expected "$actual"
323 test_expect_success
'__gitdir - gitfile in parent' '
324 echo "$ROOT/otherrepo/.git" >expected &&
325 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
326 test_when_finished "rm -f subdir/.git" &&
328 cd subdir/subsubdir &&
331 test_cmp expected "$actual"
334 test_expect_success SYMLINKS
'__gitdir - resulting path avoids symlinks' '
335 echo "$ROOT/otherrepo/.git" >expected &&
336 mkdir otherrepo/dir &&
337 test_when_finished "rm -rf otherrepo/dir" &&
338 ln -s otherrepo/dir link &&
339 test_when_finished "rm -f link" &&
344 test_cmp expected "$actual"
347 test_expect_success
'__gitdir - not a git repository' '
348 nongit test_must_fail __gitdir >"$actual" &&
349 test_must_be_empty "$actual"
352 test_expect_success
'__gitcomp - trailing space - options' '
353 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
354 --reset-author" <<-EOF
361 test_expect_success
'__gitcomp - trailing space - config keys' '
362 test_gitcomp "br" "branch. branch.autosetupmerge
363 branch.autosetuprebase browser." <<-\EOF
365 branch.autosetupmerge Z
366 branch.autosetuprebase Z
371 test_expect_success
'__gitcomp - option parameter' '
372 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
379 test_expect_success
'__gitcomp - prefix' '
380 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
381 "branch.maint." "me" <<-\EOF
383 branch.maint.mergeoptions Z
387 test_expect_success
'__gitcomp - suffix' '
388 test_gitcomp "branch.me" "master maint next pu" "branch." \
395 test_expect_success
'__gitcomp - doesnt fail because of invalid variable name' '
396 __gitcomp "$invalid_variable_name"
399 read -r -d "" refs
<<-\EOF
406 test_expect_success '__gitcomp_nl - trailing space
' '
407 test_gitcomp_nl
"m" "$refs" <<-EOF
413 test_expect_success '__gitcomp_nl
- prefix
' '
414 test_gitcomp_nl
"--fixup=m" "$refs" "--fixup=" "m" <<-EOF
420 test_expect_success '__gitcomp_nl
- suffix
' '
421 test_gitcomp_nl
"branch.ma" "$refs" "branch." "ma" "." <<-\EOF
427 test_expect_success '__gitcomp_nl - no suffix
' '
428 test_gitcomp_nl
"ma" "$refs" "" "ma" "" <<-\EOF
434 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name
' '
435 __gitcomp_nl
"$invalid_variable_name"
438 test_expect_success '__git_remotes
- list remotes from
$GIT_DIR/remotes and from config
file' '
439 cat >expect
<<-EOF &&
445 test_when_finished
"rm -rf .git/remotes" &&
446 mkdir
-p .git
/remotes
&&
447 >.git
/remotes
/remote_from_file_1
&&
448 >.git
/remotes
/remote_from_file_2
&&
449 test_when_finished
"git remote remove remote_in_config_1" &&
450 git remote add remote_in_config_1 git
://remote_1
&&
451 test_when_finished
"git remote remove remote_in_config_2" &&
452 git remote add remote_in_config_2 git
://remote_2
&&
453 __git_remotes
>actual
&&
454 test_cmp expect actual
457 test_expect_success '__git_is_configured_remote
' '
458 test_when_finished
"git remote remove remote_1" &&
459 git remote add remote_1 git
://remote_1
&&
460 test_when_finished
"git remote remove remote_2" &&
461 git remote add remote_2 git
://remote_2
&&
462 verbose __git_is_configured_remote remote_2
&&
463 test_must_fail __git_is_configured_remote non-existent
466 test_expect_success 'setup
for ref completion
' '
467 git commit
--allow-empty -m initial
&&
468 git branch matching-branch
&&
469 git tag matching-tag
&&
472 git commit
--allow-empty -m initial
&&
473 git branch
-m master master-in-other
&&
474 git branch branch-in-other
&&
477 git remote add other
"$ROOT/otherrepo/.git" &&
478 git fetch
--no-tags other
&&
479 rm -f .git
/FETCH_HEAD
&&
483 test_expect_success '__git_refs
- simple
' '
484 cat >expected
<<-EOF &&
488 other/branch-in-other
489 other/master-in-other
494 __git_refs
>"$actual"
496 test_cmp expected
"$actual"
499 test_expect_success '__git_refs
- full refs
' '
500 cat >expected
<<-EOF &&
502 refs/heads/matching-branch
506 __git_refs
>"$actual"
508 test_cmp expected
"$actual"
511 test_expect_success '__git_refs
- repo given on the
command line
' '
512 cat >expected
<<-EOF &&
519 __git_dir
="$ROOT/otherrepo/.git" &&
521 __git_refs
>"$actual"
523 test_cmp expected
"$actual"
526 test_expect_success '__git_refs
- remote on
local file system
' '
527 cat >expected
<<-EOF &&
535 __git_refs otherrepo
>"$actual"
537 test_cmp expected
"$actual"
540 test_expect_success '__git_refs
- remote on
local file system
- full refs
' '
541 cat >expected
<<-EOF &&
542 refs/heads/branch-in-other
543 refs/heads/master-in-other
544 refs/tags/tag-in-other
548 __git_refs otherrepo
>"$actual"
550 test_cmp expected
"$actual"
553 test_expect_success '__git_refs
- configured remote
' '
554 cat >expected
<<-EOF &&
561 __git_refs other
>"$actual"
563 test_cmp expected
"$actual"
566 test_expect_success '__git_refs
- configured remote
- full refs
' '
567 cat >expected
<<-EOF &&
568 refs/heads/branch-in-other
569 refs/heads/master-in-other
570 refs/tags/tag-in-other
574 __git_refs other
>"$actual"
576 test_cmp expected
"$actual"
579 test_expect_success '__git_refs
- configured remote
- repo given on the
command line
' '
580 cat >expected
<<-EOF &&
587 __git_dir
="$ROOT/.git" &&
589 __git_refs other
>"$actual"
591 test_cmp expected
"$actual"
594 test_expect_success '__git_refs
- configured remote
- full refs
- repo given on the
command line
' '
595 cat >expected
<<-EOF &&
596 refs/heads/branch-in-other
597 refs/heads/master-in-other
598 refs/tags/tag-in-other
602 __git_dir
="$ROOT/.git" &&
604 __git_refs other
>"$actual"
606 test_cmp expected
"$actual"
609 test_expect_success '__git_refs
- configured remote
- remote name matches a directory
' '
610 cat >expected
<<-EOF &&
616 test_when_finished
"rm -rf other" &&
619 __git_refs other
>"$actual"
621 test_cmp expected
"$actual"
624 test_expect_success '__git_refs
- URL remote
' '
625 cat >expected
<<-EOF &&
633 __git_refs
"file://$ROOT/otherrepo/.git" >"$actual"
635 test_cmp expected
"$actual"
638 test_expect_success '__git_refs
- URL remote
- full refs
' '
639 cat >expected
<<-EOF &&
640 refs/heads/branch-in-other
641 refs/heads/master-in-other
642 refs/tags/tag-in-other
646 __git_refs
"file://$ROOT/otherrepo/.git" >"$actual"
648 test_cmp expected
"$actual"
651 test_expect_success '__git_refs
- non-existing remote
' '
654 __git_refs non-existing
>"$actual"
656 test_must_be_empty
"$actual"
659 test_expect_success '__git_refs
- non-existing remote
- full refs
' '
662 __git_refs non-existing
>"$actual"
664 test_must_be_empty
"$actual"
667 test_expect_success '__git_refs
- non-existing URL remote
' '
670 __git_refs
"file://$ROOT/non-existing" >"$actual"
672 test_must_be_empty
"$actual"
675 test_expect_success '__git_refs
- non-existing URL remote
- full refs
' '
678 __git_refs
"file://$ROOT/non-existing" >"$actual"
680 test_must_be_empty
"$actual"
683 test_expect_success '__git_refs
- not
in a git repository
' '
685 GIT_CEILING_DIRECTORIES
="$ROOT" &&
686 export GIT_CEILING_DIRECTORIES
&&
689 __git_refs
>"$actual"
691 test_must_be_empty
"$actual"
694 test_expect_success '__git_refs
- unique remote branches
for git checkout DWIMery
' '
695 cat >expected
<<-EOF &&
700 other/branch-in-other
701 other/master-in-other
703 remote/branch-in-remote
709 for remote_ref
in refs
/remotes
/other
/ambiguous \
710 refs
/remotes
/remote
/ambiguous \
711 refs
/remotes
/remote
/branch-in-remote
713 git update-ref
$remote_ref master
&&
714 test_when_finished
"git update-ref -d $remote_ref"
718 __git_refs
"" 1 >"$actual"
720 test_cmp expected
"$actual"
723 test_expect_success 'teardown after ref completion
' '
724 git branch
-d matching-branch
&&
725 git tag
-d matching-tag
&&
726 git remote remove other
729 test_expect_success '__git_get_config_variables
' '
730 cat >expect
<<-EOF &&
734 test_config interesting.name-1 good
&&
735 test_config interesting.name-2 good
&&
736 test_config subsection.interesting.name-3 bad
&&
737 __git_get_config_variables interesting
>actual
&&
738 test_cmp expect actual
741 test_expect_success '__git_pretty_aliases
' '
742 cat >expect
<<-EOF &&
746 test_config pretty.author
"%an %ae" &&
747 test_config pretty.
hash %H
&&
748 __git_pretty_aliases
>actual
&&
749 test_cmp expect actual
752 test_expect_success '__git_aliases
' '
753 cat >expect
<<-EOF &&
757 test_config
alias.ci commit
&&
758 test_config
alias.co checkout
&&
759 __git_aliases
>actual
&&
760 test_cmp expect actual
763 test_expect_success 'basic
' '
764 run_completion
"git " &&
766 grep -q "^add \$" out
&&
768 grep -q "^filter-branch \$" out
&&
770 ! grep -q "^ls-files \$" out
&&
772 run_completion
"git f" &&
773 ! grep -q -v "^f" out
776 test_expect_success 'double dash
"git" itself
' '
777 test_completion
"git --" <<-\EOF
790 --no-replace-objects Z
795 test_expect_success 'double dash "git checkout"' '
796 test_completion "git checkout --" <<-\
EOF
809 test_expect_success 'general options' '
810 test_completion "git --ver" "--version " &&
811 test_completion "git --hel" "--help " &&
812 test_completion "git --exe" <<-\EOF &&
816 test_completion "git --htm" "--html-path " &&
817 test_completion "git --pag" "--paginate " &&
818 test_completion "git --no-p" "--no-pager " &&
819 test_completion "git --git" "--git-dir=" &&
820 test_completion "git --wor" "--work-tree=" &&
821 test_completion "git --nam" "--namespace=" &&
822 test_completion "git --bar" "--bare " &&
823 test_completion "git --inf" "--info-path " &&
824 test_completion "git --no-r" "--no-replace-objects "
827 test_expect_success 'general options plus command' '
828 test_completion "git --version check" "checkout " &&
829 test_completion "git --paginate check" "checkout " &&
830 test_completion "git --git-dir=foo check" "checkout " &&
831 test_completion "git --bare check" "checkout " &&
832 test_completion "git --exec-path=foo check" "checkout " &&
833 test_completion "git --html-path check" "checkout " &&
834 test_completion "git --no-pager check" "checkout " &&
835 test_completion "git --work-tree=foo check" "checkout " &&
836 test_completion "git --namespace=foo check" "checkout " &&
837 test_completion "git --paginate check" "checkout " &&
838 test_completion "git --info-path check" "checkout " &&
839 test_completion "git --no-replace-objects check" "checkout " &&
840 test_completion "git --git-dir some/path check" "checkout " &&
841 test_completion "git -c conf.var=value check" "checkout " &&
842 test_completion "git -C some/path check" "checkout " &&
843 test_completion "git --work-tree some/path check" "checkout " &&
844 test_completion "git --namespace name/space check" "checkout "
847 test_expect_success 'git --help completion' '
848 test_completion "git --help ad" "add " &&
849 test_completion "git --help core" "core-tutorial "
852 test_expect_success 'setup for integration tests' '
853 echo content >file1 &&
855 git add file1 file2 &&
857 git branch mybranch &&
861 test_expect_success 'checkout completes ref names' '
862 test_completion "git checkout m" <<-\EOF
869 test_expect_success 'git -C <path> checkout uses the right repo' '
870 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
875 test_expect_success 'show completes all refs' '
876 test_completion "git show m" <<-\EOF
883 test_expect_success '<ref>: completes paths' '
884 test_completion "git show mytag:f" <<-\EOF
890 test_expect_success 'complete tree filename with spaces' '
891 echo content >"name with spaces" &&
892 git add "name with spaces" &&
893 git commit -m spaces &&
894 test_completion "git show HEAD:nam" <<-\EOF
899 test_expect_success 'complete tree filename with metacharacters' '
900 echo content >"name with \${meta}" &&
901 git add "name with \${meta}" &&
902 git commit -m meta &&
903 test_completion "git show HEAD:nam" <<-\EOF
909 test_expect_success 'send-email' '
910 test_completion "git send-email --cov" "--cover-letter " &&
911 test_completion "git send-email ma" "master "
914 test_expect_success 'complete files' '
915 git init tmp && cd tmp &&
916 test_when_finished "cd .. && rm -rf tmp" &&
918 echo "expected" > .gitignore &&
919 echo "out" >> .gitignore &&
921 git add .gitignore &&
922 test_completion "git commit " ".gitignore" &&
924 git commit -m ignore &&
927 test_completion "git add " "new" &&
930 git commit -a -m new &&
931 test_completion "git add " "" &&
933 git mv new modified &&
934 echo modify > modified &&
935 test_completion "git add " "modified" &&
939 : TODO .gitignore should not be here &&
940 test_completion "git rm " <<-\EOF &&
945 test_completion "git clean " "untracked" &&
947 : TODO .gitignore should not be here &&
948 test_completion "git mv " <<-\EOF &&
954 touch dir/file-in-dir &&
955 git add dir/file-in-dir &&
958 mkdir untracked-dir &&
960 : TODO .gitignore should not be here &&
961 test_completion "git mv modified " <<-\EOF &&
969 test_completion "git commit " "modified" &&
971 : TODO .gitignore should not be here &&
972 test_completion "git ls-files " <<-\EOF &&
979 test_completion "git add mom" "momified"
982 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
983 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
984 test_completion "git co m" <<-\EOF
991 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
992 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
993 test_completion "git co m" <<-\EOF
1000 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
1001 test_config alias.co "!f() { : git checkout ; if ... } f" &&
1002 test_completion "git co m" <<-\EOF
1009 test_expect_failure 'complete with tilde expansion' '
1010 git init tmp && cd tmp &&
1011 test_when_finished "cd .. && rm -rf tmp" &&
1015 test_completion "git add ~/tmp/" "~/tmp/file"