Use simpler relative_path when set_git_dir
[alt-git.git] / t / t9903-bash-prompt.sh
blob083b319ed11132ae03c1f653f1ba13149f5a1ebe
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 echo 00 > file &&
32 git commit -m "another b2" file &&
33 echo 000 > file &&
34 git commit -m "yet another b2" file &&
35 git checkout master
38 test_expect_success 'gitdir - from command line (through $__git_dir)' '
39 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
41 __git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
42 __gitdir > "$actual"
43 ) &&
44 test_cmp expected "$actual"
47 test_expect_success 'gitdir - repo as argument' '
48 echo "otherrepo/.git" > expected &&
49 __gitdir "otherrepo" > "$actual" &&
50 test_cmp expected "$actual"
53 test_expect_success 'gitdir - remote as argument' '
54 echo "remote" > expected &&
55 __gitdir "remote" > "$actual" &&
56 test_cmp expected "$actual"
59 test_expect_success 'gitdir - .git directory in cwd' '
60 echo ".git" > expected &&
61 __gitdir > "$actual" &&
62 test_cmp expected "$actual"
65 test_expect_success 'gitdir - .git directory in parent' '
66 echo "$(pwd -P)/.git" > expected &&
68 cd subdir/subsubdir &&
69 __gitdir > "$actual"
70 ) &&
71 test_cmp expected "$actual"
74 test_expect_success 'gitdir - cwd is a .git directory' '
75 echo "." > expected &&
77 cd .git &&
78 __gitdir > "$actual"
79 ) &&
80 test_cmp expected "$actual"
83 test_expect_success 'gitdir - parent is a .git directory' '
84 echo "$(pwd -P)/.git" > expected &&
86 cd .git/refs/heads &&
87 __gitdir > "$actual"
88 ) &&
89 test_cmp expected "$actual"
92 test_expect_success 'gitdir - $GIT_DIR set while .git directory in cwd' '
93 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
95 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
96 export GIT_DIR &&
97 __gitdir > "$actual"
98 ) &&
99 test_cmp expected "$actual"
102 test_expect_success 'gitdir - $GIT_DIR set while .git directory in parent' '
103 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
105 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
106 export GIT_DIR &&
107 cd subdir &&
108 __gitdir > "$actual"
109 ) &&
110 test_cmp expected "$actual"
113 test_expect_success 'gitdir - non-existing $GIT_DIR' '
115 GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
116 export GIT_DIR &&
117 test_must_fail __gitdir
121 test_expect_success 'gitdir - gitfile in cwd' '
122 echo "$(pwd -P)/otherrepo/.git" > expected &&
123 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
124 test_when_finished "rm -f subdir/.git" &&
126 cd subdir &&
127 __gitdir > "$actual"
128 ) &&
129 test_cmp expected "$actual"
132 test_expect_success 'gitdir - gitfile in parent' '
133 echo "$(pwd -P)/otherrepo/.git" > expected &&
134 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
135 test_when_finished "rm -f subdir/.git" &&
137 cd subdir/subsubdir &&
138 __gitdir > "$actual"
139 ) &&
140 test_cmp expected "$actual"
143 test_expect_success SYMLINKS 'gitdir - resulting path avoids symlinks' '
144 echo "$(pwd -P)/otherrepo/.git" > expected &&
145 mkdir otherrepo/dir &&
146 test_when_finished "rm -rf otherrepo/dir" &&
147 ln -s otherrepo/dir link &&
148 test_when_finished "rm -f link" &&
150 cd link &&
151 __gitdir > "$actual"
152 ) &&
153 test_cmp expected "$actual"
156 test_expect_success 'gitdir - not a git repository' '
158 cd subdir/subsubdir &&
159 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
160 export GIT_CEILING_DIRECTORIES &&
161 test_must_fail __gitdir
165 test_expect_success 'prompt - branch name' '
166 printf " (master)" > expected &&
167 __git_ps1 > "$actual" &&
168 test_cmp expected "$actual"
171 test_expect_success 'prompt - detached head' '
172 printf " ((%s...))" $(git log -1 --format="%h" b1^) > expected &&
173 git checkout b1^ &&
174 test_when_finished "git checkout master" &&
175 __git_ps1 > "$actual" &&
176 test_cmp expected "$actual"
179 test_expect_success 'prompt - describe detached head - contains' '
180 printf " ((t2~1))" > expected &&
181 git checkout b1^ &&
182 test_when_finished "git checkout master" &&
184 GIT_PS1_DESCRIBE_STYLE=contains &&
185 __git_ps1 > "$actual"
186 ) &&
187 test_cmp expected "$actual"
190 test_expect_success 'prompt - describe detached head - branch' '
191 printf " ((b1~1))" > expected &&
192 git checkout b1^ &&
193 test_when_finished "git checkout master" &&
195 GIT_PS1_DESCRIBE_STYLE=branch &&
196 __git_ps1 > "$actual"
197 ) &&
198 test_cmp expected "$actual"
201 test_expect_success 'prompt - describe detached head - describe' '
202 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) > expected &&
203 git checkout b1^ &&
204 test_when_finished "git checkout master" &&
206 GIT_PS1_DESCRIBE_STYLE=describe &&
207 __git_ps1 > "$actual"
208 ) &&
209 test_cmp expected "$actual"
212 test_expect_success 'prompt - describe detached head - default' '
213 printf " ((t2))" > expected &&
214 git checkout --detach b1 &&
215 test_when_finished "git checkout master" &&
216 __git_ps1 > "$actual" &&
217 test_cmp expected "$actual"
220 test_expect_success 'prompt - inside .git directory' '
221 printf " (GIT_DIR!)" > expected &&
223 cd .git &&
224 __git_ps1 > "$actual"
225 ) &&
226 test_cmp expected "$actual"
229 test_expect_success 'prompt - deep inside .git directory' '
230 printf " (GIT_DIR!)" > expected &&
232 cd .git/refs/heads &&
233 __git_ps1 > "$actual"
234 ) &&
235 test_cmp expected "$actual"
238 test_expect_success 'prompt - inside bare repository' '
239 printf " (BARE:master)" > expected &&
240 git init --bare bare.git &&
241 test_when_finished "rm -rf bare.git" &&
243 cd bare.git &&
244 __git_ps1 > "$actual"
245 ) &&
246 test_cmp expected "$actual"
249 test_expect_success 'prompt - interactive rebase' '
250 printf " (b1|REBASE-i 2/3)" > expected
251 echo "#!$SHELL_PATH" >fake_editor.sh &&
252 cat >>fake_editor.sh <<\EOF &&
253 echo "exec echo" > "$1"
254 echo "edit $(git log -1 --format="%h")" >> "$1"
255 echo "exec echo" >> "$1"
257 test_when_finished "rm -f fake_editor.sh" &&
258 chmod a+x fake_editor.sh &&
259 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
260 git checkout b1 &&
261 test_when_finished "git checkout master" &&
262 git rebase -i HEAD^ &&
263 test_when_finished "git rebase --abort"
264 __git_ps1 > "$actual" &&
265 test_cmp expected "$actual"
268 test_expect_success 'prompt - rebase merge' '
269 printf " (b2|REBASE-m 1/3)" > expected &&
270 git checkout b2 &&
271 test_when_finished "git checkout master" &&
272 test_must_fail git rebase --merge b1 b2 &&
273 test_when_finished "git rebase --abort" &&
274 __git_ps1 > "$actual" &&
275 test_cmp expected "$actual"
278 test_expect_success 'prompt - rebase' '
279 printf " ((t2)|REBASE 1/3)" > expected &&
280 git checkout b2 &&
281 test_when_finished "git checkout master" &&
282 test_must_fail git rebase b1 b2 &&
283 test_when_finished "git rebase --abort" &&
284 __git_ps1 > "$actual" &&
285 test_cmp expected "$actual"
288 test_expect_success 'prompt - merge' '
289 printf " (b1|MERGING)" > expected &&
290 git checkout b1 &&
291 test_when_finished "git checkout master" &&
292 test_must_fail git merge b2 &&
293 test_when_finished "git reset --hard" &&
294 __git_ps1 > "$actual" &&
295 test_cmp expected "$actual"
298 test_expect_success 'prompt - cherry-pick' '
299 printf " (master|CHERRY-PICKING)" > expected &&
300 test_must_fail git cherry-pick b1 &&
301 test_when_finished "git reset --hard" &&
302 __git_ps1 > "$actual" &&
303 test_cmp expected "$actual"
306 test_expect_success 'prompt - bisect' '
307 printf " (master|BISECTING)" > expected &&
308 git bisect start &&
309 test_when_finished "git bisect reset" &&
310 __git_ps1 > "$actual" &&
311 test_cmp expected "$actual"
314 test_expect_success 'prompt - dirty status indicator - clean' '
315 printf " (master)" > expected &&
317 GIT_PS1_SHOWDIRTYSTATE=y &&
318 __git_ps1 > "$actual"
319 ) &&
320 test_cmp expected "$actual"
323 test_expect_success 'prompt - dirty status indicator - dirty worktree' '
324 printf " (master *)" > expected &&
325 echo "dirty" > file &&
326 test_when_finished "git reset --hard" &&
328 GIT_PS1_SHOWDIRTYSTATE=y &&
329 __git_ps1 > "$actual"
330 ) &&
331 test_cmp expected "$actual"
334 test_expect_success 'prompt - dirty status indicator - dirty index' '
335 printf " (master +)" > expected &&
336 echo "dirty" > file &&
337 test_when_finished "git reset --hard" &&
338 git add -u &&
340 GIT_PS1_SHOWDIRTYSTATE=y &&
341 __git_ps1 > "$actual"
342 ) &&
343 test_cmp expected "$actual"
346 test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
347 printf " (master *+)" > expected &&
348 echo "dirty index" > file &&
349 test_when_finished "git reset --hard" &&
350 git add -u &&
351 echo "dirty worktree" > file &&
353 GIT_PS1_SHOWDIRTYSTATE=y &&
354 __git_ps1 > "$actual"
355 ) &&
356 test_cmp expected "$actual"
359 test_expect_success 'prompt - dirty status indicator - before root commit' '
360 printf " (master #)" > expected &&
362 GIT_PS1_SHOWDIRTYSTATE=y &&
363 cd otherrepo &&
364 __git_ps1 > "$actual"
365 ) &&
366 test_cmp expected "$actual"
369 test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
370 printf " (master)" > expected &&
371 echo "dirty" > file &&
372 test_when_finished "git reset --hard" &&
373 test_config bash.showDirtyState false &&
375 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
376 __git_ps1 > "$actual"
377 ) &&
378 test_cmp expected "$actual"
381 test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
382 printf " (master)" > expected &&
383 echo "dirty" > file &&
384 test_when_finished "git reset --hard" &&
385 test_config bash.showDirtyState true &&
387 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
388 __git_ps1 > "$actual"
389 ) &&
390 test_cmp expected "$actual"
393 test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
394 printf " (master)" > expected &&
395 echo "dirty" > file &&
396 test_when_finished "git reset --hard" &&
397 test_config bash.showDirtyState false &&
399 GIT_PS1_SHOWDIRTYSTATE=y &&
400 __git_ps1 > "$actual"
401 ) &&
402 test_cmp expected "$actual"
405 test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
406 printf " (master *)" > expected &&
407 echo "dirty" > file &&
408 test_when_finished "git reset --hard" &&
409 test_config bash.showDirtyState true &&
411 GIT_PS1_SHOWDIRTYSTATE=y &&
412 __git_ps1 > "$actual"
413 ) &&
414 test_cmp expected "$actual"
417 test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
418 printf " (GIT_DIR!)" > expected &&
419 echo "dirty" > file &&
420 test_when_finished "git reset --hard" &&
422 GIT_PS1_SHOWDIRTYSTATE=y &&
423 cd .git &&
424 __git_ps1 > "$actual"
425 ) &&
426 test_cmp expected "$actual"
429 test_expect_success 'prompt - stash status indicator - no stash' '
430 printf " (master)" > expected &&
432 GIT_PS1_SHOWSTASHSTATE=y &&
433 __git_ps1 > "$actual"
434 ) &&
435 test_cmp expected "$actual"
438 test_expect_success 'prompt - stash status indicator - stash' '
439 printf " (master $)" > expected &&
440 echo 2 >file &&
441 git stash &&
442 test_when_finished "git stash drop" &&
444 GIT_PS1_SHOWSTASHSTATE=y &&
445 __git_ps1 > "$actual"
446 ) &&
447 test_cmp expected "$actual"
450 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
451 printf " (GIT_DIR!)" > expected &&
452 echo 2 >file &&
453 git stash &&
454 test_when_finished "git stash drop" &&
456 GIT_PS1_SHOWSTASHSTATE=y &&
457 cd .git &&
458 __git_ps1 > "$actual"
459 ) &&
460 test_cmp expected "$actual"
463 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
464 printf " (master)" > expected &&
466 GIT_PS1_SHOWUNTRACKEDFILES=y &&
467 cd otherrepo &&
468 __git_ps1 > "$actual"
469 ) &&
470 test_cmp expected "$actual"
473 test_expect_success 'prompt - untracked files status indicator - untracked files' '
474 printf " (master %%)" > expected &&
476 GIT_PS1_SHOWUNTRACKEDFILES=y &&
477 __git_ps1 > "$actual"
478 ) &&
479 test_cmp expected "$actual"
482 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
483 printf " (master)" > expected &&
484 test_config bash.showUntrackedFiles false &&
486 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
487 __git_ps1 > "$actual"
488 ) &&
489 test_cmp expected "$actual"
492 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
493 printf " (master)" > expected &&
494 test_config bash.showUntrackedFiles true &&
496 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
497 __git_ps1 > "$actual"
498 ) &&
499 test_cmp expected "$actual"
502 test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
503 printf " (master)" > expected &&
504 test_config bash.showUntrackedFiles false &&
506 GIT_PS1_SHOWUNTRACKEDFILES=y &&
507 __git_ps1 > "$actual"
508 ) &&
509 test_cmp expected "$actual"
512 test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
513 printf " (master %%)" > expected &&
514 test_config bash.showUntrackedFiles true &&
516 GIT_PS1_SHOWUNTRACKEDFILES=y &&
517 __git_ps1 > "$actual"
518 ) &&
519 test_cmp expected "$actual"
522 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
523 printf " (GIT_DIR!)" > expected &&
525 GIT_PS1_SHOWUNTRACKEDFILES=y &&
526 cd .git &&
527 __git_ps1 > "$actual"
528 ) &&
529 test_cmp expected "$actual"
532 test_expect_success 'prompt - format string starting with dash' '
533 printf -- "-master" > expected &&
534 __git_ps1 "-%s" > "$actual" &&
535 test_cmp expected "$actual"
538 test_done