3 # Copyright (c) 2012 SZEDER Gábor
6 test_description
='test git-specific bash prompt functions'
10 .
"$GIT_BUILD_DIR/contrib/completion/git-completion.bash"
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_failure
'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 - gitfile in cwd' '
110 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
111 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
112 test_when_finished "rm -f subdir/.git" &&
117 test_cmp expected "$actual"
120 test_expect_success
'gitdir - gitfile in parent' '
121 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
122 echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" > subdir/.git &&
123 test_when_finished "rm -f subdir/.git" &&
125 cd subdir/subsubdir &&
128 test_cmp expected "$actual"
131 test_expect_success SYMLINKS
'gitdir - resulting path avoids symlinks' '
132 echo "$TRASH_DIRECTORY/otherrepo/.git" > expected &&
133 mkdir otherrepo/dir &&
134 test_when_finished "rm -rf otherrepo/dir" &&
135 ln -s otherrepo/dir link &&
136 test_when_finished "rm -f link" &&
141 test_cmp expected "$actual"
144 test_expect_success
'gitdir - not a git repository' '
146 cd subdir/subsubdir &&
147 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
148 export GIT_CEILING_DIRECTORIES &&
149 test_must_fail __gitdir
153 test_expect_success
'prompt - branch name' '
154 printf " (master)" > expected &&
155 __git_ps1 > "$actual" &&
156 test_cmp expected "$actual"
159 test_expect_success
'prompt - detached head' '
160 printf " ((%s...))" $(git log -1 --format="%h" b1^) > expected &&
162 test_when_finished "git checkout master" &&
163 __git_ps1 > "$actual" &&
164 test_cmp expected "$actual"
167 test_expect_success
'prompt - describe detached head - contains' '
168 printf " ((t2~1))" > expected &&
170 test_when_finished "git checkout master" &&
172 GIT_PS1_DESCRIBE_STYLE=contains &&
173 __git_ps1 > "$actual"
175 test_cmp expected "$actual"
178 test_expect_success
'prompt - describe detached head - branch' '
179 printf " ((b1~1))" > expected &&
181 test_when_finished "git checkout master" &&
183 GIT_PS1_DESCRIBE_STYLE=branch &&
184 __git_ps1 > "$actual"
186 test_cmp expected "$actual"
189 test_expect_success
'prompt - describe detached head - describe' '
190 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) > expected &&
192 test_when_finished "git checkout master" &&
194 GIT_PS1_DESCRIBE_STYLE=describe &&
195 __git_ps1 > "$actual"
197 test_cmp expected "$actual"
200 test_expect_success
'prompt - describe detached head - default' '
201 printf " ((t2))" > expected &&
202 git checkout --detach b1 &&
203 test_when_finished "git checkout master" &&
204 __git_ps1 > "$actual" &&
205 test_cmp expected "$actual"
208 test_expect_success
'prompt - inside .git directory' '
209 printf " (GIT_DIR!)" > expected &&
212 __git_ps1 > "$actual"
214 test_cmp expected "$actual"
217 test_expect_success
'prompt - deep inside .git directory' '
218 printf " (GIT_DIR!)" > expected &&
220 cd .git/refs/heads &&
221 __git_ps1 > "$actual"
223 test_cmp expected "$actual"
226 test_expect_success
'prompt - inside bare repository' '
227 printf " (BARE:master)" > expected &&
228 git init --bare bare.git &&
229 test_when_finished "rm -rf bare.git" &&
232 __git_ps1 > "$actual"
234 test_cmp expected "$actual"
237 test_expect_success
'prompt - interactive rebase' '
238 printf " (b1|REBASE-i)" > expected
239 echo "#!$SHELL_PATH" >fake_editor.sh &&
240 cat >>fake_editor.sh <<\EOF &&
241 echo "edit $(git log -1 --format="%h")" > "$1"
243 test_when_finished "rm -f fake_editor.sh" &&
244 chmod a+x fake_editor.sh &&
245 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
247 test_when_finished "git checkout master" &&
248 git rebase -i HEAD^ &&
249 test_when_finished "git rebase --abort"
250 __git_ps1 > "$actual" &&
251 test_cmp expected "$actual"
254 test_expect_success
'prompt - rebase merge' '
255 printf " (b2|REBASE-m)" > expected &&
257 test_when_finished "git checkout master" &&
258 test_must_fail git rebase --merge b1 b2 &&
259 test_when_finished "git rebase --abort" &&
260 __git_ps1 > "$actual" &&
261 test_cmp expected "$actual"
264 test_expect_success
'prompt - rebase' '
265 printf " ((t2)|REBASE)" > expected &&
267 test_when_finished "git checkout master" &&
268 test_must_fail git rebase b1 b2 &&
269 test_when_finished "git rebase --abort" &&
270 __git_ps1 > "$actual" &&
271 test_cmp expected "$actual"
274 test_expect_success
'prompt - merge' '
275 printf " (b1|MERGING)" > expected &&
277 test_when_finished "git checkout master" &&
278 test_must_fail git merge b2 &&
279 test_when_finished "git reset --hard" &&
280 __git_ps1 > "$actual" &&
281 test_cmp expected "$actual"
284 test_expect_success
'prompt - cherry-pick' '
285 printf " (master|CHERRY-PICKING)" > expected &&
286 test_must_fail git cherry-pick b1 &&
287 test_when_finished "git reset --hard" &&
288 __git_ps1 > "$actual" &&
289 test_cmp expected "$actual"
292 test_expect_success
'prompt - bisect' '
293 printf " (master|BISECTING)" > expected &&
295 test_when_finished "git bisect reset" &&
296 __git_ps1 > "$actual" &&
297 test_cmp expected "$actual"
300 test_expect_success
'prompt - dirty status indicator - clean' '
301 printf " (master)" > expected &&
303 GIT_PS1_SHOWDIRTYSTATE=y &&
304 __git_ps1 > "$actual"
306 test_cmp expected "$actual"
309 test_expect_success
'prompt - dirty status indicator - dirty worktree' '
310 printf " (master *)" > expected &&
311 echo "dirty" > file &&
312 test_when_finished "git reset --hard" &&
314 GIT_PS1_SHOWDIRTYSTATE=y &&
315 __git_ps1 > "$actual"
317 test_cmp expected "$actual"
320 test_expect_success
'prompt - dirty status indicator - dirty index' '
321 printf " (master +)" > expected &&
322 echo "dirty" > file &&
323 test_when_finished "git reset --hard" &&
326 GIT_PS1_SHOWDIRTYSTATE=y &&
327 __git_ps1 > "$actual"
329 test_cmp expected "$actual"
332 test_expect_success
'prompt - dirty status indicator - dirty index and worktree' '
333 printf " (master *+)" > expected &&
334 echo "dirty index" > file &&
335 test_when_finished "git reset --hard" &&
337 echo "dirty worktree" > file &&
339 GIT_PS1_SHOWDIRTYSTATE=y &&
340 __git_ps1 > "$actual"
342 test_cmp expected "$actual"
345 test_expect_success
'prompt - dirty status indicator - before root commit' '
346 printf " (master #)" > expected &&
348 GIT_PS1_SHOWDIRTYSTATE=y &&
350 __git_ps1 > "$actual"
352 test_cmp expected "$actual"
355 test_expect_success
'prompt - dirty status indicator - disabled by config' '
356 printf " (master)" > expected &&
357 echo "dirty" > file &&
358 test_when_finished "git reset --hard" &&
359 test_config bash.showDirtyState false &&
361 GIT_PS1_SHOWDIRTYSTATE=y &&
362 __git_ps1 > "$actual"
364 test_cmp expected "$actual"
367 test_expect_success
'prompt - dirty status indicator - not shown inside .git directory' '
368 printf " (GIT_DIR!)" > expected &&
369 echo "dirty" > file &&
370 test_when_finished "git reset --hard" &&
372 GIT_PS1_SHOWDIRTYSTATE=y &&
374 __git_ps1 > "$actual"
376 test_cmp expected "$actual"
379 test_expect_success
'prompt - stash status indicator - no stash' '
380 printf " (master)" > expected &&
382 GIT_PS1_SHOWSTASHSTATE=y &&
383 __git_ps1 > "$actual"
385 test_cmp expected "$actual"
388 test_expect_success
'prompt - stash status indicator - stash' '
389 printf " (master $)" > expected &&
392 test_when_finished "git stash drop" &&
394 GIT_PS1_SHOWSTASHSTATE=y &&
395 __git_ps1 > "$actual"
397 test_cmp expected "$actual"
400 test_expect_success
'prompt - stash status indicator - not shown inside .git directory' '
401 printf " (GIT_DIR!)" > expected &&
404 test_when_finished "git stash drop" &&
406 GIT_PS1_SHOWSTASHSTATE=y &&
408 __git_ps1 > "$actual"
410 test_cmp expected "$actual"
413 test_expect_success
'prompt - untracked files status indicator - no untracked files' '
414 printf " (master)" > expected &&
416 GIT_PS1_SHOWUNTRACKEDFILES=y &&
418 __git_ps1 > "$actual"
420 test_cmp expected "$actual"
423 test_expect_success
'prompt - untracked files status indicator - untracked files' '
424 printf " (master %%)" > expected &&
426 GIT_PS1_SHOWUNTRACKEDFILES=y &&
427 __git_ps1 > "$actual"
429 test_cmp expected "$actual"
432 test_expect_success
'prompt - untracked files status indicator - not shown inside .git directory' '
433 printf " (GIT_DIR!)" > expected &&
435 GIT_PS1_SHOWUNTRACKEDFILES=y &&
437 __git_ps1 > "$actual"
439 test_cmp expected "$actual"
442 test_expect_success
'prompt - format string starting with dash' '
443 printf -- "-master" > expected &&
444 __git_ps1 "-%s" > "$actual" &&
445 test_cmp expected "$actual"