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