f250dfc55ac3ef58f04b0a0c901d56fc8e311883
1#!/bin/sh
2#
3# Copyright (c) 2012 SZEDER Gábor
4#
5
6test_description='test git-specific bash prompt functions'
7
8. ./lib-bash.sh
9
10. "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
11
12actual="$TRASH_DIRECTORY/actual"
13c_red='\\[\\e[31m\\]'
14c_green='\\[\\e[32m\\]'
15c_lblue='\\[\\e[1;34m\\]'
16c_clear='\\[\\e[0m\\]'
17
18test_expect_success 'setup for prompt tests' '
19 mkdir -p subdir/subsubdir &&
20 git init otherrepo &&
21 echo 1 > file &&
22 git add file &&
23 test_tick &&
24 git commit -m initial &&
25 git tag -a -m msg1 t1 &&
26 git checkout -b b1 &&
27 echo 2 > file &&
28 git commit -m "second b1" file &&
29 echo 3 > file &&
30 git commit -m "third b1" file &&
31 git tag -a -m msg2 t2 &&
32 git checkout -b b2 master &&
33 echo 0 > file &&
34 git commit -m "second b2" file &&
35 echo 00 > file &&
36 git commit -m "another b2" file &&
37 echo 000 > file &&
38 git commit -m "yet another b2" file &&
39 git checkout master
40'
41
42test_expect_success 'gitdir - from command line (through $__git_dir)' '
43 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
44 (
45 __git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
46 __gitdir > "$actual"
47 ) &&
48 test_cmp expected "$actual"
49'
50
51test_expect_success 'gitdir - repo as argument' '
52 echo "otherrepo/.git" > expected &&
53 __gitdir "otherrepo" > "$actual" &&
54 test_cmp expected "$actual"
55'
56
57test_expect_success 'gitdir - remote as argument' '
58 echo "remote" > expected &&
59 __gitdir "remote" > "$actual" &&
60 test_cmp expected "$actual"
61'
62
63test_expect_success 'gitdir - .git directory in cwd' '
64 echo ".git" > expected &&
65 __gitdir > "$actual" &&
66 test_cmp expected "$actual"
67'
68
69test_expect_success 'gitdir - .git directory in parent' '
70 echo "$(pwd -P)/.git" > expected &&
71 (
72 cd subdir/subsubdir &&
73 __gitdir > "$actual"
74 ) &&
75 test_cmp expected "$actual"
76'
77
78test_expect_success 'gitdir - cwd is a .git directory' '
79 echo "." > expected &&
80 (
81 cd .git &&
82 __gitdir > "$actual"
83 ) &&
84 test_cmp expected "$actual"
85'
86
87test_expect_success 'gitdir - parent is a .git directory' '
88 echo "$(pwd -P)/.git" > expected &&
89 (
90 cd .git/refs/heads &&
91 __gitdir > "$actual"
92 ) &&
93 test_cmp expected "$actual"
94'
95
96test_expect_success 'gitdir - $GIT_DIR set while .git directory in cwd' '
97 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
98 (
99 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
100 export GIT_DIR &&
101 __gitdir > "$actual"
102 ) &&
103 test_cmp expected "$actual"
104'
105
106test_expect_success 'gitdir - $GIT_DIR set while .git directory in parent' '
107 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
108 (
109 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
110 export GIT_DIR &&
111 cd subdir &&
112 __gitdir > "$actual"
113 ) &&
114 test_cmp expected "$actual"
115'
116
117test_expect_success 'gitdir - non-existing $GIT_DIR' '
118 (
119 GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
120 export GIT_DIR &&
121 test_must_fail __gitdir
122 )
123'
124
125test_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" &&
129 (
130 cd subdir &&
131 __gitdir > "$actual"
132 ) &&
133 test_cmp expected "$actual"
134'
135
136test_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" &&
140 (
141 cd subdir/subsubdir &&
142 __gitdir > "$actual"
143 ) &&
144 test_cmp expected "$actual"
145'
146
147test_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" &&
153 (
154 cd link &&
155 __gitdir > "$actual"
156 ) &&
157 test_cmp expected "$actual"
158'
159
160test_expect_success 'gitdir - not a git repository' '
161 (
162 cd subdir/subsubdir &&
163 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
164 export GIT_CEILING_DIRECTORIES &&
165 test_must_fail __gitdir
166 )
167'
168
169test_expect_success 'prompt - branch name' '
170 printf " (master)" > expected &&
171 __git_ps1 > "$actual" &&
172 test_cmp expected "$actual"
173'
174
175test_expect_success 'prompt - detached head' '
176 printf " ((%s...))" $(git log -1 --format="%h" b1^) > expected &&
177 git checkout b1^ &&
178 test_when_finished "git checkout master" &&
179 __git_ps1 > "$actual" &&
180 test_cmp expected "$actual"
181'
182
183test_expect_success 'prompt - describe detached head - contains' '
184 printf " ((t2~1))" > expected &&
185 git checkout b1^ &&
186 test_when_finished "git checkout master" &&
187 (
188 GIT_PS1_DESCRIBE_STYLE=contains &&
189 __git_ps1 > "$actual"
190 ) &&
191 test_cmp expected "$actual"
192'
193
194test_expect_success 'prompt - describe detached head - branch' '
195 printf " ((b1~1))" > expected &&
196 git checkout b1^ &&
197 test_when_finished "git checkout master" &&
198 (
199 GIT_PS1_DESCRIBE_STYLE=branch &&
200 __git_ps1 > "$actual"
201 ) &&
202 test_cmp expected "$actual"
203'
204
205test_expect_success 'prompt - describe detached head - describe' '
206 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) > expected &&
207 git checkout b1^ &&
208 test_when_finished "git checkout master" &&
209 (
210 GIT_PS1_DESCRIBE_STYLE=describe &&
211 __git_ps1 > "$actual"
212 ) &&
213 test_cmp expected "$actual"
214'
215
216test_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"
222'
223
224test_expect_success 'prompt - inside .git directory' '
225 printf " (GIT_DIR!)" > expected &&
226 (
227 cd .git &&
228 __git_ps1 > "$actual"
229 ) &&
230 test_cmp expected "$actual"
231'
232
233test_expect_success 'prompt - deep inside .git directory' '
234 printf " (GIT_DIR!)" > expected &&
235 (
236 cd .git/refs/heads &&
237 __git_ps1 > "$actual"
238 ) &&
239 test_cmp expected "$actual"
240'
241
242test_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" &&
246 (
247 cd bare.git &&
248 __git_ps1 > "$actual"
249 ) &&
250 test_cmp expected "$actual"
251'
252
253test_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 &&
257echo "exec echo" > "$1"
258echo "edit $(git log -1 --format="%h")" >> "$1"
259echo "exec echo" >> "$1"
260EOF
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" &&
264 git checkout b1 &&
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"
270'
271
272test_expect_success 'prompt - rebase merge' '
273 printf " (b2|REBASE-m 1/3)" > expected &&
274 git checkout b2 &&
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"
280'
281
282test_expect_success 'prompt - rebase' '
283 printf " (b2|REBASE 1/3)" > expected &&
284 git checkout b2 &&
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"
290'
291
292test_expect_success 'prompt - merge' '
293 printf " (b1|MERGING)" > expected &&
294 git checkout b1 &&
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"
300'
301
302test_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"
308'
309
310test_expect_success 'prompt - bisect' '
311 printf " (master|BISECTING)" > expected &&
312 git bisect start &&
313 test_when_finished "git bisect reset" &&
314 __git_ps1 > "$actual" &&
315 test_cmp expected "$actual"
316'
317
318test_expect_success 'prompt - dirty status indicator - clean' '
319 printf " (master)" > expected &&
320 (
321 GIT_PS1_SHOWDIRTYSTATE=y &&
322 __git_ps1 > "$actual"
323 ) &&
324 test_cmp expected "$actual"
325'
326
327test_expect_success 'prompt - dirty status indicator - dirty worktree' '
328 printf " (master *)" > expected &&
329 echo "dirty" > file &&
330 test_when_finished "git reset --hard" &&
331 (
332 GIT_PS1_SHOWDIRTYSTATE=y &&
333 __git_ps1 > "$actual"
334 ) &&
335 test_cmp expected "$actual"
336'
337
338test_expect_success 'prompt - dirty status indicator - dirty index' '
339 printf " (master +)" > expected &&
340 echo "dirty" > file &&
341 test_when_finished "git reset --hard" &&
342 git add -u &&
343 (
344 GIT_PS1_SHOWDIRTYSTATE=y &&
345 __git_ps1 > "$actual"
346 ) &&
347 test_cmp expected "$actual"
348'
349
350test_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" &&
354 git add -u &&
355 echo "dirty worktree" > file &&
356 (
357 GIT_PS1_SHOWDIRTYSTATE=y &&
358 __git_ps1 > "$actual"
359 ) &&
360 test_cmp expected "$actual"
361'
362
363test_expect_success 'prompt - dirty status indicator - before root commit' '
364 printf " (master #)" > expected &&
365 (
366 GIT_PS1_SHOWDIRTYSTATE=y &&
367 cd otherrepo &&
368 __git_ps1 > "$actual"
369 ) &&
370 test_cmp expected "$actual"
371'
372
373test_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 &&
378 (
379 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
380 __git_ps1 > "$actual"
381 ) &&
382 test_cmp expected "$actual"
383'
384
385test_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 &&
390 (
391 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
392 __git_ps1 > "$actual"
393 ) &&
394 test_cmp expected "$actual"
395'
396
397test_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 &&
402 (
403 GIT_PS1_SHOWDIRTYSTATE=y &&
404 __git_ps1 > "$actual"
405 ) &&
406 test_cmp expected "$actual"
407'
408
409test_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 &&
414 (
415 GIT_PS1_SHOWDIRTYSTATE=y &&
416 __git_ps1 > "$actual"
417 ) &&
418 test_cmp expected "$actual"
419'
420
421test_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" &&
425 (
426 GIT_PS1_SHOWDIRTYSTATE=y &&
427 cd .git &&
428 __git_ps1 > "$actual"
429 ) &&
430 test_cmp expected "$actual"
431'
432
433test_expect_success 'prompt - stash status indicator - no stash' '
434 printf " (master)" > expected &&
435 (
436 GIT_PS1_SHOWSTASHSTATE=y &&
437 __git_ps1 > "$actual"
438 ) &&
439 test_cmp expected "$actual"
440'
441
442test_expect_success 'prompt - stash status indicator - stash' '
443 printf " (master $)" > expected &&
444 echo 2 >file &&
445 git stash &&
446 test_when_finished "git stash drop" &&
447 (
448 GIT_PS1_SHOWSTASHSTATE=y &&
449 __git_ps1 > "$actual"
450 ) &&
451 test_cmp expected "$actual"
452'
453
454test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
455 printf " (GIT_DIR!)" > expected &&
456 echo 2 >file &&
457 git stash &&
458 test_when_finished "git stash drop" &&
459 (
460 GIT_PS1_SHOWSTASHSTATE=y &&
461 cd .git &&
462 __git_ps1 > "$actual"
463 ) &&
464 test_cmp expected "$actual"
465'
466
467test_expect_success 'prompt - untracked files status indicator - no untracked files' '
468 printf " (master)" > expected &&
469 (
470 GIT_PS1_SHOWUNTRACKEDFILES=y &&
471 cd otherrepo &&
472 __git_ps1 > "$actual"
473 ) &&
474 test_cmp expected "$actual"
475'
476
477test_expect_success 'prompt - untracked files status indicator - untracked files' '
478 printf " (master %%)" > expected &&
479 (
480 GIT_PS1_SHOWUNTRACKEDFILES=y &&
481 __git_ps1 > "$actual"
482 ) &&
483 test_cmp expected "$actual"
484'
485
486test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
487 printf " (master)" > expected &&
488 test_config bash.showUntrackedFiles false &&
489 (
490 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
491 __git_ps1 > "$actual"
492 ) &&
493 test_cmp expected "$actual"
494'
495
496test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
497 printf " (master)" > expected &&
498 test_config bash.showUntrackedFiles true &&
499 (
500 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
501 __git_ps1 > "$actual"
502 ) &&
503 test_cmp expected "$actual"
504'
505
506test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
507 printf " (master)" > expected &&
508 test_config bash.showUntrackedFiles false &&
509 (
510 GIT_PS1_SHOWUNTRACKEDFILES=y &&
511 __git_ps1 > "$actual"
512 ) &&
513 test_cmp expected "$actual"
514'
515
516test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
517 printf " (master %%)" > expected &&
518 test_config bash.showUntrackedFiles true &&
519 (
520 GIT_PS1_SHOWUNTRACKEDFILES=y &&
521 __git_ps1 > "$actual"
522 ) &&
523 test_cmp expected "$actual"
524'
525
526test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
527 printf " (GIT_DIR!)" > expected &&
528 (
529 GIT_PS1_SHOWUNTRACKEDFILES=y &&
530 cd .git &&
531 __git_ps1 > "$actual"
532 ) &&
533 test_cmp expected "$actual"
534'
535
536test_expect_success 'prompt - format string starting with dash' '
537 printf -- "-master" > expected &&
538 __git_ps1 "-%s" > "$actual" &&
539 test_cmp expected "$actual"
540'
541
542test_expect_success 'prompt - pc mode' '
543 printf "BEFORE: (master):AFTER" >expected &&
544 printf "" >expected_output &&
545 (
546 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
547 test_cmp expected_output "$actual" &&
548 printf "%s" "$PS1" >"$actual"
549 ) &&
550 test_cmp expected "$actual"
551'
552
553test_expect_success 'prompt - bash color pc mode - branch name' '
554 printf "BEFORE: (${c_green}master${c_clear}${c_clear}):AFTER" >expected &&
555 (
556 GIT_PS1_SHOWCOLORHINTS=y &&
557 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
558 printf "%s" "$PS1" >"$actual"
559 ) &&
560 test_cmp expected "$actual"
561'
562
563test_expect_success 'prompt - bash color pc mode - detached head' '
564 printf "BEFORE: (${c_red}(%s...)${c_clear}${c_clear}):AFTER" $(git log -1 --format="%h" b1^) >expected &&
565 git checkout b1^ &&
566 test_when_finished "git checkout master" &&
567 (
568 GIT_PS1_SHOWCOLORHINTS=y &&
569 __git_ps1 "BEFORE:" ":AFTER" &&
570 printf "%s" "$PS1" >"$actual"
571 ) &&
572 test_cmp expected "$actual"
573'
574
575test_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" &&
579 (
580 GIT_PS1_SHOWDIRTYSTATE=y &&
581 GIT_PS1_SHOWCOLORHINTS=y &&
582 __git_ps1 "BEFORE:" ":AFTER" &&
583 printf "%s" "$PS1" >"$actual"
584 ) &&
585 test_cmp expected "$actual"
586'
587
588test_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" &&
592 git add -u &&
593 (
594 GIT_PS1_SHOWDIRTYSTATE=y &&
595 GIT_PS1_SHOWCOLORHINTS=y &&
596 __git_ps1 "BEFORE:" ":AFTER" &&
597 printf "%s" "$PS1" >"$actual"
598 ) &&
599 test_cmp expected "$actual"
600'
601
602test_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" &&
606 git add -u &&
607 echo "dirty worktree" >file &&
608 (
609 GIT_PS1_SHOWCOLORHINTS=y &&
610 GIT_PS1_SHOWDIRTYSTATE=y &&
611 __git_ps1 "BEFORE:" ":AFTER" &&
612 printf "%s" "$PS1" >"$actual"
613 ) &&
614 test_cmp expected "$actual"
615'
616
617test_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 &&
619 (
620 GIT_PS1_SHOWDIRTYSTATE=y &&
621 GIT_PS1_SHOWCOLORHINTS=y &&
622 cd otherrepo &&
623 __git_ps1 "BEFORE:" ":AFTER" &&
624 printf "%s" "$PS1" >"$actual"
625 ) &&
626 test_cmp expected "$actual"
627'
628
629test_expect_success 'prompt - bash color pc mode - inside .git directory' '
630 printf "BEFORE: (${c_green}GIT_DIR!${c_clear}${c_clear}):AFTER" >expected &&
631 echo "dirty" >file &&
632 test_when_finished "git reset --hard" &&
633 (
634 GIT_PS1_SHOWDIRTYSTATE=y &&
635 GIT_PS1_SHOWCOLORHINTS=y &&
636 cd .git &&
637 __git_ps1 "BEFORE:" ":AFTER" &&
638 printf "%s" "$PS1" >"$actual"
639 ) &&
640 test_cmp expected "$actual"
641'
642
643test_expect_success 'prompt - bash color pc mode - stash status indicator' '
644 printf "BEFORE: (${c_green}master${c_clear} ${c_lblue}\$${c_clear}):AFTER" >expected &&
645 echo 2 >file &&
646 git stash &&
647 test_when_finished "git stash drop" &&
648 (
649 GIT_PS1_SHOWSTASHSTATE=y &&
650 GIT_PS1_SHOWCOLORHINTS=y &&
651 __git_ps1 "BEFORE:" ":AFTER" &&
652 printf "%s" "$PS1" >"$actual"
653 ) &&
654 test_cmp expected "$actual"
655'
656
657test_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 &&
659 (
660 GIT_PS1_SHOWUNTRACKEDFILES=y &&
661 GIT_PS1_SHOWCOLORHINTS=y &&
662 __git_ps1 "BEFORE:" ":AFTER" &&
663 printf "%s" "$PS1" >"$actual"
664 ) &&
665 test_cmp expected "$actual"
666'
667
668test_expect_success 'prompt - zsh color pc mode' '
669 printf "BEFORE: (%%F{green}master%%f%%f):AFTER" >expected &&
670 (
671 ZSH_VERSION=5.0.0 &&
672 GIT_PS1_SHOWCOLORHINTS=y &&
673 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
674 printf "%s" "$PS1" >"$actual"
675 ) &&
676 test_cmp expected "$actual"
677'
678
679test_done