bash prompt: add a test for symbolic link symbolic refs
[git/raj.git] / t / t9903-bash-prompt.sh
blob416e6219ce13a16ce4aa608ac29025cefcacc231
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 - detached head' '
53 printf " ((%s...))" $(git log -1 --format="%h" b1^) >expected &&
54 git checkout b1^ &&
55 test_when_finished "git checkout master" &&
56 __git_ps1 >"$actual" &&
57 test_cmp expected "$actual"
60 test_expect_success 'prompt - describe detached head - contains' '
61 printf " ((t2~1))" >expected &&
62 git checkout b1^ &&
63 test_when_finished "git checkout master" &&
65 GIT_PS1_DESCRIBE_STYLE=contains &&
66 __git_ps1 >"$actual"
67 ) &&
68 test_cmp expected "$actual"
71 test_expect_success 'prompt - describe detached head - branch' '
72 printf " ((b1~1))" >expected &&
73 git checkout b1^ &&
74 test_when_finished "git checkout master" &&
76 GIT_PS1_DESCRIBE_STYLE=branch &&
77 __git_ps1 >"$actual"
78 ) &&
79 test_cmp expected "$actual"
82 test_expect_success 'prompt - describe detached head - describe' '
83 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
84 git checkout b1^ &&
85 test_when_finished "git checkout master" &&
87 GIT_PS1_DESCRIBE_STYLE=describe &&
88 __git_ps1 >"$actual"
89 ) &&
90 test_cmp expected "$actual"
93 test_expect_success 'prompt - describe detached head - default' '
94 printf " ((t2))" >expected &&
95 git checkout --detach b1 &&
96 test_when_finished "git checkout master" &&
97 __git_ps1 >"$actual" &&
98 test_cmp expected "$actual"
101 test_expect_success 'prompt - inside .git directory' '
102 printf " (GIT_DIR!)" >expected &&
104 cd .git &&
105 __git_ps1 >"$actual"
106 ) &&
107 test_cmp expected "$actual"
110 test_expect_success 'prompt - deep inside .git directory' '
111 printf " (GIT_DIR!)" >expected &&
113 cd .git/refs/heads &&
114 __git_ps1 >"$actual"
115 ) &&
116 test_cmp expected "$actual"
119 test_expect_success 'prompt - inside bare repository' '
120 printf " (BARE:master)" >expected &&
121 git init --bare bare.git &&
122 test_when_finished "rm -rf bare.git" &&
124 cd bare.git &&
125 __git_ps1 >"$actual"
126 ) &&
127 test_cmp expected "$actual"
130 test_expect_success 'prompt - interactive rebase' '
131 printf " (b1|REBASE-i 2/3)" >expected
132 write_script fake_editor.sh <<-\EOF &&
133 echo "exec echo" >"$1"
134 echo "edit $(git log -1 --format="%h")" >>"$1"
135 echo "exec echo" >>"$1"
137 test_when_finished "rm -f fake_editor.sh" &&
138 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
139 git checkout b1 &&
140 test_when_finished "git checkout master" &&
141 git rebase -i HEAD^ &&
142 test_when_finished "git rebase --abort"
143 __git_ps1 >"$actual" &&
144 test_cmp expected "$actual"
147 test_expect_success 'prompt - rebase merge' '
148 printf " (b2|REBASE-m 1/3)" >expected &&
149 git checkout b2 &&
150 test_when_finished "git checkout master" &&
151 test_must_fail git rebase --merge b1 b2 &&
152 test_when_finished "git rebase --abort" &&
153 __git_ps1 >"$actual" &&
154 test_cmp expected "$actual"
157 test_expect_success 'prompt - rebase' '
158 printf " (b2|REBASE 1/3)" >expected &&
159 git checkout b2 &&
160 test_when_finished "git checkout master" &&
161 test_must_fail git rebase b1 b2 &&
162 test_when_finished "git rebase --abort" &&
163 __git_ps1 >"$actual" &&
164 test_cmp expected "$actual"
167 test_expect_success 'prompt - merge' '
168 printf " (b1|MERGING)" >expected &&
169 git checkout b1 &&
170 test_when_finished "git checkout master" &&
171 test_must_fail git merge b2 &&
172 test_when_finished "git reset --hard" &&
173 __git_ps1 >"$actual" &&
174 test_cmp expected "$actual"
177 test_expect_success 'prompt - cherry-pick' '
178 printf " (master|CHERRY-PICKING)" >expected &&
179 test_must_fail git cherry-pick b1 &&
180 test_when_finished "git reset --hard" &&
181 __git_ps1 >"$actual" &&
182 test_cmp expected "$actual"
185 test_expect_success 'prompt - bisect' '
186 printf " (master|BISECTING)" >expected &&
187 git bisect start &&
188 test_when_finished "git bisect reset" &&
189 __git_ps1 >"$actual" &&
190 test_cmp expected "$actual"
193 test_expect_success 'prompt - dirty status indicator - clean' '
194 printf " (master)" >expected &&
196 GIT_PS1_SHOWDIRTYSTATE=y &&
197 __git_ps1 >"$actual"
198 ) &&
199 test_cmp expected "$actual"
202 test_expect_success 'prompt - dirty status indicator - dirty worktree' '
203 printf " (master *)" >expected &&
204 echo "dirty" >file &&
205 test_when_finished "git reset --hard" &&
207 GIT_PS1_SHOWDIRTYSTATE=y &&
208 __git_ps1 >"$actual"
209 ) &&
210 test_cmp expected "$actual"
213 test_expect_success 'prompt - dirty status indicator - dirty index' '
214 printf " (master +)" >expected &&
215 echo "dirty" >file &&
216 test_when_finished "git reset --hard" &&
217 git add -u &&
219 GIT_PS1_SHOWDIRTYSTATE=y &&
220 __git_ps1 >"$actual"
221 ) &&
222 test_cmp expected "$actual"
225 test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
226 printf " (master *+)" >expected &&
227 echo "dirty index" >file &&
228 test_when_finished "git reset --hard" &&
229 git add -u &&
230 echo "dirty worktree" >file &&
232 GIT_PS1_SHOWDIRTYSTATE=y &&
233 __git_ps1 >"$actual"
234 ) &&
235 test_cmp expected "$actual"
238 test_expect_success 'prompt - dirty status indicator - before root commit' '
239 printf " (master #)" >expected &&
241 GIT_PS1_SHOWDIRTYSTATE=y &&
242 cd otherrepo &&
243 __git_ps1 >"$actual"
244 ) &&
245 test_cmp expected "$actual"
248 test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
249 printf " (master)" >expected &&
250 echo "dirty" >file &&
251 test_when_finished "git reset --hard" &&
252 test_config bash.showDirtyState false &&
254 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
255 __git_ps1 >"$actual"
256 ) &&
257 test_cmp expected "$actual"
260 test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
261 printf " (master)" >expected &&
262 echo "dirty" >file &&
263 test_when_finished "git reset --hard" &&
264 test_config bash.showDirtyState true &&
266 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
267 __git_ps1 >"$actual"
268 ) &&
269 test_cmp expected "$actual"
272 test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
273 printf " (master)" >expected &&
274 echo "dirty" >file &&
275 test_when_finished "git reset --hard" &&
276 test_config bash.showDirtyState false &&
278 GIT_PS1_SHOWDIRTYSTATE=y &&
279 __git_ps1 >"$actual"
280 ) &&
281 test_cmp expected "$actual"
284 test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
285 printf " (master *)" >expected &&
286 echo "dirty" >file &&
287 test_when_finished "git reset --hard" &&
288 test_config bash.showDirtyState true &&
290 GIT_PS1_SHOWDIRTYSTATE=y &&
291 __git_ps1 >"$actual"
292 ) &&
293 test_cmp expected "$actual"
296 test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
297 printf " (GIT_DIR!)" >expected &&
298 echo "dirty" >file &&
299 test_when_finished "git reset --hard" &&
301 GIT_PS1_SHOWDIRTYSTATE=y &&
302 cd .git &&
303 __git_ps1 >"$actual"
304 ) &&
305 test_cmp expected "$actual"
308 test_expect_success 'prompt - stash status indicator - no stash' '
309 printf " (master)" >expected &&
311 GIT_PS1_SHOWSTASHSTATE=y &&
312 __git_ps1 >"$actual"
313 ) &&
314 test_cmp expected "$actual"
317 test_expect_success 'prompt - stash status indicator - stash' '
318 printf " (master $)" >expected &&
319 echo 2 >file &&
320 git stash &&
321 test_when_finished "git stash drop" &&
323 GIT_PS1_SHOWSTASHSTATE=y &&
324 __git_ps1 >"$actual"
325 ) &&
326 test_cmp expected "$actual"
329 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
330 printf " (GIT_DIR!)" >expected &&
331 echo 2 >file &&
332 git stash &&
333 test_when_finished "git stash drop" &&
335 GIT_PS1_SHOWSTASHSTATE=y &&
336 cd .git &&
337 __git_ps1 >"$actual"
338 ) &&
339 test_cmp expected "$actual"
342 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
343 printf " (master)" >expected &&
345 GIT_PS1_SHOWUNTRACKEDFILES=y &&
346 cd otherrepo &&
347 __git_ps1 >"$actual"
348 ) &&
349 test_cmp expected "$actual"
352 test_expect_success 'prompt - untracked files status indicator - untracked files' '
353 printf " (master %%)" >expected &&
355 GIT_PS1_SHOWUNTRACKEDFILES=y &&
356 __git_ps1 >"$actual"
357 ) &&
358 test_cmp expected "$actual"
361 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
362 printf " (master)" >expected &&
363 test_config bash.showUntrackedFiles false &&
365 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
366 __git_ps1 >"$actual"
367 ) &&
368 test_cmp expected "$actual"
371 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
372 printf " (master)" >expected &&
373 test_config bash.showUntrackedFiles true &&
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 set with config disabled' '
382 printf " (master)" >expected &&
383 test_config bash.showUntrackedFiles false &&
385 GIT_PS1_SHOWUNTRACKEDFILES=y &&
386 __git_ps1 >"$actual"
387 ) &&
388 test_cmp expected "$actual"
391 test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
392 printf " (master %%)" >expected &&
393 test_config bash.showUntrackedFiles true &&
395 GIT_PS1_SHOWUNTRACKEDFILES=y &&
396 __git_ps1 >"$actual"
397 ) &&
398 test_cmp expected "$actual"
401 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
402 printf " (GIT_DIR!)" >expected &&
404 GIT_PS1_SHOWUNTRACKEDFILES=y &&
405 cd .git &&
406 __git_ps1 >"$actual"
407 ) &&
408 test_cmp expected "$actual"
411 test_expect_success 'prompt - format string starting with dash' '
412 printf -- "-master" >expected &&
413 __git_ps1 "-%s" >"$actual" &&
414 test_cmp expected "$actual"
417 test_done