3 # Copyright (c) 2012 SZEDER Gábor
6 test_description
='test git-specific bash prompt functions'
8 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
9 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
11 TEST_PASSES_SANITIZE_LEAK
=true
14 .
"$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
16 actual
="$TRASH_DIRECTORY/actual"
17 c_red
='\001\e[31m\002'
18 c_green
='\001\e[32m\002'
19 c_lblue
='\001\e[1;34m\002'
20 c_clear
='\001\e[0m\002'
22 test_expect_success
'setup for prompt tests' '
27 git commit -m initial &&
28 git tag -a -m msg1 t1 &&
31 git commit -m "second b1" file &&
33 git commit -m "third b1" file &&
34 git tag -a -m msg2 t2 &&
35 git checkout -b b2 main &&
37 git commit -m "second b2" file &&
39 git commit -m "another b2" file &&
41 git commit -m "yet another b2" file &&
43 echo "ignored_dir/" >>.gitignore &&
47 test_expect_success
'prompt - branch name' '
48 printf " (main)" >expected &&
49 __git_ps1 >"$actual" &&
50 test_cmp expected "$actual"
53 test_expect_success SYMLINKS
'prompt - branch name - symlink symref' '
54 printf " (main)" >expected &&
55 test_when_finished "git checkout main" &&
56 test_config core.preferSymlinkRefs true &&
58 __git_ps1 >"$actual" &&
59 test_cmp expected "$actual"
62 test_expect_success
'prompt - unborn branch' '
63 printf " (unborn)" >expected &&
64 git checkout --orphan unborn &&
65 test_when_finished "git checkout main" &&
66 __git_ps1 >"$actual" &&
67 test_cmp expected "$actual"
70 if test_have_prereq
!FUNNYNAMES
; then
71 say
'Your filesystem does not allow newlines in filenames.'
74 test_expect_success FUNNYNAMES
'prompt - with newline in path' '
75 repo_with_newline="repo
78 mkdir "$repo_with_newline" &&
79 printf " (main)" >expected &&
80 git init "$repo_with_newline" &&
81 test_when_finished "rm -rf \"$repo_with_newline\"" &&
82 mkdir "$repo_with_newline"/subdir &&
84 cd "$repo_with_newline/subdir" &&
87 test_cmp expected "$actual"
90 test_expect_success
'prompt - detached head' '
91 printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
92 test_config core.abbrev 13 &&
94 test_when_finished "git checkout main" &&
95 __git_ps1 >"$actual" &&
96 test_cmp expected "$actual"
99 test_expect_success
'prompt - describe detached head - contains' '
100 printf " ((t2~1))" >expected &&
102 test_when_finished "git checkout main" &&
104 GIT_PS1_DESCRIBE_STYLE=contains &&
107 test_cmp expected "$actual"
110 test_expect_success
'prompt - describe detached head - branch' '
111 printf " ((tags/t2~1))" >expected &&
113 test_when_finished "git checkout main" &&
115 GIT_PS1_DESCRIBE_STYLE=branch &&
118 test_cmp expected "$actual"
121 test_expect_success
'prompt - describe detached head - describe' '
122 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
124 test_when_finished "git checkout main" &&
126 GIT_PS1_DESCRIBE_STYLE=describe &&
129 test_cmp expected "$actual"
132 test_expect_success
'prompt - describe detached head - default' '
133 printf " ((t2))" >expected &&
134 git checkout --detach b1 &&
135 test_when_finished "git checkout main" &&
136 __git_ps1 >"$actual" &&
137 test_cmp expected "$actual"
140 test_expect_success
'prompt - inside .git directory' '
141 printf " (GIT_DIR!)" >expected &&
146 test_cmp expected "$actual"
149 test_expect_success
'prompt - deep inside .git directory' '
150 printf " (GIT_DIR!)" >expected &&
155 test_cmp expected "$actual"
158 test_expect_success
'prompt - inside bare repository' '
159 printf " (BARE:main)" >expected &&
160 git init --bare bare.git &&
161 test_when_finished "rm -rf bare.git" &&
166 test_cmp expected "$actual"
169 test_expect_success
'prompt - interactive rebase' '
170 printf " (b1|REBASE 2/3)" >expected &&
171 write_script fake_editor.sh <<-\EOF &&
172 echo "exec echo" >"$1"
173 echo "edit $(git log -1 --format="%h")" >>"$1"
174 echo "exec echo" >>"$1"
176 test_when_finished "rm -f fake_editor.sh" &&
177 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
179 test_when_finished "git checkout main" &&
180 git rebase -i HEAD^ &&
181 test_when_finished "git rebase --abort" &&
182 __git_ps1 >"$actual" &&
183 test_cmp expected "$actual"
186 test_expect_success
'prompt - rebase merge' '
187 printf " (b2|REBASE 1/3)" >expected &&
189 test_when_finished "git checkout main" &&
190 test_must_fail git rebase --merge b1 b2 &&
191 test_when_finished "git rebase --abort" &&
192 __git_ps1 >"$actual" &&
193 test_cmp expected "$actual"
196 test_expect_success
'prompt - rebase am' '
197 printf " (b2|REBASE 1/3)" >expected &&
199 test_when_finished "git checkout main" &&
200 test_must_fail git rebase --apply b1 b2 &&
201 test_when_finished "git rebase --abort" &&
202 __git_ps1 >"$actual" &&
203 test_cmp expected "$actual"
206 test_expect_success
'prompt - merge' '
207 printf " (b1|MERGING)" >expected &&
209 test_when_finished "git checkout main" &&
210 test_must_fail git merge b2 &&
211 test_when_finished "git reset --hard" &&
212 __git_ps1 >"$actual" &&
213 test_cmp expected "$actual"
216 test_expect_success
'prompt - cherry-pick' '
217 printf " (main|CHERRY-PICKING)" >expected &&
218 test_must_fail git cherry-pick b1 b1^ &&
219 test_when_finished "git cherry-pick --abort" &&
220 __git_ps1 >"$actual" &&
221 test_cmp expected "$actual" &&
223 test_must_fail git rev-parse CHERRY_PICK_HEAD &&
224 __git_ps1 >"$actual" &&
225 test_cmp expected "$actual"
228 test_expect_success
'prompt - revert' '
229 printf " (main|REVERTING)" >expected &&
230 test_must_fail git revert b1^ b1 &&
231 test_when_finished "git revert --abort" &&
232 __git_ps1 >"$actual" &&
233 test_cmp expected "$actual" &&
235 test_must_fail git rev-parse REVERT_HEAD &&
236 __git_ps1 >"$actual" &&
237 test_cmp expected "$actual"
240 test_expect_success
'prompt - bisect' '
241 printf " (main|BISECTING)" >expected &&
243 test_when_finished "git bisect reset" &&
244 __git_ps1 >"$actual" &&
245 test_cmp expected "$actual"
248 test_expect_success
'prompt - dirty status indicator - clean' '
249 printf " (main)" >expected &&
251 GIT_PS1_SHOWDIRTYSTATE=y &&
254 test_cmp expected "$actual"
257 test_expect_success
'prompt - dirty status indicator - dirty worktree' '
258 printf " (main *)" >expected &&
259 echo "dirty" >file &&
260 test_when_finished "git reset --hard" &&
262 GIT_PS1_SHOWDIRTYSTATE=y &&
265 test_cmp expected "$actual"
268 test_expect_success
'prompt - dirty status indicator - dirty index' '
269 printf " (main +)" >expected &&
270 echo "dirty" >file &&
271 test_when_finished "git reset --hard" &&
274 GIT_PS1_SHOWDIRTYSTATE=y &&
277 test_cmp expected "$actual"
280 test_expect_success
'prompt - dirty status indicator - dirty index and worktree' '
281 printf " (main *+)" >expected &&
282 echo "dirty index" >file &&
283 test_when_finished "git reset --hard" &&
285 echo "dirty worktree" >file &&
287 GIT_PS1_SHOWDIRTYSTATE=y &&
290 test_cmp expected "$actual"
293 test_expect_success
'prompt - dirty status indicator - orphan branch - clean' '
294 printf " (orphan #)" >expected &&
295 test_when_finished "git checkout main" &&
296 git checkout --orphan orphan &&
299 GIT_PS1_SHOWDIRTYSTATE=y &&
302 test_cmp expected "$actual"
305 test_expect_success
'prompt - dirty status indicator - orphan branch - dirty index' '
306 printf " (orphan +)" >expected &&
307 test_when_finished "git checkout main" &&
308 git checkout --orphan orphan &&
310 GIT_PS1_SHOWDIRTYSTATE=y &&
313 test_cmp expected "$actual"
316 test_expect_success
'prompt - dirty status indicator - orphan branch - dirty index and worktree' '
317 printf " (orphan *+)" >expected &&
318 test_when_finished "git checkout main" &&
319 git checkout --orphan orphan &&
322 GIT_PS1_SHOWDIRTYSTATE=y &&
325 test_cmp expected "$actual"
328 test_expect_success
'prompt - dirty status indicator - shell variable unset with config disabled' '
329 printf " (main)" >expected &&
330 echo "dirty" >file &&
331 test_when_finished "git reset --hard" &&
332 test_config bash.showDirtyState false &&
334 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
337 test_cmp expected "$actual"
340 test_expect_success
'prompt - dirty status indicator - shell variable unset with config enabled' '
341 printf " (main)" >expected &&
342 echo "dirty" >file &&
343 test_when_finished "git reset --hard" &&
344 test_config bash.showDirtyState true &&
346 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
349 test_cmp expected "$actual"
352 test_expect_success
'prompt - dirty status indicator - shell variable set with config disabled' '
353 printf " (main)" >expected &&
354 echo "dirty" >file &&
355 test_when_finished "git reset --hard" &&
356 test_config bash.showDirtyState false &&
358 GIT_PS1_SHOWDIRTYSTATE=y &&
361 test_cmp expected "$actual"
364 test_expect_success
'prompt - dirty status indicator - shell variable set with config enabled' '
365 printf " (main *)" >expected &&
366 echo "dirty" >file &&
367 test_when_finished "git reset --hard" &&
368 test_config bash.showDirtyState true &&
370 GIT_PS1_SHOWDIRTYSTATE=y &&
373 test_cmp expected "$actual"
376 test_expect_success
'prompt - dirty status indicator - not shown inside .git directory' '
377 printf " (GIT_DIR!)" >expected &&
378 echo "dirty" >file &&
379 test_when_finished "git reset --hard" &&
381 GIT_PS1_SHOWDIRTYSTATE=y &&
385 test_cmp expected "$actual"
388 test_expect_success
'prompt - stash status indicator - no stash' '
389 printf " (main)" >expected &&
391 GIT_PS1_SHOWSTASHSTATE=y &&
394 test_cmp expected "$actual"
397 test_expect_success
'prompt - stash status indicator - stash' '
398 printf " (main $)" >expected &&
401 test_when_finished "git stash drop" &&
402 git pack-refs --all &&
404 GIT_PS1_SHOWSTASHSTATE=y &&
407 test_cmp expected "$actual"
410 test_expect_success
'prompt - stash status indicator - not shown inside .git directory' '
411 printf " (GIT_DIR!)" >expected &&
414 test_when_finished "git stash drop" &&
416 GIT_PS1_SHOWSTASHSTATE=y &&
420 test_cmp expected "$actual"
423 test_expect_success
'prompt - untracked files status indicator - no untracked files' '
424 printf " (main)" >expected &&
426 GIT_PS1_SHOWUNTRACKEDFILES=y &&
430 test_cmp expected "$actual"
433 test_expect_success
'prompt - untracked files status indicator - untracked files' '
434 printf " (main %%)" >expected &&
436 GIT_PS1_SHOWUNTRACKEDFILES=y &&
439 test_cmp expected "$actual"
442 test_expect_success
'prompt - untracked files status indicator - empty untracked dir' '
443 printf " (main)" >expected &&
444 mkdir otherrepo/untracked-dir &&
445 test_when_finished "rm -rf otherrepo/untracked-dir" &&
447 GIT_PS1_SHOWUNTRACKEDFILES=y &&
451 test_cmp expected "$actual"
454 test_expect_success
'prompt - untracked files status indicator - non-empty untracked dir' '
455 printf " (main %%)" >expected &&
456 mkdir otherrepo/untracked-dir &&
457 test_when_finished "rm -rf otherrepo/untracked-dir" &&
458 >otherrepo/untracked-dir/untracked-file &&
460 GIT_PS1_SHOWUNTRACKEDFILES=y &&
464 test_cmp expected "$actual"
467 test_expect_success
'prompt - untracked files status indicator - untracked files outside cwd' '
468 printf " (main %%)" >expected &&
470 mkdir -p ignored_dir &&
472 GIT_PS1_SHOWUNTRACKEDFILES=y &&
475 test_cmp expected "$actual"
478 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config disabled' '
479 printf " (main)" >expected &&
480 test_config bash.showUntrackedFiles false &&
482 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
485 test_cmp expected "$actual"
488 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config enabled' '
489 printf " (main)" >expected &&
490 test_config bash.showUntrackedFiles true &&
492 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
495 test_cmp expected "$actual"
498 test_expect_success
'prompt - untracked files status indicator - shell variable set with config disabled' '
499 printf " (main)" >expected &&
500 test_config bash.showUntrackedFiles false &&
502 GIT_PS1_SHOWUNTRACKEDFILES=y &&
505 test_cmp expected "$actual"
508 test_expect_success
'prompt - untracked files status indicator - shell variable set with config enabled' '
509 printf " (main %%)" >expected &&
510 test_config bash.showUntrackedFiles true &&
512 GIT_PS1_SHOWUNTRACKEDFILES=y &&
515 test_cmp expected "$actual"
518 test_expect_success
'prompt - untracked files status indicator - not shown inside .git directory' '
519 printf " (GIT_DIR!)" >expected &&
521 GIT_PS1_SHOWUNTRACKEDFILES=y &&
525 test_cmp expected "$actual"
528 test_expect_success
'prompt - format string starting with dash' '
529 printf -- "-main" >expected &&
530 __git_ps1 "-%s" >"$actual" &&
531 test_cmp expected "$actual"
534 test_expect_success
'prompt - pc mode' '
535 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER\\nmain" >expected &&
537 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
538 test_must_be_empty "$actual" &&
539 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
541 test_cmp expected "$actual"
544 test_expect_success
'prompt - bash color pc mode - branch name' '
545 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nmain" >expected &&
547 GIT_PS1_SHOWCOLORHINTS=y &&
548 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
549 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
551 test_cmp expected "$actual"
554 test_expect_success
'prompt - bash color pc mode - detached head' '
555 printf "BEFORE: (${c_red}\${__git_ps1_branch_name}${c_clear}):AFTER\\n(%s...)" $(git log -1 --format="%h" b1^) >expected &&
557 test_when_finished "git checkout main" &&
559 GIT_PS1_SHOWCOLORHINTS=y &&
560 __git_ps1 "BEFORE:" ":AFTER" &&
561 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
563 test_cmp expected "$actual"
566 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
567 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}):AFTER\\nmain" >expected &&
568 echo "dirty" >file &&
569 test_when_finished "git reset --hard" &&
571 GIT_PS1_SHOWDIRTYSTATE=y &&
572 GIT_PS1_SHOWCOLORHINTS=y &&
573 __git_ps1 "BEFORE:" ":AFTER" &&
574 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
576 test_cmp expected "$actual"
579 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index' '
580 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}+${c_clear}):AFTER\\nmain" >expected &&
581 echo "dirty" >file &&
582 test_when_finished "git reset --hard" &&
585 GIT_PS1_SHOWDIRTYSTATE=y &&
586 GIT_PS1_SHOWCOLORHINTS=y &&
587 __git_ps1 "BEFORE:" ":AFTER" &&
588 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
590 test_cmp expected "$actual"
593 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
594 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}${c_green}+${c_clear}):AFTER\\nmain" >expected &&
595 echo "dirty index" >file &&
596 test_when_finished "git reset --hard" &&
598 echo "dirty worktree" >file &&
600 GIT_PS1_SHOWCOLORHINTS=y &&
601 GIT_PS1_SHOWDIRTYSTATE=y &&
602 __git_ps1 "BEFORE:" ":AFTER" &&
603 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
605 test_cmp expected "$actual"
608 test_expect_success
'prompt - bash color pc mode - dirty status indicator - before root commit' '
609 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}#${c_clear}):AFTER\\nmain" >expected &&
611 GIT_PS1_SHOWDIRTYSTATE=y &&
612 GIT_PS1_SHOWCOLORHINTS=y &&
614 __git_ps1 "BEFORE:" ":AFTER" &&
615 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
617 test_cmp expected "$actual"
620 test_expect_success
'prompt - bash color pc mode - inside .git directory' '
621 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nGIT_DIR!" >expected &&
622 echo "dirty" >file &&
623 test_when_finished "git reset --hard" &&
625 GIT_PS1_SHOWDIRTYSTATE=y &&
626 GIT_PS1_SHOWCOLORHINTS=y &&
628 __git_ps1 "BEFORE:" ":AFTER" &&
629 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
631 test_cmp expected "$actual"
634 test_expect_success
'prompt - bash color pc mode - stash status indicator' '
635 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_lblue}\$${c_clear}):AFTER\\nmain" >expected &&
638 test_when_finished "git stash drop" &&
640 GIT_PS1_SHOWSTASHSTATE=y &&
641 GIT_PS1_SHOWCOLORHINTS=y &&
642 __git_ps1 "BEFORE:" ":AFTER" &&
643 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
645 test_cmp expected "$actual"
648 test_expect_success
'prompt - bash color pc mode - untracked files status indicator' '
649 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}%%${c_clear}):AFTER\\nmain" >expected &&
651 GIT_PS1_SHOWUNTRACKEDFILES=y &&
652 GIT_PS1_SHOWCOLORHINTS=y &&
653 __git_ps1 "BEFORE:" ":AFTER" &&
654 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
656 test_cmp expected "$actual"
659 test_expect_success
'prompt - zsh color pc mode' '
660 printf "BEFORE: (%%F{green}main%%f):AFTER" >expected &&
663 GIT_PS1_SHOWCOLORHINTS=y &&
664 __git_ps1 "BEFORE:" ":AFTER" &&
665 printf "%s" "$PS1" >"$actual"
667 test_cmp expected "$actual"
670 test_expect_success
'prompt - hide if pwd ignored - env var unset, config disabled' '
671 printf " (main)" >expected &&
672 test_config bash.hideIfPwdIgnored false &&
677 test_cmp expected "$actual"
680 test_expect_success
'prompt - hide if pwd ignored - env var unset, config disabled, pc mode' '
681 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
682 test_config bash.hideIfPwdIgnored false &&
685 __git_ps1 "BEFORE:" ":AFTER" &&
686 printf "%s" "$PS1" >"$actual"
688 test_cmp expected "$actual"
691 test_expect_success
'prompt - hide if pwd ignored - env var unset, config unset' '
692 printf " (main)" >expected &&
697 test_cmp expected "$actual"
700 test_expect_success
'prompt - hide if pwd ignored - env var unset, config unset, pc mode' '
701 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
704 __git_ps1 "BEFORE:" ":AFTER" &&
705 printf "%s" "$PS1" >"$actual"
707 test_cmp expected "$actual"
710 test_expect_success
'prompt - hide if pwd ignored - env var set, config disabled' '
711 printf " (main)" >expected &&
712 test_config bash.hideIfPwdIgnored false &&
715 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
718 test_cmp expected "$actual"
721 test_expect_success
'prompt - hide if pwd ignored - env var set, config disabled, pc mode' '
722 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
723 test_config bash.hideIfPwdIgnored false &&
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 - env var set, config unset' '
736 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
739 test_must_be_empty "$actual"
742 test_expect_success
'prompt - hide if pwd ignored - env var set, config unset, pc mode' '
743 printf "BEFORE::AFTER" >expected &&
746 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
747 __git_ps1 "BEFORE:" ":AFTER" &&
748 printf "%s" "$PS1" >"$actual"
750 test_cmp expected "$actual"
753 test_expect_success
'prompt - hide if pwd ignored - inside gitdir' '
754 printf " (GIT_DIR!)" >expected &&
756 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
760 test_cmp expected "$actual"
763 test_expect_success
'prompt - conflict indicator' '
764 printf " (main|CONFLICT)" >expected &&
765 echo "stash" >file &&
767 test_when_finished "git stash drop" &&
768 echo "commit" >file &&
769 git commit -m "commit" file &&
770 test_when_finished "git reset --hard HEAD~" &&
771 test_must_fail git stash apply &&
773 GIT_PS1_SHOWCONFLICTSTATE="yes" &&
776 test_cmp expected "$actual"