bash prompt: use 'write_script' helper in interactive rebase test
[git] / t / t9903-bash-prompt.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2012 SZEDER Gábor
4 #
5
6 test_description='test git-specific bash prompt functions'
7
8 . ./lib-bash.sh
9
10 . "$GIT_BUILD_DIR/contrib/completion/git-prompt.sh"
11
12 actual="$TRASH_DIRECTORY/actual"
13
14 test_expect_success 'setup for prompt tests' '
15         mkdir -p subdir/subsubdir &&
16         git init otherrepo &&
17         echo 1 >file &&
18         git add file &&
19         test_tick &&
20         git commit -m initial &&
21         git tag -a -m msg1 t1 &&
22         git checkout -b b1 &&
23         echo 2 >file &&
24         git commit -m "second b1" file &&
25         echo 3 >file &&
26         git commit -m "third b1" file &&
27         git tag -a -m msg2 t2 &&
28         git checkout -b b2 master &&
29         echo 0 >file &&
30         git commit -m "second b2" file &&
31         echo 00 >file &&
32         git commit -m "another b2" file &&
33         echo 000 >file &&
34         git commit -m "yet another b2" file &&
35         git checkout master
36 '
37
38 test_expect_success 'gitdir - from command line (through $__git_dir)' '
39         echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
40         (
41                 __git_dir="$TRASH_DIRECTORY/otherrepo/.git" &&
42                 __gitdir >"$actual"
43         ) &&
44         test_cmp expected "$actual"
45 '
46
47 test_expect_success 'gitdir - repo as argument' '
48         echo "otherrepo/.git" >expected &&
49         __gitdir "otherrepo" >"$actual" &&
50         test_cmp expected "$actual"
51 '
52
53 test_expect_success 'gitdir - remote as argument' '
54         echo "remote" >expected &&
55         __gitdir "remote" >"$actual" &&
56         test_cmp expected "$actual"
57 '
58
59 test_expect_success 'gitdir - .git directory in cwd' '
60         echo ".git" >expected &&
61         __gitdir >"$actual" &&
62         test_cmp expected "$actual"
63 '
64
65 test_expect_success 'gitdir - .git directory in parent' '
66         echo "$(pwd -P)/.git" >expected &&
67         (
68                 cd subdir/subsubdir &&
69                 __gitdir >"$actual"
70         ) &&
71         test_cmp expected "$actual"
72 '
73
74 test_expect_success 'gitdir - cwd is a .git directory' '
75         echo "." >expected &&
76         (
77                 cd .git &&
78                 __gitdir >"$actual"
79         ) &&
80         test_cmp expected "$actual"
81 '
82
83 test_expect_success 'gitdir - parent is a .git directory' '
84         echo "$(pwd -P)/.git" >expected &&
85         (
86                 cd .git/refs/heads &&
87                 __gitdir >"$actual"
88         ) &&
89         test_cmp expected "$actual"
90 '
91
92 test_expect_success 'gitdir - $GIT_DIR set while .git directory in cwd' '
93         echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
94         (
95                 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
96                 export GIT_DIR &&
97                 __gitdir >"$actual"
98         ) &&
99         test_cmp expected "$actual"
100 '
101
102 test_expect_success 'gitdir - $GIT_DIR set while .git directory in parent' '
103         echo "$TRASH_DIRECTORY/otherrepo/.git" >expected &&
104         (
105                 GIT_DIR="$TRASH_DIRECTORY/otherrepo/.git" &&
106                 export GIT_DIR &&
107                 cd subdir &&
108                 __gitdir >"$actual"
109         ) &&
110         test_cmp expected "$actual"
111 '
112
113 test_expect_success 'gitdir - non-existing $GIT_DIR' '
114         (
115                 GIT_DIR="$TRASH_DIRECTORY/non-existing" &&
116                 export GIT_DIR &&
117                 test_must_fail __gitdir
118         )
119 '
120
121 test_expect_success 'gitdir - gitfile in cwd' '
122         echo "$(pwd -P)/otherrepo/.git" >expected &&
123         echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" >subdir/.git &&
124         test_when_finished "rm -f subdir/.git" &&
125         (
126                 cd subdir &&
127                 __gitdir >"$actual"
128         ) &&
129         test_cmp expected "$actual"
130 '
131
132 test_expect_success 'gitdir - gitfile in parent' '
133         echo "$(pwd -P)/otherrepo/.git" >expected &&
134         echo "gitdir: $TRASH_DIRECTORY/otherrepo/.git" >subdir/.git &&
135         test_when_finished "rm -f subdir/.git" &&
136         (
137                 cd subdir/subsubdir &&
138                 __gitdir >"$actual"
139         ) &&
140         test_cmp expected "$actual"
141 '
142
143 test_expect_success SYMLINKS 'gitdir - resulting path avoids symlinks' '
144         echo "$(pwd -P)/otherrepo/.git" >expected &&
145         mkdir otherrepo/dir &&
146         test_when_finished "rm -rf otherrepo/dir" &&
147         ln -s otherrepo/dir link &&
148         test_when_finished "rm -f link" &&
149         (
150                 cd link &&
151                 __gitdir >"$actual"
152         ) &&
153         test_cmp expected "$actual"
154 '
155
156 test_expect_success 'gitdir - not a git repository' '
157         (
158                 cd subdir/subsubdir &&
159                 GIT_CEILING_DIRECTORIES="$TRASH_DIRECTORY" &&
160                 export GIT_CEILING_DIRECTORIES &&
161                 test_must_fail __gitdir
162         )
163 '
164
165 test_expect_success 'prompt - branch name' '
166         printf " (master)" >expected &&
167         __git_ps1 >"$actual" &&
168         test_cmp expected "$actual"
169 '
170
171 test_expect_success 'prompt - detached head' '
172         printf " ((%s...))" $(git log -1 --format="%h" b1^) >expected &&
173         git checkout b1^ &&
174         test_when_finished "git checkout master" &&
175         __git_ps1 >"$actual" &&
176         test_cmp expected "$actual"
177 '
178
179 test_expect_success 'prompt - describe detached head - contains' '
180         printf " ((t2~1))" >expected &&
181         git checkout b1^ &&
182         test_when_finished "git checkout master" &&
183         (
184                 GIT_PS1_DESCRIBE_STYLE=contains &&
185                 __git_ps1 >"$actual"
186         ) &&
187         test_cmp expected "$actual"
188 '
189
190 test_expect_success 'prompt - describe detached head - branch' '
191         printf " ((b1~1))" >expected &&
192         git checkout b1^ &&
193         test_when_finished "git checkout master" &&
194         (
195                 GIT_PS1_DESCRIBE_STYLE=branch &&
196                 __git_ps1 >"$actual"
197         ) &&
198         test_cmp expected "$actual"
199 '
200
201 test_expect_success 'prompt - describe detached head - describe' '
202         printf " ((t1-1-g%s))" $(git log -1 --format="%h" b1^) >expected &&
203         git checkout b1^ &&
204         test_when_finished "git checkout master" &&
205         (
206                 GIT_PS1_DESCRIBE_STYLE=describe &&
207                 __git_ps1 >"$actual"
208         ) &&
209         test_cmp expected "$actual"
210 '
211
212 test_expect_success 'prompt - describe detached head - default' '
213         printf " ((t2))" >expected &&
214         git checkout --detach b1 &&
215         test_when_finished "git checkout master" &&
216         __git_ps1 >"$actual" &&
217         test_cmp expected "$actual"
218 '
219
220 test_expect_success 'prompt - inside .git directory' '
221         printf " (GIT_DIR!)" >expected &&
222         (
223                 cd .git &&
224                 __git_ps1 >"$actual"
225         ) &&
226         test_cmp expected "$actual"
227 '
228
229 test_expect_success 'prompt - deep inside .git directory' '
230         printf " (GIT_DIR!)" >expected &&
231         (
232                 cd .git/refs/heads &&
233                 __git_ps1 >"$actual"
234         ) &&
235         test_cmp expected "$actual"
236 '
237
238 test_expect_success 'prompt - inside bare repository' '
239         printf " (BARE:master)" >expected &&
240         git init --bare bare.git &&
241         test_when_finished "rm -rf bare.git" &&
242         (
243                 cd bare.git &&
244                 __git_ps1 >"$actual"
245         ) &&
246         test_cmp expected "$actual"
247 '
248
249 test_expect_success 'prompt - interactive rebase' '
250         printf " (b1|REBASE-i 2/3)" >expected
251         write_script fake_editor.sh <<-\EOF &&
252                 echo "exec echo" >"$1"
253                 echo "edit $(git log -1 --format="%h")" >>"$1"
254                 echo "exec echo" >>"$1"
255         EOF
256         test_when_finished "rm -f fake_editor.sh" &&
257         test_set_editor "$TRASH_DIRECTORY/fake_editor.sh" &&
258         git checkout b1 &&
259         test_when_finished "git checkout master" &&
260         git rebase -i HEAD^ &&
261         test_when_finished "git rebase --abort"
262         __git_ps1 >"$actual" &&
263         test_cmp expected "$actual"
264 '
265
266 test_expect_success 'prompt - rebase merge' '
267         printf " (b2|REBASE-m 1/3)" >expected &&
268         git checkout b2 &&
269         test_when_finished "git checkout master" &&
270         test_must_fail git rebase --merge b1 b2 &&
271         test_when_finished "git rebase --abort" &&
272         __git_ps1 >"$actual" &&
273         test_cmp expected "$actual"
274 '
275
276 test_expect_success 'prompt - rebase' '
277         printf " (b2|REBASE 1/3)" >expected &&
278         git checkout b2 &&
279         test_when_finished "git checkout master" &&
280         test_must_fail git rebase b1 b2 &&
281         test_when_finished "git rebase --abort" &&
282         __git_ps1 >"$actual" &&
283         test_cmp expected "$actual"
284 '
285
286 test_expect_success 'prompt - merge' '
287         printf " (b1|MERGING)" >expected &&
288         git checkout b1 &&
289         test_when_finished "git checkout master" &&
290         test_must_fail git merge b2 &&
291         test_when_finished "git reset --hard" &&
292         __git_ps1 >"$actual" &&
293         test_cmp expected "$actual"
294 '
295
296 test_expect_success 'prompt - cherry-pick' '
297         printf " (master|CHERRY-PICKING)" >expected &&
298         test_must_fail git cherry-pick b1 &&
299         test_when_finished "git reset --hard" &&
300         __git_ps1 >"$actual" &&
301         test_cmp expected "$actual"
302 '
303
304 test_expect_success 'prompt - bisect' '
305         printf " (master|BISECTING)" >expected &&
306         git bisect start &&
307         test_when_finished "git bisect reset" &&
308         __git_ps1 >"$actual" &&
309         test_cmp expected "$actual"
310 '
311
312 test_expect_success 'prompt - dirty status indicator - clean' '
313         printf " (master)" >expected &&
314         (
315                 GIT_PS1_SHOWDIRTYSTATE=y &&
316                 __git_ps1 >"$actual"
317         ) &&
318         test_cmp expected "$actual"
319 '
320
321 test_expect_success 'prompt - dirty status indicator - dirty worktree' '
322         printf " (master *)" >expected &&
323         echo "dirty" >file &&
324         test_when_finished "git reset --hard" &&
325         (
326                 GIT_PS1_SHOWDIRTYSTATE=y &&
327                 __git_ps1 >"$actual"
328         ) &&
329         test_cmp expected "$actual"
330 '
331
332 test_expect_success 'prompt - dirty status indicator - dirty index' '
333         printf " (master +)" >expected &&
334         echo "dirty" >file &&
335         test_when_finished "git reset --hard" &&
336         git add -u &&
337         (
338                 GIT_PS1_SHOWDIRTYSTATE=y &&
339                 __git_ps1 >"$actual"
340         ) &&
341         test_cmp expected "$actual"
342 '
343
344 test_expect_success 'prompt - dirty status indicator - dirty index and worktree' '
345         printf " (master *+)" >expected &&
346         echo "dirty index" >file &&
347         test_when_finished "git reset --hard" &&
348         git add -u &&
349         echo "dirty worktree" >file &&
350         (
351                 GIT_PS1_SHOWDIRTYSTATE=y &&
352                 __git_ps1 >"$actual"
353         ) &&
354         test_cmp expected "$actual"
355 '
356
357 test_expect_success 'prompt - dirty status indicator - before root commit' '
358         printf " (master #)" >expected &&
359         (
360                 GIT_PS1_SHOWDIRTYSTATE=y &&
361                 cd otherrepo &&
362                 __git_ps1 >"$actual"
363         ) &&
364         test_cmp expected "$actual"
365 '
366
367 test_expect_success 'prompt - dirty status indicator - shell variable unset with config disabled' '
368         printf " (master)" >expected &&
369         echo "dirty" >file &&
370         test_when_finished "git reset --hard" &&
371         test_config bash.showDirtyState false &&
372         (
373                 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
374                 __git_ps1 >"$actual"
375         ) &&
376         test_cmp expected "$actual"
377 '
378
379 test_expect_success 'prompt - dirty status indicator - shell variable unset with config enabled' '
380         printf " (master)" >expected &&
381         echo "dirty" >file &&
382         test_when_finished "git reset --hard" &&
383         test_config bash.showDirtyState true &&
384         (
385                 sane_unset GIT_PS1_SHOWDIRTYSTATE &&
386                 __git_ps1 >"$actual"
387         ) &&
388         test_cmp expected "$actual"
389 '
390
391 test_expect_success 'prompt - dirty status indicator - shell variable set with config disabled' '
392         printf " (master)" >expected &&
393         echo "dirty" >file &&
394         test_when_finished "git reset --hard" &&
395         test_config bash.showDirtyState false &&
396         (
397                 GIT_PS1_SHOWDIRTYSTATE=y &&
398                 __git_ps1 >"$actual"
399         ) &&
400         test_cmp expected "$actual"
401 '
402
403 test_expect_success 'prompt - dirty status indicator - shell variable set with config enabled' '
404         printf " (master *)" >expected &&
405         echo "dirty" >file &&
406         test_when_finished "git reset --hard" &&
407         test_config bash.showDirtyState true &&
408         (
409                 GIT_PS1_SHOWDIRTYSTATE=y &&
410                 __git_ps1 >"$actual"
411         ) &&
412         test_cmp expected "$actual"
413 '
414
415 test_expect_success 'prompt - dirty status indicator - not shown inside .git directory' '
416         printf " (GIT_DIR!)" >expected &&
417         echo "dirty" >file &&
418         test_when_finished "git reset --hard" &&
419         (
420                 GIT_PS1_SHOWDIRTYSTATE=y &&
421                 cd .git &&
422                 __git_ps1 >"$actual"
423         ) &&
424         test_cmp expected "$actual"
425 '
426
427 test_expect_success 'prompt - stash status indicator - no stash' '
428         printf " (master)" >expected &&
429         (
430                 GIT_PS1_SHOWSTASHSTATE=y &&
431                 __git_ps1 >"$actual"
432         ) &&
433         test_cmp expected "$actual"
434 '
435
436 test_expect_success 'prompt - stash status indicator - stash' '
437         printf " (master $)" >expected &&
438         echo 2 >file &&
439         git stash &&
440         test_when_finished "git stash drop" &&
441         (
442                 GIT_PS1_SHOWSTASHSTATE=y &&
443                 __git_ps1 >"$actual"
444         ) &&
445         test_cmp expected "$actual"
446 '
447
448 test_expect_success 'prompt - stash status indicator - not shown inside .git directory' '
449         printf " (GIT_DIR!)" >expected &&
450         echo 2 >file &&
451         git stash &&
452         test_when_finished "git stash drop" &&
453         (
454                 GIT_PS1_SHOWSTASHSTATE=y &&
455                 cd .git &&
456                 __git_ps1 >"$actual"
457         ) &&
458         test_cmp expected "$actual"
459 '
460
461 test_expect_success 'prompt - untracked files status indicator - no untracked files' '
462         printf " (master)" >expected &&
463         (
464                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
465                 cd otherrepo &&
466                 __git_ps1 >"$actual"
467         ) &&
468         test_cmp expected "$actual"
469 '
470
471 test_expect_success 'prompt - untracked files status indicator - untracked files' '
472         printf " (master %%)" >expected &&
473         (
474                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
475                 __git_ps1 >"$actual"
476         ) &&
477         test_cmp expected "$actual"
478 '
479
480 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config disabled' '
481         printf " (master)" >expected &&
482         test_config bash.showUntrackedFiles false &&
483         (
484                 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
485                 __git_ps1 >"$actual"
486         ) &&
487         test_cmp expected "$actual"
488 '
489
490 test_expect_success 'prompt - untracked files status indicator - shell variable unset with config enabled' '
491         printf " (master)" >expected &&
492         test_config bash.showUntrackedFiles true &&
493         (
494                 sane_unset GIT_PS1_SHOWUNTRACKEDFILES &&
495                 __git_ps1 >"$actual"
496         ) &&
497         test_cmp expected "$actual"
498 '
499
500 test_expect_success 'prompt - untracked files status indicator - shell variable set with config disabled' '
501         printf " (master)" >expected &&
502         test_config bash.showUntrackedFiles false &&
503         (
504                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
505                 __git_ps1 >"$actual"
506         ) &&
507         test_cmp expected "$actual"
508 '
509
510 test_expect_success 'prompt - untracked files status indicator - shell variable set with config enabled' '
511         printf " (master %%)" >expected &&
512         test_config bash.showUntrackedFiles true &&
513         (
514                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
515                 __git_ps1 >"$actual"
516         ) &&
517         test_cmp expected "$actual"
518 '
519
520 test_expect_success 'prompt - untracked files status indicator - not shown inside .git directory' '
521         printf " (GIT_DIR!)" >expected &&
522         (
523                 GIT_PS1_SHOWUNTRACKEDFILES=y &&
524                 cd .git &&
525                 __git_ps1 >"$actual"
526         ) &&
527         test_cmp expected "$actual"
528 '
529
530 test_expect_success 'prompt - format string starting with dash' '
531         printf -- "-master" >expected &&
532         __git_ps1 "-%s" >"$actual" &&
533         test_cmp expected "$actual"
534 '
535
536 test_done