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$//' |sort >expected
89 run_completion "$1" &&
90 sort out >out_sorted &&
91 test_cmp expected out_sorted
95 # The first argument is the typed text so far (cur); the rest are
96 # passed to __gitcomp. Expected output comes is read from the
97 # standard input, like test_completion().
100 local -a COMPREPLY &&
101 sed -e 's/Z$//' >expected &&
106 test_cmp expected out
111 # 1: current word (cur)
112 # -: the rest are passed to __gitcomp_nl
115 local -a COMPREPLY &&
116 sed -e 's/Z$//' >expected &&
121 test_cmp expected out
124 invalid_variable_name='${foo.bar}'
126 actual="$TRASH_DIRECTORY/actual"
128 if test_have_prereq MINGW
135 test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
136 mkdir -p subdir/subsubdir &&
141 test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '
142 echo "$ROOT/otherrepo/.git" >expected &&
144 __git_dir="$ROOT/otherrepo/.git" &&
145 __git_find_repo_path &&
146 echo "$__git_repo_path" >"$actual"
148 test_cmp expected "$actual"
151 test_expect_success '__git_find_repo_path - .git directory in cwd' '
152 echo ".git" >expected &&
154 __git_find_repo_path &&
155 echo "$__git_repo_path" >"$actual"
157 test_cmp expected "$actual"
160 test_expect_success '__git_find_repo_path - .git directory in parent' '
161 echo "$ROOT/.git" >expected &&
163 cd subdir/subsubdir &&
164 __git_find_repo_path &&
165 echo "$__git_repo_path" >"$actual"
167 test_cmp expected "$actual"
170 test_expect_success '__git_find_repo_path - cwd is a .git directory' '
171 echo "." >expected &&
174 __git_find_repo_path &&
175 echo "$__git_repo_path" >"$actual"
177 test_cmp expected "$actual"
180 test_expect_success '__git_find_repo_path - parent is a .git directory' '
181 echo "$ROOT/.git" >expected &&
183 cd .git/refs/heads &&
184 __git_find_repo_path &&
185 echo "$__git_repo_path" >"$actual"
187 test_cmp expected "$actual"
190 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
191 echo "$ROOT/otherrepo/.git" >expected &&
193 GIT_DIR="$ROOT/otherrepo/.git" &&
195 __git_find_repo_path &&
196 echo "$__git_repo_path" >"$actual"
198 test_cmp expected "$actual"
201 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
202 echo "$ROOT/otherrepo/.git" >expected &&
204 GIT_DIR="$ROOT/otherrepo/.git" &&
207 __git_find_repo_path &&
208 echo "$__git_repo_path" >"$actual"
210 test_cmp expected "$actual"
213 test_expect_success '__git_find_repo_path - from command line while "git -C"' '
214 echo "$ROOT/.git" >expected &&
216 __git_dir="$ROOT/.git" &&
217 __git_C_args=(-C otherrepo) &&
218 __git_find_repo_path &&
219 echo "$__git_repo_path" >"$actual"
221 test_cmp expected "$actual"
224 test_expect_success '__git_find_repo_path - relative dir from command line and "git -C"' '
225 echo "$ROOT/otherrepo/.git" >expected &&
228 __git_dir="otherrepo/.git" &&
229 __git_C_args=(-C ..) &&
230 __git_find_repo_path &&
231 echo "$__git_repo_path" >"$actual"
233 test_cmp expected "$actual"
236 test_expect_success '__git_find_repo_path - $GIT_DIR set while "git -C"' '
237 echo "$ROOT/.git" >expected &&
239 GIT_DIR="$ROOT/.git" &&
241 __git_C_args=(-C otherrepo) &&
242 __git_find_repo_path &&
243 echo "$__git_repo_path" >"$actual"
245 test_cmp expected "$actual"
248 test_expect_success '__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
249 echo "$ROOT/otherrepo/.git" >expected &&
252 GIT_DIR="otherrepo/.git" &&
254 __git_C_args=(-C ..) &&
255 __git_find_repo_path &&
256 echo "$__git_repo_path" >"$actual"
258 test_cmp expected "$actual"
261 test_expect_success '__git_find_repo_path - "git -C" while .git directory in cwd' '
262 echo "$ROOT/otherrepo/.git" >expected &&
264 __git_C_args=(-C otherrepo) &&
265 __git_find_repo_path &&
266 echo "$__git_repo_path" >"$actual"
268 test_cmp expected "$actual"
271 test_expect_success '__git_find_repo_path - "git -C" while cwd is a .git directory' '
272 echo "$ROOT/otherrepo/.git" >expected &&
275 __git_C_args=(-C .. -C otherrepo) &&
276 __git_find_repo_path &&
277 echo "$__git_repo_path" >"$actual"
279 test_cmp expected "$actual"
282 test_expect_success '__git_find_repo_path - "git -C" while .git directory in parent' '
283 echo "$ROOT/otherrepo/.git" >expected &&
286 __git_C_args=(-C .. -C otherrepo) &&
287 __git_find_repo_path &&
288 echo "$__git_repo_path" >"$actual"
290 test_cmp expected "$actual"
293 test_expect_success '__git_find_repo_path - non-existing path in "git -C"' '
295 __git_C_args=(-C non-existing) &&
296 test_must_fail __git_find_repo_path &&
297 printf "$__git_repo_path" >"$actual"
299 test_must_be_empty "$actual"
302 test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '
304 __git_dir="non-existing" &&
305 test_must_fail __git_find_repo_path &&
306 printf "$__git_repo_path" >"$actual"
308 test_must_be_empty "$actual"
311 test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
313 GIT_DIR="$ROOT/non-existing" &&
315 test_must_fail __git_find_repo_path &&
316 printf "$__git_repo_path" >"$actual"
318 test_must_be_empty "$actual"
321 test_expect_success '__git_find_repo_path - gitfile in cwd' '
322 echo "$ROOT/otherrepo/.git" >expected &&
323 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
324 test_when_finished "rm -f subdir/.git" &&
327 __git_find_repo_path &&
328 echo "$__git_repo_path" >"$actual"
330 test_cmp expected "$actual"
333 test_expect_success '__git_find_repo_path - gitfile in parent' '
334 echo "$ROOT/otherrepo/.git" >expected &&
335 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
336 test_when_finished "rm -f subdir/.git" &&
338 cd subdir/subsubdir &&
339 __git_find_repo_path &&
340 echo "$__git_repo_path" >"$actual"
342 test_cmp expected "$actual"
345 test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '
346 echo "$ROOT/otherrepo/.git" >expected &&
347 mkdir otherrepo/dir &&
348 test_when_finished "rm -rf otherrepo/dir" &&
349 ln -s otherrepo/dir link &&
350 test_when_finished "rm -f link" &&
353 __git_find_repo_path &&
354 echo "$__git_repo_path" >"$actual"
356 test_cmp expected "$actual"
359 test_expect_success '__git_find_repo_path - not a git repository' '
362 GIT_CEILING_DIRECTORIES="$ROOT" &&
363 export GIT_CEILING_DIRECTORIES &&
364 test_must_fail __git_find_repo_path &&
365 printf "$__git_repo_path" >"$actual"
367 test_must_be_empty "$actual"
370 test_expect_success '__gitdir - finds repo' '
371 echo "$ROOT/.git" >expected &&
373 cd subdir/subsubdir &&
376 test_cmp expected "$actual"
380 test_expect_success '__gitdir - returns error when cant find repo' '
382 __git_dir="non-existing" &&
383 test_must_fail __gitdir >"$actual"
385 test_must_be_empty "$actual"
388 test_expect_success '__gitdir - repo as argument' '
389 echo "otherrepo/.git" >expected &&
391 __gitdir "otherrepo" >"$actual"
393 test_cmp expected "$actual"
396 test_expect_success '__gitdir - remote as argument' '
397 echo "remote" >expected &&
399 __gitdir "remote" >"$actual"
401 test_cmp expected "$actual"
405 test_expect_success '__git_dequote - plain unquoted word' '
406 __git_dequote unquoted-word &&
407 verbose test unquoted-word = "$dequoted_word"
410 # input: b\a\c\k\'\\\"s\l\a\s\h\es
411 # expected: back'\"slashes
412 test_expect_success '__git_dequote - backslash escaped' '
413 __git_dequote "b\a\c\k\\'\''\\\\\\\"s\l\a\s\h\es" &&
414 verbose test "back'\''\\\"slashes" = "$dequoted_word"
417 # input: sin'gle\' '"quo'ted
418 # expected: single\ "quoted
419 test_expect_success '__git_dequote - single quoted' '
420 __git_dequote "'"sin'gle\\\\' '\\\"quo'ted"'" &&
421 verbose test '\''single\ "quoted'\'' = "$dequoted_word"
424 # input: dou"ble\\" "\"\quot"ed
425 # expected: double\ "\quoted
426 test_expect_success '__git_dequote - double quoted' '
427 __git_dequote '\''dou"ble\\" "\"\quot"ed'\'' &&
428 verbose test '\''double\ "\quoted'\'' = "$dequoted_word"
431 # input: 'open single quote
432 test_expect_success '__git_dequote - open single quote' '
433 __git_dequote "'\''open single quote" &&
434 verbose test "open single quote" = "$dequoted_word"
437 # input: "open double quote
438 test_expect_success '__git_dequote - open double quote' '
439 __git_dequote "\"open double quote" &&
440 verbose test "open double quote" = "$dequoted_word"
444 test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' '
445 sed -e "s/Z$//g" >expected <<-EOF &&
446 with-trailing-space Z
447 without-trailing-spaceZ
450 $invalid_variable_name Z
453 cur=should_be_ignored &&
454 __gitcomp_direct "$(cat expected)" &&
457 test_cmp expected out
460 test_expect_success '__gitcomp - trailing space - options' '
461 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
462 --reset-author" <<-EOF
469 test_expect_success '__gitcomp - trailing space - config keys' '
470 test_gitcomp "br" "branch. branch.autosetupmerge
471 branch.autosetuprebase browser." <<-\EOF
473 branch.autosetupmerge Z
474 branch.autosetuprebase Z
479 test_expect_success '__gitcomp - option parameter' '
480 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
487 test_expect_success '__gitcomp - prefix' '
488 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
489 "branch.maint." "me" <<-\EOF
491 branch.maint.mergeoptions Z
495 test_expect_success '__gitcomp - suffix' '
496 test_gitcomp "branch.me" "master maint next pu" "branch." \
503 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
504 __gitcomp "$invalid_variable_name"
507 read -r -d "" refs <<-\EOF
514 test_expect_success '__gitcomp_nl - trailing space' '
515 test_gitcomp_nl "m" "$refs" <<-EOF
521 test_expect_success '__gitcomp_nl - prefix' '
522 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
528 test_expect_success '__gitcomp_nl - suffix' '
529 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
535 test_expect_success '__gitcomp_nl - no suffix' '
536 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
542 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
543 __gitcomp_nl "$invalid_variable_name"
546 test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
547 cat >expect <<-EOF &&
553 test_when_finished "rm -rf .git/remotes" &&
554 mkdir -p .git/remotes &&
555 >.git/remotes/remote_from_file_1 &&
556 >.git/remotes/remote_from_file_2 &&
557 test_when_finished "git remote remove remote_in_config_1" &&
558 git remote add remote_in_config_1 git://remote_1 &&
559 test_when_finished "git remote remove remote_in_config_2" &&
560 git remote add remote_in_config_2 git://remote_2 &&
562 __git_remotes >actual
564 test_cmp expect actual
567 test_expect_success '__git_is_configured_remote' '
568 test_when_finished "git remote remove remote_1" &&
569 git remote add remote_1 git://remote_1 &&
570 test_when_finished "git remote remove remote_2" &&
571 git remote add remote_2 git://remote_2 &&
573 verbose __git_is_configured_remote remote_2 &&
574 test_must_fail __git_is_configured_remote non-existent
578 test_expect_success 'setup for ref completion' '
579 git commit --allow-empty -m initial &&
580 git branch matching-branch &&
581 git tag matching-tag &&
584 git commit --allow-empty -m initial &&
585 git branch -m master master-in-other &&
586 git branch branch-in-other &&
589 git remote add other "$ROOT/otherrepo/.git" &&
590 git fetch --no-tags other &&
591 rm -f .git/FETCH_HEAD &&
595 test_expect_success '__git_refs - simple' '
596 cat >expected <<-EOF &&
600 other/branch-in-other
601 other/master-in-other
606 __git_refs >"$actual"
608 test_cmp expected "$actual"
611 test_expect_success '__git_refs - full refs' '
612 cat >expected <<-EOF &&
614 refs/heads/matching-branch
615 refs/remotes/other/branch-in-other
616 refs/remotes/other/master-in-other
617 refs/tags/matching-tag
621 __git_refs >"$actual"
623 test_cmp expected "$actual"
626 test_expect_success '__git_refs - repo given on the command line' '
627 cat >expected <<-EOF &&
634 __git_dir="$ROOT/otherrepo/.git" &&
636 __git_refs >"$actual"
638 test_cmp expected "$actual"
641 test_expect_success '__git_refs - remote on local file system' '
642 cat >expected <<-EOF &&
650 __git_refs otherrepo >"$actual"
652 test_cmp expected "$actual"
655 test_expect_success '__git_refs - remote on local file system - full refs' '
656 cat >expected <<-EOF &&
657 refs/heads/branch-in-other
658 refs/heads/master-in-other
659 refs/tags/tag-in-other
663 __git_refs otherrepo >"$actual"
665 test_cmp expected "$actual"
668 test_expect_success '__git_refs - configured remote' '
669 cat >expected <<-EOF &&
676 __git_refs other >"$actual"
678 test_cmp expected "$actual"
681 test_expect_success '__git_refs - configured remote - full refs' '
682 cat >expected <<-EOF &&
684 refs/heads/branch-in-other
685 refs/heads/master-in-other
686 refs/tags/tag-in-other
690 __git_refs other >"$actual"
692 test_cmp expected "$actual"
695 test_expect_success '__git_refs - configured remote - repo given on the command line' '
696 cat >expected <<-EOF &&
703 __git_dir="$ROOT/.git" &&
705 __git_refs other >"$actual"
707 test_cmp expected "$actual"
710 test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
711 cat >expected <<-EOF &&
713 refs/heads/branch-in-other
714 refs/heads/master-in-other
715 refs/tags/tag-in-other
719 __git_dir="$ROOT/.git" &&
721 __git_refs other >"$actual"
723 test_cmp expected "$actual"
726 test_expect_success '__git_refs - configured remote - remote name matches a directory' '
727 cat >expected <<-EOF &&
733 test_when_finished "rm -rf other" &&
736 __git_refs other >"$actual"
738 test_cmp expected "$actual"
741 test_expect_success '__git_refs - URL remote' '
742 cat >expected <<-EOF &&
750 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
752 test_cmp expected "$actual"
755 test_expect_success '__git_refs - URL remote - full refs' '
756 cat >expected <<-EOF &&
758 refs/heads/branch-in-other
759 refs/heads/master-in-other
760 refs/tags/tag-in-other
764 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
766 test_cmp expected "$actual"
769 test_expect_success '__git_refs - non-existing remote' '
772 __git_refs non-existing >"$actual"
774 test_must_be_empty "$actual"
777 test_expect_success '__git_refs - non-existing remote - full refs' '
780 __git_refs non-existing >"$actual"
782 test_must_be_empty "$actual"
785 test_expect_success '__git_refs - non-existing URL remote' '
788 __git_refs "file://$ROOT/non-existing" >"$actual"
790 test_must_be_empty "$actual"
793 test_expect_success '__git_refs - non-existing URL remote - full refs' '
796 __git_refs "file://$ROOT/non-existing" >"$actual"
798 test_must_be_empty "$actual"
801 test_expect_success '__git_refs - not in a git repository' '
803 GIT_CEILING_DIRECTORIES="$ROOT" &&
804 export GIT_CEILING_DIRECTORIES &&
807 __git_refs >"$actual"
809 test_must_be_empty "$actual"
812 test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
813 cat >expected <<-EOF &&
818 other/branch-in-other
819 other/master-in-other
821 remote/branch-in-remote
827 for remote_ref in refs/remotes/other/ambiguous \
828 refs/remotes/remote/ambiguous \
829 refs/remotes/remote/branch-in-remote
831 git update-ref $remote_ref master &&
832 test_when_finished "git update-ref -d $remote_ref"
836 __git_refs "" 1 >"$actual"
838 test_cmp expected "$actual"
841 test_expect_success '__git_refs - after --opt=' '
842 cat >expected <<-EOF &&
846 other/branch-in-other
847 other/master-in-other
852 __git_refs "" "" "" "" >"$actual"
854 test_cmp expected "$actual"
857 test_expect_success '__git_refs - after --opt= - full refs' '
858 cat >expected <<-EOF &&
860 refs/heads/matching-branch
861 refs/remotes/other/branch-in-other
862 refs/remotes/other/master-in-other
863 refs/tags/matching-tag
867 __git_refs "" "" "" refs/ >"$actual"
869 test_cmp expected "$actual"
872 test_expect_success '__git refs - exluding refs' '
873 cat >expected <<-EOF &&
877 ^other/branch-in-other
878 ^other/master-in-other
883 __git_refs >"$actual"
885 test_cmp expected "$actual"
888 test_expect_success '__git refs - exluding full refs' '
889 cat >expected <<-EOF &&
891 ^refs/heads/matching-branch
892 ^refs/remotes/other/branch-in-other
893 ^refs/remotes/other/master-in-other
894 ^refs/tags/matching-tag
898 __git_refs >"$actual"
900 test_cmp expected "$actual"
903 test_expect_success 'setup for filtering matching refs' '
904 git branch matching/branch &&
905 git tag matching/tag &&
906 git -C otherrepo branch matching/branch-in-other &&
907 git fetch --no-tags other &&
908 rm -f .git/FETCH_HEAD
911 test_expect_success '__git_refs - dont filter refs unless told so' '
912 cat >expected <<-EOF &&
917 other/branch-in-other
918 other/master-in-other
919 other/matching/branch-in-other
925 __git_refs >"$actual"
927 test_cmp expected "$actual"
930 test_expect_success '__git_refs - only matching refs' '
931 cat >expected <<-EOF &&
939 __git_refs "" "" "" "$cur" >"$actual"
941 test_cmp expected "$actual"
944 test_expect_success '__git_refs - only matching refs - full refs' '
945 cat >expected <<-EOF &&
946 refs/heads/matching-branch
947 refs/heads/matching/branch
950 cur=refs/heads/mat &&
951 __git_refs "" "" "" "$cur" >"$actual"
953 test_cmp expected "$actual"
956 test_expect_success '__git_refs - only matching refs - remote on local file system' '
957 cat >expected <<-EOF &&
959 matching/branch-in-other
963 __git_refs otherrepo "" "" "$cur" >"$actual"
965 test_cmp expected "$actual"
968 test_expect_success '__git_refs - only matching refs - configured remote' '
969 cat >expected <<-EOF &&
971 matching/branch-in-other
975 __git_refs other "" "" "$cur" >"$actual"
977 test_cmp expected "$actual"
980 test_expect_success '__git_refs - only matching refs - remote - full refs' '
981 cat >expected <<-EOF &&
982 refs/heads/master-in-other
983 refs/heads/matching/branch-in-other
987 __git_refs other "" "" "$cur" >"$actual"
989 test_cmp expected "$actual"
992 test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
993 cat >expected <<-EOF &&
998 matching/branch-in-other
1000 for remote_ref in refs/remotes/other/ambiguous \
1001 refs/remotes/remote/ambiguous \
1002 refs/remotes/remote/branch-in-remote
1004 git update-ref $remote_ref master &&
1005 test_when_finished "git update-ref -d $remote_ref"
1009 __git_refs "" 1 "" "$cur" >"$actual"
1011 test_cmp expected "$actual"
1014 test_expect_success 'teardown after filtering matching refs' '
1015 git branch -d matching/branch &&
1016 git tag -d matching/tag &&
1017 git update-ref -d refs/remotes/other/matching/branch-in-other &&
1018 git -C otherrepo branch -D matching/branch-in-other
1021 test_expect_success '__git_refs - for-each-ref format specifiers in prefix' '
1022 cat >expected <<-EOF &&
1023 evil-%%-%42-%(refname)..master
1026 cur="evil-%%-%42-%(refname)..mas" &&
1027 __git_refs "" "" "evil-%%-%42-%(refname).." mas >"$actual"
1029 test_cmp expected "$actual"
1032 test_expect_success '__git_complete_refs - simple' '
1033 sed -e "s/Z$//" >expected <<-EOF &&
1037 other/branch-in-other Z
1038 other/master-in-other Z
1043 __git_complete_refs &&
1046 test_cmp expected out
1049 test_expect_success '__git_complete_refs - matching' '
1050 sed -e "s/Z$//" >expected <<-EOF &&
1056 __git_complete_refs &&
1059 test_cmp expected out
1062 test_expect_success '__git_complete_refs - remote' '
1063 sed -e "s/Z$//" >expected <<-EOF &&
1070 __git_complete_refs --remote=other &&
1073 test_cmp expected out
1076 test_expect_success '__git_complete_refs - track' '
1077 sed -e "s/Z$//" >expected <<-EOF &&
1081 other/branch-in-other Z
1082 other/master-in-other Z
1089 __git_complete_refs --track &&
1092 test_cmp expected out
1095 test_expect_success '__git_complete_refs - current word' '
1096 sed -e "s/Z$//" >expected <<-EOF &&
1101 cur="--option=mat" &&
1102 __git_complete_refs --cur="${cur#*=}" &&
1105 test_cmp expected out
1108 test_expect_success '__git_complete_refs - prefix' '
1109 sed -e "s/Z$//" >expected <<-EOF &&
1110 v1.0..matching-branch Z
1111 v1.0..matching-tag Z
1115 __git_complete_refs --pfx=v1.0.. --cur=mat &&
1118 test_cmp expected out
1121 test_expect_success '__git_complete_refs - suffix' '
1122 cat >expected <<-EOF &&
1126 other/branch-in-other.
1127 other/master-in-other.
1132 __git_complete_refs --sfx=. &&
1135 test_cmp expected out
1138 test_expect_success '__git_complete_fetch_refspecs - simple' '
1139 sed -e "s/Z$//" >expected <<-EOF &&
1141 branch-in-other:branch-in-other Z
1142 master-in-other:master-in-other Z
1146 __git_complete_fetch_refspecs other &&
1149 test_cmp expected out
1152 test_expect_success '__git_complete_fetch_refspecs - matching' '
1153 sed -e "s/Z$//" >expected <<-EOF &&
1154 branch-in-other:branch-in-other Z
1158 __git_complete_fetch_refspecs other "" br &&
1161 test_cmp expected out
1164 test_expect_success '__git_complete_fetch_refspecs - prefix' '
1165 sed -e "s/Z$//" >expected <<-EOF &&
1167 +branch-in-other:branch-in-other Z
1168 +master-in-other:master-in-other Z
1172 __git_complete_fetch_refspecs other "+" "" &&
1175 test_cmp expected out
1178 test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
1179 sed -e "s/Z$//" >expected <<-EOF &&
1180 refs/heads/branch-in-other:refs/heads/branch-in-other Z
1181 refs/heads/master-in-other:refs/heads/master-in-other Z
1182 refs/tags/tag-in-other:refs/tags/tag-in-other Z
1186 __git_complete_fetch_refspecs other "" refs/ &&
1189 test_cmp expected out
1192 test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
1193 sed -e "s/Z$//" >expected <<-EOF &&
1194 +refs/heads/branch-in-other:refs/heads/branch-in-other Z
1195 +refs/heads/master-in-other:refs/heads/master-in-other Z
1196 +refs/tags/tag-in-other:refs/tags/tag-in-other Z
1200 __git_complete_fetch_refspecs other + refs/ &&
1203 test_cmp expected out
1206 test_expect_success 'teardown after ref completion' '
1207 git branch -d matching-branch &&
1208 git tag -d matching-tag &&
1209 git remote remove other
1213 test_path_completion ()
1215 test $# = 2 || error "bug in the test script: not 2 parameters to test_path_completion"
1217 local cur="$1" expected="$2"
1218 echo "$expected" >expected &&
1220 # In the following tests calling this function we only
1221 # care about how __git_complete_index_file() deals with
1222 # unusual characters in path names. By requesting only
1223 # untracked files we dont have to bother adding any
1224 # paths to the index in those tests.
1225 __git_complete_index_file --others &&
1228 test_cmp expected out
1231 test_expect_success 'setup for path completion tests' '
1235 touch simple-dir/simple-file \
1236 "spaces in dir/spaces in file" \
1237 "árvíztűrő/Сайн яваарай" &&
1238 if test_have_prereq !MINGW &&
1240 '$'separators\034in\035dir'' &&
1241 touch BS\\dir/DQ\"file \
1242 '$'separators\034in\035dir/sep\036in\037file''
1244 test_set_prereq FUNNYNAMES
1246 rm -rf BS\\dir '$'separators\034in\035dir''
1250 test_expect_success '__git_complete_index_file - simple' '
1251 test_path_completion simple simple-dir && # Bash is supposed to
1252 # add the trailing /.
1253 test_path_completion simple-dir/simple simple-dir/simple-file
1256 test_expect_success \
1257 '__git_complete_index_file - escaped characters on cmdline' '
1258 test_path_completion spac "spaces in dir" && # Bash will turn this
1259 # into "spaces\ in\ dir"
1260 test_path_completion "spaces\\ i" \
1262 test_path_completion "spaces\\ in\\ dir/s" \
1263 "spaces in dir/spaces in file" &&
1264 test_path_completion "spaces\\ in\\ dir/spaces\\ i" \
1265 "spaces in dir/spaces in file"
1268 test_expect_success \
1269 '__git_complete_index_file - quoted characters on cmdline' '
1270 # Testing with an opening but without a corresponding closing
1271 # double quote is important.
1272 test_path_completion \"spac "spaces in dir" &&
1273 test_path_completion "\"spaces i" \
1275 test_path_completion "\"spaces in dir/s" \
1276 "spaces in dir/spaces in file" &&
1277 test_path_completion "\"spaces in dir/spaces i" \
1278 "spaces in dir/spaces in file"
1281 test_expect_success '__git_complete_index_file - UTF-8 in ls-files output' '
1282 test_path_completion á árvíztűrő &&
1283 test_path_completion árvíztűrő/С "árvíztűrő/Сайн яваарай"
1286 test_expect_success FUNNYNAMES \
1287 '__git_complete_index_file - C-style escapes in ls-files output' '
1288 test_path_completion BS \
1290 test_path_completion BS\\\\d \
1292 test_path_completion BS\\\\dir/DQ \
1294 test_path_completion BS\\\\dir/DQ\\\"f \
1298 test_expect_success FUNNYNAMES \
1299 '__git_complete_index_file - \nnn-escaped characters in ls-files output' '
1300 test_path_completion sep '$'separators\034in\035dir'' &&
1301 test_path_completion '$'separators\034i'' \
1302 '$'separators\034in\035dir'' &&
1303 test_path_completion '$'separators\034in\035dir/sep'' \
1304 '$'separators\034in\035dir/sep\036in\037file'' &&
1305 test_path_completion '$'separators\034in\035dir/sep\036i'' \
1306 '$'separators\034in\035dir/sep\036in\037file''
1309 test_expect_success FUNNYNAMES \
1310 '__git_complete_index_file - removing repeated quoted path components' '
1311 test_when_finished rm -r repeated-quoted &&
1312 mkdir repeated-quoted && # A directory whose name in itself
1313 # would not be quoted ...
1314 >repeated-quoted/0-file &&
1315 >repeated-quoted/1\"file && # ... but here the file makes the
1316 # dirname quoted ...
1317 >repeated-quoted/2-file &&
1318 >repeated-quoted/3\"file && # ... and here, too.
1320 # Still, we shold only list the directory name only once.
1321 test_path_completion repeated repeated-quoted
1324 test_expect_success 'teardown after path completion tests' '
1325 rm -rf simple-dir "spaces in dir" árvíztűrő \
1326 BS\\dir '$'separators\034in\035dir''
1330 test_expect_success '__git_get_config_variables' '
1331 cat >expect <<-EOF &&
1335 test_config interesting.name-1 good &&
1336 test_config interesting.name-2 good &&
1337 test_config subsection.interesting.name-3 bad &&
1338 __git_get_config_variables interesting >actual &&
1339 test_cmp expect actual
1342 test_expect_success '__git_pretty_aliases' '
1343 cat >expect <<-EOF &&
1347 test_config pretty.author "%an %ae" &&
1348 test_config pretty.hash %H &&
1349 __git_pretty_aliases >actual &&
1350 test_cmp expect actual
1353 test_expect_success '__git_aliases' '
1354 cat >expect <<-EOF &&
1358 test_config alias.ci commit &&
1359 test_config alias.co checkout &&
1360 __git_aliases >actual &&
1361 test_cmp expect actual
1364 test_expect_success 'basic' '
1365 run_completion "git " &&
1367 grep -q "^add \$" out &&
1369 grep -q "^filter-branch \$" out &&
1371 ! grep -q "^ls-files \$" out &&
1373 run_completion "git f" &&
1374 ! grep -q -v "^f" out
1377 test_expect_success 'double dash "git" itself' '
1378 test_completion "git --" <<-\EOF
1391 --no-replace-objects Z
1396 test_expect_success 'double dash "git checkout"' '
1397 test_completion "git checkout --" <<-\EOF
1407 --ignore-skip-worktree-bits Z
1408 --ignore-other-worktrees Z
1409 --recurse-submodules Z
1412 --no-recurse-submodules Z
1416 test_expect_success 'general options' '
1417 test_completion "git --ver" "--version " &&
1418 test_completion "git --hel" "--help " &&
1419 test_completion "git --exe" <<-\EOF &&
1423 test_completion "git --htm" "--html-path " &&
1424 test_completion "git --pag" "--paginate " &&
1425 test_completion "git --no-p" "--no-pager " &&
1426 test_completion "git --git" "--git-dir=" &&
1427 test_completion "git --wor" "--work-tree=" &&
1428 test_completion "git --nam" "--namespace=" &&
1429 test_completion "git --bar" "--bare " &&
1430 test_completion "git --inf" "--info-path " &&
1431 test_completion "git --no-r" "--no-replace-objects "
1434 test_expect_success 'general options plus command' '
1435 test_completion "git --version check" "checkout " &&
1436 test_completion "git --paginate check" "checkout " &&
1437 test_completion "git --git-dir=foo check" "checkout " &&
1438 test_completion "git --bare check" "checkout " &&
1439 test_completion "git --exec-path=foo check" "checkout " &&
1440 test_completion "git --html-path check" "checkout " &&
1441 test_completion "git --no-pager check" "checkout " &&
1442 test_completion "git --work-tree=foo check" "checkout " &&
1443 test_completion "git --namespace=foo check" "checkout " &&
1444 test_completion "git --paginate check" "checkout " &&
1445 test_completion "git --info-path check" "checkout " &&
1446 test_completion "git --no-replace-objects check" "checkout " &&
1447 test_completion "git --git-dir some/path check" "checkout " &&
1448 test_completion "git -c conf.var=value check" "checkout " &&
1449 test_completion "git -C some/path check" "checkout " &&
1450 test_completion "git --work-tree some/path check" "checkout " &&
1451 test_completion "git --namespace name/space check" "checkout "
1454 test_expect_success 'git --help completion' '
1455 test_completion "git --help ad" "add " &&
1456 test_completion "git --help core" "core-tutorial "
1459 test_expect_success 'setup for integration tests' '
1460 echo content >file1 &&
1462 git add file1 file2 &&
1463 git commit -m one &&
1464 git branch mybranch &&
1468 test_expect_success 'checkout completes ref names' '
1469 test_completion "git checkout m" <<-\EOF
1476 test_expect_success 'git -C <path> checkout uses the right repo' '
1477 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
1482 test_expect_success 'show completes all refs' '
1483 test_completion "git show m" <<-\EOF
1490 test_expect_success '<ref>: completes paths' '
1491 test_completion "git show mytag:f" <<-\EOF
1497 test_expect_success 'complete tree filename with spaces' '
1498 echo content >"name with spaces" &&
1499 git add "name with spaces" &&
1500 git commit -m spaces &&
1501 test_completion "git show HEAD:nam" <<-\EOF
1506 test_expect_success 'complete tree filename with metacharacters' '
1507 echo content >"name with \${meta}" &&
1508 git add "name with \${meta}" &&
1509 git commit -m meta &&
1510 test_completion "git show HEAD:nam" <<-\EOF
1516 test_expect_success 'send-email' '
1517 test_completion "git send-email --cov" "--cover-letter " &&
1518 test_completion "git send-email ma" "master "
1521 test_expect_success 'complete files' '
1522 git init tmp && cd tmp &&
1523 test_when_finished "cd .. && rm -rf tmp" &&
1525 echo "expected" > .gitignore &&
1526 echo "out" >> .gitignore &&
1527 echo "out_sorted" >> .gitignore &&
1529 git add .gitignore &&
1530 test_completion "git commit " ".gitignore" &&
1532 git commit -m ignore &&
1535 test_completion "git add " "new" &&
1538 git commit -a -m new &&
1539 test_completion "git add " "" &&
1541 git mv new modified &&
1542 echo modify > modified &&
1543 test_completion "git add " "modified" &&
1547 : TODO .gitignore should not be here &&
1548 test_completion "git rm " <<-\EOF &&
1553 test_completion "git clean " "untracked" &&
1555 : TODO .gitignore should not be here &&
1556 test_completion "git mv " <<-\EOF &&
1562 touch dir/file-in-dir &&
1563 git add dir/file-in-dir &&
1564 git commit -m dir &&
1566 mkdir untracked-dir &&
1568 : TODO .gitignore should not be here &&
1569 test_completion "git mv modified " <<-\EOF &&
1577 test_completion "git commit " "modified" &&
1579 : TODO .gitignore should not be here &&
1580 test_completion "git ls-files " <<-\EOF &&
1587 test_completion "git add mom" "momified"
1590 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
1591 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
1592 test_completion "git co m" <<-\EOF
1599 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
1600 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
1601 test_completion "git co m" <<-\EOF
1608 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
1609 test_config alias.co "!f() { : git checkout ; if ... } f" &&
1610 test_completion "git co m" <<-\EOF
1617 test_expect_success 'completion without explicit _git_xxx function' '
1618 test_completion "git version --" <<-\EOF
1623 test_expect_failure 'complete with tilde expansion' '
1624 git init tmp && cd tmp &&
1625 test_when_finished "cd .. && rm -rf tmp" &&
1629 test_completion "git add ~/tmp/" "~/tmp/file"
1632 test_expect_success 'setup other remote for remote reference completion' '
1633 git remote add other otherrepo &&
1637 for flag in -d --delete
1639 test_expect_success "__git_complete_remote_or_refspec - push $flag other" '
1640 sed -e "s/Z$//" >expected <<-EOF &&
1644 words=(git push '$flag' other ma) &&
1645 cword=${#words[@]} cur=${words[cword-1]} &&
1646 __git_complete_remote_or_refspec &&
1649 test_cmp expected out
1652 test_expect_failure "__git_complete_remote_or_refspec - push other $flag" '
1653 sed -e "s/Z$//" >expected <<-EOF &&
1657 words=(git push other '$flag' ma) &&
1658 cword=${#words[@]} cur=${words[cword-1]} &&
1659 __git_complete_remote_or_refspec &&
1662 test_cmp expected out
1666 test_expect_success 'sourcing the completion script clears cached commands' '
1667 __git_compute_all_commands &&
1668 verbose test -n "$__git_all_commands" &&
1669 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1670 verbose test -z "$__git_all_commands"
1673 test_expect_success 'sourcing the completion script clears cached porcelain commands' '
1674 __git_compute_porcelain_commands &&
1675 verbose test -n "$__git_porcelain_commands" &&
1676 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1677 verbose test -z "$__git_porcelain_commands"
1680 test_expect_success !GETTEXT_POISON 'sourcing the completion script clears cached merge strategies' '
1681 __git_compute_merge_strategies &&
1682 verbose test -n "$__git_merge_strategies" &&
1683 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1684 verbose test -z "$__git_merge_strategies"
1687 test_expect_success 'sourcing the completion script clears cached --options' '
1688 __gitcomp_builtin checkout &&
1689 verbose test -n "$__gitcomp_builtin_checkout" &&
1690 __gitcomp_builtin notes_edit &&
1691 verbose test -n "$__gitcomp_builtin_notes_edit" &&
1692 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1693 verbose test -z "$__gitcomp_builtin_checkout" &&
1694 verbose test -z "$__gitcomp_builtin_notes_edit"