bash prompt: use bash builtins to check stash state
[git/mingw/j6t.git] / t / t9903-bash-prompt.sh
blobc05458cbe671b4881af3596d9b06a120674519d7
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 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"
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"
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"
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" &&
74 GIT_PS1_DESCRIBE_STYLE=contains &&
75 __git_ps1 >"$actual"
76 ) &&
77 test_cmp expected "$actual"
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" &&
85 GIT_PS1_DESCRIBE_STYLE=branch &&
86 __git_ps1 >"$actual"
87 ) &&
88 test_cmp expected "$actual"
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" &&
96 GIT_PS1_DESCRIBE_STYLE=describe &&
97 __git_ps1 >"$actual"
98 ) &&
99 test_cmp expected "$actual"
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"
110 test_expect_success 'prompt - inside .git directory' '
111 printf " (GIT_DIR!)" >expected &&
113 cd .git &&
114 __git_ps1 >"$actual"
115 ) &&
116 test_cmp expected "$actual"
119 test_expect_success 'prompt - deep inside .git directory' '
120 printf " (GIT_DIR!)" >expected &&
122 cd .git/refs/heads &&
123 __git_ps1 >"$actual"
124 ) &&
125 test_cmp expected "$actual"
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" &&
133 cd bare.git &&
134 __git_ps1 >"$actual"
135 ) &&
136 test_cmp expected "$actual"
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"
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"
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"
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"
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"
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"
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"
202 test_expect_success 'prompt - dirty status indicator - clean' '
203 printf " (master)" >expected &&
205 GIT_PS1_SHOWDIRTYSTATE=y &&
206 __git_ps1 >"$actual"
207 ) &&
208 test_cmp expected "$actual"
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" &&
216 GIT_PS1_SHOWDIRTYSTATE=y &&
217 __git_ps1 >"$actual"
218 ) &&
219 test_cmp expected "$actual"
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 &&
228 GIT_PS1_SHOWDIRTYSTATE=y &&
229 __git_ps1 >"$actual"
230 ) &&
231 test_cmp expected "$actual"
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 &&
241 GIT_PS1_SHOWDIRTYSTATE=y &&
242 __git_ps1 >"$actual"
243 ) &&
244 test_cmp expected "$actual"
247 test_expect_success 'prompt - dirty status indicator - before root commit' '
248 printf " (master #)" >expected &&
250 GIT_PS1_SHOWDIRTYSTATE=y &&
251 cd otherrepo &&
252 __git_ps1 >"$actual"
253 ) &&
254 test_cmp expected "$actual"
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 &&
263 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
264 __git_ps1 >"$actual"
265 ) &&
266 test_cmp expected "$actual"
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 &&
275 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
276 __git_ps1 >"$actual"
277 ) &&
278 test_cmp expected "$actual"
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 &&
287 GIT_PS1_SHOWDIRTYSTATE=y &&
288 __git_ps1 >"$actual"
289 ) &&
290 test_cmp expected "$actual"
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 &&
299 GIT_PS1_SHOWDIRTYSTATE=y &&
300 __git_ps1 >"$actual"
301 ) &&
302 test_cmp expected "$actual"
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" &&
310 GIT_PS1_SHOWDIRTYSTATE=y &&
311 cd .git &&
312 __git_ps1 >"$actual"
313 ) &&
314 test_cmp expected "$actual"
317 test_expect_success 'prompt - stash status indicator - no stash' '
318 printf " (master)" >expected &&
320 GIT_PS1_SHOWSTASHSTATE=y &&
321 __git_ps1 >"$actual"
322 ) &&
323 test_cmp expected "$actual"
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 &&
333 GIT_PS1_SHOWSTASHSTATE=y &&
334 __git_ps1 >"$actual"
335 ) &&
336 test_cmp expected "$actual"
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" &&
345 GIT_PS1_SHOWSTASHSTATE=y &&
346 cd .git &&
347 __git_ps1 >"$actual"
348 ) &&
349 test_cmp expected "$actual"
352 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
353 printf " (master)" >expected &&
355 GIT_PS1_SHOWUNTRACKEDFILES=y &&
356 cd otherrepo &&
357 __git_ps1 >"$actual"
358 ) &&
359 test_cmp expected "$actual"
362 test_expect_success 'prompt - untracked files status indicator - untracked files' '
363 printf " (master %%)" >expected &&
365 GIT_PS1_SHOWUNTRACKEDFILES=y &&
366 __git_ps1 >"$actual"
367 ) &&
368 test_cmp expected "$actual"
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 &&
375 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
376 __git_ps1 >"$actual"
377 ) &&
378 test_cmp expected "$actual"
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 &&
385 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
386 __git_ps1 >"$actual"
387 ) &&
388 test_cmp expected "$actual"
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 &&
395 GIT_PS1_SHOWUNTRACKEDFILES=y &&
396 __git_ps1 >"$actual"
397 ) &&
398 test_cmp expected "$actual"
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 &&
405 GIT_PS1_SHOWUNTRACKEDFILES=y &&
406 __git_ps1 >"$actual"
407 ) &&
408 test_cmp expected "$actual"
411 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
412 printf " (GIT_DIR!)" >expected &&
414 GIT_PS1_SHOWUNTRACKEDFILES=y &&
415 cd .git &&
416 __git_ps1 >"$actual"
417 ) &&
418 test_cmp expected "$actual"
421 test_expect_success 'prompt - format string starting with dash' '
422 printf -- "-master" >expected &&
423 __git_ps1 "-%s" >"$actual" &&
424 test_cmp expected "$actual"
427 test_done