Merge branch 'maint'
[git.git] / t / t9903-bash-prompt.sh
blob2101d914f2d34d28b05ce17982a24c0b008809af
1 #!/bin/sh
3 # Copyright (c) 2012 SZEDER Gábor
6 test_description='test git-specific bash prompt functions'
8 . ./lib-bash.sh
10 . "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
12 actual="$TRASH_DIRECTORY/actual"
14 test_expect_success 'setup for prompt tests' '
15 mkdir -p subdir/subsubdir &&
16 git init otherrepo &&
17 echo 1 > file &&
18 git add file &&
19 test_tick &&
20 git commit -m initial &&
21 git tag -a -m msg1 t1 &&
22 git checkout -b b1 &&
23 echo 2 > file &&
24 git commit -m "second b1" file &&
25 echo 3 > file &&
26 git commit -m "third b1" file &&
27 git tag -a -m msg2 t2 &&
28 git checkout -b b2 master &&
29 echo 0 > file &&
30 git commit -m "second b2" file &&
31 git checkout master
34 test_expect_success 'gitdir - from command line (through $__git_dir)' '
35 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
37 __git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
38 __gitdir > "$actual"
39 ) &&
40 test_cmp expected "$actual"
43 test_expect_success 'gitdir - repo as argument' '
44 echo "otherrepo/.git" > expected &&
45 __gitdir "otherrepo" > "$actual" &&
46 test_cmp expected "$actual"
49 test_expect_success 'gitdir - remote as argument' '
50 echo "remote" > expected &&
51 __gitdir "remote" > "$actual" &&
52 test_cmp expected "$actual"
55 test_expect_success 'gitdir - .git directory in cwd' '
56 echo ".git" > expected &&
57 __gitdir > "$actual" &&
58 test_cmp expected "$actual"
61 test_expect_success 'gitdir - .git directory in parent' '
62 echo "$TRASH_DIRECTORY/.git" > expected &&
64 cd subdir/subsubdir &&
65 __gitdir > "$actual"
66 ) &&
67 test_cmp expected "$actual"
70 test_expect_success 'gitdir - cwd is a .git directory' '
71 echo "." > expected &&
73 cd .git &&
74 __gitdir > "$actual"
75 ) &&
76 test_cmp expected "$actual"
79 test_expect_success 'gitdir - parent is a .git directory' '
80 echo "$TRASH_DIRECTORY/.git" > expected &&
82 cd .git/refs/heads &&
83 __gitdir > "$actual"
84 ) &&
85 test_cmp expected "$actual"
88 test_expect_success 'gitdir - $GIT_DIR set while .git directory in cwd' '
89 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
91 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
92 export GIT_DIR &&
93 __gitdir > "$actual"
94 ) &&
95 test_cmp expected "$actual"
98 test_expect_success 'gitdir - $GIT_DIR set while .git directory in parent' '
99 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
101 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
102 export GIT_DIR &&
103 cd subdir &&
104 __gitdir > "$actual"
105 ) &&
106 test_cmp expected "$actual"
109 test_expect_success 'gitdir - non-existing $GIT_DIR' '
111 GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
112 export GIT_DIR &&
113 test_must_fail __gitdir
117 test_expect_success 'gitdir - gitfile in cwd' '
118 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
119 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
120 test_when_finished "rm -f subdir/.git" &&
122 cd subdir &&
123 __gitdir > "$actual"
124 ) &&
125 test_cmp expected "$actual"
128 test_expect_success 'gitdir - gitfile in parent' '
129 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
130 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
131 test_when_finished "rm -f subdir/.git" &&
133 cd subdir/subsubdir &&
134 __gitdir > "$actual"
135 ) &&
136 test_cmp expected "$actual"
139 test_expect_success SYMLINKS 'gitdir - resulting path avoids symlinks' '
140 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
141 mkdir otherrepo/dir &&
142 test_when_finished "rm -rf otherrepo/dir" &&
143 ln -s otherrepo/dir link &&
144 test_when_finished "rm -f link" &&
146 cd link &&
147 __gitdir > "$actual"
148 ) &&
149 test_cmp expected "$actual"
152 test_expect_success 'gitdir - not a git repository' '
154 cd subdir/subsubdir &&
155 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
156 export GIT_CEILING_DIRECTORIES &&
157 test_must_fail __gitdir
161 test_expect_success 'prompt - branch name' '
162 printf " (master)" > expected &&
163 __git_ps1 > "$actual" &&
164 test_cmp expected "$actual"
167 test_expect_success 'prompt - detached head' '
168 printf " ((%s...))" $(git log -1 --format="%h" b1^) > expected &&
169 git checkout b1^ &&
170 test_when_finished "git checkout master" &&
171 __git_ps1 > "$actual" &&
172 test_cmp expected "$actual"
175 test_expect_success 'prompt - describe detached head - contains' '
176 printf " ((t2~1))" > expected &&
177 git checkout b1^ &&
178 test_when_finished "git checkout master" &&
180 GIT_PS1_DESCRIBE_STYLE=contains &&
181 __git_ps1 > "$actual"
182 ) &&
183 test_cmp expected "$actual"
186 test_expect_success 'prompt - describe detached head - branch' '
187 printf " ((b1~1))" > expected &&
188 git checkout b1^ &&
189 test_when_finished "git checkout master" &&
191 GIT_PS1_DESCRIBE_STYLE=branch &&
192 __git_ps1 > "$actual"
193 ) &&
194 test_cmp expected "$actual"
197 test_expect_success 'prompt - describe detached head - describe' '
198 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) > expected &&
199 git checkout b1^ &&
200 test_when_finished "git checkout master" &&
202 GIT_PS1_DESCRIBE_STYLE=describe &&
203 __git_ps1 > "$actual"
204 ) &&
205 test_cmp expected "$actual"
208 test_expect_success 'prompt - describe detached head - default' '
209 printf " ((t2))" > expected &&
210 git checkout --detach b1 &&
211 test_when_finished "git checkout master" &&
212 __git_ps1 > "$actual" &&
213 test_cmp expected "$actual"
216 test_expect_success 'prompt - inside .git directory' '
217 printf " (GIT_DIR!)" > expected &&
219 cd .git &&
220 __git_ps1 > "$actual"
221 ) &&
222 test_cmp expected "$actual"
225 test_expect_success 'prompt - deep inside .git directory' '
226 printf " (GIT_DIR!)" > expected &&
228 cd .git/refs/heads &&
229 __git_ps1 > "$actual"
230 ) &&
231 test_cmp expected "$actual"
234 test_expect_success 'prompt - inside bare repository' '
235 printf " (BARE:master)" > expected &&
236 git init --bare bare.git &&
237 test_when_finished "rm -rf bare.git" &&
239 cd bare.git &&
240 __git_ps1 > "$actual"
241 ) &&
242 test_cmp expected "$actual"
245 test_expect_success 'prompt - interactive rebase' '
246 printf " (b1|REBASE-i)" > expected
247 echo "#!$SHELL_PATH" >fake_editor.sh &&
248 cat >>fake_editor.sh <<\EOF &&
249 echo "edit $(git log -1 --format="%h")" > "$1"
251 test_when_finished "rm -f fake_editor.sh" &&
252 chmod a+x fake_editor.sh &&
253 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
254 git checkout b1 &&
255 test_when_finished "git checkout master" &&
256 git rebase -i HEAD^ &&
257 test_when_finished "git rebase --abort"
258 __git_ps1 > "$actual" &&
259 test_cmp expected "$actual"
262 test_expect_success 'prompt - rebase merge' '
263 printf " (b2|REBASE-m)" > expected &&
264 git checkout b2 &&
265 test_when_finished "git checkout master" &&
266 test_must_fail git rebase --merge b1 b2 &&
267 test_when_finished "git rebase --abort" &&
268 __git_ps1 > "$actual" &&
269 test_cmp expected "$actual"
272 test_expect_success 'prompt - rebase' '
273 printf " ((t2)|REBASE)" > expected &&
274 git checkout b2 &&
275 test_when_finished "git checkout master" &&
276 test_must_fail git rebase b1 b2 &&
277 test_when_finished "git rebase --abort" &&
278 __git_ps1 > "$actual" &&
279 test_cmp expected "$actual"
282 test_expect_success 'prompt - merge' '
283 printf " (b1|MERGING)" > expected &&
284 git checkout b1 &&
285 test_when_finished "git checkout master" &&
286 test_must_fail git merge b2 &&
287 test_when_finished "git reset --hard" &&
288 __git_ps1 > "$actual" &&
289 test_cmp expected "$actual"
292 test_expect_success 'prompt - cherry-pick' '
293 printf " (master|CHERRY-PICKING)" > expected &&
294 test_must_fail git cherry-pick b1 &&
295 test_when_finished "git reset --hard" &&
296 __git_ps1 > "$actual" &&
297 test_cmp expected "$actual"
300 test_expect_success 'prompt - bisect' '
301 printf " (master|BISECTING)" > expected &&
302 git bisect start &&
303 test_when_finished "git bisect reset" &&
304 __git_ps1 > "$actual" &&
305 test_cmp expected "$actual"
308 test_expect_success 'prompt - dirty status indicator - clean' '
309 printf " (master)" > expected &&
311 GIT_PS1_SHOWDIRTYSTATE=y &&
312 __git_ps1 > "$actual"
313 ) &&
314 test_cmp expected "$actual"
317 test_expect_success 'prompt - dirty status indicator - dirty worktree' '
318 printf " (master *)" > expected &&
319 echo "dirty" > file &&
320 test_when_finished "git reset --hard" &&
322 GIT_PS1_SHOWDIRTYSTATE=y &&
323 __git_ps1 > "$actual"
324 ) &&
325 test_cmp expected "$actual"
328 test_expect_success 'prompt - dirty status indicator - dirty index' '
329 printf " (master +)" > expected &&
330 echo "dirty" > file &&
331 test_when_finished "git reset --hard" &&
332 git add -u &&
334 GIT_PS1_SHOWDIRTYSTATE=y &&
335 __git_ps1 > "$actual"
336 ) &&
337 test_cmp expected "$actual"
340 test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
341 printf " (master *+)" > expected &&
342 echo "dirty index" > file &&
343 test_when_finished "git reset --hard" &&
344 git add -u &&
345 echo "dirty worktree" > file &&
347 GIT_PS1_SHOWDIRTYSTATE=y &&
348 __git_ps1 > "$actual"
349 ) &&
350 test_cmp expected "$actual"
353 test_expect_success 'prompt - dirty status indicator - before root commit' '
354 printf " (master #)" > expected &&
356 GIT_PS1_SHOWDIRTYSTATE=y &&
357 cd otherrepo &&
358 __git_ps1 > "$actual"
359 ) &&
360 test_cmp expected "$actual"
363 test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
364 printf " (master)" > expected &&
365 echo "dirty" > file &&
366 test_when_finished "git reset --hard" &&
367 test_config bash.showDirtyState false &&
369 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
370 __git_ps1 > "$actual"
371 ) &&
372 test_cmp expected "$actual"
375 test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
376 printf " (master)" > expected &&
377 echo "dirty" > file &&
378 test_when_finished "git reset --hard" &&
379 test_config bash.showDirtyState true &&
381 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
382 __git_ps1 > "$actual"
383 ) &&
384 test_cmp expected "$actual"
387 test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
388 printf " (master)" > expected &&
389 echo "dirty" > file &&
390 test_when_finished "git reset --hard" &&
391 test_config bash.showDirtyState false &&
393 GIT_PS1_SHOWDIRTYSTATE=y &&
394 __git_ps1 > "$actual"
395 ) &&
396 test_cmp expected "$actual"
399 test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
400 printf " (master *)" > expected &&
401 echo "dirty" > file &&
402 test_when_finished "git reset --hard" &&
403 test_config bash.showDirtyState true &&
405 GIT_PS1_SHOWDIRTYSTATE=y &&
406 __git_ps1 > "$actual"
407 ) &&
408 test_cmp expected "$actual"
411 test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
412 printf " (GIT_DIR!)" > expected &&
413 echo "dirty" > file &&
414 test_when_finished "git reset --hard" &&
416 GIT_PS1_SHOWDIRTYSTATE=y &&
417 cd .git &&
418 __git_ps1 > "$actual"
419 ) &&
420 test_cmp expected "$actual"
423 test_expect_success 'prompt - stash status indicator - no stash' '
424 printf " (master)" > expected &&
426 GIT_PS1_SHOWSTASHSTATE=y &&
427 __git_ps1 > "$actual"
428 ) &&
429 test_cmp expected "$actual"
432 test_expect_success 'prompt - stash status indicator - stash' '
433 printf " (master $)" > expected &&
434 echo 2 >file &&
435 git stash &&
436 test_when_finished "git stash drop" &&
438 GIT_PS1_SHOWSTASHSTATE=y &&
439 __git_ps1 > "$actual"
440 ) &&
441 test_cmp expected "$actual"
444 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
445 printf " (GIT_DIR!)" > expected &&
446 echo 2 >file &&
447 git stash &&
448 test_when_finished "git stash drop" &&
450 GIT_PS1_SHOWSTASHSTATE=y &&
451 cd .git &&
452 __git_ps1 > "$actual"
453 ) &&
454 test_cmp expected "$actual"
457 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
458 printf " (master)" > expected &&
460 GIT_PS1_SHOWUNTRACKEDFILES=y &&
461 cd otherrepo &&
462 __git_ps1 > "$actual"
463 ) &&
464 test_cmp expected "$actual"
467 test_expect_success 'prompt - untracked files status indicator - untracked files' '
468 printf " (master %%)" > expected &&
470 GIT_PS1_SHOWUNTRACKEDFILES=y &&
471 __git_ps1 > "$actual"
472 ) &&
473 test_cmp expected "$actual"
476 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
477 printf " (master)" > expected &&
478 test_config bash.showUntrackedFiles false &&
480 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
481 __git_ps1 > "$actual"
482 ) &&
483 test_cmp expected "$actual"
486 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
487 printf " (master)" > expected &&
488 test_config bash.showUntrackedFiles true &&
490 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
491 __git_ps1 > "$actual"
492 ) &&
493 test_cmp expected "$actual"
496 test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
497 printf " (master)" > expected &&
498 test_config bash.showUntrackedFiles false &&
500 GIT_PS1_SHOWUNTRACKEDFILES=y &&
501 __git_ps1 > "$actual"
502 ) &&
503 test_cmp expected "$actual"
506 test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
507 printf " (master %%)" > expected &&
508 test_config bash.showUntrackedFiles true &&
510 GIT_PS1_SHOWUNTRACKEDFILES=y &&
511 __git_ps1 > "$actual"
512 ) &&
513 test_cmp expected "$actual"
516 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
517 printf " (GIT_DIR!)" > expected &&
519 GIT_PS1_SHOWUNTRACKEDFILES=y &&
520 cd .git &&
521 __git_ps1 > "$actual"
522 ) &&
523 test_cmp expected "$actual"
526 test_expect_success 'prompt - format string starting with dash' '
527 printf -- "-master" > expected &&
528 __git_ps1 "-%s" > "$actual" &&
529 test_cmp expected "$actual"
532 test_done