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