bash prompt: use bash builtins to check stash state
[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 SYMLINKS 'prompt - branch name - symlink symref' '
44         printf " (master)" >expected &&
45         test_when_finished "git checkout master" &&
46         test_config core.preferSymlinkRefs true &&
47         git checkout master &&
48         __git_ps1 >"$actual" &&
49         test_cmp expected "$actual"
50 '
51
52 test_expect_success 'prompt - unborn branch' '
53         printf " (unborn)" >expected &&
54         git checkout --orphan unborn &&
55         test_when_finished "git checkout master" &&
56         __git_ps1 >"$actual" &&
57         test_cmp expected "$actual"
58 '
59
60 test_expect_success 'prompt - detached head' '
61         printf " ((%s...))" $(git log -1 --format="%h" --abbrev=13 b1^) >expected &&
62         test_config core.abbrev 13 &&
63         git checkout b1^ &&
64         test_when_finished "git checkout master" &&
65         __git_ps1 >"$actual" &&
66         test_cmp expected "$actual"
67 '
68
69 test_expect_success 'prompt - describe detached head - contains' '
70         printf " ((t2~1))" >expected &&
71         git checkout b1^ &&
72         test_when_finished "git checkout master" &&
73         (
74                 GIT_PS1_DESCRIBE_STYLE=contains &&
75                 __git_ps1 >"$actual"
76         ) &&
77         test_cmp expected "$actual"
78 '
79
80 test_expect_success 'prompt - describe detached head - branch' '
81         printf " ((b1~1))" >expected &&
82         git checkout b1^ &&
83         test_when_finished "git checkout master" &&
84         (
85                 GIT_PS1_DESCRIBE_STYLE=branch &&
86                 __git_ps1 >"$actual"
87         ) &&
88         test_cmp expected "$actual"
89 '
90
91 test_expect_success 'prompt - describe detached head - describe' '
92         printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
93         git checkout b1^ &&
94         test_when_finished "git checkout master" &&
95         (
96                 GIT_PS1_DESCRIBE_STYLE=describe &&
97                 __git_ps1 >"$actual"
98         ) &&
99         test_cmp expected "$actual"
100 '
101
102 test_expect_success 'prompt - describe detached head - default' '
103         printf " ((t2))" >expected &&
104         git checkout --detach b1 &&
105         test_when_finished "git checkout master" &&
106         __git_ps1 >"$actual" &&
107         test_cmp expected "$actual"
108 '
109
110 test_expect_success 'prompt - inside .git directory' '
111         printf " (GIT_DIR!)" >expected &&
112         (
113                 cd .git &&
114                 __git_ps1 >"$actual"
115         ) &&
116         test_cmp expected "$actual"
117 '
118
119 test_expect_success 'prompt - deep inside .git directory' '
120         printf " (GIT_DIR!)" >expected &&
121         (
122                 cd .git/refs/heads &&
123                 __git_ps1 >"$actual"
124         ) &&
125         test_cmp expected "$actual"
126 '
127
128 test_expect_success 'prompt - inside bare repository' '
129         printf " (BARE:master)" >expected &&
130         git init --bare bare.git &&
131         test_when_finished "rm -rf bare.git" &&
132         (
133                 cd bare.git &&
134                 __git_ps1 >"$actual"
135         ) &&
136         test_cmp expected "$actual"
137 '
138
139 test_expect_success 'prompt - interactive rebase' '
140         printf " (b1|REBASE-i 2/3)" >expected
141         write_script fake_editor.sh <<-\EOF &&
142                 echo "exec echo" >"$1"
143                 echo "edit $(git log -1 --format="%h")" >>"$1"
144                 echo "exec echo" >>"$1"
145         EOF
146         test_when_finished "rm -f fake_editor.sh" &&
147         test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
148         git checkout b1 &&
149         test_when_finished "git checkout master" &&
150         git rebase -i HEAD^ &&
151         test_when_finished "git rebase --abort"
152         __git_ps1 >"$actual" &&
153         test_cmp expected "$actual"
154 '
155
156 test_expect_success 'prompt - rebase merge' '
157         printf " (b2|REBASE-m 1/3)" >expected &&
158         git checkout b2 &&
159         test_when_finished "git checkout master" &&
160         test_must_fail git rebase --merge b1 b2 &&
161         test_when_finished "git rebase --abort" &&
162         __git_ps1 >"$actual" &&
163         test_cmp expected "$actual"
164 '
165
166 test_expect_success 'prompt - rebase' '
167         printf " (b2|REBASE 1/3)" >expected &&
168         git checkout b2 &&
169         test_when_finished "git checkout master" &&
170         test_must_fail git rebase b1 b2 &&
171         test_when_finished "git rebase --abort" &&
172         __git_ps1 >"$actual" &&
173         test_cmp expected "$actual"
174 '
175
176 test_expect_success 'prompt - merge' '
177         printf " (b1|MERGING)" >expected &&
178         git checkout b1 &&
179         test_when_finished "git checkout master" &&
180         test_must_fail git merge b2 &&
181         test_when_finished "git reset --hard" &&
182         __git_ps1 >"$actual" &&
183         test_cmp expected "$actual"
184 '
185
186 test_expect_success 'prompt - cherry-pick' '
187         printf " (master|CHERRY-PICKING)" >expected &&
188         test_must_fail git cherry-pick b1 &&
189         test_when_finished "git reset --hard" &&
190         __git_ps1 >"$actual" &&
191         test_cmp expected "$actual"
192 '
193
194 test_expect_success 'prompt - bisect' '
195         printf " (master|BISECTING)" >expected &&
196         git bisect start &&
197         test_when_finished "git bisect reset" &&
198         __git_ps1 >"$actual" &&
199         test_cmp expected "$actual"
200 '
201
202 test_expect_success 'prompt - dirty status indicator - clean' '
203         printf " (master)" >expected &&
204         (
205                 GIT_PS1_SHOWDIRTYSTATE=y &&
206                 __git_ps1 >"$actual"
207         ) &&
208         test_cmp expected "$actual"
209 '
210
211 test_expect_success 'prompt - dirty status indicator - dirty worktree' '
212         printf " (master *)" >expected &&
213         echo "dirty" >file &&
214         test_when_finished "git reset --hard" &&
215         (
216                 GIT_PS1_SHOWDIRTYSTATE=y &&
217                 __git_ps1 >"$actual"
218         ) &&
219         test_cmp expected "$actual"
220 '
221
222 test_expect_success 'prompt - dirty status indicator - dirty index' '
223         printf " (master +)" >expected &&
224         echo "dirty" >file &&
225         test_when_finished "git reset --hard" &&
226         git add -u &&
227         (
228                 GIT_PS1_SHOWDIRTYSTATE=y &&
229                 __git_ps1 >"$actual"
230         ) &&
231         test_cmp expected "$actual"
232 '
233
234 test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
235         printf " (master *+)" >expected &&
236         echo "dirty index" >file &&
237         test_when_finished "git reset --hard" &&
238         git add -u &&
239         echo "dirty worktree" >file &&
240         (
241                 GIT_PS1_SHOWDIRTYSTATE=y &&
242                 __git_ps1 >"$actual"
243         ) &&
244         test_cmp expected "$actual"
245 '
246
247 test_expect_success 'prompt - dirty status indicator - before root commit' '
248         printf " (master #)" >expected &&
249         (
250                 GIT_PS1_SHOWDIRTYSTATE=y &&
251                 cd otherrepo &&
252                 __git_ps1 >"$actual"
253         ) &&
254         test_cmp expected "$actual"
255 '
256
257 test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
258         printf " (master)" >expected &&
259         echo "dirty" >file &&
260         test_when_finished "git reset --hard" &&
261         test_config bash.showDirtyState false &&
262         (
263                 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
264                 __git_ps1 >"$actual"
265         ) &&
266         test_cmp expected "$actual"
267 '
268
269 test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
270         printf " (master)" >expected &&
271         echo "dirty" >file &&
272         test_when_finished "git reset --hard" &&
273         test_config bash.showDirtyState true &&
274         (
275                 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
276                 __git_ps1 >"$actual"
277         ) &&
278         test_cmp expected "$actual"
279 '
280
281 test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
282         printf " (master)" >expected &&
283         echo "dirty" >file &&
284         test_when_finished "git reset --hard" &&
285         test_config bash.showDirtyState false &&
286         (
287                 GIT_PS1_SHOWDIRTYSTATE=y &&
288                 __git_ps1 >"$actual"
289         ) &&
290         test_cmp expected "$actual"
291 '
292
293 test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
294         printf " (master *)" >expected &&
295         echo "dirty" >file &&
296         test_when_finished "git reset --hard" &&
297         test_config bash.showDirtyState true &&
298         (
299                 GIT_PS1_SHOWDIRTYSTATE=y &&
300                 __git_ps1 >"$actual"
301         ) &&
302         test_cmp expected "$actual"
303 '
304
305 test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
306         printf " (GIT_DIR!)" >expected &&
307         echo "dirty" >file &&
308         test_when_finished "git reset --hard" &&
309         (
310                 GIT_PS1_SHOWDIRTYSTATE=y &&
311                 cd .git &&
312                 __git_ps1 >"$actual"
313         ) &&
314         test_cmp expected "$actual"
315 '
316
317 test_expect_success 'prompt - stash status indicator - no stash' '
318         printf " (master)" >expected &&
319         (
320                 GIT_PS1_SHOWSTASHSTATE=y &&
321                 __git_ps1 >"$actual"
322         ) &&
323         test_cmp expected "$actual"
324 '
325
326 test_expect_success 'prompt - stash status indicator - stash' '
327         printf " (master $)" >expected &&
328         echo 2 >file &&
329         git stash &&
330         test_when_finished "git stash drop" &&
331         git pack-refs --all &&
332         (
333                 GIT_PS1_SHOWSTASHSTATE=y &&
334                 __git_ps1 >"$actual"
335         ) &&
336         test_cmp expected "$actual"
337 '
338
339 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
340         printf " (GIT_DIR!)" >expected &&
341         echo 2 >file &&
342         git stash &&
343         test_when_finished "git stash drop" &&
344         (
345                 GIT_PS1_SHOWSTASHSTATE=y &&
346                 cd .git &&
347                 __git_ps1 >"$actual"
348         ) &&
349         test_cmp expected "$actual"
350 '
351
352 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
353         printf " (master)" >expected &&
354         (
355                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
356                 cd otherrepo &&
357                 __git_ps1 >"$actual"
358         ) &&
359         test_cmp expected "$actual"
360 '
361
362 test_expect_success 'prompt - untracked files status indicator - untracked files' '
363         printf " (master %%)" >expected &&
364         (
365                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
366                 __git_ps1 >"$actual"
367         ) &&
368         test_cmp expected "$actual"
369 '
370
371 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
372         printf " (master)" >expected &&
373         test_config bash.showUntrackedFiles false &&
374         (
375                 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
376                 __git_ps1 >"$actual"
377         ) &&
378         test_cmp expected "$actual"
379 '
380
381 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
382         printf " (master)" >expected &&
383         test_config bash.showUntrackedFiles true &&
384         (
385                 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
386                 __git_ps1 >"$actual"
387         ) &&
388         test_cmp expected "$actual"
389 '
390
391 test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
392         printf " (master)" >expected &&
393         test_config bash.showUntrackedFiles false &&
394         (
395                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
396                 __git_ps1 >"$actual"
397         ) &&
398         test_cmp expected "$actual"
399 '
400
401 test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
402         printf " (master %%)" >expected &&
403         test_config bash.showUntrackedFiles true &&
404         (
405                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
406                 __git_ps1 >"$actual"
407         ) &&
408         test_cmp expected "$actual"
409 '
410
411 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
412         printf " (GIT_DIR!)" >expected &&
413         (
414                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
415                 cd .git &&
416                 __git_ps1 >"$actual"
417         ) &&
418         test_cmp expected "$actual"
419 '
420
421 test_expect_success 'prompt - format string starting with dash' '
422         printf -- "-master" >expected &&
423         __git_ps1 "-%s" >"$actual" &&
424         test_cmp expected "$actual"
425 '
426
427 test_done