]> git.ipfire.org Git - thirdparty/git.git/blame - t/t9903-bash-prompt.sh
bash prompt: fix redirection coding style in tests
[thirdparty/git.git] / t / t9903-bash-prompt.sh
CommitLineData
963c0407
SG
1#!/bin/sh
2#
3# Copyright (c) 2012 SZEDER Gábor
4#
5
6test_description='test git-specific bash prompt functions'
7
8. ./lib-bash.sh
9
af31a456 10. "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
963c0407
SG
11
12actual="$TRASH_DIRECTORY/actual"
13
14test_expect_success 'setup for prompt tests' '
15 mkdir -p subdir/subsubdir &&
16 git init otherrepo &&
4fe00b4f 17 echo 1 >file &&
963c0407
SG
18 git add file &&
19 test_tick &&
20 git commit -m initial &&
21 git tag -a -m msg1 t1 &&
22 git checkout -b b1 &&
4fe00b4f 23 echo 2 >file &&
963c0407 24 git commit -m "second b1" file &&
4fe00b4f 25 echo 3 >file &&
963c0407
SG
26 git commit -m "third b1" file &&
27 git tag -a -m msg2 t2 &&
28 git checkout -b b2 master &&
4fe00b4f 29 echo 0 >file &&
963c0407 30 git commit -m "second b2" file &&
4fe00b4f 31 echo 00 >file &&
b71dc3e1 32 git commit -m "another b2" file &&
4fe00b4f 33 echo 000 >file &&
b71dc3e1 34 git commit -m "yet another b2" file &&
963c0407
SG
35 git checkout master
36'
37
38test_expect_success 'gitdir - from command line (through $__git_dir)' '
4fe00b4f 39 echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
963c0407
SG
40 (
41 __git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
4fe00b4f 42 __gitdir >"$actual"
963c0407
SG
43 ) &&
44 test_cmp expected "$actual"
45'
46
47test_expect_success 'gitdir - repo as argument' '
4fe00b4f
SG
48 echo "otherrepo/.git" >expected &&
49 __gitdir "otherrepo" >"$actual" &&
963c0407
SG
50 test_cmp expected "$actual"
51'
52
53test_expect_success 'gitdir - remote as argument' '
4fe00b4f
SG
54 echo "remote" >expected &&
55 __gitdir "remote" >"$actual" &&
963c0407
SG
56 test_cmp expected "$actual"
57'
58
59test_expect_success 'gitdir - .git directory in cwd' '
4fe00b4f
SG
60 echo ".git" >expected &&
61 __gitdir >"$actual" &&
963c0407
SG
62 test_cmp expected "$actual"
63'
64
65test_expect_success 'gitdir - .git directory in parent' '
4fe00b4f 66 echo "$(pwd -P)/.git" >expected &&
963c0407
SG
67 (
68 cd subdir/subsubdir &&
4fe00b4f 69 __gitdir >"$actual"
963c0407
SG
70 ) &&
71 test_cmp expected "$actual"
72'
73
74test_expect_success 'gitdir - cwd is a .git directory' '
4fe00b4f 75 echo "." >expected &&
963c0407
SG
76 (
77 cd .git &&
4fe00b4f 78 __gitdir >"$actual"
963c0407
SG
79 ) &&
80 test_cmp expected "$actual"
81'
82
83test_expect_success 'gitdir - parent is a .git directory' '
4fe00b4f 84 echo "$(pwd -P)/.git" >expected &&
963c0407
SG
85 (
86 cd .git/refs/heads &&
4fe00b4f 87 __gitdir >"$actual"
963c0407
SG
88 ) &&
89 test_cmp expected "$actual"
90'
91
b7be4366 92test_expect_success 'gitdir - $GIT_DIR set while .git directory in cwd' '
4fe00b4f 93 echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
963c0407
SG
94 (
95 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
96 export GIT_DIR &&
4fe00b4f 97 __gitdir >"$actual"
963c0407
SG
98 ) &&
99 test_cmp expected "$actual"
100'
101
102test_expect_success 'gitdir - $GIT_DIR set while .git directory in parent' '
4fe00b4f 103 echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
963c0407
SG
104 (
105 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
106 export GIT_DIR &&
107 cd subdir &&
4fe00b4f 108 __gitdir >"$actual"
963c0407
SG
109 ) &&
110 test_cmp expected "$actual"
111'
112
b7be4366
SG
113test_expect_success 'gitdir - non-existing $GIT_DIR' '
114 (
115 GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
116 export GIT_DIR &&
117 test_must_fail __gitdir
118 )
119'
120
963c0407 121test_expect_success 'gitdir - gitfile in cwd' '
4fe00b4f
SG
122 echo "$(pwd -P)/otherrepo/.git" >expected &&
123 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" >subdir/.git &&
963c0407
SG
124 test_when_finished "rm -f subdir/.git" &&
125 (
126 cd subdir &&
4fe00b4f 127 __gitdir >"$actual"
963c0407
SG
128 ) &&
129 test_cmp expected "$actual"
130'
131
132test_expect_success 'gitdir - gitfile in parent' '
4fe00b4f
SG
133 echo "$(pwd -P)/otherrepo/.git" >expected &&
134 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" >subdir/.git &&
963c0407
SG
135 test_when_finished "rm -f subdir/.git" &&
136 (
137 cd subdir/subsubdir &&
4fe00b4f 138 __gitdir >"$actual"
963c0407
SG
139 ) &&
140 test_cmp expected "$actual"
141'
142
143test_expect_success SYMLINKS 'gitdir - resulting path avoids symlinks' '
4fe00b4f 144 echo "$(pwd -P)/otherrepo/.git" >expected &&
963c0407
SG
145 mkdir otherrepo/dir &&
146 test_when_finished "rm -rf otherrepo/dir" &&
147 ln -s otherrepo/dir link &&
148 test_when_finished "rm -f link" &&
149 (
150 cd link &&
4fe00b4f 151 __gitdir >"$actual"
963c0407
SG
152 ) &&
153 test_cmp expected "$actual"
154'
155
156test_expect_success 'gitdir - not a git repository' '
157 (
158 cd subdir/subsubdir &&
159 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
160 export GIT_CEILING_DIRECTORIES &&
161 test_must_fail __gitdir
162 )
163'
164
165test_expect_success 'prompt - branch name' '
4fe00b4f
SG
166 printf " (master)" >expected &&
167 __git_ps1 >"$actual" &&
963c0407
SG
168 test_cmp expected "$actual"
169'
170
171test_expect_success 'prompt - detached head' '
4fe00b4f 172 printf " ((%s...))" $(git log -1 --format="%h" b1^) >expected &&
963c0407
SG
173 git checkout b1^ &&
174 test_when_finished "git checkout master" &&
4fe00b4f 175 __git_ps1 >"$actual" &&
963c0407
SG
176 test_cmp expected "$actual"
177'
178
179test_expect_success 'prompt - describe detached head - contains' '
4fe00b4f 180 printf " ((t2~1))" >expected &&
963c0407
SG
181 git checkout b1^ &&
182 test_when_finished "git checkout master" &&
183 (
184 GIT_PS1_DESCRIBE_STYLE=contains &&
4fe00b4f 185 __git_ps1 >"$actual"
963c0407
SG
186 ) &&
187 test_cmp expected "$actual"
188'
189
190test_expect_success 'prompt - describe detached head - branch' '
4fe00b4f 191 printf " ((b1~1))" >expected &&
963c0407
SG
192 git checkout b1^ &&
193 test_when_finished "git checkout master" &&
194 (
195 GIT_PS1_DESCRIBE_STYLE=branch &&
4fe00b4f 196 __git_ps1 >"$actual"
963c0407
SG
197 ) &&
198 test_cmp expected "$actual"
199'
200
201test_expect_success 'prompt - describe detached head - describe' '
4fe00b4f 202 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
963c0407
SG
203 git checkout b1^ &&
204 test_when_finished "git checkout master" &&
205 (
206 GIT_PS1_DESCRIBE_STYLE=describe &&
4fe00b4f 207 __git_ps1 >"$actual"
963c0407
SG
208 ) &&
209 test_cmp expected "$actual"
210'
211
212test_expect_success 'prompt - describe detached head - default' '
4fe00b4f 213 printf " ((t2))" >expected &&
963c0407
SG
214 git checkout --detach b1 &&
215 test_when_finished "git checkout master" &&
4fe00b4f 216 __git_ps1 >"$actual" &&
963c0407
SG
217 test_cmp expected "$actual"
218'
219
220test_expect_success 'prompt - inside .git directory' '
4fe00b4f 221 printf " (GIT_DIR!)" >expected &&
963c0407
SG
222 (
223 cd .git &&
4fe00b4f 224 __git_ps1 >"$actual"
963c0407
SG
225 ) &&
226 test_cmp expected "$actual"
227'
228
229test_expect_success 'prompt - deep inside .git directory' '
4fe00b4f 230 printf " (GIT_DIR!)" >expected &&
963c0407
SG
231 (
232 cd .git/refs/heads &&
4fe00b4f 233 __git_ps1 >"$actual"
963c0407
SG
234 ) &&
235 test_cmp expected "$actual"
236'
237
238test_expect_success 'prompt - inside bare repository' '
4fe00b4f 239 printf " (BARE:master)" >expected &&
963c0407
SG
240 git init --bare bare.git &&
241 test_when_finished "rm -rf bare.git" &&
242 (
243 cd bare.git &&
4fe00b4f 244 __git_ps1 >"$actual"
963c0407
SG
245 ) &&
246 test_cmp expected "$actual"
247'
248
249test_expect_success 'prompt - interactive rebase' '
4fe00b4f 250 printf " (b1|REBASE-i 2/3)" >expected
963c0407
SG
251 echo "#!$SHELL_PATH" >fake_editor.sh &&
252 cat >>fake_editor.sh <<\EOF &&
4fe00b4f
SG
253echo "exec echo" >"$1"
254echo "edit $(git log -1 --format="%h")" >>"$1"
255echo "exec echo" >>"$1"
963c0407
SG
256EOF
257 test_when_finished "rm -f fake_editor.sh" &&
258 chmod a+x fake_editor.sh &&
259 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
260 git checkout b1 &&
261 test_when_finished "git checkout master" &&
262 git rebase -i HEAD^ &&
263 test_when_finished "git rebase --abort"
4fe00b4f 264 __git_ps1 >"$actual" &&
963c0407
SG
265 test_cmp expected "$actual"
266'
267
268test_expect_success 'prompt - rebase merge' '
4fe00b4f 269 printf " (b2|REBASE-m 1/3)" >expected &&
963c0407
SG
270 git checkout b2 &&
271 test_when_finished "git checkout master" &&
272 test_must_fail git rebase --merge b1 b2 &&
273 test_when_finished "git rebase --abort" &&
4fe00b4f 274 __git_ps1 >"$actual" &&
963c0407
SG
275 test_cmp expected "$actual"
276'
277
278test_expect_success 'prompt - rebase' '
4fe00b4f 279 printf " (b2|REBASE 1/3)" >expected &&
963c0407
SG
280 git checkout b2 &&
281 test_when_finished "git checkout master" &&
282 test_must_fail git rebase b1 b2 &&
283 test_when_finished "git rebase --abort" &&
4fe00b4f 284 __git_ps1 >"$actual" &&
963c0407
SG
285 test_cmp expected "$actual"
286'
287
288test_expect_success 'prompt - merge' '
4fe00b4f 289 printf " (b1|MERGING)" >expected &&
963c0407
SG
290 git checkout b1 &&
291 test_when_finished "git checkout master" &&
292 test_must_fail git merge b2 &&
293 test_when_finished "git reset --hard" &&
4fe00b4f 294 __git_ps1 >"$actual" &&
963c0407
SG
295 test_cmp expected "$actual"
296'
297
298test_expect_success 'prompt - cherry-pick' '
4fe00b4f 299 printf " (master|CHERRY-PICKING)" >expected &&
963c0407
SG
300 test_must_fail git cherry-pick b1 &&
301 test_when_finished "git reset --hard" &&
4fe00b4f 302 __git_ps1 >"$actual" &&
963c0407
SG
303 test_cmp expected "$actual"
304'
305
306test_expect_success 'prompt - bisect' '
4fe00b4f 307 printf " (master|BISECTING)" >expected &&
963c0407
SG
308 git bisect start &&
309 test_when_finished "git bisect reset" &&
4fe00b4f 310 __git_ps1 >"$actual" &&
963c0407
SG
311 test_cmp expected "$actual"
312'
313
314test_expect_success 'prompt - dirty status indicator - clean' '
4fe00b4f 315 printf " (master)" >expected &&
963c0407
SG
316 (
317 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 318 __git_ps1 >"$actual"
963c0407
SG
319 ) &&
320 test_cmp expected "$actual"
321'
322
323test_expect_success 'prompt - dirty status indicator - dirty worktree' '
4fe00b4f
SG
324 printf " (master *)" >expected &&
325 echo "dirty" >file &&
963c0407
SG
326 test_when_finished "git reset --hard" &&
327 (
328 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 329 __git_ps1 >"$actual"
963c0407
SG
330 ) &&
331 test_cmp expected "$actual"
332'
333
334test_expect_success 'prompt - dirty status indicator - dirty index' '
4fe00b4f
SG
335 printf " (master +)" >expected &&
336 echo "dirty" >file &&
963c0407
SG
337 test_when_finished "git reset --hard" &&
338 git add -u &&
339 (
340 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 341 __git_ps1 >"$actual"
963c0407
SG
342 ) &&
343 test_cmp expected "$actual"
344'
345
346test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
4fe00b4f
SG
347 printf " (master *+)" >expected &&
348 echo "dirty index" >file &&
963c0407
SG
349 test_when_finished "git reset --hard" &&
350 git add -u &&
4fe00b4f 351 echo "dirty worktree" >file &&
963c0407
SG
352 (
353 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 354 __git_ps1 >"$actual"
963c0407
SG
355 ) &&
356 test_cmp expected "$actual"
357'
358
359test_expect_success 'prompt - dirty status indicator - before root commit' '
4fe00b4f 360 printf " (master #)" >expected &&
963c0407
SG
361 (
362 GIT_PS1_SHOWDIRTYSTATE=y &&
363 cd otherrepo &&
4fe00b4f 364 __git_ps1 >"$actual"
963c0407
SG
365 ) &&
366 test_cmp expected "$actual"
367'
368
dc7e7bce 369test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
4fe00b4f
SG
370 printf " (master)" >expected &&
371 echo "dirty" >file &&
963c0407
SG
372 test_when_finished "git reset --hard" &&
373 test_config bash.showDirtyState false &&
dc7e7bce
MEW
374 (
375 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
4fe00b4f 376 __git_ps1 >"$actual"
dc7e7bce
MEW
377 ) &&
378 test_cmp expected "$actual"
379'
380
381test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
4fe00b4f
SG
382 printf " (master)" >expected &&
383 echo "dirty" >file &&
dc7e7bce
MEW
384 test_when_finished "git reset --hard" &&
385 test_config bash.showDirtyState true &&
386 (
387 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
4fe00b4f 388 __git_ps1 >"$actual"
dc7e7bce
MEW
389 ) &&
390 test_cmp expected "$actual"
391'
392
393test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
4fe00b4f
SG
394 printf " (master)" >expected &&
395 echo "dirty" >file &&
dc7e7bce
MEW
396 test_when_finished "git reset --hard" &&
397 test_config bash.showDirtyState false &&
398 (
399 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 400 __git_ps1 >"$actual"
dc7e7bce
MEW
401 ) &&
402 test_cmp expected "$actual"
403'
404
405test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
4fe00b4f
SG
406 printf " (master *)" >expected &&
407 echo "dirty" >file &&
dc7e7bce
MEW
408 test_when_finished "git reset --hard" &&
409 test_config bash.showDirtyState true &&
963c0407
SG
410 (
411 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 412 __git_ps1 >"$actual"
963c0407
SG
413 ) &&
414 test_cmp expected "$actual"
415'
416
417test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
4fe00b4f
SG
418 printf " (GIT_DIR!)" >expected &&
419 echo "dirty" >file &&
963c0407
SG
420 test_when_finished "git reset --hard" &&
421 (
422 GIT_PS1_SHOWDIRTYSTATE=y &&
423 cd .git &&
4fe00b4f 424 __git_ps1 >"$actual"
963c0407
SG
425 ) &&
426 test_cmp expected "$actual"
427'
428
429test_expect_success 'prompt - stash status indicator - no stash' '
4fe00b4f 430 printf " (master)" >expected &&
963c0407
SG
431 (
432 GIT_PS1_SHOWSTASHSTATE=y &&
4fe00b4f 433 __git_ps1 >"$actual"
963c0407
SG
434 ) &&
435 test_cmp expected "$actual"
436'
437
438test_expect_success 'prompt - stash status indicator - stash' '
4fe00b4f 439 printf " (master $)" >expected &&
963c0407
SG
440 echo 2 >file &&
441 git stash &&
442 test_when_finished "git stash drop" &&
443 (
444 GIT_PS1_SHOWSTASHSTATE=y &&
4fe00b4f 445 __git_ps1 >"$actual"
963c0407
SG
446 ) &&
447 test_cmp expected "$actual"
448'
449
450test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
4fe00b4f 451 printf " (GIT_DIR!)" >expected &&
963c0407
SG
452 echo 2 >file &&
453 git stash &&
454 test_when_finished "git stash drop" &&
455 (
456 GIT_PS1_SHOWSTASHSTATE=y &&
457 cd .git &&
4fe00b4f 458 __git_ps1 >"$actual"
963c0407
SG
459 ) &&
460 test_cmp expected "$actual"
461'
462
463test_expect_success 'prompt - untracked files status indicator - no untracked files' '
4fe00b4f 464 printf " (master)" >expected &&
963c0407
SG
465 (
466 GIT_PS1_SHOWUNTRACKEDFILES=y &&
467 cd otherrepo &&
4fe00b4f 468 __git_ps1 >"$actual"
963c0407
SG
469 ) &&
470 test_cmp expected "$actual"
471'
472
473test_expect_success 'prompt - untracked files status indicator - untracked files' '
4fe00b4f 474 printf " (master %%)" >expected &&
963c0407
SG
475 (
476 GIT_PS1_SHOWUNTRACKEDFILES=y &&
4fe00b4f 477 __git_ps1 >"$actual"
963c0407
SG
478 ) &&
479 test_cmp expected "$actual"
480'
481
58978e82 482test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
4fe00b4f 483 printf " (master)" >expected &&
58978e82
MEW
484 test_config bash.showUntrackedFiles false &&
485 (
486 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
4fe00b4f 487 __git_ps1 >"$actual"
58978e82
MEW
488 ) &&
489 test_cmp expected "$actual"
490'
491
492test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
4fe00b4f 493 printf " (master)" >expected &&
58978e82
MEW
494 test_config bash.showUntrackedFiles true &&
495 (
496 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
4fe00b4f 497 __git_ps1 >"$actual"
58978e82
MEW
498 ) &&
499 test_cmp expected "$actual"
500'
501
502test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
4fe00b4f 503 printf " (master)" >expected &&
58978e82
MEW
504 test_config bash.showUntrackedFiles false &&
505 (
506 GIT_PS1_SHOWUNTRACKEDFILES=y &&
4fe00b4f 507 __git_ps1 >"$actual"
58978e82
MEW
508 ) &&
509 test_cmp expected "$actual"
510'
511
512test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
4fe00b4f 513 printf " (master %%)" >expected &&
58978e82
MEW
514 test_config bash.showUntrackedFiles true &&
515 (
516 GIT_PS1_SHOWUNTRACKEDFILES=y &&
4fe00b4f 517 __git_ps1 >"$actual"
58978e82
MEW
518 ) &&
519 test_cmp expected "$actual"
520'
521
963c0407 522test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
4fe00b4f 523 printf " (GIT_DIR!)" >expected &&
963c0407
SG
524 (
525 GIT_PS1_SHOWUNTRACKEDFILES=y &&
526 cd .git &&
4fe00b4f 527 __git_ps1 >"$actual"
963c0407
SG
528 ) &&
529 test_cmp expected "$actual"
530'
531
532test_expect_success 'prompt - format string starting with dash' '
4fe00b4f
SG
533 printf -- "-master" >expected &&
534 __git_ps1 "-%s" >"$actual" &&
963c0407
SG
535 test_cmp expected "$actual"
536'
537
538test_done