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 - detached head' '
53 printf " ((%s...))" $(git log -1 --format="%h" b1^) >expected &&
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 &&
63 test_when_finished "git checkout master" &&
65 GIT_PS1_DESCRIBE_STYLE=contains &&
68 test_cmp expected "$actual"
71 test_expect_success
'prompt - describe detached head - branch' '
72 printf " ((b1~1))" >expected &&
74 test_when_finished "git checkout master" &&
76 GIT_PS1_DESCRIBE_STYLE=branch &&
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 &&
85 test_when_finished "git checkout master" &&
87 GIT_PS1_DESCRIBE_STYLE=describe &&
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 &&
107 test_cmp expected "$actual"
110 test_expect_success
'prompt - deep inside .git directory' '
111 printf " (GIT_DIR!)" >expected &&
113 cd .git/refs/heads &&
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" &&
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" &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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" &&
219 GIT_PS1_SHOWDIRTYSTATE=y &&
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" &&
230 echo "dirty worktree" >file &&
232 GIT_PS1_SHOWDIRTYSTATE=y &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
305 test_cmp expected "$actual"
308 test_expect_success
'prompt - stash status indicator - no stash' '
309 printf " (master)" >expected &&
311 GIT_PS1_SHOWSTASHSTATE=y &&
314 test_cmp expected "$actual"
317 test_expect_success
'prompt - stash status indicator - stash' '
318 printf " (master $)" >expected &&
321 test_when_finished "git stash drop" &&
323 GIT_PS1_SHOWSTASHSTATE=y &&
326 test_cmp expected "$actual"
329 test_expect_success
'prompt - stash status indicator - not shown inside .git directory' '
330 printf " (GIT_DIR!)" >expected &&
333 test_when_finished "git stash drop" &&
335 GIT_PS1_SHOWSTASHSTATE=y &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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 &&
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"