bash prompt: print unique detached HEAD abbreviated object name
[git/gitweb.git] / t / t9903-bash-prompt.sh
blob0d53aa6d6948640d287bc514ae9b001cfbf71a42
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" --abbrev=13 b1^) >expected &&
54 test_config core.abbrev 13 &&
55 git checkout b1^ &&
56 test_when_finished "git checkout master" &&
57 __git_ps1 >"$actual" &&
58 test_cmp expected "$actual"
61 test_expect_success 'prompt - describe detached head - contains' '
62 printf " ((t2~1))" >expected &&
63 git checkout b1^ &&
64 test_when_finished "git checkout master" &&
66 GIT_PS1_DESCRIBE_STYLE=contains &&
67 __git_ps1 >"$actual"
68 ) &&
69 test_cmp expected "$actual"
72 test_expect_success 'prompt - describe detached head - branch' '
73 printf " ((b1~1))" >expected &&
74 git checkout b1^ &&
75 test_when_finished "git checkout master" &&
77 GIT_PS1_DESCRIBE_STYLE=branch &&
78 __git_ps1 >"$actual"
79 ) &&
80 test_cmp expected "$actual"
83 test_expect_success 'prompt - describe detached head - describe' '
84 printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
85 git checkout b1^ &&
86 test_when_finished "git checkout master" &&
88 GIT_PS1_DESCRIBE_STYLE=describe &&
89 __git_ps1 >"$actual"
90 ) &&
91 test_cmp expected "$actual"
94 test_expect_success 'prompt - describe detached head - default' '
95 printf " ((t2))" >expected &&
96 git checkout --detach b1 &&
97 test_when_finished "git checkout master" &&
98 __git_ps1 >"$actual" &&
99 test_cmp expected "$actual"
102 test_expect_success 'prompt - inside .git directory' '
103 printf " (GIT_DIR!)" >expected &&
105 cd .git &&
106 __git_ps1 >"$actual"
107 ) &&
108 test_cmp expected "$actual"
111 test_expect_success 'prompt - deep inside .git directory' '
112 printf " (GIT_DIR!)" >expected &&
114 cd .git/refs/heads &&
115 __git_ps1 >"$actual"
116 ) &&
117 test_cmp expected "$actual"
120 test_expect_success 'prompt - inside bare repository' '
121 printf " (BARE:master)" >expected &&
122 git init --bare bare.git &&
123 test_when_finished "rm -rf bare.git" &&
125 cd bare.git &&
126 __git_ps1 >"$actual"
127 ) &&
128 test_cmp expected "$actual"
131 test_expect_success 'prompt - interactive rebase' '
132 printf " (b1|REBASE-i 2/3)" >expected
133 write_script fake_editor.sh <<-\EOF &&
134 echo "exec echo" >"$1"
135 echo "edit $(git log -1 --format="%h")" >>"$1"
136 echo "exec echo" >>"$1"
138 test_when_finished "rm -f fake_editor.sh" &&
139 test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
140 git checkout b1 &&
141 test_when_finished "git checkout master" &&
142 git rebase -i HEAD^ &&
143 test_when_finished "git rebase --abort"
144 __git_ps1 >"$actual" &&
145 test_cmp expected "$actual"
148 test_expect_success 'prompt - rebase merge' '
149 printf " (b2|REBASE-m 1/3)" >expected &&
150 git checkout b2 &&
151 test_when_finished "git checkout master" &&
152 test_must_fail git rebase --merge b1 b2 &&
153 test_when_finished "git rebase --abort" &&
154 __git_ps1 >"$actual" &&
155 test_cmp expected "$actual"
158 test_expect_success 'prompt - rebase' '
159 printf " (b2|REBASE 1/3)" >expected &&
160 git checkout b2 &&
161 test_when_finished "git checkout master" &&
162 test_must_fail git rebase b1 b2 &&
163 test_when_finished "git rebase --abort" &&
164 __git_ps1 >"$actual" &&
165 test_cmp expected "$actual"
168 test_expect_success 'prompt - merge' '
169 printf " (b1|MERGING)" >expected &&
170 git checkout b1 &&
171 test_when_finished "git checkout master" &&
172 test_must_fail git merge b2 &&
173 test_when_finished "git reset --hard" &&
174 __git_ps1 >"$actual" &&
175 test_cmp expected "$actual"
178 test_expect_success 'prompt - cherry-pick' '
179 printf " (master|CHERRY-PICKING)" >expected &&
180 test_must_fail git cherry-pick b1 &&
181 test_when_finished "git reset --hard" &&
182 __git_ps1 >"$actual" &&
183 test_cmp expected "$actual"
186 test_expect_success 'prompt - bisect' '
187 printf " (master|BISECTING)" >expected &&
188 git bisect start &&
189 test_when_finished "git bisect reset" &&
190 __git_ps1 >"$actual" &&
191 test_cmp expected "$actual"
194 test_expect_success 'prompt - dirty status indicator - clean' '
195 printf " (master)" >expected &&
197 GIT_PS1_SHOWDIRTYSTATE=y &&
198 __git_ps1 >"$actual"
199 ) &&
200 test_cmp expected "$actual"
203 test_expect_success 'prompt - dirty status indicator - dirty worktree' '
204 printf " (master *)" >expected &&
205 echo "dirty" >file &&
206 test_when_finished "git reset --hard" &&
208 GIT_PS1_SHOWDIRTYSTATE=y &&
209 __git_ps1 >"$actual"
210 ) &&
211 test_cmp expected "$actual"
214 test_expect_success 'prompt - dirty status indicator - dirty index' '
215 printf " (master +)" >expected &&
216 echo "dirty" >file &&
217 test_when_finished "git reset --hard" &&
218 git add -u &&
220 GIT_PS1_SHOWDIRTYSTATE=y &&
221 __git_ps1 >"$actual"
222 ) &&
223 test_cmp expected "$actual"
226 test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
227 printf " (master *+)" >expected &&
228 echo "dirty index" >file &&
229 test_when_finished "git reset --hard" &&
230 git add -u &&
231 echo "dirty worktree" >file &&
233 GIT_PS1_SHOWDIRTYSTATE=y &&
234 __git_ps1 >"$actual"
235 ) &&
236 test_cmp expected "$actual"
239 test_expect_success 'prompt - dirty status indicator - before root commit' '
240 printf " (master #)" >expected &&
242 GIT_PS1_SHOWDIRTYSTATE=y &&
243 cd otherrepo &&
244 __git_ps1 >"$actual"
245 ) &&
246 test_cmp expected "$actual"
249 test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
250 printf " (master)" >expected &&
251 echo "dirty" >file &&
252 test_when_finished "git reset --hard" &&
253 test_config bash.showDirtyState false &&
255 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
256 __git_ps1 >"$actual"
257 ) &&
258 test_cmp expected "$actual"
261 test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
262 printf " (master)" >expected &&
263 echo "dirty" >file &&
264 test_when_finished "git reset --hard" &&
265 test_config bash.showDirtyState true &&
267 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
268 __git_ps1 >"$actual"
269 ) &&
270 test_cmp expected "$actual"
273 test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
274 printf " (master)" >expected &&
275 echo "dirty" >file &&
276 test_when_finished "git reset --hard" &&
277 test_config bash.showDirtyState false &&
279 GIT_PS1_SHOWDIRTYSTATE=y &&
280 __git_ps1 >"$actual"
281 ) &&
282 test_cmp expected "$actual"
285 test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
286 printf " (master *)" >expected &&
287 echo "dirty" >file &&
288 test_when_finished "git reset --hard" &&
289 test_config bash.showDirtyState true &&
291 GIT_PS1_SHOWDIRTYSTATE=y &&
292 __git_ps1 >"$actual"
293 ) &&
294 test_cmp expected "$actual"
297 test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
298 printf " (GIT_DIR!)" >expected &&
299 echo "dirty" >file &&
300 test_when_finished "git reset --hard" &&
302 GIT_PS1_SHOWDIRTYSTATE=y &&
303 cd .git &&
304 __git_ps1 >"$actual"
305 ) &&
306 test_cmp expected "$actual"
309 test_expect_success 'prompt - stash status indicator - no stash' '
310 printf " (master)" >expected &&
312 GIT_PS1_SHOWSTASHSTATE=y &&
313 __git_ps1 >"$actual"
314 ) &&
315 test_cmp expected "$actual"
318 test_expect_success 'prompt - stash status indicator - stash' '
319 printf " (master $)" >expected &&
320 echo 2 >file &&
321 git stash &&
322 test_when_finished "git stash drop" &&
324 GIT_PS1_SHOWSTASHSTATE=y &&
325 __git_ps1 >"$actual"
326 ) &&
327 test_cmp expected "$actual"
330 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
331 printf " (GIT_DIR!)" >expected &&
332 echo 2 >file &&
333 git stash &&
334 test_when_finished "git stash drop" &&
336 GIT_PS1_SHOWSTASHSTATE=y &&
337 cd .git &&
338 __git_ps1 >"$actual"
339 ) &&
340 test_cmp expected "$actual"
343 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
344 printf " (master)" >expected &&
346 GIT_PS1_SHOWUNTRACKEDFILES=y &&
347 cd otherrepo &&
348 __git_ps1 >"$actual"
349 ) &&
350 test_cmp expected "$actual"
353 test_expect_success 'prompt - untracked files status indicator - untracked files' '
354 printf " (master %%)" >expected &&
356 GIT_PS1_SHOWUNTRACKEDFILES=y &&
357 __git_ps1 >"$actual"
358 ) &&
359 test_cmp expected "$actual"
362 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
363 printf " (master)" >expected &&
364 test_config bash.showUntrackedFiles false &&
366 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
367 __git_ps1 >"$actual"
368 ) &&
369 test_cmp expected "$actual"
372 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
373 printf " (master)" >expected &&
374 test_config bash.showUntrackedFiles true &&
376 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
377 __git_ps1 >"$actual"
378 ) &&
379 test_cmp expected "$actual"
382 test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
383 printf " (master)" >expected &&
384 test_config bash.showUntrackedFiles false &&
386 GIT_PS1_SHOWUNTRACKEDFILES=y &&
387 __git_ps1 >"$actual"
388 ) &&
389 test_cmp expected "$actual"
392 test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
393 printf " (master %%)" >expected &&
394 test_config bash.showUntrackedFiles true &&
396 GIT_PS1_SHOWUNTRACKEDFILES=y &&
397 __git_ps1 >"$actual"
398 ) &&
399 test_cmp expected "$actual"
402 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
403 printf " (GIT_DIR!)" >expected &&
405 GIT_PS1_SHOWUNTRACKEDFILES=y &&
406 cd .git &&
407 __git_ps1 >"$actual"
408 ) &&
409 test_cmp expected "$actual"
412 test_expect_success 'prompt - format string starting with dash' '
413 printf -- "-master" >expected &&
414 __git_ps1 "-%s" >"$actual" &&
415 test_cmp expected "$actual"
418 test_done