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 test_expect_success 'setup for prompt tests' '
15 mkdir -p subdir/subsubdir &&
20 git commit -m initial &&
21 git tag -a -m msg1 t1 &&
24 git commit -m "second b1" file &&
26 git commit -m "third b1" file &&
27 git tag -a -m msg2 t2 &&
28 git checkout -b b2 master &&
30 git commit -m "second b2" file &&
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" &&
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 &&
67 test_cmp expected "$actual"
70 test_expect_success 'gitdir - cwd is a .git directory' '
71 echo "." > expected &&
76 test_cmp expected "$actual"
79 test_expect_success 'gitdir - parent is a .git directory' '
80 echo "$TRASH_DIRECTORY/.git" > expected &&
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" &&
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" &&
106 test_cmp expected "$actual"
109 test_expect_success 'gitdir - non-existing $GIT_DIR' '
111 GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
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" &&
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 &&
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" &&
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 &&
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 &&
178 test_when_finished "git checkout master" &&
180 GIT_PS1_DESCRIBE_STYLE=contains &&
181 __git_ps1 > "$actual"
183 test_cmp expected "$actual"
186 test_expect_success 'prompt - describe detached head - branch' '
187 printf " ((b1~1))" > expected &&
189 test_when_finished "git checkout master" &&
191 GIT_PS1_DESCRIBE_STYLE=branch &&
192 __git_ps1 > "$actual"
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 &&
200 test_when_finished "git checkout master" &&
202 GIT_PS1_DESCRIBE_STYLE=describe &&
203 __git_ps1 > "$actual"
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 &&
220 __git_ps1 > "$actual"
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"
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" &&
240 __git_ps1 > "$actual"
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" &&
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 &&
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 &&
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 &&
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 &&
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"
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"
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" &&
334 GIT_PS1_SHOWDIRTYSTATE=y &&
335 __git_ps1 > "$actual"
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" &&
345 echo "dirty worktree" > file &&
347 GIT_PS1_SHOWDIRTYSTATE=y &&
348 __git_ps1 > "$actual"
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 &&
358 __git_ps1 > "$actual"
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"
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"
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"
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"
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 &&
418 __git_ps1 > "$actual"
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"
429 test_cmp expected "$actual"
432 test_expect_success 'prompt - stash status indicator - stash' '
433 printf " (master $)" > expected &&
436 test_when_finished "git stash drop" &&
438 GIT_PS1_SHOWSTASHSTATE=y &&
439 __git_ps1 > "$actual"
441 test_cmp expected "$actual"
444 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
445 printf " (GIT_DIR!)" > expected &&
448 test_when_finished "git stash drop" &&
450 GIT_PS1_SHOWSTASHSTATE=y &&
452 __git_ps1 > "$actual"
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 &&
462 __git_ps1 > "$actual"
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"
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"
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"
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"
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"
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 &&
521 __git_ps1 > "$actual"
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"