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