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' '
19 mkdir -p subdir/subsubdir &&
24 git commit -m initial &&
25 git tag -a -m msg1 t1 &&
28 git commit -m "second b1" file &&
30 git commit -m "third b1" file &&
31 git tag -a -m msg2 t2 &&
32 git checkout -b b2 master &&
34 git commit -m "second b2" file &&
36 git commit -m "another b2" file &&
38 git commit -m "yet another b2" file &&
42 test_expect_success
'gitdir - from command line (through $__git_dir)' '
43 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
45 __git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
48 test_cmp expected "$actual"
51 test_expect_success
'gitdir - repo as argument' '
52 echo "otherrepo/.git" > expected &&
53 __gitdir "otherrepo" > "$actual" &&
54 test_cmp expected "$actual"
57 test_expect_success
'gitdir - remote as argument' '
58 echo "remote" > expected &&
59 __gitdir "remote" > "$actual" &&
60 test_cmp expected "$actual"
63 test_expect_success
'gitdir - .git directory in cwd' '
64 echo ".git" > expected &&
65 __gitdir > "$actual" &&
66 test_cmp expected "$actual"
69 test_expect_success
'gitdir - .git directory in parent' '
70 echo "$(pwd -P)/.git" > expected &&
72 cd subdir/subsubdir &&
75 test_cmp expected "$actual"
78 test_expect_success
'gitdir - cwd is a .git directory' '
79 echo "." > expected &&
84 test_cmp expected "$actual"
87 test_expect_success
'gitdir - parent is a .git directory' '
88 echo "$(pwd -P)/.git" > expected &&
93 test_cmp expected "$actual"
96 test_expect_success
'gitdir - $GIT_DIR set while .git directory in cwd' '
97 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
99 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
103 test_cmp expected "$actual"
106 test_expect_success
'gitdir - $GIT_DIR set while .git directory in parent' '
107 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
109 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
114 test_cmp expected "$actual"
117 test_expect_success
'gitdir - non-existing $GIT_DIR' '
119 GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
121 test_must_fail __gitdir
125 test_expect_success
'gitdir - gitfile in cwd' '
126 echo "$(pwd -P)/otherrepo/.git" > expected &&
127 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
128 test_when_finished "rm -f subdir/.git" &&
133 test_cmp expected "$actual"
136 test_expect_success
'gitdir - gitfile in parent' '
137 echo "$(pwd -P)/otherrepo/.git" > expected &&
138 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
139 test_when_finished "rm -f subdir/.git" &&
141 cd subdir/subsubdir &&
144 test_cmp expected "$actual"
147 test_expect_success SYMLINKS
'gitdir - resulting path avoids symlinks' '
148 echo "$(pwd -P)/otherrepo/.git" > expected &&
149 mkdir otherrepo/dir &&
150 test_when_finished "rm -rf otherrepo/dir" &&
151 ln -s otherrepo/dir link &&
152 test_when_finished "rm -f link" &&
157 test_cmp expected "$actual"
160 test_expect_success
'gitdir - not a git repository' '
162 cd subdir/subsubdir &&
163 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
164 export GIT_CEILING_DIRECTORIES &&
165 test_must_fail __gitdir
169 test_expect_success
'prompt - branch name' '
170 printf " (master)" > expected &&
171 __git_ps1 > "$actual" &&
172 test_cmp expected "$actual"
175 test_expect_success
'prompt - detached head' '
176 printf " ((%s...))" $(git log -1 --format="%h" b1^) > expected &&
178 test_when_finished "git checkout master" &&
179 __git_ps1 > "$actual" &&
180 test_cmp expected "$actual"
183 test_expect_success
'prompt - describe detached head - contains' '
184 printf " ((t2~1))" > expected &&
186 test_when_finished "git checkout master" &&
188 GIT_PS1_DESCRIBE_STYLE=contains &&
189 __git_ps1 > "$actual"
191 test_cmp expected "$actual"
194 test_expect_success
'prompt - describe detached head - branch' '
195 printf " ((b1~1))" > expected &&
197 test_when_finished "git checkout master" &&
199 GIT_PS1_DESCRIBE_STYLE=branch &&
200 __git_ps1 > "$actual"
202 test_cmp expected "$actual"
205 test_expect_success
'prompt - describe detached head - describe' '
206 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) > expected &&
208 test_when_finished "git checkout master" &&
210 GIT_PS1_DESCRIBE_STYLE=describe &&
211 __git_ps1 > "$actual"
213 test_cmp expected "$actual"
216 test_expect_success
'prompt - describe detached head - default' '
217 printf " ((t2))" > expected &&
218 git checkout --detach b1 &&
219 test_when_finished "git checkout master" &&
220 __git_ps1 > "$actual" &&
221 test_cmp expected "$actual"
224 test_expect_success
'prompt - inside .git directory' '
225 printf " (GIT_DIR!)" > expected &&
228 __git_ps1 > "$actual"
230 test_cmp expected "$actual"
233 test_expect_success
'prompt - deep inside .git directory' '
234 printf " (GIT_DIR!)" > expected &&
236 cd .git/refs/heads &&
237 __git_ps1 > "$actual"
239 test_cmp expected "$actual"
242 test_expect_success
'prompt - inside bare repository' '
243 printf " (BARE:master)" > expected &&
244 git init --bare bare.git &&
245 test_when_finished "rm -rf bare.git" &&
248 __git_ps1 > "$actual"
250 test_cmp expected "$actual"
253 test_expect_success
'prompt - interactive rebase' '
254 printf " (b1|REBASE-i 2/3)" > expected
255 echo "#!$SHELL_PATH" >fake_editor.sh &&
256 cat >>fake_editor.sh <<\EOF &&
257 echo "exec echo" > "$1"
258 echo "edit $(git log -1 --format="%h")" >> "$1"
259 echo "exec echo" >> "$1"
261 test_when_finished "rm -f fake_editor.sh" &&
262 chmod a+x fake_editor.sh &&
263 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
265 test_when_finished "git checkout master" &&
266 git rebase -i HEAD^ &&
267 test_when_finished "git rebase --abort"
268 __git_ps1 > "$actual" &&
269 test_cmp expected "$actual"
272 test_expect_success
'prompt - rebase merge' '
273 printf " (b2|REBASE-m 1/3)" > expected &&
275 test_when_finished "git checkout master" &&
276 test_must_fail git rebase --merge b1 b2 &&
277 test_when_finished "git rebase --abort" &&
278 __git_ps1 > "$actual" &&
279 test_cmp expected "$actual"
282 test_expect_success
'prompt - rebase' '
283 printf " (b2|REBASE 1/3)" > expected &&
285 test_when_finished "git checkout master" &&
286 test_must_fail git rebase b1 b2 &&
287 test_when_finished "git rebase --abort" &&
288 __git_ps1 > "$actual" &&
289 test_cmp expected "$actual"
292 test_expect_success
'prompt - merge' '
293 printf " (b1|MERGING)" > expected &&
295 test_when_finished "git checkout master" &&
296 test_must_fail git merge b2 &&
297 test_when_finished "git reset --hard" &&
298 __git_ps1 > "$actual" &&
299 test_cmp expected "$actual"
302 test_expect_success
'prompt - cherry-pick' '
303 printf " (master|CHERRY-PICKING)" > expected &&
304 test_must_fail git cherry-pick b1 &&
305 test_when_finished "git reset --hard" &&
306 __git_ps1 > "$actual" &&
307 test_cmp expected "$actual"
310 test_expect_success
'prompt - bisect' '
311 printf " (master|BISECTING)" > expected &&
313 test_when_finished "git bisect reset" &&
314 __git_ps1 > "$actual" &&
315 test_cmp expected "$actual"
318 test_expect_success
'prompt - dirty status indicator - clean' '
319 printf " (master)" > expected &&
321 GIT_PS1_SHOWDIRTYSTATE=y &&
322 __git_ps1 > "$actual"
324 test_cmp expected "$actual"
327 test_expect_success
'prompt - dirty status indicator - dirty worktree' '
328 printf " (master *)" > expected &&
329 echo "dirty" > file &&
330 test_when_finished "git reset --hard" &&
332 GIT_PS1_SHOWDIRTYSTATE=y &&
333 __git_ps1 > "$actual"
335 test_cmp expected "$actual"
338 test_expect_success
'prompt - dirty status indicator - dirty index' '
339 printf " (master +)" > expected &&
340 echo "dirty" > file &&
341 test_when_finished "git reset --hard" &&
344 GIT_PS1_SHOWDIRTYSTATE=y &&
345 __git_ps1 > "$actual"
347 test_cmp expected "$actual"
350 test_expect_success
'prompt - dirty status indicator - dirty index and worktree' '
351 printf " (master *+)" > expected &&
352 echo "dirty index" > file &&
353 test_when_finished "git reset --hard" &&
355 echo "dirty worktree" > file &&
357 GIT_PS1_SHOWDIRTYSTATE=y &&
358 __git_ps1 > "$actual"
360 test_cmp expected "$actual"
363 test_expect_success
'prompt - dirty status indicator - before root commit' '
364 printf " (master #)" > expected &&
366 GIT_PS1_SHOWDIRTYSTATE=y &&
368 __git_ps1 > "$actual"
370 test_cmp expected "$actual"
373 test_expect_success
'prompt - dirty status indicator - shell variable unset with config disabled' '
374 printf " (master)" > expected &&
375 echo "dirty" > file &&
376 test_when_finished "git reset --hard" &&
377 test_config bash.showDirtyState false &&
379 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
380 __git_ps1 > "$actual"
382 test_cmp expected "$actual"
385 test_expect_success
'prompt - dirty status indicator - shell variable unset with config enabled' '
386 printf " (master)" > expected &&
387 echo "dirty" > file &&
388 test_when_finished "git reset --hard" &&
389 test_config bash.showDirtyState true &&
391 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
392 __git_ps1 > "$actual"
394 test_cmp expected "$actual"
397 test_expect_success
'prompt - dirty status indicator - shell variable set with config disabled' '
398 printf " (master)" > expected &&
399 echo "dirty" > file &&
400 test_when_finished "git reset --hard" &&
401 test_config bash.showDirtyState false &&
403 GIT_PS1_SHOWDIRTYSTATE=y &&
404 __git_ps1 > "$actual"
406 test_cmp expected "$actual"
409 test_expect_success
'prompt - dirty status indicator - shell variable set with config enabled' '
410 printf " (master *)" > expected &&
411 echo "dirty" > file &&
412 test_when_finished "git reset --hard" &&
413 test_config bash.showDirtyState true &&
415 GIT_PS1_SHOWDIRTYSTATE=y &&
416 __git_ps1 > "$actual"
418 test_cmp expected "$actual"
421 test_expect_success
'prompt - dirty status indicator - not shown inside .git directory' '
422 printf " (GIT_DIR!)" > expected &&
423 echo "dirty" > file &&
424 test_when_finished "git reset --hard" &&
426 GIT_PS1_SHOWDIRTYSTATE=y &&
428 __git_ps1 > "$actual"
430 test_cmp expected "$actual"
433 test_expect_success
'prompt - stash status indicator - no stash' '
434 printf " (master)" > expected &&
436 GIT_PS1_SHOWSTASHSTATE=y &&
437 __git_ps1 > "$actual"
439 test_cmp expected "$actual"
442 test_expect_success
'prompt - stash status indicator - stash' '
443 printf " (master $)" > expected &&
446 test_when_finished "git stash drop" &&
448 GIT_PS1_SHOWSTASHSTATE=y &&
449 __git_ps1 > "$actual"
451 test_cmp expected "$actual"
454 test_expect_success
'prompt - stash status indicator - not shown inside .git directory' '
455 printf " (GIT_DIR!)" > expected &&
458 test_when_finished "git stash drop" &&
460 GIT_PS1_SHOWSTASHSTATE=y &&
462 __git_ps1 > "$actual"
464 test_cmp expected "$actual"
467 test_expect_success
'prompt - untracked files status indicator - no untracked files' '
468 printf " (master)" > expected &&
470 GIT_PS1_SHOWUNTRACKEDFILES=y &&
472 __git_ps1 > "$actual"
474 test_cmp expected "$actual"
477 test_expect_success
'prompt - untracked files status indicator - untracked files' '
478 printf " (master %%)" > expected &&
480 GIT_PS1_SHOWUNTRACKEDFILES=y &&
481 __git_ps1 > "$actual"
483 test_cmp expected "$actual"
486 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config disabled' '
487 printf " (master)" > expected &&
488 test_config bash.showUntrackedFiles false &&
490 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
491 __git_ps1 > "$actual"
493 test_cmp expected "$actual"
496 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config enabled' '
497 printf " (master)" > expected &&
498 test_config bash.showUntrackedFiles true &&
500 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
501 __git_ps1 > "$actual"
503 test_cmp expected "$actual"
506 test_expect_success
'prompt - untracked files status indicator - shell variable set with config disabled' '
507 printf " (master)" > expected &&
508 test_config bash.showUntrackedFiles false &&
510 GIT_PS1_SHOWUNTRACKEDFILES=y &&
511 __git_ps1 > "$actual"
513 test_cmp expected "$actual"
516 test_expect_success
'prompt - untracked files status indicator - shell variable set with config enabled' '
517 printf " (master %%)" > expected &&
518 test_config bash.showUntrackedFiles true &&
520 GIT_PS1_SHOWUNTRACKEDFILES=y &&
521 __git_ps1 > "$actual"
523 test_cmp expected "$actual"
526 test_expect_success
'prompt - untracked files status indicator - not shown inside .git directory' '
527 printf " (GIT_DIR!)" > expected &&
529 GIT_PS1_SHOWUNTRACKEDFILES=y &&
531 __git_ps1 > "$actual"
533 test_cmp expected "$actual"
536 test_expect_success
'prompt - format string starting with dash' '
537 printf -- "-master" > expected &&
538 __git_ps1 "-%s" > "$actual" &&
539 test_cmp expected "$actual"
542 test_expect_success
'prompt - pc mode' '
543 printf "BEFORE: (master):AFTER" >expected &&
544 printf "" >expected_output &&
546 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
547 test_cmp expected_output "$actual" &&
548 printf "%s" "$PS1" >"$actual"
550 test_cmp expected "$actual"
553 test_expect_success
'prompt - bash color pc mode - branch name' '
554 printf "BEFORE: (${c_green}master${c_clear}):AFTER" >expected &&
556 GIT_PS1_SHOWCOLORHINTS=y &&
557 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
558 printf "%s" "$PS1" >"$actual"
560 test_cmp expected "$actual"
563 test_expect_success
'prompt - bash color pc mode - detached head' '
564 printf "BEFORE: (${c_red}(%s...)${c_clear}):AFTER" $(git log -1 --format="%h" b1^) >expected &&
566 test_when_finished "git checkout master" &&
568 GIT_PS1_SHOWCOLORHINTS=y &&
569 __git_ps1 "BEFORE:" ":AFTER" &&
570 printf "%s" "$PS1" >"$actual"
572 test_cmp expected "$actual"
575 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
576 printf "BEFORE: (${c_green}master${c_clear} ${c_red}*${c_clear}):AFTER" >expected &&
577 echo "dirty" >file &&
578 test_when_finished "git reset --hard" &&
580 GIT_PS1_SHOWDIRTYSTATE=y &&
581 GIT_PS1_SHOWCOLORHINTS=y &&
582 __git_ps1 "BEFORE:" ":AFTER" &&
583 printf "%s" "$PS1" >"$actual"
585 test_cmp expected "$actual"
588 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index' '
589 printf "BEFORE: (${c_green}master${c_clear} ${c_green}+${c_clear}):AFTER" >expected &&
590 echo "dirty" >file &&
591 test_when_finished "git reset --hard" &&
594 GIT_PS1_SHOWDIRTYSTATE=y &&
595 GIT_PS1_SHOWCOLORHINTS=y &&
596 __git_ps1 "BEFORE:" ":AFTER" &&
597 printf "%s" "$PS1" >"$actual"
599 test_cmp expected "$actual"
602 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
603 printf "BEFORE: (${c_green}master${c_clear} ${c_red}*${c_green}+${c_clear}):AFTER" >expected &&
604 echo "dirty index" >file &&
605 test_when_finished "git reset --hard" &&
607 echo "dirty worktree" >file &&
609 GIT_PS1_SHOWCOLORHINTS=y &&
610 GIT_PS1_SHOWDIRTYSTATE=y &&
611 __git_ps1 "BEFORE:" ":AFTER" &&
612 printf "%s" "$PS1" >"$actual"
614 test_cmp expected "$actual"
617 test_expect_success
'prompt - bash color pc mode - dirty status indicator - before root commit' '
618 printf "BEFORE: (${c_green}master${c_clear} ${c_green}#${c_clear}):AFTER" >expected &&
620 GIT_PS1_SHOWDIRTYSTATE=y &&
621 GIT_PS1_SHOWCOLORHINTS=y &&
623 __git_ps1 "BEFORE:" ":AFTER" &&
624 printf "%s" "$PS1" >"$actual"
626 test_cmp expected "$actual"
629 test_expect_success
'prompt - bash color pc mode - inside .git directory' '
630 printf "BEFORE: (${c_green}GIT_DIR!${c_clear}):AFTER" >expected &&
631 echo "dirty" >file &&
632 test_when_finished "git reset --hard" &&
634 GIT_PS1_SHOWDIRTYSTATE=y &&
635 GIT_PS1_SHOWCOLORHINTS=y &&
637 __git_ps1 "BEFORE:" ":AFTER" &&
638 printf "%s" "$PS1" >"$actual"
640 test_cmp expected "$actual"
643 test_expect_success
'prompt - bash color pc mode - stash status indicator' '
644 printf "BEFORE: (${c_green}master${c_clear} ${c_lblue}\$${c_clear}):AFTER" >expected &&
647 test_when_finished "git stash drop" &&
649 GIT_PS1_SHOWSTASHSTATE=y &&
650 GIT_PS1_SHOWCOLORHINTS=y &&
651 __git_ps1 "BEFORE:" ":AFTER" &&
652 printf "%s" "$PS1" >"$actual"
654 test_cmp expected "$actual"
657 test_expect_success
'prompt - bash color pc mode - untracked files status indicator' '
658 printf "BEFORE: (${c_green}master${c_clear} ${c_red}%%${c_clear}):AFTER" >expected &&
660 GIT_PS1_SHOWUNTRACKEDFILES=y &&
661 GIT_PS1_SHOWCOLORHINTS=y &&
662 __git_ps1 "BEFORE:" ":AFTER" &&
663 printf "%s" "$PS1" >"$actual"
665 test_cmp expected "$actual"
668 test_expect_success
'prompt - zsh color pc mode' '
669 printf "BEFORE: (%%F{green}master%%f):AFTER" >expected &&
672 GIT_PS1_SHOWCOLORHINTS=y &&
673 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
674 printf "%s" "$PS1" >"$actual"
676 test_cmp expected "$actual"