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 test_expect_success
'prompt - detached head' '
65 printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
66 test_config core.abbrev 13 &&
68 test_when_finished "git checkout master" &&
69 __git_ps1 >"$actual" &&
70 test_cmp expected "$actual"
73 test_expect_success
'prompt - describe detached head - contains' '
74 printf " ((t2~1))" >expected &&
76 test_when_finished "git checkout master" &&
78 GIT_PS1_DESCRIBE_STYLE=contains &&
81 test_cmp expected "$actual"
84 test_expect_success
'prompt - describe detached head - branch' '
85 printf " ((b1~1))" >expected &&
87 test_when_finished "git checkout master" &&
89 GIT_PS1_DESCRIBE_STYLE=branch &&
92 test_cmp expected "$actual"
95 test_expect_success
'prompt - describe detached head - describe' '
96 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
98 test_when_finished "git checkout master" &&
100 GIT_PS1_DESCRIBE_STYLE=describe &&
103 test_cmp expected "$actual"
106 test_expect_success
'prompt - describe detached head - default' '
107 printf " ((t2))" >expected &&
108 git checkout --detach b1 &&
109 test_when_finished "git checkout master" &&
110 __git_ps1 >"$actual" &&
111 test_cmp expected "$actual"
114 test_expect_success
'prompt - inside .git directory' '
115 printf " (GIT_DIR!)" >expected &&
120 test_cmp expected "$actual"
123 test_expect_success
'prompt - deep inside .git directory' '
124 printf " (GIT_DIR!)" >expected &&
126 cd .git/refs/heads &&
129 test_cmp expected "$actual"
132 test_expect_success
'prompt - inside bare repository' '
133 printf " (BARE:master)" >expected &&
134 git init --bare bare.git &&
135 test_when_finished "rm -rf bare.git" &&
140 test_cmp expected "$actual"
143 test_expect_success
'prompt - interactive rebase' '
144 printf " (b1|REBASE-i 2/3)" >expected
145 write_script fake_editor.sh <<-\EOF &&
146 echo "exec echo" >"$1"
147 echo "edit $(git log -1 --format="%h")" >>"$1"
148 echo "exec echo" >>"$1"
150 test_when_finished "rm -f fake_editor.sh" &&
151 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
153 test_when_finished "git checkout master" &&
154 git rebase -i HEAD^ &&
155 test_when_finished "git rebase --abort"
156 __git_ps1 >"$actual" &&
157 test_cmp expected "$actual"
160 test_expect_success
'prompt - rebase merge' '
161 printf " (b2|REBASE-m 1/3)" >expected &&
163 test_when_finished "git checkout master" &&
164 test_must_fail git rebase --merge b1 b2 &&
165 test_when_finished "git rebase --abort" &&
166 __git_ps1 >"$actual" &&
167 test_cmp expected "$actual"
170 test_expect_success
'prompt - rebase' '
171 printf " (b2|REBASE 1/3)" >expected &&
173 test_when_finished "git checkout master" &&
174 test_must_fail git rebase b1 b2 &&
175 test_when_finished "git rebase --abort" &&
176 __git_ps1 >"$actual" &&
177 test_cmp expected "$actual"
180 test_expect_success
'prompt - merge' '
181 printf " (b1|MERGING)" >expected &&
183 test_when_finished "git checkout master" &&
184 test_must_fail git merge b2 &&
185 test_when_finished "git reset --hard" &&
186 __git_ps1 >"$actual" &&
187 test_cmp expected "$actual"
190 test_expect_success
'prompt - cherry-pick' '
191 printf " (master|CHERRY-PICKING)" >expected &&
192 test_must_fail git cherry-pick b1 &&
193 test_when_finished "git reset --hard" &&
194 __git_ps1 >"$actual" &&
195 test_cmp expected "$actual"
198 test_expect_success
'prompt - bisect' '
199 printf " (master|BISECTING)" >expected &&
201 test_when_finished "git bisect reset" &&
202 __git_ps1 >"$actual" &&
203 test_cmp expected "$actual"
206 test_expect_success
'prompt - dirty status indicator - clean' '
207 printf " (master)" >expected &&
209 GIT_PS1_SHOWDIRTYSTATE=y &&
212 test_cmp expected "$actual"
215 test_expect_success
'prompt - dirty status indicator - dirty worktree' '
216 printf " (master *)" >expected &&
217 echo "dirty" >file &&
218 test_when_finished "git reset --hard" &&
220 GIT_PS1_SHOWDIRTYSTATE=y &&
223 test_cmp expected "$actual"
226 test_expect_success
'prompt - dirty status indicator - dirty index' '
227 printf " (master +)" >expected &&
228 echo "dirty" >file &&
229 test_when_finished "git reset --hard" &&
232 GIT_PS1_SHOWDIRTYSTATE=y &&
235 test_cmp expected "$actual"
238 test_expect_success
'prompt - dirty status indicator - dirty index and worktree' '
239 printf " (master *+)" >expected &&
240 echo "dirty index" >file &&
241 test_when_finished "git reset --hard" &&
243 echo "dirty worktree" >file &&
245 GIT_PS1_SHOWDIRTYSTATE=y &&
248 test_cmp expected "$actual"
251 test_expect_success
'prompt - dirty status indicator - before root commit' '
252 printf " (master #)" >expected &&
254 GIT_PS1_SHOWDIRTYSTATE=y &&
258 test_cmp expected "$actual"
261 test_expect_success
'prompt - dirty status indicator - shell variable unset with config disabled' '
262 printf " (master)" >expected &&
263 echo "dirty" >file &&
264 test_when_finished "git reset --hard" &&
265 test_config bash.showDirtyState false &&
267 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
270 test_cmp expected "$actual"
273 test_expect_success
'prompt - dirty status indicator - shell variable unset with config enabled' '
274 printf " (master)" >expected &&
275 echo "dirty" >file &&
276 test_when_finished "git reset --hard" &&
277 test_config bash.showDirtyState true &&
279 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
282 test_cmp expected "$actual"
285 test_expect_success
'prompt - dirty status indicator - shell variable set with config disabled' '
286 printf " (master)" >expected &&
287 echo "dirty" >file &&
288 test_when_finished "git reset --hard" &&
289 test_config bash.showDirtyState false &&
291 GIT_PS1_SHOWDIRTYSTATE=y &&
294 test_cmp expected "$actual"
297 test_expect_success
'prompt - dirty status indicator - shell variable set with config enabled' '
298 printf " (master *)" >expected &&
299 echo "dirty" >file &&
300 test_when_finished "git reset --hard" &&
301 test_config bash.showDirtyState true &&
303 GIT_PS1_SHOWDIRTYSTATE=y &&
306 test_cmp expected "$actual"
309 test_expect_success
'prompt - dirty status indicator - not shown inside .git directory' '
310 printf " (GIT_DIR!)" >expected &&
311 echo "dirty" >file &&
312 test_when_finished "git reset --hard" &&
314 GIT_PS1_SHOWDIRTYSTATE=y &&
318 test_cmp expected "$actual"
321 test_expect_success
'prompt - stash status indicator - no stash' '
322 printf " (master)" >expected &&
324 GIT_PS1_SHOWSTASHSTATE=y &&
327 test_cmp expected "$actual"
330 test_expect_success
'prompt - stash status indicator - stash' '
331 printf " (master $)" >expected &&
334 test_when_finished "git stash drop" &&
335 git pack-refs --all &&
337 GIT_PS1_SHOWSTASHSTATE=y &&
340 test_cmp expected "$actual"
343 test_expect_success
'prompt - stash status indicator - not shown inside .git directory' '
344 printf " (GIT_DIR!)" >expected &&
347 test_when_finished "git stash drop" &&
349 GIT_PS1_SHOWSTASHSTATE=y &&
353 test_cmp expected "$actual"
356 test_expect_success
'prompt - untracked files status indicator - no untracked files' '
357 printf " (master)" >expected &&
359 GIT_PS1_SHOWUNTRACKEDFILES=y &&
363 test_cmp expected "$actual"
366 test_expect_success
'prompt - untracked files status indicator - untracked files' '
367 printf " (master %%)" >expected &&
369 GIT_PS1_SHOWUNTRACKEDFILES=y &&
372 test_cmp expected "$actual"
375 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config disabled' '
376 printf " (master)" >expected &&
377 test_config bash.showUntrackedFiles false &&
379 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
382 test_cmp expected "$actual"
385 test_expect_success
'prompt - untracked files status indicator - shell variable unset with config enabled' '
386 printf " (master)" >expected &&
387 test_config bash.showUntrackedFiles true &&
389 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
392 test_cmp expected "$actual"
395 test_expect_success
'prompt - untracked files status indicator - shell variable set with config disabled' '
396 printf " (master)" >expected &&
397 test_config bash.showUntrackedFiles false &&
399 GIT_PS1_SHOWUNTRACKEDFILES=y &&
402 test_cmp expected "$actual"
405 test_expect_success
'prompt - untracked files status indicator - shell variable set with config enabled' '
406 printf " (master %%)" >expected &&
407 test_config bash.showUntrackedFiles true &&
409 GIT_PS1_SHOWUNTRACKEDFILES=y &&
412 test_cmp expected "$actual"
415 test_expect_success
'prompt - untracked files status indicator - not shown inside .git directory' '
416 printf " (GIT_DIR!)" >expected &&
418 GIT_PS1_SHOWUNTRACKEDFILES=y &&
422 test_cmp expected "$actual"
425 test_expect_success
'prompt - format string starting with dash' '
426 printf -- "-master" >expected &&
427 __git_ps1 "-%s" >"$actual" &&
428 test_cmp expected "$actual"
431 test_expect_success
'prompt - pc mode' '
432 printf "BEFORE: (master):AFTER" >expected &&
433 printf "" >expected_output &&
435 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
436 test_cmp expected_output "$actual" &&
437 printf "%s" "$PS1" >"$actual"
439 test_cmp expected "$actual"
442 test_expect_success
'prompt - bash color pc mode - branch name' '
443 printf "BEFORE: (${c_green}master${c_clear}):AFTER" >expected &&
445 GIT_PS1_SHOWCOLORHINTS=y &&
446 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
447 printf "%s" "$PS1" >"$actual"
449 test_cmp expected "$actual"
452 test_expect_success
'prompt - bash color pc mode - detached head' '
453 printf "BEFORE: (${c_red}(%s...)${c_clear}):AFTER" $(git log -1 --format="%h" b1^) >expected &&
455 test_when_finished "git checkout master" &&
457 GIT_PS1_SHOWCOLORHINTS=y &&
458 __git_ps1 "BEFORE:" ":AFTER" &&
459 printf "%s" "$PS1" >"$actual"
461 test_cmp expected "$actual"
464 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty worktree' '
465 printf "BEFORE: (${c_green}master${c_clear} ${c_red}*${c_clear}):AFTER" >expected &&
466 echo "dirty" >file &&
467 test_when_finished "git reset --hard" &&
469 GIT_PS1_SHOWDIRTYSTATE=y &&
470 GIT_PS1_SHOWCOLORHINTS=y &&
471 __git_ps1 "BEFORE:" ":AFTER" &&
472 printf "%s" "$PS1" >"$actual"
474 test_cmp expected "$actual"
477 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index' '
478 printf "BEFORE: (${c_green}master${c_clear} ${c_green}+${c_clear}):AFTER" >expected &&
479 echo "dirty" >file &&
480 test_when_finished "git reset --hard" &&
483 GIT_PS1_SHOWDIRTYSTATE=y &&
484 GIT_PS1_SHOWCOLORHINTS=y &&
485 __git_ps1 "BEFORE:" ":AFTER" &&
486 printf "%s" "$PS1" >"$actual"
488 test_cmp expected "$actual"
491 test_expect_success
'prompt - bash color pc mode - dirty status indicator - dirty index and worktree' '
492 printf "BEFORE: (${c_green}master${c_clear} ${c_red}*${c_green}+${c_clear}):AFTER" >expected &&
493 echo "dirty index" >file &&
494 test_when_finished "git reset --hard" &&
496 echo "dirty worktree" >file &&
498 GIT_PS1_SHOWCOLORHINTS=y &&
499 GIT_PS1_SHOWDIRTYSTATE=y &&
500 __git_ps1 "BEFORE:" ":AFTER" &&
501 printf "%s" "$PS1" >"$actual"
503 test_cmp expected "$actual"
506 test_expect_success
'prompt - bash color pc mode - dirty status indicator - before root commit' '
507 printf "BEFORE: (${c_green}master${c_clear} ${c_green}#${c_clear}):AFTER" >expected &&
509 GIT_PS1_SHOWDIRTYSTATE=y &&
510 GIT_PS1_SHOWCOLORHINTS=y &&
512 __git_ps1 "BEFORE:" ":AFTER" &&
513 printf "%s" "$PS1" >"$actual"
515 test_cmp expected "$actual"
518 test_expect_success
'prompt - bash color pc mode - inside .git directory' '
519 printf "BEFORE: (${c_green}GIT_DIR!${c_clear}):AFTER" >expected &&
520 echo "dirty" >file &&
521 test_when_finished "git reset --hard" &&
523 GIT_PS1_SHOWDIRTYSTATE=y &&
524 GIT_PS1_SHOWCOLORHINTS=y &&
526 __git_ps1 "BEFORE:" ":AFTER" &&
527 printf "%s" "$PS1" >"$actual"
529 test_cmp expected "$actual"
532 test_expect_success
'prompt - bash color pc mode - stash status indicator' '
533 printf "BEFORE: (${c_green}master${c_clear} ${c_lblue}\$${c_clear}):AFTER" >expected &&
536 test_when_finished "git stash drop" &&
538 GIT_PS1_SHOWSTASHSTATE=y &&
539 GIT_PS1_SHOWCOLORHINTS=y &&
540 __git_ps1 "BEFORE:" ":AFTER" &&
541 printf "%s" "$PS1" >"$actual"
543 test_cmp expected "$actual"
546 test_expect_success
'prompt - bash color pc mode - untracked files status indicator' '
547 printf "BEFORE: (${c_green}master${c_clear} ${c_red}%%${c_clear}):AFTER" >expected &&
549 GIT_PS1_SHOWUNTRACKEDFILES=y &&
550 GIT_PS1_SHOWCOLORHINTS=y &&
551 __git_ps1 "BEFORE:" ":AFTER" &&
552 printf "%s" "$PS1" >"$actual"
554 test_cmp expected "$actual"
557 test_expect_success
'prompt - zsh color pc mode' '
558 printf "BEFORE: (%%F{green}master%%f):AFTER" >expected &&
561 GIT_PS1_SHOWCOLORHINTS=y &&
562 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
563 printf "%s" "$PS1" >"$actual"
565 test_cmp expected "$actual"