completion, bash prompt: move __gitdir() tests to completion test suite
[git] / t / t9903-bash-prompt.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012 SZEDER Gábor
4 #
5
6 test_description='test git-specific bash prompt functions'
7
8 . ./lib-bash.sh
9
10 . "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
11
12 actual="$TRASH_DIRECTORY/actual"
13
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
35 '
36
37 test_expect_success 'prompt - branch name' '
38         printf " (master)" >expected &&
39         __git_ps1 >"$actual" &&
40         test_cmp expected "$actual"
41 '
42
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"
49 '
50
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" &&
55         (
56                 GIT_PS1_DESCRIBE_STYLE=contains &&
57                 __git_ps1 >"$actual"
58         ) &&
59         test_cmp expected "$actual"
60 '
61
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" &&
66         (
67                 GIT_PS1_DESCRIBE_STYLE=branch &&
68                 __git_ps1 >"$actual"
69         ) &&
70         test_cmp expected "$actual"
71 '
72
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" &&
77         (
78                 GIT_PS1_DESCRIBE_STYLE=describe &&
79                 __git_ps1 >"$actual"
80         ) &&
81         test_cmp expected "$actual"
82 '
83
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"
90 '
91
92 test_expect_success 'prompt - inside .git directory' '
93         printf " (GIT_DIR!)" >expected &&
94         (
95                 cd .git &&
96                 __git_ps1 >"$actual"
97         ) &&
98         test_cmp expected "$actual"
99 '
100
101 test_expect_success 'prompt - deep inside .git directory' '
102         printf " (GIT_DIR!)" >expected &&
103         (
104                 cd .git/refs/heads &&
105                 __git_ps1 >"$actual"
106         ) &&
107         test_cmp expected "$actual"
108 '
109
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" &&
114         (
115                 cd bare.git &&
116                 __git_ps1 >"$actual"
117         ) &&
118         test_cmp expected "$actual"
119 '
120
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"
127         EOF
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"
136 '
137
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"
146 '
147
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"
156 '
157
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"
166 '
167
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"
174 '
175
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"
182 '
183
184 test_expect_success 'prompt - dirty status indicator - clean' '
185         printf " (master)" >expected &&
186         (
187                 GIT_PS1_SHOWDIRTYSTATE=y &&
188                 __git_ps1 >"$actual"
189         ) &&
190         test_cmp expected "$actual"
191 '
192
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" &&
197         (
198                 GIT_PS1_SHOWDIRTYSTATE=y &&
199                 __git_ps1 >"$actual"
200         ) &&
201         test_cmp expected "$actual"
202 '
203
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 &&
209         (
210                 GIT_PS1_SHOWDIRTYSTATE=y &&
211                 __git_ps1 >"$actual"
212         ) &&
213         test_cmp expected "$actual"
214 '
215
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 &&
222         (
223                 GIT_PS1_SHOWDIRTYSTATE=y &&
224                 __git_ps1 >"$actual"
225         ) &&
226         test_cmp expected "$actual"
227 '
228
229 test_expect_success 'prompt - dirty status indicator - before root commit' '
230         printf " (master #)" >expected &&
231         (
232                 GIT_PS1_SHOWDIRTYSTATE=y &&
233                 cd otherrepo &&
234                 __git_ps1 >"$actual"
235         ) &&
236         test_cmp expected "$actual"
237 '
238
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 &&
244         (
245                 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
246                 __git_ps1 >"$actual"
247         ) &&
248         test_cmp expected "$actual"
249 '
250
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 &&
256         (
257                 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
258                 __git_ps1 >"$actual"
259         ) &&
260         test_cmp expected "$actual"
261 '
262
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 &&
268         (
269                 GIT_PS1_SHOWDIRTYSTATE=y &&
270                 __git_ps1 >"$actual"
271         ) &&
272         test_cmp expected "$actual"
273 '
274
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 &&
280         (
281                 GIT_PS1_SHOWDIRTYSTATE=y &&
282                 __git_ps1 >"$actual"
283         ) &&
284         test_cmp expected "$actual"
285 '
286
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" &&
291         (
292                 GIT_PS1_SHOWDIRTYSTATE=y &&
293                 cd .git &&
294                 __git_ps1 >"$actual"
295         ) &&
296         test_cmp expected "$actual"
297 '
298
299 test_expect_success 'prompt - stash status indicator - no stash' '
300         printf " (master)" >expected &&
301         (
302                 GIT_PS1_SHOWSTASHSTATE=y &&
303                 __git_ps1 >"$actual"
304         ) &&
305         test_cmp expected "$actual"
306 '
307
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" &&
313         (
314                 GIT_PS1_SHOWSTASHSTATE=y &&
315                 __git_ps1 >"$actual"
316         ) &&
317         test_cmp expected "$actual"
318 '
319
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" &&
325         (
326                 GIT_PS1_SHOWSTASHSTATE=y &&
327                 cd .git &&
328                 __git_ps1 >"$actual"
329         ) &&
330         test_cmp expected "$actual"
331 '
332
333 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
334         printf " (master)" >expected &&
335         (
336                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
337                 cd otherrepo &&
338                 __git_ps1 >"$actual"
339         ) &&
340         test_cmp expected "$actual"
341 '
342
343 test_expect_success 'prompt - untracked files status indicator - untracked files' '
344         printf " (master %%)" >expected &&
345         (
346                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
347                 __git_ps1 >"$actual"
348         ) &&
349         test_cmp expected "$actual"
350 '
351
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 &&
355         (
356                 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
357                 __git_ps1 >"$actual"
358         ) &&
359         test_cmp expected "$actual"
360 '
361
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 &&
365         (
366                 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
367                 __git_ps1 >"$actual"
368         ) &&
369         test_cmp expected "$actual"
370 '
371
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 &&
375         (
376                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
377                 __git_ps1 >"$actual"
378         ) &&
379         test_cmp expected "$actual"
380 '
381
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 &&
385         (
386                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
387                 __git_ps1 >"$actual"
388         ) &&
389         test_cmp expected "$actual"
390 '
391
392 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
393         printf " (GIT_DIR!)" >expected &&
394         (
395                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
396                 cd .git &&
397                 __git_ps1 >"$actual"
398         ) &&
399         test_cmp expected "$actual"
400 '
401
402 test_expect_success 'prompt - format string starting with dash' '
403         printf -- "-master" >expected &&
404         __git_ps1 "-%s" >"$actual" &&
405         test_cmp expected "$actual"
406 '
407
408 test_done