1#!/bin/sh
2#
3# Copyright (c) 2012 Felipe Contreras
4#
5
6test_description='test bash completion'
7
8. ./lib-bash.sh
9
10complete ()
11{
12 # do nothing
13 return 0
14}
15
16# Be careful when updating this list:
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
33GIT_TESTING_COMMAND_COMPLETION='add checkout check-attr filter-branch ls-files'
34
35. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
36
37# We don't need this function to actually join words or do anything special.
38# Also, it's cleaner to avoid touching bash's internal completion variables.
39# So let's override it with a minimal version for testing purposes.
40_get_comp_words_by_ref ()
41{
42 while [ $# -gt 0 ]; do
43 case "$1" in
44 cur)
45 cur=${_words[_cword]}
46 ;;
47 prev)
48 prev=${_words[_cword-1]}
49 ;;
50 words)
51 words=("${_words[@]}")
52 ;;
53 cword)
54 cword=$_cword
55 ;;
56 esac
57 shift
58 done
59}
60
61print_comp ()
62{
63 local IFS=$'\n'
64 echo "${COMPREPLY[*]}" > out
65}
66
67run_completion ()
68{
69 local -a COMPREPLY _words
70 local _cword
71 _words=( $1 )
72 test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
73 (( _cword = ${#_words[@]} - 1 ))
74 __git_wrap__git_main && print_comp
75}
76
77# Test high-level completion
78# Arguments are:
79# 1: typed text so far (cur)
80# 2: expected completion
81test_completion ()
82{
83 if test $# -gt 1
84 then
85 printf '%s\n' "$2" >expected
86 else
87 sed -e 's/Z$//' >expected
88 fi &&
89 run_completion "$1" &&
90 test_cmp expected out
91}
92
93# Test __gitcomp.
94# The first argument is the typed text so far (cur); the rest are
95# passed to __gitcomp. Expected output comes is read from the
96# standard input, like test_completion().
97test_gitcomp ()
98{
99 local -a COMPREPLY &&
100 sed -e 's/Z$//' >expected &&
101 local cur="$1" &&
102 shift &&
103 __gitcomp "$@" &&
104 print_comp &&
105 test_cmp expected out
106}
107
108# Test __gitcomp_nl
109# Arguments are:
110# 1: current word (cur)
111# -: the rest are passed to __gitcomp_nl
112test_gitcomp_nl ()
113{
114 local -a COMPREPLY &&
115 sed -e 's/Z$//' >expected &&
116 local cur="$1" &&
117 shift &&
118 __gitcomp_nl "$@" &&
119 print_comp &&
120 test_cmp expected out
121}
122
123invalid_variable_name='${foo.bar}'
124
125actual="$TRASH_DIRECTORY/actual"
126
127if test_have_prereq MINGW
128then
129 ROOT="$(pwd -W)"
130else
131 ROOT="$(pwd)"
132fi
133
134test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
135 mkdir -p subdir/subsubdir &&
136 mkdir -p non-repo &&
137 git init otherrepo
138'
139
140test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '
141 echo "$ROOT/otherrepo/.git" >expected &&
142 (
143 __git_dir="$ROOT/otherrepo/.git" &&
144 __git_find_repo_path &&
145 echo "$__git_repo_path" >"$actual"
146 ) &&
147 test_cmp expected "$actual"
148'
149
150test_expect_success '__git_find_repo_path - .git directory in cwd' '
151 echo ".git" >expected &&
152 (
153 __git_find_repo_path &&
154 echo "$__git_repo_path" >"$actual"
155 ) &&
156 test_cmp expected "$actual"
157'
158
159test_expect_success '__git_find_repo_path - .git directory in parent' '
160 echo "$ROOT/.git" >expected &&
161 (
162 cd subdir/subsubdir &&
163 __git_find_repo_path &&
164 echo "$__git_repo_path" >"$actual"
165 ) &&
166 test_cmp expected "$actual"
167'
168
169test_expect_success '__git_find_repo_path - cwd is a .git directory' '
170 echo "." >expected &&
171 (
172 cd .git &&
173 __git_find_repo_path &&
174 echo "$__git_repo_path" >"$actual"
175 ) &&
176 test_cmp expected "$actual"
177'
178
179test_expect_success '__git_find_repo_path - parent is a .git directory' '
180 echo "$ROOT/.git" >expected &&
181 (
182 cd .git/refs/heads &&
183 __git_find_repo_path &&
184 echo "$__git_repo_path" >"$actual"
185 ) &&
186 test_cmp expected "$actual"
187'
188
189test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
190 echo "$ROOT/otherrepo/.git" >expected &&
191 (
192 GIT_DIR="$ROOT/otherrepo/.git" &&
193 export GIT_DIR &&
194 __git_find_repo_path &&
195 echo "$__git_repo_path" >"$actual"
196 ) &&
197 test_cmp expected "$actual"
198'
199
200test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
201 echo "$ROOT/otherrepo/.git" >expected &&
202 (
203 GIT_DIR="$ROOT/otherrepo/.git" &&
204 export GIT_DIR &&
205 cd subdir &&
206 __git_find_repo_path &&
207 echo "$__git_repo_path" >"$actual"
208 ) &&
209 test_cmp expected "$actual"
210'
211
212test_expect_success '__git_find_repo_path - from command line while "git -C"' '
213 echo "$ROOT/.git" >expected &&
214 (
215 __git_dir="$ROOT/.git" &&
216 __git_C_args=(-C otherrepo) &&
217 __git_find_repo_path &&
218 echo "$__git_repo_path" >"$actual"
219 ) &&
220 test_cmp expected "$actual"
221'
222
223test_expect_success '__git_find_repo_path - relative dir from command line and "git -C"' '
224 echo "$ROOT/otherrepo/.git" >expected &&
225 (
226 cd subdir &&
227 __git_dir="otherrepo/.git" &&
228 __git_C_args=(-C ..) &&
229 __git_find_repo_path &&
230 echo "$__git_repo_path" >"$actual"
231 ) &&
232 test_cmp expected "$actual"
233'
234
235test_expect_success '__git_find_repo_path - $GIT_DIR set while "git -C"' '
236 echo "$ROOT/.git" >expected &&
237 (
238 GIT_DIR="$ROOT/.git" &&
239 export GIT_DIR &&
240 __git_C_args=(-C otherrepo) &&
241 __git_find_repo_path &&
242 echo "$__git_repo_path" >"$actual"
243 ) &&
244 test_cmp expected "$actual"
245'
246
247test_expect_success '__git_find_repo_path - relative dir in $GIT_DIR and "git -C"' '
248 echo "$ROOT/otherrepo/.git" >expected &&
249 (
250 cd subdir &&
251 GIT_DIR="otherrepo/.git" &&
252 export GIT_DIR &&
253 __git_C_args=(-C ..) &&
254 __git_find_repo_path &&
255 echo "$__git_repo_path" >"$actual"
256 ) &&
257 test_cmp expected "$actual"
258'
259
260test_expect_success '__git_find_repo_path - "git -C" while .git directory in cwd' '
261 echo "$ROOT/otherrepo/.git" >expected &&
262 (
263 __git_C_args=(-C otherrepo) &&
264 __git_find_repo_path &&
265 echo "$__git_repo_path" >"$actual"
266 ) &&
267 test_cmp expected "$actual"
268'
269
270test_expect_success '__git_find_repo_path - "git -C" while cwd is a .git directory' '
271 echo "$ROOT/otherrepo/.git" >expected &&
272 (
273 cd .git &&
274 __git_C_args=(-C .. -C otherrepo) &&
275 __git_find_repo_path &&
276 echo "$__git_repo_path" >"$actual"
277 ) &&
278 test_cmp expected "$actual"
279'
280
281test_expect_success '__git_find_repo_path - "git -C" while .git directory in parent' '
282 echo "$ROOT/otherrepo/.git" >expected &&
283 (
284 cd subdir &&
285 __git_C_args=(-C .. -C otherrepo) &&
286 __git_find_repo_path &&
287 echo "$__git_repo_path" >"$actual"
288 ) &&
289 test_cmp expected "$actual"
290'
291
292test_expect_success '__git_find_repo_path - non-existing path in "git -C"' '
293 (
294 __git_C_args=(-C non-existing) &&
295 test_must_fail __git_find_repo_path &&
296 printf "$__git_repo_path" >"$actual"
297 ) &&
298 test_must_be_empty "$actual"
299'
300
301test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '
302 (
303 __git_dir="non-existing" &&
304 test_must_fail __git_find_repo_path &&
305 printf "$__git_repo_path" >"$actual"
306 ) &&
307 test_must_be_empty "$actual"
308'
309
310test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
311 (
312 GIT_DIR="$ROOT/non-existing" &&
313 export GIT_DIR &&
314 test_must_fail __git_find_repo_path &&
315 printf "$__git_repo_path" >"$actual"
316 ) &&
317 test_must_be_empty "$actual"
318'
319
320test_expect_success '__git_find_repo_path - gitfile in cwd' '
321 echo "$ROOT/otherrepo/.git" >expected &&
322 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
323 test_when_finished "rm -f subdir/.git" &&
324 (
325 cd subdir &&
326 __git_find_repo_path &&
327 echo "$__git_repo_path" >"$actual"
328 ) &&
329 test_cmp expected "$actual"
330'
331
332test_expect_success '__git_find_repo_path - gitfile in parent' '
333 echo "$ROOT/otherrepo/.git" >expected &&
334 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
335 test_when_finished "rm -f subdir/.git" &&
336 (
337 cd subdir/subsubdir &&
338 __git_find_repo_path &&
339 echo "$__git_repo_path" >"$actual"
340 ) &&
341 test_cmp expected "$actual"
342'
343
344test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '
345 echo "$ROOT/otherrepo/.git" >expected &&
346 mkdir otherrepo/dir &&
347 test_when_finished "rm -rf otherrepo/dir" &&
348 ln -s otherrepo/dir link &&
349 test_when_finished "rm -f link" &&
350 (
351 cd link &&
352 __git_find_repo_path &&
353 echo "$__git_repo_path" >"$actual"
354 ) &&
355 test_cmp expected "$actual"
356'
357
358test_expect_success '__git_find_repo_path - not a git repository' '
359 (
360 cd non-repo &&
361 GIT_CEILING_DIRECTORIES="$ROOT" &&
362 export GIT_CEILING_DIRECTORIES &&
363 test_must_fail __git_find_repo_path &&
364 printf "$__git_repo_path" >"$actual"
365 ) &&
366 test_must_be_empty "$actual"
367'
368
369test_expect_success '__gitdir - finds repo' '
370 echo "$ROOT/.git" >expected &&
371 (
372 cd subdir/subsubdir &&
373 __gitdir >"$actual"
374 ) &&
375 test_cmp expected "$actual"
376'
377
378
379test_expect_success '__gitdir - returns error when cant find repo' '
380 (
381 __git_dir="non-existing" &&
382 test_must_fail __gitdir >"$actual"
383 ) &&
384 test_must_be_empty "$actual"
385'
386
387test_expect_success '__gitdir - repo as argument' '
388 echo "otherrepo/.git" >expected &&
389 (
390 __gitdir "otherrepo" >"$actual"
391 ) &&
392 test_cmp expected "$actual"
393'
394
395test_expect_success '__gitdir - remote as argument' '
396 echo "remote" >expected &&
397 (
398 __gitdir "remote" >"$actual"
399 ) &&
400 test_cmp expected "$actual"
401'
402
403test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' '
404 sed -e "s/Z$//g" >expected <<-EOF &&
405 with-trailing-space Z
406 without-trailing-spaceZ
407 --option Z
408 --option=Z
409 $invalid_variable_name Z
410 EOF
411 (
412 cur=should_be_ignored &&
413 __gitcomp_direct "$(cat expected)" &&
414 print_comp
415 ) &&
416 test_cmp expected out
417'
418
419test_expect_success '__gitcomp - trailing space - options' '
420 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
421 --reset-author" <<-EOF
422 --reuse-message=Z
423 --reedit-message=Z
424 --reset-author Z
425 EOF
426'
427
428test_expect_success '__gitcomp - trailing space - config keys' '
429 test_gitcomp "br" "branch. branch.autosetupmerge
430 branch.autosetuprebase browser." <<-\EOF
431 branch.Z
432 branch.autosetupmerge Z
433 branch.autosetuprebase Z
434 browser.Z
435 EOF
436'
437
438test_expect_success '__gitcomp - option parameter' '
439 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
440 "" "re" <<-\EOF
441 recursive Z
442 resolve Z
443 EOF
444'
445
446test_expect_success '__gitcomp - prefix' '
447 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
448 "branch.maint." "me" <<-\EOF
449 branch.maint.merge Z
450 branch.maint.mergeoptions Z
451 EOF
452'
453
454test_expect_success '__gitcomp - suffix' '
455 test_gitcomp "branch.me" "master maint next pu" "branch." \
456 "ma" "." <<-\EOF
457 branch.master.Z
458 branch.maint.Z
459 EOF
460'
461
462test_expect_success '__gitcomp - ignore optional negative options' '
463 test_gitcomp "--" "--abc --def --no-one -- --no-two" <<-\EOF
464 --abc Z
465 --def Z
466 --no-one Z
467 --no-... Z
468 EOF
469'
470
471test_expect_success '__gitcomp - ignore/narrow optional negative options' '
472 test_gitcomp "--a" "--abc --abcdef --no-one -- --no-two" <<-\EOF
473 --abc Z
474 --abcdef Z
475 EOF
476'
477
478test_expect_success '__gitcomp - ignore/narrow optional negative options' '
479 test_gitcomp "--n" "--abc --def --no-one -- --no-two" <<-\EOF
480 --no-one Z
481 --no-... Z
482 EOF
483'
484
485test_expect_success '__gitcomp - expand all negative options' '
486 test_gitcomp "--no-" "--abc --def --no-one -- --no-two" <<-\EOF
487 --no-one Z
488 --no-two Z
489 EOF
490'
491
492test_expect_success '__gitcomp - expand/narrow all negative options' '
493 test_gitcomp "--no-o" "--abc --def --no-one -- --no-two" <<-\EOF
494 --no-one Z
495 EOF
496'
497
498test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
499 __gitcomp "$invalid_variable_name"
500'
501
502read -r -d "" refs <<-\EOF
503maint
504master
505next
506pu
507EOF
508
509test_expect_success '__gitcomp_nl - trailing space' '
510 test_gitcomp_nl "m" "$refs" <<-EOF
511 maint Z
512 master Z
513 EOF
514'
515
516test_expect_success '__gitcomp_nl - prefix' '
517 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
518 --fixup=maint Z
519 --fixup=master Z
520 EOF
521'
522
523test_expect_success '__gitcomp_nl - suffix' '
524 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
525 branch.maint.Z
526 branch.master.Z
527 EOF
528'
529
530test_expect_success '__gitcomp_nl - no suffix' '
531 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
532 maintZ
533 masterZ
534 EOF
535'
536
537test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
538 __gitcomp_nl "$invalid_variable_name"
539'
540
541test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
542 cat >expect <<-EOF &&
543 remote_from_file_1
544 remote_from_file_2
545 remote_in_config_1
546 remote_in_config_2
547 EOF
548 test_when_finished "rm -rf .git/remotes" &&
549 mkdir -p .git/remotes &&
550 >.git/remotes/remote_from_file_1 &&
551 >.git/remotes/remote_from_file_2 &&
552 test_when_finished "git remote remove remote_in_config_1" &&
553 git remote add remote_in_config_1 git://remote_1 &&
554 test_when_finished "git remote remove remote_in_config_2" &&
555 git remote add remote_in_config_2 git://remote_2 &&
556 (
557 __git_remotes >actual
558 ) &&
559 test_cmp expect actual
560'
561
562test_expect_success '__git_is_configured_remote' '
563 test_when_finished "git remote remove remote_1" &&
564 git remote add remote_1 git://remote_1 &&
565 test_when_finished "git remote remove remote_2" &&
566 git remote add remote_2 git://remote_2 &&
567 (
568 verbose __git_is_configured_remote remote_2 &&
569 test_must_fail __git_is_configured_remote non-existent
570 )
571'
572
573test_expect_success 'setup for ref completion' '
574 git commit --allow-empty -m initial &&
575 git branch matching-branch &&
576 git tag matching-tag &&
577 (
578 cd otherrepo &&
579 git commit --allow-empty -m initial &&
580 git branch -m master master-in-other &&
581 git branch branch-in-other &&
582 git tag tag-in-other
583 ) &&
584 git remote add other "$ROOT/otherrepo/.git" &&
585 git fetch --no-tags other &&
586 rm -f .git/FETCH_HEAD &&
587 git init thirdrepo
588'
589
590test_expect_success '__git_refs - simple' '
591 cat >expected <<-EOF &&
592 HEAD
593 master
594 matching-branch
595 other/branch-in-other
596 other/master-in-other
597 matching-tag
598 EOF
599 (
600 cur= &&
601 __git_refs >"$actual"
602 ) &&
603 test_cmp expected "$actual"
604'
605
606test_expect_success '__git_refs - full refs' '
607 cat >expected <<-EOF &&
608 refs/heads/master
609 refs/heads/matching-branch
610 refs/remotes/other/branch-in-other
611 refs/remotes/other/master-in-other
612 refs/tags/matching-tag
613 EOF
614 (
615 cur=refs/heads/ &&
616 __git_refs >"$actual"
617 ) &&
618 test_cmp expected "$actual"
619'
620
621test_expect_success '__git_refs - repo given on the command line' '
622 cat >expected <<-EOF &&
623 HEAD
624 branch-in-other
625 master-in-other
626 tag-in-other
627 EOF
628 (
629 __git_dir="$ROOT/otherrepo/.git" &&
630 cur= &&
631 __git_refs >"$actual"
632 ) &&
633 test_cmp expected "$actual"
634'
635
636test_expect_success '__git_refs - remote on local file system' '
637 cat >expected <<-EOF &&
638 HEAD
639 branch-in-other
640 master-in-other
641 tag-in-other
642 EOF
643 (
644 cur= &&
645 __git_refs otherrepo >"$actual"
646 ) &&
647 test_cmp expected "$actual"
648'
649
650test_expect_success '__git_refs - remote on local file system - full refs' '
651 cat >expected <<-EOF &&
652 refs/heads/branch-in-other
653 refs/heads/master-in-other
654 refs/tags/tag-in-other
655 EOF
656 (
657 cur=refs/ &&
658 __git_refs otherrepo >"$actual"
659 ) &&
660 test_cmp expected "$actual"
661'
662
663test_expect_success '__git_refs - configured remote' '
664 cat >expected <<-EOF &&
665 HEAD
666 branch-in-other
667 master-in-other
668 EOF
669 (
670 cur= &&
671 __git_refs other >"$actual"
672 ) &&
673 test_cmp expected "$actual"
674'
675
676test_expect_success '__git_refs - configured remote - full refs' '
677 cat >expected <<-EOF &&
678 HEAD
679 refs/heads/branch-in-other
680 refs/heads/master-in-other
681 refs/tags/tag-in-other
682 EOF
683 (
684 cur=refs/ &&
685 __git_refs other >"$actual"
686 ) &&
687 test_cmp expected "$actual"
688'
689
690test_expect_success '__git_refs - configured remote - repo given on the command line' '
691 cat >expected <<-EOF &&
692 HEAD
693 branch-in-other
694 master-in-other
695 EOF
696 (
697 cd thirdrepo &&
698 __git_dir="$ROOT/.git" &&
699 cur= &&
700 __git_refs other >"$actual"
701 ) &&
702 test_cmp expected "$actual"
703'
704
705test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
706 cat >expected <<-EOF &&
707 HEAD
708 refs/heads/branch-in-other
709 refs/heads/master-in-other
710 refs/tags/tag-in-other
711 EOF
712 (
713 cd thirdrepo &&
714 __git_dir="$ROOT/.git" &&
715 cur=refs/ &&
716 __git_refs other >"$actual"
717 ) &&
718 test_cmp expected "$actual"
719'
720
721test_expect_success '__git_refs - configured remote - remote name matches a directory' '
722 cat >expected <<-EOF &&
723 HEAD
724 branch-in-other
725 master-in-other
726 EOF
727 mkdir other &&
728 test_when_finished "rm -rf other" &&
729 (
730 cur= &&
731 __git_refs other >"$actual"
732 ) &&
733 test_cmp expected "$actual"
734'
735
736test_expect_success '__git_refs - URL remote' '
737 cat >expected <<-EOF &&
738 HEAD
739 branch-in-other
740 master-in-other
741 tag-in-other
742 EOF
743 (
744 cur= &&
745 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
746 ) &&
747 test_cmp expected "$actual"
748'
749
750test_expect_success '__git_refs - URL remote - full refs' '
751 cat >expected <<-EOF &&
752 HEAD
753 refs/heads/branch-in-other
754 refs/heads/master-in-other
755 refs/tags/tag-in-other
756 EOF
757 (
758 cur=refs/ &&
759 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
760 ) &&
761 test_cmp expected "$actual"
762'
763
764test_expect_success '__git_refs - non-existing remote' '
765 (
766 cur= &&
767 __git_refs non-existing >"$actual"
768 ) &&
769 test_must_be_empty "$actual"
770'
771
772test_expect_success '__git_refs - non-existing remote - full refs' '
773 (
774 cur=refs/ &&
775 __git_refs non-existing >"$actual"
776 ) &&
777 test_must_be_empty "$actual"
778'
779
780test_expect_success '__git_refs - non-existing URL remote' '
781 (
782 cur= &&
783 __git_refs "file://$ROOT/non-existing" >"$actual"
784 ) &&
785 test_must_be_empty "$actual"
786'
787
788test_expect_success '__git_refs - non-existing URL remote - full refs' '
789 (
790 cur=refs/ &&
791 __git_refs "file://$ROOT/non-existing" >"$actual"
792 ) &&
793 test_must_be_empty "$actual"
794'
795
796test_expect_success '__git_refs - not in a git repository' '
797 (
798 GIT_CEILING_DIRECTORIES="$ROOT" &&
799 export GIT_CEILING_DIRECTORIES &&
800 cd subdir &&
801 cur= &&
802 __git_refs >"$actual"
803 ) &&
804 test_must_be_empty "$actual"
805'
806
807test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
808 cat >expected <<-EOF &&
809 HEAD
810 master
811 matching-branch
812 other/ambiguous
813 other/branch-in-other
814 other/master-in-other
815 remote/ambiguous
816 remote/branch-in-remote
817 matching-tag
818 branch-in-other
819 branch-in-remote
820 master-in-other
821 EOF
822 for remote_ref in refs/remotes/other/ambiguous \
823 refs/remotes/remote/ambiguous \
824 refs/remotes/remote/branch-in-remote
825 do
826 git update-ref $remote_ref master &&
827 test_when_finished "git update-ref -d $remote_ref"
828 done &&
829 (
830 cur= &&
831 __git_refs "" 1 >"$actual"
832 ) &&
833 test_cmp expected "$actual"
834'
835
836test_expect_success '__git_refs - after --opt=' '
837 cat >expected <<-EOF &&
838 HEAD
839 master
840 matching-branch
841 other/branch-in-other
842 other/master-in-other
843 matching-tag
844 EOF
845 (
846 cur="--opt=" &&
847 __git_refs "" "" "" "" >"$actual"
848 ) &&
849 test_cmp expected "$actual"
850'
851
852test_expect_success '__git_refs - after --opt= - full refs' '
853 cat >expected <<-EOF &&
854 refs/heads/master
855 refs/heads/matching-branch
856 refs/remotes/other/branch-in-other
857 refs/remotes/other/master-in-other
858 refs/tags/matching-tag
859 EOF
860 (
861 cur="--opt=refs/" &&
862 __git_refs "" "" "" refs/ >"$actual"
863 ) &&
864 test_cmp expected "$actual"
865'
866
867test_expect_success '__git refs - exluding refs' '
868 cat >expected <<-EOF &&
869 ^HEAD
870 ^master
871 ^matching-branch
872 ^other/branch-in-other
873 ^other/master-in-other
874 ^matching-tag
875 EOF
876 (
877 cur=^ &&
878 __git_refs >"$actual"
879 ) &&
880 test_cmp expected "$actual"
881'
882
883test_expect_success '__git refs - exluding full refs' '
884 cat >expected <<-EOF &&
885 ^refs/heads/master
886 ^refs/heads/matching-branch
887 ^refs/remotes/other/branch-in-other
888 ^refs/remotes/other/master-in-other
889 ^refs/tags/matching-tag
890 EOF
891 (
892 cur=^refs/ &&
893 __git_refs >"$actual"
894 ) &&
895 test_cmp expected "$actual"
896'
897
898test_expect_success 'setup for filtering matching refs' '
899 git branch matching/branch &&
900 git tag matching/tag &&
901 git -C otherrepo branch matching/branch-in-other &&
902 git fetch --no-tags other &&
903 rm -f .git/FETCH_HEAD
904'
905
906test_expect_success '__git_refs - dont filter refs unless told so' '
907 cat >expected <<-EOF &&
908 HEAD
909 master
910 matching-branch
911 matching/branch
912 other/branch-in-other
913 other/master-in-other
914 other/matching/branch-in-other
915 matching-tag
916 matching/tag
917 EOF
918 (
919 cur=master &&
920 __git_refs >"$actual"
921 ) &&
922 test_cmp expected "$actual"
923'
924
925test_expect_success '__git_refs - only matching refs' '
926 cat >expected <<-EOF &&
927 matching-branch
928 matching/branch
929 matching-tag
930 matching/tag
931 EOF
932 (
933 cur=mat &&
934 __git_refs "" "" "" "$cur" >"$actual"
935 ) &&
936 test_cmp expected "$actual"
937'
938
939test_expect_success '__git_refs - only matching refs - full refs' '
940 cat >expected <<-EOF &&
941 refs/heads/matching-branch
942 refs/heads/matching/branch
943 EOF
944 (
945 cur=refs/heads/mat &&
946 __git_refs "" "" "" "$cur" >"$actual"
947 ) &&
948 test_cmp expected "$actual"
949'
950
951test_expect_success '__git_refs - only matching refs - remote on local file system' '
952 cat >expected <<-EOF &&
953 master-in-other
954 matching/branch-in-other
955 EOF
956 (
957 cur=ma &&
958 __git_refs otherrepo "" "" "$cur" >"$actual"
959 ) &&
960 test_cmp expected "$actual"
961'
962
963test_expect_success '__git_refs - only matching refs - configured remote' '
964 cat >expected <<-EOF &&
965 master-in-other
966 matching/branch-in-other
967 EOF
968 (
969 cur=ma &&
970 __git_refs other "" "" "$cur" >"$actual"
971 ) &&
972 test_cmp expected "$actual"
973'
974
975test_expect_success '__git_refs - only matching refs - remote - full refs' '
976 cat >expected <<-EOF &&
977 refs/heads/master-in-other
978 refs/heads/matching/branch-in-other
979 EOF
980 (
981 cur=refs/heads/ma &&
982 __git_refs other "" "" "$cur" >"$actual"
983 ) &&
984 test_cmp expected "$actual"
985'
986
987test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
988 cat >expected <<-EOF &&
989 matching-branch
990 matching/branch
991 matching-tag
992 matching/tag
993 matching/branch-in-other
994 EOF
995 for remote_ref in refs/remotes/other/ambiguous \
996 refs/remotes/remote/ambiguous \
997 refs/remotes/remote/branch-in-remote
998 do
999 git update-ref $remote_ref master &&
1000 test_when_finished "git update-ref -d $remote_ref"
1001 done &&
1002 (
1003 cur=mat &&
1004 __git_refs "" 1 "" "$cur" >"$actual"
1005 ) &&
1006 test_cmp expected "$actual"
1007'
1008
1009test_expect_success 'teardown after filtering matching refs' '
1010 git branch -d matching/branch &&
1011 git tag -d matching/tag &&
1012 git update-ref -d refs/remotes/other/matching/branch-in-other &&
1013 git -C otherrepo branch -D matching/branch-in-other
1014'
1015
1016test_expect_success '__git_refs - for-each-ref format specifiers in prefix' '
1017 cat >expected <<-EOF &&
1018 evil-%%-%42-%(refname)..master
1019 EOF
1020 (
1021 cur="evil-%%-%42-%(refname)..mas" &&
1022 __git_refs "" "" "evil-%%-%42-%(refname).." mas >"$actual"
1023 ) &&
1024 test_cmp expected "$actual"
1025'
1026
1027test_expect_success '__git_complete_refs - simple' '
1028 sed -e "s/Z$//" >expected <<-EOF &&
1029 HEAD Z
1030 master Z
1031 matching-branch Z
1032 other/branch-in-other Z
1033 other/master-in-other Z
1034 matching-tag Z
1035 EOF
1036 (
1037 cur= &&
1038 __git_complete_refs &&
1039 print_comp
1040 ) &&
1041 test_cmp expected out
1042'
1043
1044test_expect_success '__git_complete_refs - matching' '
1045 sed -e "s/Z$//" >expected <<-EOF &&
1046 matching-branch Z
1047 matching-tag Z
1048 EOF
1049 (
1050 cur=mat &&
1051 __git_complete_refs &&
1052 print_comp
1053 ) &&
1054 test_cmp expected out
1055'
1056
1057test_expect_success '__git_complete_refs - remote' '
1058 sed -e "s/Z$//" >expected <<-EOF &&
1059 HEAD Z
1060 branch-in-other Z
1061 master-in-other Z
1062 EOF
1063 (
1064 cur=
1065 __git_complete_refs --remote=other &&
1066 print_comp
1067 ) &&
1068 test_cmp expected out
1069'
1070
1071test_expect_success '__git_complete_refs - track' '
1072 sed -e "s/Z$//" >expected <<-EOF &&
1073 HEAD Z
1074 master Z
1075 matching-branch Z
1076 other/branch-in-other Z
1077 other/master-in-other Z
1078 matching-tag Z
1079 branch-in-other Z
1080 master-in-other Z
1081 EOF
1082 (
1083 cur=
1084 __git_complete_refs --track &&
1085 print_comp
1086 ) &&
1087 test_cmp expected out
1088'
1089
1090test_expect_success '__git_complete_refs - current word' '
1091 sed -e "s/Z$//" >expected <<-EOF &&
1092 matching-branch Z
1093 matching-tag Z
1094 EOF
1095 (
1096 cur="--option=mat" &&
1097 __git_complete_refs --cur="${cur#*=}" &&
1098 print_comp
1099 ) &&
1100 test_cmp expected out
1101'
1102
1103test_expect_success '__git_complete_refs - prefix' '
1104 sed -e "s/Z$//" >expected <<-EOF &&
1105 v1.0..matching-branch Z
1106 v1.0..matching-tag Z
1107 EOF
1108 (
1109 cur=v1.0..mat &&
1110 __git_complete_refs --pfx=v1.0.. --cur=mat &&
1111 print_comp
1112 ) &&
1113 test_cmp expected out
1114'
1115
1116test_expect_success '__git_complete_refs - suffix' '
1117 cat >expected <<-EOF &&
1118 HEAD.
1119 master.
1120 matching-branch.
1121 other/branch-in-other.
1122 other/master-in-other.
1123 matching-tag.
1124 EOF
1125 (
1126 cur= &&
1127 __git_complete_refs --sfx=. &&
1128 print_comp
1129 ) &&
1130 test_cmp expected out
1131'
1132
1133test_expect_success '__git_complete_fetch_refspecs - simple' '
1134 sed -e "s/Z$//" >expected <<-EOF &&
1135 HEAD:HEAD Z
1136 branch-in-other:branch-in-other Z
1137 master-in-other:master-in-other Z
1138 EOF
1139 (
1140 cur= &&
1141 __git_complete_fetch_refspecs other &&
1142 print_comp
1143 ) &&
1144 test_cmp expected out
1145'
1146
1147test_expect_success '__git_complete_fetch_refspecs - matching' '
1148 sed -e "s/Z$//" >expected <<-EOF &&
1149 branch-in-other:branch-in-other Z
1150 EOF
1151 (
1152 cur=br &&
1153 __git_complete_fetch_refspecs other "" br &&
1154 print_comp
1155 ) &&
1156 test_cmp expected out
1157'
1158
1159test_expect_success '__git_complete_fetch_refspecs - prefix' '
1160 sed -e "s/Z$//" >expected <<-EOF &&
1161 +HEAD:HEAD Z
1162 +branch-in-other:branch-in-other Z
1163 +master-in-other:master-in-other Z
1164 EOF
1165 (
1166 cur="+" &&
1167 __git_complete_fetch_refspecs other "+" "" &&
1168 print_comp
1169 ) &&
1170 test_cmp expected out
1171'
1172
1173test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
1174 sed -e "s/Z$//" >expected <<-EOF &&
1175 refs/heads/branch-in-other:refs/heads/branch-in-other Z
1176 refs/heads/master-in-other:refs/heads/master-in-other Z
1177 refs/tags/tag-in-other:refs/tags/tag-in-other Z
1178 EOF
1179 (
1180 cur=refs/ &&
1181 __git_complete_fetch_refspecs other "" refs/ &&
1182 print_comp
1183 ) &&
1184 test_cmp expected out
1185'
1186
1187test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
1188 sed -e "s/Z$//" >expected <<-EOF &&
1189 +refs/heads/branch-in-other:refs/heads/branch-in-other Z
1190 +refs/heads/master-in-other:refs/heads/master-in-other Z
1191 +refs/tags/tag-in-other:refs/tags/tag-in-other Z
1192 EOF
1193 (
1194 cur=+refs/ &&
1195 __git_complete_fetch_refspecs other + refs/ &&
1196 print_comp
1197 ) &&
1198 test_cmp expected out
1199'
1200
1201test_expect_success 'teardown after ref completion' '
1202 git branch -d matching-branch &&
1203 git tag -d matching-tag &&
1204 git remote remove other
1205'
1206
1207test_expect_success '__git_get_config_variables' '
1208 cat >expect <<-EOF &&
1209 name-1
1210 name-2
1211 EOF
1212 test_config interesting.name-1 good &&
1213 test_config interesting.name-2 good &&
1214 test_config subsection.interesting.name-3 bad &&
1215 __git_get_config_variables interesting >actual &&
1216 test_cmp expect actual
1217'
1218
1219test_expect_success '__git_pretty_aliases' '
1220 cat >expect <<-EOF &&
1221 author
1222 hash
1223 EOF
1224 test_config pretty.author "%an %ae" &&
1225 test_config pretty.hash %H &&
1226 __git_pretty_aliases >actual &&
1227 test_cmp expect actual
1228'
1229
1230test_expect_success '__git_aliases' '
1231 cat >expect <<-EOF &&
1232 ci
1233 co
1234 EOF
1235 test_config alias.ci commit &&
1236 test_config alias.co checkout &&
1237 __git_aliases >actual &&
1238 test_cmp expect actual
1239'
1240
1241test_expect_success 'basic' '
1242 run_completion "git " &&
1243 # built-in
1244 grep -q "^add \$" out &&
1245 # script
1246 grep -q "^filter-branch \$" out &&
1247 # plumbing
1248 ! grep -q "^ls-files \$" out &&
1249
1250 run_completion "git f" &&
1251 ! grep -q -v "^f" out
1252'
1253
1254test_expect_success 'double dash "git" itself' '
1255 test_completion "git --" <<-\EOF
1256 --paginate Z
1257 --no-pager Z
1258 --git-dir=
1259 --bare Z
1260 --version Z
1261 --exec-path Z
1262 --exec-path=
1263 --html-path Z
1264 --man-path Z
1265 --info-path Z
1266 --work-tree=
1267 --namespace=
1268 --no-replace-objects Z
1269 --help Z
1270 EOF
1271'
1272
1273test_expect_success 'double dash "git checkout"' '
1274 test_completion "git checkout --" <<-\EOF
1275 --quiet Z
1276 --detach Z
1277 --track Z
1278 --orphan=Z
1279 --ours Z
1280 --theirs Z
1281 --merge Z
1282 --conflict=Z
1283 --patch Z
1284 --ignore-skip-worktree-bits Z
1285 --ignore-other-worktrees Z
1286 --recurse-submodules Z
1287 --progress Z
1288 --no-quiet Z
1289 --no-... Z
1290 EOF
1291'
1292
1293test_expect_success 'general options' '
1294 test_completion "git --ver" "--version " &&
1295 test_completion "git --hel" "--help " &&
1296 test_completion "git --exe" <<-\EOF &&
1297 --exec-path Z
1298 --exec-path=
1299 EOF
1300 test_completion "git --htm" "--html-path " &&
1301 test_completion "git --pag" "--paginate " &&
1302 test_completion "git --no-p" "--no-pager " &&
1303 test_completion "git --git" "--git-dir=" &&
1304 test_completion "git --wor" "--work-tree=" &&
1305 test_completion "git --nam" "--namespace=" &&
1306 test_completion "git --bar" "--bare " &&
1307 test_completion "git --inf" "--info-path " &&
1308 test_completion "git --no-r" "--no-replace-objects "
1309'
1310
1311test_expect_success 'general options plus command' '
1312 test_completion "git --version check" "checkout " &&
1313 test_completion "git --paginate check" "checkout " &&
1314 test_completion "git --git-dir=foo check" "checkout " &&
1315 test_completion "git --bare check" "checkout " &&
1316 test_completion "git --exec-path=foo check" "checkout " &&
1317 test_completion "git --html-path check" "checkout " &&
1318 test_completion "git --no-pager check" "checkout " &&
1319 test_completion "git --work-tree=foo check" "checkout " &&
1320 test_completion "git --namespace=foo check" "checkout " &&
1321 test_completion "git --paginate check" "checkout " &&
1322 test_completion "git --info-path check" "checkout " &&
1323 test_completion "git --no-replace-objects check" "checkout " &&
1324 test_completion "git --git-dir some/path check" "checkout " &&
1325 test_completion "git -c conf.var=value check" "checkout " &&
1326 test_completion "git -C some/path check" "checkout " &&
1327 test_completion "git --work-tree some/path check" "checkout " &&
1328 test_completion "git --namespace name/space check" "checkout "
1329'
1330
1331test_expect_success 'git --help completion' '
1332 test_completion "git --help ad" "add " &&
1333 test_completion "git --help core" "core-tutorial "
1334'
1335
1336test_expect_success 'setup for integration tests' '
1337 echo content >file1 &&
1338 echo more >file2 &&
1339 git add file1 file2 &&
1340 git commit -m one &&
1341 git branch mybranch &&
1342 git tag mytag
1343'
1344
1345test_expect_success 'checkout completes ref names' '
1346 test_completion "git checkout m" <<-\EOF
1347 master Z
1348 mybranch Z
1349 mytag Z
1350 EOF
1351'
1352
1353test_expect_success 'git -C <path> checkout uses the right repo' '
1354 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
1355 branch-in-other Z
1356 EOF
1357'
1358
1359test_expect_success 'show completes all refs' '
1360 test_completion "git show m" <<-\EOF
1361 master Z
1362 mybranch Z
1363 mytag Z
1364 EOF
1365'
1366
1367test_expect_success '<ref>: completes paths' '
1368 test_completion "git show mytag:f" <<-\EOF
1369 file1 Z
1370 file2 Z
1371 EOF
1372'
1373
1374test_expect_success 'complete tree filename with spaces' '
1375 echo content >"name with spaces" &&
1376 git add "name with spaces" &&
1377 git commit -m spaces &&
1378 test_completion "git show HEAD:nam" <<-\EOF
1379 name with spaces Z
1380 EOF
1381'
1382
1383test_expect_success 'complete tree filename with metacharacters' '
1384 echo content >"name with \${meta}" &&
1385 git add "name with \${meta}" &&
1386 git commit -m meta &&
1387 test_completion "git show HEAD:nam" <<-\EOF
1388 name with ${meta} Z
1389 name with spaces Z
1390 EOF
1391'
1392
1393test_expect_success 'send-email' '
1394 test_completion "git send-email --cov" "--cover-letter " &&
1395 test_completion "git send-email ma" "master "
1396'
1397
1398test_expect_success 'complete files' '
1399 git init tmp && cd tmp &&
1400 test_when_finished "cd .. && rm -rf tmp" &&
1401
1402 echo "expected" > .gitignore &&
1403 echo "out" >> .gitignore &&
1404
1405 git add .gitignore &&
1406 test_completion "git commit " ".gitignore" &&
1407
1408 git commit -m ignore &&
1409
1410 touch new &&
1411 test_completion "git add " "new" &&
1412
1413 git add new &&
1414 git commit -a -m new &&
1415 test_completion "git add " "" &&
1416
1417 git mv new modified &&
1418 echo modify > modified &&
1419 test_completion "git add " "modified" &&
1420
1421 touch untracked &&
1422
1423 : TODO .gitignore should not be here &&
1424 test_completion "git rm " <<-\EOF &&
1425 .gitignore
1426 modified
1427 EOF
1428
1429 test_completion "git clean " "untracked" &&
1430
1431 : TODO .gitignore should not be here &&
1432 test_completion "git mv " <<-\EOF &&
1433 .gitignore
1434 modified
1435 EOF
1436
1437 mkdir dir &&
1438 touch dir/file-in-dir &&
1439 git add dir/file-in-dir &&
1440 git commit -m dir &&
1441
1442 mkdir untracked-dir &&
1443
1444 : TODO .gitignore should not be here &&
1445 test_completion "git mv modified " <<-\EOF &&
1446 .gitignore
1447 dir
1448 modified
1449 untracked
1450 untracked-dir
1451 EOF
1452
1453 test_completion "git commit " "modified" &&
1454
1455 : TODO .gitignore should not be here &&
1456 test_completion "git ls-files " <<-\EOF &&
1457 .gitignore
1458 dir
1459 modified
1460 EOF
1461
1462 touch momified &&
1463 test_completion "git add mom" "momified"
1464'
1465
1466test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
1467 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
1468 test_completion "git co m" <<-\EOF
1469 master Z
1470 mybranch Z
1471 mytag Z
1472 EOF
1473'
1474
1475test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
1476 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
1477 test_completion "git co m" <<-\EOF
1478 master Z
1479 mybranch Z
1480 mytag Z
1481 EOF
1482'
1483
1484test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
1485 test_config alias.co "!f() { : git checkout ; if ... } f" &&
1486 test_completion "git co m" <<-\EOF
1487 master Z
1488 mybranch Z
1489 mytag Z
1490 EOF
1491'
1492
1493test_expect_success 'completion without explicit _git_xxx function' '
1494 test_completion "git version --" <<-\EOF
1495 --build-options Z
1496 --no-build-options Z
1497 EOF
1498'
1499
1500test_expect_failure 'complete with tilde expansion' '
1501 git init tmp && cd tmp &&
1502 test_when_finished "cd .. && rm -rf tmp" &&
1503
1504 touch ~/tmp/file &&
1505
1506 test_completion "git add ~/tmp/" "~/tmp/file"
1507'
1508
1509test_expect_success 'setup other remote for remote reference completion' '
1510 git remote add other otherrepo &&
1511 git fetch other
1512'
1513
1514for flag in -d --delete
1515do
1516 test_expect_success "__git_complete_remote_or_refspec - push $flag other" '
1517 sed -e "s/Z$//" >expected <<-EOF &&
1518 master-in-other Z
1519 EOF
1520 (
1521 words=(git push '$flag' other ma) &&
1522 cword=${#words[@]} cur=${words[cword-1]} &&
1523 __git_complete_remote_or_refspec &&
1524 print_comp
1525 ) &&
1526 test_cmp expected out
1527 '
1528
1529 test_expect_failure "__git_complete_remote_or_refspec - push other $flag" '
1530 sed -e "s/Z$//" >expected <<-EOF &&
1531 master-in-other Z
1532 EOF
1533 (
1534 words=(git push other '$flag' ma) &&
1535 cword=${#words[@]} cur=${words[cword-1]} &&
1536 __git_complete_remote_or_refspec &&
1537 print_comp
1538 ) &&
1539 test_cmp expected out
1540 '
1541done
1542
1543test_expect_success 'sourcing the completion script clears cached commands' '
1544 __git_compute_all_commands &&
1545 verbose test -n "$__git_all_commands" &&
1546 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1547 verbose test -z "$__git_all_commands"
1548'
1549
1550test_expect_success 'sourcing the completion script clears cached porcelain commands' '
1551 __git_compute_porcelain_commands &&
1552 verbose test -n "$__git_porcelain_commands" &&
1553 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1554 verbose test -z "$__git_porcelain_commands"
1555'
1556
1557test_expect_success !GETTEXT_POISON 'sourcing the completion script clears cached merge strategies' '
1558 __git_compute_merge_strategies &&
1559 verbose test -n "$__git_merge_strategies" &&
1560 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1561 verbose test -z "$__git_merge_strategies"
1562'
1563
1564test_expect_success 'sourcing the completion script clears cached --options' '
1565 __gitcomp_builtin checkout &&
1566 verbose test -n "$__gitcomp_builtin_checkout" &&
1567 __gitcomp_builtin notes_edit &&
1568 verbose test -n "$__gitcomp_builtin_notes_edit" &&
1569 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
1570 verbose test -z "$__gitcomp_builtin_checkout" &&
1571 verbose test -z "$__gitcomp_builtin_notes_edit"
1572'
1573
1574test_done