]> git.ipfire.org Git - thirdparty/git.git/blob - t/t9902-completion.sh
Merge branch 'en/fetch-negotiation-default-fix'
[thirdparty/git.git] / t / t9902-completion.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012-2020 Felipe Contreras
4 #
5
6 test_description='test bash completion'
7
8 . ./lib-bash.sh
9
10 complete ()
11 {
12 # do nothing
13 return 0
14 }
15
16 # Be careful when updating these lists:
17 #
18 # (1) The build tree may have build artifact from different branch, or
19 # the user's $PATH may have a random executable that may begin
20 # with "git-check" that are not part of the subcommands this build
21 # will ship, e.g. "check-ignore". The tests for completion for
22 # subcommand names tests how "check" is expanded; we limit the
23 # possible candidates to "checkout" and "check-attr" to make sure
24 # "check-attr", which is known by the filter function as a
25 # subcommand to be thrown out, while excluding other random files
26 # that happen to begin with "check" to avoid letting them get in
27 # the way.
28 #
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 # "rebase" and "ls-files" are listed for this.
32
33 GIT_TESTING_ALL_COMMAND_LIST='add checkout check-attr rebase ls-files'
34 GIT_TESTING_PORCELAIN_COMMAND_LIST='add checkout rebase'
35
36 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
37
38 # We don't need this function to actually join words or do anything special.
39 # Also, it's cleaner to avoid touching bash's internal completion variables.
40 # So let's override it with a minimal version for testing purposes.
41 _get_comp_words_by_ref ()
42 {
43 while [ $# -gt 0 ]; do
44 case "$1" in
45 cur)
46 cur=${_words[_cword]}
47 ;;
48 prev)
49 prev=${_words[_cword-1]}
50 ;;
51 words)
52 words=("${_words[@]}")
53 ;;
54 cword)
55 cword=$_cword
56 ;;
57 esac
58 shift
59 done
60 }
61
62 print_comp ()
63 {
64 local IFS=$'\n'
65 echo "${COMPREPLY[*]}" > out
66 }
67
68 run_completion ()
69 {
70 local -a COMPREPLY _words
71 local _cword
72 _words=( $1 )
73 test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
74 (( _cword = ${#_words[@]} - 1 ))
75 __git_wrap__git_main && print_comp
76 }
77
78 # Test high-level completion
79 # Arguments are:
80 # 1: typed text so far (cur)
81 # 2: expected completion
82 test_completion ()
83 {
84 if test $# -gt 1
85 then
86 printf '%s\n' "$2" >expected
87 else
88 sed -e 's/Z$//' |sort >expected
89 fi &&
90 run_completion "$1" &&
91 sort out >out_sorted &&
92 test_cmp expected out_sorted
93 }
94
95 # Test __gitcomp.
96 # The first argument is the typed text so far (cur); the rest are
97 # passed to __gitcomp. Expected output comes is read from the
98 # standard input, like test_completion().
99 test_gitcomp ()
100 {
101 local -a COMPREPLY &&
102 sed -e 's/Z$//' >expected &&
103 local cur="$1" &&
104 shift &&
105 __gitcomp "$@" &&
106 print_comp &&
107 test_cmp expected out
108 }
109
110 # Test __gitcomp_nl
111 # Arguments are:
112 # 1: current word (cur)
113 # -: the rest are passed to __gitcomp_nl
114 test_gitcomp_nl ()
115 {
116 local -a COMPREPLY &&
117 sed -e 's/Z$//' >expected &&
118 local cur="$1" &&
119 shift &&
120 __gitcomp_nl "$@" &&
121 print_comp &&
122 test_cmp expected out
123 }
124
125 invalid_variable_name='${foo.bar}'
126
127 actual="$TRASH_DIRECTORY/actual"
128
129 if test_have_prereq MINGW
130 then
131 ROOT="$(pwd -W)"
132 else
133 ROOT="$(pwd)"
134 fi
135
136 test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
137 mkdir -p subdir/subsubdir &&
138 mkdir -p non-repo &&
139 git init -b main otherrepo
140 '
141
142 test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '
143 echo "$ROOT/otherrepo/.git" >expected &&
144 (
145 __git_dir="$ROOT/otherrepo/.git" &&
146 __git_find_repo_path &&
147 echo "$__git_repo_path" >"$actual"
148 ) &&
149 test_cmp expected "$actual"
150 '
151
152 test_expect_success '__git_find_repo_path - .git directory in cwd' '
153 echo ".git" >expected &&
154 (
155 __git_find_repo_path &&
156 echo "$__git_repo_path" >"$actual"
157 ) &&
158 test_cmp expected "$actual"
159 '
160
161 test_expect_success '__git_find_repo_path - .git directory in parent' '
162 echo "$ROOT/.git" >expected &&
163 (
164 cd subdir/subsubdir &&
165 __git_find_repo_path &&
166 echo "$__git_repo_path" >"$actual"
167 ) &&
168 test_cmp expected "$actual"
169 '
170
171 test_expect_success '__git_find_repo_path - cwd is a .git directory' '
172 echo "." >expected &&
173 (
174 cd .git &&
175 __git_find_repo_path &&
176 echo "$__git_repo_path" >"$actual"
177 ) &&
178 test_cmp expected "$actual"
179 '
180
181 test_expect_success '__git_find_repo_path - parent is a .git directory' '
182 echo "$ROOT/.git" >expected &&
183 (
184 cd .git/objects &&
185 __git_find_repo_path &&
186 echo "$__git_repo_path" >"$actual"
187 ) &&
188 test_cmp expected "$actual"
189 '
190
191 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
192 echo "$ROOT/otherrepo/.git" >expected &&
193 (
194 GIT_DIR="$ROOT/otherrepo/.git" &&
195 export GIT_DIR &&
196 __git_find_repo_path &&
197 echo "$__git_repo_path" >"$actual"
198 ) &&
199 test_cmp expected "$actual"
200 '
201
202 test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
203 echo "$ROOT/otherrepo/.git" >expected &&
204 (
205 GIT_DIR="$ROOT/otherrepo/.git" &&
206 export GIT_DIR &&
207 cd subdir &&
208 __git_find_repo_path &&
209 echo "$__git_repo_path" >"$actual"
210 ) &&
211 test_cmp expected "$actual"
212 '
213
214 test_expect_success '__git_find_repo_path - from command line while "git -C"' '
215 echo "$ROOT/.git" >expected &&
216 (
217 __git_dir="$ROOT/.git" &&
218 __git_C_args=(-C otherrepo) &&
219 __git_find_repo_path &&
220 echo "$__git_repo_path" >"$actual"
221 ) &&
222 test_cmp expected "$actual"
223 '
224
225 test_expect_success '__git_find_repo_path - relative dir from command line and "git -C"' '
226 echo "$ROOT/otherrepo/.git" >expected &&
227 (
228 cd subdir &&
229 __git_dir="otherrepo/.git" &&
230 __git_C_args=(-C ..) &&
231 __git_find_repo_path &&
232 echo "$__git_repo_path" >"$actual"
233 ) &&
234 test_cmp expected "$actual"
235 '
236
237 test_expect_success '__git_find_repo_path - $GIT_DIR set while "git -C"' '
238 echo "$ROOT/.git" >expected &&
239 (
240 GIT_DIR="$ROOT/.git" &&
241 export GIT_DIR &&
242 __git_C_args=(-C otherrepo) &&
243 __git_find_repo_path &&
244 echo "$__git_repo_path" >"$actual"
245 ) &&
246 test_cmp expected "$actual"
247 '
248
249 test_expect_success '__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
250 echo "$ROOT/otherrepo/.git" >expected &&
251 (
252 cd subdir &&
253 GIT_DIR="otherrepo/.git" &&
254 export GIT_DIR &&
255 __git_C_args=(-C ..) &&
256 __git_find_repo_path &&
257 echo "$__git_repo_path" >"$actual"
258 ) &&
259 test_cmp expected "$actual"
260 '
261
262 test_expect_success '__git_find_repo_path - "git -C" while .git directory in cwd' '
263 echo "$ROOT/otherrepo/.git" >expected &&
264 (
265 __git_C_args=(-C otherrepo) &&
266 __git_find_repo_path &&
267 echo "$__git_repo_path" >"$actual"
268 ) &&
269 test_cmp expected "$actual"
270 '
271
272 test_expect_success '__git_find_repo_path - "git -C" while cwd is a .git directory' '
273 echo "$ROOT/otherrepo/.git" >expected &&
274 (
275 cd .git &&
276 __git_C_args=(-C .. -C otherrepo) &&
277 __git_find_repo_path &&
278 echo "$__git_repo_path" >"$actual"
279 ) &&
280 test_cmp expected "$actual"
281 '
282
283 test_expect_success '__git_find_repo_path - "git -C" while .git directory in parent' '
284 echo "$ROOT/otherrepo/.git" >expected &&
285 (
286 cd subdir &&
287 __git_C_args=(-C .. -C otherrepo) &&
288 __git_find_repo_path &&
289 echo "$__git_repo_path" >"$actual"
290 ) &&
291 test_cmp expected "$actual"
292 '
293
294 test_expect_success '__git_find_repo_path - non-existing path in "git -C"' '
295 (
296 __git_C_args=(-C non-existing) &&
297 test_must_fail __git_find_repo_path &&
298 printf "$__git_repo_path" >"$actual"
299 ) &&
300 test_must_be_empty "$actual"
301 '
302
303 test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '
304 (
305 __git_dir="non-existing" &&
306 test_must_fail __git_find_repo_path &&
307 printf "$__git_repo_path" >"$actual"
308 ) &&
309 test_must_be_empty "$actual"
310 '
311
312 test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
313 (
314 GIT_DIR="$ROOT/non-existing" &&
315 export GIT_DIR &&
316 test_must_fail __git_find_repo_path &&
317 printf "$__git_repo_path" >"$actual"
318 ) &&
319 test_must_be_empty "$actual"
320 '
321
322 test_expect_success '__git_find_repo_path - gitfile in cwd' '
323 echo "$ROOT/otherrepo/.git" >expected &&
324 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
325 test_when_finished "rm -f subdir/.git" &&
326 (
327 cd subdir &&
328 __git_find_repo_path &&
329 echo "$__git_repo_path" >"$actual"
330 ) &&
331 test_cmp expected "$actual"
332 '
333
334 test_expect_success '__git_find_repo_path - gitfile in parent' '
335 echo "$ROOT/otherrepo/.git" >expected &&
336 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
337 test_when_finished "rm -f subdir/.git" &&
338 (
339 cd subdir/subsubdir &&
340 __git_find_repo_path &&
341 echo "$__git_repo_path" >"$actual"
342 ) &&
343 test_cmp expected "$actual"
344 '
345
346 test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '
347 echo "$ROOT/otherrepo/.git" >expected &&
348 mkdir otherrepo/dir &&
349 test_when_finished "rm -rf otherrepo/dir" &&
350 ln -s otherrepo/dir link &&
351 test_when_finished "rm -f link" &&
352 (
353 cd link &&
354 __git_find_repo_path &&
355 echo "$__git_repo_path" >"$actual"
356 ) &&
357 test_cmp expected "$actual"
358 '
359
360 test_expect_success '__git_find_repo_path - not a git repository' '
361 (
362 cd non-repo &&
363 GIT_CEILING_DIRECTORIES="$ROOT" &&
364 export GIT_CEILING_DIRECTORIES &&
365 test_must_fail __git_find_repo_path &&
366 printf "$__git_repo_path" >"$actual"
367 ) &&
368 test_must_be_empty "$actual"
369 '
370
371 test_expect_success '__gitdir - finds repo' '
372 echo "$ROOT/.git" >expected &&
373 (
374 cd subdir/subsubdir &&
375 __gitdir >"$actual"
376 ) &&
377 test_cmp expected "$actual"
378 '
379
380
381 test_expect_success '__gitdir - returns error when cannot find repo' '
382 (
383 __git_dir="non-existing" &&
384 test_must_fail __gitdir >"$actual"
385 ) &&
386 test_must_be_empty "$actual"
387 '
388
389 test_expect_success '__gitdir - repo as argument' '
390 echo "otherrepo/.git" >expected &&
391 (
392 __gitdir "otherrepo" >"$actual"
393 ) &&
394 test_cmp expected "$actual"
395 '
396
397 test_expect_success '__gitdir - remote as argument' '
398 echo "remote" >expected &&
399 (
400 __gitdir "remote" >"$actual"
401 ) &&
402 test_cmp expected "$actual"
403 '
404
405
406 test_expect_success '__git_dequote - plain unquoted word' '
407 __git_dequote unquoted-word &&
408 verbose test unquoted-word = "$dequoted_word"
409 '
410
411 # input: b\a\c\k\'\\\"s\l\a\s\h\es
412 # expected: back'\"slashes
413 test_expect_success '__git_dequote - backslash escaped' '
414 __git_dequote "b\a\c\k\\'\''\\\\\\\"s\l\a\s\h\es" &&
415 verbose test "back'\''\\\"slashes" = "$dequoted_word"
416 '
417
418 # input: sin'gle\' '"quo'ted
419 # expected: single\ "quoted
420 test_expect_success '__git_dequote - single quoted' '
421 __git_dequote "'"sin'gle\\\\' '\\\"quo'ted"'" &&
422 verbose test '\''single\ "quoted'\'' = "$dequoted_word"
423 '
424
425 # input: dou"ble\\" "\"\quot"ed
426 # expected: double\ "\quoted
427 test_expect_success '__git_dequote - double quoted' '
428 __git_dequote '\''dou"ble\\" "\"\quot"ed'\'' &&
429 verbose test '\''double\ "\quoted'\'' = "$dequoted_word"
430 '
431
432 # input: 'open single quote
433 test_expect_success '__git_dequote - open single quote' '
434 __git_dequote "'\''open single quote" &&
435 verbose test "open single quote" = "$dequoted_word"
436 '
437
438 # input: "open double quote
439 test_expect_success '__git_dequote - open double quote' '
440 __git_dequote "\"open double quote" &&
441 verbose test "open double quote" = "$dequoted_word"
442 '
443
444
445 test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' '
446 sed -e "s/Z$//g" >expected <<-EOF &&
447 with-trailing-space Z
448 without-trailing-spaceZ
449 --option Z
450 --option=Z
451 $invalid_variable_name Z
452 EOF
453 (
454 cur=should_be_ignored &&
455 __gitcomp_direct "$(cat expected)" &&
456 print_comp
457 ) &&
458 test_cmp expected out
459 '
460
461 test_expect_success '__gitcomp - trailing space - options' '
462 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
463 --reset-author" <<-EOF
464 --reuse-message=Z
465 --reedit-message=Z
466 --reset-author Z
467 EOF
468 '
469
470 test_expect_success '__gitcomp - trailing space - config keys' '
471 test_gitcomp "br" "branch. branch.autosetupmerge
472 branch.autosetuprebase browser." <<-\EOF
473 branch.Z
474 branch.autosetupmerge Z
475 branch.autosetuprebase Z
476 browser.Z
477 EOF
478 '
479
480 test_expect_success '__gitcomp - option parameter' '
481 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
482 "" "re" <<-\EOF
483 recursive Z
484 resolve Z
485 EOF
486 '
487
488 test_expect_success '__gitcomp - prefix' '
489 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
490 "branch.maint." "me" <<-\EOF
491 branch.maint.merge Z
492 branch.maint.mergeoptions Z
493 EOF
494 '
495
496 test_expect_success '__gitcomp - suffix' '
497 test_gitcomp "branch.me" "master maint next seen" "branch." \
498 "ma" "." <<-\EOF
499 branch.master.Z
500 branch.maint.Z
501 EOF
502 '
503
504 test_expect_success '__gitcomp - ignore optional negative options' '
505 test_gitcomp "--" "--abc --def --no-one -- --no-two" <<-\EOF
506 --abc Z
507 --def Z
508 --no-one Z
509 --no-... Z
510 EOF
511 '
512
513 test_expect_success '__gitcomp - ignore/narrow optional negative options' '
514 test_gitcomp "--a" "--abc --abcdef --no-one -- --no-two" <<-\EOF
515 --abc Z
516 --abcdef Z
517 EOF
518 '
519
520 test_expect_success '__gitcomp - ignore/narrow optional negative options' '
521 test_gitcomp "--n" "--abc --def --no-one -- --no-two" <<-\EOF
522 --no-one Z
523 --no-... Z
524 EOF
525 '
526
527 test_expect_success '__gitcomp - expand all negative options' '
528 test_gitcomp "--no-" "--abc --def --no-one -- --no-two" <<-\EOF
529 --no-one Z
530 --no-two Z
531 EOF
532 '
533
534 test_expect_success '__gitcomp - expand/narrow all negative options' '
535 test_gitcomp "--no-o" "--abc --def --no-one -- --no-two" <<-\EOF
536 --no-one Z
537 EOF
538 '
539
540 test_expect_success '__gitcomp - equal skip' '
541 test_gitcomp "--option=" "--option=" <<-\EOF &&
542
543 EOF
544 test_gitcomp "option=" "option=" <<-\EOF
545
546 EOF
547 '
548
549 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
550 __gitcomp "$invalid_variable_name"
551 '
552
553 read -r -d "" refs <<-\EOF
554 main
555 maint
556 next
557 seen
558 EOF
559
560 test_expect_success '__gitcomp_nl - trailing space' '
561 test_gitcomp_nl "m" "$refs" <<-EOF
562 main Z
563 maint Z
564 EOF
565 '
566
567 test_expect_success '__gitcomp_nl - prefix' '
568 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
569 --fixup=main Z
570 --fixup=maint Z
571 EOF
572 '
573
574 test_expect_success '__gitcomp_nl - suffix' '
575 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
576 branch.main.Z
577 branch.maint.Z
578 EOF
579 '
580
581 test_expect_success '__gitcomp_nl - no suffix' '
582 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
583 mainZ
584 maintZ
585 EOF
586 '
587
588 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
589 __gitcomp_nl "$invalid_variable_name"
590 '
591
592 test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
593 cat >expect <<-EOF &&
594 remote_from_file_1
595 remote_from_file_2
596 remote_in_config_1
597 remote_in_config_2
598 EOF
599 test_when_finished "rm -rf .git/remotes" &&
600 mkdir -p .git/remotes &&
601 >.git/remotes/remote_from_file_1 &&
602 >.git/remotes/remote_from_file_2 &&
603 test_when_finished "git remote remove remote_in_config_1" &&
604 git remote add remote_in_config_1 git://remote_1 &&
605 test_when_finished "git remote remove remote_in_config_2" &&
606 git remote add remote_in_config_2 git://remote_2 &&
607 (
608 __git_remotes >actual
609 ) &&
610 test_cmp expect actual
611 '
612
613 test_expect_success '__git_is_configured_remote' '
614 test_when_finished "git remote remove remote_1" &&
615 git remote add remote_1 git://remote_1 &&
616 test_when_finished "git remote remove remote_2" &&
617 git remote add remote_2 git://remote_2 &&
618 (
619 verbose __git_is_configured_remote remote_2 &&
620 test_must_fail __git_is_configured_remote non-existent
621 )
622 '
623
624 test_expect_success 'setup for ref completion' '
625 git commit --allow-empty -m initial &&
626 git branch -M main &&
627 git branch matching-branch &&
628 git tag matching-tag &&
629 (
630 cd otherrepo &&
631 git commit --allow-empty -m initial &&
632 git branch -m main main-in-other &&
633 git branch branch-in-other &&
634 git tag tag-in-other
635 ) &&
636 git remote add other "$ROOT/otherrepo/.git" &&
637 git fetch --no-tags other &&
638 rm -f .git/FETCH_HEAD &&
639 git init thirdrepo
640 '
641
642 test_expect_success '__git_refs - simple' '
643 cat >expected <<-EOF &&
644 HEAD
645 main
646 matching-branch
647 other/branch-in-other
648 other/main-in-other
649 matching-tag
650 EOF
651 (
652 cur= &&
653 __git_refs >"$actual"
654 ) &&
655 test_cmp expected "$actual"
656 '
657
658 test_expect_success '__git_refs - full refs' '
659 cat >expected <<-EOF &&
660 refs/heads/main
661 refs/heads/matching-branch
662 refs/remotes/other/branch-in-other
663 refs/remotes/other/main-in-other
664 refs/tags/matching-tag
665 EOF
666 (
667 cur=refs/heads/ &&
668 __git_refs >"$actual"
669 ) &&
670 test_cmp expected "$actual"
671 '
672
673 test_expect_success '__git_refs - repo given on the command line' '
674 cat >expected <<-EOF &&
675 HEAD
676 branch-in-other
677 main-in-other
678 tag-in-other
679 EOF
680 (
681 __git_dir="$ROOT/otherrepo/.git" &&
682 cur= &&
683 __git_refs >"$actual"
684 ) &&
685 test_cmp expected "$actual"
686 '
687
688 test_expect_success '__git_refs - remote on local file system' '
689 cat >expected <<-EOF &&
690 HEAD
691 branch-in-other
692 main-in-other
693 tag-in-other
694 EOF
695 (
696 cur= &&
697 __git_refs otherrepo >"$actual"
698 ) &&
699 test_cmp expected "$actual"
700 '
701
702 test_expect_success '__git_refs - remote on local file system - full refs' '
703 cat >expected <<-EOF &&
704 refs/heads/branch-in-other
705 refs/heads/main-in-other
706 refs/tags/tag-in-other
707 EOF
708 (
709 cur=refs/ &&
710 __git_refs otherrepo >"$actual"
711 ) &&
712 test_cmp expected "$actual"
713 '
714
715 test_expect_success '__git_refs - configured remote' '
716 cat >expected <<-EOF &&
717 HEAD
718 branch-in-other
719 main-in-other
720 EOF
721 (
722 cur= &&
723 __git_refs other >"$actual"
724 ) &&
725 test_cmp expected "$actual"
726 '
727
728 test_expect_success '__git_refs - configured remote - full refs' '
729 cat >expected <<-EOF &&
730 HEAD
731 refs/heads/branch-in-other
732 refs/heads/main-in-other
733 refs/tags/tag-in-other
734 EOF
735 (
736 cur=refs/ &&
737 __git_refs other >"$actual"
738 ) &&
739 test_cmp expected "$actual"
740 '
741
742 test_expect_success '__git_refs - configured remote - repo given on the command line' '
743 cat >expected <<-EOF &&
744 HEAD
745 branch-in-other
746 main-in-other
747 EOF
748 (
749 cd thirdrepo &&
750 __git_dir="$ROOT/.git" &&
751 cur= &&
752 __git_refs other >"$actual"
753 ) &&
754 test_cmp expected "$actual"
755 '
756
757 test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
758 cat >expected <<-EOF &&
759 HEAD
760 refs/heads/branch-in-other
761 refs/heads/main-in-other
762 refs/tags/tag-in-other
763 EOF
764 (
765 cd thirdrepo &&
766 __git_dir="$ROOT/.git" &&
767 cur=refs/ &&
768 __git_refs other >"$actual"
769 ) &&
770 test_cmp expected "$actual"
771 '
772
773 test_expect_success '__git_refs - configured remote - remote name matches a directory' '
774 cat >expected <<-EOF &&
775 HEAD
776 branch-in-other
777 main-in-other
778 EOF
779 mkdir other &&
780 test_when_finished "rm -rf other" &&
781 (
782 cur= &&
783 __git_refs other >"$actual"
784 ) &&
785 test_cmp expected "$actual"
786 '
787
788 test_expect_success '__git_refs - URL remote' '
789 cat >expected <<-EOF &&
790 HEAD
791 branch-in-other
792 main-in-other
793 tag-in-other
794 EOF
795 (
796 cur= &&
797 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
798 ) &&
799 test_cmp expected "$actual"
800 '
801
802 test_expect_success '__git_refs - URL remote - full refs' '
803 cat >expected <<-EOF &&
804 HEAD
805 refs/heads/branch-in-other
806 refs/heads/main-in-other
807 refs/tags/tag-in-other
808 EOF
809 (
810 cur=refs/ &&
811 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
812 ) &&
813 test_cmp expected "$actual"
814 '
815
816 test_expect_success '__git_refs - non-existing remote' '
817 (
818 cur= &&
819 __git_refs non-existing >"$actual"
820 ) &&
821 test_must_be_empty "$actual"
822 '
823
824 test_expect_success '__git_refs - non-existing remote - full refs' '
825 (
826 cur=refs/ &&
827 __git_refs non-existing >"$actual"
828 ) &&
829 test_must_be_empty "$actual"
830 '
831
832 test_expect_success '__git_refs - non-existing URL remote' '
833 (
834 cur= &&
835 __git_refs "file://$ROOT/non-existing" >"$actual"
836 ) &&
837 test_must_be_empty "$actual"
838 '
839
840 test_expect_success '__git_refs - non-existing URL remote - full refs' '
841 (
842 cur=refs/ &&
843 __git_refs "file://$ROOT/non-existing" >"$actual"
844 ) &&
845 test_must_be_empty "$actual"
846 '
847
848 test_expect_success '__git_refs - not in a git repository' '
849 (
850 GIT_CEILING_DIRECTORIES="$ROOT" &&
851 export GIT_CEILING_DIRECTORIES &&
852 cd subdir &&
853 cur= &&
854 __git_refs >"$actual"
855 ) &&
856 test_must_be_empty "$actual"
857 '
858
859 test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
860 cat >expected <<-EOF &&
861 HEAD
862 main
863 matching-branch
864 other/ambiguous
865 other/branch-in-other
866 other/main-in-other
867 remote/ambiguous
868 remote/branch-in-remote
869 matching-tag
870 branch-in-other
871 branch-in-remote
872 main-in-other
873 EOF
874 for remote_ref in refs/remotes/other/ambiguous \
875 refs/remotes/remote/ambiguous \
876 refs/remotes/remote/branch-in-remote
877 do
878 git update-ref $remote_ref main &&
879 test_when_finished "git update-ref -d $remote_ref" || return 1
880 done &&
881 (
882 cur= &&
883 __git_refs "" 1 >"$actual"
884 ) &&
885 test_cmp expected "$actual"
886 '
887
888 test_expect_success '__git_refs - after --opt=' '
889 cat >expected <<-EOF &&
890 HEAD
891 main
892 matching-branch
893 other/branch-in-other
894 other/main-in-other
895 matching-tag
896 EOF
897 (
898 cur="--opt=" &&
899 __git_refs "" "" "" "" >"$actual"
900 ) &&
901 test_cmp expected "$actual"
902 '
903
904 test_expect_success '__git_refs - after --opt= - full refs' '
905 cat >expected <<-EOF &&
906 refs/heads/main
907 refs/heads/matching-branch
908 refs/remotes/other/branch-in-other
909 refs/remotes/other/main-in-other
910 refs/tags/matching-tag
911 EOF
912 (
913 cur="--opt=refs/" &&
914 __git_refs "" "" "" refs/ >"$actual"
915 ) &&
916 test_cmp expected "$actual"
917 '
918
919 test_expect_success '__git refs - excluding refs' '
920 cat >expected <<-EOF &&
921 ^HEAD
922 ^main
923 ^matching-branch
924 ^other/branch-in-other
925 ^other/main-in-other
926 ^matching-tag
927 EOF
928 (
929 cur=^ &&
930 __git_refs >"$actual"
931 ) &&
932 test_cmp expected "$actual"
933 '
934
935 test_expect_success '__git refs - excluding full refs' '
936 cat >expected <<-EOF &&
937 ^refs/heads/main
938 ^refs/heads/matching-branch
939 ^refs/remotes/other/branch-in-other
940 ^refs/remotes/other/main-in-other
941 ^refs/tags/matching-tag
942 EOF
943 (
944 cur=^refs/ &&
945 __git_refs >"$actual"
946 ) &&
947 test_cmp expected "$actual"
948 '
949
950 test_expect_success 'setup for filtering matching refs' '
951 git branch matching/branch &&
952 git tag matching/tag &&
953 git -C otherrepo branch matching/branch-in-other &&
954 git fetch --no-tags other &&
955 rm -f .git/FETCH_HEAD
956 '
957
958 test_expect_success '__git_refs - do not filter refs unless told so' '
959 cat >expected <<-EOF &&
960 HEAD
961 main
962 matching-branch
963 matching/branch
964 other/branch-in-other
965 other/main-in-other
966 other/matching/branch-in-other
967 matching-tag
968 matching/tag
969 EOF
970 (
971 cur=main &&
972 __git_refs >"$actual"
973 ) &&
974 test_cmp expected "$actual"
975 '
976
977 test_expect_success '__git_refs - only matching refs' '
978 cat >expected <<-EOF &&
979 matching-branch
980 matching/branch
981 matching-tag
982 matching/tag
983 EOF
984 (
985 cur=mat &&
986 __git_refs "" "" "" "$cur" >"$actual"
987 ) &&
988 test_cmp expected "$actual"
989 '
990
991 test_expect_success '__git_refs - only matching refs - full refs' '
992 cat >expected <<-EOF &&
993 refs/heads/matching-branch
994 refs/heads/matching/branch
995 EOF
996 (
997 cur=refs/heads/mat &&
998 __git_refs "" "" "" "$cur" >"$actual"
999 ) &&
1000 test_cmp expected "$actual"
1001 '
1002
1003 test_expect_success '__git_refs - only matching refs - remote on local file system' '
1004 cat >expected <<-EOF &&
1005 main-in-other
1006 matching/branch-in-other
1007 EOF
1008 (
1009 cur=ma &&
1010 __git_refs otherrepo "" "" "$cur" >"$actual"
1011 ) &&
1012 test_cmp expected "$actual"
1013 '
1014
1015 test_expect_success '__git_refs - only matching refs - configured remote' '
1016 cat >expected <<-EOF &&
1017 main-in-other
1018 matching/branch-in-other
1019 EOF
1020 (
1021 cur=ma &&
1022 __git_refs other "" "" "$cur" >"$actual"
1023 ) &&
1024 test_cmp expected "$actual"
1025 '
1026
1027 test_expect_success '__git_refs - only matching refs - remote - full refs' '
1028 cat >expected <<-EOF &&
1029 refs/heads/main-in-other
1030 refs/heads/matching/branch-in-other
1031 EOF
1032 (
1033 cur=refs/heads/ma &&
1034 __git_refs other "" "" "$cur" >"$actual"
1035 ) &&
1036 test_cmp expected "$actual"
1037 '
1038
1039 test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
1040 cat >expected <<-EOF &&
1041 matching-branch
1042 matching/branch
1043 matching-tag
1044 matching/tag
1045 matching/branch-in-other
1046 EOF
1047 for remote_ref in refs/remotes/other/ambiguous \
1048 refs/remotes/remote/ambiguous \
1049 refs/remotes/remote/branch-in-remote
1050 do
1051 git update-ref $remote_ref main &&
1052 test_when_finished "git update-ref -d $remote_ref" || return 1
1053 done &&
1054 (
1055 cur=mat &&
1056 __git_refs "" 1 "" "$cur" >"$actual"
1057 ) &&
1058 test_cmp expected "$actual"
1059 '
1060
1061 test_expect_success 'teardown after filtering matching refs' '
1062 git branch -d matching/branch &&
1063 git tag -d matching/tag &&
1064 git update-ref -d refs/remotes/other/matching/branch-in-other &&
1065 git -C otherrepo branch -D matching/branch-in-other
1066 '
1067
1068 test_expect_success '__git_refs - for-each-ref format specifiers in prefix' '
1069 cat >expected <<-EOF &&
1070 evil-%%-%42-%(refname)..main
1071 EOF
1072 (
1073 cur="evil-%%-%42-%(refname)..mai" &&
1074 __git_refs "" "" "evil-%%-%42-%(refname).." mai >"$actual"
1075 ) &&
1076 test_cmp expected "$actual"
1077 '
1078
1079 test_expect_success '__git_complete_refs - simple' '
1080 sed -e "s/Z$//" >expected <<-EOF &&
1081 HEAD Z
1082 main Z
1083 matching-branch Z
1084 other/branch-in-other Z
1085 other/main-in-other Z
1086 matching-tag Z
1087 EOF
1088 (
1089 cur= &&
1090 __git_complete_refs &&
1091 print_comp
1092 ) &&
1093 test_cmp expected out
1094 '
1095
1096 test_expect_success '__git_complete_refs - matching' '
1097 sed -e "s/Z$//" >expected <<-EOF &&
1098 matching-branch Z
1099 matching-tag Z
1100 EOF
1101 (
1102 cur=mat &&
1103 __git_complete_refs &&
1104 print_comp
1105 ) &&
1106 test_cmp expected out
1107 '
1108
1109 test_expect_success '__git_complete_refs - remote' '
1110 sed -e "s/Z$//" >expected <<-EOF &&
1111 HEAD Z
1112 branch-in-other Z
1113 main-in-other Z
1114 EOF
1115 (
1116 cur= &&
1117 __git_complete_refs --remote=other &&
1118 print_comp
1119 ) &&
1120 test_cmp expected out
1121 '
1122
1123 test_expect_success '__git_complete_refs - track' '
1124 sed -e "s/Z$//" >expected <<-EOF &&
1125 HEAD Z
1126 main Z
1127 matching-branch Z
1128 other/branch-in-other Z
1129 other/main-in-other Z
1130 matching-tag Z
1131 branch-in-other Z
1132 main-in-other Z
1133 EOF
1134 (
1135 cur= &&
1136 __git_complete_refs --track &&
1137 print_comp
1138 ) &&
1139 test_cmp expected out
1140 '
1141
1142 test_expect_success '__git_complete_refs - current word' '
1143 sed -e "s/Z$//" >expected <<-EOF &&
1144 matching-branch Z
1145 matching-tag Z
1146 EOF
1147 (
1148 cur="--option=mat" &&
1149 __git_complete_refs --cur="${cur#*=}" &&
1150 print_comp
1151 ) &&
1152 test_cmp expected out
1153 '
1154
1155 test_expect_success '__git_complete_refs - prefix' '
1156 sed -e "s/Z$//" >expected <<-EOF &&
1157 v1.0..matching-branch Z
1158 v1.0..matching-tag Z
1159 EOF
1160 (
1161 cur=v1.0..mat &&
1162 __git_complete_refs --pfx=v1.0.. --cur=mat &&
1163 print_comp
1164 ) &&
1165 test_cmp expected out
1166 '
1167
1168 test_expect_success '__git_complete_refs - suffix' '
1169 cat >expected <<-EOF &&
1170 HEAD.
1171 main.
1172 matching-branch.
1173 other/branch-in-other.
1174 other/main-in-other.
1175 matching-tag.
1176 EOF
1177 (
1178 cur= &&
1179 __git_complete_refs --sfx=. &&
1180 print_comp
1181 ) &&
1182 test_cmp expected out
1183 '
1184
1185 test_expect_success '__git_complete_fetch_refspecs - simple' '
1186 sed -e "s/Z$//" >expected <<-EOF &&
1187 HEAD:HEAD Z
1188 branch-in-other:branch-in-other Z
1189 main-in-other:main-in-other Z
1190 EOF
1191 (
1192 cur= &&
1193 __git_complete_fetch_refspecs other &&
1194 print_comp
1195 ) &&
1196 test_cmp expected out
1197 '
1198
1199 test_expect_success '__git_complete_fetch_refspecs - matching' '
1200 sed -e "s/Z$//" >expected <<-EOF &&
1201 branch-in-other:branch-in-other Z
1202 EOF
1203 (
1204 cur=br &&
1205 __git_complete_fetch_refspecs other "" br &&
1206 print_comp
1207 ) &&
1208 test_cmp expected out
1209 '
1210
1211 test_expect_success '__git_complete_fetch_refspecs - prefix' '
1212 sed -e "s/Z$//" >expected <<-EOF &&
1213 +HEAD:HEAD Z
1214 +branch-in-other:branch-in-other Z
1215 +main-in-other:main-in-other Z
1216 EOF
1217 (
1218 cur="+" &&
1219 __git_complete_fetch_refspecs other "+" "" &&
1220 print_comp
1221 ) &&
1222 test_cmp expected out
1223 '
1224
1225 test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
1226 sed -e "s/Z$//" >expected <<-EOF &&
1227 refs/heads/branch-in-other:refs/heads/branch-in-other Z
1228 refs/heads/main-in-other:refs/heads/main-in-other Z
1229 refs/tags/tag-in-other:refs/tags/tag-in-other Z
1230 EOF
1231 (
1232 cur=refs/ &&
1233 __git_complete_fetch_refspecs other "" refs/ &&
1234 print_comp
1235 ) &&
1236 test_cmp expected out
1237 '
1238
1239 test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
1240 sed -e "s/Z$//" >expected <<-EOF &&
1241 +refs/heads/branch-in-other:refs/heads/branch-in-other Z
1242 +refs/heads/main-in-other:refs/heads/main-in-other Z
1243 +refs/tags/tag-in-other:refs/tags/tag-in-other Z
1244 EOF
1245 (
1246 cur=+refs/ &&
1247 __git_complete_fetch_refspecs other + refs/ &&
1248 print_comp
1249 ) &&
1250 test_cmp expected out
1251 '
1252
1253 test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
1254 test_completion "git switch " <<-\EOF
1255 branch-in-other Z
1256 main Z
1257 main-in-other Z
1258 matching-branch Z
1259 EOF
1260 '
1261
1262 test_expect_success 'git checkout - completes refs and unique remote branches for DWIM' '
1263 test_completion "git checkout " <<-\EOF
1264 HEAD Z
1265 branch-in-other Z
1266 main Z
1267 main-in-other Z
1268 matching-branch Z
1269 matching-tag Z
1270 other/branch-in-other Z
1271 other/main-in-other Z
1272 EOF
1273 '
1274
1275 test_expect_success 'git switch - with --no-guess, complete only local branches' '
1276 test_completion "git switch --no-guess " <<-\EOF
1277 main Z
1278 matching-branch Z
1279 EOF
1280 '
1281
1282 test_expect_success 'git switch - with GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete only local branches' '
1283 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch " <<-\EOF
1284 main Z
1285 matching-branch Z
1286 EOF
1287 '
1288
1289 test_expect_success 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete local branches and unique remote names for DWIM logic' '
1290 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch --guess " <<-\EOF
1291 branch-in-other Z
1292 main Z
1293 main-in-other Z
1294 matching-branch Z
1295 EOF
1296 '
1297
1298 test_expect_success 'git switch - a later --guess overrides previous --no-guess, complete local and remote unique branches for DWIM' '
1299 test_completion "git switch --no-guess --guess " <<-\EOF
1300 branch-in-other Z
1301 main Z
1302 main-in-other Z
1303 matching-branch Z
1304 EOF
1305 '
1306
1307 test_expect_success 'git switch - a later --no-guess overrides previous --guess, complete only local branches' '
1308 test_completion "git switch --guess --no-guess " <<-\EOF
1309 main Z
1310 matching-branch Z
1311 EOF
1312 '
1313
1314 test_expect_success 'git checkout - with GIT_COMPLETION_NO_GUESS=1 only completes refs' '
1315 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout " <<-\EOF
1316 HEAD Z
1317 main Z
1318 matching-branch Z
1319 matching-tag Z
1320 other/branch-in-other Z
1321 other/main-in-other Z
1322 EOF
1323 '
1324
1325 test_expect_success 'git checkout - --guess overrides GIT_COMPLETION_NO_GUESS=1, complete refs and unique remote branches for DWIM' '
1326 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout --guess " <<-\EOF
1327 HEAD Z
1328 branch-in-other Z
1329 main Z
1330 main-in-other Z
1331 matching-branch Z
1332 matching-tag Z
1333 other/branch-in-other Z
1334 other/main-in-other Z
1335 EOF
1336 '
1337
1338 test_expect_success 'git checkout - with --no-guess, only completes refs' '
1339 test_completion "git checkout --no-guess " <<-\EOF
1340 HEAD Z
1341 main Z
1342 matching-branch Z
1343 matching-tag Z
1344 other/branch-in-other Z
1345 other/main-in-other Z
1346 EOF
1347 '
1348
1349 test_expect_success 'git checkout - a later --guess overrides previous --no-guess, complete refs and unique remote branches for DWIM' '
1350 test_completion "git checkout --no-guess --guess " <<-\EOF
1351 HEAD Z
1352 branch-in-other Z
1353 main Z
1354 main-in-other Z
1355 matching-branch Z
1356 matching-tag Z
1357 other/branch-in-other Z
1358 other/main-in-other Z
1359 EOF
1360 '
1361
1362 test_expect_success 'git checkout - a later --no-guess overrides previous --guess, complete only refs' '
1363 test_completion "git checkout --guess --no-guess " <<-\EOF
1364 HEAD Z
1365 main Z
1366 matching-branch Z
1367 matching-tag Z
1368 other/branch-in-other Z
1369 other/main-in-other Z
1370 EOF
1371 '
1372
1373 test_expect_success 'git checkout - with checkout.guess = false, only completes refs' '
1374 test_config checkout.guess false &&
1375 test_completion "git checkout " <<-\EOF
1376 HEAD Z
1377 main Z
1378 matching-branch Z
1379 matching-tag Z
1380 other/branch-in-other Z
1381 other/main-in-other Z
1382 EOF
1383 '
1384
1385 test_expect_success 'git checkout - with checkout.guess = true, completes refs and unique remote branches for DWIM' '
1386 test_config checkout.guess true &&
1387 test_completion "git checkout " <<-\EOF
1388 HEAD Z
1389 branch-in-other Z
1390 main Z
1391 main-in-other Z
1392 matching-branch Z
1393 matching-tag Z
1394 other/branch-in-other Z
1395 other/main-in-other Z
1396 EOF
1397 '
1398
1399 test_expect_success 'git checkout - a later --guess overrides previous checkout.guess = false, complete refs and unique remote branches for DWIM' '
1400 test_config checkout.guess false &&
1401 test_completion "git checkout --guess " <<-\EOF
1402 HEAD Z
1403 branch-in-other Z
1404 main Z
1405 main-in-other Z
1406 matching-branch Z
1407 matching-tag Z
1408 other/branch-in-other Z
1409 other/main-in-other Z
1410 EOF
1411 '
1412
1413 test_expect_success 'git checkout - a later --no-guess overrides previous checkout.guess = true, complete only refs' '
1414 test_config checkout.guess true &&
1415 test_completion "git checkout --no-guess " <<-\EOF
1416 HEAD Z
1417 main Z
1418 matching-branch Z
1419 matching-tag Z
1420 other/branch-in-other Z
1421 other/main-in-other Z
1422 EOF
1423 '
1424
1425 test_expect_success 'git switch - with --detach, complete all references' '
1426 test_completion "git switch --detach " <<-\EOF
1427 HEAD Z
1428 main Z
1429 matching-branch Z
1430 matching-tag Z
1431 other/branch-in-other Z
1432 other/main-in-other Z
1433 EOF
1434 '
1435
1436 test_expect_success 'git checkout - with --detach, complete only references' '
1437 test_completion "git checkout --detach " <<-\EOF
1438 HEAD Z
1439 main Z
1440 matching-branch Z
1441 matching-tag Z
1442 other/branch-in-other Z
1443 other/main-in-other Z
1444 EOF
1445 '
1446
1447 test_expect_success 'setup sparse-checkout tests' '
1448 # set up sparse-checkout repo
1449 git init sparse-checkout &&
1450 (
1451 cd sparse-checkout &&
1452 mkdir -p folder1/0/1 folder2/0 folder3 &&
1453 touch folder1/0/1/t.txt &&
1454 touch folder2/0/t.txt &&
1455 touch folder3/t.txt &&
1456 git add . &&
1457 git commit -am "Initial commit"
1458 )
1459 '
1460
1461 test_expect_success 'sparse-checkout completes subcommands' '
1462 test_completion "git sparse-checkout " <<-\EOF
1463 list Z
1464 init Z
1465 set Z
1466 add Z
1467 reapply Z
1468 disable Z
1469 EOF
1470 '
1471
1472 test_expect_success 'cone mode sparse-checkout completes directory names' '
1473 # initialize sparse-checkout definitions
1474 git -C sparse-checkout sparse-checkout set --cone folder1/0 folder3 &&
1475
1476 # test tab completion
1477 (
1478 cd sparse-checkout &&
1479 test_completion "git sparse-checkout set f" <<-\EOF
1480 folder1/
1481 folder2/
1482 folder3/
1483 EOF
1484 ) &&
1485
1486 (
1487 cd sparse-checkout &&
1488 test_completion "git sparse-checkout set folder1/" <<-\EOF
1489 folder1/0/
1490 EOF
1491 ) &&
1492
1493 (
1494 cd sparse-checkout &&
1495 test_completion "git sparse-checkout set folder1/0/" <<-\EOF
1496 folder1/0/1/
1497 EOF
1498 ) &&
1499
1500 (
1501 cd sparse-checkout/folder1 &&
1502 test_completion "git sparse-checkout add 0" <<-\EOF
1503 0/
1504 EOF
1505 )
1506 '
1507
1508 test_expect_success 'cone mode sparse-checkout completes directory names with spaces and accents' '
1509 # reset sparse-checkout
1510 git -C sparse-checkout sparse-checkout disable &&
1511 (
1512 cd sparse-checkout &&
1513 mkdir "directory with spaces" &&
1514 mkdir "directory-with-áccent" &&
1515 >"directory with spaces/randomfile" &&
1516 >"directory-with-áccent/randomfile" &&
1517 git add . &&
1518 git commit -m "Add directory with spaces and directory with accent" &&
1519 git sparse-checkout set --cone "directory with spaces" \
1520 "directory-with-áccent" &&
1521 test_completion "git sparse-checkout add dir" <<-\EOF &&
1522 directory with spaces/
1523 directory-with-áccent/
1524 EOF
1525 rm -rf "directory with spaces" &&
1526 rm -rf "directory-with-áccent" &&
1527 git add . &&
1528 git commit -m "Remove directory with spaces and directory with accent"
1529 )
1530 '
1531
1532 # use FUNNYNAMES to avoid running on Windows, which doesn't permit backslashes or tabs in paths
1533 test_expect_success FUNNYNAMES 'cone mode sparse-checkout completes directory names with backslashes and tabs' '
1534 # reset sparse-checkout
1535 git -C sparse-checkout sparse-checkout disable &&
1536 (
1537 cd sparse-checkout &&
1538 mkdir "directory\with\backslashes" &&
1539 mkdir "$(printf "directory\twith\ttabs")" &&
1540 >"directory\with\backslashes/randomfile" &&
1541 >"$(printf "directory\twith\ttabs")/randomfile" &&
1542 git add . &&
1543 git commit -m "Add directory with backslashes and directory with tabs" &&
1544 git sparse-checkout set --cone "directory\with\backslashes" \
1545 "$(printf "directory\twith\ttabs")" &&
1546 test_completion "git sparse-checkout add dir" <<-\EOF &&
1547 directory\with\backslashes/
1548 directory with tabs/
1549 EOF
1550 rm -rf "directory\with\backslashes" &&
1551 rm -rf "$(printf "directory\twith\ttabs")" &&
1552 git add . &&
1553 git commit -m "Remove directory with backslashes and directory with tabs"
1554 )
1555 '
1556
1557 test_expect_success 'non-cone mode sparse-checkout uses bash completion' '
1558 # reset sparse-checkout repo to non-cone mode
1559 git -C sparse-checkout sparse-checkout disable &&
1560 git -C sparse-checkout sparse-checkout set --no-cone &&
1561
1562 (
1563 cd sparse-checkout &&
1564 # expected to be empty since we have not configured
1565 # custom completion for non-cone mode
1566 test_completion "git sparse-checkout set f" <<-\EOF
1567
1568 EOF
1569 )
1570 '
1571
1572 test_expect_success 'git sparse-checkout set --cone completes directory names' '
1573 git -C sparse-checkout sparse-checkout disable &&
1574
1575 (
1576 cd sparse-checkout &&
1577 test_completion "git sparse-checkout set --cone f" <<-\EOF
1578 folder1/
1579 folder2/
1580 folder3/
1581 EOF
1582 )
1583 '
1584
1585 test_expect_success 'git switch - with -d, complete all references' '
1586 test_completion "git switch -d " <<-\EOF
1587 HEAD Z
1588 main Z
1589 matching-branch Z
1590 matching-tag Z
1591 other/branch-in-other Z
1592 other/main-in-other Z
1593 EOF
1594 '
1595
1596 test_expect_success 'git checkout - with -d, complete only references' '
1597 test_completion "git checkout -d " <<-\EOF
1598 HEAD Z
1599 main Z
1600 matching-branch Z
1601 matching-tag Z
1602 other/branch-in-other Z
1603 other/main-in-other Z
1604 EOF
1605 '
1606
1607 test_expect_success 'git switch - with --track, complete only remote branches' '
1608 test_completion "git switch --track " <<-\EOF
1609 other/branch-in-other Z
1610 other/main-in-other Z
1611 EOF
1612 '
1613
1614 test_expect_success 'git checkout - with --track, complete only remote branches' '
1615 test_completion "git checkout --track " <<-\EOF
1616 other/branch-in-other Z
1617 other/main-in-other Z
1618 EOF
1619 '
1620
1621 test_expect_success 'git switch - with --no-track, complete only local branch names' '
1622 test_completion "git switch --no-track " <<-\EOF
1623 main Z
1624 matching-branch Z
1625 EOF
1626 '
1627
1628 test_expect_success 'git checkout - with --no-track, complete only local references' '
1629 test_completion "git checkout --no-track " <<-\EOF
1630 HEAD Z
1631 main Z
1632 matching-branch Z
1633 matching-tag Z
1634 other/branch-in-other Z
1635 other/main-in-other Z
1636 EOF
1637 '
1638
1639 test_expect_success 'git switch - with -c, complete all references' '
1640 test_completion "git switch -c new-branch " <<-\EOF
1641 HEAD Z
1642 main Z
1643 matching-branch Z
1644 matching-tag Z
1645 other/branch-in-other Z
1646 other/main-in-other Z
1647 EOF
1648 '
1649
1650 test_expect_success 'git switch - with -C, complete all references' '
1651 test_completion "git switch -C new-branch " <<-\EOF
1652 HEAD Z
1653 main Z
1654 matching-branch Z
1655 matching-tag Z
1656 other/branch-in-other Z
1657 other/main-in-other Z
1658 EOF
1659 '
1660
1661 test_expect_success 'git switch - with -c and --track, complete all references' '
1662 test_completion "git switch -c new-branch --track " <<-EOF
1663 HEAD Z
1664 main Z
1665 matching-branch Z
1666 matching-tag Z
1667 other/branch-in-other Z
1668 other/main-in-other Z
1669 EOF
1670 '
1671
1672 test_expect_success 'git switch - with -C and --track, complete all references' '
1673 test_completion "git switch -C new-branch --track " <<-EOF
1674 HEAD Z
1675 main Z
1676 matching-branch Z
1677 matching-tag Z
1678 other/branch-in-other Z
1679 other/main-in-other Z
1680 EOF
1681 '
1682
1683 test_expect_success 'git switch - with -c and --no-track, complete all references' '
1684 test_completion "git switch -c new-branch --no-track " <<-\EOF
1685 HEAD Z
1686 main Z
1687 matching-branch Z
1688 matching-tag Z
1689 other/branch-in-other Z
1690 other/main-in-other Z
1691 EOF
1692 '
1693
1694 test_expect_success 'git switch - with -C and --no-track, complete all references' '
1695 test_completion "git switch -C new-branch --no-track " <<-\EOF
1696 HEAD Z
1697 main Z
1698 matching-branch Z
1699 matching-tag Z
1700 other/branch-in-other Z
1701 other/main-in-other Z
1702 EOF
1703 '
1704
1705 test_expect_success 'git checkout - with -b, complete all references' '
1706 test_completion "git checkout -b new-branch " <<-\EOF
1707 HEAD Z
1708 main Z
1709 matching-branch Z
1710 matching-tag Z
1711 other/branch-in-other Z
1712 other/main-in-other Z
1713 EOF
1714 '
1715
1716 test_expect_success 'git checkout - with -B, complete all references' '
1717 test_completion "git checkout -B new-branch " <<-\EOF
1718 HEAD Z
1719 main Z
1720 matching-branch Z
1721 matching-tag Z
1722 other/branch-in-other Z
1723 other/main-in-other Z
1724 EOF
1725 '
1726
1727 test_expect_success 'git checkout - with -b and --track, complete all references' '
1728 test_completion "git checkout -b new-branch --track " <<-EOF
1729 HEAD Z
1730 main Z
1731 matching-branch Z
1732 matching-tag Z
1733 other/branch-in-other Z
1734 other/main-in-other Z
1735 EOF
1736 '
1737
1738 test_expect_success 'git checkout - with -B and --track, complete all references' '
1739 test_completion "git checkout -B new-branch --track " <<-EOF
1740 HEAD Z
1741 main Z
1742 matching-branch Z
1743 matching-tag Z
1744 other/branch-in-other Z
1745 other/main-in-other Z
1746 EOF
1747 '
1748
1749 test_expect_success 'git checkout - with -b and --no-track, complete all references' '
1750 test_completion "git checkout -b new-branch --no-track " <<-\EOF
1751 HEAD Z
1752 main Z
1753 matching-branch Z
1754 matching-tag Z
1755 other/branch-in-other Z
1756 other/main-in-other Z
1757 EOF
1758 '
1759
1760 test_expect_success 'git checkout - with -B and --no-track, complete all references' '
1761 test_completion "git checkout -B new-branch --no-track " <<-\EOF
1762 HEAD Z
1763 main Z
1764 matching-branch Z
1765 matching-tag Z
1766 other/branch-in-other Z
1767 other/main-in-other Z
1768 EOF
1769 '
1770
1771 test_expect_success 'git switch - for -c, complete local branches and unique remote branches' '
1772 test_completion "git switch -c " <<-\EOF
1773 branch-in-other Z
1774 main Z
1775 main-in-other Z
1776 matching-branch Z
1777 EOF
1778 '
1779
1780 test_expect_success 'git switch - for -C, complete local branches and unique remote branches' '
1781 test_completion "git switch -C " <<-\EOF
1782 branch-in-other Z
1783 main Z
1784 main-in-other Z
1785 matching-branch Z
1786 EOF
1787 '
1788
1789 test_expect_success 'git switch - for -c with --no-guess, complete local branches only' '
1790 test_completion "git switch --no-guess -c " <<-\EOF
1791 main Z
1792 matching-branch Z
1793 EOF
1794 '
1795
1796 test_expect_success 'git switch - for -C with --no-guess, complete local branches only' '
1797 test_completion "git switch --no-guess -C " <<-\EOF
1798 main Z
1799 matching-branch Z
1800 EOF
1801 '
1802
1803 test_expect_success 'git switch - for -c with --no-track, complete local branches only' '
1804 test_completion "git switch --no-track -c " <<-\EOF
1805 main Z
1806 matching-branch Z
1807 EOF
1808 '
1809
1810 test_expect_success 'git switch - for -C with --no-track, complete local branches only' '
1811 test_completion "git switch --no-track -C " <<-\EOF
1812 main Z
1813 matching-branch Z
1814 EOF
1815 '
1816
1817 test_expect_success 'git checkout - for -b, complete local branches and unique remote branches' '
1818 test_completion "git checkout -b " <<-\EOF
1819 branch-in-other Z
1820 main Z
1821 main-in-other Z
1822 matching-branch Z
1823 EOF
1824 '
1825
1826 test_expect_success 'git checkout - for -B, complete local branches and unique remote branches' '
1827 test_completion "git checkout -B " <<-\EOF
1828 branch-in-other Z
1829 main Z
1830 main-in-other Z
1831 matching-branch Z
1832 EOF
1833 '
1834
1835 test_expect_success 'git checkout - for -b with --no-guess, complete local branches only' '
1836 test_completion "git checkout --no-guess -b " <<-\EOF
1837 main Z
1838 matching-branch Z
1839 EOF
1840 '
1841
1842 test_expect_success 'git checkout - for -B with --no-guess, complete local branches only' '
1843 test_completion "git checkout --no-guess -B " <<-\EOF
1844 main Z
1845 matching-branch Z
1846 EOF
1847 '
1848
1849 test_expect_success 'git checkout - for -b with --no-track, complete local branches only' '
1850 test_completion "git checkout --no-track -b " <<-\EOF
1851 main Z
1852 matching-branch Z
1853 EOF
1854 '
1855
1856 test_expect_success 'git checkout - for -B with --no-track, complete local branches only' '
1857 test_completion "git checkout --no-track -B " <<-\EOF
1858 main Z
1859 matching-branch Z
1860 EOF
1861 '
1862
1863 test_expect_success 'git switch - with --orphan completes local branch names and unique remote branch names' '
1864 test_completion "git switch --orphan " <<-\EOF
1865 branch-in-other Z
1866 main Z
1867 main-in-other Z
1868 matching-branch Z
1869 EOF
1870 '
1871
1872 test_expect_success 'git switch - --orphan with branch already provided completes nothing else' '
1873 test_completion "git switch --orphan main " <<-\EOF
1874
1875 EOF
1876 '
1877
1878 test_expect_success 'git checkout - with --orphan completes local branch names and unique remote branch names' '
1879 test_completion "git checkout --orphan " <<-\EOF
1880 branch-in-other Z
1881 main Z
1882 main-in-other Z
1883 matching-branch Z
1884 EOF
1885 '
1886
1887 test_expect_success 'git checkout - --orphan with branch already provided completes local refs for a start-point' '
1888 test_completion "git checkout --orphan main " <<-\EOF
1889 HEAD Z
1890 main Z
1891 matching-branch Z
1892 matching-tag Z
1893 other/branch-in-other Z
1894 other/main-in-other Z
1895 EOF
1896 '
1897
1898 test_expect_success 'teardown after ref completion' '
1899 git branch -d matching-branch &&
1900 git tag -d matching-tag &&
1901 git remote remove other
1902 '
1903
1904
1905 test_path_completion ()
1906 {
1907 test $# = 2 || BUG "not 2 parameters to test_path_completion"
1908
1909 local cur="$1" expected="$2"
1910 echo "$expected" >expected &&
1911 (
1912 # In the following tests calling this function we only
1913 # care about how __git_complete_index_file() deals with
1914 # unusual characters in path names. By requesting only
1915 # untracked files we do not have to bother adding any
1916 # paths to the index in those tests.
1917 __git_complete_index_file --others &&
1918 print_comp
1919 ) &&
1920 test_cmp expected out
1921 }
1922
1923 test_expect_success 'setup for path completion tests' '
1924 mkdir simple-dir \
1925 "spaces in dir" \
1926 árvíztűrő &&
1927 touch simple-dir/simple-file \
1928 "spaces in dir/spaces in file" \
1929 "árvíztűrő/Сайн яваарай" &&
1930 if test_have_prereq !MINGW &&
1931 mkdir BS\\dir \
1932 '$'separators\034in\035dir'' &&
1933 touch BS\\dir/DQ\"file \
1934 '$'separators\034in\035dir/sep\036in\037file''
1935 then
1936 test_set_prereq FUNNIERNAMES
1937 else
1938 rm -rf BS\\dir '$'separators\034in\035dir''
1939 fi
1940 '
1941
1942 test_expect_success '__git_complete_index_file - simple' '
1943 test_path_completion simple simple-dir && # Bash is supposed to
1944 # add the trailing /.
1945 test_path_completion simple-dir/simple simple-dir/simple-file
1946 '
1947
1948 test_expect_success \
1949 '__git_complete_index_file - escaped characters on cmdline' '
1950 test_path_completion spac "spaces in dir" && # Bash will turn this
1951 # into "spaces\ in\ dir"
1952 test_path_completion "spaces\\ i" \
1953 "spaces in dir" &&
1954 test_path_completion "spaces\\ in\\ dir/s" \
1955 "spaces in dir/spaces in file" &&
1956 test_path_completion "spaces\\ in\\ dir/spaces\\ i" \
1957 "spaces in dir/spaces in file"
1958 '
1959
1960 test_expect_success \
1961 '__git_complete_index_file - quoted characters on cmdline' '
1962 # Testing with an opening but without a corresponding closing
1963 # double quote is important.
1964 test_path_completion \"spac "spaces in dir" &&
1965 test_path_completion "\"spaces i" \
1966 "spaces in dir" &&
1967 test_path_completion "\"spaces in dir/s" \
1968 "spaces in dir/spaces in file" &&
1969 test_path_completion "\"spaces in dir/spaces i" \
1970 "spaces in dir/spaces in file"
1971 '
1972
1973 test_expect_success '__git_complete_index_file - UTF-8 in ls-files output' '
1974 test_path_completion á árvíztűrő &&
1975 test_path_completion árvíztűrő/С "árvíztűrő/Сайн яваарай"
1976 '
1977
1978 test_expect_success FUNNIERNAMES \
1979 '__git_complete_index_file - C-style escapes in ls-files output' '
1980 test_path_completion BS \
1981 BS\\dir &&
1982 test_path_completion BS\\\\d \
1983 BS\\dir &&
1984 test_path_completion BS\\\\dir/DQ \
1985 BS\\dir/DQ\"file &&
1986 test_path_completion BS\\\\dir/DQ\\\"f \
1987 BS\\dir/DQ\"file
1988 '
1989
1990 test_expect_success FUNNIERNAMES \
1991 '__git_complete_index_file - \nnn-escaped characters in ls-files output' '
1992 test_path_completion sep '$'separators\034in\035dir'' &&
1993 test_path_completion '$'separators\034i'' \
1994 '$'separators\034in\035dir'' &&
1995 test_path_completion '$'separators\034in\035dir/sep'' \
1996 '$'separators\034in\035dir/sep\036in\037file'' &&
1997 test_path_completion '$'separators\034in\035dir/sep\036i'' \
1998 '$'separators\034in\035dir/sep\036in\037file''
1999 '
2000
2001 test_expect_success FUNNYNAMES \
2002 '__git_complete_index_file - removing repeated quoted path components' '
2003 test_when_finished rm -r repeated-quoted &&
2004 mkdir repeated-quoted && # A directory whose name in itself
2005 # would not be quoted ...
2006 >repeated-quoted/0-file &&
2007 >repeated-quoted/1\"file && # ... but here the file makes the
2008 # dirname quoted ...
2009 >repeated-quoted/2-file &&
2010 >repeated-quoted/3\"file && # ... and here, too.
2011
2012 # Still, we shold only list the directory name only once.
2013 test_path_completion repeated repeated-quoted
2014 '
2015
2016 test_expect_success 'teardown after path completion tests' '
2017 rm -rf simple-dir "spaces in dir" árvíztűrő \
2018 BS\\dir '$'separators\034in\035dir''
2019 '
2020
2021 test_expect_success '__git_find_on_cmdline - single match' '
2022 echo list >expect &&
2023 (
2024 words=(git command --opt list) &&
2025 cword=${#words[@]} &&
2026 __git_cmd_idx=1 &&
2027 __git_find_on_cmdline "add list remove" >actual
2028 ) &&
2029 test_cmp expect actual
2030 '
2031
2032 test_expect_success '__git_find_on_cmdline - multiple matches' '
2033 echo remove >expect &&
2034 (
2035 words=(git command -o --opt remove list add) &&
2036 cword=${#words[@]} &&
2037 __git_cmd_idx=1 &&
2038 __git_find_on_cmdline "add list remove" >actual
2039 ) &&
2040 test_cmp expect actual
2041 '
2042
2043 test_expect_success '__git_find_on_cmdline - no match' '
2044 (
2045 words=(git command --opt branch) &&
2046 cword=${#words[@]} &&
2047 __git_cmd_idx=1 &&
2048 __git_find_on_cmdline "add list remove" >actual
2049 ) &&
2050 test_must_be_empty actual
2051 '
2052
2053 test_expect_success '__git_find_on_cmdline - single match with index' '
2054 echo "3 list" >expect &&
2055 (
2056 words=(git command --opt list) &&
2057 cword=${#words[@]} &&
2058 __git_cmd_idx=1 &&
2059 __git_find_on_cmdline --show-idx "add list remove" >actual
2060 ) &&
2061 test_cmp expect actual
2062 '
2063
2064 test_expect_success '__git_find_on_cmdline - multiple matches with index' '
2065 echo "4 remove" >expect &&
2066 (
2067 words=(git command -o --opt remove list add) &&
2068 cword=${#words[@]} &&
2069 __git_cmd_idx=1 &&
2070 __git_find_on_cmdline --show-idx "add list remove" >actual
2071 ) &&
2072 test_cmp expect actual
2073 '
2074
2075 test_expect_success '__git_find_on_cmdline - no match with index' '
2076 (
2077 words=(git command --opt branch) &&
2078 cword=${#words[@]} &&
2079 __git_cmd_idx=1 &&
2080 __git_find_on_cmdline --show-idx "add list remove" >actual
2081 ) &&
2082 test_must_be_empty actual
2083 '
2084
2085 test_expect_success '__git_find_on_cmdline - ignores matches before command with index' '
2086 echo "6 remove" >expect &&
2087 (
2088 words=(git -C remove command -o --opt remove list add) &&
2089 cword=${#words[@]} &&
2090 __git_cmd_idx=3 &&
2091 __git_find_on_cmdline --show-idx "add list remove" >actual
2092 ) &&
2093 test_cmp expect actual
2094 '
2095
2096 test_expect_success '__git_get_config_variables' '
2097 cat >expect <<-EOF &&
2098 name-1
2099 name-2
2100 EOF
2101 test_config interesting.name-1 good &&
2102 test_config interesting.name-2 good &&
2103 test_config subsection.interesting.name-3 bad &&
2104 __git_get_config_variables interesting >actual &&
2105 test_cmp expect actual
2106 '
2107
2108 test_expect_success '__git_pretty_aliases' '
2109 cat >expect <<-EOF &&
2110 author
2111 hash
2112 EOF
2113 test_config pretty.author "%an %ae" &&
2114 test_config pretty.hash %H &&
2115 __git_pretty_aliases >actual &&
2116 test_cmp expect actual
2117 '
2118
2119 test_expect_success 'basic' '
2120 run_completion "git " &&
2121 # built-in
2122 grep -q "^add \$" out &&
2123 # script
2124 grep -q "^rebase \$" out &&
2125 # plumbing
2126 ! grep -q "^ls-files \$" out &&
2127
2128 run_completion "git r" &&
2129 ! grep -q -v "^r" out
2130 '
2131
2132 test_expect_success 'double dash "git" itself' '
2133 test_completion "git --" <<-\EOF
2134 --paginate Z
2135 --no-pager Z
2136 --git-dir=
2137 --bare Z
2138 --version Z
2139 --exec-path Z
2140 --exec-path=
2141 --html-path Z
2142 --man-path Z
2143 --info-path Z
2144 --work-tree=
2145 --namespace=
2146 --no-replace-objects Z
2147 --help Z
2148 EOF
2149 '
2150
2151 test_expect_success 'double dash "git checkout"' '
2152 test_completion "git checkout --" <<-\EOF
2153 --quiet Z
2154 --detach Z
2155 --track Z
2156 --orphan=Z
2157 --ours Z
2158 --theirs Z
2159 --merge Z
2160 --conflict=Z
2161 --patch Z
2162 --ignore-skip-worktree-bits Z
2163 --ignore-other-worktrees Z
2164 --recurse-submodules Z
2165 --progress Z
2166 --guess Z
2167 --no-guess Z
2168 --no-... Z
2169 --overlay Z
2170 --pathspec-file-nul Z
2171 --pathspec-from-file=Z
2172 EOF
2173 '
2174
2175 test_expect_success 'general options' '
2176 test_completion "git --ver" "--version " &&
2177 test_completion "git --hel" "--help " &&
2178 test_completion "git --exe" <<-\EOF &&
2179 --exec-path Z
2180 --exec-path=
2181 EOF
2182 test_completion "git --htm" "--html-path " &&
2183 test_completion "git --pag" "--paginate " &&
2184 test_completion "git --no-p" "--no-pager " &&
2185 test_completion "git --git" "--git-dir=" &&
2186 test_completion "git --wor" "--work-tree=" &&
2187 test_completion "git --nam" "--namespace=" &&
2188 test_completion "git --bar" "--bare " &&
2189 test_completion "git --inf" "--info-path " &&
2190 test_completion "git --no-r" "--no-replace-objects "
2191 '
2192
2193 test_expect_success 'general options plus command' '
2194 test_completion "git --version check" "checkout " &&
2195 test_completion "git --paginate check" "checkout " &&
2196 test_completion "git --git-dir=foo check" "checkout " &&
2197 test_completion "git --bare check" "checkout " &&
2198 test_completion "git --exec-path=foo check" "checkout " &&
2199 test_completion "git --html-path check" "checkout " &&
2200 test_completion "git --no-pager check" "checkout " &&
2201 test_completion "git --work-tree=foo check" "checkout " &&
2202 test_completion "git --namespace=foo check" "checkout " &&
2203 test_completion "git --paginate check" "checkout " &&
2204 test_completion "git --info-path check" "checkout " &&
2205 test_completion "git --no-replace-objects check" "checkout " &&
2206 test_completion "git --git-dir some/path check" "checkout " &&
2207 test_completion "git -c conf.var=value check" "checkout " &&
2208 test_completion "git -C some/path check" "checkout " &&
2209 test_completion "git --work-tree some/path check" "checkout " &&
2210 test_completion "git --namespace name/space check" "checkout "
2211 '
2212
2213 test_expect_success 'git --help completion' '
2214 test_completion "git --help ad" "add " &&
2215 test_completion "git --help core" "core-tutorial "
2216 '
2217
2218 test_expect_success 'completion.commands removes multiple commands' '
2219 test_config completion.commands "-cherry -mergetool" &&
2220 git --list-cmds=list-mainporcelain,list-complete,config >out &&
2221 ! grep -E "^(cherry|mergetool)$" out
2222 '
2223
2224 test_expect_success 'setup for integration tests' '
2225 echo content >file1 &&
2226 echo more >file2 &&
2227 git add file1 file2 &&
2228 git commit -m one &&
2229 git branch mybranch &&
2230 git tag mytag
2231 '
2232
2233 test_expect_success 'checkout completes ref names' '
2234 test_completion "git checkout m" <<-\EOF
2235 main Z
2236 mybranch Z
2237 mytag Z
2238 EOF
2239 '
2240
2241 test_expect_success 'git -C <path> checkout uses the right repo' '
2242 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
2243 branch-in-other Z
2244 EOF
2245 '
2246
2247 test_expect_success 'show completes all refs' '
2248 test_completion "git show m" <<-\EOF
2249 main Z
2250 mybranch Z
2251 mytag Z
2252 EOF
2253 '
2254
2255 test_expect_success '<ref>: completes paths' '
2256 test_completion "git show mytag:f" <<-\EOF
2257 file1Z
2258 file2Z
2259 EOF
2260 '
2261
2262 test_expect_success 'complete tree filename with spaces' '
2263 echo content >"name with spaces" &&
2264 git add "name with spaces" &&
2265 git commit -m spaces &&
2266 test_completion "git show HEAD:nam" <<-\EOF
2267 name with spacesZ
2268 EOF
2269 '
2270
2271 test_expect_success 'complete tree filename with metacharacters' '
2272 echo content >"name with \${meta}" &&
2273 git add "name with \${meta}" &&
2274 git commit -m meta &&
2275 test_completion "git show HEAD:nam" <<-\EOF
2276 name with ${meta}Z
2277 name with spacesZ
2278 EOF
2279 '
2280
2281 test_expect_success PERL 'send-email' '
2282 test_completion "git send-email --cov" <<-\EOF &&
2283 --cover-from-description=Z
2284 --cover-letter Z
2285 EOF
2286 test_completion "git send-email --val" <<-\EOF &&
2287 --validate Z
2288 EOF
2289 test_completion "git send-email ma" "main "
2290 '
2291
2292 test_expect_success 'complete files' '
2293 git init tmp && cd tmp &&
2294 test_when_finished "cd .. && rm -rf tmp" &&
2295
2296 echo "expected" > .gitignore &&
2297 echo "out" >> .gitignore &&
2298 echo "out_sorted" >> .gitignore &&
2299
2300 git add .gitignore &&
2301 test_completion "git commit " ".gitignore" &&
2302
2303 git commit -m ignore &&
2304
2305 touch new &&
2306 test_completion "git add " "new" &&
2307
2308 git add new &&
2309 git commit -a -m new &&
2310 test_completion "git add " "" &&
2311
2312 git mv new modified &&
2313 echo modify > modified &&
2314 test_completion "git add " "modified" &&
2315
2316 mkdir -p some/deep &&
2317 touch some/deep/path &&
2318 test_completion "git add some/" "some/deep" &&
2319 git clean -f some &&
2320
2321 touch untracked &&
2322
2323 : TODO .gitignore should not be here &&
2324 test_completion "git rm " <<-\EOF &&
2325 .gitignore
2326 modified
2327 EOF
2328
2329 test_completion "git clean " "untracked" &&
2330
2331 : TODO .gitignore should not be here &&
2332 test_completion "git mv " <<-\EOF &&
2333 .gitignore
2334 modified
2335 EOF
2336
2337 mkdir dir &&
2338 touch dir/file-in-dir &&
2339 git add dir/file-in-dir &&
2340 git commit -m dir &&
2341
2342 mkdir untracked-dir &&
2343
2344 : TODO .gitignore should not be here &&
2345 test_completion "git mv modified " <<-\EOF &&
2346 .gitignore
2347 dir
2348 modified
2349 untracked
2350 untracked-dir
2351 EOF
2352
2353 test_completion "git commit " "modified" &&
2354
2355 : TODO .gitignore should not be here &&
2356 test_completion "git ls-files " <<-\EOF &&
2357 .gitignore
2358 dir
2359 modified
2360 EOF
2361
2362 touch momified &&
2363 test_completion "git add mom" "momified"
2364 '
2365
2366 test_expect_success "simple alias" '
2367 test_config alias.co checkout &&
2368 test_completion "git co m" <<-\EOF
2369 main Z
2370 mybranch Z
2371 mytag Z
2372 EOF
2373 '
2374
2375 test_expect_success "recursive alias" '
2376 test_config alias.co checkout &&
2377 test_config alias.cod "co --detached" &&
2378 test_completion "git cod m" <<-\EOF
2379 main Z
2380 mybranch Z
2381 mytag Z
2382 EOF
2383 '
2384
2385 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
2386 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
2387 test_completion "git co m" <<-\EOF
2388 main Z
2389 mybranch Z
2390 mytag Z
2391 EOF
2392 '
2393
2394 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
2395 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
2396 test_completion "git co m" <<-\EOF
2397 main Z
2398 mybranch Z
2399 mytag Z
2400 EOF
2401 '
2402
2403 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
2404 test_config alias.co "!f() { : git checkout ; if ... } f" &&
2405 test_completion "git co m" <<-\EOF
2406 main Z
2407 mybranch Z
2408 mytag Z
2409 EOF
2410 '
2411
2412 test_expect_success 'completion without explicit _git_xxx function' '
2413 test_completion "git version --" <<-\EOF
2414 --build-options Z
2415 --no-build-options Z
2416 EOF
2417 '
2418
2419 test_expect_failure 'complete with tilde expansion' '
2420 git init tmp && cd tmp &&
2421 test_when_finished "cd .. && rm -rf tmp" &&
2422
2423 touch ~/tmp/file &&
2424
2425 test_completion "git add ~/tmp/" "~/tmp/file"
2426 '
2427
2428 test_expect_success 'setup other remote for remote reference completion' '
2429 git remote add other otherrepo &&
2430 git fetch other
2431 '
2432
2433 for flag in -d --delete
2434 do
2435 test_expect_success "__git_complete_remote_or_refspec - push $flag other" '
2436 sed -e "s/Z$//" >expected <<-EOF &&
2437 main-in-other Z
2438 EOF
2439 (
2440 words=(git push '$flag' other ma) &&
2441 cword=${#words[@]} cur=${words[cword-1]} &&
2442 __git_cmd_idx=1 &&
2443 __git_complete_remote_or_refspec &&
2444 print_comp
2445 ) &&
2446 test_cmp expected out
2447 '
2448
2449 test_expect_failure "__git_complete_remote_or_refspec - push other $flag" '
2450 sed -e "s/Z$//" >expected <<-EOF &&
2451 main-in-other Z
2452 EOF
2453 (
2454 words=(git push other '$flag' ma) &&
2455 cword=${#words[@]} cur=${words[cword-1]} &&
2456 __git_cmd_idx=1 &&
2457 __git_complete_remote_or_refspec &&
2458 print_comp
2459 ) &&
2460 test_cmp expected out
2461 '
2462 done
2463
2464 test_expect_success 'git config - section' '
2465 test_completion "git config br" <<-\EOF
2466 branch.Z
2467 browser.Z
2468 EOF
2469 '
2470
2471 test_expect_success 'git config - variable name' '
2472 test_completion "git config log.d" <<-\EOF
2473 log.date Z
2474 log.decorate Z
2475 log.diffMerges Z
2476 EOF
2477 '
2478
2479 test_expect_success 'git config - value' '
2480 test_completion "git config color.pager " <<-\EOF
2481 false Z
2482 true Z
2483 EOF
2484 '
2485
2486 test_expect_success 'git -c - section' '
2487 test_completion "git -c br" <<-\EOF
2488 branch.Z
2489 browser.Z
2490 EOF
2491 '
2492
2493 test_expect_success 'git -c - variable name' '
2494 test_completion "git -c log.d" <<-\EOF
2495 log.date=Z
2496 log.decorate=Z
2497 log.diffMerges=Z
2498 EOF
2499 '
2500
2501 test_expect_success 'git -c - value' '
2502 test_completion "git -c color.pager=" <<-\EOF
2503 false Z
2504 true Z
2505 EOF
2506 '
2507
2508 test_expect_success 'git clone --config= - section' '
2509 test_completion "git clone --config=br" <<-\EOF
2510 branch.Z
2511 browser.Z
2512 EOF
2513 '
2514
2515 test_expect_success 'git clone --config= - variable name' '
2516 test_completion "git clone --config=log.d" <<-\EOF
2517 log.date=Z
2518 log.decorate=Z
2519 log.diffMerges=Z
2520 EOF
2521 '
2522
2523 test_expect_success 'git clone --config= - value' '
2524 test_completion "git clone --config=color.pager=" <<-\EOF
2525 false Z
2526 true Z
2527 EOF
2528 '
2529
2530 test_expect_success 'options with value' '
2531 test_completion "git merge -X diff-algorithm=" <<-\EOF
2532
2533 EOF
2534 '
2535
2536 test_expect_success 'sourcing the completion script clears cached commands' '
2537 __git_compute_all_commands &&
2538 verbose test -n "$__git_all_commands" &&
2539 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2540 verbose test -z "$__git_all_commands"
2541 '
2542
2543 test_expect_success 'sourcing the completion script clears cached merge strategies' '
2544 __git_compute_merge_strategies &&
2545 verbose test -n "$__git_merge_strategies" &&
2546 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2547 verbose test -z "$__git_merge_strategies"
2548 '
2549
2550 test_expect_success 'sourcing the completion script clears cached --options' '
2551 __gitcomp_builtin checkout &&
2552 verbose test -n "$__gitcomp_builtin_checkout" &&
2553 __gitcomp_builtin notes_edit &&
2554 verbose test -n "$__gitcomp_builtin_notes_edit" &&
2555 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2556 verbose test -z "$__gitcomp_builtin_checkout" &&
2557 verbose test -z "$__gitcomp_builtin_notes_edit"
2558 '
2559
2560 test_expect_success 'option aliases are not shown by default' '
2561 test_completion "git clone --recurs" "--recurse-submodules "
2562 '
2563
2564 test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
2565 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2566 GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2567 test_completion "git clone --recurs" <<-\EOF
2568 --recurse-submodules Z
2569 --recursive Z
2570 EOF
2571 '
2572
2573 test_expect_success '__git_complete' '
2574 unset -f __git_wrap__git_main &&
2575
2576 __git_complete foo __git_main &&
2577 __git_have_func __git_wrap__git_main &&
2578 unset -f __git_wrap__git_main &&
2579
2580 __git_complete gf _git_fetch &&
2581 __git_have_func __git_wrap_git_fetch &&
2582
2583 __git_complete foo git &&
2584 __git_have_func __git_wrap__git_main &&
2585 unset -f __git_wrap__git_main &&
2586
2587 __git_complete gd git_diff &&
2588 __git_have_func __git_wrap_git_diff &&
2589
2590 test_must_fail __git_complete ga missing
2591 '
2592
2593 test_done