]> git.ipfire.org Git - thirdparty/git.git/blame - t/t9902-completion.sh
Git 2.0
[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
215test_expect_success '__gitdir - gitfile in cwd' '
216 echo "$(pwd -P)/otherrepo/.git" >expected &&
217 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" >subdir/.git &&
218 test_when_finished "rm -f subdir/.git" &&
219 (
220 cd subdir &&
221 __gitdir >"$actual"
222 ) &&
223 test_cmp expected "$actual"
224'
225
226test_expect_success '__gitdir - gitfile in parent' '
227 echo "$(pwd -P)/otherrepo/.git" >expected &&
228 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" >subdir/.git &&
229 test_when_finished "rm -f subdir/.git" &&
230 (
231 cd subdir/subsubdir &&
232 __gitdir >"$actual"
233 ) &&
234 test_cmp expected "$actual"
235'
236
237test_expect_success SYMLINKS '__gitdir - resulting path avoids symlinks' '
238 echo "$(pwd -P)/otherrepo/.git" >expected &&
239 mkdir otherrepo/dir &&
240 test_when_finished "rm -rf otherrepo/dir" &&
241 ln -s otherrepo/dir link &&
242 test_when_finished "rm -f link" &&
243 (
244 cd link &&
245 __gitdir >"$actual"
246 ) &&
247 test_cmp expected "$actual"
248'
249
250test_expect_success '__gitdir - not a git repository' '
251 (
252 cd subdir/subsubdir &&
253 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
254 export GIT_CEILING_DIRECTORIES &&
255 test_must_fail __gitdir
256 )
257'
258
74a8c849 259test_expect_success '__gitcomp - trailing space - options' '
e4615238
FC
260 test_gitcomp "--re" "--dry-run --reuse-message= --reedit-message=
261 --reset-author" <<-EOF
74a8c849
SG
262 --reuse-message=Z
263 --reedit-message=Z
264 --reset-author Z
265 EOF
74a8c849
SG
266'
267
268test_expect_success '__gitcomp - trailing space - config keys' '
e4615238
FC
269 test_gitcomp "br" "branch. branch.autosetupmerge
270 branch.autosetuprebase browser." <<-\EOF
74a8c849
SG
271 branch.Z
272 branch.autosetupmerge Z
273 branch.autosetuprebase Z
274 browser.Z
275 EOF
74a8c849
SG
276'
277
278test_expect_success '__gitcomp - option parameter' '
e4615238
FC
279 test_gitcomp "--strategy=re" "octopus ours recursive resolve subtree" \
280 "" "re" <<-\EOF
74a8c849
SG
281 recursive Z
282 resolve Z
283 EOF
74a8c849
SG
284'
285
286test_expect_success '__gitcomp - prefix' '
e4615238
FC
287 test_gitcomp "branch.me" "remote merge mergeoptions rebase" \
288 "branch.maint." "me" <<-\EOF
74a8c849
SG
289 branch.maint.merge Z
290 branch.maint.mergeoptions Z
291 EOF
74a8c849
SG
292'
293
294test_expect_success '__gitcomp - suffix' '
e4615238
FC
295 test_gitcomp "branch.me" "master maint next pu" "branch." \
296 "ma" "." <<-\EOF
74a8c849
SG
297 branch.master.Z
298 branch.maint.Z
299 EOF
74a8c849
SG
300'
301
7d13e0a3 302test_expect_success '__gitcomp - doesnt fail because of invalid variable name' '
43369a22
FC
303 __gitcomp "$invalid_variable_name"
304'
305
306read -r -d "" refs <<-\EOF
307maint
308master
309next
310pu
311EOF
312
313test_expect_success '__gitcomp_nl - trailing space' '
314 test_gitcomp_nl "m" "$refs" <<-EOF
315 maint Z
316 master Z
317 EOF
318'
319
320test_expect_success '__gitcomp_nl - prefix' '
321 test_gitcomp_nl "--fixup=m" "$refs" "--fixup=" "m" <<-EOF
322 --fixup=maint Z
323 --fixup=master Z
324 EOF
325'
326
327test_expect_success '__gitcomp_nl - suffix' '
328 test_gitcomp_nl "branch.ma" "$refs" "branch." "ma" "." <<-\EOF
329 branch.maint.Z
330 branch.master.Z
331 EOF
332'
333
334test_expect_success '__gitcomp_nl - no suffix' '
335 test_gitcomp_nl "ma" "$refs" "" "ma" "" <<-\EOF
336 maintZ
337 masterZ
338 EOF
339'
340
7d13e0a3 341test_expect_success '__gitcomp_nl - doesnt fail because of invalid variable name' '
43369a22
FC
342 __gitcomp_nl "$invalid_variable_name"
343'
344
5c293a6b 345test_expect_success 'basic' '
cdbff7d6 346 run_completion "git " &&
5c293a6b
FC
347 # built-in
348 grep -q "^add \$" out &&
349 # script
350 grep -q "^filter-branch \$" out &&
351 # plumbing
352 ! grep -q "^ls-files \$" out &&
353
354 run_completion "git f" &&
355 ! grep -q -v "^f" out
356'
357
358test_expect_success 'double dash "git" itself' '
2fbaf813 359 test_completion "git --" <<-\EOF
5c293a6b
FC
360 --paginate Z
361 --no-pager Z
362 --git-dir=
363 --bare Z
364 --version Z
365 --exec-path Z
3ffcd086 366 --exec-path=
5c293a6b 367 --html-path Z
66fb37d0 368 --man-path Z
69ef3c02 369 --info-path Z
5c293a6b
FC
370 --work-tree=
371 --namespace=
69ef3c02 372 --no-replace-objects Z
5c293a6b
FC
373 --help Z
374 EOF
5c293a6b
FC
375'
376
377test_expect_success 'double dash "git checkout"' '
2fbaf813 378 test_completion "git checkout --" <<-\EOF
5c293a6b
FC
379 --quiet Z
380 --ours Z
381 --theirs Z
382 --track Z
383 --no-track Z
384 --merge Z
385 --conflict=
386 --orphan Z
387 --patch Z
388 EOF
5c293a6b
FC
389'
390
69ef3c02
FC
391test_expect_success 'general options' '
392 test_completion "git --ver" "--version " &&
393 test_completion "git --hel" "--help " &&
2fbaf813 394 test_completion "git --exe" <<-\EOF &&
3ffcd086
JN
395 --exec-path Z
396 --exec-path=
397 EOF
69ef3c02
FC
398 test_completion "git --htm" "--html-path " &&
399 test_completion "git --pag" "--paginate " &&
400 test_completion "git --no-p" "--no-pager " &&
401 test_completion "git --git" "--git-dir=" &&
402 test_completion "git --wor" "--work-tree=" &&
403 test_completion "git --nam" "--namespace=" &&
404 test_completion "git --bar" "--bare " &&
405 test_completion "git --inf" "--info-path " &&
406 test_completion "git --no-r" "--no-replace-objects "
407'
911d5da6
SG
408
409test_expect_success 'general options plus command' '
410 test_completion "git --version check" "checkout " &&
411 test_completion "git --paginate check" "checkout " &&
412 test_completion "git --git-dir=foo check" "checkout " &&
413 test_completion "git --bare check" "checkout " &&
911d5da6
SG
414 test_completion "git --exec-path=foo check" "checkout " &&
415 test_completion "git --html-path check" "checkout " &&
416 test_completion "git --no-pager check" "checkout " &&
417 test_completion "git --work-tree=foo check" "checkout " &&
418 test_completion "git --namespace=foo check" "checkout " &&
419 test_completion "git --paginate check" "checkout " &&
420 test_completion "git --info-path check" "checkout " &&
421 test_completion "git --no-replace-objects check" "checkout "
422'
423
50478223
JH
424test_expect_success 'git --help completion' '
425 test_completion "git --help ad" "add " &&
426 test_completion "git --help core" "core-tutorial "
427'
428
49ba92b4
JK
429test_expect_success 'setup for ref completion' '
430 echo content >file1 &&
431 echo more >file2 &&
432 git add . &&
433 git commit -m one &&
434 git branch mybranch &&
435 git tag mytag
436'
437
438test_expect_success 'checkout completes ref names' '
2fbaf813 439 test_completion "git checkout m" <<-\EOF
43ea0812
FC
440 master Z
441 mybranch Z
442 mytag Z
49ba92b4
JK
443 EOF
444'
445
446test_expect_success 'show completes all refs' '
2fbaf813 447 test_completion "git show m" <<-\EOF
43ea0812
FC
448 master Z
449 mybranch Z
450 mytag Z
49ba92b4
JK
451 EOF
452'
453
454test_expect_success '<ref>: completes paths' '
2fbaf813 455 test_completion "git show mytag:f" <<-\EOF
43ea0812
FC
456 file1 Z
457 file2 Z
49ba92b4
JK
458 EOF
459'
460
bafed0df
JK
461test_expect_success 'complete tree filename with spaces' '
462 echo content >"name with spaces" &&
463 git add . &&
464 git commit -m spaces &&
2fbaf813 465 test_completion "git show HEAD:nam" <<-\EOF
43ea0812 466 name with spaces Z
bafed0df
JK
467 EOF
468'
469
7d13e0a3 470test_expect_success 'complete tree filename with metacharacters' '
bafed0df
JK
471 echo content >"name with \${meta}" &&
472 git add . &&
473 git commit -m meta &&
2fbaf813 474 test_completion "git show HEAD:nam" <<-\EOF
43ea0812
FC
475 name with ${meta} Z
476 name with spaces Z
bafed0df
JK
477 EOF
478'
479
2f65494d
FC
480test_expect_success 'send-email' '
481 test_completion "git send-email --cov" "--cover-letter " &&
482 test_completion "git send-email ma" "master "
483'
484
ddf07bdd
FC
485test_expect_success 'complete files' '
486 git init tmp && cd tmp &&
487 test_when_finished "cd .. && rm -rf tmp" &&
488
489 echo "expected" > .gitignore &&
490 echo "out" >> .gitignore &&
491
492 git add .gitignore &&
493 test_completion "git commit " ".gitignore" &&
494
495 git commit -m ignore &&
496
497 touch new &&
498 test_completion "git add " "new" &&
499
500 git add new &&
501 git commit -a -m new &&
502 test_completion "git add " "" &&
503
504 git mv new modified &&
505 echo modify > modified &&
506 test_completion "git add " "modified" &&
507
508 touch untracked &&
509
510 : TODO .gitignore should not be here &&
511 test_completion "git rm " <<-\EOF &&
512 .gitignore
513 modified
514 EOF
515
516 test_completion "git clean " "untracked" &&
517
518 : TODO .gitignore should not be here &&
519 test_completion "git mv " <<-\EOF &&
520 .gitignore
521 modified
522 EOF
523
524 mkdir dir &&
525 touch dir/file-in-dir &&
526 git add dir/file-in-dir &&
527 git commit -m dir &&
528
529 mkdir untracked-dir &&
530
531 : TODO .gitignore should not be here &&
532 test_completion "git mv modified " <<-\EOF &&
533 .gitignore
534 dir
535 modified
536 untracked
537 untracked-dir
538 EOF
539
540 test_completion "git commit " "modified" &&
541
542 : TODO .gitignore should not be here &&
543 test_completion "git ls-files " <<-\EOF
544 .gitignore
545 dir
546 modified
547 EOF
548
549 touch momified &&
550 test_completion "git add mom" "momified"
551'
552
f03efba4
FC
553test_expect_failure 'complete with tilde expansion' '
554 git init tmp && cd tmp &&
555 test_when_finished "cd .. && rm -rf tmp" &&
556
557 touch ~/tmp/file &&
558
559 test_completion "git add ~/tmp/" "~/tmp/file"
560'
561
5c293a6b 562test_done