t/lib-httpd: require SANITY prereq
[git/mingw.git] / t / t9903-bash-prompt.sh
blob6efd0d9c78fda8b28e79fad7153b610a0b40173f
1 #!/bin/sh
3 # Copyright (c) 2012 SZEDER Gábor
6 test_description='test git-specific bash prompt functions'
8 . ./lib-bash.sh
10 . "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
12 actual="$TRASH_DIRECTORY/actual"
13 c_red='\\[\\e[31m\\]'
14 c_green='\\[\\e[32m\\]'
15 c_lblue='\\[\\e[1;34m\\]'
16 c_clear='\\[\\e[0m\\]'
18 test_expect_success 'setup for prompt tests' '
19 git init otherrepo &&
20 echo 1 >file &&
21 git add file &&
22 test_tick &&
23 git commit -m initial &&
24 git tag -a -m msg1 t1 &&
25 git checkout -b b1 &&
26 echo 2 >file &&
27 git commit -m "second b1" file &&
28 echo 3 >file &&
29 git commit -m "third b1" file &&
30 git tag -a -m msg2 t2 &&
31 git checkout -b b2 master &&
32 echo 0 >file &&
33 git commit -m "second b2" file &&
34 echo 00 >file &&
35 git commit -m "another b2" file &&
36 echo 000 >file &&
37 git commit -m "yet another b2" file &&
38 git checkout master
41 test_expect_success 'prompt - branch name' '
42 printf " (master)" >expected &&
43 __git_ps1 >"$actual" &&
44 test_cmp expected "$actual"
47 test_expect_success SYMLINKS 'prompt - branch name - symlink symref' '
48 printf " (master)" >expected &&
49 test_when_finished "git checkout master" &&
50 test_config core.preferSymlinkRefs true &&
51 git checkout master &&
52 __git_ps1 >"$actual" &&
53 test_cmp expected "$actual"
56 test_expect_success 'prompt - unborn branch' '
57 printf " (unborn)" >expected &&
58 git checkout --orphan unborn &&
59 test_when_finished "git checkout master" &&
60 __git_ps1 >"$actual" &&
61 test_cmp expected "$actual"
64 repo_with_newline='repo
65 with
66 newline'
68 if mkdir "$repo_with_newline" 2>/dev/null
69 then
70 test_set_prereq FUNNYNAMES
71 else
72 say 'Your filesystem does not allow newlines in filenames.'
75 test_expect_success FUNNYNAMES 'prompt - with newline in path' '
76 printf " (master)" >expected &&
77 git init "$repo_with_newline" &&
78 test_when_finished "rm -rf \"$repo_with_newline\"" &&
79 mkdir "$repo_with_newline"/subdir &&
81 cd "$repo_with_newline/subdir" &&
82 __git_ps1 >"$actual"
83 ) &&
84 test_cmp expected "$actual"
87 test_expect_success 'prompt - detached head' '
88 printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
89 test_config core.abbrev 13 &&
90 git checkout b1^ &&
91 test_when_finished "git checkout master" &&
92 __git_ps1 >"$actual" &&
93 test_cmp expected "$actual"
96 test_expect_success 'prompt - describe detached head - contains' '
97 printf " ((t2~1))" >expected &&
98 git checkout b1^ &&
99 test_when_finished "git checkout master" &&
101 GIT_PS1_DESCRIBE_STYLE=contains &&
102 __git_ps1 >"$actual"
103 ) &&
104 test_cmp expected "$actual"
107 test_expect_success 'prompt - describe detached head - branch' '
108 printf " ((b1~1))" >expected &&
109 git checkout b1^ &&
110 test_when_finished "git checkout master" &&
112 GIT_PS1_DESCRIBE_STYLE=branch &&
113 __git_ps1 >"$actual"
114 ) &&
115 test_cmp expected "$actual"
118 test_expect_success 'prompt - describe detached head - describe' '
119 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
120 git checkout b1^ &&
121 test_when_finished "git checkout master" &&
123 GIT_PS1_DESCRIBE_STYLE=describe &&
124 __git_ps1 >"$actual"
125 ) &&
126 test_cmp expected "$actual"
129 test_expect_success 'prompt - describe detached head - default' '
130 printf " ((t2))" >expected &&
131 git checkout --detach b1 &&
132 test_when_finished "git checkout master" &&
133 __git_ps1 >"$actual" &&
134 test_cmp expected "$actual"
137 test_expect_success 'prompt - inside .git directory' '
138 printf " (GIT_DIR!)" >expected &&
140 cd .git &&
141 __git_ps1 >"$actual"
142 ) &&
143 test_cmp expected "$actual"
146 test_expect_success 'prompt - deep inside .git directory' '
147 printf " (GIT_DIR!)" >expected &&
149 cd .git/refs/heads &&
150 __git_ps1 >"$actual"
151 ) &&
152 test_cmp expected "$actual"
155 test_expect_success 'prompt - inside bare repository' '
156 printf " (BARE:master)" >expected &&
157 git init --bare bare.git &&
158 test_when_finished "rm -rf bare.git" &&
160 cd bare.git &&
161 __git_ps1 >"$actual"
162 ) &&
163 test_cmp expected "$actual"
166 test_expect_success 'prompt - interactive rebase' '
167 printf " (b1|REBASE-i 2/3)" >expected
168 write_script fake_editor.sh <<-\EOF &&
169 echo "exec echo" >"$1"
170 echo "edit $(git log -1 --format="%h")" >>"$1"
171 echo "exec echo" >>"$1"
173 test_when_finished "rm -f fake_editor.sh" &&
174 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
175 git checkout b1 &&
176 test_when_finished "git checkout master" &&
177 git rebase -i HEAD^ &&
178 test_when_finished "git rebase --abort"
179 __git_ps1 >"$actual" &&
180 test_cmp expected "$actual"
183 test_expect_success 'prompt - rebase merge' '
184 printf " (b2|REBASE-m 1/3)" >expected &&
185 git checkout b2 &&
186 test_when_finished "git checkout master" &&
187 test_must_fail git rebase --merge b1 b2 &&
188 test_when_finished "git rebase --abort" &&
189 __git_ps1 >"$actual" &&
190 test_cmp expected "$actual"
193 test_expect_success 'prompt - rebase' '
194 printf " (b2|REBASE 1/3)" >expected &&
195 git checkout b2 &&
196 test_when_finished "git checkout master" &&
197 test_must_fail git rebase b1 b2 &&
198 test_when_finished "git rebase --abort" &&
199 __git_ps1 >"$actual" &&
200 test_cmp expected "$actual"
203 test_expect_success 'prompt - merge' '
204 printf " (b1|MERGING)" >expected &&
205 git checkout b1 &&
206 test_when_finished "git checkout master" &&
207 test_must_fail git merge b2 &&
208 test_when_finished "git reset --hard" &&
209 __git_ps1 >"$actual" &&
210 test_cmp expected "$actual"
213 test_expect_success 'prompt - cherry-pick' '
214 printf " (master|CHERRY-PICKING)" >expected &&
215 test_must_fail git cherry-pick b1 &&
216 test_when_finished "git reset --hard" &&
217 __git_ps1 >"$actual" &&
218 test_cmp expected "$actual"
221 test_expect_success 'prompt - bisect' '
222 printf " (master|BISECTING)" >expected &&
223 git bisect start &&
224 test_when_finished "git bisect reset" &&
225 __git_ps1 >"$actual" &&
226 test_cmp expected "$actual"
229 test_expect_success 'prompt - dirty status indicator - clean' '
230 printf " (master)" >expected &&
232 GIT_PS1_SHOWDIRTYSTATE=y &&
233 __git_ps1 >"$actual"
234 ) &&
235 test_cmp expected "$actual"
238 test_expect_success 'prompt - dirty status indicator - dirty worktree' '
239 printf " (master *)" >expected &&
240 echo "dirty" >file &&
241 test_when_finished "git reset --hard" &&
243 GIT_PS1_SHOWDIRTYSTATE=y &&
244 __git_ps1 >"$actual"
245 ) &&
246 test_cmp expected "$actual"
249 test_expect_success 'prompt - dirty status indicator - dirty index' '
250 printf " (master +)" >expected &&
251 echo "dirty" >file &&
252 test_when_finished "git reset --hard" &&
253 git add -u &&
255 GIT_PS1_SHOWDIRTYSTATE=y &&
256 __git_ps1 >"$actual"
257 ) &&
258 test_cmp expected "$actual"
261 test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
262 printf " (master *+)" >expected &&
263 echo "dirty index" >file &&
264 test_when_finished "git reset --hard" &&
265 git add -u &&
266 echo "dirty worktree" >file &&
268 GIT_PS1_SHOWDIRTYSTATE=y &&
269 __git_ps1 >"$actual"
270 ) &&
271 test_cmp expected "$actual"
274 test_expect_success 'prompt - dirty status indicator - before root commit' '
275 printf " (master #)" >expected &&
277 GIT_PS1_SHOWDIRTYSTATE=y &&
278 cd otherrepo &&
279 __git_ps1 >"$actual"
280 ) &&
281 test_cmp expected "$actual"
284 test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
285 printf " (master)" >expected &&
286 echo "dirty" >file &&
287 test_when_finished "git reset --hard" &&
288 test_config bash.showDirtyState false &&
290 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
291 __git_ps1 >"$actual"
292 ) &&
293 test_cmp expected "$actual"
296 test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
297 printf " (master)" >expected &&
298 echo "dirty" >file &&
299 test_when_finished "git reset --hard" &&
300 test_config bash.showDirtyState true &&
302 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
303 __git_ps1 >"$actual"
304 ) &&
305 test_cmp expected "$actual"
308 test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
309 printf " (master)" >expected &&
310 echo "dirty" >file &&
311 test_when_finished "git reset --hard" &&
312 test_config bash.showDirtyState false &&
314 GIT_PS1_SHOWDIRTYSTATE=y &&
315 __git_ps1 >"$actual"
316 ) &&
317 test_cmp expected "$actual"
320 test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
321 printf " (master *)" >expected &&
322 echo "dirty" >file &&
323 test_when_finished "git reset --hard" &&
324 test_config bash.showDirtyState true &&
326 GIT_PS1_SHOWDIRTYSTATE=y &&
327 __git_ps1 >"$actual"
328 ) &&
329 test_cmp expected "$actual"
332 test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
333 printf " (GIT_DIR!)" >expected &&
334 echo "dirty" >file &&
335 test_when_finished "git reset --hard" &&
337 GIT_PS1_SHOWDIRTYSTATE=y &&
338 cd .git &&
339 __git_ps1 >"$actual"
340 ) &&
341 test_cmp expected "$actual"
344 test_expect_success 'prompt - stash status indicator - no stash' '
345 printf " (master)" >expected &&
347 GIT_PS1_SHOWSTASHSTATE=y &&
348 __git_ps1 >"$actual"
349 ) &&
350 test_cmp expected "$actual"
353 test_expect_success 'prompt - stash status indicator - stash' '
354 printf " (master $)" >expected &&
355 echo 2 >file &&
356 git stash &&
357 test_when_finished "git stash drop" &&
358 git pack-refs --all &&
360 GIT_PS1_SHOWSTASHSTATE=y &&
361 __git_ps1 >"$actual"
362 ) &&
363 test_cmp expected "$actual"
366 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
367 printf " (GIT_DIR!)" >expected &&
368 echo 2 >file &&
369 git stash &&
370 test_when_finished "git stash drop" &&
372 GIT_PS1_SHOWSTASHSTATE=y &&
373 cd .git &&
374 __git_ps1 >"$actual"
375 ) &&
376 test_cmp expected "$actual"
379 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
380 printf " (master)" >expected &&
382 GIT_PS1_SHOWUNTRACKEDFILES=y &&
383 cd otherrepo &&
384 __git_ps1 >"$actual"
385 ) &&
386 test_cmp expected "$actual"
389 test_expect_success 'prompt - untracked files status indicator - untracked files' '
390 printf " (master %%)" >expected &&
392 GIT_PS1_SHOWUNTRACKEDFILES=y &&
393 __git_ps1 >"$actual"
394 ) &&
395 test_cmp expected "$actual"
398 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
399 printf " (master)" >expected &&
400 test_config bash.showUntrackedFiles false &&
402 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
403 __git_ps1 >"$actual"
404 ) &&
405 test_cmp expected "$actual"
408 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
409 printf " (master)" >expected &&
410 test_config bash.showUntrackedFiles true &&
412 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
413 __git_ps1 >"$actual"
414 ) &&
415 test_cmp expected "$actual"
418 test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
419 printf " (master)" >expected &&
420 test_config bash.showUntrackedFiles false &&
422 GIT_PS1_SHOWUNTRACKEDFILES=y &&
423 __git_ps1 >"$actual"
424 ) &&
425 test_cmp expected "$actual"
428 test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
429 printf " (master %%)" >expected &&
430 test_config bash.showUntrackedFiles true &&
432 GIT_PS1_SHOWUNTRACKEDFILES=y &&
433 __git_ps1 >"$actual"
434 ) &&
435 test_cmp expected "$actual"
438 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
439 printf " (GIT_DIR!)" >expected &&
441 GIT_PS1_SHOWUNTRACKEDFILES=y &&
442 cd .git &&
443 __git_ps1 >"$actual"
444 ) &&
445 test_cmp expected "$actual"
448 test_expect_success 'prompt - format string starting with dash' '
449 printf -- "-master" >expected &&
450 __git_ps1 "-%s" >"$actual" &&
451 test_cmp expected "$actual"
454 test_expect_success 'prompt - pc mode' '
455 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER\\nmaster" >expected &&
456 printf "" >expected_output &&
458 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
459 test_cmp expected_output "$actual" &&
460 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
461 ) &&
462 test_cmp expected "$actual"
465 test_expect_success 'prompt - bash color pc mode - branch name' '
466 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nmaster" >expected &&
468 GIT_PS1_SHOWCOLORHINTS=y &&
469 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
470 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
471 ) &&
472 test_cmp expected "$actual"
475 test_expect_success 'prompt - bash color pc mode - detached head' '
476 printf "BEFORE: (${c_red}\${__git_ps1_branch_name}${c_clear}):AFTER\\n(%s...)" $(git log -1 --format="%h" b1^) >expected &&
477 git checkout b1^ &&
478 test_when_finished "git checkout master" &&
480 GIT_PS1_SHOWCOLORHINTS=y &&
481 __git_ps1 "BEFORE:" ":AFTER" &&
482 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
483 ) &&
484 test_cmp expected "$actual"
487 test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
488 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}):AFTER\\nmaster" >expected &&
489 echo "dirty" >file &&
490 test_when_finished "git reset --hard" &&
492 GIT_PS1_SHOWDIRTYSTATE=y &&
493 GIT_PS1_SHOWCOLORHINTS=y &&
494 __git_ps1 "BEFORE:" ":AFTER" &&
495 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
496 ) &&
497 test_cmp expected "$actual"
500 test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index' '
501 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}+${c_clear}):AFTER\\nmaster" >expected &&
502 echo "dirty" >file &&
503 test_when_finished "git reset --hard" &&
504 git add -u &&
506 GIT_PS1_SHOWDIRTYSTATE=y &&
507 GIT_PS1_SHOWCOLORHINTS=y &&
508 __git_ps1 "BEFORE:" ":AFTER" &&
509 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
510 ) &&
511 test_cmp expected "$actual"
514 test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
515 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_green}+${c_clear}):AFTER\\nmaster" >expected &&
516 echo "dirty index" >file &&
517 test_when_finished "git reset --hard" &&
518 git add -u &&
519 echo "dirty worktree" >file &&
521 GIT_PS1_SHOWCOLORHINTS=y &&
522 GIT_PS1_SHOWDIRTYSTATE=y &&
523 __git_ps1 "BEFORE:" ":AFTER" &&
524 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
525 ) &&
526 test_cmp expected "$actual"
529 test_expect_success 'prompt - bash color pc mode - dirty status indicator - before root commit' '
530 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}#${c_clear}):AFTER\\nmaster" >expected &&
532 GIT_PS1_SHOWDIRTYSTATE=y &&
533 GIT_PS1_SHOWCOLORHINTS=y &&
534 cd otherrepo &&
535 __git_ps1 "BEFORE:" ":AFTER" &&
536 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
537 ) &&
538 test_cmp expected "$actual"
541 test_expect_success 'prompt - bash color pc mode - inside .git directory' '
542 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nGIT_DIR!" >expected &&
543 echo "dirty" >file &&
544 test_when_finished "git reset --hard" &&
546 GIT_PS1_SHOWDIRTYSTATE=y &&
547 GIT_PS1_SHOWCOLORHINTS=y &&
548 cd .git &&
549 __git_ps1 "BEFORE:" ":AFTER" &&
550 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
551 ) &&
552 test_cmp expected "$actual"
555 test_expect_success 'prompt - bash color pc mode - stash status indicator' '
556 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_lblue}\$${c_clear}):AFTER\\nmaster" >expected &&
557 echo 2 >file &&
558 git stash &&
559 test_when_finished "git stash drop" &&
561 GIT_PS1_SHOWSTASHSTATE=y &&
562 GIT_PS1_SHOWCOLORHINTS=y &&
563 __git_ps1 "BEFORE:" ":AFTER" &&
564 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
565 ) &&
566 test_cmp expected "$actual"
569 test_expect_success 'prompt - bash color pc mode - untracked files status indicator' '
570 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}%%${c_clear}):AFTER\\nmaster" >expected &&
572 GIT_PS1_SHOWUNTRACKEDFILES=y &&
573 GIT_PS1_SHOWCOLORHINTS=y &&
574 __git_ps1 "BEFORE:" ":AFTER" &&
575 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
576 ) &&
577 test_cmp expected "$actual"
580 test_expect_success 'prompt - zsh color pc mode' '
581 printf "BEFORE: (%%F{green}\${__git_ps1_branch_name}%%f):AFTER\\nmaster" >expected &&
583 ZSH_VERSION=5.0.0 &&
584 GIT_PS1_SHOWCOLORHINTS=y &&
585 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
586 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
587 ) &&
588 test_cmp expected "$actual"
591 test_done