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