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 __git_find_repo_path/__gitdir tests' '
135 mkdir -p subdir/subsubdir &&
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"
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"
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"
166 test_cmp expected "$actual"
169 test_expect_success '__git_find_repo_path - cwd is a .git directory' '
170 echo "." >expected &&
173 __git_find_repo_path &&
174 echo "$__git_repo_path" >"$actual"
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"
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" &&
194 __git_find_repo_path &&
195 echo "$__git_repo_path" >"$actual"
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" &&
206 __git_find_repo_path &&
207 echo "$__git_repo_path" >"$actual"
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"
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 &&
227 __git_dir="otherrepo/.git" &&
228 __git_C_args=(-C ..) &&
229 __git_find_repo_path &&
230 echo "$__git_repo_path" >"$actual"
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" &&
240 __git_C_args=(-C otherrepo) &&
241 __git_find_repo_path &&
242 echo "$__git_repo_path" >"$actual"
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 &&
251 GIT_DIR="otherrepo/.git" &&
253 __git_C_args=(-C ..) &&
254 __git_find_repo_path &&
255 echo "$__git_repo_path" >"$actual"
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"
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 &&
274 __git_C_args=(-C .. -C otherrepo) &&
275 __git_find_repo_path &&
276 echo "$__git_repo_path" >"$actual"
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 &&
285 __git_C_args=(-C .. -C otherrepo) &&
286 __git_find_repo_path &&
287 echo "$__git_repo_path" >"$actual"
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"
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"
307 test_must_be_empty "$actual"
310 test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
312 GIT_DIR="$ROOT/non-existing" &&
314 test_must_fail __git_find_repo_path &&
315 printf "$__git_repo_path" >"$actual"
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" &&
326 __git_find_repo_path &&
327 echo "$__git_repo_path" >"$actual"
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"
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" &&
352 __git_find_repo_path &&
353 echo "$__git_repo_path" >"$actual"
355 test_cmp expected "$actual"
358 test_expect_success '__git_find_repo_path - not a git repository' '
361 GIT_CEILING_DIRECTORIES="$ROOT" &&
362 export GIT_CEILING_DIRECTORIES &&
363 test_must_fail __git_find_repo_path &&
364 printf "$__git_repo_path" >"$actual"
366 test_must_be_empty "$actual"
369 test_expect_success '__gitdir - finds repo' '
370 echo "$ROOT/.git" >expected &&
372 cd subdir/subsubdir &&
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"
384 test_must_be_empty "$actual"
387 test_expect_success '__gitdir - repo as argument' '
388 echo "otherrepo/.git" >expected &&
390 __gitdir "otherrepo" >"$actual"
392 test_cmp expected "$actual"
395 test_expect_success '__gitdir - remote as argument' '
396 echo "remote" >expected &&
398 __gitdir "remote" >"$actual"
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
412 test_expect_success '__gitcomp - trailing space - config keys' '
413 test_gitcomp "br" "branch. branch.autosetupmerge
414 branch.autosetuprebase browser." <<-\EOF
416 branch.autosetupmerge Z
417 branch.autosetuprebase Z
422 test_expect_success '__gitcomp - option parameter' '
423 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
430 test_expect_success '__gitcomp - prefix' '
431 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
432 "branch.maint." "me" <<-\EOF
434 branch.maint.mergeoptions Z
438 test_expect_success '__gitcomp - suffix' '
439 test_gitcomp "branch.me" "master maint next pu" "branch." \
446 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
447 __gitcomp "$invalid_variable_name"
450 read -r -d "" refs <<-\EOF
457 test_expect_success '__gitcomp_nl - trailing space' '
458 test_gitcomp_nl "m" "$refs" <<-EOF
464 test_expect_success '__gitcomp_nl - prefix' '
465 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
471 test_expect_success '__gitcomp_nl - suffix' '
472 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
478 test_expect_success '__gitcomp_nl - no suffix' '
479 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
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 &&
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
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 &&
527 git commit --allow-empty -m initial &&
528 git branch -m master master-in-other &&
529 git branch branch-in-other &&
532 git remote add other "$ROOT/otherrepo/.git" &&
533 git fetch --no-tags other &&
534 rm -f .git/FETCH_HEAD &&
538 test_expect_success '__git_refs - simple' '
539 cat >expected <<-EOF &&
543 other/branch-in-other
544 other/master-in-other
549 __git_refs >"$actual"
551 test_cmp expected "$actual"
554 test_expect_success '__git_refs - full refs' '
555 cat >expected <<-EOF &&
557 refs/heads/matching-branch
561 __git_refs >"$actual"
563 test_cmp expected "$actual"
566 test_expect_success '__git_refs - repo given on the command line' '
567 cat >expected <<-EOF &&
574 __git_dir="$ROOT/otherrepo/.git" &&
576 __git_refs >"$actual"
578 test_cmp expected "$actual"
581 test_expect_success '__git_refs - remote on local file system' '
582 cat >expected <<-EOF &&
590 __git_refs otherrepo >"$actual"
592 test_cmp expected "$actual"
595 test_expect_success '__git_refs - remote on local file system - full refs' '
596 cat >expected <<-EOF &&
597 refs/heads/branch-in-other
598 refs/heads/master-in-other
599 refs/tags/tag-in-other
603 __git_refs otherrepo >"$actual"
605 test_cmp expected "$actual"
608 test_expect_success '__git_refs - configured remote' '
609 cat >expected <<-EOF &&
616 __git_refs other >"$actual"
618 test_cmp expected "$actual"
621 test_expect_success '__git_refs - configured remote - full refs' '
622 cat >expected <<-EOF &&
623 refs/heads/branch-in-other
624 refs/heads/master-in-other
625 refs/tags/tag-in-other
629 __git_refs other >"$actual"
631 test_cmp expected "$actual"
634 test_expect_success '__git_refs - configured remote - repo given on the command line' '
635 cat >expected <<-EOF &&
642 __git_dir="$ROOT/.git" &&
644 __git_refs other >"$actual"
646 test_cmp expected "$actual"
649 test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
650 cat >expected <<-EOF &&
651 refs/heads/branch-in-other
652 refs/heads/master-in-other
653 refs/tags/tag-in-other
657 __git_dir="$ROOT/.git" &&
659 __git_refs other >"$actual"
661 test_cmp expected "$actual"
664 test_expect_success '__git_refs - configured remote - remote name matches a directory' '
665 cat >expected <<-EOF &&
671 test_when_finished "rm -rf other" &&
674 __git_refs other >"$actual"
676 test_cmp expected "$actual"
679 test_expect_success '__git_refs - URL remote' '
680 cat >expected <<-EOF &&
688 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
690 test_cmp expected "$actual"
693 test_expect_success '__git_refs - URL remote - full refs' '
694 cat >expected <<-EOF &&
695 refs/heads/branch-in-other
696 refs/heads/master-in-other
697 refs/tags/tag-in-other
701 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
703 test_cmp expected "$actual"
706 test_expect_success '__git_refs - non-existing remote' '
709 __git_refs non-existing >"$actual"
711 test_must_be_empty "$actual"
714 test_expect_success '__git_refs - non-existing remote - full refs' '
717 __git_refs non-existing >"$actual"
719 test_must_be_empty "$actual"
722 test_expect_success '__git_refs - non-existing URL remote' '
725 __git_refs "file://$ROOT/non-existing" >"$actual"
727 test_must_be_empty "$actual"
730 test_expect_success '__git_refs - non-existing URL remote - full refs' '
733 __git_refs "file://$ROOT/non-existing" >"$actual"
735 test_must_be_empty "$actual"
738 test_expect_success '__git_refs - not in a git repository' '
740 GIT_CEILING_DIRECTORIES="$ROOT" &&
741 export GIT_CEILING_DIRECTORIES &&
744 __git_refs >"$actual"
746 test_must_be_empty "$actual"
749 test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
750 cat >expected <<-EOF &&
755 other/branch-in-other
756 other/master-in-other
758 remote/branch-in-remote
764 for remote_ref in refs/remotes/other/ambiguous \
765 refs/remotes/remote/ambiguous \
766 refs/remotes/remote/branch-in-remote
768 git update-ref $remote_ref master &&
769 test_when_finished "git update-ref -d $remote_ref"
773 __git_refs "" 1 >"$actual"
775 test_cmp expected "$actual"
778 test_expect_success 'teardown after ref completion' '
779 git branch -d matching-branch &&
780 git tag -d matching-tag &&
781 git remote remove other
784 test_expect_success '__git_get_config_variables' '
785 cat >expect <<-EOF &&
789 test_config interesting.name-1 good &&
790 test_config interesting.name-2 good &&
791 test_config subsection.interesting.name-3 bad &&
792 __git_get_config_variables interesting >actual &&
793 test_cmp expect actual
796 test_expect_success '__git_pretty_aliases' '
797 cat >expect <<-EOF &&
801 test_config pretty.author "%an %ae" &&
802 test_config pretty.hash %H &&
803 __git_pretty_aliases >actual &&
804 test_cmp expect actual
807 test_expect_success '__git_aliases' '
808 cat >expect <<-EOF &&
812 test_config alias.ci commit &&
813 test_config alias.co checkout &&
814 __git_aliases >actual &&
815 test_cmp expect actual
818 test_expect_success 'basic' '
819 run_completion "git " &&
821 grep -q "^add \$" out &&
823 grep -q "^filter-branch \$" out &&
825 ! grep -q "^ls-files \$" out &&
827 run_completion "git f" &&
828 ! grep -q -v "^f" out
831 test_expect_success 'double dash "git" itself' '
832 test_completion "git --" <<-\EOF
845 --no-replace-objects Z
850 test_expect_success 'double dash "git checkout"' '
851 test_completion "git checkout --" <<-\EOF
864 test_expect_success 'general options' '
865 test_completion "git --ver" "--version " &&
866 test_completion "git --hel" "--help " &&
867 test_completion "git --exe" <<-\EOF &&
871 test_completion "git --htm" "--html-path " &&
872 test_completion "git --pag" "--paginate " &&
873 test_completion "git --no-p" "--no-pager " &&
874 test_completion "git --git" "--git-dir=" &&
875 test_completion "git --wor" "--work-tree=" &&
876 test_completion "git --nam" "--namespace=" &&
877 test_completion "git --bar" "--bare " &&
878 test_completion "git --inf" "--info-path " &&
879 test_completion "git --no-r" "--no-replace-objects "
882 test_expect_success 'general options plus command' '
883 test_completion "git --version check" "checkout " &&
884 test_completion "git --paginate check" "checkout " &&
885 test_completion "git --git-dir=foo check" "checkout " &&
886 test_completion "git --bare check" "checkout " &&
887 test_completion "git --exec-path=foo check" "checkout " &&
888 test_completion "git --html-path check" "checkout " &&
889 test_completion "git --no-pager check" "checkout " &&
890 test_completion "git --work-tree=foo check" "checkout " &&
891 test_completion "git --namespace=foo check" "checkout " &&
892 test_completion "git --paginate check" "checkout " &&
893 test_completion "git --info-path check" "checkout " &&
894 test_completion "git --no-replace-objects check" "checkout " &&
895 test_completion "git --git-dir some/path check" "checkout " &&
896 test_completion "git -c conf.var=value check" "checkout " &&
897 test_completion "git -C some/path check" "checkout " &&
898 test_completion "git --work-tree some/path check" "checkout " &&
899 test_completion "git --namespace name/space check" "checkout "
902 test_expect_success 'git --help completion' '
903 test_completion "git --help ad" "add " &&
904 test_completion "git --help core" "core-tutorial "
907 test_expect_success 'setup for integration tests' '
908 echo content >file1 &&
910 git add file1 file2 &&
912 git branch mybranch &&
916 test_expect_success 'checkout completes ref names' '
917 test_completion "git checkout m" <<-\EOF
924 test_expect_success 'git -C <path> checkout uses the right repo' '
925 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
930 test_expect_success 'show completes all refs' '
931 test_completion "git show m" <<-\EOF
938 test_expect_success '<ref>: completes paths' '
939 test_completion "git show mytag:f" <<-\EOF
945 test_expect_success 'complete tree filename with spaces' '
946 echo content >"name with spaces" &&
947 git add "name with spaces" &&
948 git commit -m spaces &&
949 test_completion "git show HEAD:nam" <<-\EOF
954 test_expect_success 'complete tree filename with metacharacters' '
955 echo content >"name with \${meta}" &&
956 git add "name with \${meta}" &&
957 git commit -m meta &&
958 test_completion "git show HEAD:nam" <<-\EOF
964 test_expect_success 'send-email' '
965 test_completion "git send-email --cov" "--cover-letter " &&
966 test_completion "git send-email ma" "master "
969 test_expect_success 'complete files' '
970 git init tmp && cd tmp &&
971 test_when_finished "cd .. && rm -rf tmp" &&
973 echo "expected" > .gitignore &&
974 echo "out" >> .gitignore &&
976 git add .gitignore &&
977 test_completion "git commit " ".gitignore" &&
979 git commit -m ignore &&
982 test_completion "git add " "new" &&
985 git commit -a -m new &&
986 test_completion "git add " "" &&
988 git mv new modified &&
989 echo modify > modified &&
990 test_completion "git add " "modified" &&
994 : TODO .gitignore should not be here &&
995 test_completion "git rm " <<-\EOF &&
1000 test_completion "git clean " "untracked" &&
1002 : TODO .gitignore should not be here &&
1003 test_completion "git mv " <<-\EOF &&
1009 touch dir/file-in-dir &&
1010 git add dir/file-in-dir &&
1011 git commit -m dir &&
1013 mkdir untracked-dir &&
1015 : TODO .gitignore should not be here &&
1016 test_completion "git mv modified " <<-\EOF &&
1024 test_completion "git commit " "modified" &&
1026 : TODO .gitignore should not be here &&
1027 test_completion "git ls-files " <<-\EOF &&
1034 test_completion "git add mom" "momified"
1037 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
1038 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
1039 test_completion "git co m" <<-\EOF
1046 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
1047 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
1048 test_completion "git co m" <<-\EOF
1055 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
1056 test_config alias.co "!f() { : git checkout ; if ... } f" &&
1057 test_completion "git co m" <<-\EOF
1064 test_expect_failure 'complete with tilde expansion' '
1065 git init tmp && cd tmp &&
1066 test_when_finished "cd .. && rm -rf tmp" &&
1070 test_completion "git add ~/tmp/" "~/tmp/file"