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