completion, bash prompt: move __gitdir() tests to completion test suite
[git/gitweb.git] / t / t9903-bash-prompt.sh
blobdf36239a59b49ef701a5612e05878e2974e59da1
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 git init otherrepo &&
16 echo 1 >file &&
17 git add file &&
18 test_tick &&
19 git commit -m initial &&
20 git tag -a -m msg1 t1 &&
21 git checkout -b b1 &&
22 echo 2 >file &&
23 git commit -m "second b1" file &&
24 echo 3 >file &&
25 git commit -m "third b1" file &&
26 git tag -a -m msg2 t2 &&
27 git checkout -b b2 master &&
28 echo 0 >file &&
29 git commit -m "second b2" file &&
30 echo 00 >file &&
31 git commit -m "another b2" file &&
32 echo 000 >file &&
33 git commit -m "yet another b2" file &&
34 git checkout master
37 test_expect_success 'prompt - branch name' '
38 printf " (master)" >expected &&
39 __git_ps1 >"$actual" &&
40 test_cmp expected "$actual"
43 test_expect_success 'prompt - detached head' '
44 printf " ((%s...))" $(git log -1 --format="%h" b1^) >expected &&
45 git checkout b1^ &&
46 test_when_finished "git checkout master" &&
47 __git_ps1 >"$actual" &&
48 test_cmp expected "$actual"
51 test_expect_success 'prompt - describe detached head - contains' '
52 printf " ((t2~1))" >expected &&
53 git checkout b1^ &&
54 test_when_finished "git checkout master" &&
56 GIT_PS1_DESCRIBE_STYLE=contains &&
57 __git_ps1 >"$actual"
58 ) &&
59 test_cmp expected "$actual"
62 test_expect_success 'prompt - describe detached head - branch' '
63 printf " ((b1~1))" >expected &&
64 git checkout b1^ &&
65 test_when_finished "git checkout master" &&
67 GIT_PS1_DESCRIBE_STYLE=branch &&
68 __git_ps1 >"$actual"
69 ) &&
70 test_cmp expected "$actual"
73 test_expect_success 'prompt - describe detached head - describe' '
74 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
75 git checkout b1^ &&
76 test_when_finished "git checkout master" &&
78 GIT_PS1_DESCRIBE_STYLE=describe &&
79 __git_ps1 >"$actual"
80 ) &&
81 test_cmp expected "$actual"
84 test_expect_success 'prompt - describe detached head - default' '
85 printf " ((t2))" >expected &&
86 git checkout --detach b1 &&
87 test_when_finished "git checkout master" &&
88 __git_ps1 >"$actual" &&
89 test_cmp expected "$actual"
92 test_expect_success 'prompt - inside .git directory' '
93 printf " (GIT_DIR!)" >expected &&
95 cd .git &&
96 __git_ps1 >"$actual"
97 ) &&
98 test_cmp expected "$actual"
101 test_expect_success 'prompt - deep inside .git directory' '
102 printf " (GIT_DIR!)" >expected &&
104 cd .git/refs/heads &&
105 __git_ps1 >"$actual"
106 ) &&
107 test_cmp expected "$actual"
110 test_expect_success 'prompt - inside bare repository' '
111 printf " (BARE:master)" >expected &&
112 git init --bare bare.git &&
113 test_when_finished "rm -rf bare.git" &&
115 cd bare.git &&
116 __git_ps1 >"$actual"
117 ) &&
118 test_cmp expected "$actual"
121 test_expect_success 'prompt - interactive rebase' '
122 printf " (b1|REBASE-i 2/3)" >expected
123 write_script fake_editor.sh <<-\EOF &&
124 echo "exec echo" >"$1"
125 echo "edit $(git log -1 --format="%h")" >>"$1"
126 echo "exec echo" >>"$1"
128 test_when_finished "rm -f fake_editor.sh" &&
129 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
130 git checkout b1 &&
131 test_when_finished "git checkout master" &&
132 git rebase -i HEAD^ &&
133 test_when_finished "git rebase --abort"
134 __git_ps1 >"$actual" &&
135 test_cmp expected "$actual"
138 test_expect_success 'prompt - rebase merge' '
139 printf " (b2|REBASE-m 1/3)" >expected &&
140 git checkout b2 &&
141 test_when_finished "git checkout master" &&
142 test_must_fail git rebase --merge b1 b2 &&
143 test_when_finished "git rebase --abort" &&
144 __git_ps1 >"$actual" &&
145 test_cmp expected "$actual"
148 test_expect_success 'prompt - rebase' '
149 printf " (b2|REBASE 1/3)" >expected &&
150 git checkout b2 &&
151 test_when_finished "git checkout master" &&
152 test_must_fail git rebase b1 b2 &&
153 test_when_finished "git rebase --abort" &&
154 __git_ps1 >"$actual" &&
155 test_cmp expected "$actual"
158 test_expect_success 'prompt - merge' '
159 printf " (b1|MERGING)" >expected &&
160 git checkout b1 &&
161 test_when_finished "git checkout master" &&
162 test_must_fail git merge b2 &&
163 test_when_finished "git reset --hard" &&
164 __git_ps1 >"$actual" &&
165 test_cmp expected "$actual"
168 test_expect_success 'prompt - cherry-pick' '
169 printf " (master|CHERRY-PICKING)" >expected &&
170 test_must_fail git cherry-pick b1 &&
171 test_when_finished "git reset --hard" &&
172 __git_ps1 >"$actual" &&
173 test_cmp expected "$actual"
176 test_expect_success 'prompt - bisect' '
177 printf " (master|BISECTING)" >expected &&
178 git bisect start &&
179 test_when_finished "git bisect reset" &&
180 __git_ps1 >"$actual" &&
181 test_cmp expected "$actual"
184 test_expect_success 'prompt - dirty status indicator - clean' '
185 printf " (master)" >expected &&
187 GIT_PS1_SHOWDIRTYSTATE=y &&
188 __git_ps1 >"$actual"
189 ) &&
190 test_cmp expected "$actual"
193 test_expect_success 'prompt - dirty status indicator - dirty worktree' '
194 printf " (master *)" >expected &&
195 echo "dirty" >file &&
196 test_when_finished "git reset --hard" &&
198 GIT_PS1_SHOWDIRTYSTATE=y &&
199 __git_ps1 >"$actual"
200 ) &&
201 test_cmp expected "$actual"
204 test_expect_success 'prompt - dirty status indicator - dirty index' '
205 printf " (master +)" >expected &&
206 echo "dirty" >file &&
207 test_when_finished "git reset --hard" &&
208 git add -u &&
210 GIT_PS1_SHOWDIRTYSTATE=y &&
211 __git_ps1 >"$actual"
212 ) &&
213 test_cmp expected "$actual"
216 test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
217 printf " (master *+)" >expected &&
218 echo "dirty index" >file &&
219 test_when_finished "git reset --hard" &&
220 git add -u &&
221 echo "dirty worktree" >file &&
223 GIT_PS1_SHOWDIRTYSTATE=y &&
224 __git_ps1 >"$actual"
225 ) &&
226 test_cmp expected "$actual"
229 test_expect_success 'prompt - dirty status indicator - before root commit' '
230 printf " (master #)" >expected &&
232 GIT_PS1_SHOWDIRTYSTATE=y &&
233 cd otherrepo &&
234 __git_ps1 >"$actual"
235 ) &&
236 test_cmp expected "$actual"
239 test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
240 printf " (master)" >expected &&
241 echo "dirty" >file &&
242 test_when_finished "git reset --hard" &&
243 test_config bash.showDirtyState false &&
245 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
246 __git_ps1 >"$actual"
247 ) &&
248 test_cmp expected "$actual"
251 test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
252 printf " (master)" >expected &&
253 echo "dirty" >file &&
254 test_when_finished "git reset --hard" &&
255 test_config bash.showDirtyState true &&
257 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
258 __git_ps1 >"$actual"
259 ) &&
260 test_cmp expected "$actual"
263 test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
264 printf " (master)" >expected &&
265 echo "dirty" >file &&
266 test_when_finished "git reset --hard" &&
267 test_config bash.showDirtyState false &&
269 GIT_PS1_SHOWDIRTYSTATE=y &&
270 __git_ps1 >"$actual"
271 ) &&
272 test_cmp expected "$actual"
275 test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
276 printf " (master *)" >expected &&
277 echo "dirty" >file &&
278 test_when_finished "git reset --hard" &&
279 test_config bash.showDirtyState true &&
281 GIT_PS1_SHOWDIRTYSTATE=y &&
282 __git_ps1 >"$actual"
283 ) &&
284 test_cmp expected "$actual"
287 test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
288 printf " (GIT_DIR!)" >expected &&
289 echo "dirty" >file &&
290 test_when_finished "git reset --hard" &&
292 GIT_PS1_SHOWDIRTYSTATE=y &&
293 cd .git &&
294 __git_ps1 >"$actual"
295 ) &&
296 test_cmp expected "$actual"
299 test_expect_success 'prompt - stash status indicator - no stash' '
300 printf " (master)" >expected &&
302 GIT_PS1_SHOWSTASHSTATE=y &&
303 __git_ps1 >"$actual"
304 ) &&
305 test_cmp expected "$actual"
308 test_expect_success 'prompt - stash status indicator - stash' '
309 printf " (master $)" >expected &&
310 echo 2 >file &&
311 git stash &&
312 test_when_finished "git stash drop" &&
314 GIT_PS1_SHOWSTASHSTATE=y &&
315 __git_ps1 >"$actual"
316 ) &&
317 test_cmp expected "$actual"
320 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
321 printf " (GIT_DIR!)" >expected &&
322 echo 2 >file &&
323 git stash &&
324 test_when_finished "git stash drop" &&
326 GIT_PS1_SHOWSTASHSTATE=y &&
327 cd .git &&
328 __git_ps1 >"$actual"
329 ) &&
330 test_cmp expected "$actual"
333 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
334 printf " (master)" >expected &&
336 GIT_PS1_SHOWUNTRACKEDFILES=y &&
337 cd otherrepo &&
338 __git_ps1 >"$actual"
339 ) &&
340 test_cmp expected "$actual"
343 test_expect_success 'prompt - untracked files status indicator - untracked files' '
344 printf " (master %%)" >expected &&
346 GIT_PS1_SHOWUNTRACKEDFILES=y &&
347 __git_ps1 >"$actual"
348 ) &&
349 test_cmp expected "$actual"
352 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
353 printf " (master)" >expected &&
354 test_config bash.showUntrackedFiles false &&
356 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
357 __git_ps1 >"$actual"
358 ) &&
359 test_cmp expected "$actual"
362 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
363 printf " (master)" >expected &&
364 test_config bash.showUntrackedFiles true &&
366 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
367 __git_ps1 >"$actual"
368 ) &&
369 test_cmp expected "$actual"
372 test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
373 printf " (master)" >expected &&
374 test_config bash.showUntrackedFiles false &&
376 GIT_PS1_SHOWUNTRACKEDFILES=y &&
377 __git_ps1 >"$actual"
378 ) &&
379 test_cmp expected "$actual"
382 test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
383 printf " (master %%)" >expected &&
384 test_config bash.showUntrackedFiles true &&
386 GIT_PS1_SHOWUNTRACKEDFILES=y &&
387 __git_ps1 >"$actual"
388 ) &&
389 test_cmp expected "$actual"
392 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
393 printf " (GIT_DIR!)" >expected &&
395 GIT_PS1_SHOWUNTRACKEDFILES=y &&
396 cd .git &&
397 __git_ps1 >"$actual"
398 ) &&
399 test_cmp expected "$actual"
402 test_expect_success 'prompt - format string starting with dash' '
403 printf -- "-master" >expected &&
404 __git_ps1 "-%s" >"$actual" &&
405 test_cmp expected "$actual"
408 test_done