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 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 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 am' '
193 printf " (b2|REBASE 1/3)" >expected &&
195 test_when_finished "git checkout master" &&
196 test_must_fail git rebase --apply 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 b1^ &&
215 test_when_finished "git cherry-pick --abort" &&
216 __git_ps1 >"$actual" &&
217 test_cmp expected "$actual" &&
219 test_must_fail git rev-parse CHERRY_PICK_HEAD &&
220 __git_ps1 >"$actual" &&
221 test_cmp expected "$actual"
224 test_expect_success
'prompt - revert' '
225 printf " (master|REVERTING)" >expected &&
226 test_must_fail git revert b1^ b1 &&
227 test_when_finished "git revert --abort" &&
228 __git_ps1 >"$actual" &&
229 test_cmp expected "$actual" &&
231 test_must_fail git rev-parse REVERT_HEAD &&
232 __git_ps1 >"$actual" &&
233 test_cmp expected "$actual"
236 test_expect_success
'prompt - bisect' '
237 printf " (master|BISECTING)" >expected &&
239 test_when_finished "git bisect reset" &&
240 __git_ps1 >"$actual" &&
241 test_cmp expected "$actual"
244 test_expect_success
'prompt - dirty status indicator - clean' '
245 printf " (master)" >expected &&
247 GIT_PS1_SHOWDIRTYSTATE=y &&
250 test_cmp expected "$actual"
253 test_expect_success
'prompt - dirty status indicator - dirty worktree' '
254 printf " (master *)" >expected &&
255 echo "dirty" >file &&
256 test_when_finished "git reset --hard" &&
258 GIT_PS1_SHOWDIRTYSTATE=y &&
261 test_cmp expected "$actual"
264 test_expect_success
'prompt - dirty status indicator - dirty index' '
265 printf " (master +)" >expected &&
266 echo "dirty" >file &&
267 test_when_finished "git reset --hard" &&
270 GIT_PS1_SHOWDIRTYSTATE=y &&
273 test_cmp expected "$actual"
276 test_expect_success
'prompt - dirty status indicator - dirty index and worktree' '
277 printf " (master *+)" >expected &&
278 echo "dirty index" >file &&
279 test_when_finished "git reset --hard" &&
281 echo "dirty worktree" >file &&
283 GIT_PS1_SHOWDIRTYSTATE=y &&
286 test_cmp expected "$actual"
289 test_expect_success
'prompt - dirty status indicator - orphan branch - clean' '
290 printf " (orphan #)" >expected &&
291 test_when_finished "git checkout master" &&
292 git checkout --orphan orphan &&
295 GIT_PS1_SHOWDIRTYSTATE=y &&
298 test_cmp expected "$actual"
301 test_expect_success
'prompt - dirty status indicator - orphan branch - dirty index' '
302 printf " (orphan +)" >expected &&
303 test_when_finished "git checkout master" &&
304 git checkout --orphan orphan &&
306 GIT_PS1_SHOWDIRTYSTATE=y &&
309 test_cmp expected "$actual"
312 test_expect_success
'prompt - dirty status indicator - orphan branch - dirty index and worktree' '
313 printf " (orphan *+)" >expected &&
314 test_when_finished "git checkout master" &&
315 git checkout --orphan orphan &&
318 GIT_PS1_SHOWDIRTYSTATE=y &&
321 test_cmp expected "$actual"
324 test_expect_success
'prompt - dirty status indicator - shell variable unset with config disabled' '
325 printf " (master)" >expected &&
326 echo "dirty" >file &&
327 test_when_finished "git reset --hard" &&
328 test_config bash.showDirtyState false &&
330 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
333 test_cmp expected "$actual"
336 test_expect_success
'prompt - dirty status indicator - shell variable unset with config enabled' '
337 printf " (master)" >expected &&
338 echo "dirty" >file &&
339 test_when_finished "git reset --hard" &&
340 test_config bash.showDirtyState true &&
342 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
345 test_cmp expected "$actual"
348 test_expect_success
'prompt - dirty status indicator - shell variable set with config disabled' '
349 printf " (master)" >expected &&
350 echo "dirty" >file &&
351 test_when_finished "git reset --hard" &&
352 test_config bash.showDirtyState false &&
354 GIT_PS1_SHOWDIRTYSTATE=y &&
357 test_cmp expected "$actual"
360 test_expect_success
'prompt - dirty status indicator - shell variable set with config enabled' '
361 printf " (master *)" >expected &&
362 echo "dirty" >file &&
363 test_when_finished "git reset --hard" &&
364 test_config bash.showDirtyState true &&
366 GIT_PS1_SHOWDIRTYSTATE=y &&
369 test_cmp expected "$actual"
372 test_expect_success
'prompt - dirty status indicator - not shown inside .git directory' '
373 printf " (GIT_DIR!)" >expected &&
374 echo "dirty" >file &&
375 test_when_finished "git reset --hard" &&
377 GIT_PS1_SHOWDIRTYSTATE=y &&
381 test_cmp expected "$actual"
384 test_expect_success
'prompt - stash status indicator - no stash' '
385 printf " (master)" >expected &&
387 GIT_PS1_SHOWSTASHSTATE=y &&
390 test_cmp expected "$actual"
393 test_expect_success
'prompt - stash status indicator - stash' '
394 printf " (master $)" >expected &&
397 test_when_finished "git stash drop" &&
398 git pack-refs --all &&
400 GIT_PS1_SHOWSTASHSTATE=y &&
403 test_cmp expected "$actual"
406 test_expect_success
'prompt - stash status indicator - not shown inside .git directory' '
407 printf " (GIT_DIR!)" >expected &&
410 test_when_finished "git stash drop" &&
412 GIT_PS1_SHOWSTASHSTATE=y &&
416 test_cmp expected "$actual"
419 test_expect_success
'prompt - untracked files status indicator - no untracked files' '
420 printf " (master)" >expected &&
422 GIT_PS1_SHOWUNTRACKEDFILES=y &&
426 test_cmp expected "$actual"
429 test_expect_success
'prompt - untracked files status indicator - untracked files' '
430 printf " (master %%)" >expected &&
432 GIT_PS1_SHOWUNTRACKEDFILES=y &&
435 test_cmp expected "$actual"
438 test_expect_success
'prompt - untracked files status indicator - empty untracked dir' '
439 printf " (master)" >expected &&
440 mkdir otherrepo/untracked-dir &&
441 test_when_finished "rm -rf otherrepo/untracked-dir" &&
443 GIT_PS1_SHOWUNTRACKEDFILES=y &&
447 test_cmp expected "$actual"
450 test_expect_success
'prompt - untracked files status indicator - non-empty untracked dir' '
451 printf " (master %%)" >expected &&
452 mkdir otherrepo/untracked-dir &&
453 test_when_finished "rm -rf otherrepo/untracked-dir" &&
454 >otherrepo/untracked-dir/untracked-file &&
456 GIT_PS1_SHOWUNTRACKEDFILES=y &&
460 test_cmp expected "$actual"
463 test_expect_success
'prompt - untracked files status indicator - untracked files outside cwd' '
464 printf " (master %%)" >expected &&
466 mkdir -p ignored_dir &&
468 GIT_PS1_SHOWUNTRACKEDFILES=y &&
471 test_cmp expected "$actual"
474 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config disabled' '
475 printf " (master)" >expected &&
476 test_config bash.showUntrackedFiles false &&
478 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
481 test_cmp expected "$actual"
484 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config enabled' '
485 printf " (master)" >expected &&
486 test_config bash.showUntrackedFiles true &&
488 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
491 test_cmp expected "$actual"
494 test_expect_success
'prompt - untracked files status indicator - shell variable set with config disabled' '
495 printf " (master)" >expected &&
496 test_config bash.showUntrackedFiles false &&
498 GIT_PS1_SHOWUNTRACKEDFILES=y &&
501 test_cmp expected "$actual"
504 test_expect_success
'prompt - untracked files status indicator - shell variable set with config enabled' '
505 printf " (master %%)" >expected &&
506 test_config bash.showUntrackedFiles true &&
508 GIT_PS1_SHOWUNTRACKEDFILES=y &&
511 test_cmp expected "$actual"
514 test_expect_success
'prompt - untracked files status indicator - not shown inside .git directory' '
515 printf " (GIT_DIR!)" >expected &&
517 GIT_PS1_SHOWUNTRACKEDFILES=y &&
521 test_cmp expected "$actual"
524 test_expect_success
'prompt - format string starting with dash' '
525 printf -- "-master" >expected &&
526 __git_ps1 "-%s" >"$actual" &&
527 test_cmp expected "$actual"
530 test_expect_success
'prompt - pc mode' '
531 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER\\nmaster" >expected &&
533 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
534 test_must_be_empty "$actual" &&
535 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
537 test_cmp expected "$actual"
540 test_expect_success
'prompt - bash color pc mode - branch name' '
541 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nmaster" >expected &&
543 GIT_PS1_SHOWCOLORHINTS=y &&
544 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
545 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
547 test_cmp expected "$actual"
550 test_expect_success
'prompt - bash color pc mode - detached head' '
551 printf "BEFORE: (${c_red}\${__git_ps1_branch_name}${c_clear}):AFTER\\n(%s...)" $(git log -1 --format="%h" b1^) >expected &&
553 test_when_finished "git checkout master" &&
555 GIT_PS1_SHOWCOLORHINTS=y &&
556 __git_ps1 "BEFORE:" ":AFTER" &&
557 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
559 test_cmp expected "$actual"
562 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
563 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}):AFTER\\nmaster" >expected &&
564 echo "dirty" >file &&
565 test_when_finished "git reset --hard" &&
567 GIT_PS1_SHOWDIRTYSTATE=y &&
568 GIT_PS1_SHOWCOLORHINTS=y &&
569 __git_ps1 "BEFORE:" ":AFTER" &&
570 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
572 test_cmp expected "$actual"
575 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index' '
576 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}+${c_clear}):AFTER\\nmaster" >expected &&
577 echo "dirty" >file &&
578 test_when_finished "git reset --hard" &&
581 GIT_PS1_SHOWDIRTYSTATE=y &&
582 GIT_PS1_SHOWCOLORHINTS=y &&
583 __git_ps1 "BEFORE:" ":AFTER" &&
584 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
586 test_cmp expected "$actual"
589 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
590 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_green}+${c_clear}):AFTER\\nmaster" >expected &&
591 echo "dirty index" >file &&
592 test_when_finished "git reset --hard" &&
594 echo "dirty worktree" >file &&
596 GIT_PS1_SHOWCOLORHINTS=y &&
597 GIT_PS1_SHOWDIRTYSTATE=y &&
598 __git_ps1 "BEFORE:" ":AFTER" &&
599 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
601 test_cmp expected "$actual"
604 test_expect_success
'prompt - bash color pc mode - dirty status indicator - before root commit' '
605 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}#${c_clear}):AFTER\\nmaster" >expected &&
607 GIT_PS1_SHOWDIRTYSTATE=y &&
608 GIT_PS1_SHOWCOLORHINTS=y &&
610 __git_ps1 "BEFORE:" ":AFTER" &&
611 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
613 test_cmp expected "$actual"
616 test_expect_success
'prompt - bash color pc mode - inside .git directory' '
617 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nGIT_DIR!" >expected &&
618 echo "dirty" >file &&
619 test_when_finished "git reset --hard" &&
621 GIT_PS1_SHOWDIRTYSTATE=y &&
622 GIT_PS1_SHOWCOLORHINTS=y &&
624 __git_ps1 "BEFORE:" ":AFTER" &&
625 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
627 test_cmp expected "$actual"
630 test_expect_success
'prompt - bash color pc mode - stash status indicator' '
631 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_lblue}\$${c_clear}):AFTER\\nmaster" >expected &&
634 test_when_finished "git stash drop" &&
636 GIT_PS1_SHOWSTASHSTATE=y &&
637 GIT_PS1_SHOWCOLORHINTS=y &&
638 __git_ps1 "BEFORE:" ":AFTER" &&
639 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
641 test_cmp expected "$actual"
644 test_expect_success
'prompt - bash color pc mode - untracked files status indicator' '
645 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}%%${c_clear}):AFTER\\nmaster" >expected &&
647 GIT_PS1_SHOWUNTRACKEDFILES=y &&
648 GIT_PS1_SHOWCOLORHINTS=y &&
649 __git_ps1 "BEFORE:" ":AFTER" &&
650 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
652 test_cmp expected "$actual"
655 test_expect_success
'prompt - zsh color pc mode' '
656 printf "BEFORE: (%%F{green}master%%f):AFTER" >expected &&
659 GIT_PS1_SHOWCOLORHINTS=y &&
660 __git_ps1 "BEFORE:" ":AFTER" &&
661 printf "%s" "$PS1" >"$actual"
663 test_cmp expected "$actual"
666 test_expect_success
'prompt - hide if pwd ignored - env var unset, config disabled' '
667 printf " (master)" >expected &&
668 test_config bash.hideIfPwdIgnored false &&
673 test_cmp expected "$actual"
676 test_expect_success
'prompt - hide if pwd ignored - env var unset, config disabled, pc mode' '
677 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
678 test_config bash.hideIfPwdIgnored false &&
681 __git_ps1 "BEFORE:" ":AFTER" &&
682 printf "%s" "$PS1" >"$actual"
684 test_cmp expected "$actual"
687 test_expect_success
'prompt - hide if pwd ignored - env var unset, config unset' '
688 printf " (master)" >expected &&
693 test_cmp expected "$actual"
696 test_expect_success
'prompt - hide if pwd ignored - env var unset, config unset, pc mode' '
697 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
700 __git_ps1 "BEFORE:" ":AFTER" &&
701 printf "%s" "$PS1" >"$actual"
703 test_cmp expected "$actual"
706 test_expect_success
'prompt - hide if pwd ignored - env var set, config disabled' '
707 printf " (master)" >expected &&
708 test_config bash.hideIfPwdIgnored false &&
711 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
714 test_cmp expected "$actual"
717 test_expect_success
'prompt - hide if pwd ignored - env var set, config disabled, pc mode' '
718 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
719 test_config bash.hideIfPwdIgnored false &&
722 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
723 __git_ps1 "BEFORE:" ":AFTER" &&
724 printf "%s" "$PS1" >"$actual"
726 test_cmp expected "$actual"
729 test_expect_success
'prompt - hide if pwd ignored - env var set, config unset' '
732 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
735 test_must_be_empty "$actual"
738 test_expect_success
'prompt - hide if pwd ignored - env var set, config unset, pc mode' '
739 printf "BEFORE::AFTER" >expected &&
742 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
743 __git_ps1 "BEFORE:" ":AFTER" &&
744 printf "%s" "$PS1" >"$actual"
746 test_cmp expected "$actual"
749 test_expect_success
'prompt - hide if pwd ignored - inside gitdir' '
750 printf " (GIT_DIR!)" >expected &&
752 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
756 test_cmp expected "$actual"