]> git.ipfire.org Git - thirdparty/git.git/blame - t/t9902-completion.sh
completion: improve __git_refs()'s in-code documentation
[thirdparty/git.git] / t / t9902-completion.sh
CommitLineData
5c293a6b
FC
1#!/bin/sh
2#
3# Copyright (c) 2012 Felipe Contreras
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
50478223
JH
16# Be careful when updating this list:
17#
18# (1) The build tree may have build artifact from different branch, or
19# the user's $PATH may have a random executable that may begin
20# with "git-check" that are not part of the subcommands this build
21# will ship, e.g. "check-ignore". The tests for completion for
22# subcommand names tests how "check" is expanded; we limit the
23# possible candidates to "checkout" and "check-attr" to make sure
24# "check-attr", which is known by the filter function as a
25# subcommand to be thrown out, while excluding other random files
26# that happen to begin with "check" to avoid letting them get in
27# the way.
28#
29# (2) A test makes sure that common subcommands are included in the
30# completion for "git <TAB>", and a plumbing is excluded. "add",
31# "filter-branch" and "ls-files" are listed for this.
32
33GIT_TESTING_COMMAND_COMPLETION='add checkout check-attr filter-branch ls-files'
34
5c293a6b
FC
35. "$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
36
37# We don't need this function to actually join words or do anything special.
38# Also, it's cleaner to avoid touching bash's internal completion variables.
39# So let's override it with a minimal version for testing purposes.
40_get_comp_words_by_ref ()
41{
42 while [ $# -gt 0 ]; do
43 case "$1" in
44 cur)
45 cur=${_words[_cword]}
46 ;;
47 prev)
48 prev=${_words[_cword-1]}
49 ;;
50 words)
51 words=("${_words[@]}")
52 ;;
53 cword)
54 cword=$_cword
55 ;;
56 esac
57 shift
58 done
59}
60
61print_comp ()
62{
63 local IFS=$'\n'
64 echo "${COMPREPLY[*]}" > out
65}
66
67run_completion ()
68{
69 local -a COMPREPLY _words
70 local _cword
71 _words=( $1 )
0ef09702 72 test "${1: -1}" = ' ' && _words[${#_words[@]}+1]=''
5c293a6b 73 (( _cword = ${#_words[@]} - 1 ))
93b291e0 74 __git_wrap__git_main && print_comp
5c293a6b
FC
75}
76
701ecdf1
FC
77# Test high-level completion
78# Arguments are:
79# 1: typed text so far (cur)
80# 2: expected completion
5c293a6b
FC
81test_completion ()
82{
2fbaf813
FC
83 if test $# -gt 1
84 then
85 printf '%s\n' "$2" >expected
86 else
87 sed -e 's/Z$//' >expected
88 fi &&
701ecdf1 89 run_completion "$1" &&
5c293a6b
FC
90 test_cmp expected out
91}
92
e4615238
FC
93# Test __gitcomp.
94# The first argument is the typed text so far (cur); the rest are
95# passed to __gitcomp. Expected output comes is read from the
96# standard input, like test_completion().
97test_gitcomp ()
49ba92b4 98{
17393033 99 local -a COMPREPLY &&
e4615238 100 sed -e 's/Z$//' >expected &&
17393033
FC
101 cur="$1" &&
102 shift &&
103 __gitcomp "$@" &&
104 print_comp &&
74a8c849 105 test_cmp expected out
49ba92b4
JK
106}
107
43369a22
FC
108# Test __gitcomp_nl
109# Arguments are:
110# 1: current word (cur)
111# -: the rest are passed to __gitcomp_nl
112test_gitcomp_nl ()
113{
114 local -a COMPREPLY &&
115 sed -e 's/Z$//' >expected &&
116 cur="$1" &&
117 shift &&
118 __gitcomp_nl "$@" &&
119 print_comp &&
120 test_cmp expected out
121}
122
123invalid_variable_name='${foo.bar}'
124
c9a102e8
SG
125actual="$TRASH_DIRECTORY/actual"
126
127test_expect_success 'setup for __gitdir tests' '
128 mkdir -p subdir/subsubdir &&
129 git init otherrepo
130'
131
132test_expect_success '__gitdir - from command line (through $__git_dir)' '
133 echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
134 (
135 __git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
136 __gitdir >"$actual"
137 ) &&
138 test_cmp expected "$actual"
139'
140
141test_expect_success '__gitdir - repo as argument' '
142 echo "otherrepo/.git" >expected &&
143 __gitdir "otherrepo" >"$actual" &&
144 test_cmp expected "$actual"
145'
146
147test_expect_success '__gitdir - remote as argument' '
148 echo "remote" >expected &&
149 __gitdir "remote" >"$actual" &&
150 test_cmp expected "$actual"
151'
152
153test_expect_success '__gitdir - .git directory in cwd' '
154 echo ".git" >expected &&
155 __gitdir >"$actual" &&
156 test_cmp expected "$actual"
157'
158
159test_expect_success '__gitdir - .git directory in parent' '
160 echo "$(pwd -P)/.git" >expected &&
161 (
162 cd subdir/subsubdir &&
163 __gitdir >"$actual"
164 ) &&
165 test_cmp expected "$actual"
166'
167
168test_expect_success '__gitdir - cwd is a .git directory' '
169 echo "." >expected &&
170 (
171 cd .git &&
172 __gitdir >"$actual"
173 ) &&
174 test_cmp expected "$actual"
175'
176
177test_expect_success '__gitdir - parent is a .git directory' '
178 echo "$(pwd -P)/.git" >expected &&
179 (
180 cd .git/refs/heads &&
181 __gitdir >"$actual"
182 ) &&
183 test_cmp expected "$actual"
184'
185
186test_expect_success '__gitdir - $GIT_DIR set while .git directory in cwd' '
187 echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
188 (
189 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
190 export GIT_DIR &&
191 __gitdir >"$actual"
192 ) &&
193 test_cmp expected "$actual"
194'
195
196test_expect_success '__gitdir - $GIT_DIR set while .git directory in parent' '
197 echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
198 (
199 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
200 export GIT_DIR &&
201 cd subdir &&
202 __gitdir >"$actual"
203 ) &&
204 test_cmp expected "$actual"
205'
206
207test_expect_success '__gitdir - non-existing $GIT_DIR' '
208 (
209 GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
210 export GIT_DIR &&
211 test_must_fail __gitdir
212 )
213'
214
44cf1c0e
PT
215function pwd_P_W () {
216 if test_have_prereq MINGW
217 then
218 pwd -W
219 else
220 pwd -P
221 fi
222}
223
c9a102e8 224test_expect_success '__gitdir - gitfile in cwd' '
44cf1c0e
PT
225 echo "$(pwd_P_W)/otherrepo/.git" >expected &&
226 echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git &&
c9a102e8
SG
227 test_when_finished "rm -f subdir/.git" &&
228 (
229 cd subdir &&
230 __gitdir >"$actual"
231 ) &&
232 test_cmp expected "$actual"
233'
234
235test_expect_success '__gitdir - gitfile in parent' '
44cf1c0e
PT
236 echo "$(pwd_P_W)/otherrepo/.git" >expected &&
237 echo "gitdir: $(pwd_P_W)/otherrepo/.git" >subdir/.git &&
c9a102e8
SG
238 test_when_finished "rm -f subdir/.git" &&
239 (
240 cd subdir/subsubdir &&
241 __gitdir >"$actual"
242 ) &&
243 test_cmp expected "$actual"
244'
245
246test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '
247 echo "$(pwd -P)/otherrepo/.git" >expected &&
248 mkdir otherrepo/dir &&
249 test_when_finished "rm -rf otherrepo/dir" &&
250 ln -s otherrepo/dir link &&
251 test_when_finished "rm -f link" &&
252 (
253 cd link &&
254 __gitdir >"$actual"
255 ) &&
256 test_cmp expected "$actual"
257'
258
259test_expect_success '__gitdir - not a git repository' '
a3c45d12 260 nongit test_must_fail __gitdir
c9a102e8
SG
261'
262
74a8c849 263test_expect_success '__gitcomp - trailing space - options' '
e4615238
FC
264 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
265 --reset-author" <<-EOF
74a8c849
SG
266 --reuse-message=Z
267 --reedit-message=Z
268 --reset-author Z
269 EOF
74a8c849
SG
270'
271
272test_expect_success '__gitcomp - trailing space - config keys' '
e4615238
FC
273 test_gitcomp "br" "branch. branch.autosetupmerge
274 branch.autosetuprebase browser." <<-\EOF
74a8c849
SG
275 branch.Z
276 branch.autosetupmerge Z
277 branch.autosetuprebase Z
278 browser.Z
279 EOF
74a8c849
SG
280'
281
282test_expect_success '__gitcomp - option parameter' '
e4615238
FC
283 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
284 "" "re" <<-\EOF
74a8c849
SG
285 recursive Z
286 resolve Z
287 EOF
74a8c849
SG
288'
289
290test_expect_success '__gitcomp - prefix' '
e4615238
FC
291 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
292 "branch.maint." "me" <<-\EOF
74a8c849
SG
293 branch.maint.merge Z
294 branch.maint.mergeoptions Z
295 EOF
74a8c849
SG
296'
297
298test_expect_success '__gitcomp - suffix' '
e4615238
FC
299 test_gitcomp "branch.me" "master maint next pu" "branch." \
300 "ma" "." <<-\EOF
74a8c849
SG
301 branch.master.Z
302 branch.maint.Z
303 EOF
74a8c849
SG
304'
305
7d13e0a3 306test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
43369a22
FC
307 __gitcomp "$invalid_variable_name"
308'
309
310read -r -d "" refs <<-\EOF
311maint
312master
313next
314pu
315EOF
316
317test_expect_success '__gitcomp_nl - trailing space' '
318 test_gitcomp_nl "m" "$refs" <<-EOF
319 maint Z
320 master Z
321 EOF
322'
323
324test_expect_success '__gitcomp_nl - prefix' '
325 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
326 --fixup=maint Z
327 --fixup=master Z
328 EOF
329'
330
331test_expect_success '__gitcomp_nl - suffix' '
332 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
333 branch.maint.Z
334 branch.master.Z
335 EOF
336'
337
338test_expect_success '__gitcomp_nl - no suffix' '
339 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
340 maintZ
341 masterZ
342 EOF
343'
344
7d13e0a3 345test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
43369a22
FC
346 __gitcomp_nl "$invalid_variable_name"
347'
348
2acc1940
SG
349test_expect_success '__git_remotes - list remotes from $GIT_DIR/remotes and from config file' '
350 cat >expect <<-EOF &&
351 remote_from_file_1
352 remote_from_file_2
353 remote_in_config_1
354 remote_in_config_2
355 EOF
356 test_when_finished "rm -rf .git/remotes" &&
357 mkdir -p .git/remotes &&
358 >.git/remotes/remote_from_file_1 &&
359 >.git/remotes/remote_from_file_2 &&
360 test_when_finished "git remote remove remote_in_config_1" &&
361 git remote add remote_in_config_1 git://remote_1 &&
362 test_when_finished "git remote remove remote_in_config_2" &&
363 git remote add remote_in_config_2 git://remote_2 &&
364 __git_remotes >actual &&
365 test_cmp expect actual
366'
367
12bdc880
SG
368test_expect_success '__git_get_config_variables' '
369 cat >expect <<-EOF &&
370 name-1
371 name-2
372 EOF
373 test_config interesting.name-1 good &&
374 test_config interesting.name-2 good &&
375 test_config subsection.interesting.name-3 bad &&
376 __git_get_config_variables interesting >actual &&
377 test_cmp expect actual
378'
379
e8f9e428
SG
380test_expect_success '__git_pretty_aliases' '
381 cat >expect <<-EOF &&
382 author
383 hash
384 EOF
385 test_config pretty.author "%an %ae" &&
386 test_config pretty.hash %H &&
387 __git_pretty_aliases >actual &&
388 test_cmp expect actual
389'
390
391test_expect_success '__git_aliases' '
392 cat >expect <<-EOF &&
393 ci
394 co
395 EOF
396 test_config alias.ci commit &&
397 test_config alias.co checkout &&
398 __git_aliases >actual &&
399 test_cmp expect actual
400'
401
5c293a6b 402test_expect_success 'basic' '
cdbff7d6 403 run_completion "git " &&
5c293a6b
FC
404 # built-in
405 grep -q "^add \$" out &&
406 # script
407 grep -q "^filter-branch \$" out &&
408 # plumbing
409 ! grep -q "^ls-files \$" out &&
410
411 run_completion "git f" &&
412 ! grep -q -v "^f" out
413'
414
415test_expect_success 'double dash "git" itself' '
2fbaf813 416 test_completion "git --" <<-\EOF
5c293a6b
FC
417 --paginate Z
418 --no-pager Z
419 --git-dir=
420 --bare Z
421 --version Z
422 --exec-path Z
3ffcd086 423 --exec-path=
5c293a6b 424 --html-path Z
66fb37d0 425 --man-path Z
69ef3c02 426 --info-path Z
5c293a6b
FC
427 --work-tree=
428 --namespace=
69ef3c02 429 --no-replace-objects Z
5c293a6b
FC
430 --help Z
431 EOF
5c293a6b
FC
432'
433
434test_expect_success 'double dash "git checkout"' '
2fbaf813 435 test_completion "git checkout --" <<-\EOF
5c293a6b
FC
436 --quiet Z
437 --ours Z
438 --theirs Z
439 --track Z
440 --no-track Z
441 --merge Z
442 --conflict=
443 --orphan Z
444 --patch Z
445 EOF
5c293a6b
FC
446'
447
69ef3c02
FC
448test_expect_success 'general options' '
449 test_completion "git --ver" "--version " &&
450 test_completion "git --hel" "--help " &&
2fbaf813 451 test_completion "git --exe" <<-\EOF &&
3ffcd086
JN
452 --exec-path Z
453 --exec-path=
454 EOF
69ef3c02
FC
455 test_completion "git --htm" "--html-path " &&
456 test_completion "git --pag" "--paginate " &&
457 test_completion "git --no-p" "--no-pager " &&
458 test_completion "git --git" "--git-dir=" &&
459 test_completion "git --wor" "--work-tree=" &&
460 test_completion "git --nam" "--namespace=" &&
461 test_completion "git --bar" "--bare " &&
462 test_completion "git --inf" "--info-path " &&
463 test_completion "git --no-r" "--no-replace-objects "
464'
911d5da6
SG
465
466test_expect_success 'general options plus command' '
467 test_completion "git --version check" "checkout " &&
468 test_completion "git --paginate check" "checkout " &&
469 test_completion "git --git-dir=foo check" "checkout " &&
470 test_completion "git --bare check" "checkout " &&
911d5da6
SG
471 test_completion "git --exec-path=foo check" "checkout " &&
472 test_completion "git --html-path check" "checkout " &&
473 test_completion "git --no-pager check" "checkout " &&
474 test_completion "git --work-tree=foo check" "checkout " &&
475 test_completion "git --namespace=foo check" "checkout " &&
476 test_completion "git --paginate check" "checkout " &&
477 test_completion "git --info-path check" "checkout " &&
478 test_completion "git --no-replace-objects check" "checkout "
479'
480
50478223
JH
481test_expect_success 'git --help completion' '
482 test_completion "git --help ad" "add " &&
483 test_completion "git --help core" "core-tutorial "
484'
485
49ba92b4
JK
486test_expect_success 'setup for ref completion' '
487 echo content >file1 &&
488 echo more >file2 &&
489 git add . &&
490 git commit -m one &&
491 git branch mybranch &&
492 git tag mytag
493'
494
495test_expect_success 'checkout completes ref names' '
2fbaf813 496 test_completion "git checkout m" <<-\EOF
43ea0812
FC
497 master Z
498 mybranch Z
499 mytag Z
49ba92b4
JK
500 EOF
501'
502
503test_expect_success 'show completes all refs' '
2fbaf813 504 test_completion "git show m" <<-\EOF
43ea0812
FC
505 master Z
506 mybranch Z
507 mytag Z
49ba92b4
JK
508 EOF
509'
510
511test_expect_success '<ref>: completes paths' '
2fbaf813 512 test_completion "git show mytag:f" <<-\EOF
43ea0812
FC
513 file1 Z
514 file2 Z
49ba92b4
JK
515 EOF
516'
517
bafed0df
JK
518test_expect_success 'complete tree filename with spaces' '
519 echo content >"name with spaces" &&
520 git add . &&
521 git commit -m spaces &&
2fbaf813 522 test_completion "git show HEAD:nam" <<-\EOF
43ea0812 523 name with spaces Z
bafed0df
JK
524 EOF
525'
526
7d13e0a3 527test_expect_success 'complete tree filename with metacharacters' '
bafed0df
JK
528 echo content >"name with \${meta}" &&
529 git add . &&
530 git commit -m meta &&
2fbaf813 531 test_completion "git show HEAD:nam" <<-\EOF
43ea0812
FC
532 name with ${meta} Z
533 name with spaces Z
bafed0df
JK
534 EOF
535'
536
2f65494d
FC
537test_expect_success 'send-email' '
538 test_completion "git send-email --cov" "--cover-letter " &&
539 test_completion "git send-email ma" "master "
540'
541
ddf07bdd
FC
542test_expect_success 'complete files' '
543 git init tmp && cd tmp &&
544 test_when_finished "cd .. && rm -rf tmp" &&
545
546 echo "expected" > .gitignore &&
547 echo "out" >> .gitignore &&
548
549 git add .gitignore &&
550 test_completion "git commit " ".gitignore" &&
551
552 git commit -m ignore &&
553
554 touch new &&
555 test_completion "git add " "new" &&
556
557 git add new &&
558 git commit -a -m new &&
559 test_completion "git add " "" &&
560
561 git mv new modified &&
562 echo modify > modified &&
563 test_completion "git add " "modified" &&
564
565 touch untracked &&
566
567 : TODO .gitignore should not be here &&
568 test_completion "git rm " <<-\EOF &&
569 .gitignore
570 modified
571 EOF
572
573 test_completion "git clean " "untracked" &&
574
575 : TODO .gitignore should not be here &&
576 test_completion "git mv " <<-\EOF &&
577 .gitignore
578 modified
579 EOF
580
581 mkdir dir &&
582 touch dir/file-in-dir &&
583 git add dir/file-in-dir &&
584 git commit -m dir &&
585
586 mkdir untracked-dir &&
587
588 : TODO .gitignore should not be here &&
589 test_completion "git mv modified " <<-\EOF &&
590 .gitignore
591 dir
592 modified
593 untracked
594 untracked-dir
595 EOF
596
597 test_completion "git commit " "modified" &&
598
599 : TODO .gitignore should not be here &&
8fb26872 600 test_completion "git ls-files " <<-\EOF &&
ddf07bdd
FC
601 .gitignore
602 dir
603 modified
604 EOF
605
606 touch momified &&
607 test_completion "git add mom" "momified"
608'
609
56f24e80
SP
610test_expect_success "completion uses <cmd> completion for alias: !sh -c 'git <cmd> ...'" '
611 test_config alias.co "!sh -c '"'"'git checkout ...'"'"'" &&
612 test_completion "git co m" <<-\EOF
613 master Z
614 mybranch Z
615 mytag Z
616 EOF
617'
618
619test_expect_success 'completion uses <cmd> completion for alias: !f () { VAR=val git <cmd> ... }' '
620 test_config alias.co "!f () { VAR=val git checkout ... ; } f" &&
621 test_completion "git co m" <<-\EOF
622 master Z
623 mybranch Z
624 mytag Z
625 EOF
626'
627
628test_expect_success 'completion used <cmd> completion for alias: !f() { : git <cmd> ; ... }' '
629 test_config alias.co "!f() { : git checkout ; if ... } f" &&
630 test_completion "git co m" <<-\EOF
631 master Z
632 mybranch Z
633 mytag Z
634 EOF
635'
636
f03efba4
FC
637test_expect_failure 'complete with tilde expansion' '
638 git init tmp && cd tmp &&
639 test_when_finished "cd .. && rm -rf tmp" &&
640
641 touch ~/tmp/file &&
642
643 test_completion "git add ~/tmp/" "~/tmp/file"
644'
645
5c293a6b 646test_done