]> git.ipfire.org Git - thirdparty/git.git/blame - t/t9903-bash-prompt.sh
Revert "Add new @ shortcut for HEAD"
[thirdparty/git.git] / t / t9903-bash-prompt.sh
CommitLineData
963c0407
SG
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
af31a456 10. "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
963c0407
SG
11
12actual="$TRASH_DIRECTORY/actual"
1572e18e
EA
13c_red='\\[\\e[31m\\]'
14c_green='\\[\\e[32m\\]'
15c_lblue='\\[\\e[1;34m\\]'
16c_clear='\\[\\e[0m\\]'
963c0407
SG
17
18test_expect_success 'setup for prompt tests' '
963c0407 19 git init otherrepo &&
4fe00b4f 20 echo 1 >file &&
963c0407
SG
21 git add file &&
22 test_tick &&
23 git commit -m initial &&
24 git tag -a -m msg1 t1 &&
25 git checkout -b b1 &&
4fe00b4f 26 echo 2 >file &&
963c0407 27 git commit -m "second b1" file &&
4fe00b4f 28 echo 3 >file &&
963c0407
SG
29 git commit -m "third b1" file &&
30 git tag -a -m msg2 t2 &&
31 git checkout -b b2 master &&
4fe00b4f 32 echo 0 >file &&
963c0407 33 git commit -m "second b2" file &&
4fe00b4f 34 echo 00 >file &&
b71dc3e1 35 git commit -m "another b2" file &&
4fe00b4f 36 echo 000 >file &&
b71dc3e1 37 git commit -m "yet another b2" file &&
963c0407
SG
38 git checkout master
39'
40
963c0407 41test_expect_success 'prompt - branch name' '
4fe00b4f
SG
42 printf " (master)" >expected &&
43 __git_ps1 >"$actual" &&
963c0407
SG
44 test_cmp expected "$actual"
45'
46
868dc1ac
SG
47test_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"
54'
55
e3e0b937
SG
56test_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"
62'
63
963c0407 64test_expect_success 'prompt - detached head' '
e8f21caf
SG
65 printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
66 test_config core.abbrev 13 &&
963c0407
SG
67 git checkout b1^ &&
68 test_when_finished "git checkout master" &&
4fe00b4f 69 __git_ps1 >"$actual" &&
963c0407
SG
70 test_cmp expected "$actual"
71'
72
73test_expect_success 'prompt - describe detached head - contains' '
4fe00b4f 74 printf " ((t2~1))" >expected &&
963c0407
SG
75 git checkout b1^ &&
76 test_when_finished "git checkout master" &&
77 (
78 GIT_PS1_DESCRIBE_STYLE=contains &&
4fe00b4f 79 __git_ps1 >"$actual"
963c0407
SG
80 ) &&
81 test_cmp expected "$actual"
82'
83
84test_expect_success 'prompt - describe detached head - branch' '
4fe00b4f 85 printf " ((b1~1))" >expected &&
963c0407
SG
86 git checkout b1^ &&
87 test_when_finished "git checkout master" &&
88 (
89 GIT_PS1_DESCRIBE_STYLE=branch &&
4fe00b4f 90 __git_ps1 >"$actual"
963c0407
SG
91 ) &&
92 test_cmp expected "$actual"
93'
94
95test_expect_success 'prompt - describe detached head - describe' '
4fe00b4f 96 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
963c0407
SG
97 git checkout b1^ &&
98 test_when_finished "git checkout master" &&
99 (
100 GIT_PS1_DESCRIBE_STYLE=describe &&
4fe00b4f 101 __git_ps1 >"$actual"
963c0407
SG
102 ) &&
103 test_cmp expected "$actual"
104'
105
106test_expect_success 'prompt - describe detached head - default' '
4fe00b4f 107 printf " ((t2))" >expected &&
963c0407
SG
108 git checkout --detach b1 &&
109 test_when_finished "git checkout master" &&
4fe00b4f 110 __git_ps1 >"$actual" &&
963c0407
SG
111 test_cmp expected "$actual"
112'
113
114test_expect_success 'prompt - inside .git directory' '
4fe00b4f 115 printf " (GIT_DIR!)" >expected &&
963c0407
SG
116 (
117 cd .git &&
4fe00b4f 118 __git_ps1 >"$actual"
963c0407
SG
119 ) &&
120 test_cmp expected "$actual"
121'
122
123test_expect_success 'prompt - deep inside .git directory' '
4fe00b4f 124 printf " (GIT_DIR!)" >expected &&
963c0407
SG
125 (
126 cd .git/refs/heads &&
4fe00b4f 127 __git_ps1 >"$actual"
963c0407
SG
128 ) &&
129 test_cmp expected "$actual"
130'
131
132test_expect_success 'prompt - inside bare repository' '
4fe00b4f 133 printf " (BARE:master)" >expected &&
963c0407
SG
134 git init --bare bare.git &&
135 test_when_finished "rm -rf bare.git" &&
136 (
137 cd bare.git &&
4fe00b4f 138 __git_ps1 >"$actual"
963c0407
SG
139 ) &&
140 test_cmp expected "$actual"
141'
142
143test_expect_success 'prompt - interactive rebase' '
4fe00b4f 144 printf " (b1|REBASE-i 2/3)" >expected
7412290c
SG
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"
149 EOF
963c0407 150 test_when_finished "rm -f fake_editor.sh" &&
963c0407
SG
151 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
152 git checkout b1 &&
153 test_when_finished "git checkout master" &&
154 git rebase -i HEAD^ &&
155 test_when_finished "git rebase --abort"
4fe00b4f 156 __git_ps1 >"$actual" &&
963c0407
SG
157 test_cmp expected "$actual"
158'
159
160test_expect_success 'prompt - rebase merge' '
4fe00b4f 161 printf " (b2|REBASE-m 1/3)" >expected &&
963c0407
SG
162 git checkout b2 &&
163 test_when_finished "git checkout master" &&
164 test_must_fail git rebase --merge b1 b2 &&
165 test_when_finished "git rebase --abort" &&
4fe00b4f 166 __git_ps1 >"$actual" &&
963c0407
SG
167 test_cmp expected "$actual"
168'
169
170test_expect_success 'prompt - rebase' '
4fe00b4f 171 printf " (b2|REBASE 1/3)" >expected &&
963c0407
SG
172 git checkout b2 &&
173 test_when_finished "git checkout master" &&
174 test_must_fail git rebase b1 b2 &&
175 test_when_finished "git rebase --abort" &&
4fe00b4f 176 __git_ps1 >"$actual" &&
963c0407
SG
177 test_cmp expected "$actual"
178'
179
180test_expect_success 'prompt - merge' '
4fe00b4f 181 printf " (b1|MERGING)" >expected &&
963c0407
SG
182 git checkout b1 &&
183 test_when_finished "git checkout master" &&
184 test_must_fail git merge b2 &&
185 test_when_finished "git reset --hard" &&
4fe00b4f 186 __git_ps1 >"$actual" &&
963c0407
SG
187 test_cmp expected "$actual"
188'
189
190test_expect_success 'prompt - cherry-pick' '
4fe00b4f 191 printf " (master|CHERRY-PICKING)" >expected &&
963c0407
SG
192 test_must_fail git cherry-pick b1 &&
193 test_when_finished "git reset --hard" &&
4fe00b4f 194 __git_ps1 >"$actual" &&
963c0407
SG
195 test_cmp expected "$actual"
196'
197
198test_expect_success 'prompt - bisect' '
4fe00b4f 199 printf " (master|BISECTING)" >expected &&
963c0407
SG
200 git bisect start &&
201 test_when_finished "git bisect reset" &&
4fe00b4f 202 __git_ps1 >"$actual" &&
963c0407
SG
203 test_cmp expected "$actual"
204'
205
206test_expect_success 'prompt - dirty status indicator - clean' '
4fe00b4f 207 printf " (master)" >expected &&
963c0407
SG
208 (
209 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 210 __git_ps1 >"$actual"
963c0407
SG
211 ) &&
212 test_cmp expected "$actual"
213'
214
215test_expect_success 'prompt - dirty status indicator - dirty worktree' '
4fe00b4f
SG
216 printf " (master *)" >expected &&
217 echo "dirty" >file &&
963c0407
SG
218 test_when_finished "git reset --hard" &&
219 (
220 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 221 __git_ps1 >"$actual"
963c0407
SG
222 ) &&
223 test_cmp expected "$actual"
224'
225
226test_expect_success 'prompt - dirty status indicator - dirty index' '
4fe00b4f
SG
227 printf " (master +)" >expected &&
228 echo "dirty" >file &&
963c0407
SG
229 test_when_finished "git reset --hard" &&
230 git add -u &&
231 (
232 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 233 __git_ps1 >"$actual"
963c0407
SG
234 ) &&
235 test_cmp expected "$actual"
236'
237
238test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
4fe00b4f
SG
239 printf " (master *+)" >expected &&
240 echo "dirty index" >file &&
963c0407
SG
241 test_when_finished "git reset --hard" &&
242 git add -u &&
4fe00b4f 243 echo "dirty worktree" >file &&
963c0407
SG
244 (
245 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 246 __git_ps1 >"$actual"
963c0407
SG
247 ) &&
248 test_cmp expected "$actual"
249'
250
251test_expect_success 'prompt - dirty status indicator - before root commit' '
4fe00b4f 252 printf " (master #)" >expected &&
963c0407
SG
253 (
254 GIT_PS1_SHOWDIRTYSTATE=y &&
255 cd otherrepo &&
4fe00b4f 256 __git_ps1 >"$actual"
963c0407
SG
257 ) &&
258 test_cmp expected "$actual"
259'
260
dc7e7bce 261test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
4fe00b4f
SG
262 printf " (master)" >expected &&
263 echo "dirty" >file &&
963c0407
SG
264 test_when_finished "git reset --hard" &&
265 test_config bash.showDirtyState false &&
dc7e7bce
MEW
266 (
267 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
4fe00b4f 268 __git_ps1 >"$actual"
dc7e7bce
MEW
269 ) &&
270 test_cmp expected "$actual"
271'
272
273test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
4fe00b4f
SG
274 printf " (master)" >expected &&
275 echo "dirty" >file &&
dc7e7bce
MEW
276 test_when_finished "git reset --hard" &&
277 test_config bash.showDirtyState true &&
278 (
279 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
4fe00b4f 280 __git_ps1 >"$actual"
dc7e7bce
MEW
281 ) &&
282 test_cmp expected "$actual"
283'
284
285test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
4fe00b4f
SG
286 printf " (master)" >expected &&
287 echo "dirty" >file &&
dc7e7bce
MEW
288 test_when_finished "git reset --hard" &&
289 test_config bash.showDirtyState false &&
290 (
291 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 292 __git_ps1 >"$actual"
dc7e7bce
MEW
293 ) &&
294 test_cmp expected "$actual"
295'
296
297test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
4fe00b4f
SG
298 printf " (master *)" >expected &&
299 echo "dirty" >file &&
dc7e7bce
MEW
300 test_when_finished "git reset --hard" &&
301 test_config bash.showDirtyState true &&
963c0407
SG
302 (
303 GIT_PS1_SHOWDIRTYSTATE=y &&
4fe00b4f 304 __git_ps1 >"$actual"
963c0407
SG
305 ) &&
306 test_cmp expected "$actual"
307'
308
309test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
4fe00b4f
SG
310 printf " (GIT_DIR!)" >expected &&
311 echo "dirty" >file &&
963c0407
SG
312 test_when_finished "git reset --hard" &&
313 (
314 GIT_PS1_SHOWDIRTYSTATE=y &&
315 cd .git &&
4fe00b4f 316 __git_ps1 >"$actual"
963c0407
SG
317 ) &&
318 test_cmp expected "$actual"
319'
320
321test_expect_success 'prompt - stash status indicator - no stash' '
4fe00b4f 322 printf " (master)" >expected &&
963c0407
SG
323 (
324 GIT_PS1_SHOWSTASHSTATE=y &&
4fe00b4f 325 __git_ps1 >"$actual"
963c0407
SG
326 ) &&
327 test_cmp expected "$actual"
328'
329
330test_expect_success 'prompt - stash status indicator - stash' '
4fe00b4f 331 printf " (master $)" >expected &&
963c0407
SG
332 echo 2 >file &&
333 git stash &&
334 test_when_finished "git stash drop" &&
dd0b72cb 335 git pack-refs --all &&
963c0407
SG
336 (
337 GIT_PS1_SHOWSTASHSTATE=y &&
4fe00b4f 338 __git_ps1 >"$actual"
963c0407
SG
339 ) &&
340 test_cmp expected "$actual"
341'
342
343test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
4fe00b4f 344 printf " (GIT_DIR!)" >expected &&
963c0407
SG
345 echo 2 >file &&
346 git stash &&
347 test_when_finished "git stash drop" &&
348 (
349 GIT_PS1_SHOWSTASHSTATE=y &&
350 cd .git &&
4fe00b4f 351 __git_ps1 >"$actual"
963c0407
SG
352 ) &&
353 test_cmp expected "$actual"
354'
355
356test_expect_success 'prompt - untracked files status indicator - no untracked files' '
4fe00b4f 357 printf " (master)" >expected &&
963c0407
SG
358 (
359 GIT_PS1_SHOWUNTRACKEDFILES=y &&
360 cd otherrepo &&
4fe00b4f 361 __git_ps1 >"$actual"
963c0407
SG
362 ) &&
363 test_cmp expected "$actual"
364'
365
366test_expect_success 'prompt - untracked files status indicator - untracked files' '
4fe00b4f 367 printf " (master %%)" >expected &&
963c0407
SG
368 (
369 GIT_PS1_SHOWUNTRACKEDFILES=y &&
4fe00b4f 370 __git_ps1 >"$actual"
963c0407
SG
371 ) &&
372 test_cmp expected "$actual"
373'
374
58978e82 375test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
4fe00b4f 376 printf " (master)" >expected &&
58978e82
MEW
377 test_config bash.showUntrackedFiles false &&
378 (
379 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
4fe00b4f 380 __git_ps1 >"$actual"
58978e82
MEW
381 ) &&
382 test_cmp expected "$actual"
383'
384
385test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
4fe00b4f 386 printf " (master)" >expected &&
58978e82
MEW
387 test_config bash.showUntrackedFiles true &&
388 (
389 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
4fe00b4f 390 __git_ps1 >"$actual"
58978e82
MEW
391 ) &&
392 test_cmp expected "$actual"
393'
394
395test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
4fe00b4f 396 printf " (master)" >expected &&
58978e82
MEW
397 test_config bash.showUntrackedFiles false &&
398 (
399 GIT_PS1_SHOWUNTRACKEDFILES=y &&
4fe00b4f 400 __git_ps1 >"$actual"
58978e82
MEW
401 ) &&
402 test_cmp expected "$actual"
403'
404
405test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
4fe00b4f 406 printf " (master %%)" >expected &&
58978e82
MEW
407 test_config bash.showUntrackedFiles true &&
408 (
409 GIT_PS1_SHOWUNTRACKEDFILES=y &&
4fe00b4f 410 __git_ps1 >"$actual"
58978e82
MEW
411 ) &&
412 test_cmp expected "$actual"
413'
414
963c0407 415test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
4fe00b4f 416 printf " (GIT_DIR!)" >expected &&
963c0407
SG
417 (
418 GIT_PS1_SHOWUNTRACKEDFILES=y &&
419 cd .git &&
4fe00b4f 420 __git_ps1 >"$actual"
963c0407
SG
421 ) &&
422 test_cmp expected "$actual"
423'
424
425test_expect_success 'prompt - format string starting with dash' '
4fe00b4f
SG
426 printf -- "-master" >expected &&
427 __git_ps1 "-%s" >"$actual" &&
963c0407
SG
428 test_cmp expected "$actual"
429'
430
1572e18e
EA
431test_expect_success 'prompt - pc mode' '
432 printf "BEFORE: (master):AFTER" >expected &&
433 printf "" >expected_output &&
434 (
435 __git_ps1 "BEFORE:" ":AFTER" >"$actual" &&
436 test_cmp expected_output "$actual" &&
437 printf "%s" "$PS1" >"$actual"
438 ) &&
439 test_cmp expected "$actual"
440'
441
442test_expect_success 'prompt - bash color pc mode - branch name' '
15981f4e 443 printf "BEFORE: (${c_green}master${c_clear}):AFTER" >expected &&
1572e18e
EA
444 (
445 GIT_PS1_SHOWCOLORHINTS=y &&
446 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
447 printf "%s" "$PS1" >"$actual"
448 ) &&
449 test_cmp expected "$actual"
450'
451
452test_expect_success 'prompt - bash color pc mode - detached head' '
15981f4e 453 printf "BEFORE: (${c_red}(%s...)${c_clear}):AFTER" $(git log -1 --format="%h" b1^) >expected &&
1572e18e
EA
454 git checkout b1^ &&
455 test_when_finished "git checkout master" &&
456 (
457 GIT_PS1_SHOWCOLORHINTS=y &&
458 __git_ps1 "BEFORE:" ":AFTER" &&
459 printf "%s" "$PS1" >"$actual"
460 ) &&
461 test_cmp expected "$actual"
462'
463
464test_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" &&
468 (
469 GIT_PS1_SHOWDIRTYSTATE=y &&
470 GIT_PS1_SHOWCOLORHINTS=y &&
471 __git_ps1 "BEFORE:" ":AFTER" &&
472 printf "%s" "$PS1" >"$actual"
473 ) &&
474 test_cmp expected "$actual"
475'
476
477test_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" &&
481 git add -u &&
482 (
483 GIT_PS1_SHOWDIRTYSTATE=y &&
484 GIT_PS1_SHOWCOLORHINTS=y &&
485 __git_ps1 "BEFORE:" ":AFTER" &&
486 printf "%s" "$PS1" >"$actual"
487 ) &&
488 test_cmp expected "$actual"
489'
490
491test_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" &&
495 git add -u &&
496 echo "dirty worktree" >file &&
497 (
498 GIT_PS1_SHOWCOLORHINTS=y &&
499 GIT_PS1_SHOWDIRTYSTATE=y &&
500 __git_ps1 "BEFORE:" ":AFTER" &&
501 printf "%s" "$PS1" >"$actual"
502 ) &&
503 test_cmp expected "$actual"
504'
505
506test_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 &&
508 (
509 GIT_PS1_SHOWDIRTYSTATE=y &&
510 GIT_PS1_SHOWCOLORHINTS=y &&
511 cd otherrepo &&
512 __git_ps1 "BEFORE:" ":AFTER" &&
513 printf "%s" "$PS1" >"$actual"
514 ) &&
515 test_cmp expected "$actual"
516'
517
518test_expect_success 'prompt - bash color pc mode - inside .git directory' '
15981f4e 519 printf "BEFORE: (${c_green}GIT_DIR!${c_clear}):AFTER" >expected &&
1572e18e
EA
520 echo "dirty" >file &&
521 test_when_finished "git reset --hard" &&
522 (
523 GIT_PS1_SHOWDIRTYSTATE=y &&
524 GIT_PS1_SHOWCOLORHINTS=y &&
525 cd .git &&
526 __git_ps1 "BEFORE:" ":AFTER" &&
527 printf "%s" "$PS1" >"$actual"
528 ) &&
529 test_cmp expected "$actual"
530'
531
532test_expect_success 'prompt - bash color pc mode - stash status indicator' '
533 printf "BEFORE: (${c_green}master${c_clear} ${c_lblue}\$${c_clear}):AFTER" >expected &&
534 echo 2 >file &&
535 git stash &&
536 test_when_finished "git stash drop" &&
537 (
538 GIT_PS1_SHOWSTASHSTATE=y &&
539 GIT_PS1_SHOWCOLORHINTS=y &&
540 __git_ps1 "BEFORE:" ":AFTER" &&
541 printf "%s" "$PS1" >"$actual"
542 ) &&
543 test_cmp expected "$actual"
544'
545
546test_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 &&
548 (
549 GIT_PS1_SHOWUNTRACKEDFILES=y &&
550 GIT_PS1_SHOWCOLORHINTS=y &&
551 __git_ps1 "BEFORE:" ":AFTER" &&
552 printf "%s" "$PS1" >"$actual"
553 ) &&
554 test_cmp expected "$actual"
555'
556
f3bd62d0 557test_expect_success 'prompt - zsh color pc mode' '
15981f4e 558 printf "BEFORE: (%%F{green}master%%f):AFTER" >expected &&
1572e18e
EA
559 (
560 ZSH_VERSION=5.0.0 &&
561 GIT_PS1_SHOWCOLORHINTS=y &&
562 __git_ps1 "BEFORE:" ":AFTER" >"$actual"
563 printf "%s" "$PS1" >"$actual"
564 ) &&
565 test_cmp expected "$actual"
566'
567
963c0407 568test_done