Imported Upstream version 2.18.0
[platform/upstream/git.git] / t / t9902-completion.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012 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 #     "filter-branch" and "ls-files" are listed for this.
32
33 GIT_TESTING_ALL_COMMAND_LIST='add checkout check-attr filter-branch ls-files'
34 GIT_TESTING_PORCELAIN_COMMAND_LIST='add checkout filter-branch'
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 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 cant 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 pu" "branch." \
498                 "ma" "." <<-\EOF
499         branch.master.Z
500         branch.maint.Z
501         EOF
502 '
503
504 test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
505         __gitcomp "$invalid_variable_name"
506 '
507
508 read -r -d "" refs <<-\EOF
509 maint
510 master
511 next
512 pu
513 EOF
514
515 test_expect_success '__gitcomp_nl - trailing space' '
516         test_gitcomp_nl "m" "$refs" <<-EOF
517         maint Z
518         master Z
519         EOF
520 '
521
522 test_expect_success '__gitcomp_nl - prefix' '
523         test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
524         --fixup=maint Z
525         --fixup=master Z
526         EOF
527 '
528
529 test_expect_success '__gitcomp_nl - suffix' '
530         test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
531         branch.maint.Z
532         branch.master.Z
533         EOF
534 '
535
536 test_expect_success '__gitcomp_nl - no suffix' '
537         test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
538         maintZ
539         masterZ
540         EOF
541 '
542
543 test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
544         __gitcomp_nl "$invalid_variable_name"
545 '
546
547 test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
548         cat >expect <<-EOF &&
549         remote_from_file_1
550         remote_from_file_2
551         remote_in_config_1
552         remote_in_config_2
553         EOF
554         test_when_finished "rm -rf .git/remotes" &&
555         mkdir -p .git/remotes &&
556         >.git/remotes/remote_from_file_1 &&
557         >.git/remotes/remote_from_file_2 &&
558         test_when_finished "git remote remove remote_in_config_1" &&
559         git remote add remote_in_config_1 git://remote_1 &&
560         test_when_finished "git remote remove remote_in_config_2" &&
561         git remote add remote_in_config_2 git://remote_2 &&
562         (
563                 __git_remotes >actual
564         ) &&
565         test_cmp expect actual
566 '
567
568 test_expect_success '__git_is_configured_remote' '
569         test_when_finished "git remote remove remote_1" &&
570         git remote add remote_1 git://remote_1 &&
571         test_when_finished "git remote remove remote_2" &&
572         git remote add remote_2 git://remote_2 &&
573         (
574                 verbose __git_is_configured_remote remote_2 &&
575                 test_must_fail __git_is_configured_remote non-existent
576         )
577 '
578
579 test_expect_success 'setup for ref completion' '
580         git commit --allow-empty -m initial &&
581         git branch matching-branch &&
582         git tag matching-tag &&
583         (
584                 cd otherrepo &&
585                 git commit --allow-empty -m initial &&
586                 git branch -m master master-in-other &&
587                 git branch branch-in-other &&
588                 git tag tag-in-other
589         ) &&
590         git remote add other "$ROOT/otherrepo/.git" &&
591         git fetch --no-tags other &&
592         rm -f .git/FETCH_HEAD &&
593         git init thirdrepo
594 '
595
596 test_expect_success '__git_refs - simple' '
597         cat >expected <<-EOF &&
598         HEAD
599         master
600         matching-branch
601         other/branch-in-other
602         other/master-in-other
603         matching-tag
604         EOF
605         (
606                 cur= &&
607                 __git_refs >"$actual"
608         ) &&
609         test_cmp expected "$actual"
610 '
611
612 test_expect_success '__git_refs - full refs' '
613         cat >expected <<-EOF &&
614         refs/heads/master
615         refs/heads/matching-branch
616         refs/remotes/other/branch-in-other
617         refs/remotes/other/master-in-other
618         refs/tags/matching-tag
619         EOF
620         (
621                 cur=refs/heads/ &&
622                 __git_refs >"$actual"
623         ) &&
624         test_cmp expected "$actual"
625 '
626
627 test_expect_success '__git_refs - repo given on the command line' '
628         cat >expected <<-EOF &&
629         HEAD
630         branch-in-other
631         master-in-other
632         tag-in-other
633         EOF
634         (
635                 __git_dir="$ROOT/otherrepo/.git" &&
636                 cur= &&
637                 __git_refs >"$actual"
638         ) &&
639         test_cmp expected "$actual"
640 '
641
642 test_expect_success '__git_refs - remote on local file system' '
643         cat >expected <<-EOF &&
644         HEAD
645         branch-in-other
646         master-in-other
647         tag-in-other
648         EOF
649         (
650                 cur= &&
651                 __git_refs otherrepo >"$actual"
652         ) &&
653         test_cmp expected "$actual"
654 '
655
656 test_expect_success '__git_refs - remote on local file system - full refs' '
657         cat >expected <<-EOF &&
658         refs/heads/branch-in-other
659         refs/heads/master-in-other
660         refs/tags/tag-in-other
661         EOF
662         (
663                 cur=refs/ &&
664                 __git_refs otherrepo >"$actual"
665         ) &&
666         test_cmp expected "$actual"
667 '
668
669 test_expect_success '__git_refs - configured remote' '
670         cat >expected <<-EOF &&
671         HEAD
672         branch-in-other
673         master-in-other
674         EOF
675         (
676                 cur= &&
677                 __git_refs other >"$actual"
678         ) &&
679         test_cmp expected "$actual"
680 '
681
682 test_expect_success '__git_refs - configured remote - full refs' '
683         cat >expected <<-EOF &&
684         HEAD
685         refs/heads/branch-in-other
686         refs/heads/master-in-other
687         refs/tags/tag-in-other
688         EOF
689         (
690                 cur=refs/ &&
691                 __git_refs other >"$actual"
692         ) &&
693         test_cmp expected "$actual"
694 '
695
696 test_expect_success '__git_refs - configured remote - repo given on the command line' '
697         cat >expected <<-EOF &&
698         HEAD
699         branch-in-other
700         master-in-other
701         EOF
702         (
703                 cd thirdrepo &&
704                 __git_dir="$ROOT/.git" &&
705                 cur= &&
706                 __git_refs other >"$actual"
707         ) &&
708         test_cmp expected "$actual"
709 '
710
711 test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
712         cat >expected <<-EOF &&
713         HEAD
714         refs/heads/branch-in-other
715         refs/heads/master-in-other
716         refs/tags/tag-in-other
717         EOF
718         (
719                 cd thirdrepo &&
720                 __git_dir="$ROOT/.git" &&
721                 cur=refs/ &&
722                 __git_refs other >"$actual"
723         ) &&
724         test_cmp expected "$actual"
725 '
726
727 test_expect_success '__git_refs - configured remote - remote name matches a directory' '
728         cat >expected <<-EOF &&
729         HEAD
730         branch-in-other
731         master-in-other
732         EOF
733         mkdir other &&
734         test_when_finished "rm -rf other" &&
735         (
736                 cur= &&
737                 __git_refs other >"$actual"
738         ) &&
739         test_cmp expected "$actual"
740 '
741
742 test_expect_success '__git_refs - URL remote' '
743         cat >expected <<-EOF &&
744         HEAD
745         branch-in-other
746         master-in-other
747         tag-in-other
748         EOF
749         (
750                 cur= &&
751                 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
752         ) &&
753         test_cmp expected "$actual"
754 '
755
756 test_expect_success '__git_refs - URL remote - full refs' '
757         cat >expected <<-EOF &&
758         HEAD
759         refs/heads/branch-in-other
760         refs/heads/master-in-other
761         refs/tags/tag-in-other
762         EOF
763         (
764                 cur=refs/ &&
765                 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
766         ) &&
767         test_cmp expected "$actual"
768 '
769
770 test_expect_success '__git_refs - non-existing remote' '
771         (
772                 cur= &&
773                 __git_refs non-existing >"$actual"
774         ) &&
775         test_must_be_empty "$actual"
776 '
777
778 test_expect_success '__git_refs - non-existing remote - full refs' '
779         (
780                 cur=refs/ &&
781                 __git_refs non-existing >"$actual"
782         ) &&
783         test_must_be_empty "$actual"
784 '
785
786 test_expect_success '__git_refs - non-existing URL remote' '
787         (
788                 cur= &&
789                 __git_refs "file://$ROOT/non-existing" >"$actual"
790         ) &&
791         test_must_be_empty "$actual"
792 '
793
794 test_expect_success '__git_refs - non-existing URL remote - full refs' '
795         (
796                 cur=refs/ &&
797                 __git_refs "file://$ROOT/non-existing" >"$actual"
798         ) &&
799         test_must_be_empty "$actual"
800 '
801
802 test_expect_success '__git_refs - not in a git repository' '
803         (
804                 GIT_CEILING_DIRECTORIES="$ROOT" &&
805                 export GIT_CEILING_DIRECTORIES &&
806                 cd subdir &&
807                 cur= &&
808                 __git_refs >"$actual"
809         ) &&
810         test_must_be_empty "$actual"
811 '
812
813 test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
814         cat >expected <<-EOF &&
815         HEAD
816         master
817         matching-branch
818         other/ambiguous
819         other/branch-in-other
820         other/master-in-other
821         remote/ambiguous
822         remote/branch-in-remote
823         matching-tag
824         branch-in-other
825         branch-in-remote
826         master-in-other
827         EOF
828         for remote_ref in refs/remotes/other/ambiguous \
829                 refs/remotes/remote/ambiguous \
830                 refs/remotes/remote/branch-in-remote
831         do
832                 git update-ref $remote_ref master &&
833                 test_when_finished "git update-ref -d $remote_ref"
834         done &&
835         (
836                 cur= &&
837                 __git_refs "" 1 >"$actual"
838         ) &&
839         test_cmp expected "$actual"
840 '
841
842 test_expect_success '__git_refs - after --opt=' '
843         cat >expected <<-EOF &&
844         HEAD
845         master
846         matching-branch
847         other/branch-in-other
848         other/master-in-other
849         matching-tag
850         EOF
851         (
852                 cur="--opt=" &&
853                 __git_refs "" "" "" "" >"$actual"
854         ) &&
855         test_cmp expected "$actual"
856 '
857
858 test_expect_success '__git_refs - after --opt= - full refs' '
859         cat >expected <<-EOF &&
860         refs/heads/master
861         refs/heads/matching-branch
862         refs/remotes/other/branch-in-other
863         refs/remotes/other/master-in-other
864         refs/tags/matching-tag
865         EOF
866         (
867                 cur="--opt=refs/" &&
868                 __git_refs "" "" "" refs/ >"$actual"
869         ) &&
870         test_cmp expected "$actual"
871 '
872
873 test_expect_success '__git refs - exluding refs' '
874         cat >expected <<-EOF &&
875         ^HEAD
876         ^master
877         ^matching-branch
878         ^other/branch-in-other
879         ^other/master-in-other
880         ^matching-tag
881         EOF
882         (
883                 cur=^ &&
884                 __git_refs >"$actual"
885         ) &&
886         test_cmp expected "$actual"
887 '
888
889 test_expect_success '__git refs - exluding full refs' '
890         cat >expected <<-EOF &&
891         ^refs/heads/master
892         ^refs/heads/matching-branch
893         ^refs/remotes/other/branch-in-other
894         ^refs/remotes/other/master-in-other
895         ^refs/tags/matching-tag
896         EOF
897         (
898                 cur=^refs/ &&
899                 __git_refs >"$actual"
900         ) &&
901         test_cmp expected "$actual"
902 '
903
904 test_expect_success 'setup for filtering matching refs' '
905         git branch matching/branch &&
906         git tag matching/tag &&
907         git -C otherrepo branch matching/branch-in-other &&
908         git fetch --no-tags other &&
909         rm -f .git/FETCH_HEAD
910 '
911
912 test_expect_success '__git_refs - dont filter refs unless told so' '
913         cat >expected <<-EOF &&
914         HEAD
915         master
916         matching-branch
917         matching/branch
918         other/branch-in-other
919         other/master-in-other
920         other/matching/branch-in-other
921         matching-tag
922         matching/tag
923         EOF
924         (
925                 cur=master &&
926                 __git_refs >"$actual"
927         ) &&
928         test_cmp expected "$actual"
929 '
930
931 test_expect_success '__git_refs - only matching refs' '
932         cat >expected <<-EOF &&
933         matching-branch
934         matching/branch
935         matching-tag
936         matching/tag
937         EOF
938         (
939                 cur=mat &&
940                 __git_refs "" "" "" "$cur" >"$actual"
941         ) &&
942         test_cmp expected "$actual"
943 '
944
945 test_expect_success '__git_refs - only matching refs - full refs' '
946         cat >expected <<-EOF &&
947         refs/heads/matching-branch
948         refs/heads/matching/branch
949         EOF
950         (
951                 cur=refs/heads/mat &&
952                 __git_refs "" "" "" "$cur" >"$actual"
953         ) &&
954         test_cmp expected "$actual"
955 '
956
957 test_expect_success '__git_refs - only matching refs - remote on local file system' '
958         cat >expected <<-EOF &&
959         master-in-other
960         matching/branch-in-other
961         EOF
962         (
963                 cur=ma &&
964                 __git_refs otherrepo "" "" "$cur" >"$actual"
965         ) &&
966         test_cmp expected "$actual"
967 '
968
969 test_expect_success '__git_refs - only matching refs - configured remote' '
970         cat >expected <<-EOF &&
971         master-in-other
972         matching/branch-in-other
973         EOF
974         (
975                 cur=ma &&
976                 __git_refs other "" "" "$cur" >"$actual"
977         ) &&
978         test_cmp expected "$actual"
979 '
980
981 test_expect_success '__git_refs - only matching refs - remote - full refs' '
982         cat >expected <<-EOF &&
983         refs/heads/master-in-other
984         refs/heads/matching/branch-in-other
985         EOF
986         (
987                 cur=refs/heads/ma &&
988                 __git_refs other "" "" "$cur" >"$actual"
989         ) &&
990         test_cmp expected "$actual"
991 '
992
993 test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
994         cat >expected <<-EOF &&
995         matching-branch
996         matching/branch
997         matching-tag
998         matching/tag
999         matching/branch-in-other
1000         EOF
1001         for remote_ref in refs/remotes/other/ambiguous \
1002                 refs/remotes/remote/ambiguous \
1003                 refs/remotes/remote/branch-in-remote
1004         do
1005                 git update-ref $remote_ref master &&
1006                 test_when_finished "git update-ref -d $remote_ref"
1007         done &&
1008         (
1009                 cur=mat &&
1010                 __git_refs "" 1 "" "$cur" >"$actual"
1011         ) &&
1012         test_cmp expected "$actual"
1013 '
1014
1015 test_expect_success 'teardown after filtering matching refs' '
1016         git branch -d matching/branch &&
1017         git tag -d matching/tag &&
1018         git update-ref -d refs/remotes/other/matching/branch-in-other &&
1019         git -C otherrepo branch -D matching/branch-in-other
1020 '
1021
1022 test_expect_success '__git_refs - for-each-ref format specifiers in prefix' '
1023         cat >expected <<-EOF &&
1024         evil-%%-%42-%(refname)..master
1025         EOF
1026         (
1027                 cur="evil-%%-%42-%(refname)..mas" &&
1028                 __git_refs "" "" "evil-%%-%42-%(refname).." mas >"$actual"
1029         ) &&
1030         test_cmp expected "$actual"
1031 '
1032
1033 test_expect_success '__git_complete_refs - simple' '
1034         sed -e "s/Z$//" >expected <<-EOF &&
1035         HEAD Z
1036         master Z
1037         matching-branch Z
1038         other/branch-in-other Z
1039         other/master-in-other Z
1040         matching-tag Z
1041         EOF
1042         (
1043                 cur= &&
1044                 __git_complete_refs &&
1045                 print_comp
1046         ) &&
1047         test_cmp expected out
1048 '
1049
1050 test_expect_success '__git_complete_refs - matching' '
1051         sed -e "s/Z$//" >expected <<-EOF &&
1052         matching-branch Z
1053         matching-tag Z
1054         EOF
1055         (
1056                 cur=mat &&
1057                 __git_complete_refs &&
1058                 print_comp
1059         ) &&
1060         test_cmp expected out
1061 '
1062
1063 test_expect_success '__git_complete_refs - remote' '
1064         sed -e "s/Z$//" >expected <<-EOF &&
1065         HEAD Z
1066         branch-in-other Z
1067         master-in-other Z
1068         EOF
1069         (
1070                 cur=
1071                 __git_complete_refs --remote=other &&
1072                 print_comp
1073         ) &&
1074         test_cmp expected out
1075 '
1076
1077 test_expect_success '__git_complete_refs - track' '
1078         sed -e "s/Z$//" >expected <<-EOF &&
1079         HEAD Z
1080         master Z
1081         matching-branch Z
1082         other/branch-in-other Z
1083         other/master-in-other Z
1084         matching-tag Z
1085         branch-in-other Z
1086         master-in-other Z
1087         EOF
1088         (
1089                 cur=
1090                 __git_complete_refs --track &&
1091                 print_comp
1092         ) &&
1093         test_cmp expected out
1094 '
1095
1096 test_expect_success '__git_complete_refs - current word' '
1097         sed -e "s/Z$//" >expected <<-EOF &&
1098         matching-branch Z
1099         matching-tag Z
1100         EOF
1101         (
1102                 cur="--option=mat" &&
1103                 __git_complete_refs --cur="${cur#*=}" &&
1104                 print_comp
1105         ) &&
1106         test_cmp expected out
1107 '
1108
1109 test_expect_success '__git_complete_refs - prefix' '
1110         sed -e "s/Z$//" >expected <<-EOF &&
1111         v1.0..matching-branch Z
1112         v1.0..matching-tag Z
1113         EOF
1114         (
1115                 cur=v1.0..mat &&
1116                 __git_complete_refs --pfx=v1.0.. --cur=mat &&
1117                 print_comp
1118         ) &&
1119         test_cmp expected out
1120 '
1121
1122 test_expect_success '__git_complete_refs - suffix' '
1123         cat >expected <<-EOF &&
1124         HEAD.
1125         master.
1126         matching-branch.
1127         other/branch-in-other.
1128         other/master-in-other.
1129         matching-tag.
1130         EOF
1131         (
1132                 cur= &&
1133                 __git_complete_refs --sfx=. &&
1134                 print_comp
1135         ) &&
1136         test_cmp expected out
1137 '
1138
1139 test_expect_success '__git_complete_fetch_refspecs - simple' '
1140         sed -e "s/Z$//" >expected <<-EOF &&
1141         HEAD:HEAD Z
1142         branch-in-other:branch-in-other Z
1143         master-in-other:master-in-other Z
1144         EOF
1145         (
1146                 cur= &&
1147                 __git_complete_fetch_refspecs other &&
1148                 print_comp
1149         ) &&
1150         test_cmp expected out
1151 '
1152
1153 test_expect_success '__git_complete_fetch_refspecs - matching' '
1154         sed -e "s/Z$//" >expected <<-EOF &&
1155         branch-in-other:branch-in-other Z
1156         EOF
1157         (
1158                 cur=br &&
1159                 __git_complete_fetch_refspecs other "" br &&
1160                 print_comp
1161         ) &&
1162         test_cmp expected out
1163 '
1164
1165 test_expect_success '__git_complete_fetch_refspecs - prefix' '
1166         sed -e "s/Z$//" >expected <<-EOF &&
1167         +HEAD:HEAD Z
1168         +branch-in-other:branch-in-other Z
1169         +master-in-other:master-in-other Z
1170         EOF
1171         (
1172                 cur="+" &&
1173                 __git_complete_fetch_refspecs other "+" ""  &&
1174                 print_comp
1175         ) &&
1176         test_cmp expected out
1177 '
1178
1179 test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
1180         sed -e "s/Z$//" >expected <<-EOF &&
1181         refs/heads/branch-in-other:refs/heads/branch-in-other Z
1182         refs/heads/master-in-other:refs/heads/master-in-other Z
1183         refs/tags/tag-in-other:refs/tags/tag-in-other Z
1184         EOF
1185         (
1186                 cur=refs/ &&
1187                 __git_complete_fetch_refspecs other "" refs/ &&
1188                 print_comp
1189         ) &&
1190         test_cmp expected out
1191 '
1192
1193 test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
1194         sed -e "s/Z$//" >expected <<-EOF &&
1195         +refs/heads/branch-in-other:refs/heads/branch-in-other Z
1196         +refs/heads/master-in-other:refs/heads/master-in-other Z
1197         +refs/tags/tag-in-other:refs/tags/tag-in-other Z
1198         EOF
1199         (
1200                 cur=+refs/ &&
1201                 __git_complete_fetch_refspecs other + refs/ &&
1202                 print_comp
1203         ) &&
1204         test_cmp expected out
1205 '
1206
1207 test_expect_success 'teardown after ref completion' '
1208         git branch -d matching-branch &&
1209         git tag -d matching-tag &&
1210         git remote remove other
1211 '
1212
1213
1214 test_path_completion ()
1215 {
1216         test $# = 2 || error "bug in the test script: not 2 parameters to test_path_completion"
1217
1218         local cur="$1" expected="$2"
1219         echo "$expected" >expected &&
1220         (
1221                 # In the following tests calling this function we only
1222                 # care about how __git_complete_index_file() deals with
1223                 # unusual characters in path names.  By requesting only
1224                 # untracked files we dont have to bother adding any
1225                 # paths to the index in those tests.
1226                 __git_complete_index_file --others &&
1227                 print_comp
1228         ) &&
1229         test_cmp expected out
1230 }
1231
1232 test_expect_success 'setup for path completion tests' '
1233         mkdir simple-dir \
1234               "spaces in dir" \
1235               árvíztűrő &&
1236         touch simple-dir/simple-file \
1237               "spaces in dir/spaces in file" \
1238               "árvíztűrő/Сайн яваарай" &&
1239         if test_have_prereq !MINGW &&
1240            mkdir BS\\dir \
1241                  '$'separators\034in\035dir'' &&
1242            touch BS\\dir/DQ\"file \
1243                  '$'separators\034in\035dir/sep\036in\037file''
1244         then
1245                 test_set_prereq FUNNYNAMES
1246         else
1247                 rm -rf BS\\dir '$'separators\034in\035dir''
1248         fi
1249 '
1250
1251 test_expect_success '__git_complete_index_file - simple' '
1252         test_path_completion simple simple-dir &&  # Bash is supposed to
1253                                                    # add the trailing /.
1254         test_path_completion simple-dir/simple simple-dir/simple-file
1255 '
1256
1257 test_expect_success \
1258     '__git_complete_index_file - escaped characters on cmdline' '
1259         test_path_completion spac "spaces in dir" &&  # Bash will turn this
1260                                                       # into "spaces\ in\ dir"
1261         test_path_completion "spaces\\ i" \
1262                              "spaces in dir" &&
1263         test_path_completion "spaces\\ in\\ dir/s" \
1264                              "spaces in dir/spaces in file" &&
1265         test_path_completion "spaces\\ in\\ dir/spaces\\ i" \
1266                              "spaces in dir/spaces in file"
1267 '
1268
1269 test_expect_success \
1270     '__git_complete_index_file - quoted characters on cmdline' '
1271         # Testing with an opening but without a corresponding closing
1272         # double quote is important.
1273         test_path_completion \"spac "spaces in dir" &&
1274         test_path_completion "\"spaces i" \
1275                              "spaces in dir" &&
1276         test_path_completion "\"spaces in dir/s" \
1277                              "spaces in dir/spaces in file" &&
1278         test_path_completion "\"spaces in dir/spaces i" \
1279                              "spaces in dir/spaces in file"
1280 '
1281
1282 test_expect_success '__git_complete_index_file - UTF-8 in ls-files output' '
1283         test_path_completion á árvíztűrő &&
1284         test_path_completion árvíztűrő/С "árvíztűrő/Сайн яваарай"
1285 '
1286
1287 test_expect_success FUNNYNAMES \
1288     '__git_complete_index_file - C-style escapes in ls-files output' '
1289         test_path_completion BS \
1290                              BS\\dir &&
1291         test_path_completion BS\\\\d \
1292                              BS\\dir &&
1293         test_path_completion BS\\\\dir/DQ \
1294                              BS\\dir/DQ\"file &&
1295         test_path_completion BS\\\\dir/DQ\\\"f \
1296                              BS\\dir/DQ\"file
1297 '
1298
1299 test_expect_success FUNNYNAMES \
1300     '__git_complete_index_file - \nnn-escaped characters in ls-files output' '
1301         test_path_completion sep '$'separators\034in\035dir'' &&
1302         test_path_completion '$'separators\034i'' \
1303                              '$'separators\034in\035dir'' &&
1304         test_path_completion '$'separators\034in\035dir/sep'' \
1305                              '$'separators\034in\035dir/sep\036in\037file'' &&
1306         test_path_completion '$'separators\034in\035dir/sep\036i'' \
1307                              '$'separators\034in\035dir/sep\036in\037file''
1308 '
1309
1310 test_expect_success FUNNYNAMES \
1311     '__git_complete_index_file - removing repeated quoted path components' '
1312         test_when_finished rm -r repeated-quoted &&
1313         mkdir repeated-quoted &&      # A directory whose name in itself
1314                                       # would not be quoted ...
1315         >repeated-quoted/0-file &&
1316         >repeated-quoted/1\"file &&   # ... but here the file makes the
1317                                       # dirname quoted ...
1318         >repeated-quoted/2-file &&
1319         >repeated-quoted/3\"file &&   # ... and here, too.
1320
1321         # Still, we shold only list the directory name only once.
1322         test_path_completion repeated repeated-quoted
1323 '
1324
1325 test_expect_success 'teardown after path completion tests' '
1326         rm -rf simple-dir "spaces in dir" árvíztűrő \
1327                BS\\dir '$'separators\034in\035dir''
1328 '
1329
1330
1331 test_expect_success '__git_get_config_variables' '
1332         cat >expect <<-EOF &&
1333         name-1
1334         name-2
1335         EOF
1336         test_config interesting.name-1 good &&
1337         test_config interesting.name-2 good &&
1338         test_config subsection.interesting.name-3 bad &&
1339         __git_get_config_variables interesting >actual &&
1340         test_cmp expect actual
1341 '
1342
1343 test_expect_success '__git_pretty_aliases' '
1344         cat >expect <<-EOF &&
1345         author
1346         hash
1347         EOF
1348         test_config pretty.author "%an %ae" &&
1349         test_config pretty.hash %H &&
1350         __git_pretty_aliases >actual &&
1351         test_cmp expect actual
1352 '
1353
1354 test_expect_success 'basic' '
1355         run_completion "git " &&
1356         # built-in
1357         grep -q "^add \$" out &&
1358         # script
1359         grep -q "^filter-branch \$" out &&
1360         # plumbing
1361         ! grep -q "^ls-files \$" out &&
1362
1363         run_completion "git f" &&
1364         ! grep -q -v "^f" out
1365 '
1366
1367 test_expect_success 'double dash "git" itself' '
1368         test_completion "git --" <<-\EOF
1369         --paginate Z
1370         --no-pager Z
1371         --git-dir=
1372         --bare Z
1373         --version Z
1374         --exec-path Z
1375         --exec-path=
1376         --html-path Z
1377         --man-path Z
1378         --info-path Z
1379         --work-tree=
1380         --namespace=
1381         --no-replace-objects Z
1382         --help Z
1383         EOF
1384 '
1385
1386 test_expect_success 'double dash "git checkout"' '
1387         test_completion "git checkout --" <<-\EOF
1388         --quiet Z
1389         --detach Z
1390         --track Z
1391         --orphan=Z
1392         --ours Z
1393         --theirs Z
1394         --merge Z
1395         --conflict=Z
1396         --patch Z
1397         --ignore-skip-worktree-bits Z
1398         --ignore-other-worktrees Z
1399         --recurse-submodules Z
1400         --progress Z
1401         --no-track Z
1402         --no-recurse-submodules Z
1403         EOF
1404 '
1405
1406 test_expect_success 'general options' '
1407         test_completion "git --ver" "--version " &&
1408         test_completion "git --hel" "--help " &&
1409         test_completion "git --exe" <<-\EOF &&
1410         --exec-path Z
1411         --exec-path=
1412         EOF
1413         test_completion "git --htm" "--html-path " &&
1414         test_completion "git --pag" "--paginate " &&
1415         test_completion "git --no-p" "--no-pager " &&
1416         test_completion "git --git" "--git-dir=" &&
1417         test_completion "git --wor" "--work-tree=" &&
1418         test_completion "git --nam" "--namespace=" &&
1419         test_completion "git --bar" "--bare " &&
1420         test_completion "git --inf" "--info-path " &&
1421         test_completion "git --no-r" "--no-replace-objects "
1422 '
1423
1424 test_expect_success 'general options plus command' '
1425         test_completion "git --version check" "checkout " &&
1426         test_completion "git --paginate check" "checkout " &&
1427         test_completion "git --git-dir=foo check" "checkout " &&
1428         test_completion "git --bare check" "checkout " &&
1429         test_completion "git --exec-path=foo check" "checkout " &&
1430         test_completion "git --html-path check" "checkout " &&
1431         test_completion "git --no-pager check" "checkout " &&
1432         test_completion "git --work-tree=foo check" "checkout " &&
1433         test_completion "git --namespace=foo check" "checkout " &&
1434         test_completion "git --paginate check" "checkout " &&
1435         test_completion "git --info-path check" "checkout " &&
1436         test_completion "git --no-replace-objects check" "checkout " &&
1437         test_completion "git --git-dir some/path check" "checkout " &&
1438         test_completion "git -c conf.var=value check" "checkout " &&
1439         test_completion "git -C some/path check" "checkout " &&
1440         test_completion "git --work-tree some/path check" "checkout " &&
1441         test_completion "git --namespace name/space check" "checkout "
1442 '
1443
1444 test_expect_success 'git --help completion' '
1445         test_completion "git --help ad" "add " &&
1446         test_completion "git --help core" "core-tutorial "
1447 '
1448
1449 test_expect_success 'setup for integration tests' '
1450         echo content >file1 &&
1451         echo more >file2 &&
1452         git add file1 file2 &&
1453         git commit -m one &&
1454         git branch mybranch &&
1455         git tag mytag
1456 '
1457
1458 test_expect_success 'checkout completes ref names' '
1459         test_completion "git checkout m" <<-\EOF
1460         master Z
1461         mybranch Z
1462         mytag Z
1463         EOF
1464 '
1465
1466 test_expect_success 'git -C <path> checkout uses the right repo' '
1467         test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
1468         branch-in-other Z
1469         EOF
1470 '
1471
1472 test_expect_success 'show completes all refs' '
1473         test_completion "git show m" <<-\EOF
1474         master Z
1475         mybranch Z
1476         mytag Z
1477         EOF
1478 '
1479
1480 test_expect_success '<ref>: completes paths' '
1481         test_completion "git show mytag:f" <<-\EOF
1482         file1 Z
1483         file2 Z
1484         EOF
1485 '
1486
1487 test_expect_success 'complete tree filename with spaces' '
1488         echo content >"name with spaces" &&
1489         git add "name with spaces" &&
1490         git commit -m spaces &&
1491         test_completion "git show HEAD:nam" <<-\EOF
1492         name with spaces Z
1493         EOF
1494 '
1495
1496 test_expect_success 'complete tree filename with metacharacters' '
1497         echo content >"name with \${meta}" &&
1498         git add "name with \${meta}" &&
1499         git commit -m meta &&
1500         test_completion "git show HEAD:nam" <<-\EOF
1501         name with ${meta} Z
1502         name with spaces Z
1503         EOF
1504 '
1505
1506 test_expect_success 'send-email' '
1507         test_completion "git send-email --cov" "--cover-letter " &&
1508         test_completion "git send-email ma" "master "
1509 '
1510
1511 test_expect_success 'complete files' '
1512         git init tmp && cd tmp &&
1513         test_when_finished "cd .. && rm -rf tmp" &&
1514
1515         echo "expected" > .gitignore &&
1516         echo "out" >> .gitignore &&
1517         echo "out_sorted" >> .gitignore &&
1518
1519         git add .gitignore &&
1520         test_completion "git commit " ".gitignore" &&
1521
1522         git commit -m ignore &&
1523
1524         touch new &&
1525         test_completion "git add " "new" &&
1526
1527         git add new &&
1528         git commit -a -m new &&
1529         test_completion "git add " "" &&
1530
1531         git mv new modified &&
1532         echo modify > modified &&
1533         test_completion "git add " "modified" &&
1534
1535         touch untracked &&
1536
1537         : TODO .gitignore should not be here &&
1538         test_completion "git rm " <<-\EOF &&
1539         .gitignore
1540         modified
1541         EOF
1542
1543         test_completion "git clean " "untracked" &&
1544
1545         : TODO .gitignore should not be here &&
1546         test_completion "git mv " <<-\EOF &&
1547         .gitignore
1548         modified
1549         EOF
1550
1551         mkdir dir &&
1552         touch dir/file-in-dir &&
1553         git add dir/file-in-dir &&
1554         git commit -m dir &&
1555
1556         mkdir untracked-dir &&
1557
1558         : TODO .gitignore should not be here &&
1559         test_completion "git mv modified " <<-\EOF &&
1560         .gitignore
1561         dir
1562         modified
1563         untracked
1564         untracked-dir
1565         EOF
1566
1567         test_completion "git commit " "modified" &&
1568
1569         : TODO .gitignore should not be here &&
1570         test_completion "git ls-files " <<-\EOF &&
1571         .gitignore
1572         dir
1573         modified
1574         EOF
1575
1576         touch momified &&
1577         test_completion "git add mom" "momified"
1578 '
1579
1580 test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
1581         test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
1582         test_completion "git co m" <<-\EOF
1583         master Z
1584         mybranch Z
1585         mytag Z
1586         EOF
1587 '
1588
1589 test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
1590         test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
1591         test_completion "git co m" <<-\EOF
1592         master Z
1593         mybranch Z
1594         mytag Z
1595         EOF
1596 '
1597
1598 test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
1599         test_config alias.co "!f() { : git checkout ; if ... } f" &&
1600         test_completion "git co m" <<-\EOF
1601         master Z
1602         mybranch Z
1603         mytag Z
1604         EOF
1605 '
1606
1607 test_expect_success 'completion without explicit _git_xxx function' '
1608         test_completion "git version --" <<-\EOF
1609         --build-options Z
1610         EOF
1611 '
1612
1613 test_expect_failure 'complete with tilde expansion' '
1614         git init tmp && cd tmp &&
1615         test_when_finished "cd .. && rm -rf tmp" &&
1616
1617         touch ~/tmp/file &&
1618
1619         test_completion "git add ~/tmp/" "~/tmp/file"
1620 '
1621
1622 test_expect_success 'setup other remote for remote reference completion' '
1623         git remote add other otherrepo &&
1624         git fetch other
1625 '
1626
1627 for flag in -d --delete
1628 do
1629         test_expect_success "__git_complete_remote_or_refspec - push $flag other" '
1630                 sed -e "s/Z$//" >expected <<-EOF &&
1631                 master-in-other Z
1632                 EOF
1633                 (
1634                         words=(git push '$flag' other ma) &&
1635                         cword=${#words[@]} cur=${words[cword-1]} &&
1636                         __git_complete_remote_or_refspec &&
1637                         print_comp
1638                 ) &&
1639                 test_cmp expected out
1640         '
1641
1642         test_expect_failure "__git_complete_remote_or_refspec - push other $flag" '
1643                 sed -e "s/Z$//" >expected <<-EOF &&
1644                 master-in-other Z
1645                 EOF
1646                 (
1647                         words=(git push other '$flag' ma) &&
1648                         cword=${#words[@]} cur=${words[cword-1]} &&
1649                         __git_complete_remote_or_refspec &&
1650                         print_comp
1651                 ) &&
1652                 test_cmp expected out
1653         '
1654 done
1655
1656 test_expect_success 'sourcing the completion script clears cached commands' '
1657         __git_compute_all_commands &&
1658         verbose test -n "$__git_all_commands" &&
1659         . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1660         verbose test -z "$__git_all_commands"
1661 '
1662
1663 test_expect_success !GETTEXT_POISON 'sourcing the completion script clears cached merge strategies' '
1664         __git_compute_merge_strategies &&
1665         verbose test -n "$__git_merge_strategies" &&
1666         . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1667         verbose test -z "$__git_merge_strategies"
1668 '
1669
1670 test_expect_success 'sourcing the completion script clears cached --options' '
1671         __gitcomp_builtin checkout &&
1672         verbose test -n "$__gitcomp_builtin_checkout" &&
1673         __gitcomp_builtin notes_edit &&
1674         verbose test -n "$__gitcomp_builtin_notes_edit" &&
1675         . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1676         verbose test -z "$__gitcomp_builtin_checkout" &&
1677         verbose test -z "$__gitcomp_builtin_notes_edit"
1678 '
1679
1680 test_done