]> git.ipfire.org Git - thirdparty/git.git/blame - t/t9902-completion.sh
The third batch
[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
6807d394
PS
8# The Bash completion scripts must not print anything to either stdout or
9# stderr, which we try to verify. When tracing is enabled without support for
10# BASH_XTRACEFD this assertion will fail, so we have to mark the test as
11# untraceable with such ancient Bash versions.
12test_untraceable=UnfortunatelyYes
13
db489ea4
BLK
14# Override environment and always use master for the default initial branch
15# name for these tests, so that rev completion candidates are as expected.
16GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=master
17export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
18
f8891cfa 19. ./lib-bash.sh
5c293a6b
FC
20
21complete ()
22{
23 # do nothing
24 return 0
25}
26
84a97131 27# Be careful when updating these lists:
50478223
JH
28#
29# (1) The build tree may have build artifact from different branch, or
30# the user's $PATH may have a random executable that may begin
31# with "git-check" that are not part of the subcommands this build
32# will ship, e.g. "check-ignore". The tests for completion for
33# subcommand names tests how "check" is expanded; we limit the
34# possible candidates to "checkout" and "check-attr" to make sure
35# "check-attr", which is known by the filter function as a
36# subcommand to be thrown out, while excluding other random files
37# that happen to begin with "check" to avoid letting them get in
38# the way.
39#
40# (2) A test makes sure that common subcommands are included in the
41# completion for "git <TAB>", and a plumbing is excluded. "add",
483e8611 42# "rebase" and "ls-files" are listed for this.
50478223 43
483e8611
EN
44GIT_TESTING_ALL_COMMAND_LIST='add checkout check-attr rebase ls-files'
45GIT_TESTING_PORCELAIN_COMMAND_LIST='add checkout rebase'
50478223 46
5c293a6b
FC
47. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
48
49# We don't need this function to actually join words or do anything special.
50# Also, it's cleaner to avoid touching bash's internal completion variables.
51# So let's override it with a minimal version for testing purposes.
52_get_comp_words_by_ref ()
53{
54 while [ $# -gt 0 ]; do
55 case "$1" in
56 cur)
57 cur=${_words[_cword]}
58 ;;
59 prev)
60 prev=${_words[_cword-1]}
61 ;;
62 words)
63 words=("${_words[@]}")
64 ;;
65 cword)
66 cword=$_cword
67 ;;
68 esac
69 shift
70 done
71}
72
73print_comp ()
74{
75 local IFS=$'\n'
76 echo "${COMPREPLY[*]}" > out
77}
78
79run_completion ()
80{
81 local -a COMPREPLY _words
82 local _cword
83 _words=( $1 )
0ef09702 84 test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
5c293a6b 85 (( _cword = ${#_words[@]} - 1 ))
93b291e0 86 __git_wrap__git_main && print_comp
5c293a6b
FC
87}
88
701ecdf1
FC
89# Test high-level completion
90# Arguments are:
91# 1: typed text so far (cur)
92# 2: expected completion
5c293a6b
FC
93test_completion ()
94{
2fbaf813
FC
95 if test $# -gt 1
96 then
97 printf '%s\n' "$2" >expected
98 else
9703797c 99 sed -e 's/Z$//' |sort >expected
2fbaf813 100 fi &&
6807d394 101 run_completion "$1" >"$TRASH_DIRECTORY"/bash-completion-output 2>&1 &&
9703797c 102 sort out >out_sorted &&
6807d394
PS
103 test_cmp expected out_sorted &&
104 test_must_be_empty "$TRASH_DIRECTORY"/bash-completion-output &&
105 rm "$TRASH_DIRECTORY"/bash-completion-output
5c293a6b
FC
106}
107
e4615238
FC
108# Test __gitcomp.
109# The first argument is the typed text so far (cur); the rest are
110# passed to __gitcomp. Expected output comes is read from the
111# standard input, like test_completion().
112test_gitcomp ()
49ba92b4 113{
17393033 114 local -a COMPREPLY &&
e4615238 115 sed -e 's/Z$//' >expected &&
eac90623 116 local cur="$1" &&
17393033
FC
117 shift &&
118 __gitcomp "$@" &&
119 print_comp &&
74a8c849 120 test_cmp expected out
49ba92b4
JK
121}
122
43369a22
FC
123# Test __gitcomp_nl
124# Arguments are:
125# 1: current word (cur)
126# -: the rest are passed to __gitcomp_nl
127test_gitcomp_nl ()
128{
129 local -a COMPREPLY &&
130 sed -e 's/Z$//' >expected &&
eac90623 131 local cur="$1" &&
43369a22
FC
132 shift &&
133 __gitcomp_nl "$@" &&
134 print_comp &&
135 test_cmp expected out
136}
137
138invalid_variable_name='${foo.bar}'
139
c9a102e8
SG
140actual="$TRASH_DIRECTORY/actual"
141
f6114408
SG
142if test_have_prereq MINGW
143then
144 ROOT="$(pwd -W)"
145else
146 ROOT="$(pwd)"
147fi
148
fad9484f 149test_expect_success 'setup for __git_find_repo_path/__gitdir tests' '
c9a102e8 150 mkdir -p subdir/subsubdir &&
fad9484f 151 mkdir -p non-repo &&
00076181 152 git init -b main otherrepo
c9a102e8
SG
153'
154
fad9484f 155test_expect_success '__git_find_repo_path - from command line (through $__git_dir)' '
f6114408 156 echo "$ROOT/otherrepo/.git" >expected &&
c9a102e8 157 (
f6114408 158 __git_dir="$ROOT/otherrepo/.git" &&
fad9484f
SG
159 __git_find_repo_path &&
160 echo "$__git_repo_path" >"$actual"
beb6ee71 161 ) &&
c9a102e8
SG
162 test_cmp expected "$actual"
163'
164
fad9484f 165test_expect_success '__git_find_repo_path - .git directory in cwd' '
c9a102e8 166 echo ".git" >expected &&
beb6ee71 167 (
fad9484f
SG
168 __git_find_repo_path &&
169 echo "$__git_repo_path" >"$actual"
beb6ee71 170 ) &&
c9a102e8
SG
171 test_cmp expected "$actual"
172'
173
fad9484f 174test_expect_success '__git_find_repo_path - .git directory in parent' '
f6114408 175 echo "$ROOT/.git" >expected &&
c9a102e8
SG
176 (
177 cd subdir/subsubdir &&
fad9484f
SG
178 __git_find_repo_path &&
179 echo "$__git_repo_path" >"$actual"
c9a102e8
SG
180 ) &&
181 test_cmp expected "$actual"
182'
183
fad9484f 184test_expect_success '__git_find_repo_path - cwd is a .git directory' '
c9a102e8
SG
185 echo "." >expected &&
186 (
187 cd .git &&
fad9484f
SG
188 __git_find_repo_path &&
189 echo "$__git_repo_path" >"$actual"
c9a102e8
SG
190 ) &&
191 test_cmp expected "$actual"
192'
193
fad9484f 194test_expect_success '__git_find_repo_path - parent is a .git directory' '
f6114408 195 echo "$ROOT/.git" >expected &&
c9a102e8 196 (
5340d471 197 cd .git/objects &&
fad9484f
SG
198 __git_find_repo_path &&
199 echo "$__git_repo_path" >"$actual"
c9a102e8
SG
200 ) &&
201 test_cmp expected "$actual"
202'
203
fad9484f 204test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in cwd' '
f6114408 205 echo "$ROOT/otherrepo/.git" >expected &&
c9a102e8 206 (
f6114408 207 GIT_DIR="$ROOT/otherrepo/.git" &&
c9a102e8 208 export GIT_DIR &&
fad9484f
SG
209 __git_find_repo_path &&
210 echo "$__git_repo_path" >"$actual"
c9a102e8
SG
211 ) &&
212 test_cmp expected "$actual"
213'
214
fad9484f 215test_expect_success '__git_find_repo_path - $GIT_DIR set while .git directory in parent' '
f6114408 216 echo "$ROOT/otherrepo/.git" >expected &&
c9a102e8 217 (
f6114408 218 GIT_DIR="$ROOT/otherrepo/.git" &&
c9a102e8
SG
219 export GIT_DIR &&
220 cd subdir &&
fad9484f
SG
221 __git_find_repo_path &&
222 echo "$__git_repo_path" >"$actual"
c9a102e8
SG
223 ) &&
224 test_cmp expected "$actual"
225'
226
fad9484f 227test_expect_success '__git_find_repo_path - from command line while "git -C"' '
80ac0744
SG
228 echo "$ROOT/.git" >expected &&
229 (
230 __git_dir="$ROOT/.git" &&
231 __git_C_args=(-C otherrepo) &&
fad9484f
SG
232 __git_find_repo_path &&
233 echo "$__git_repo_path" >"$actual"
80ac0744
SG
234 ) &&
235 test_cmp expected "$actual"
236'
237
fad9484f 238test_expect_success '__git_find_repo_path - relative dir from command line and "git -C"' '
80ac0744
SG
239 echo "$ROOT/otherrepo/.git" >expected &&
240 (
241 cd subdir &&
242 __git_dir="otherrepo/.git" &&
243 __git_C_args=(-C ..) &&
fad9484f
SG
244 __git_find_repo_path &&
245 echo "$__git_repo_path" >"$actual"
80ac0744
SG
246 ) &&
247 test_cmp expected "$actual"
248'
249
fad9484f 250test_expect_success '__git_find_repo_path - $GIT_DIR set while "git -C"' '
80ac0744
SG
251 echo "$ROOT/.git" >expected &&
252 (
253 GIT_DIR="$ROOT/.git" &&
254 export GIT_DIR &&
255 __git_C_args=(-C otherrepo) &&
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 - relative dir in $GIT_DIR and "git -C"' '
80ac0744
SG
263 echo "$ROOT/otherrepo/.git" >expected &&
264 (
265 cd subdir &&
266 GIT_DIR="otherrepo/.git" &&
267 export GIT_DIR &&
268 __git_C_args=(-C ..) &&
fad9484f
SG
269 __git_find_repo_path &&
270 echo "$__git_repo_path" >"$actual"
80ac0744
SG
271 ) &&
272 test_cmp expected "$actual"
273'
274
fad9484f 275test_expect_success '__git_find_repo_path - "git -C" while .git directory in cwd' '
80ac0744
SG
276 echo "$ROOT/otherrepo/.git" >expected &&
277 (
278 __git_C_args=(-C otherrepo) &&
fad9484f
SG
279 __git_find_repo_path &&
280 echo "$__git_repo_path" >"$actual"
80ac0744
SG
281 ) &&
282 test_cmp expected "$actual"
283'
284
fad9484f 285test_expect_success '__git_find_repo_path - "git -C" while cwd is a .git directory' '
80ac0744
SG
286 echo "$ROOT/otherrepo/.git" >expected &&
287 (
288 cd .git &&
289 __git_C_args=(-C .. -C otherrepo) &&
fad9484f
SG
290 __git_find_repo_path &&
291 echo "$__git_repo_path" >"$actual"
80ac0744
SG
292 ) &&
293 test_cmp expected "$actual"
294'
295
fad9484f 296test_expect_success '__git_find_repo_path - "git -C" while .git directory in parent' '
80ac0744
SG
297 echo "$ROOT/otherrepo/.git" >expected &&
298 (
299 cd subdir &&
300 __git_C_args=(-C .. -C otherrepo) &&
fad9484f
SG
301 __git_find_repo_path &&
302 echo "$__git_repo_path" >"$actual"
80ac0744
SG
303 ) &&
304 test_cmp expected "$actual"
305'
306
fad9484f 307test_expect_success '__git_find_repo_path - non-existing path in "git -C"' '
80ac0744
SG
308 (
309 __git_C_args=(-C non-existing) &&
fad9484f
SG
310 test_must_fail __git_find_repo_path &&
311 printf "$__git_repo_path" >"$actual"
80ac0744
SG
312 ) &&
313 test_must_be_empty "$actual"
314'
315
fad9484f 316test_expect_success '__git_find_repo_path - non-existing path in $__git_dir' '
a2f03b0e
SG
317 (
318 __git_dir="non-existing" &&
fad9484f
SG
319 test_must_fail __git_find_repo_path &&
320 printf "$__git_repo_path" >"$actual"
a2f03b0e
SG
321 ) &&
322 test_must_be_empty "$actual"
323'
324
fad9484f 325test_expect_success '__git_find_repo_path - non-existing $GIT_DIR' '
c9a102e8 326 (
f6114408 327 GIT_DIR="$ROOT/non-existing" &&
c9a102e8 328 export GIT_DIR &&
fad9484f
SG
329 test_must_fail __git_find_repo_path &&
330 printf "$__git_repo_path" >"$actual"
8f0fa85d
SG
331 ) &&
332 test_must_be_empty "$actual"
c9a102e8
SG
333'
334
fad9484f 335test_expect_success '__git_find_repo_path - gitfile in cwd' '
f6114408
SG
336 echo "$ROOT/otherrepo/.git" >expected &&
337 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
c9a102e8
SG
338 test_when_finished "rm -f subdir/.git" &&
339 (
340 cd subdir &&
fad9484f
SG
341 __git_find_repo_path &&
342 echo "$__git_repo_path" >"$actual"
c9a102e8
SG
343 ) &&
344 test_cmp expected "$actual"
345'
346
fad9484f 347test_expect_success '__git_find_repo_path - gitfile in parent' '
f6114408
SG
348 echo "$ROOT/otherrepo/.git" >expected &&
349 echo "gitdir: $ROOT/otherrepo/.git" >subdir/.git &&
c9a102e8
SG
350 test_when_finished "rm -f subdir/.git" &&
351 (
352 cd subdir/subsubdir &&
fad9484f
SG
353 __git_find_repo_path &&
354 echo "$__git_repo_path" >"$actual"
c9a102e8
SG
355 ) &&
356 test_cmp expected "$actual"
357'
358
fad9484f 359test_expect_success SYMLINKS '__git_find_repo_path - resulting path avoids symlinks' '
f6114408 360 echo "$ROOT/otherrepo/.git" >expected &&
c9a102e8
SG
361 mkdir otherrepo/dir &&
362 test_when_finished "rm -rf otherrepo/dir" &&
363 ln -s otherrepo/dir link &&
364 test_when_finished "rm -f link" &&
365 (
366 cd link &&
fad9484f
SG
367 __git_find_repo_path &&
368 echo "$__git_repo_path" >"$actual"
369 ) &&
370 test_cmp expected "$actual"
371'
372
373test_expect_success '__git_find_repo_path - not a git repository' '
374 (
375 cd non-repo &&
376 GIT_CEILING_DIRECTORIES="$ROOT" &&
377 export GIT_CEILING_DIRECTORIES &&
378 test_must_fail __git_find_repo_path &&
379 printf "$__git_repo_path" >"$actual"
380 ) &&
381 test_must_be_empty "$actual"
382'
383
384test_expect_success '__gitdir - finds repo' '
385 echo "$ROOT/.git" >expected &&
386 (
387 cd subdir/subsubdir &&
c9a102e8
SG
388 __gitdir >"$actual"
389 ) &&
390 test_cmp expected "$actual"
391'
392
fad9484f 393
4dc8b1c1 394test_expect_success '__gitdir - returns error when cannot find repo' '
fad9484f
SG
395 (
396 __git_dir="non-existing" &&
397 test_must_fail __gitdir >"$actual"
398 ) &&
8f0fa85d 399 test_must_be_empty "$actual"
c9a102e8
SG
400'
401
fad9484f
SG
402test_expect_success '__gitdir - repo as argument' '
403 echo "otherrepo/.git" >expected &&
404 (
405 __gitdir "otherrepo" >"$actual"
406 ) &&
407 test_cmp expected "$actual"
408'
409
410test_expect_success '__gitdir - remote as argument' '
411 echo "remote" >expected &&
412 (
413 __gitdir "remote" >"$actual"
414 ) &&
415 test_cmp expected "$actual"
416'
417
f12785a3
SG
418
419test_expect_success '__git_dequote - plain unquoted word' '
420 __git_dequote unquoted-word &&
8ddfce71 421 test unquoted-word = "$dequoted_word"
f12785a3
SG
422'
423
424# input: b\a\c\k\'\\\"s\l\a\s\h\es
425# expected: back'\"slashes
426test_expect_success '__git_dequote - backslash escaped' '
427 __git_dequote "b\a\c\k\\'\''\\\\\\\"s\l\a\s\h\es" &&
8ddfce71 428 test "back'\''\\\"slashes" = "$dequoted_word"
f12785a3
SG
429'
430
431# input: sin'gle\' '"quo'ted
432# expected: single\ "quoted
433test_expect_success '__git_dequote - single quoted' '
434 __git_dequote "'"sin'gle\\\\' '\\\"quo'ted"'" &&
8ddfce71 435 test '\''single\ "quoted'\'' = "$dequoted_word"
f12785a3
SG
436'
437
438# input: dou"ble\\" "\"\quot"ed
439# expected: double\ "\quoted
440test_expect_success '__git_dequote - double quoted' '
441 __git_dequote '\''dou"ble\\" "\"\quot"ed'\'' &&
8ddfce71 442 test '\''double\ "\quoted'\'' = "$dequoted_word"
f12785a3
SG
443'
444
445# input: 'open single quote
446test_expect_success '__git_dequote - open single quote' '
447 __git_dequote "'\''open single quote" &&
8ddfce71 448 test "open single quote" = "$dequoted_word"
f12785a3
SG
449'
450
451# input: "open double quote
452test_expect_success '__git_dequote - open double quote' '
453 __git_dequote "\"open double quote" &&
8ddfce71 454 test "open double quote" = "$dequoted_word"
f12785a3
SG
455'
456
457
fef56eb0
SG
458test_expect_success '__gitcomp_direct - puts everything into COMPREPLY as-is' '
459 sed -e "s/Z$//g" >expected <<-EOF &&
460 with-trailing-space Z
461 without-trailing-spaceZ
462 --option Z
463 --option=Z
464 $invalid_variable_name Z
465 EOF
466 (
467 cur=should_be_ignored &&
468 __gitcomp_direct "$(cat expected)" &&
469 print_comp
470 ) &&
471 test_cmp expected out
472'
473
74a8c849 474test_expect_success '__gitcomp - trailing space - options' '
e4615238
FC
475 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
476 --reset-author" <<-EOF
74a8c849
SG
477 --reuse-message=Z
478 --reedit-message=Z
479 --reset-author Z
480 EOF
74a8c849
SG
481'
482
483test_expect_success '__gitcomp - trailing space - config keys' '
e4615238
FC
484 test_gitcomp "br" "branch. branch.autosetupmerge
485 branch.autosetuprebase browser." <<-\EOF
74a8c849
SG
486 branch.Z
487 branch.autosetupmerge Z
488 branch.autosetuprebase Z
489 browser.Z
490 EOF
74a8c849
SG
491'
492
493test_expect_success '__gitcomp - option parameter' '
e4615238
FC
494 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
495 "" "re" <<-\EOF
74a8c849
SG
496 recursive Z
497 resolve Z
498 EOF
74a8c849
SG
499'
500
501test_expect_success '__gitcomp - prefix' '
e4615238
FC
502 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
503 "branch.maint." "me" <<-\EOF
74a8c849
SG
504 branch.maint.merge Z
505 branch.maint.mergeoptions Z
506 EOF
74a8c849
SG
507'
508
509test_expect_success '__gitcomp - suffix' '
6dca5dbf 510 test_gitcomp "branch.me" "master maint next seen" "branch." \
e4615238 511 "ma" "." <<-\EOF
74a8c849
SG
512 branch.master.Z
513 branch.maint.Z
514 EOF
74a8c849
SG
515'
516
b221b5ab
NTND
517test_expect_success '__gitcomp - ignore optional negative options' '
518 test_gitcomp "--" "--abc --def --no-one -- --no-two" <<-\EOF
519 --abc Z
520 --def Z
521 --no-one Z
522 --no-... Z
523 EOF
524'
525
526test_expect_success '__gitcomp - ignore/narrow optional negative options' '
527 test_gitcomp "--a" "--abc --abcdef --no-one -- --no-two" <<-\EOF
528 --abc Z
529 --abcdef Z
530 EOF
531'
532
533test_expect_success '__gitcomp - ignore/narrow optional negative options' '
534 test_gitcomp "--n" "--abc --def --no-one -- --no-two" <<-\EOF
535 --no-one Z
536 --no-... Z
537 EOF
538'
539
540test_expect_success '__gitcomp - expand all negative options' '
541 test_gitcomp "--no-" "--abc --def --no-one -- --no-two" <<-\EOF
542 --no-one Z
543 --no-two Z
544 EOF
545'
546
547test_expect_success '__gitcomp - expand/narrow all negative options' '
548 test_gitcomp "--no-o" "--abc --def --no-one -- --no-two" <<-\EOF
549 --no-one Z
550 EOF
551'
552
e9f2118d
FC
553test_expect_success '__gitcomp - equal skip' '
554 test_gitcomp "--option=" "--option=" <<-\EOF &&
555
556 EOF
557 test_gitcomp "option=" "option=" <<-\EOF
558
559 EOF
560'
561
7d13e0a3 562test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
43369a22
FC
563 __gitcomp "$invalid_variable_name"
564'
565
566read -r -d "" refs <<-\EOF
f33f2d3d 567main
43369a22 568maint
43369a22 569next
6dca5dbf 570seen
43369a22
FC
571EOF
572
573test_expect_success '__gitcomp_nl - trailing space' '
574 test_gitcomp_nl "m" "$refs" <<-EOF
f33f2d3d 575 main Z
43369a22 576 maint Z
43369a22
FC
577 EOF
578'
579
580test_expect_success '__gitcomp_nl - prefix' '
581 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
f33f2d3d 582 --fixup=main Z
43369a22 583 --fixup=maint Z
43369a22
FC
584 EOF
585'
586
587test_expect_success '__gitcomp_nl - suffix' '
588 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
f33f2d3d 589 branch.main.Z
43369a22 590 branch.maint.Z
43369a22
FC
591 EOF
592'
593
594test_expect_success '__gitcomp_nl - no suffix' '
595 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
f33f2d3d 596 mainZ
43369a22 597 maintZ
43369a22
FC
598 EOF
599'
600
7d13e0a3 601test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
43369a22
FC
602 __gitcomp_nl "$invalid_variable_name"
603'
604
2acc1940
SG
605test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
606 cat >expect <<-EOF &&
607 remote_from_file_1
608 remote_from_file_2
609 remote_in_config_1
610 remote_in_config_2
611 EOF
612 test_when_finished "rm -rf .git/remotes" &&
613 mkdir -p .git/remotes &&
614 >.git/remotes/remote_from_file_1 &&
615 >.git/remotes/remote_from_file_2 &&
616 test_when_finished "git remote remove remote_in_config_1" &&
617 git remote add remote_in_config_1 git://remote_1 &&
618 test_when_finished "git remote remove remote_in_config_2" &&
619 git remote add remote_in_config_2 git://remote_2 &&
beb6ee71
SG
620 (
621 __git_remotes >actual
622 ) &&
2acc1940
SG
623 test_cmp expect actual
624'
625
69a77596
SG
626test_expect_success '__git_is_configured_remote' '
627 test_when_finished "git remote remove remote_1" &&
628 git remote add remote_1 git://remote_1 &&
629 test_when_finished "git remote remove remote_2" &&
630 git remote add remote_2 git://remote_2 &&
beb6ee71 631 (
8ddfce71 632 __git_is_configured_remote remote_2 &&
beb6ee71
SG
633 test_must_fail __git_is_configured_remote non-existent
634 )
69a77596
SG
635'
636
fb9cd420
SG
637test_expect_success 'setup for ref completion' '
638 git commit --allow-empty -m initial &&
00076181 639 git branch -M main &&
fb9cd420
SG
640 git branch matching-branch &&
641 git tag matching-tag &&
642 (
643 cd otherrepo &&
644 git commit --allow-empty -m initial &&
00076181 645 git branch -m main main-in-other &&
fb9cd420
SG
646 git branch branch-in-other &&
647 git tag tag-in-other
648 ) &&
649 git remote add other "$ROOT/otherrepo/.git" &&
650 git fetch --no-tags other &&
651 rm -f .git/FETCH_HEAD &&
652 git init thirdrepo
653'
654
655test_expect_success '__git_refs - simple' '
656 cat >expected <<-EOF &&
657 HEAD
00076181 658 main
fb9cd420
SG
659 matching-branch
660 other/branch-in-other
00076181 661 other/main-in-other
fb9cd420
SG
662 matching-tag
663 EOF
664 (
665 cur= &&
666 __git_refs >"$actual"
667 ) &&
668 test_cmp expected "$actual"
669'
670
671test_expect_success '__git_refs - full refs' '
672 cat >expected <<-EOF &&
00076181 673 refs/heads/main
fb9cd420 674 refs/heads/matching-branch
e896369b 675 refs/remotes/other/branch-in-other
00076181 676 refs/remotes/other/main-in-other
e896369b 677 refs/tags/matching-tag
fb9cd420
SG
678 EOF
679 (
680 cur=refs/heads/ &&
681 __git_refs >"$actual"
682 ) &&
683 test_cmp expected "$actual"
684'
685
686test_expect_success '__git_refs - repo given on the command line' '
687 cat >expected <<-EOF &&
688 HEAD
689 branch-in-other
00076181 690 main-in-other
fb9cd420
SG
691 tag-in-other
692 EOF
693 (
694 __git_dir="$ROOT/otherrepo/.git" &&
695 cur= &&
696 __git_refs >"$actual"
697 ) &&
698 test_cmp expected "$actual"
699'
700
701test_expect_success '__git_refs - remote on local file system' '
702 cat >expected <<-EOF &&
703 HEAD
704 branch-in-other
00076181 705 main-in-other
fb9cd420
SG
706 tag-in-other
707 EOF
708 (
709 cur= &&
710 __git_refs otherrepo >"$actual"
711 ) &&
712 test_cmp expected "$actual"
713'
714
715test_expect_success '__git_refs - remote on local file system - full refs' '
716 cat >expected <<-EOF &&
717 refs/heads/branch-in-other
00076181 718 refs/heads/main-in-other
fb9cd420
SG
719 refs/tags/tag-in-other
720 EOF
721 (
722 cur=refs/ &&
723 __git_refs otherrepo >"$actual"
724 ) &&
725 test_cmp expected "$actual"
726'
727
728test_expect_success '__git_refs - configured remote' '
729 cat >expected <<-EOF &&
730 HEAD
731 branch-in-other
00076181 732 main-in-other
fb9cd420
SG
733 EOF
734 (
735 cur= &&
736 __git_refs other >"$actual"
737 ) &&
738 test_cmp expected "$actual"
739'
740
741test_expect_success '__git_refs - configured remote - full refs' '
742 cat >expected <<-EOF &&
e896369b 743 HEAD
fb9cd420 744 refs/heads/branch-in-other
00076181 745 refs/heads/main-in-other
fb9cd420
SG
746 refs/tags/tag-in-other
747 EOF
748 (
749 cur=refs/ &&
750 __git_refs other >"$actual"
751 ) &&
752 test_cmp expected "$actual"
753'
754
5c12f642 755test_expect_success '__git_refs - configured remote - repo given on the command line' '
fb9cd420
SG
756 cat >expected <<-EOF &&
757 HEAD
758 branch-in-other
00076181 759 main-in-other
fb9cd420
SG
760 EOF
761 (
762 cd thirdrepo &&
763 __git_dir="$ROOT/.git" &&
764 cur= &&
765 __git_refs other >"$actual"
766 ) &&
767 test_cmp expected "$actual"
768'
769
5c12f642 770test_expect_success '__git_refs - configured remote - full refs - repo given on the command line' '
fb9cd420 771 cat >expected <<-EOF &&
e896369b 772 HEAD
fb9cd420 773 refs/heads/branch-in-other
00076181 774 refs/heads/main-in-other
fb9cd420
SG
775 refs/tags/tag-in-other
776 EOF
777 (
778 cd thirdrepo &&
779 __git_dir="$ROOT/.git" &&
780 cur=refs/ &&
781 __git_refs other >"$actual"
782 ) &&
783 test_cmp expected "$actual"
784'
785
69a77596 786test_expect_success '__git_refs - configured remote - remote name matches a directory' '
fb9cd420
SG
787 cat >expected <<-EOF &&
788 HEAD
789 branch-in-other
00076181 790 main-in-other
fb9cd420
SG
791 EOF
792 mkdir other &&
793 test_when_finished "rm -rf other" &&
794 (
795 cur= &&
796 __git_refs other >"$actual"
797 ) &&
798 test_cmp expected "$actual"
799'
800
91b7ea81 801test_expect_success '__git_refs - URL remote' '
fb9cd420
SG
802 cat >expected <<-EOF &&
803 HEAD
804 branch-in-other
00076181 805 main-in-other
fb9cd420
SG
806 tag-in-other
807 EOF
808 (
809 cur= &&
810 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
811 ) &&
812 test_cmp expected "$actual"
813'
814
815test_expect_success '__git_refs - URL remote - full refs' '
816 cat >expected <<-EOF &&
e896369b 817 HEAD
fb9cd420 818 refs/heads/branch-in-other
00076181 819 refs/heads/main-in-other
fb9cd420
SG
820 refs/tags/tag-in-other
821 EOF
822 (
823 cur=refs/ &&
824 __git_refs "file://$ROOT/otherrepo/.git" >"$actual"
825 ) &&
826 test_cmp expected "$actual"
827'
828
91b7ea81 829test_expect_success '__git_refs - non-existing remote' '
fb9cd420
SG
830 (
831 cur= &&
832 __git_refs non-existing >"$actual"
833 ) &&
834 test_must_be_empty "$actual"
835'
836
837test_expect_success '__git_refs - non-existing remote - full refs' '
838 (
839 cur=refs/ &&
840 __git_refs non-existing >"$actual"
841 ) &&
842 test_must_be_empty "$actual"
843'
844
91b7ea81 845test_expect_success '__git_refs - non-existing URL remote' '
fb9cd420
SG
846 (
847 cur= &&
848 __git_refs "file://$ROOT/non-existing" >"$actual"
849 ) &&
850 test_must_be_empty "$actual"
851'
852
853test_expect_success '__git_refs - non-existing URL remote - full refs' '
854 (
855 cur=refs/ &&
856 __git_refs "file://$ROOT/non-existing" >"$actual"
857 ) &&
858 test_must_be_empty "$actual"
859'
860
62a1b732 861test_expect_success '__git_refs - not in a git repository' '
fb9cd420
SG
862 (
863 GIT_CEILING_DIRECTORIES="$ROOT" &&
864 export GIT_CEILING_DIRECTORIES &&
865 cd subdir &&
866 cur= &&
867 __git_refs >"$actual"
868 ) &&
869 test_must_be_empty "$actual"
870'
871
872test_expect_success '__git_refs - unique remote branches for git checkout DWIMery' '
873 cat >expected <<-EOF &&
874 HEAD
00076181 875 main
fb9cd420
SG
876 matching-branch
877 other/ambiguous
878 other/branch-in-other
00076181 879 other/main-in-other
fb9cd420
SG
880 remote/ambiguous
881 remote/branch-in-remote
882 matching-tag
883 branch-in-other
884 branch-in-remote
00076181 885 main-in-other
fb9cd420
SG
886 EOF
887 for remote_ref in refs/remotes/other/ambiguous \
888 refs/remotes/remote/ambiguous \
889 refs/remotes/remote/branch-in-remote
890 do
00076181 891 git update-ref $remote_ref main &&
0c51d6b4 892 test_when_finished "git update-ref -d $remote_ref" || return 1
fb9cd420
SG
893 done &&
894 (
895 cur= &&
896 __git_refs "" 1 >"$actual"
897 ) &&
898 test_cmp expected "$actual"
899'
900
2ea328a1
SG
901test_expect_success '__git_refs - after --opt=' '
902 cat >expected <<-EOF &&
903 HEAD
00076181 904 main
2ea328a1
SG
905 matching-branch
906 other/branch-in-other
00076181 907 other/main-in-other
2ea328a1
SG
908 matching-tag
909 EOF
910 (
911 cur="--opt=" &&
912 __git_refs "" "" "" "" >"$actual"
913 ) &&
914 test_cmp expected "$actual"
915'
916
917test_expect_success '__git_refs - after --opt= - full refs' '
918 cat >expected <<-EOF &&
00076181 919 refs/heads/main
2ea328a1
SG
920 refs/heads/matching-branch
921 refs/remotes/other/branch-in-other
00076181 922 refs/remotes/other/main-in-other
2ea328a1
SG
923 refs/tags/matching-tag
924 EOF
925 (
926 cur="--opt=refs/" &&
927 __git_refs "" "" "" refs/ >"$actual"
928 ) &&
929 test_cmp expected "$actual"
930'
931
b64b43d2 932test_expect_success '__git refs - excluding refs' '
aed38813
SG
933 cat >expected <<-EOF &&
934 ^HEAD
00076181 935 ^main
aed38813
SG
936 ^matching-branch
937 ^other/branch-in-other
00076181 938 ^other/main-in-other
aed38813
SG
939 ^matching-tag
940 EOF
941 (
942 cur=^ &&
943 __git_refs >"$actual"
944 ) &&
945 test_cmp expected "$actual"
946'
947
b64b43d2 948test_expect_success '__git refs - excluding full refs' '
aed38813 949 cat >expected <<-EOF &&
00076181 950 ^refs/heads/main
aed38813
SG
951 ^refs/heads/matching-branch
952 ^refs/remotes/other/branch-in-other
00076181 953 ^refs/remotes/other/main-in-other
aed38813
SG
954 ^refs/tags/matching-tag
955 EOF
956 (
957 cur=^refs/ &&
958 __git_refs >"$actual"
959 ) &&
960 test_cmp expected "$actual"
961'
962
e896369b
SG
963test_expect_success 'setup for filtering matching refs' '
964 git branch matching/branch &&
965 git tag matching/tag &&
966 git -C otherrepo branch matching/branch-in-other &&
967 git fetch --no-tags other &&
968 rm -f .git/FETCH_HEAD
969'
970
4dc8b1c1 971test_expect_success '__git_refs - do not filter refs unless told so' '
e896369b
SG
972 cat >expected <<-EOF &&
973 HEAD
00076181 974 main
e896369b
SG
975 matching-branch
976 matching/branch
977 other/branch-in-other
00076181 978 other/main-in-other
e896369b
SG
979 other/matching/branch-in-other
980 matching-tag
981 matching/tag
982 EOF
983 (
00076181 984 cur=main &&
e896369b
SG
985 __git_refs >"$actual"
986 ) &&
987 test_cmp expected "$actual"
988'
989
990test_expect_success '__git_refs - only matching refs' '
991 cat >expected <<-EOF &&
992 matching-branch
993 matching/branch
994 matching-tag
995 matching/tag
996 EOF
997 (
998 cur=mat &&
999 __git_refs "" "" "" "$cur" >"$actual"
1000 ) &&
1001 test_cmp expected "$actual"
1002'
1003
1004test_expect_success '__git_refs - only matching refs - full refs' '
1005 cat >expected <<-EOF &&
1006 refs/heads/matching-branch
1007 refs/heads/matching/branch
1008 EOF
1009 (
1010 cur=refs/heads/mat &&
1011 __git_refs "" "" "" "$cur" >"$actual"
1012 ) &&
1013 test_cmp expected "$actual"
1014'
1015
1016test_expect_success '__git_refs - only matching refs - remote on local file system' '
1017 cat >expected <<-EOF &&
00076181 1018 main-in-other
e896369b
SG
1019 matching/branch-in-other
1020 EOF
1021 (
1022 cur=ma &&
1023 __git_refs otherrepo "" "" "$cur" >"$actual"
1024 ) &&
1025 test_cmp expected "$actual"
1026'
1027
1028test_expect_success '__git_refs - only matching refs - configured remote' '
1029 cat >expected <<-EOF &&
00076181 1030 main-in-other
e896369b
SG
1031 matching/branch-in-other
1032 EOF
1033 (
1034 cur=ma &&
1035 __git_refs other "" "" "$cur" >"$actual"
1036 ) &&
1037 test_cmp expected "$actual"
1038'
1039
1040test_expect_success '__git_refs - only matching refs - remote - full refs' '
1041 cat >expected <<-EOF &&
00076181 1042 refs/heads/main-in-other
e896369b
SG
1043 refs/heads/matching/branch-in-other
1044 EOF
1045 (
1046 cur=refs/heads/ma &&
1047 __git_refs other "" "" "$cur" >"$actual"
1048 ) &&
1049 test_cmp expected "$actual"
1050'
1051
1052test_expect_success '__git_refs - only matching refs - checkout DWIMery' '
1053 cat >expected <<-EOF &&
1054 matching-branch
1055 matching/branch
1056 matching-tag
1057 matching/tag
1058 matching/branch-in-other
1059 EOF
1060 for remote_ref in refs/remotes/other/ambiguous \
1061 refs/remotes/remote/ambiguous \
1062 refs/remotes/remote/branch-in-remote
1063 do
00076181 1064 git update-ref $remote_ref main &&
0c51d6b4 1065 test_when_finished "git update-ref -d $remote_ref" || return 1
e896369b
SG
1066 done &&
1067 (
1068 cur=mat &&
1069 __git_refs "" 1 "" "$cur" >"$actual"
1070 ) &&
1071 test_cmp expected "$actual"
1072'
1073
1074test_expect_success 'teardown after filtering matching refs' '
1075 git branch -d matching/branch &&
1076 git tag -d matching/tag &&
1077 git update-ref -d refs/remotes/other/matching/branch-in-other &&
1078 git -C otherrepo branch -D matching/branch-in-other
1079'
1080
00076181 1081test_expect_success '__git_refs - for-each-ref format specifiers in prefix' '
fef56eb0 1082 cat >expected <<-EOF &&
00076181 1083 evil-%%-%42-%(refname)..main
fef56eb0
SG
1084 EOF
1085 (
8164360f
JS
1086 cur="evil-%%-%42-%(refname)..mai" &&
1087 __git_refs "" "" "evil-%%-%42-%(refname).." mai >"$actual"
fef56eb0
SG
1088 ) &&
1089 test_cmp expected "$actual"
1090'
1091
15b4a163
SG
1092test_expect_success '__git_complete_refs - simple' '
1093 sed -e "s/Z$//" >expected <<-EOF &&
1094 HEAD Z
00076181 1095 main Z
15b4a163
SG
1096 matching-branch Z
1097 other/branch-in-other Z
00076181 1098 other/main-in-other Z
15b4a163
SG
1099 matching-tag Z
1100 EOF
1101 (
1102 cur= &&
1103 __git_complete_refs &&
1104 print_comp
1105 ) &&
1106 test_cmp expected out
1107'
1108
1109test_expect_success '__git_complete_refs - matching' '
1110 sed -e "s/Z$//" >expected <<-EOF &&
1111 matching-branch Z
1112 matching-tag Z
1113 EOF
1114 (
1115 cur=mat &&
1116 __git_complete_refs &&
1117 print_comp
1118 ) &&
1119 test_cmp expected out
1120'
1121
1122test_expect_success '__git_complete_refs - remote' '
1123 sed -e "s/Z$//" >expected <<-EOF &&
1124 HEAD Z
1125 branch-in-other Z
00076181 1126 main-in-other Z
15b4a163
SG
1127 EOF
1128 (
cff4243d 1129 cur= &&
15b4a163
SG
1130 __git_complete_refs --remote=other &&
1131 print_comp
1132 ) &&
1133 test_cmp expected out
1134'
1135
1136test_expect_success '__git_complete_refs - track' '
1137 sed -e "s/Z$//" >expected <<-EOF &&
1138 HEAD Z
00076181 1139 main Z
15b4a163
SG
1140 matching-branch Z
1141 other/branch-in-other Z
00076181 1142 other/main-in-other Z
15b4a163
SG
1143 matching-tag Z
1144 branch-in-other Z
00076181 1145 main-in-other Z
15b4a163
SG
1146 EOF
1147 (
cff4243d 1148 cur= &&
15b4a163
SG
1149 __git_complete_refs --track &&
1150 print_comp
1151 ) &&
1152 test_cmp expected out
1153'
1154
1155test_expect_success '__git_complete_refs - current word' '
1156 sed -e "s/Z$//" >expected <<-EOF &&
1157 matching-branch Z
1158 matching-tag Z
1159 EOF
1160 (
1161 cur="--option=mat" &&
1162 __git_complete_refs --cur="${cur#*=}" &&
1163 print_comp
1164 ) &&
1165 test_cmp expected out
1166'
1167
1168test_expect_success '__git_complete_refs - prefix' '
1169 sed -e "s/Z$//" >expected <<-EOF &&
1170 v1.0..matching-branch Z
1171 v1.0..matching-tag Z
1172 EOF
1173 (
1174 cur=v1.0..mat &&
1175 __git_complete_refs --pfx=v1.0.. --cur=mat &&
1176 print_comp
1177 ) &&
1178 test_cmp expected out
1179'
1180
1181test_expect_success '__git_complete_refs - suffix' '
1182 cat >expected <<-EOF &&
1183 HEAD.
00076181 1184 main.
15b4a163
SG
1185 matching-branch.
1186 other/branch-in-other.
00076181 1187 other/main-in-other.
15b4a163
SG
1188 matching-tag.
1189 EOF
1190 (
1191 cur= &&
1192 __git_complete_refs --sfx=. &&
1193 print_comp
1194 ) &&
1195 test_cmp expected out
1196'
1197
aa0644f7
SG
1198test_expect_success '__git_complete_fetch_refspecs - simple' '
1199 sed -e "s/Z$//" >expected <<-EOF &&
1200 HEAD:HEAD Z
1201 branch-in-other:branch-in-other Z
00076181 1202 main-in-other:main-in-other Z
aa0644f7
SG
1203 EOF
1204 (
1205 cur= &&
1206 __git_complete_fetch_refspecs other &&
1207 print_comp
1208 ) &&
1209 test_cmp expected out
1210'
1211
1212test_expect_success '__git_complete_fetch_refspecs - matching' '
1213 sed -e "s/Z$//" >expected <<-EOF &&
1214 branch-in-other:branch-in-other Z
1215 EOF
1216 (
1217 cur=br &&
1218 __git_complete_fetch_refspecs other "" br &&
1219 print_comp
1220 ) &&
1221 test_cmp expected out
1222'
1223
1224test_expect_success '__git_complete_fetch_refspecs - prefix' '
1225 sed -e "s/Z$//" >expected <<-EOF &&
1226 +HEAD:HEAD Z
1227 +branch-in-other:branch-in-other Z
00076181 1228 +main-in-other:main-in-other Z
aa0644f7
SG
1229 EOF
1230 (
1231 cur="+" &&
1232 __git_complete_fetch_refspecs other "+" "" &&
1233 print_comp
1234 ) &&
1235 test_cmp expected out
1236'
1237
1238test_expect_success '__git_complete_fetch_refspecs - fully qualified' '
1239 sed -e "s/Z$//" >expected <<-EOF &&
1240 refs/heads/branch-in-other:refs/heads/branch-in-other Z
00076181 1241 refs/heads/main-in-other:refs/heads/main-in-other Z
aa0644f7
SG
1242 refs/tags/tag-in-other:refs/tags/tag-in-other Z
1243 EOF
1244 (
1245 cur=refs/ &&
1246 __git_complete_fetch_refspecs other "" refs/ &&
1247 print_comp
1248 ) &&
1249 test_cmp expected out
1250'
1251
1252test_expect_success '__git_complete_fetch_refspecs - fully qualified & prefix' '
1253 sed -e "s/Z$//" >expected <<-EOF &&
1254 +refs/heads/branch-in-other:refs/heads/branch-in-other Z
00076181 1255 +refs/heads/main-in-other:refs/heads/main-in-other Z
aa0644f7
SG
1256 +refs/tags/tag-in-other:refs/tags/tag-in-other Z
1257 EOF
1258 (
1259 cur=+refs/ &&
1260 __git_complete_fetch_refspecs other + refs/ &&
1261 print_comp
1262 ) &&
1263 test_cmp expected out
1264'
1265
3574816d
RJ
1266test_expect_success '__git_complete_worktree_paths' '
1267 test_when_finished "git worktree remove other_wt" &&
1268 git worktree add --orphan other_wt &&
1269 run_completion "git worktree remove " &&
1270 grep other_wt out
1271'
1272
1273test_expect_success '__git_complete_worktree_paths - not a git repository' '
1274 (
1275 cd non-repo &&
1276 GIT_CEILING_DIRECTORIES="$ROOT" &&
1277 export GIT_CEILING_DIRECTORIES &&
1278 test_completion "git worktree remove " ""
1279 )
1280'
1281
1282test_expect_success '__git_complete_worktree_paths with -C' '
1283 test_when_finished "git -C otherrepo worktree remove otherrepo_wt" &&
1284 git -C otherrepo worktree add --orphan otherrepo_wt &&
1285 run_completion "git -C otherrepo worktree remove " &&
1286 grep otherrepo_wt out
1287'
1288
68d97c7f 1289test_expect_success 'git switch - with no options, complete local branches and unique remote branch names for DWIM logic' '
fab466f9
JK
1290 test_completion "git switch " <<-\EOF
1291 branch-in-other Z
00076181
JS
1292 main Z
1293 main-in-other Z
fab466f9
JK
1294 matching-branch Z
1295 EOF
1296'
1297
e1f74dd5
BLK
1298test_expect_success 'git bisect - when not bisecting, complete only replay and start subcommands' '
1299 test_completion "git bisect " <<-\EOF
1300 replay Z
1301 start Z
1302 EOF
1303'
1304
41928aeb
BLK
1305test_expect_success 'git bisect - complete options to start subcommand' '
1306 test_completion "git bisect start --" <<-\EOF
1307 --term-new Z
1308 --term-bad Z
1309 --term-old Z
1310 --term-good Z
1311 --no-checkout Z
1312 --first-parent Z
1313 EOF
1314'
1315
e1f74dd5
BLK
1316test_expect_success 'setup for git-bisect tests requiring a repo' '
1317 git init git-bisect &&
1318 (
1319 cd git-bisect &&
1320 echo "initial contents" >file &&
1321 git add file &&
1322 git commit -am "Initial commit" &&
1323 git tag initial &&
1324 echo "new line" >>file &&
1325 git commit -am "First change" &&
1326 echo "another new line" >>file &&
1327 git commit -am "Second change" &&
1328 git tag final
1329 )
1330'
1331
1332test_expect_success 'git bisect - start subcommand arguments before double-dash are completed as revs' '
1333 (
1334 cd git-bisect &&
1335 test_completion "git bisect start " <<-\EOF
1336 HEAD Z
1337 final Z
1338 initial Z
1339 master Z
1340 EOF
1341 )
1342'
1343
1344# Note that these arguments are <pathspec>s, which in practice the fallback
1345# completion (not the git completion) later ends up completing as paths.
1346test_expect_success 'git bisect - start subcommand arguments after double-dash are not completed' '
1347 (
1348 cd git-bisect &&
1349 test_completion "git bisect start final initial -- " ""
1350 )
1351'
1352
1353test_expect_success 'setup for git-bisect tests requiring ongoing bisection' '
1354 (
1355 cd git-bisect &&
1356 git bisect start --term-new=custom_new --term-old=custom_old final initial
1357 )
1358'
1359
1360test_expect_success 'git-bisect - when bisecting all subcommands are candidates' '
1361 (
1362 cd git-bisect &&
1363 test_completion "git bisect " <<-\EOF
1364 start Z
1365 bad Z
af8910a2
BLK
1366 custom_new Z
1367 custom_old Z
e1f74dd5
BLK
1368 new Z
1369 good Z
1370 old Z
af8910a2 1371 terms Z
e1f74dd5
BLK
1372 skip Z
1373 reset Z
1374 visualize Z
1375 replay Z
1376 log Z
1377 run Z
1378 help Z
1379 EOF
1380 )
1381'
1382
af8910a2
BLK
1383test_expect_success 'git-bisect - options to terms subcommand are candidates' '
1384 (
1385 cd git-bisect &&
1386 test_completion "git bisect terms --" <<-\EOF
1387 --term-bad Z
1388 --term-good Z
1389 --term-new Z
1390 --term-old Z
1391 EOF
1392 )
1393'
1394
d115b877
BLK
1395test_expect_success 'git-bisect - git-log options to visualize subcommand are candidates' '
1396 (
1397 cd git-bisect &&
1398 # The completion used for git-log and here does not complete
1399 # every git-log option, so rather than hope to stay in sync
1400 # with exactly what it does we will just spot-test here.
1401 test_completion "git bisect visualize --sta" <<-\EOF &&
1402 --stat Z
1403 EOF
1404 test_completion "git bisect visualize --summar" <<-\EOF
1405 --summary Z
1406 EOF
1407 )
1408'
1409
d8e08f07
BLK
1410test_expect_success 'git-bisect - view subcommand is not a candidate' '
1411 (
1412 cd git-bisect &&
1413 test_completion "git bisect vi" <<-\EOF
1414 visualize Z
1415 EOF
1416 )
1417'
1418
1419test_expect_success 'git-bisect - existing view subcommand is recognized and enables completion of git-log options' '
1420 (
1421 cd git-bisect &&
1422 # The completion used for git-log and here does not complete
1423 # every git-log option, so rather than hope to stay in sync
1424 # with exactly what it does we will just spot-test here.
1425 test_completion "git bisect view --sta" <<-\EOF &&
1426 --stat Z
1427 EOF
1428 test_completion "git bisect view --summar" <<-\EOF
1429 --summary Z
1430 EOF
1431 )
1432'
1433
fab466f9
JK
1434test_expect_success 'git checkout - completes refs and unique remote branches for DWIM' '
1435 test_completion "git checkout " <<-\EOF
1436 HEAD Z
1437 branch-in-other Z
00076181
JS
1438 main Z
1439 main-in-other Z
fab466f9
JK
1440 matching-branch Z
1441 matching-tag Z
1442 other/branch-in-other Z
00076181 1443 other/main-in-other Z
fab466f9
JK
1444 EOF
1445'
1446
ab58e90f
JK
1447test_expect_success 'git switch - with --no-guess, complete only local branches' '
1448 test_completion "git switch --no-guess " <<-\EOF
00076181 1449 main Z
ab58e90f
JK
1450 matching-branch Z
1451 EOF
1452'
1453
1454test_expect_success 'git switch - with GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete only local branches' '
1455 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch " <<-\EOF
00076181 1456 main Z
ab58e90f
JK
1457 matching-branch Z
1458 EOF
1459'
1460
68d97c7f 1461test_expect_success 'git switch - --guess overrides GIT_COMPLETION_CHECKOUT_NO_GUESS=1, complete local branches and unique remote names for DWIM logic' '
ab58e90f
JK
1462 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git switch --guess " <<-\EOF
1463 branch-in-other Z
00076181
JS
1464 main Z
1465 main-in-other Z
ab58e90f
JK
1466 matching-branch Z
1467 EOF
1468'
1469
68d97c7f 1470test_expect_success 'git switch - a later --guess overrides previous --no-guess, complete local and remote unique branches for DWIM' '
ab58e90f
JK
1471 test_completion "git switch --no-guess --guess " <<-\EOF
1472 branch-in-other Z
00076181
JS
1473 main Z
1474 main-in-other Z
ab58e90f
JK
1475 matching-branch Z
1476 EOF
1477'
1478
4e79adf4 1479test_expect_success 'git switch - a later --no-guess overrides previous --guess, complete only local branches' '
ab58e90f 1480 test_completion "git switch --guess --no-guess " <<-\EOF
00076181 1481 main Z
ab58e90f
JK
1482 matching-branch Z
1483 EOF
1484'
1485
1486test_expect_success 'git checkout - with GIT_COMPLETION_NO_GUESS=1 only completes refs' '
1487 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout " <<-\EOF
1488 HEAD Z
00076181 1489 main Z
ab58e90f
JK
1490 matching-branch Z
1491 matching-tag Z
1492 other/branch-in-other Z
00076181 1493 other/main-in-other Z
ab58e90f
JK
1494 EOF
1495'
1496
4e79adf4 1497test_expect_success 'git checkout - --guess overrides GIT_COMPLETION_NO_GUESS=1, complete refs and unique remote branches for DWIM' '
ab58e90f
JK
1498 GIT_COMPLETION_CHECKOUT_NO_GUESS=1 test_completion "git checkout --guess " <<-\EOF
1499 HEAD Z
1500 branch-in-other Z
00076181
JS
1501 main Z
1502 main-in-other Z
ab58e90f
JK
1503 matching-branch Z
1504 matching-tag Z
1505 other/branch-in-other Z
00076181 1506 other/main-in-other Z
ab58e90f
JK
1507 EOF
1508'
1509
1510test_expect_success 'git checkout - with --no-guess, only completes refs' '
1511 test_completion "git checkout --no-guess " <<-\EOF
1512 HEAD Z
00076181 1513 main Z
ab58e90f
JK
1514 matching-branch Z
1515 matching-tag Z
1516 other/branch-in-other Z
00076181 1517 other/main-in-other Z
ab58e90f
JK
1518 EOF
1519'
1520
4e79adf4 1521test_expect_success 'git checkout - a later --guess overrides previous --no-guess, complete refs and unique remote branches for DWIM' '
ab58e90f
JK
1522 test_completion "git checkout --no-guess --guess " <<-\EOF
1523 HEAD Z
1524 branch-in-other Z
00076181
JS
1525 main Z
1526 main-in-other Z
ab58e90f
JK
1527 matching-branch Z
1528 matching-tag Z
1529 other/branch-in-other Z
00076181 1530 other/main-in-other Z
ab58e90f
JK
1531 EOF
1532'
1533
1534test_expect_success 'git checkout - a later --no-guess overrides previous --guess, complete only refs' '
1535 test_completion "git checkout --guess --no-guess " <<-\EOF
1536 HEAD Z
00076181 1537 main Z
ab58e90f
JK
1538 matching-branch Z
1539 matching-tag Z
1540 other/branch-in-other Z
00076181 1541 other/main-in-other Z
ab58e90f
JK
1542 EOF
1543'
1544
64f1f58f
DL
1545test_expect_success 'git checkout - with checkout.guess = false, only completes refs' '
1546 test_config checkout.guess false &&
1547 test_completion "git checkout " <<-\EOF
1548 HEAD Z
00076181 1549 main Z
64f1f58f
DL
1550 matching-branch Z
1551 matching-tag Z
1552 other/branch-in-other Z
00076181 1553 other/main-in-other Z
64f1f58f
DL
1554 EOF
1555'
1556
1557test_expect_success 'git checkout - with checkout.guess = true, completes refs and unique remote branches for DWIM' '
1558 test_config checkout.guess true &&
1559 test_completion "git checkout " <<-\EOF
1560 HEAD Z
1561 branch-in-other Z
00076181
JS
1562 main Z
1563 main-in-other Z
64f1f58f
DL
1564 matching-branch Z
1565 matching-tag Z
1566 other/branch-in-other Z
00076181 1567 other/main-in-other Z
64f1f58f
DL
1568 EOF
1569'
1570
1571test_expect_success 'git checkout - a later --guess overrides previous checkout.guess = false, complete refs and unique remote branches for DWIM' '
1572 test_config checkout.guess false &&
1573 test_completion "git checkout --guess " <<-\EOF
1574 HEAD Z
1575 branch-in-other Z
00076181
JS
1576 main Z
1577 main-in-other Z
64f1f58f
DL
1578 matching-branch Z
1579 matching-tag Z
1580 other/branch-in-other Z
00076181 1581 other/main-in-other Z
64f1f58f
DL
1582 EOF
1583'
1584
1585test_expect_success 'git checkout - a later --no-guess overrides previous checkout.guess = true, complete only refs' '
1586 test_config checkout.guess true &&
1587 test_completion "git checkout --no-guess " <<-\EOF
1588 HEAD Z
00076181 1589 main Z
64f1f58f
DL
1590 matching-branch Z
1591 matching-tag Z
1592 other/branch-in-other Z
00076181 1593 other/main-in-other Z
64f1f58f
DL
1594 EOF
1595'
1596
e69fb0a1
JK
1597test_expect_success 'git switch - with --detach, complete all references' '
1598 test_completion "git switch --detach " <<-\EOF
1599 HEAD Z
00076181 1600 main Z
e69fb0a1
JK
1601 matching-branch Z
1602 matching-tag Z
1603 other/branch-in-other Z
00076181 1604 other/main-in-other Z
e69fb0a1
JK
1605 EOF
1606'
1607
6d76a5cc 1608test_expect_success 'git checkout - with --detach, complete only references' '
e69fb0a1
JK
1609 test_completion "git checkout --detach " <<-\EOF
1610 HEAD Z
00076181 1611 main Z
e69fb0a1
JK
1612 matching-branch Z
1613 matching-tag Z
1614 other/branch-in-other Z
00076181 1615 other/main-in-other Z
e69fb0a1
JK
1616 EOF
1617'
1618
fd6d9bec
LD
1619test_expect_success 'setup sparse-checkout tests' '
1620 # set up sparse-checkout repo
1621 git init sparse-checkout &&
1622 (
1623 cd sparse-checkout &&
1624 mkdir -p folder1/0/1 folder2/0 folder3 &&
1625 touch folder1/0/1/t.txt &&
1626 touch folder2/0/t.txt &&
1627 touch folder3/t.txt &&
1628 git add . &&
1629 git commit -am "Initial commit"
1630 )
1631'
1632
1633test_expect_success 'sparse-checkout completes subcommands' '
1634 test_completion "git sparse-checkout " <<-\EOF
1635 list Z
1636 init Z
1637 set Z
1638 add Z
1639 reapply Z
1640 disable Z
1641 EOF
1642'
1643
1644test_expect_success 'cone mode sparse-checkout completes directory names' '
1645 # initialize sparse-checkout definitions
1646 git -C sparse-checkout sparse-checkout set --cone folder1/0 folder3 &&
1647
1648 # test tab completion
1649 (
1650 cd sparse-checkout &&
1651 test_completion "git sparse-checkout set f" <<-\EOF
c5f5c508
LD
1652 folder1/
1653 folder2/
1654 folder3/
1655 EOF
1656 ) &&
1657
1658 (
1659 cd sparse-checkout &&
1660 test_completion "git sparse-checkout set folder1/" <<-\EOF
1661 folder1/0/
1662 EOF
1663 ) &&
1664
1665 (
1666 cd sparse-checkout &&
1667 test_completion "git sparse-checkout set folder1/0/" <<-\EOF
1668 folder1/0/1/
fd6d9bec
LD
1669 EOF
1670 ) &&
1671
1672 (
1673 cd sparse-checkout/folder1 &&
c5f5c508
LD
1674 test_completion "git sparse-checkout add 0" <<-\EOF
1675 0/
fd6d9bec
LD
1676 EOF
1677 )
1678'
1679
48803821
LD
1680test_expect_success 'cone mode sparse-checkout completes directory names with spaces and accents' '
1681 # reset sparse-checkout
1682 git -C sparse-checkout sparse-checkout disable &&
1683 (
1684 cd sparse-checkout &&
1685 mkdir "directory with spaces" &&
1686 mkdir "directory-with-áccent" &&
1687 >"directory with spaces/randomfile" &&
1688 >"directory-with-áccent/randomfile" &&
1689 git add . &&
1690 git commit -m "Add directory with spaces and directory with accent" &&
1691 git sparse-checkout set --cone "directory with spaces" \
1692 "directory-with-áccent" &&
1693 test_completion "git sparse-checkout add dir" <<-\EOF &&
1694 directory with spaces/
1695 directory-with-áccent/
1696 EOF
1697 rm -rf "directory with spaces" &&
1698 rm -rf "directory-with-áccent" &&
1699 git add . &&
1700 git commit -m "Remove directory with spaces and directory with accent"
1701 )
1702'
1703
6d340dfa
AD
1704# use FUNNYNAMES to avoid running on Windows, which doesn't permit tabs in paths
1705test_expect_success FUNNYNAMES 'cone mode sparse-checkout completes directory names with tabs' '
48803821
LD
1706 # reset sparse-checkout
1707 git -C sparse-checkout sparse-checkout disable &&
1708 (
1709 cd sparse-checkout &&
48803821 1710 mkdir "$(printf "directory\twith\ttabs")" &&
48803821
LD
1711 >"$(printf "directory\twith\ttabs")/randomfile" &&
1712 git add . &&
6d340dfa
AD
1713 git commit -m "Add directory with tabs" &&
1714 git sparse-checkout set --cone \
48803821
LD
1715 "$(printf "directory\twith\ttabs")" &&
1716 test_completion "git sparse-checkout add dir" <<-\EOF &&
48803821
LD
1717 directory with tabs/
1718 EOF
48803821
LD
1719 rm -rf "$(printf "directory\twith\ttabs")" &&
1720 git add . &&
6d340dfa
AD
1721 git commit -m "Remove directory with tabs"
1722 )
1723'
1724
1725# use FUNNYNAMES to avoid running on Windows, and !CYGWIN for Cygwin, as neither permit backslashes in paths
1726test_expect_success FUNNYNAMES,!CYGWIN 'cone mode sparse-checkout completes directory names with backslashes' '
1727 # reset sparse-checkout
1728 git -C sparse-checkout sparse-checkout disable &&
1729 (
1730 cd sparse-checkout &&
1731 mkdir "directory\with\backslashes" &&
1732 >"directory\with\backslashes/randomfile" &&
1733 git add . &&
1734 git commit -m "Add directory with backslashes" &&
1735 git sparse-checkout set --cone \
1736 "directory\with\backslashes" &&
1737 test_completion "git sparse-checkout add dir" <<-\EOF &&
1738 directory\with\backslashes/
1739 EOF
1740 rm -rf "directory\with\backslashes" &&
1741 git add . &&
1742 git commit -m "Remove directory with backslashes"
48803821
LD
1743 )
1744'
1745
a1fbe26a 1746test_expect_success 'non-cone mode sparse-checkout gives rooted paths' '
fd6d9bec
LD
1747 # reset sparse-checkout repo to non-cone mode
1748 git -C sparse-checkout sparse-checkout disable &&
1749 git -C sparse-checkout sparse-checkout set --no-cone &&
1750
1751 (
1752 cd sparse-checkout &&
1753 # expected to be empty since we have not configured
1754 # custom completion for non-cone mode
1755 test_completion "git sparse-checkout set f" <<-\EOF
a1fbe26a
EN
1756 /folder1/0/1/t.txt Z
1757 /folder1/expected Z
1758 /folder1/out Z
1759 /folder1/out_sorted Z
1760 /folder2/0/t.txt Z
1761 /folder3/t.txt Z
fd6d9bec
LD
1762 EOF
1763 )
1764'
1765
1766test_expect_success 'git sparse-checkout set --cone completes directory names' '
1767 git -C sparse-checkout sparse-checkout disable &&
1768
1769 (
1770 cd sparse-checkout &&
1771 test_completion "git sparse-checkout set --cone f" <<-\EOF
c5f5c508
LD
1772 folder1/
1773 folder2/
1774 folder3/
fd6d9bec
LD
1775 EOF
1776 )
1777'
1778
e69fb0a1
JK
1779test_expect_success 'git switch - with -d, complete all references' '
1780 test_completion "git switch -d " <<-\EOF
1781 HEAD Z
00076181 1782 main Z
e69fb0a1
JK
1783 matching-branch Z
1784 matching-tag Z
1785 other/branch-in-other Z
00076181 1786 other/main-in-other Z
e69fb0a1
JK
1787 EOF
1788'
1789
6d76a5cc 1790test_expect_success 'git checkout - with -d, complete only references' '
e69fb0a1
JK
1791 test_completion "git checkout -d " <<-\EOF
1792 HEAD Z
00076181 1793 main Z
e69fb0a1
JK
1794 matching-branch Z
1795 matching-tag Z
1796 other/branch-in-other Z
00076181 1797 other/main-in-other Z
e69fb0a1
JK
1798 EOF
1799'
1800
00e7bd2b 1801test_expect_success 'git switch - with --track, complete only remote branches' '
9f892830
JS
1802 test_completion "git switch --track " <<-\EOF &&
1803 other/branch-in-other Z
1804 other/main-in-other Z
1805 EOF
1806 test_completion "git switch -t " <<-\EOF
c55b99c3 1807 other/branch-in-other Z
00076181 1808 other/main-in-other Z
c55b99c3
JK
1809 EOF
1810'
1811
00e7bd2b 1812test_expect_success 'git checkout - with --track, complete only remote branches' '
9f892830
JS
1813 test_completion "git checkout --track " <<-\EOF &&
1814 other/branch-in-other Z
1815 other/main-in-other Z
1816 EOF
1817 test_completion "git checkout -t " <<-\EOF
c55b99c3 1818 other/branch-in-other Z
00076181 1819 other/main-in-other Z
c55b99c3
JK
1820 EOF
1821'
1822
1823test_expect_success 'git switch - with --no-track, complete only local branch names' '
1824 test_completion "git switch --no-track " <<-\EOF
00076181 1825 main Z
c55b99c3
JK
1826 matching-branch Z
1827 EOF
1828'
1829
1830test_expect_success 'git checkout - with --no-track, complete only local references' '
1831 test_completion "git checkout --no-track " <<-\EOF
1832 HEAD Z
00076181 1833 main Z
c55b99c3
JK
1834 matching-branch Z
1835 matching-tag Z
1836 other/branch-in-other Z
00076181 1837 other/main-in-other Z
c55b99c3
JK
1838 EOF
1839'
1840
acb658fe 1841test_expect_success 'git switch - with -c, complete all references' '
b07d77a2
JK
1842 test_completion "git switch -c new-branch " <<-\EOF
1843 HEAD Z
00076181 1844 main Z
b07d77a2
JK
1845 matching-branch Z
1846 matching-tag Z
1847 other/branch-in-other Z
00076181 1848 other/main-in-other Z
b07d77a2
JK
1849 EOF
1850'
1851
acb658fe 1852test_expect_success 'git switch - with -C, complete all references' '
b07d77a2
JK
1853 test_completion "git switch -C new-branch " <<-\EOF
1854 HEAD Z
00076181 1855 main Z
b07d77a2
JK
1856 matching-branch Z
1857 matching-tag Z
1858 other/branch-in-other Z
00076181 1859 other/main-in-other Z
b07d77a2
JK
1860 EOF
1861'
1862
acb658fe 1863test_expect_success 'git switch - with -c and --track, complete all references' '
b07d77a2
JK
1864 test_completion "git switch -c new-branch --track " <<-EOF
1865 HEAD Z
00076181 1866 main Z
b07d77a2
JK
1867 matching-branch Z
1868 matching-tag Z
1869 other/branch-in-other Z
00076181 1870 other/main-in-other Z
b07d77a2
JK
1871 EOF
1872'
1873
acb658fe 1874test_expect_success 'git switch - with -C and --track, complete all references' '
b07d77a2
JK
1875 test_completion "git switch -C new-branch --track " <<-EOF
1876 HEAD Z
00076181 1877 main Z
b07d77a2
JK
1878 matching-branch Z
1879 matching-tag Z
1880 other/branch-in-other Z
00076181 1881 other/main-in-other Z
b07d77a2
JK
1882 EOF
1883'
1884
acb658fe 1885test_expect_success 'git switch - with -c and --no-track, complete all references' '
b07d77a2
JK
1886 test_completion "git switch -c new-branch --no-track " <<-\EOF
1887 HEAD Z
00076181 1888 main Z
b07d77a2
JK
1889 matching-branch Z
1890 matching-tag Z
1891 other/branch-in-other Z
00076181 1892 other/main-in-other Z
b07d77a2
JK
1893 EOF
1894'
1895
acb658fe 1896test_expect_success 'git switch - with -C and --no-track, complete all references' '
b07d77a2
JK
1897 test_completion "git switch -C new-branch --no-track " <<-\EOF
1898 HEAD Z
00076181 1899 main Z
b07d77a2
JK
1900 matching-branch Z
1901 matching-tag Z
1902 other/branch-in-other Z
00076181 1903 other/main-in-other Z
b07d77a2
JK
1904 EOF
1905'
1906
acb658fe 1907test_expect_success 'git checkout - with -b, complete all references' '
b07d77a2
JK
1908 test_completion "git checkout -b new-branch " <<-\EOF
1909 HEAD Z
00076181 1910 main Z
b07d77a2
JK
1911 matching-branch Z
1912 matching-tag Z
1913 other/branch-in-other Z
00076181 1914 other/main-in-other Z
b07d77a2
JK
1915 EOF
1916'
1917
acb658fe 1918test_expect_success 'git checkout - with -B, complete all references' '
b07d77a2
JK
1919 test_completion "git checkout -B new-branch " <<-\EOF
1920 HEAD Z
00076181 1921 main Z
b07d77a2
JK
1922 matching-branch Z
1923 matching-tag Z
1924 other/branch-in-other Z
00076181 1925 other/main-in-other Z
b07d77a2
JK
1926 EOF
1927'
1928
acb658fe 1929test_expect_success 'git checkout - with -b and --track, complete all references' '
b07d77a2
JK
1930 test_completion "git checkout -b new-branch --track " <<-EOF
1931 HEAD Z
00076181 1932 main Z
b07d77a2
JK
1933 matching-branch Z
1934 matching-tag Z
1935 other/branch-in-other Z
00076181 1936 other/main-in-other Z
b07d77a2
JK
1937 EOF
1938'
1939
acb658fe 1940test_expect_success 'git checkout - with -B and --track, complete all references' '
b07d77a2
JK
1941 test_completion "git checkout -B new-branch --track " <<-EOF
1942 HEAD Z
00076181 1943 main Z
b07d77a2
JK
1944 matching-branch Z
1945 matching-tag Z
1946 other/branch-in-other Z
00076181 1947 other/main-in-other Z
b07d77a2
JK
1948 EOF
1949'
1950
1951test_expect_success 'git checkout - with -b and --no-track, complete all references' '
1952 test_completion "git checkout -b new-branch --no-track " <<-\EOF
1953 HEAD Z
00076181 1954 main Z
b07d77a2
JK
1955 matching-branch Z
1956 matching-tag Z
1957 other/branch-in-other Z
00076181 1958 other/main-in-other Z
b07d77a2
JK
1959 EOF
1960'
1961
1962test_expect_success 'git checkout - with -B and --no-track, complete all references' '
1963 test_completion "git checkout -B new-branch --no-track " <<-\EOF
1964 HEAD Z
00076181 1965 main Z
b07d77a2
JK
1966 matching-branch Z
1967 matching-tag Z
1968 other/branch-in-other Z
00076181 1969 other/main-in-other Z
b07d77a2
JK
1970 EOF
1971'
1972
68d97c7f 1973test_expect_success 'git switch - for -c, complete local branches and unique remote branches' '
7f59d604
JK
1974 test_completion "git switch -c " <<-\EOF
1975 branch-in-other Z
00076181
JS
1976 main Z
1977 main-in-other Z
7f59d604
JK
1978 matching-branch Z
1979 EOF
1980'
1981
68d97c7f 1982test_expect_success 'git switch - for -C, complete local branches and unique remote branches' '
7f59d604
JK
1983 test_completion "git switch -C " <<-\EOF
1984 branch-in-other Z
00076181
JS
1985 main Z
1986 main-in-other Z
7f59d604
JK
1987 matching-branch Z
1988 EOF
1989'
1990
1991test_expect_success 'git switch - for -c with --no-guess, complete local branches only' '
1992 test_completion "git switch --no-guess -c " <<-\EOF
00076181 1993 main Z
7f59d604
JK
1994 matching-branch Z
1995 EOF
1996'
1997
1998test_expect_success 'git switch - for -C with --no-guess, complete local branches only' '
1999 test_completion "git switch --no-guess -C " <<-\EOF
00076181 2000 main Z
7f59d604
JK
2001 matching-branch Z
2002 EOF
2003'
2004
2005test_expect_success 'git switch - for -c with --no-track, complete local branches only' '
2006 test_completion "git switch --no-track -c " <<-\EOF
00076181 2007 main Z
7f59d604
JK
2008 matching-branch Z
2009 EOF
2010'
2011
2012test_expect_success 'git switch - for -C with --no-track, complete local branches only' '
2013 test_completion "git switch --no-track -C " <<-\EOF
00076181 2014 main Z
7f59d604
JK
2015 matching-branch Z
2016 EOF
2017'
2018
acb658fe 2019test_expect_success 'git checkout - for -b, complete local branches and unique remote branches' '
7f59d604
JK
2020 test_completion "git checkout -b " <<-\EOF
2021 branch-in-other Z
00076181
JS
2022 main Z
2023 main-in-other Z
7f59d604
JK
2024 matching-branch Z
2025 EOF
2026'
2027
acb658fe 2028test_expect_success 'git checkout - for -B, complete local branches and unique remote branches' '
7f59d604
JK
2029 test_completion "git checkout -B " <<-\EOF
2030 branch-in-other Z
00076181
JS
2031 main Z
2032 main-in-other Z
7f59d604
JK
2033 matching-branch Z
2034 EOF
2035'
2036
acb658fe 2037test_expect_success 'git checkout - for -b with --no-guess, complete local branches only' '
7f59d604 2038 test_completion "git checkout --no-guess -b " <<-\EOF
00076181 2039 main Z
7f59d604
JK
2040 matching-branch Z
2041 EOF
2042'
2043
acb658fe 2044test_expect_success 'git checkout - for -B with --no-guess, complete local branches only' '
7f59d604 2045 test_completion "git checkout --no-guess -B " <<-\EOF
00076181 2046 main Z
7f59d604
JK
2047 matching-branch Z
2048 EOF
2049'
2050
acb658fe 2051test_expect_success 'git checkout - for -b with --no-track, complete local branches only' '
7f59d604 2052 test_completion "git checkout --no-track -b " <<-\EOF
00076181 2053 main Z
7f59d604
JK
2054 matching-branch Z
2055 EOF
2056'
2057
acb658fe 2058test_expect_success 'git checkout - for -B with --no-track, complete local branches only' '
7f59d604 2059 test_completion "git checkout --no-track -B " <<-\EOF
00076181 2060 main Z
7f59d604
JK
2061 matching-branch Z
2062 EOF
2063'
2064
68d97c7f 2065test_expect_success 'git switch - with --orphan completes local branch names and unique remote branch names' '
c81ca56b
JK
2066 test_completion "git switch --orphan " <<-\EOF
2067 branch-in-other Z
00076181
JS
2068 main Z
2069 main-in-other Z
c81ca56b
JK
2070 matching-branch Z
2071 EOF
2072'
2073
91439928 2074test_expect_success 'git switch - --orphan with branch already provided completes nothing else' '
00076181 2075 test_completion "git switch --orphan main " <<-\EOF
c81ca56b
JK
2076
2077 EOF
2078'
2079
91439928 2080test_expect_success 'git checkout - with --orphan completes local branch names and unique remote branch names' '
c81ca56b
JK
2081 test_completion "git checkout --orphan " <<-\EOF
2082 branch-in-other Z
00076181
JS
2083 main Z
2084 main-in-other Z
c81ca56b
JK
2085 matching-branch Z
2086 EOF
2087'
2088
91439928 2089test_expect_success 'git checkout - --orphan with branch already provided completes local refs for a start-point' '
00076181 2090 test_completion "git checkout --orphan main " <<-\EOF
c81ca56b 2091 HEAD Z
00076181 2092 main Z
c81ca56b
JK
2093 matching-branch Z
2094 matching-tag Z
2095 other/branch-in-other Z
00076181 2096 other/main-in-other Z
c81ca56b
JK
2097 EOF
2098'
2099
9a9c3113
PS
2100test_expect_success 'git restore completes modified files' '
2101 test_commit A a.file &&
2102 echo B >a.file &&
2103 test_completion "git restore a." <<-\EOF
2104 a.file
2105 EOF
2106'
2107
fb9cd420
SG
2108test_expect_success 'teardown after ref completion' '
2109 git branch -d matching-branch &&
2110 git tag -d matching-tag &&
2111 git remote remove other
2112'
2113
7d314073
SG
2114
2115test_path_completion ()
2116{
165293af 2117 test $# = 2 || BUG "not 2 parameters to test_path_completion"
7d314073
SG
2118
2119 local cur="$1" expected="$2"
2120 echo "$expected" >expected &&
2121 (
2122 # In the following tests calling this function we only
2123 # care about how __git_complete_index_file() deals with
2124 # unusual characters in path names. By requesting only
7a40cf15 2125 # untracked files we do not have to bother adding any
7d314073
SG
2126 # paths to the index in those tests.
2127 __git_complete_index_file --others &&
2128 print_comp
2129 ) &&
2130 test_cmp expected out
2131}
2132
2133test_expect_success 'setup for path completion tests' '
2134 mkdir simple-dir \
2135 "spaces in dir" \
2136 árvíztűrő &&
2137 touch simple-dir/simple-file \
2138 "spaces in dir/spaces in file" \
2139 "árvíztűrő/Сайн яваарай" &&
2140 if test_have_prereq !MINGW &&
2141 mkdir BS\\dir \
2142 '$'separators\034in\035dir'' &&
2143 touch BS\\dir/DQ\"file \
2144 '$'separators\034in\035dir/sep\036in\037file''
2145 then
6ec63305 2146 test_set_prereq FUNNIERNAMES
7d314073
SG
2147 else
2148 rm -rf BS\\dir '$'separators\034in\035dir''
2149 fi
2150'
2151
2152test_expect_success '__git_complete_index_file - simple' '
2153 test_path_completion simple simple-dir && # Bash is supposed to
2154 # add the trailing /.
2155 test_path_completion simple-dir/simple simple-dir/simple-file
2156'
2157
2158test_expect_success \
2159 '__git_complete_index_file - escaped characters on cmdline' '
2160 test_path_completion spac "spaces in dir" && # Bash will turn this
2161 # into "spaces\ in\ dir"
2162 test_path_completion "spaces\\ i" \
2163 "spaces in dir" &&
2164 test_path_completion "spaces\\ in\\ dir/s" \
2165 "spaces in dir/spaces in file" &&
2166 test_path_completion "spaces\\ in\\ dir/spaces\\ i" \
2167 "spaces in dir/spaces in file"
2168'
2169
2170test_expect_success \
2171 '__git_complete_index_file - quoted characters on cmdline' '
2172 # Testing with an opening but without a corresponding closing
2173 # double quote is important.
2174 test_path_completion \"spac "spaces in dir" &&
2175 test_path_completion "\"spaces i" \
2176 "spaces in dir" &&
2177 test_path_completion "\"spaces in dir/s" \
2178 "spaces in dir/spaces in file" &&
2179 test_path_completion "\"spaces in dir/spaces i" \
2180 "spaces in dir/spaces in file"
2181'
2182
2183test_expect_success '__git_complete_index_file - UTF-8 in ls-files output' '
2184 test_path_completion á árvíztűrő &&
2185 test_path_completion árvíztűrő/С "árvíztűrő/Сайн яваарай"
2186'
2187
6ec63305 2188test_expect_success FUNNIERNAMES \
7d314073
SG
2189 '__git_complete_index_file - C-style escapes in ls-files output' '
2190 test_path_completion BS \
2191 BS\\dir &&
2192 test_path_completion BS\\\\d \
2193 BS\\dir &&
2194 test_path_completion BS\\\\dir/DQ \
2195 BS\\dir/DQ\"file &&
2196 test_path_completion BS\\\\dir/DQ\\\"f \
2197 BS\\dir/DQ\"file
2198'
2199
6ec63305 2200test_expect_success FUNNIERNAMES \
7d314073
SG
2201 '__git_complete_index_file - \nnn-escaped characters in ls-files output' '
2202 test_path_completion sep '$'separators\034in\035dir'' &&
2203 test_path_completion '$'separators\034i'' \
2204 '$'separators\034in\035dir'' &&
2205 test_path_completion '$'separators\034in\035dir/sep'' \
2206 '$'separators\034in\035dir/sep\036in\037file'' &&
2207 test_path_completion '$'separators\034in\035dir/sep\036i'' \
2208 '$'separators\034in\035dir/sep\036in\037file''
2209'
2210
2211test_expect_success FUNNYNAMES \
2212 '__git_complete_index_file - removing repeated quoted path components' '
2213 test_when_finished rm -r repeated-quoted &&
2214 mkdir repeated-quoted && # A directory whose name in itself
2215 # would not be quoted ...
2216 >repeated-quoted/0-file &&
2217 >repeated-quoted/1\"file && # ... but here the file makes the
2218 # dirname quoted ...
2219 >repeated-quoted/2-file &&
2220 >repeated-quoted/3\"file && # ... and here, too.
2221
2222 # Still, we shold only list the directory name only once.
2223 test_path_completion repeated repeated-quoted
2224'
2225
2226test_expect_success 'teardown after path completion tests' '
2227 rm -rf simple-dir "spaces in dir" árvíztűrő \
2228 BS\\dir '$'separators\034in\035dir''
2229'
2230
2712e915
SG
2231test_expect_success '__git_find_on_cmdline - single match' '
2232 echo list >expect &&
2233 (
2234 words=(git command --opt list) &&
2235 cword=${#words[@]} &&
59d85a2a 2236 __git_cmd_idx=1 &&
2712e915
SG
2237 __git_find_on_cmdline "add list remove" >actual
2238 ) &&
2239 test_cmp expect actual
2240'
2241
2242test_expect_success '__git_find_on_cmdline - multiple matches' '
2243 echo remove >expect &&
2244 (
2245 words=(git command -o --opt remove list add) &&
2246 cword=${#words[@]} &&
59d85a2a 2247 __git_cmd_idx=1 &&
2712e915
SG
2248 __git_find_on_cmdline "add list remove" >actual
2249 ) &&
2250 test_cmp expect actual
2251'
2252
2253test_expect_success '__git_find_on_cmdline - no match' '
2254 (
2255 words=(git command --opt branch) &&
2256 cword=${#words[@]} &&
59d85a2a 2257 __git_cmd_idx=1 &&
2712e915
SG
2258 __git_find_on_cmdline "add list remove" >actual
2259 ) &&
2260 test_must_be_empty actual
2261'
7d314073 2262
367efd54
SG
2263test_expect_success '__git_find_on_cmdline - single match with index' '
2264 echo "3 list" >expect &&
2265 (
2266 words=(git command --opt list) &&
2267 cword=${#words[@]} &&
59d85a2a 2268 __git_cmd_idx=1 &&
367efd54
SG
2269 __git_find_on_cmdline --show-idx "add list remove" >actual
2270 ) &&
2271 test_cmp expect actual
2272'
2273
2274test_expect_success '__git_find_on_cmdline - multiple matches with index' '
2275 echo "4 remove" >expect &&
2276 (
2277 words=(git command -o --opt remove list add) &&
2278 cword=${#words[@]} &&
59d85a2a 2279 __git_cmd_idx=1 &&
367efd54
SG
2280 __git_find_on_cmdline --show-idx "add list remove" >actual
2281 ) &&
2282 test_cmp expect actual
2283'
2284
2285test_expect_success '__git_find_on_cmdline - no match with index' '
2286 (
2287 words=(git command --opt branch) &&
2288 cword=${#words[@]} &&
59d85a2a 2289 __git_cmd_idx=1 &&
367efd54
SG
2290 __git_find_on_cmdline --show-idx "add list remove" >actual
2291 ) &&
2292 test_must_be_empty actual
2293'
7d314073 2294
59d85a2a
DL
2295test_expect_success '__git_find_on_cmdline - ignores matches before command with index' '
2296 echo "6 remove" >expect &&
2297 (
2298 words=(git -C remove command -o --opt remove list add) &&
2299 cword=${#words[@]} &&
2300 __git_cmd_idx=3 &&
2301 __git_find_on_cmdline --show-idx "add list remove" >actual
2302 ) &&
2303 test_cmp expect actual
2304'
2305
12bdc880
SG
2306test_expect_success '__git_get_config_variables' '
2307 cat >expect <<-EOF &&
2308 name-1
2309 name-2
2310 EOF
2311 test_config interesting.name-1 good &&
2312 test_config interesting.name-2 good &&
2313 test_config subsection.interesting.name-3 bad &&
2314 __git_get_config_variables interesting >actual &&
2315 test_cmp expect actual
2316'
2317
e8f9e428
SG
2318test_expect_success '__git_pretty_aliases' '
2319 cat >expect <<-EOF &&
2320 author
2321 hash
2322 EOF
2323 test_config pretty.author "%an %ae" &&
2324 test_config pretty.hash %H &&
2325 __git_pretty_aliases >actual &&
2326 test_cmp expect actual
2327'
2328
5c293a6b 2329test_expect_success 'basic' '
cdbff7d6 2330 run_completion "git " &&
5c293a6b
FC
2331 # built-in
2332 grep -q "^add \$" out &&
2333 # script
483e8611 2334 grep -q "^rebase \$" out &&
5c293a6b
FC
2335 # plumbing
2336 ! grep -q "^ls-files \$" out &&
2337
483e8611
EN
2338 run_completion "git r" &&
2339 ! grep -q -v "^r" out
5c293a6b
FC
2340'
2341
2342test_expect_success 'double dash "git" itself' '
2fbaf813 2343 test_completion "git --" <<-\EOF
5c293a6b
FC
2344 --paginate Z
2345 --no-pager Z
2346 --git-dir=
2347 --bare Z
2348 --version Z
2349 --exec-path Z
3ffcd086 2350 --exec-path=
5c293a6b 2351 --html-path Z
66fb37d0 2352 --man-path Z
69ef3c02 2353 --info-path Z
5c293a6b
FC
2354 --work-tree=
2355 --namespace=
69ef3c02 2356 --no-replace-objects Z
5c293a6b
FC
2357 --help Z
2358 EOF
5c293a6b
FC
2359'
2360
2361test_expect_success 'double dash "git checkout"' '
2fbaf813 2362 test_completion "git checkout --" <<-\EOF
5c293a6b 2363 --quiet Z
77afafb2
NTND
2364 --detach Z
2365 --track Z
2366 --orphan=Z
5c293a6b
FC
2367 --ours Z
2368 --theirs Z
5c293a6b 2369 --merge Z
77afafb2 2370 --conflict=Z
5c293a6b 2371 --patch Z
6357d9d0 2372 --ignore-skip-worktree-bits Z
77afafb2 2373 --ignore-other-worktrees Z
6357d9d0 2374 --recurse-submodules Z
77afafb2 2375 --progress Z
be4908f1
NTND
2376 --guess Z
2377 --no-guess Z
b221b5ab 2378 --no-... Z
091e04bc 2379 --overlay Z
a9aecc7a
AM
2380 --pathspec-file-nul Z
2381 --pathspec-from-file=Z
5c293a6b 2382 EOF
5c293a6b
FC
2383'
2384
69ef3c02
FC
2385test_expect_success 'general options' '
2386 test_completion "git --ver" "--version " &&
2387 test_completion "git --hel" "--help " &&
2fbaf813 2388 test_completion "git --exe" <<-\EOF &&
3ffcd086
JN
2389 --exec-path Z
2390 --exec-path=
2391 EOF
69ef3c02
FC
2392 test_completion "git --htm" "--html-path " &&
2393 test_completion "git --pag" "--paginate " &&
2394 test_completion "git --no-p" "--no-pager " &&
2395 test_completion "git --git" "--git-dir=" &&
2396 test_completion "git --wor" "--work-tree=" &&
2397 test_completion "git --nam" "--namespace=" &&
2398 test_completion "git --bar" "--bare " &&
2399 test_completion "git --inf" "--info-path " &&
2400 test_completion "git --no-r" "--no-replace-objects "
2401'
911d5da6
SG
2402
2403test_expect_success 'general options plus command' '
2404 test_completion "git --version check" "checkout " &&
2405 test_completion "git --paginate check" "checkout " &&
2406 test_completion "git --git-dir=foo check" "checkout " &&
2407 test_completion "git --bare check" "checkout " &&
911d5da6
SG
2408 test_completion "git --exec-path=foo check" "checkout " &&
2409 test_completion "git --html-path check" "checkout " &&
2410 test_completion "git --no-pager check" "checkout " &&
2411 test_completion "git --work-tree=foo check" "checkout " &&
2412 test_completion "git --namespace=foo check" "checkout " &&
2413 test_completion "git --paginate check" "checkout " &&
2414 test_completion "git --info-path check" "checkout " &&
336d694c
SG
2415 test_completion "git --no-replace-objects check" "checkout " &&
2416 test_completion "git --git-dir some/path check" "checkout " &&
2417 test_completion "git -c conf.var=value check" "checkout " &&
2418 test_completion "git -C some/path check" "checkout " &&
2419 test_completion "git --work-tree some/path check" "checkout " &&
2420 test_completion "git --namespace name/space check" "checkout "
911d5da6
SG
2421'
2422
50478223
JH
2423test_expect_success 'git --help completion' '
2424 test_completion "git --help ad" "add " &&
2425 test_completion "git --help core" "core-tutorial "
2426'
2427
057ab54b 2428test_expect_success 'completion.commands removes multiple commands' '
402e3e15
TZ
2429 test_config completion.commands "-cherry -mergetool" &&
2430 git --list-cmds=list-mainporcelain,list-complete,config >out &&
2431 ! grep -E "^(cherry|mergetool)$" out
2432'
2433
fb9cd420 2434test_expect_success 'setup for integration tests' '
49ba92b4
JK
2435 echo content >file1 &&
2436 echo more >file2 &&
e5edbef4 2437 git add file1 file2 &&
49ba92b4
JK
2438 git commit -m one &&
2439 git branch mybranch &&
2440 git tag mytag
2441'
2442
2443test_expect_success 'checkout completes ref names' '
2fbaf813 2444 test_completion "git checkout m" <<-\EOF
00076181 2445 main Z
43ea0812
FC
2446 mybranch Z
2447 mytag Z
49ba92b4
JK
2448 EOF
2449'
2450
9bab766f
AW
2451test_expect_success 'checkout does not match ref names of a different case' '
2452 test_completion "git checkout M" ""
2453'
2454
2455test_expect_success 'checkout matches case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2456 (
2457 GIT_COMPLETION_IGNORE_CASE=1 &&
2458 test_completion "git checkout M" <<-\EOF
2459 main Z
2460 mybranch Z
2461 mytag Z
2462 EOF
2463 )
2464'
2465
9de31f7b
AW
2466test_expect_success 'checkout completes pseudo refs' '
2467 test_completion "git checkout H" <<-\EOF
2468 HEAD Z
2469 EOF
2470'
2471
2472test_expect_success 'checkout completes pseudo refs case insensitively with GIT_COMPLETION_IGNORE_CASE' '
2473 (
2474 GIT_COMPLETION_IGNORE_CASE=1 &&
2475 test_completion "git checkout h" <<-\EOF
2476 HEAD Z
2477 EOF
2478 )
2479'
2480
80ac0744
SG
2481test_expect_success 'git -C <path> checkout uses the right repo' '
2482 test_completion "git -C subdir -C subsubdir -C .. -C ../otherrepo checkout b" <<-\EOF
2483 branch-in-other Z
2484 EOF
2485'
2486
49ba92b4 2487test_expect_success 'show completes all refs' '
2fbaf813 2488 test_completion "git show m" <<-\EOF
00076181 2489 main Z
43ea0812
FC
2490 mybranch Z
2491 mytag Z
49ba92b4
JK
2492 EOF
2493'
2494
2495test_expect_success '<ref>: completes paths' '
2fbaf813 2496 test_completion "git show mytag:f" <<-\EOF
6d54f528
CY
2497 file1Z
2498 file2Z
49ba92b4
JK
2499 EOF
2500'
2501
bafed0df
JK
2502test_expect_success 'complete tree filename with spaces' '
2503 echo content >"name with spaces" &&
e5edbef4 2504 git add "name with spaces" &&
bafed0df 2505 git commit -m spaces &&
2fbaf813 2506 test_completion "git show HEAD:nam" <<-\EOF
6d54f528 2507 name with spacesZ
bafed0df
JK
2508 EOF
2509'
2510
7d13e0a3 2511test_expect_success 'complete tree filename with metacharacters' '
bafed0df 2512 echo content >"name with \${meta}" &&
e5edbef4 2513 git add "name with \${meta}" &&
bafed0df 2514 git commit -m meta &&
2fbaf813 2515 test_completion "git show HEAD:nam" <<-\EOF
6d54f528
CY
2516 name with ${meta}Z
2517 name with spacesZ
bafed0df
JK
2518 EOF
2519'
2520
cb85fdf4
RH
2521test_expect_success 'symbolic-ref completes builtin options' '
2522 test_completion "git symbolic-ref --d" <<-\EOF
2523 --delete Z
2524 EOF
2525'
2526
2527test_expect_success 'symbolic-ref completes short ref names' '
2528 test_completion "git symbolic-ref foo m" <<-\EOF
2529 main Z
2530 mybranch Z
2531 mytag Z
2532 EOF
2533'
2534
2535test_expect_success 'symbolic-ref completes full ref names' '
2536 test_completion "git symbolic-ref foo refs/" <<-\EOF
2537 refs/heads/main Z
2538 refs/heads/mybranch Z
2539 refs/tags/mytag Z
2540 refs/tags/A Z
2541 EOF
2542'
2543
0365b9ec 2544test_expect_success PERL 'send-email' '
bf8e65b3
DL
2545 test_completion "git send-email --cov" <<-\EOF &&
2546 --cover-from-description=Z
2547 --cover-letter Z
2548 EOF
2b7b7585
TP
2549 test_completion "git send-email --val" <<-\EOF &&
2550 --validate Z
2551 EOF
00076181 2552 test_completion "git send-email ma" "main "
2f65494d
FC
2553'
2554
ddf07bdd
FC
2555test_expect_success 'complete files' '
2556 git init tmp && cd tmp &&
2557 test_when_finished "cd .. && rm -rf tmp" &&
2558
2559 echo "expected" > .gitignore &&
2560 echo "out" >> .gitignore &&
9703797c 2561 echo "out_sorted" >> .gitignore &&
ddf07bdd
FC
2562
2563 git add .gitignore &&
2564 test_completion "git commit " ".gitignore" &&
2565
2566 git commit -m ignore &&
2567
2568 touch new &&
2569 test_completion "git add " "new" &&
2570
2571 git add new &&
2572 git commit -a -m new &&
2573 test_completion "git add " "" &&
2574
2575 git mv new modified &&
2576 echo modify > modified &&
2577 test_completion "git add " "modified" &&
2578
c0af173a
EN
2579 mkdir -p some/deep &&
2580 touch some/deep/path &&
2581 test_completion "git add some/" "some/deep" &&
2582 git clean -f some &&
2583
ddf07bdd
FC
2584 touch untracked &&
2585
2586 : TODO .gitignore should not be here &&
2587 test_completion "git rm " <<-\EOF &&
2588 .gitignore
2589 modified
2590 EOF
2591
2592 test_completion "git clean " "untracked" &&
2593
2594 : TODO .gitignore should not be here &&
2595 test_completion "git mv " <<-\EOF &&
2596 .gitignore
2597 modified
2598 EOF
2599
2600 mkdir dir &&
2601 touch dir/file-in-dir &&
2602 git add dir/file-in-dir &&
2603 git commit -m dir &&
2604
2605 mkdir untracked-dir &&
2606
2607 : TODO .gitignore should not be here &&
2608 test_completion "git mv modified " <<-\EOF &&
2609 .gitignore
2610 dir
2611 modified
2612 untracked
2613 untracked-dir
2614 EOF
2615
2616 test_completion "git commit " "modified" &&
2617
2618 : TODO .gitignore should not be here &&
8fb26872 2619 test_completion "git ls-files " <<-\EOF &&
ddf07bdd
FC
2620 .gitignore
2621 dir
2622 modified
2623 EOF
2624
2625 touch momified &&
2626 test_completion "git add mom" "momified"
2627'
2628
9414938c
FC
2629test_expect_success "simple alias" '
2630 test_config alias.co checkout &&
2631 test_completion "git co m" <<-\EOF
00076181 2632 main Z
9414938c
FC
2633 mybranch Z
2634 mytag Z
2635 EOF
2636'
2637
2638test_expect_success "recursive alias" '
2639 test_config alias.co checkout &&
2640 test_config alias.cod "co --detached" &&
2641 test_completion "git cod m" <<-\EOF
00076181 2642 main Z
9414938c
FC
2643 mybranch Z
2644 mytag Z
2645 EOF
2646'
2647
56f24e80
SP
2648test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
2649 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
2650 test_completion "git co m" <<-\EOF
00076181 2651 main Z
56f24e80
SP
2652 mybranch Z
2653 mytag Z
2654 EOF
2655'
2656
2657test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
2658 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
2659 test_completion "git co m" <<-\EOF
00076181 2660 main Z
56f24e80
SP
2661 mybranch Z
2662 mytag Z
2663 EOF
2664'
2665
2666test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
2667 test_config alias.co "!f() { : git checkout ; if ... } f" &&
2668 test_completion "git co m" <<-\EOF
00076181 2669 main Z
56f24e80
SP
2670 mybranch Z
2671 mytag Z
2672 EOF
2673'
2674
8d73a2cc
JH
2675test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd> ; ... }' '
2676 test_config alias.co "!f() { : checkout ; if ... } f" &&
2677 test_completion "git co m" <<-\EOF
2678 main Z
2679 mybranch Z
2680 mytag Z
2681 EOF
2682'
2683
2684test_expect_success 'completion used <cmd> completion for alias: !f() { : <cmd>; ... }' '
2685 test_config alias.co "!f() { : checkout; if ... } f" &&
2686 test_completion "git co m" <<-\EOF
2687 main Z
2688 mybranch Z
2689 mytag Z
2690 EOF
2691'
2692
9f642a71
NTND
2693test_expect_success 'completion without explicit _git_xxx function' '
2694 test_completion "git version --" <<-\EOF
2695 --build-options Z
2b1c01d2 2696 --no-build-options Z
9f642a71
NTND
2697 EOF
2698'
2699
f03efba4
FC
2700test_expect_failure 'complete with tilde expansion' '
2701 git init tmp && cd tmp &&
2702 test_when_finished "cd .. && rm -rf tmp" &&
2703
2704 touch ~/tmp/file &&
2705
2706 test_completion "git add ~/tmp/" "~/tmp/file"
2707'
2708
723c1d52
ÆAB
2709test_expect_success 'setup other remote for remote reference completion' '
2710 git remote add other otherrepo &&
2711 git fetch other
2712'
2713
2714for flag in -d --delete
2715do
2716 test_expect_success "__git_complete_remote_or_refspec - push $flag other" '
2717 sed -e "s/Z$//" >expected <<-EOF &&
00076181 2718 main-in-other Z
723c1d52
ÆAB
2719 EOF
2720 (
2721 words=(git push '$flag' other ma) &&
2722 cword=${#words[@]} cur=${words[cword-1]} &&
59d85a2a 2723 __git_cmd_idx=1 &&
723c1d52
ÆAB
2724 __git_complete_remote_or_refspec &&
2725 print_comp
2726 ) &&
2727 test_cmp expected out
2728 '
2729
2730 test_expect_failure "__git_complete_remote_or_refspec - push other $flag" '
2731 sed -e "s/Z$//" >expected <<-EOF &&
00076181 2732 main-in-other Z
723c1d52
ÆAB
2733 EOF
2734 (
2735 words=(git push other '$flag' ma) &&
2736 cword=${#words[@]} cur=${words[cword-1]} &&
59d85a2a 2737 __git_cmd_idx=1 &&
723c1d52
ÆAB
2738 __git_complete_remote_or_refspec &&
2739 print_comp
2740 ) &&
2741 test_cmp expected out
2742 '
2743done
2744
d9438873 2745test_expect_success 'git config - section' '
7a09a8f0
SG
2746 test_completion "git config br" <<-\EOF
2747 branch.Z
2748 browser.Z
2749 EOF
2750'
2751
07aed580
MB
2752test_expect_success 'git config - section include, includeIf' '
2753 test_completion "git config inclu" <<-\EOF
2754 include.Z
2755 includeIf.Z
2756 EOF
2757'
2758
7a09a8f0
SG
2759test_expect_success 'git config - variable name' '
2760 test_completion "git config log.d" <<-\EOF
2761 log.date Z
2762 log.decorate Z
17c13e60 2763 log.diffMerges Z
7a09a8f0
SG
2764 EOF
2765'
2766
07aed580
MB
2767test_expect_success 'git config - variable name include' '
2768 test_completion "git config include.p" <<-\EOF
2769 include.path Z
2770 EOF
2771'
2772
b1d0cc68
PB
2773test_expect_success 'setup for git config submodule tests' '
2774 test_create_repo sub &&
2775 test_commit -C sub initial &&
2776 git submodule add ./sub
2777'
2778
1e0ee408 2779test_expect_success 'git config - variable name - submodule and __git_compute_first_level_config_vars_for_section' '
b1d0cc68
PB
2780 test_completion "git config submodule." <<-\EOF
2781 submodule.active Z
2782 submodule.alternateErrorStrategy Z
2783 submodule.alternateLocation Z
2784 submodule.fetchJobs Z
2785 submodule.propagateBranches Z
2786 submodule.recurse Z
2787 submodule.sub.Z
2788 EOF
2789'
2790
6e32f718 2791test_expect_success 'git config - variable name - __git_compute_second_level_config_vars_for_section' '
b1d0cc68
PB
2792 test_completion "git config submodule.sub." <<-\EOF
2793 submodule.sub.url Z
2794 submodule.sub.update Z
2795 submodule.sub.branch Z
2796 submodule.sub.fetchRecurseSubmodules Z
2797 submodule.sub.ignore Z
2798 submodule.sub.active Z
2799 EOF
2800'
2801
7a09a8f0
SG
2802test_expect_success 'git config - value' '
2803 test_completion "git config color.pager " <<-\EOF
2804 false Z
2805 true Z
2806 EOF
2807'
2808
e1e00089
SG
2809test_expect_success 'git -c - section' '
2810 test_completion "git -c br" <<-\EOF
2811 branch.Z
2812 browser.Z
2813 EOF
2814'
2815
2816test_expect_success 'git -c - variable name' '
2817 test_completion "git -c log.d" <<-\EOF
2818 log.date=Z
2819 log.decorate=Z
17c13e60 2820 log.diffMerges=Z
e1e00089
SG
2821 EOF
2822'
2823
dd334728
SG
2824test_expect_success 'git -c - value' '
2825 test_completion "git -c color.pager=" <<-\EOF
5af9d5f6
SG
2826 false Z
2827 true Z
2828 EOF
2829'
2830
2831test_expect_success 'git clone --config= - section' '
2832 test_completion "git clone --config=br" <<-\EOF
2833 branch.Z
2834 browser.Z
2835 EOF
2836'
2837
2838test_expect_success 'git clone --config= - variable name' '
2839 test_completion "git clone --config=log.d" <<-\EOF
2840 log.date=Z
2841 log.decorate=Z
17c13e60 2842 log.diffMerges=Z
5af9d5f6
SG
2843 EOF
2844'
2845
2846test_expect_success 'git clone --config= - value' '
2847 test_completion "git clone --config=color.pager=" <<-\EOF
dd334728
SG
2848 false Z
2849 true Z
2850 EOF
2851'
2852
85452a1d
RJ
2853test_expect_success 'git reflog show' '
2854 test_when_finished "git checkout - && git branch -d shown" &&
2855 git checkout -b shown &&
2856 test_completion "git reflog sho" <<-\EOF &&
2857 show Z
2858 shown Z
2859 EOF
2860 test_completion "git reflog show sho" "shown " &&
c689c38b
RJ
2861 test_completion "git reflog shown sho" "shown " &&
2862 test_completion "git reflog --unt" "--until=" &&
2863 test_completion "git reflog show --unt" "--until=" &&
2864 test_completion "git reflog shown --unt" "--until="
85452a1d
RJ
2865'
2866
e9f2118d
FC
2867test_expect_success 'options with value' '
2868 test_completion "git merge -X diff-algorithm=" <<-\EOF
2869
2870 EOF
2871'
2872
8b0eaa41 2873test_expect_success 'sourcing the completion script clears cached commands' '
59d9442f
ÆAB
2874 (
2875 __git_compute_all_commands &&
8ddfce71 2876 test -n "$__git_all_commands" &&
59d9442f 2877 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
8ddfce71 2878 test -z "$__git_all_commands"
59d9442f 2879 )
8b0eaa41
SG
2880'
2881
6cdccfce 2882test_expect_success 'sourcing the completion script clears cached merge strategies' '
59d9442f
ÆAB
2883 (
2884 __git_compute_merge_strategies &&
8ddfce71 2885 test -n "$__git_merge_strategies" &&
59d9442f 2886 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
8ddfce71 2887 test -z "$__git_merge_strategies"
59d9442f 2888 )
8b0eaa41
SG
2889'
2890
2891test_expect_success 'sourcing the completion script clears cached --options' '
59d9442f
ÆAB
2892 (
2893 __gitcomp_builtin checkout &&
8ddfce71 2894 test -n "$__gitcomp_builtin_checkout" &&
59d9442f 2895 __gitcomp_builtin notes_edit &&
8ddfce71 2896 test -n "$__gitcomp_builtin_notes_edit" &&
59d9442f 2897 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
8ddfce71
JK
2898 test -z "$__gitcomp_builtin_checkout" &&
2899 test -z "$__gitcomp_builtin_notes_edit"
59d9442f 2900 )
8b0eaa41
SG
2901'
2902
ca2d62b7
PB
2903test_expect_success 'option aliases are not shown by default' '
2904 test_completion "git clone --recurs" "--recurse-submodules "
2905'
2906
2907test_expect_success 'option aliases are shown with GIT_COMPLETION_SHOW_ALL' '
59d9442f
ÆAB
2908 (
2909 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2910 GIT_COMPLETION_SHOW_ALL=1 && export GIT_COMPLETION_SHOW_ALL &&
2911 test_completion "git clone --recurs" <<-\EOF
2912 --recurse-submodules Z
2913 --recursive Z
2914 EOF
2915 )
ca2d62b7
PB
2916'
2917
d9f88dd8
ÆAB
2918test_expect_success 'plumbing commands are excluded without GIT_COMPLETION_SHOW_ALL_COMMANDS' '
2919 (
2920 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2921 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2922
2923 # Just mainporcelain, not plumbing commands
2924 run_completion "git c" &&
2925 grep checkout out &&
2926 ! grep cat-file out
2927 )
2928'
2929
2930test_expect_success 'all commands are shown with GIT_COMPLETION_SHOW_ALL_COMMANDS (also main non-builtin)' '
2931 (
2932 . "$GIT_BUILD_DIR/contrib/completion/git-completion.bash" &&
2933 GIT_COMPLETION_SHOW_ALL_COMMANDS=1 &&
2934 export GIT_COMPLETION_SHOW_ALL_COMMANDS &&
2935 sane_unset GIT_TESTING_PORCELAIN_COMMAND_LIST &&
2936
2937 # Both mainporcelain and plumbing commands
2938 run_completion "git c" &&
2939 grep checkout out &&
2940 grep cat-file out &&
2941
2942 # Check "gitk", a "main" command, but not a built-in + more plumbing
2943 run_completion "git g" &&
2944 grep gitk out &&
2945 grep get-tar-commit-id out
2946 )
ca2d62b7
PB
2947'
2948
0e02bdc1
FC
2949test_expect_success '__git_complete' '
2950 unset -f __git_wrap__git_main &&
5a067ba9 2951
0e02bdc1
FC
2952 __git_complete foo __git_main &&
2953 __git_have_func __git_wrap__git_main &&
5a067ba9
FC
2954 unset -f __git_wrap__git_main &&
2955
0e02bdc1 2956 __git_complete gf _git_fetch &&
5a067ba9
FC
2957 __git_have_func __git_wrap_git_fetch &&
2958
2959 __git_complete foo git &&
2960 __git_have_func __git_wrap__git_main &&
2961 unset -f __git_wrap__git_main &&
2962
2963 __git_complete gd git_diff &&
2964 __git_have_func __git_wrap_git_diff &&
2965
2966 test_must_fail __git_complete ga missing
0e02bdc1
FC
2967'
2968
9a9c3113
PS
2969test_expect_success '__git_pseudoref_exists' '
2970 test_when_finished "rm -rf repo" &&
2971 git init repo &&
2972 (
2973 cd repo &&
2974 sane_unset __git_repo_path &&
2975
020e0a08
PS
2976 # HEAD should exist, even if it points to an unborn branch.
2977 __git_pseudoref_exists HEAD >output 2>&1 &&
2978 test_must_be_empty output &&
2979
9a9c3113
PS
2980 # HEAD points to an existing branch, so it should exist.
2981 test_commit A &&
2982 __git_pseudoref_exists HEAD >output 2>&1 &&
2983 test_must_be_empty output &&
2984
2985 # CHERRY_PICK_HEAD does not exist, so the existence check should fail.
2986 ! __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
2987 test_must_be_empty output &&
2988
2989 # CHERRY_PICK_HEAD points to a commit, so it should exist.
2990 git update-ref CHERRY_PICK_HEAD A &&
2991 __git_pseudoref_exists CHERRY_PICK_HEAD >output 2>&1 &&
2992 test_must_be_empty output
2993 )
2994'
2995
5c293a6b 2996test_done