3 # Copyright (c) 2012 SZEDER Gábor
6 test_description
='test git-specific bash prompt functions'
10 .
"$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
12 actual
="$TRASH_DIRECTORY/actual"
14 test_expect_success
'setup for prompt tests' '
19 git commit -m initial &&
20 git tag -a -m msg1 t1 &&
23 git commit -m "second b1" file &&
25 git commit -m "third b1" file &&
26 git tag -a -m msg2 t2 &&
27 git checkout -b b2 master &&
29 git commit -m "second b2" file &&
31 git commit -m "another b2" file &&
33 git commit -m "yet another b2" file &&
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 &&
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 &&
72 test_when_finished "git checkout master" &&
74 GIT_PS1_DESCRIBE_STYLE=contains &&
77 test_cmp expected "$actual"
80 test_expect_success
'prompt - describe detached head - branch' '
81 printf " ((b1~1))" >expected &&
83 test_when_finished "git checkout master" &&
85 GIT_PS1_DESCRIBE_STYLE=branch &&
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 &&
94 test_when_finished "git checkout master" &&
96 GIT_PS1_DESCRIBE_STYLE=describe &&
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 &&
116 test_cmp expected "$actual"
119 test_expect_success
'prompt - deep inside .git directory' '
120 printf " (GIT_DIR!)" >expected &&
122 cd .git/refs/heads &&
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" &&
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" &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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" &&
228 GIT_PS1_SHOWDIRTYSTATE=y &&
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" &&
239 echo "dirty worktree" >file &&
241 GIT_PS1_SHOWDIRTYSTATE=y &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
314 test_cmp expected "$actual"
317 test_expect_success
'prompt - stash status indicator - no stash' '
318 printf " (master)" >expected &&
320 GIT_PS1_SHOWSTASHSTATE=y &&
323 test_cmp expected "$actual"
326 test_expect_success
'prompt - stash status indicator - stash' '
327 printf " (master $)" >expected &&
330 test_when_finished "git stash drop" &&
331 git pack-refs --all &&
333 GIT_PS1_SHOWSTASHSTATE=y &&
336 test_cmp expected "$actual"
339 test_expect_success
'prompt - stash status indicator - not shown inside .git directory' '
340 printf " (GIT_DIR!)" >expected &&
343 test_when_finished "git stash drop" &&
345 GIT_PS1_SHOWSTASHSTATE=y &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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"