3 # Copyright (c) 2012 SZEDER Gábor
6 test_description
='test git-specific bash prompt functions'
10 .
"$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
12 actual
="$TRASH_DIRECTORY/actual"
14 c_green
='\\[\\e[32m\\]'
15 c_lblue
='\\[\\e[1;34m\\]'
16 c_clear
='\\[\\e[0m\\]'
18 test_expect_success
'setup for prompt tests' '
23 git commit -m initial &&
24 git tag -a -m msg1 t1 &&
27 git commit -m "second b1" file &&
29 git commit -m "third b1" file &&
30 git tag -a -m msg2 t2 &&
31 git checkout -b b2 master &&
33 git commit -m "second b2" file &&
35 git commit -m "another b2" file &&
37 git commit -m "yet another b2" file &&
39 echo "ignored_dir/" >>.gitignore &&
43 test_expect_success
'prompt - branch name' '
44 printf " (master)" >expected &&
45 __git_ps1 >"$actual" &&
46 test_cmp expected "$actual"
49 test_expect_success SYMLINKS
'prompt - branch name - symlink symref' '
50 printf " (master)" >expected &&
51 test_when_finished "git checkout master" &&
52 test_config core.preferSymlinkRefs true &&
53 git checkout master &&
54 __git_ps1 >"$actual" &&
55 test_cmp expected "$actual"
58 test_expect_success
'prompt - unborn branch' '
59 printf " (unborn)" >expected &&
60 git checkout --orphan unborn &&
61 test_when_finished "git checkout master" &&
62 __git_ps1 >"$actual" &&
63 test_cmp expected "$actual"
66 if test_have_prereq
!FUNNYNAMES
; then
67 say
'Your filesystem does not allow newlines in filenames.'
70 test_expect_success FUNNYNAMES
'prompt - with newline in path' '
71 repo_with_newline="repo
74 mkdir "$repo_with_newline" &&
75 printf " (master)" >expected &&
76 git init "$repo_with_newline" &&
77 test_when_finished "rm -rf \"$repo_with_newline\"" &&
78 mkdir "$repo_with_newline"/subdir &&
80 cd "$repo_with_newline/subdir" &&
83 test_cmp expected "$actual"
86 test_expect_success
'prompt - detached head' '
87 printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
88 test_config core.abbrev 13 &&
90 test_when_finished "git checkout master" &&
91 __git_ps1 >"$actual" &&
92 test_cmp expected "$actual"
95 test_expect_success
'prompt - describe detached head - contains' '
96 printf " ((t2~1))" >expected &&
98 test_when_finished "git checkout master" &&
100 GIT_PS1_DESCRIBE_STYLE=contains &&
103 test_cmp expected "$actual"
106 test_expect_success
'prompt - describe detached head - branch' '
107 printf " ((tags/t2~1))" >expected &&
109 test_when_finished "git checkout master" &&
111 GIT_PS1_DESCRIBE_STYLE=branch &&
114 test_cmp expected "$actual"
117 test_expect_success
'prompt - describe detached head - describe' '
118 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
120 test_when_finished "git checkout master" &&
122 GIT_PS1_DESCRIBE_STYLE=describe &&
125 test_cmp expected "$actual"
128 test_expect_success
'prompt - describe detached head - default' '
129 printf " ((t2))" >expected &&
130 git checkout --detach b1 &&
131 test_when_finished "git checkout master" &&
132 __git_ps1 >"$actual" &&
133 test_cmp expected "$actual"
136 test_expect_success
'prompt - inside .git directory' '
137 printf " (GIT_DIR!)" >expected &&
142 test_cmp expected "$actual"
145 test_expect_success
'prompt - deep inside .git directory' '
146 printf " (GIT_DIR!)" >expected &&
151 test_cmp expected "$actual"
154 test_expect_success
'prompt - inside bare repository' '
155 printf " (BARE:master)" >expected &&
156 git init --bare bare.git &&
157 test_when_finished "rm -rf bare.git" &&
162 test_cmp expected "$actual"
165 test_expect_success
'prompt - interactive rebase' '
166 printf " (b1|REBASE-i 2/3)" >expected &&
167 write_script fake_editor.sh <<-\EOF &&
168 echo "exec echo" >"$1"
169 echo "edit $(git log -1 --format="%h")" >>"$1"
170 echo "exec echo" >>"$1"
172 test_when_finished "rm -f fake_editor.sh" &&
173 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
175 test_when_finished "git checkout master" &&
176 git rebase -i HEAD^ &&
177 test_when_finished "git rebase --abort" &&
178 __git_ps1 >"$actual" &&
179 test_cmp expected "$actual"
182 test_expect_success
'prompt - rebase merge' '
183 printf " (b2|REBASE-m 1/3)" >expected &&
185 test_when_finished "git checkout master" &&
186 test_must_fail git rebase --merge b1 b2 &&
187 test_when_finished "git rebase --abort" &&
188 __git_ps1 >"$actual" &&
189 test_cmp expected "$actual"
192 test_expect_success
'prompt - rebase' '
193 printf " (b2|REBASE 1/3)" >expected &&
195 test_when_finished "git checkout master" &&
196 test_must_fail git rebase b1 b2 &&
197 test_when_finished "git rebase --abort" &&
198 __git_ps1 >"$actual" &&
199 test_cmp expected "$actual"
202 test_expect_success
'prompt - merge' '
203 printf " (b1|MERGING)" >expected &&
205 test_when_finished "git checkout master" &&
206 test_must_fail git merge b2 &&
207 test_when_finished "git reset --hard" &&
208 __git_ps1 >"$actual" &&
209 test_cmp expected "$actual"
212 test_expect_success
'prompt - cherry-pick' '
213 printf " (master|CHERRY-PICKING)" >expected &&
214 test_must_fail git cherry-pick b1 &&
215 test_when_finished "git reset --hard" &&
216 __git_ps1 >"$actual" &&
217 test_cmp expected "$actual"
220 test_expect_success
'prompt - bisect' '
221 printf " (master|BISECTING)" >expected &&
223 test_when_finished "git bisect reset" &&
224 __git_ps1 >"$actual" &&
225 test_cmp expected "$actual"
228 test_expect_success
'prompt - dirty status indicator - clean' '
229 printf " (master)" >expected &&
231 GIT_PS1_SHOWDIRTYSTATE=y &&
234 test_cmp expected "$actual"
237 test_expect_success
'prompt - dirty status indicator - dirty worktree' '
238 printf " (master *)" >expected &&
239 echo "dirty" >file &&
240 test_when_finished "git reset --hard" &&
242 GIT_PS1_SHOWDIRTYSTATE=y &&
245 test_cmp expected "$actual"
248 test_expect_success
'prompt - dirty status indicator - dirty index' '
249 printf " (master +)" >expected &&
250 echo "dirty" >file &&
251 test_when_finished "git reset --hard" &&
254 GIT_PS1_SHOWDIRTYSTATE=y &&
257 test_cmp expected "$actual"
260 test_expect_success
'prompt - dirty status indicator - dirty index and worktree' '
261 printf " (master *+)" >expected &&
262 echo "dirty index" >file &&
263 test_when_finished "git reset --hard" &&
265 echo "dirty worktree" >file &&
267 GIT_PS1_SHOWDIRTYSTATE=y &&
270 test_cmp expected "$actual"
273 test_expect_success
'prompt - dirty status indicator - orphan branch - clean' '
274 printf " (orphan #)" >expected &&
275 test_when_finished "git checkout master" &&
276 git checkout --orphan orphan &&
279 GIT_PS1_SHOWDIRTYSTATE=y &&
282 test_cmp expected "$actual"
285 test_expect_success
'prompt - dirty status indicator - orphan branch - dirty index' '
286 printf " (orphan +)" >expected &&
287 test_when_finished "git checkout master" &&
288 git checkout --orphan orphan &&
290 GIT_PS1_SHOWDIRTYSTATE=y &&
293 test_cmp expected "$actual"
296 test_expect_success
'prompt - dirty status indicator - orphan branch - dirty index and worktree' '
297 printf " (orphan *+)" >expected &&
298 test_when_finished "git checkout master" &&
299 git checkout --orphan orphan &&
302 GIT_PS1_SHOWDIRTYSTATE=y &&
305 test_cmp expected "$actual"
308 test_expect_success
'prompt - dirty status indicator - shell variable unset 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 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
317 test_cmp expected "$actual"
320 test_expect_success
'prompt - dirty status indicator - shell variable unset 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 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
329 test_cmp expected "$actual"
332 test_expect_success
'prompt - dirty status indicator - shell variable set with config disabled' '
333 printf " (master)" >expected &&
334 echo "dirty" >file &&
335 test_when_finished "git reset --hard" &&
336 test_config bash.showDirtyState false &&
338 GIT_PS1_SHOWDIRTYSTATE=y &&
341 test_cmp expected "$actual"
344 test_expect_success
'prompt - dirty status indicator - shell variable set with config enabled' '
345 printf " (master *)" >expected &&
346 echo "dirty" >file &&
347 test_when_finished "git reset --hard" &&
348 test_config bash.showDirtyState true &&
350 GIT_PS1_SHOWDIRTYSTATE=y &&
353 test_cmp expected "$actual"
356 test_expect_success
'prompt - dirty status indicator - not shown inside .git directory' '
357 printf " (GIT_DIR!)" >expected &&
358 echo "dirty" >file &&
359 test_when_finished "git reset --hard" &&
361 GIT_PS1_SHOWDIRTYSTATE=y &&
365 test_cmp expected "$actual"
368 test_expect_success
'prompt - stash status indicator - no stash' '
369 printf " (master)" >expected &&
371 GIT_PS1_SHOWSTASHSTATE=y &&
374 test_cmp expected "$actual"
377 test_expect_success
'prompt - stash status indicator - stash' '
378 printf " (master $)" >expected &&
381 test_when_finished "git stash drop" &&
382 git pack-refs --all &&
384 GIT_PS1_SHOWSTASHSTATE=y &&
387 test_cmp expected "$actual"
390 test_expect_success
'prompt - stash status indicator - not shown inside .git directory' '
391 printf " (GIT_DIR!)" >expected &&
394 test_when_finished "git stash drop" &&
396 GIT_PS1_SHOWSTASHSTATE=y &&
400 test_cmp expected "$actual"
403 test_expect_success
'prompt - untracked files status indicator - no untracked files' '
404 printf " (master)" >expected &&
406 GIT_PS1_SHOWUNTRACKEDFILES=y &&
410 test_cmp expected "$actual"
413 test_expect_success
'prompt - untracked files status indicator - untracked files' '
414 printf " (master %%)" >expected &&
416 GIT_PS1_SHOWUNTRACKEDFILES=y &&
419 test_cmp expected "$actual"
422 test_expect_success
'prompt - untracked files status indicator - empty untracked dir' '
423 printf " (master)" >expected &&
424 mkdir otherrepo/untracked-dir &&
425 test_when_finished "rm -rf otherrepo/untracked-dir" &&
427 GIT_PS1_SHOWUNTRACKEDFILES=y &&
431 test_cmp expected "$actual"
434 test_expect_success
'prompt - untracked files status indicator - non-empty untracked dir' '
435 printf " (master %%)" >expected &&
436 mkdir otherrepo/untracked-dir &&
437 test_when_finished "rm -rf otherrepo/untracked-dir" &&
438 >otherrepo/untracked-dir/untracked-file &&
440 GIT_PS1_SHOWUNTRACKEDFILES=y &&
444 test_cmp expected "$actual"
447 test_expect_success
'prompt - untracked files status indicator - untracked files outside cwd' '
448 printf " (master %%)" >expected &&
450 mkdir -p ignored_dir &&
452 GIT_PS1_SHOWUNTRACKEDFILES=y &&
455 test_cmp expected "$actual"
458 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config disabled' '
459 printf " (master)" >expected &&
460 test_config bash.showUntrackedFiles false &&
462 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
465 test_cmp expected "$actual"
468 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config enabled' '
469 printf " (master)" >expected &&
470 test_config bash.showUntrackedFiles true &&
472 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
475 test_cmp expected "$actual"
478 test_expect_success
'prompt - untracked files status indicator - shell variable set with config disabled' '
479 printf " (master)" >expected &&
480 test_config bash.showUntrackedFiles false &&
482 GIT_PS1_SHOWUNTRACKEDFILES=y &&
485 test_cmp expected "$actual"
488 test_expect_success
'prompt - untracked files status indicator - shell variable set with config enabled' '
489 printf " (master %%)" >expected &&
490 test_config bash.showUntrackedFiles true &&
492 GIT_PS1_SHOWUNTRACKEDFILES=y &&
495 test_cmp expected "$actual"
498 test_expect_success
'prompt - untracked files status indicator - not shown inside .git directory' '
499 printf " (GIT_DIR!)" >expected &&
501 GIT_PS1_SHOWUNTRACKEDFILES=y &&
505 test_cmp expected "$actual"
508 test_expect_success
'prompt - format string starting with dash' '
509 printf -- "-master" >expected &&
510 __git_ps1 "-%s" >"$actual" &&
511 test_cmp expected "$actual"
514 test_expect_success
'prompt - pc mode' '
515 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER\\nmaster" >expected &&
517 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
518 test_must_be_empty "$actual" &&
519 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
521 test_cmp expected "$actual"
524 test_expect_success
'prompt - bash color pc mode - branch name' '
525 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nmaster" >expected &&
527 GIT_PS1_SHOWCOLORHINTS=y &&
528 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
529 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
531 test_cmp expected "$actual"
534 test_expect_success
'prompt - bash color pc mode - detached head' '
535 printf "BEFORE: (${c_red}\${__git_ps1_branch_name}${c_clear}):AFTER\\n(%s...)" $(git log -1 --format="%h" b1^) >expected &&
537 test_when_finished "git checkout master" &&
539 GIT_PS1_SHOWCOLORHINTS=y &&
540 __git_ps1 "BEFORE:" ":AFTER" &&
541 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
543 test_cmp expected "$actual"
546 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
547 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}):AFTER\\nmaster" >expected &&
548 echo "dirty" >file &&
549 test_when_finished "git reset --hard" &&
551 GIT_PS1_SHOWDIRTYSTATE=y &&
552 GIT_PS1_SHOWCOLORHINTS=y &&
553 __git_ps1 "BEFORE:" ":AFTER" &&
554 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
556 test_cmp expected "$actual"
559 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index' '
560 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}+${c_clear}):AFTER\\nmaster" >expected &&
561 echo "dirty" >file &&
562 test_when_finished "git reset --hard" &&
565 GIT_PS1_SHOWDIRTYSTATE=y &&
566 GIT_PS1_SHOWCOLORHINTS=y &&
567 __git_ps1 "BEFORE:" ":AFTER" &&
568 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
570 test_cmp expected "$actual"
573 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
574 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_green}+${c_clear}):AFTER\\nmaster" >expected &&
575 echo "dirty index" >file &&
576 test_when_finished "git reset --hard" &&
578 echo "dirty worktree" >file &&
580 GIT_PS1_SHOWCOLORHINTS=y &&
581 GIT_PS1_SHOWDIRTYSTATE=y &&
582 __git_ps1 "BEFORE:" ":AFTER" &&
583 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
585 test_cmp expected "$actual"
588 test_expect_success
'prompt - bash color pc mode - dirty status indicator - before root commit' '
589 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}#${c_clear}):AFTER\\nmaster" >expected &&
591 GIT_PS1_SHOWDIRTYSTATE=y &&
592 GIT_PS1_SHOWCOLORHINTS=y &&
594 __git_ps1 "BEFORE:" ":AFTER" &&
595 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
597 test_cmp expected "$actual"
600 test_expect_success
'prompt - bash color pc mode - inside .git directory' '
601 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nGIT_DIR!" >expected &&
602 echo "dirty" >file &&
603 test_when_finished "git reset --hard" &&
605 GIT_PS1_SHOWDIRTYSTATE=y &&
606 GIT_PS1_SHOWCOLORHINTS=y &&
608 __git_ps1 "BEFORE:" ":AFTER" &&
609 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
611 test_cmp expected "$actual"
614 test_expect_success
'prompt - bash color pc mode - stash status indicator' '
615 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_lblue}\$${c_clear}):AFTER\\nmaster" >expected &&
618 test_when_finished "git stash drop" &&
620 GIT_PS1_SHOWSTASHSTATE=y &&
621 GIT_PS1_SHOWCOLORHINTS=y &&
622 __git_ps1 "BEFORE:" ":AFTER" &&
623 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
625 test_cmp expected "$actual"
628 test_expect_success
'prompt - bash color pc mode - untracked files status indicator' '
629 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}%%${c_clear}):AFTER\\nmaster" >expected &&
631 GIT_PS1_SHOWUNTRACKEDFILES=y &&
632 GIT_PS1_SHOWCOLORHINTS=y &&
633 __git_ps1 "BEFORE:" ":AFTER" &&
634 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
636 test_cmp expected "$actual"
639 test_expect_success
'prompt - zsh color pc mode' '
640 printf "BEFORE: (%%F{green}master%%f):AFTER" >expected &&
643 GIT_PS1_SHOWCOLORHINTS=y &&
644 __git_ps1 "BEFORE:" ":AFTER" &&
645 printf "%s" "$PS1" >"$actual"
647 test_cmp expected "$actual"
650 test_expect_success
'prompt - hide if pwd ignored - env var unset, config disabled' '
651 printf " (master)" >expected &&
652 test_config bash.hideIfPwdIgnored false &&
657 test_cmp expected "$actual"
660 test_expect_success
'prompt - hide if pwd ignored - env var unset, config disabled, pc mode' '
661 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
662 test_config bash.hideIfPwdIgnored false &&
665 __git_ps1 "BEFORE:" ":AFTER" &&
666 printf "%s" "$PS1" >"$actual"
668 test_cmp expected "$actual"
671 test_expect_success
'prompt - hide if pwd ignored - env var unset, config unset' '
672 printf " (master)" >expected &&
677 test_cmp expected "$actual"
680 test_expect_success
'prompt - hide if pwd ignored - env var unset, config unset, pc mode' '
681 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
684 __git_ps1 "BEFORE:" ":AFTER" &&
685 printf "%s" "$PS1" >"$actual"
687 test_cmp expected "$actual"
690 test_expect_success
'prompt - hide if pwd ignored - env var set, config disabled' '
691 printf " (master)" >expected &&
692 test_config bash.hideIfPwdIgnored false &&
695 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
698 test_cmp expected "$actual"
701 test_expect_success
'prompt - hide if pwd ignored - env var set, config disabled, pc mode' '
702 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
703 test_config bash.hideIfPwdIgnored false &&
706 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
707 __git_ps1 "BEFORE:" ":AFTER" &&
708 printf "%s" "$PS1" >"$actual"
710 test_cmp expected "$actual"
713 test_expect_success
'prompt - hide if pwd ignored - env var set, config unset' '
716 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
719 test_must_be_empty "$actual"
722 test_expect_success
'prompt - hide if pwd ignored - env var set, config unset, pc mode' '
723 printf "BEFORE::AFTER" >expected &&
726 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
727 __git_ps1 "BEFORE:" ":AFTER" &&
728 printf "%s" "$PS1" >"$actual"
730 test_cmp expected "$actual"
733 test_expect_success
'prompt - hide if pwd ignored - inside gitdir' '
734 printf " (GIT_DIR!)" >expected &&
736 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
740 test_cmp expected "$actual"