bash prompt: combine 'git rev-parse' for detached head
[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         (
332                 GIT_PS1_SHOWSTASHSTATE=y &&
333                 __git_ps1 >"$actual"
334         ) &&
335         test_cmp expected "$actual"
336 '
337
338 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
339         printf " (GIT_DIR!)" >expected &&
340         echo 2 >file &&
341         git stash &&
342         test_when_finished "git stash drop" &&
343         (
344                 GIT_PS1_SHOWSTASHSTATE=y &&
345                 cd .git &&
346                 __git_ps1 >"$actual"
347         ) &&
348         test_cmp expected "$actual"
349 '
350
351 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
352         printf " (master)" >expected &&
353         (
354                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
355                 cd otherrepo &&
356                 __git_ps1 >"$actual"
357         ) &&
358         test_cmp expected "$actual"
359 '
360
361 test_expect_success 'prompt - untracked files status indicator - untracked files' '
362         printf " (master %%)" >expected &&
363         (
364                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
365                 __git_ps1 >"$actual"
366         ) &&
367         test_cmp expected "$actual"
368 '
369
370 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
371         printf " (master)" >expected &&
372         test_config bash.showUntrackedFiles false &&
373         (
374                 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
375                 __git_ps1 >"$actual"
376         ) &&
377         test_cmp expected "$actual"
378 '
379
380 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
381         printf " (master)" >expected &&
382         test_config bash.showUntrackedFiles true &&
383         (
384                 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
385                 __git_ps1 >"$actual"
386         ) &&
387         test_cmp expected "$actual"
388 '
389
390 test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
391         printf " (master)" >expected &&
392         test_config bash.showUntrackedFiles false &&
393         (
394                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
395                 __git_ps1 >"$actual"
396         ) &&
397         test_cmp expected "$actual"
398 '
399
400 test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
401         printf " (master %%)" >expected &&
402         test_config bash.showUntrackedFiles true &&
403         (
404                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
405                 __git_ps1 >"$actual"
406         ) &&
407         test_cmp expected "$actual"
408 '
409
410 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
411         printf " (GIT_DIR!)" >expected &&
412         (
413                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
414                 cd .git &&
415                 __git_ps1 >"$actual"
416         ) &&
417         test_cmp expected "$actual"
418 '
419
420 test_expect_success 'prompt - format string starting with dash' '
421         printf -- "-master" >expected &&
422         __git_ps1 "-%s" >"$actual" &&
423         test_cmp expected "$actual"
424 '
425
426 test_done