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
13 .
"$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
15 actual
="$TRASH_DIRECTORY/actual"
17 c_green
='\\[\\e[32m\\]'
18 c_lblue
='\\[\\e[1;34m\\]'
19 c_clear
='\\[\\e[0m\\]'
21 test_expect_success
'setup for prompt tests' '
26 git commit -m initial &&
27 git tag -a -m msg1 t1 &&
30 git commit -m "second b1" file &&
32 git commit -m "third b1" file &&
33 git tag -a -m msg2 t2 &&
34 git checkout -b b2 main &&
36 git commit -m "second b2" file &&
38 git commit -m "another b2" file &&
40 git commit -m "yet another b2" file &&
42 echo "ignored_dir/" >>.gitignore &&
46 test_expect_success
'prompt - branch name' '
47 printf " (main)" >expected &&
48 __git_ps1 >"$actual" &&
49 test_cmp expected "$actual"
52 test_expect_success SYMLINKS
'prompt - branch name - symlink symref' '
53 printf " (main)" >expected &&
54 test_when_finished "git checkout main" &&
55 test_config core.preferSymlinkRefs true &&
57 __git_ps1 >"$actual" &&
58 test_cmp expected "$actual"
61 test_expect_success
'prompt - unborn branch' '
62 printf " (unborn)" >expected &&
63 git checkout --orphan unborn &&
64 test_when_finished "git checkout main" &&
65 __git_ps1 >"$actual" &&
66 test_cmp expected "$actual"
69 if test_have_prereq
!FUNNYNAMES
; then
70 say
'Your filesystem does not allow newlines in filenames.'
73 test_expect_success FUNNYNAMES
'prompt - with newline in path' '
74 repo_with_newline="repo
77 mkdir "$repo_with_newline" &&
78 printf " (main)" >expected &&
79 git init "$repo_with_newline" &&
80 test_when_finished "rm -rf \"$repo_with_newline\"" &&
81 mkdir "$repo_with_newline"/subdir &&
83 cd "$repo_with_newline/subdir" &&
86 test_cmp expected "$actual"
89 test_expect_success
'prompt - detached head' '
90 printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
91 test_config core.abbrev 13 &&
93 test_when_finished "git checkout main" &&
94 __git_ps1 >"$actual" &&
95 test_cmp expected "$actual"
98 test_expect_success
'prompt - describe detached head - contains' '
99 printf " ((t2~1))" >expected &&
101 test_when_finished "git checkout main" &&
103 GIT_PS1_DESCRIBE_STYLE=contains &&
106 test_cmp expected "$actual"
109 test_expect_success
'prompt - describe detached head - branch' '
110 printf " ((tags/t2~1))" >expected &&
112 test_when_finished "git checkout main" &&
114 GIT_PS1_DESCRIBE_STYLE=branch &&
117 test_cmp expected "$actual"
120 test_expect_success
'prompt - describe detached head - describe' '
121 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
123 test_when_finished "git checkout main" &&
125 GIT_PS1_DESCRIBE_STYLE=describe &&
128 test_cmp expected "$actual"
131 test_expect_success
'prompt - describe detached head - default' '
132 printf " ((t2))" >expected &&
133 git checkout --detach b1 &&
134 test_when_finished "git checkout main" &&
135 __git_ps1 >"$actual" &&
136 test_cmp expected "$actual"
139 test_expect_success
'prompt - inside .git directory' '
140 printf " (GIT_DIR!)" >expected &&
145 test_cmp expected "$actual"
148 test_expect_success
'prompt - deep inside .git directory' '
149 printf " (GIT_DIR!)" >expected &&
154 test_cmp expected "$actual"
157 test_expect_success
'prompt - inside bare repository' '
158 printf " (BARE:main)" >expected &&
159 git init --bare bare.git &&
160 test_when_finished "rm -rf bare.git" &&
165 test_cmp expected "$actual"
168 test_expect_success
'prompt - interactive rebase' '
169 printf " (b1|REBASE 2/3)" >expected &&
170 write_script fake_editor.sh <<-\EOF &&
171 echo "exec echo" >"$1"
172 echo "edit $(git log -1 --format="%h")" >>"$1"
173 echo "exec echo" >>"$1"
175 test_when_finished "rm -f fake_editor.sh" &&
176 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
178 test_when_finished "git checkout main" &&
179 git rebase -i HEAD^ &&
180 test_when_finished "git rebase --abort" &&
181 __git_ps1 >"$actual" &&
182 test_cmp expected "$actual"
185 test_expect_success
'prompt - rebase merge' '
186 printf " (b2|REBASE 1/3)" >expected &&
188 test_when_finished "git checkout main" &&
189 test_must_fail git rebase --merge b1 b2 &&
190 test_when_finished "git rebase --abort" &&
191 __git_ps1 >"$actual" &&
192 test_cmp expected "$actual"
195 test_expect_success
'prompt - rebase am' '
196 printf " (b2|REBASE 1/3)" >expected &&
198 test_when_finished "git checkout main" &&
199 test_must_fail git rebase --apply b1 b2 &&
200 test_when_finished "git rebase --abort" &&
201 __git_ps1 >"$actual" &&
202 test_cmp expected "$actual"
205 test_expect_success
'prompt - merge' '
206 printf " (b1|MERGING)" >expected &&
208 test_when_finished "git checkout main" &&
209 test_must_fail git merge b2 &&
210 test_when_finished "git reset --hard" &&
211 __git_ps1 >"$actual" &&
212 test_cmp expected "$actual"
215 test_expect_success
'prompt - cherry-pick' '
216 printf " (main|CHERRY-PICKING)" >expected &&
217 test_must_fail git cherry-pick b1 b1^ &&
218 test_when_finished "git cherry-pick --abort" &&
219 __git_ps1 >"$actual" &&
220 test_cmp expected "$actual" &&
222 test_must_fail git rev-parse CHERRY_PICK_HEAD &&
223 __git_ps1 >"$actual" &&
224 test_cmp expected "$actual"
227 test_expect_success
'prompt - revert' '
228 printf " (main|REVERTING)" >expected &&
229 test_must_fail git revert b1^ b1 &&
230 test_when_finished "git revert --abort" &&
231 __git_ps1 >"$actual" &&
232 test_cmp expected "$actual" &&
234 test_must_fail git rev-parse REVERT_HEAD &&
235 __git_ps1 >"$actual" &&
236 test_cmp expected "$actual"
239 test_expect_success
'prompt - bisect' '
240 printf " (main|BISECTING)" >expected &&
242 test_when_finished "git bisect reset" &&
243 __git_ps1 >"$actual" &&
244 test_cmp expected "$actual"
247 test_expect_success
'prompt - dirty status indicator - clean' '
248 printf " (main)" >expected &&
250 GIT_PS1_SHOWDIRTYSTATE=y &&
253 test_cmp expected "$actual"
256 test_expect_success
'prompt - dirty status indicator - dirty worktree' '
257 printf " (main *)" >expected &&
258 echo "dirty" >file &&
259 test_when_finished "git reset --hard" &&
261 GIT_PS1_SHOWDIRTYSTATE=y &&
264 test_cmp expected "$actual"
267 test_expect_success
'prompt - dirty status indicator - dirty index' '
268 printf " (main +)" >expected &&
269 echo "dirty" >file &&
270 test_when_finished "git reset --hard" &&
273 GIT_PS1_SHOWDIRTYSTATE=y &&
276 test_cmp expected "$actual"
279 test_expect_success
'prompt - dirty status indicator - dirty index and worktree' '
280 printf " (main *+)" >expected &&
281 echo "dirty index" >file &&
282 test_when_finished "git reset --hard" &&
284 echo "dirty worktree" >file &&
286 GIT_PS1_SHOWDIRTYSTATE=y &&
289 test_cmp expected "$actual"
292 test_expect_success
'prompt - dirty status indicator - orphan branch - clean' '
293 printf " (orphan #)" >expected &&
294 test_when_finished "git checkout main" &&
295 git checkout --orphan orphan &&
298 GIT_PS1_SHOWDIRTYSTATE=y &&
301 test_cmp expected "$actual"
304 test_expect_success
'prompt - dirty status indicator - orphan branch - dirty index' '
305 printf " (orphan +)" >expected &&
306 test_when_finished "git checkout main" &&
307 git checkout --orphan orphan &&
309 GIT_PS1_SHOWDIRTYSTATE=y &&
312 test_cmp expected "$actual"
315 test_expect_success
'prompt - dirty status indicator - orphan branch - dirty index and worktree' '
316 printf " (orphan *+)" >expected &&
317 test_when_finished "git checkout main" &&
318 git checkout --orphan orphan &&
321 GIT_PS1_SHOWDIRTYSTATE=y &&
324 test_cmp expected "$actual"
327 test_expect_success
'prompt - dirty status indicator - shell variable unset with config disabled' '
328 printf " (main)" >expected &&
329 echo "dirty" >file &&
330 test_when_finished "git reset --hard" &&
331 test_config bash.showDirtyState false &&
333 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
336 test_cmp expected "$actual"
339 test_expect_success
'prompt - dirty status indicator - shell variable unset with config enabled' '
340 printf " (main)" >expected &&
341 echo "dirty" >file &&
342 test_when_finished "git reset --hard" &&
343 test_config bash.showDirtyState true &&
345 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
348 test_cmp expected "$actual"
351 test_expect_success
'prompt - dirty status indicator - shell variable set with config disabled' '
352 printf " (main)" >expected &&
353 echo "dirty" >file &&
354 test_when_finished "git reset --hard" &&
355 test_config bash.showDirtyState false &&
357 GIT_PS1_SHOWDIRTYSTATE=y &&
360 test_cmp expected "$actual"
363 test_expect_success
'prompt - dirty status indicator - shell variable set with config enabled' '
364 printf " (main *)" >expected &&
365 echo "dirty" >file &&
366 test_when_finished "git reset --hard" &&
367 test_config bash.showDirtyState true &&
369 GIT_PS1_SHOWDIRTYSTATE=y &&
372 test_cmp expected "$actual"
375 test_expect_success
'prompt - dirty status indicator - not shown inside .git directory' '
376 printf " (GIT_DIR!)" >expected &&
377 echo "dirty" >file &&
378 test_when_finished "git reset --hard" &&
380 GIT_PS1_SHOWDIRTYSTATE=y &&
384 test_cmp expected "$actual"
387 test_expect_success
'prompt - stash status indicator - no stash' '
388 printf " (main)" >expected &&
390 GIT_PS1_SHOWSTASHSTATE=y &&
393 test_cmp expected "$actual"
396 test_expect_success
'prompt - stash status indicator - stash' '
397 printf " (main $)" >expected &&
400 test_when_finished "git stash drop" &&
401 git pack-refs --all &&
403 GIT_PS1_SHOWSTASHSTATE=y &&
406 test_cmp expected "$actual"
409 test_expect_success
'prompt - stash status indicator - not shown inside .git directory' '
410 printf " (GIT_DIR!)" >expected &&
413 test_when_finished "git stash drop" &&
415 GIT_PS1_SHOWSTASHSTATE=y &&
419 test_cmp expected "$actual"
422 test_expect_success
'prompt - untracked files status indicator - no untracked files' '
423 printf " (main)" >expected &&
425 GIT_PS1_SHOWUNTRACKEDFILES=y &&
429 test_cmp expected "$actual"
432 test_expect_success
'prompt - untracked files status indicator - untracked files' '
433 printf " (main %%)" >expected &&
435 GIT_PS1_SHOWUNTRACKEDFILES=y &&
438 test_cmp expected "$actual"
441 test_expect_success
'prompt - untracked files status indicator - empty untracked dir' '
442 printf " (main)" >expected &&
443 mkdir otherrepo/untracked-dir &&
444 test_when_finished "rm -rf otherrepo/untracked-dir" &&
446 GIT_PS1_SHOWUNTRACKEDFILES=y &&
450 test_cmp expected "$actual"
453 test_expect_success
'prompt - untracked files status indicator - non-empty untracked dir' '
454 printf " (main %%)" >expected &&
455 mkdir otherrepo/untracked-dir &&
456 test_when_finished "rm -rf otherrepo/untracked-dir" &&
457 >otherrepo/untracked-dir/untracked-file &&
459 GIT_PS1_SHOWUNTRACKEDFILES=y &&
463 test_cmp expected "$actual"
466 test_expect_success
'prompt - untracked files status indicator - untracked files outside cwd' '
467 printf " (main %%)" >expected &&
469 mkdir -p ignored_dir &&
471 GIT_PS1_SHOWUNTRACKEDFILES=y &&
474 test_cmp expected "$actual"
477 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config disabled' '
478 printf " (main)" >expected &&
479 test_config bash.showUntrackedFiles false &&
481 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
484 test_cmp expected "$actual"
487 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config enabled' '
488 printf " (main)" >expected &&
489 test_config bash.showUntrackedFiles true &&
491 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
494 test_cmp expected "$actual"
497 test_expect_success
'prompt - untracked files status indicator - shell variable set with config disabled' '
498 printf " (main)" >expected &&
499 test_config bash.showUntrackedFiles false &&
501 GIT_PS1_SHOWUNTRACKEDFILES=y &&
504 test_cmp expected "$actual"
507 test_expect_success
'prompt - untracked files status indicator - shell variable set with config enabled' '
508 printf " (main %%)" >expected &&
509 test_config bash.showUntrackedFiles true &&
511 GIT_PS1_SHOWUNTRACKEDFILES=y &&
514 test_cmp expected "$actual"
517 test_expect_success
'prompt - untracked files status indicator - not shown inside .git directory' '
518 printf " (GIT_DIR!)" >expected &&
520 GIT_PS1_SHOWUNTRACKEDFILES=y &&
524 test_cmp expected "$actual"
527 test_expect_success
'prompt - format string starting with dash' '
528 printf -- "-main" >expected &&
529 __git_ps1 "-%s" >"$actual" &&
530 test_cmp expected "$actual"
533 test_expect_success
'prompt - pc mode' '
534 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER\\nmain" >expected &&
536 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
537 test_must_be_empty "$actual" &&
538 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
540 test_cmp expected "$actual"
543 test_expect_success
'prompt - bash color pc mode - branch name' '
544 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nmain" >expected &&
546 GIT_PS1_SHOWCOLORHINTS=y &&
547 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
548 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
550 test_cmp expected "$actual"
553 test_expect_success
'prompt - bash color pc mode - detached head' '
554 printf "BEFORE: (${c_red}\${__git_ps1_branch_name}${c_clear}):AFTER\\n(%s...)" $(git log -1 --format="%h" b1^) >expected &&
556 test_when_finished "git checkout main" &&
558 GIT_PS1_SHOWCOLORHINTS=y &&
559 __git_ps1 "BEFORE:" ":AFTER" &&
560 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
562 test_cmp expected "$actual"
565 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
566 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}):AFTER\\nmain" >expected &&
567 echo "dirty" >file &&
568 test_when_finished "git reset --hard" &&
570 GIT_PS1_SHOWDIRTYSTATE=y &&
571 GIT_PS1_SHOWCOLORHINTS=y &&
572 __git_ps1 "BEFORE:" ":AFTER" &&
573 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
575 test_cmp expected "$actual"
578 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index' '
579 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}+${c_clear}):AFTER\\nmain" >expected &&
580 echo "dirty" >file &&
581 test_when_finished "git reset --hard" &&
584 GIT_PS1_SHOWDIRTYSTATE=y &&
585 GIT_PS1_SHOWCOLORHINTS=y &&
586 __git_ps1 "BEFORE:" ":AFTER" &&
587 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
589 test_cmp expected "$actual"
592 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
593 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}*${c_clear}${c_green}+${c_clear}):AFTER\\nmain" >expected &&
594 echo "dirty index" >file &&
595 test_when_finished "git reset --hard" &&
597 echo "dirty worktree" >file &&
599 GIT_PS1_SHOWCOLORHINTS=y &&
600 GIT_PS1_SHOWDIRTYSTATE=y &&
601 __git_ps1 "BEFORE:" ":AFTER" &&
602 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
604 test_cmp expected "$actual"
607 test_expect_success
'prompt - bash color pc mode - dirty status indicator - before root commit' '
608 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_green}#${c_clear}):AFTER\\nmain" >expected &&
610 GIT_PS1_SHOWDIRTYSTATE=y &&
611 GIT_PS1_SHOWCOLORHINTS=y &&
613 __git_ps1 "BEFORE:" ":AFTER" &&
614 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
616 test_cmp expected "$actual"
619 test_expect_success
'prompt - bash color pc mode - inside .git directory' '
620 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear}):AFTER\\nGIT_DIR!" >expected &&
621 echo "dirty" >file &&
622 test_when_finished "git reset --hard" &&
624 GIT_PS1_SHOWDIRTYSTATE=y &&
625 GIT_PS1_SHOWCOLORHINTS=y &&
627 __git_ps1 "BEFORE:" ":AFTER" &&
628 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
630 test_cmp expected "$actual"
633 test_expect_success
'prompt - bash color pc mode - stash status indicator' '
634 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_lblue}\$${c_clear}):AFTER\\nmain" >expected &&
637 test_when_finished "git stash drop" &&
639 GIT_PS1_SHOWSTASHSTATE=y &&
640 GIT_PS1_SHOWCOLORHINTS=y &&
641 __git_ps1 "BEFORE:" ":AFTER" &&
642 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
644 test_cmp expected "$actual"
647 test_expect_success
'prompt - bash color pc mode - untracked files status indicator' '
648 printf "BEFORE: (${c_green}\${__git_ps1_branch_name}${c_clear} ${c_red}%%${c_clear}):AFTER\\nmain" >expected &&
650 GIT_PS1_SHOWUNTRACKEDFILES=y &&
651 GIT_PS1_SHOWCOLORHINTS=y &&
652 __git_ps1 "BEFORE:" ":AFTER" &&
653 printf "%s\\n%s" "$PS1" "${__git_ps1_branch_name}" >"$actual"
655 test_cmp expected "$actual"
658 test_expect_success
'prompt - zsh color pc mode' '
659 printf "BEFORE: (%%F{green}main%%f):AFTER" >expected &&
662 GIT_PS1_SHOWCOLORHINTS=y &&
663 __git_ps1 "BEFORE:" ":AFTER" &&
664 printf "%s" "$PS1" >"$actual"
666 test_cmp expected "$actual"
669 test_expect_success
'prompt - hide if pwd ignored - env var unset, config disabled' '
670 printf " (main)" >expected &&
671 test_config bash.hideIfPwdIgnored false &&
676 test_cmp expected "$actual"
679 test_expect_success
'prompt - hide if pwd ignored - env var unset, config disabled, pc mode' '
680 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
681 test_config bash.hideIfPwdIgnored false &&
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 unset, config unset' '
691 printf " (main)" >expected &&
696 test_cmp expected "$actual"
699 test_expect_success
'prompt - hide if pwd ignored - env var unset, config unset, pc mode' '
700 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
703 __git_ps1 "BEFORE:" ":AFTER" &&
704 printf "%s" "$PS1" >"$actual"
706 test_cmp expected "$actual"
709 test_expect_success
'prompt - hide if pwd ignored - env var set, config disabled' '
710 printf " (main)" >expected &&
711 test_config bash.hideIfPwdIgnored false &&
714 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
717 test_cmp expected "$actual"
720 test_expect_success
'prompt - hide if pwd ignored - env var set, config disabled, pc mode' '
721 printf "BEFORE: (\${__git_ps1_branch_name}):AFTER" >expected &&
722 test_config bash.hideIfPwdIgnored false &&
725 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
726 __git_ps1 "BEFORE:" ":AFTER" &&
727 printf "%s" "$PS1" >"$actual"
729 test_cmp expected "$actual"
732 test_expect_success
'prompt - hide if pwd ignored - env var set, config unset' '
735 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
738 test_must_be_empty "$actual"
741 test_expect_success
'prompt - hide if pwd ignored - env var set, config unset, pc mode' '
742 printf "BEFORE::AFTER" >expected &&
745 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
746 __git_ps1 "BEFORE:" ":AFTER" &&
747 printf "%s" "$PS1" >"$actual"
749 test_cmp expected "$actual"
752 test_expect_success
'prompt - hide if pwd ignored - inside gitdir' '
753 printf " (GIT_DIR!)" >expected &&
755 GIT_PS1_HIDE_IF_PWD_IGNORED=y &&
759 test_cmp expected "$actual"