3 test_description
='test show-branch'
5 TEST_PASSES_SANITIZE_LEAK
=true
8 test_expect_success
'error descriptions on empty repository' '
9 current=$(git branch --show-current) &&
11 error: no commit on branch '\''$current'\'' yet
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) &&
22 fatal: no commit on branch '\''$current'\'' yet
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
38 --sort=version:refname \
39 --format="%(refname:strip=2)" \
40 "refs/heads/branch*" >branches.sorted &&
41 sed "s/^> //" >expect <<-\EOF
51 > * [branch10] branch10
53 > * [branch10] branch10
63 > +++++++++* [branch10^] initial
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
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
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 &&
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
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
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
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
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
197 --merge-base --reflog
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
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
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
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' '
255 git checkout -b branch &&
257 git reset --hard HEAD^ &&
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
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