t/README: add missing value for GIT_TEST_DEFAULT_REF_FORMAT
[git/gitster.git] / t / t3202-show-branch.sh
blob3b6dad0c4661505b6c26ee111eb351efd3d0caca
1 #!/bin/sh
3 test_description='test show-branch'
5 TEST_PASSES_SANITIZE_LEAK=true
6 . ./test-lib.sh
8 test_expect_success 'error descriptions on empty repository' '
9 current=$(git branch --show-current) &&
10 cat >expect <<-EOF &&
11 error: no commit on branch '\''$current'\'' yet
12 EOF
13 test_must_fail git branch --edit-description 2>actual &&
14 test_cmp expect actual &&
15 test_must_fail git branch --edit-description $current 2>actual &&
16 test_cmp expect actual
19 test_expect_success 'fatal descriptions on empty repository' '
20 current=$(git branch --show-current) &&
21 cat >expect <<-EOF &&
22 fatal: no commit on branch '\''$current'\'' yet
23 EOF
24 test_must_fail git branch --set-upstream-to=non-existent 2>actual &&
25 test_cmp expect actual &&
26 test_must_fail git branch -c new-branch 2>actual &&
27 test_cmp expect actual
30 test_expect_success 'setup' '
31 test_commit initial &&
32 for i in $(test_seq 1 10)
34 git checkout -b branch$i initial &&
35 test_commit --no-tag branch$i || return 1
36 done &&
37 git for-each-ref \
38 --sort=version:refname \
39 --format="%(refname:strip=2)" \
40 "refs/heads/branch*" >branches.sorted &&
41 sed "s/^> //" >expect <<-\EOF
42 > ! [branch1] branch1
43 > ! [branch2] branch2
44 > ! [branch3] branch3
45 > ! [branch4] branch4
46 > ! [branch5] branch5
47 > ! [branch6] branch6
48 > ! [branch7] branch7
49 > ! [branch8] branch8
50 > ! [branch9] branch9
51 > * [branch10] branch10
52 > ----------
53 > * [branch10] branch10
54 > + [branch9] branch9
55 > + [branch8] branch8
56 > + [branch7] branch7
57 > + [branch6] branch6
58 > + [branch5] branch5
59 > + [branch4] branch4
60 > + [branch3] branch3
61 > + [branch2] branch2
62 > + [branch1] branch1
63 > +++++++++* [branch10^] initial
64 EOF
67 test_expect_success 'show-branch with more than 8 branches' '
68 git show-branch $(cat branches.sorted) >actual &&
69 test_cmp expect actual
72 test_expect_success 'show-branch with showbranch.default' '
73 for branch in $(cat branches.sorted)
75 test_config showbranch.default $branch --add || return 1
76 done &&
77 git show-branch >actual &&
78 test_cmp expect actual
81 test_expect_success 'show-branch --color output' '
82 sed "s/^> //" >expect <<-\EOF &&
83 > <RED>!<RESET> [branch1] branch1
84 > <GREEN>!<RESET> [branch2] branch2
85 > <YELLOW>!<RESET> [branch3] branch3
86 > <BLUE>!<RESET> [branch4] branch4
87 > <MAGENTA>!<RESET> [branch5] branch5
88 > <CYAN>!<RESET> [branch6] branch6
89 > <BOLD;RED>!<RESET> [branch7] branch7
90 > <BOLD;GREEN>!<RESET> [branch8] branch8
91 > <BOLD;YELLOW>!<RESET> [branch9] branch9
92 > <BOLD;BLUE>*<RESET> [branch10] branch10
93 > ----------
94 > <BOLD;BLUE>*<RESET> [branch10] branch10
95 > <BOLD;YELLOW>+<RESET> [branch9] branch9
96 > <BOLD;GREEN>+<RESET> [branch8] branch8
97 > <BOLD;RED>+<RESET> [branch7] branch7
98 > <CYAN>+<RESET> [branch6] branch6
99 > <MAGENTA>+<RESET> [branch5] branch5
100 > <BLUE>+<RESET> [branch4] branch4
101 > <YELLOW>+<RESET> [branch3] branch3
102 > <GREEN>+<RESET> [branch2] branch2
103 > <RED>+<RESET> [branch1] branch1
104 > <RED>+<RESET><GREEN>+<RESET><YELLOW>+<RESET><BLUE>+<RESET><MAGENTA>+<RESET><CYAN>+<RESET><BOLD;RED>+<RESET><BOLD;GREEN>+<RESET><BOLD;YELLOW>+<RESET><BOLD;BLUE>*<RESET> [branch10^] initial
106 git show-branch --color=always $(cat branches.sorted) >actual.raw &&
107 test_decode_color <actual.raw >actual &&
108 test_cmp expect actual
111 test_expect_success 'show branch --remotes' '
112 cat >expect.err <<-\EOF &&
113 No revs to be shown.
115 git show-branch -r 2>actual.err >actual.out &&
116 test_cmp expect.err actual.err &&
117 test_must_be_empty actual.out
120 test_expect_success 'show-branch --sparse' '
121 test_when_finished "git checkout branch10 && git branch -D branchA" &&
122 git checkout -b branchA branch10 &&
123 git merge -s ours -m "merge 1 and 10 to make A" branch1 &&
124 git commit --allow-empty -m "another" &&
126 git show-branch --sparse >out &&
127 grep "merge 1 and 10 to make A" out &&
129 git show-branch >out &&
130 ! grep "merge 1 and 10 to make A" out &&
132 git show-branch --no-sparse >out &&
133 ! grep "merge 1 and 10 to make A" out
136 test_expect_success 'setup show branch --list' '
137 sed "s/^> //" >expect <<-\EOF
138 > [branch1] branch1
139 > [branch2] branch2
140 > [branch3] branch3
141 > [branch4] branch4
142 > [branch5] branch5
143 > [branch6] branch6
144 > [branch7] branch7
145 > [branch8] branch8
146 > [branch9] branch9
147 > * [branch10] branch10
151 test_expect_success 'show branch --list' '
152 git show-branch --list $(cat branches.sorted) >actual &&
153 test_cmp expect actual
156 test_expect_success 'show branch --list has no --color output' '
157 git show-branch --color=always --list $(cat branches.sorted) >actual &&
158 test_cmp expect actual
161 test_expect_success 'show branch --merge-base with one argument' '
162 for branch in $(cat branches.sorted)
164 git rev-parse $branch >expect &&
165 git show-branch --merge-base $branch >actual &&
166 test_cmp expect actual || return 1
167 done
170 test_expect_success 'show branch --merge-base with two arguments' '
171 for branch in $(cat branches.sorted)
173 git rev-parse initial >expect &&
174 git show-branch --merge-base initial $branch >actual &&
175 test_cmp expect actual || return 1
176 done
179 test_expect_success 'show branch --merge-base with N arguments' '
180 git rev-parse initial >expect &&
181 git show-branch --merge-base $(cat branches.sorted) >actual &&
182 test_cmp expect actual &&
184 git merge-base $(cat branches.sorted) >actual &&
185 test_cmp expect actual
188 # incompatible options
189 while read combo
191 test_expect_success "show-branch $combo (should fail)" '
192 test_must_fail git show-branch $combo 2>error &&
193 grep -e "cannot be used together" -e "usage:" error
195 done <<\EOF
196 --all --reflog
197 --merge-base --reflog
198 --list --merge-base
199 --reflog --current
202 # unnegatable options
203 for opt in topo-order date-order reflog
205 test_expect_success "show-branch --no-$opt (should fail)" '
206 test_must_fail git show-branch --no-$opt 2>err &&
207 grep "unknown option .no-$opt." err
209 done
211 test_expect_success 'error descriptions on non-existent branch' '
212 cat >expect <<-EOF &&
213 error: no branch named '\''non-existent'\''
215 test_must_fail git branch --edit-description non-existent 2>actual &&
216 test_cmp expect actual
219 test_expect_success 'fatal descriptions on non-existent branch' '
220 cat >expect <<-EOF &&
221 fatal: branch '\''non-existent'\'' does not exist
223 test_must_fail git branch --set-upstream-to=non-existent non-existent 2>actual &&
224 test_cmp expect actual &&
226 cat >expect <<-EOF &&
227 fatal: no branch named '\''non-existent'\''
229 test_must_fail git branch -c non-existent new-branch 2>actual &&
230 test_cmp expect actual &&
231 test_must_fail git branch -m non-existent new-branch 2>actual &&
232 test_cmp expect actual
235 test_expect_success 'error descriptions on orphan branch' '
236 test_when_finished git worktree remove -f wt &&
237 git worktree add wt --detach &&
238 git -C wt checkout --orphan orphan-branch &&
239 test_branch_op_in_wt() {
240 test_orphan_error() {
241 test_must_fail git $* 2>actual &&
242 test_grep "no commit on branch .orphan-branch. yet$" actual
243 } &&
244 test_orphan_error -C wt branch $1 $2 && # implicit branch
245 test_orphan_error -C wt branch $1 orphan-branch $2 && # explicit branch
246 test_orphan_error branch $1 orphan-branch $2 # different worktree
247 } &&
248 test_branch_op_in_wt --edit-description &&
249 test_branch_op_in_wt --set-upstream-to=ne &&
250 test_branch_op_in_wt -c new-branch
253 test_expect_success 'setup reflogs' '
254 test_commit base &&
255 git checkout -b branch &&
256 test_commit one &&
257 git reset --hard HEAD^ &&
258 test_commit two &&
259 test_commit three
262 test_expect_success '--reflog shows reflog entries' '
263 cat >expect <<-\EOF &&
264 ! [branch@{0}] (0 seconds ago) commit: three
265 ! [branch@{1}] (60 seconds ago) commit: two
266 ! [branch@{2}] (2 minutes ago) reset: moving to HEAD^
267 ! [branch@{3}] (2 minutes ago) commit: one
268 ----
269 + [branch@{0}] three
270 ++ [branch@{1}] two
271 + [branch@{3}] one
272 ++++ [branch@{2}] base
274 # the output always contains relative timestamps; use
275 # a known time to get deterministic results
276 GIT_TEST_DATE_NOW=$test_tick \
277 git show-branch --reflog branch >actual &&
278 test_cmp expect actual
281 test_expect_success '--reflog handles missing reflog' '
282 git reflog expire --expire=now branch &&
283 git show-branch --reflog branch >actual &&
284 test_must_be_empty actual
287 test_done