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 &&
41 test_expect_success 'prompt - branch name' '
42 printf " (master)" >expected &&
43 __git_ps1 >"$actual" &&
44 test_cmp expected "$actual"
47 test_expect_success SYMLINKS 'prompt - branch name - symlink symref' '
48 printf " (master)" >expected &&
49 test_when_finished "git checkout master" &&
50 test_config core.preferSymlinkRefs true &&
51 git checkout master &&
52 __git_ps1 >"$actual" &&
53 test_cmp expected "$actual"
56 test_expect_success 'prompt - unborn branch' '
57 printf " (unborn)" >expected &&
58 git checkout --orphan unborn &&
59 test_when_finished "git checkout master" &&
60 __git_ps1 >"$actual" &&
61 test_cmp expected "$actual"
64 repo_with_newline='repo
68 if mkdir "$repo_with_newline" 2>/dev/null
70 test_set_prereq FUNNYNAMES
72 say 'Your filesystem does not allow newlines in filenames.'
75 test_expect_success FUNNYNAMES 'prompt - with newline in path' '
76 printf " (master)" >expected &&
77 git init "$repo_with_newline" &&
78 test_when_finished "rm -rf \"$repo_with_newline\"" &&
79 mkdir "$repo_with_newline"/subdir &&
81 cd "$repo_with_newline/subdir" &&
84 test_cmp expected "$actual"
87 test_expect_success 'prompt - detached head' '
88 printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
89 test_config core.abbrev 13 &&
91 test_when_finished "git checkout master" &&
92 __git_ps1 >"$actual" &&
93 test_cmp expected "$actual"
96 test_expect_success 'prompt - describe detached head - contains' '
97 printf " ((t2~1))" >expected &&
99 test_when_finished "git checkout master" &&
101 GIT_PS1_DESCRIBE_STYLE=contains &&
104 test_cmp expected "$actual"
107 test_expect_success 'prompt - describe detached head - branch' '
108 printf " ((b1~1))" >expected &&
110 test_when_finished "git checkout master" &&
112 GIT_PS1_DESCRIBE_STYLE=branch &&
115 test_cmp expected "$actual"
118 test_expect_success 'prompt - describe detached head - describe' '
119 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
121 test_when_finished "git checkout master" &&
123 GIT_PS1_DESCRIBE_STYLE=describe &&
126 test_cmp expected "$actual"
129 test_expect_success 'prompt - describe detached head - default' '
130 printf " ((t2))" >expected &&
131 git checkout --detach b1 &&
132 test_when_finished "git checkout master" &&
133 __git_ps1 >"$actual" &&
134 test_cmp expected "$actual"
137 test_expect_success 'prompt - inside .git directory' '
138 printf " (GIT_DIR!)" >expected &&
143 test_cmp expected "$actual"
146 test_expect_success 'prompt - deep inside .git directory' '
147 printf " (GIT_DIR!)" >expected &&
149 cd .git/refs/heads &&
152 test_cmp expected "$actual"
155 test_expect_success 'prompt - inside bare repository' '
156 printf " (BARE:master)" >expected &&
157 git init --bare bare.git &&
158 test_when_finished "rm -rf bare.git" &&
163 test_cmp expected "$actual"
166 test_expect_success 'prompt - interactive rebase' '
167 printf " (b1|REBASE-i 2/3)" >expected
168 write_script fake_editor.sh <<-\EOF &&
169 echo "exec echo" >"$1"
170 echo "edit $(git log -1 --format="%h")" >>"$1"
171 echo "exec echo" >>"$1"
173 test_when_finished "rm -f fake_editor.sh" &&
174 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
176 test_when_finished "git checkout master" &&
177 git rebase -i HEAD^ &&
178 test_when_finished "git rebase --abort"
179 __git_ps1 >"$actual" &&
180 test_cmp expected "$actual"
183 test_expect_success 'prompt - rebase merge' '
184 printf " (b2|REBASE-m 1/3)" >expected &&
186 test_when_finished "git checkout master" &&
187 test_must_fail git rebase --merge b1 b2 &&
188 test_when_finished "git rebase --abort" &&
189 __git_ps1 >"$actual" &&
190 test_cmp expected "$actual"
193 test_expect_success 'prompt - rebase' '
194 printf " (b2|REBASE 1/3)" >expected &&
196 test_when_finished "git checkout master" &&
197 test_must_fail git rebase b1 b2 &&
198 test_when_finished "git rebase --abort" &&
199 __git_ps1 >"$actual" &&
200 test_cmp expected "$actual"
203 test_expect_success 'prompt - merge' '
204 printf " (b1|MERGING)" >expected &&
206 test_when_finished "git checkout master" &&
207 test_must_fail git merge b2 &&
208 test_when_finished "git reset --hard" &&
209 __git_ps1 >"$actual" &&
210 test_cmp expected "$actual"
213 test_expect_success 'prompt - cherry-pick' '
214 printf " (master|CHERRY-PICKING)" >expected &&
215 test_must_fail git cherry-pick b1 &&
216 test_when_finished "git reset --hard" &&
217 __git_ps1 >"$actual" &&
218 test_cmp expected "$actual"
221 test_expect_success 'prompt - bisect' '
222 printf " (master|BISECTING)" >expected &&
224 test_when_finished "git bisect reset" &&
225 __git_ps1 >"$actual" &&
226 test_cmp expected "$actual"
229 test_expect_success 'prompt - dirty status indicator - clean' '
230 printf " (master)" >expected &&
232 GIT_PS1_SHOWDIRTYSTATE=y &&
235 test_cmp expected "$actual"
238 test_expect_success 'prompt - dirty status indicator - dirty worktree' '
239 printf " (master *)" >expected &&
240 echo "dirty" >file &&
241 test_when_finished "git reset --hard" &&
243 GIT_PS1_SHOWDIRTYSTATE=y &&
246 test_cmp expected "$actual"
249 test_expect_success 'prompt - dirty status indicator - dirty index' '
250 printf " (master +)" >expected &&
251 echo "dirty" >file &&
252 test_when_finished "git reset --hard" &&
255 GIT_PS1_SHOWDIRTYSTATE=y &&
258 test_cmp expected "$actual"
261 test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
262 printf " (master *+)" >expected &&
263 echo "dirty index" >file &&
264 test_when_finished "git reset --hard" &&
266 echo "dirty worktree" >file &&
268 GIT_PS1_SHOWDIRTYSTATE=y &&
271 test_cmp expected "$actual"
274 test_expect_success 'prompt - dirty status indicator - before root commit' '
275 printf " (master #)" >expected &&
277 GIT_PS1_SHOWDIRTYSTATE=y &&
281 test_cmp expected "$actual"
284 test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
285 printf " (master)" >expected &&
286 echo "dirty" >file &&
287 test_when_finished "git reset --hard" &&
288 test_config bash.showDirtyState false &&
290 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
293 test_cmp expected "$actual"
296 test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
297 printf " (master)" >expected &&
298 echo "dirty" >file &&
299 test_when_finished "git reset --hard" &&
300 test_config bash.showDirtyState true &&
302 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
305 test_cmp expected "$actual"
308 test_expect_success 'prompt - dirty status indicator - shell variable set 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 GIT_PS1_SHOWDIRTYSTATE=y &&
317 test_cmp expected "$actual"
320 test_expect_success 'prompt - dirty status indicator - shell variable set 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 GIT_PS1_SHOWDIRTYSTATE=y &&
329 test_cmp expected "$actual"
332 test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
333 printf " (GIT_DIR!)" >expected &&
334 echo "dirty" >file &&
335 test_when_finished "git reset --hard" &&
337 GIT_PS1_SHOWDIRTYSTATE=y &&
341 test_cmp expected "$actual"
344 test_expect_success 'prompt - stash status indicator - no stash' '
345 printf " (master)" >expected &&
347 GIT_PS1_SHOWSTASHSTATE=y &&
350 test_cmp expected "$actual"
353 test_expect_success 'prompt - stash status indicator - stash' '
354 printf " (master $)" >expected &&
357 test_when_finished "git stash drop" &&
358 git pack-refs --all &&
360 GIT_PS1_SHOWSTASHSTATE=y &&
363 test_cmp expected "$actual"
366 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
367 printf " (GIT_DIR!)" >expected &&
370 test_when_finished "git stash drop" &&
372 GIT_PS1_SHOWSTASHSTATE=y &&
376 test_cmp expected "$actual"
379 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
380 printf " (master)" >expected &&
382 GIT_PS1_SHOWUNTRACKEDFILES=y &&
386 test_cmp expected "$actual"
389 test_expect_success 'prompt - untracked files status indicator - untracked files' '
390 printf " (master %%)" >expected &&
392 GIT_PS1_SHOWUNTRACKEDFILES=y &&
395 test_cmp expected "$actual"
398 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
399 printf " (master)" >expected &&
400 test_config bash.showUntrackedFiles false &&
402 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
405 test_cmp expected "$actual"
408 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
409 printf " (master)" >expected &&
410 test_config bash.showUntrackedFiles true &&
412 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
415 test_cmp expected "$actual"
418 test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
419 printf " (master)" >expected &&
420 test_config bash.showUntrackedFiles false &&
422 GIT_PS1_SHOWUNTRACKEDFILES=y &&
425 test_cmp expected "$actual"
428 test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
429 printf " (master %%)" >expected &&
430 test_config bash.showUntrackedFiles true &&
432 GIT_PS1_SHOWUNTRACKEDFILES=y &&
435 test_cmp expected "$actual"
438 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
439 printf " (GIT_DIR!)" >expected &&
441 GIT_PS1_SHOWUNTRACKEDFILES=y &&
445 test_cmp expected "$actual"
448 test_expect_success 'prompt - format string starting with dash' '
449 printf -- "-master" >expected &&
450 __git_ps1 "-%s" >"$actual" &&
451 test_cmp expected "$actual"
454 test_expect_success 'prompt - pc mode' '
455 printf "BEFORE: (master):AFTER" >expected &&
456 printf "" >expected_output &&
458 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
459 test_cmp expected_output "$actual" &&
460 printf "%s" "$PS1" >"$actual"
462 test_cmp expected "$actual"
465 test_expect_success 'prompt - bash color pc mode - branch name' '
466 printf "BEFORE: (${c_green}master${c_clear}):AFTER" >expected &&
468 GIT_PS1_SHOWCOLORHINTS=y &&
469 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
470 printf "%s" "$PS1" >"$actual"
472 test_cmp expected "$actual"
475 test_expect_success 'prompt - bash color pc mode - detached head' '
476 printf "BEFORE: (${c_red}(%s...)${c_clear}):AFTER" $(git log -1 --format="%h" b1^) >expected &&
478 test_when_finished "git checkout master" &&
480 GIT_PS1_SHOWCOLORHINTS=y &&
481 __git_ps1 "BEFORE:" ":AFTER" &&
482 printf "%s" "$PS1" >"$actual"
484 test_cmp expected "$actual"
487 test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
488 printf "BEFORE: (${c_green}master${c_clear} ${c_red}*${c_clear}):AFTER" >expected &&
489 echo "dirty" >file &&
490 test_when_finished "git reset --hard" &&
492 GIT_PS1_SHOWDIRTYSTATE=y &&
493 GIT_PS1_SHOWCOLORHINTS=y &&
494 __git_ps1 "BEFORE:" ":AFTER" &&
495 printf "%s" "$PS1" >"$actual"
497 test_cmp expected "$actual"
500 test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index' '
501 printf "BEFORE: (${c_green}master${c_clear} ${c_green}+${c_clear}):AFTER" >expected &&
502 echo "dirty" >file &&
503 test_when_finished "git reset --hard" &&
506 GIT_PS1_SHOWDIRTYSTATE=y &&
507 GIT_PS1_SHOWCOLORHINTS=y &&
508 __git_ps1 "BEFORE:" ":AFTER" &&
509 printf "%s" "$PS1" >"$actual"
511 test_cmp expected "$actual"
514 test_expect_success 'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
515 printf "BEFORE: (${c_green}master${c_clear} ${c_red}*${c_green}+${c_clear}):AFTER" >expected &&
516 echo "dirty index" >file &&
517 test_when_finished "git reset --hard" &&
519 echo "dirty worktree" >file &&
521 GIT_PS1_SHOWCOLORHINTS=y &&
522 GIT_PS1_SHOWDIRTYSTATE=y &&
523 __git_ps1 "BEFORE:" ":AFTER" &&
524 printf "%s" "$PS1" >"$actual"
526 test_cmp expected "$actual"
529 test_expect_success 'prompt - bash color pc mode - dirty status indicator - before root commit' '
530 printf "BEFORE: (${c_green}master${c_clear} ${c_green}#${c_clear}):AFTER" >expected &&
532 GIT_PS1_SHOWDIRTYSTATE=y &&
533 GIT_PS1_SHOWCOLORHINTS=y &&
535 __git_ps1 "BEFORE:" ":AFTER" &&
536 printf "%s" "$PS1" >"$actual"
538 test_cmp expected "$actual"
541 test_expect_success 'prompt - bash color pc mode - inside .git directory' '
542 printf "BEFORE: (${c_green}GIT_DIR!${c_clear}):AFTER" >expected &&
543 echo "dirty" >file &&
544 test_when_finished "git reset --hard" &&
546 GIT_PS1_SHOWDIRTYSTATE=y &&
547 GIT_PS1_SHOWCOLORHINTS=y &&
549 __git_ps1 "BEFORE:" ":AFTER" &&
550 printf "%s" "$PS1" >"$actual"
552 test_cmp expected "$actual"
555 test_expect_success 'prompt - bash color pc mode - stash status indicator' '
556 printf "BEFORE: (${c_green}master${c_clear} ${c_lblue}\$${c_clear}):AFTER" >expected &&
559 test_when_finished "git stash drop" &&
561 GIT_PS1_SHOWSTASHSTATE=y &&
562 GIT_PS1_SHOWCOLORHINTS=y &&
563 __git_ps1 "BEFORE:" ":AFTER" &&
564 printf "%s" "$PS1" >"$actual"
566 test_cmp expected "$actual"
569 test_expect_success 'prompt - bash color pc mode - untracked files status indicator' '
570 printf "BEFORE: (${c_green}master${c_clear} ${c_red}%%${c_clear}):AFTER" >expected &&
572 GIT_PS1_SHOWUNTRACKEDFILES=y &&
573 GIT_PS1_SHOWCOLORHINTS=y &&
574 __git_ps1 "BEFORE:" ":AFTER" &&
575 printf "%s" "$PS1" >"$actual"
577 test_cmp expected "$actual"
580 test_expect_success 'prompt - zsh color pc mode' '
581 printf "BEFORE: (%%F{green}master%%f):AFTER" >expected &&
584 GIT_PS1_SHOWCOLORHINTS=y &&
585 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
586 printf "%s" "$PS1" >"$actual"
588 test_cmp expected "$actual"