]>
Commit | Line | Data |
---|---|---|
a3b427b9 AW |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2005 Amos Waterland | |
4 | # | |
5 | ||
c2d17ba3 | 6 | test_description='git branch assorted tests' |
a3b427b9 | 7 | |
d6c6b108 | 8 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
9 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
10 | ||
80d32e84 | 11 | TEST_PASSES_SANITIZE_LEAK=true |
a3b427b9 | 12 | . ./test-lib.sh |
1f537be3 | 13 | . "$TEST_DIRECTORY"/lib-rebase.sh |
a3b427b9 | 14 | |
0d158ebb RR |
15 | test_expect_success 'prepare a trivial repository' ' |
16 | echo Hello >A && | |
17 | git update-index --add A && | |
18 | git commit -m "Initial commit." && | |
ec9779bc | 19 | git branch -M main && |
0d158ebb RR |
20 | echo World >>A && |
21 | git update-index --add A && | |
22 | git commit -m "Second commit." && | |
140cd845 FC |
23 | HEAD=$(git rev-parse --verify HEAD) |
24 | ' | |
0d158ebb RR |
25 | |
26 | test_expect_success 'git branch --help should not have created a bogus branch' ' | |
01e8d327 | 27 | test_might_fail git branch --man --help </dev/null >/dev/null 2>&1 && |
0497e6c6 | 28 | test_ref_missing refs/heads/--help |
41ac414e | 29 | ' |
a3b427b9 | 30 | |
a6f43364 | 31 | test_expect_success REFFILES 'branch -h in broken repository' ' |
1dacfbcf NTND |
32 | mkdir broken && |
33 | ( | |
34 | cd broken && | |
ec9779bc JS |
35 | git init -b main && |
36 | >.git/refs/heads/main && | |
1dacfbcf NTND |
37 | test_expect_code 129 git branch -h >usage 2>&1 |
38 | ) && | |
6789275d | 39 | test_grep "[Uu]sage" broken/usage |
1dacfbcf NTND |
40 | ' |
41 | ||
0d158ebb | 42 | test_expect_success 'git branch abc should create a branch' ' |
0497e6c6 PS |
43 | git branch abc && |
44 | test_ref_exists refs/heads/abc | |
0d158ebb | 45 | ' |
08db81a9 | 46 | |
bc0893cf GC |
47 | test_expect_success 'git branch abc should fail when abc exists' ' |
48 | test_must_fail git branch abc | |
49 | ' | |
50 | ||
51 | test_expect_success 'git branch --force abc should fail when abc is checked out' ' | |
52 | test_when_finished git switch main && | |
53 | git switch abc && | |
54 | test_must_fail git branch --force abc HEAD~1 | |
55 | ' | |
56 | ||
57 | test_expect_success 'git branch --force abc should succeed when abc exists' ' | |
58 | git rev-parse HEAD~1 >expect && | |
59 | git branch --force abc HEAD~1 && | |
60 | git rev-parse abc >actual && | |
61 | test_cmp expect actual | |
62 | ' | |
63 | ||
0d158ebb | 64 | test_expect_success 'git branch a/b/c should create a branch' ' |
0497e6c6 PS |
65 | git branch a/b/c && |
66 | test_ref_exists refs/heads/a/b/c | |
0d158ebb | 67 | ' |
08db81a9 | 68 | |
ec9779bc | 69 | test_expect_success 'git branch mb main... should create a branch' ' |
0497e6c6 PS |
70 | git branch mb main... && |
71 | test_ref_exists refs/heads/mb | |
e3d6539d DL |
72 | ' |
73 | ||
0cb24fe8 JH |
74 | test_expect_success 'git branch HEAD should fail' ' |
75 | test_must_fail git branch HEAD | |
76 | ' | |
8efb8899 | 77 | |
d7fb7a37 | 78 | cat >expect <<EOF |
668e31c6 | 79 | $HEAD refs/heads/d/e/f@{0}: branch: Created from main |
d7fb7a37 | 80 | EOF |
7687f19e | 81 | test_expect_success 'git branch --create-reflog d/e/f should create a branch and a log' ' |
0d158ebb | 82 | GIT_COMMITTER_DATE="2005-05-26 23:30" \ |
7687f19e | 83 | git -c core.logallrefupdates=false branch --create-reflog d/e/f && |
0497e6c6 | 84 | test_ref_exists refs/heads/d/e/f && |
668e31c6 PS |
85 | git reflog show --no-abbrev-commit refs/heads/d/e/f >actual && |
86 | test_cmp expect actual | |
0d158ebb RR |
87 | ' |
88 | ||
89 | test_expect_success 'git branch -d d/e/f should delete a branch and a log' ' | |
90 | git branch -d d/e/f && | |
0497e6c6 | 91 | test_ref_missing refs/heads/d/e/f && |
d0ab0584 | 92 | test_must_fail git reflog exists refs/heads/d/e/f |
0d158ebb RR |
93 | ' |
94 | ||
95 | test_expect_success 'git branch j/k should work after branch j has been deleted' ' | |
96 | git branch j && | |
97 | git branch -d j && | |
98 | git branch j/k | |
99 | ' | |
100 | ||
101 | test_expect_success 'git branch l should work after branch l/m has been deleted' ' | |
102 | git branch l/m && | |
103 | git branch -d l/m && | |
104 | git branch l | |
105 | ' | |
106 | ||
107 | test_expect_success 'git branch -m dumps usage' ' | |
108 | test_expect_code 128 git branch -m 2>err && | |
6789275d | 109 | test_grep "branch name required" err |
0d158ebb RR |
110 | ' |
111 | ||
12fd3496 DT |
112 | test_expect_success 'git branch -m m broken_symref should work' ' |
113 | test_when_finished "git branch -D broken_symref" && | |
7687f19e | 114 | git branch --create-reflog m && |
12fd3496 DT |
115 | git symbolic-ref refs/heads/broken_symref refs/heads/i_am_broken && |
116 | git branch -m m broken_symref && | |
117 | git reflog exists refs/heads/broken_symref && | |
118 | test_must_fail git reflog exists refs/heads/i_am_broken | |
119 | ' | |
120 | ||
0d158ebb | 121 | test_expect_success 'git branch -m m m/m should work' ' |
7687f19e | 122 | git branch --create-reflog m && |
0d158ebb | 123 | git branch -m m m/m && |
d0ab0584 | 124 | git reflog exists refs/heads/m/m |
0d158ebb RR |
125 | ' |
126 | ||
127 | test_expect_success 'git branch -m n/n n should work' ' | |
7687f19e | 128 | git branch --create-reflog n/n && |
2f139044 | 129 | git branch -m n/n n && |
d0ab0584 | 130 | git reflog exists refs/heads/n |
0d158ebb | 131 | ' |
c976d415 | 132 | |
bb8efa17 SD |
133 | # The topmost entry in reflog for branch bbb is about branch creation. |
134 | # Hence, we compare bbb@{1} (instead of bbb@{0}) with aaa@{0}. | |
135 | ||
136 | test_expect_success 'git branch -m bbb should rename checked out branch' ' | |
137 | test_when_finished git branch -D bbb && | |
ec9779bc | 138 | test_when_finished git checkout main && |
bb8efa17 SD |
139 | git checkout -b aaa && |
140 | git commit --allow-empty -m "a new commit" && | |
141 | git rev-parse aaa@{0} >expect && | |
142 | git branch -m bbb && | |
143 | git rev-parse bbb@{1} >actual && | |
144 | test_cmp expect actual && | |
145 | git symbolic-ref HEAD >actual && | |
146 | echo refs/heads/bbb >expect && | |
147 | test_cmp expect actual | |
148 | ' | |
149 | ||
a1c1d817 JK |
150 | test_expect_success 'renaming checked out branch works with d/f conflict' ' |
151 | test_when_finished "git branch -D foo/bar || git branch -D foo" && | |
ec9779bc | 152 | test_when_finished git checkout main && |
a1c1d817 JK |
153 | git checkout -b foo && |
154 | git branch -m foo/bar && | |
155 | git symbolic-ref HEAD >actual && | |
156 | echo refs/heads/foo/bar >expect && | |
157 | test_cmp expect actual | |
158 | ' | |
159 | ||
41ac414e JH |
160 | test_expect_success 'git branch -m o/o o should fail when o/p exists' ' |
161 | git branch o/o && | |
0d158ebb | 162 | git branch o/p && |
d492b31c | 163 | test_must_fail git branch -m o/o o |
41ac414e | 164 | ' |
c976d415 | 165 | |
ff7aa81f MG |
166 | test_expect_success 'git branch -m o/q o/p should fail when o/p exists' ' |
167 | git branch o/q && | |
168 | test_must_fail git branch -m o/q o/p | |
169 | ' | |
170 | ||
171 | test_expect_success 'git branch -M o/q o/p should work when o/p exists' ' | |
172 | git branch -M o/q o/p | |
173 | ' | |
174 | ||
356e91f2 MG |
175 | test_expect_success 'git branch -m -f o/q o/p should work when o/p exists' ' |
176 | git branch o/q && | |
177 | git branch -m -f o/q o/p | |
178 | ' | |
179 | ||
41ac414e JH |
180 | test_expect_success 'git branch -m q r/q should fail when r exists' ' |
181 | git branch q && | |
182 | git branch r && | |
d492b31c | 183 | test_must_fail git branch -m q r/q |
41ac414e | 184 | ' |
c976d415 | 185 | |
55c4a673 CI |
186 | test_expect_success 'git branch -M foo bar should fail when bar is checked out' ' |
187 | git branch bar && | |
188 | git checkout -b foo && | |
189 | test_must_fail git branch -M bar foo | |
190 | ' | |
191 | ||
593a2a5d AK |
192 | test_expect_success 'git branch -M foo bar should fail when bar is checked out in worktree' ' |
193 | git branch -f bar && | |
194 | test_when_finished "git worktree remove wt && git branch -D wt" && | |
195 | git worktree add wt && | |
196 | test_must_fail git branch -M bar wt | |
197 | ' | |
198 | ||
55c4a673 CI |
199 | test_expect_success 'git branch -M baz bam should succeed when baz is checked out' ' |
200 | git checkout -b baz && | |
201 | git branch bam && | |
70999e9c KY |
202 | git branch -M baz bam && |
203 | test $(git rev-parse --abbrev-ref HEAD) = bam | |
204 | ' | |
205 | ||
668e31c6 PS |
206 | test_expect_success 'git branch -M baz bam should add entries to HEAD reflog' ' |
207 | git reflog show HEAD >actual && | |
208 | grep "HEAD@{0}: Branch: renamed refs/heads/baz to refs/heads/bam" actual | |
893dbf5b KM |
209 | ' |
210 | ||
31824d18 | 211 | test_expect_success 'git branch -M should leave orphaned HEAD alone' ' |
ec9779bc | 212 | git init -b main orphan && |
31824d18 NTND |
213 | ( |
214 | cd orphan && | |
215 | test_commit initial && | |
216 | git checkout --orphan lonely && | |
23937116 PS |
217 | git symbolic-ref HEAD >expect && |
218 | echo refs/heads/lonely >actual && | |
219 | test_cmp expect actual && | |
0497e6c6 | 220 | test_ref_missing refs/head/lonely && |
ec9779bc | 221 | git branch -M main mistress && |
23937116 PS |
222 | git symbolic-ref HEAD >expect && |
223 | test_cmp expect actual | |
31824d18 NTND |
224 | ) |
225 | ' | |
226 | ||
2272d3e5 JK |
227 | test_expect_success 'resulting reflog can be shown by log -g' ' |
228 | oid=$(git rev-parse HEAD) && | |
229 | cat >expect <<-EOF && | |
668e31c6 | 230 | HEAD@{0} $oid Branch: renamed refs/heads/baz to refs/heads/bam |
2272d3e5 JK |
231 | HEAD@{2} $oid checkout: moving from foo to baz |
232 | EOF | |
d08565bf | 233 | git log -g --format="%gd %H %gs" -2 HEAD >actual && |
2272d3e5 JK |
234 | test_cmp expect actual |
235 | ' | |
236 | ||
70999e9c | 237 | test_expect_success 'git branch -M baz bam should succeed when baz is checked out as linked working tree' ' |
ec9779bc | 238 | git checkout main && |
70999e9c KY |
239 | git worktree add -b baz bazdir && |
240 | git worktree add -f bazdir2 baz && | |
241 | git branch -M baz bam && | |
242 | test $(git -C bazdir rev-parse --abbrev-ref HEAD) = bam && | |
ab313814 NB |
243 | test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam && |
244 | rm -r bazdir bazdir2 && | |
245 | git worktree prune | |
70999e9c KY |
246 | ' |
247 | ||
a6f43364 | 248 | test_expect_success REFFILES 'git branch -M fails if updating any linked working tree fails' ' |
2e8af499 RJ |
249 | git worktree add -b baz bazdir1 && |
250 | git worktree add -f bazdir2 baz && | |
251 | touch .git/worktrees/bazdir1/HEAD.lock && | |
252 | test_must_fail git branch -M baz bam && | |
253 | test $(git -C bazdir2 rev-parse --abbrev-ref HEAD) = bam && | |
254 | git branch -M bam baz && | |
255 | rm .git/worktrees/bazdir1/HEAD.lock && | |
256 | touch .git/worktrees/bazdir2/HEAD.lock && | |
257 | test_must_fail git branch -M baz bam && | |
258 | test $(git -C bazdir1 rev-parse --abbrev-ref HEAD) = bam && | |
259 | rm -rf bazdir1 bazdir2 && | |
260 | git worktree prune | |
261 | ' | |
262 | ||
70999e9c KY |
263 | test_expect_success 'git branch -M baz bam should succeed within a worktree in which baz is checked out' ' |
264 | git checkout -b baz && | |
ab313814 | 265 | git worktree add -f bazdir baz && |
70999e9c | 266 | ( |
ab313814 | 267 | cd bazdir && |
70999e9c | 268 | git branch -M baz bam && |
0cd1a881 ÆAB |
269 | echo bam >expect && |
270 | git rev-parse --abbrev-ref HEAD >actual && | |
271 | test_cmp expect actual | |
70999e9c | 272 | ) && |
0cd1a881 ÆAB |
273 | echo bam >expect && |
274 | git rev-parse --abbrev-ref HEAD >actual && | |
275 | test_cmp expect actual && | |
ab313814 NB |
276 | rm -r bazdir && |
277 | git worktree prune | |
55c4a673 CI |
278 | ' |
279 | ||
ec9779bc JS |
280 | test_expect_success 'git branch -M main should work when main is checked out' ' |
281 | git checkout main && | |
282 | git branch -M main | |
3f59481e JN |
283 | ' |
284 | ||
ec9779bc JS |
285 | test_expect_success 'git branch -M main main should work when main is checked out' ' |
286 | git checkout main && | |
287 | git branch -M main main | |
3f59481e JN |
288 | ' |
289 | ||
ec9779bc JS |
290 | test_expect_success 'git branch -M topic topic should work when main is checked out' ' |
291 | git checkout main && | |
432f5e63 JS |
292 | git branch topic && |
293 | git branch -M topic topic | |
3f59481e JN |
294 | ' |
295 | ||
77e7267e RJ |
296 | test_expect_success 'git branch -M and -C fail on detached HEAD' ' |
297 | git checkout HEAD^{} && | |
298 | test_when_finished git checkout - && | |
12b99928 | 299 | echo "fatal: cannot rename the current branch while not on any" >expect && |
77e7267e RJ |
300 | test_must_fail git branch -M must-fail 2>err && |
301 | test_cmp expect err && | |
12b99928 | 302 | echo "fatal: cannot copy the current branch while not on any" >expect && |
77e7267e RJ |
303 | test_must_fail git branch -C must-fail 2>err && |
304 | test_cmp expect err | |
305 | ' | |
306 | ||
a675ad17 RJ |
307 | test_expect_success 'git branch -m should work with orphan branches' ' |
308 | test_when_finished git checkout - && | |
309 | test_when_finished git worktree remove -f wt && | |
310 | git worktree add wt --detach && | |
311 | # rename orphan in another worktreee | |
312 | git -C wt checkout --orphan orphan-foo-wt && | |
313 | git branch -m orphan-foo-wt orphan-bar-wt && | |
314 | test orphan-bar-wt=$(git -C orphan-worktree branch --show-current) && | |
315 | # rename orphan in the current worktree | |
316 | git checkout --orphan orphan-foo && | |
317 | git branch -m orphan-foo orphan-bar && | |
318 | test orphan-bar=$(git branch --show-current) | |
319 | ' | |
320 | ||
eb20e63f JK |
321 | test_expect_success 'git branch -d on orphan HEAD (merged)' ' |
322 | test_when_finished git checkout main && | |
323 | git checkout --orphan orphan && | |
324 | test_when_finished "rm -rf .git/objects/commit-graph*" && | |
325 | git commit-graph write --reachable && | |
326 | git branch --track to-delete main && | |
327 | git branch -d to-delete | |
328 | ' | |
329 | ||
330 | test_expect_success 'git branch -d on orphan HEAD (merged, graph)' ' | |
331 | test_when_finished git checkout main && | |
332 | git checkout --orphan orphan && | |
333 | git branch --track to-delete main && | |
334 | git branch -d to-delete | |
335 | ' | |
336 | ||
337 | test_expect_success 'git branch -d on orphan HEAD (unmerged)' ' | |
338 | test_when_finished git checkout main && | |
339 | git checkout --orphan orphan && | |
340 | test_when_finished "git branch -D to-delete" && | |
341 | git branch to-delete main && | |
342 | test_must_fail git branch -d to-delete 2>err && | |
343 | grep "not fully merged" err | |
344 | ' | |
345 | ||
346 | test_expect_success 'git branch -d on orphan HEAD (unmerged, graph)' ' | |
347 | test_when_finished git checkout main && | |
348 | git checkout --orphan orphan && | |
349 | test_when_finished "git branch -D to-delete" && | |
350 | git branch to-delete main && | |
351 | test_when_finished "rm -rf .git/objects/commit-graph*" && | |
352 | git commit-graph write --reachable && | |
353 | test_must_fail git branch -d to-delete 2>err && | |
354 | grep "not fully merged" err | |
355 | ' | |
356 | ||
cddd127b MG |
357 | test_expect_success 'git branch -v -d t should work' ' |
358 | git branch t && | |
cbc5cf7c | 359 | git rev-parse --verify refs/heads/t && |
cddd127b | 360 | git branch -v -d t && |
cbc5cf7c | 361 | test_must_fail git rev-parse --verify refs/heads/t |
cddd127b MG |
362 | ' |
363 | ||
364 | test_expect_success 'git branch -v -m t s should work' ' | |
365 | git branch t && | |
cbc5cf7c | 366 | git rev-parse --verify refs/heads/t && |
cddd127b | 367 | git branch -v -m t s && |
cbc5cf7c DT |
368 | test_must_fail git rev-parse --verify refs/heads/t && |
369 | git rev-parse --verify refs/heads/s && | |
cddd127b MG |
370 | git branch -d s |
371 | ' | |
372 | ||
373 | test_expect_success 'git branch -m -d t s should fail' ' | |
374 | git branch t && | |
cbc5cf7c | 375 | git rev-parse refs/heads/t && |
cddd127b MG |
376 | test_must_fail git branch -m -d t s && |
377 | git branch -d t && | |
cbc5cf7c | 378 | test_must_fail git rev-parse refs/heads/t |
cddd127b MG |
379 | ' |
380 | ||
381 | test_expect_success 'git branch --list -d t should fail' ' | |
382 | git branch t && | |
cbc5cf7c | 383 | git rev-parse refs/heads/t && |
cddd127b MG |
384 | test_must_fail git branch --list -d t && |
385 | git branch -d t && | |
cbc5cf7c | 386 | test_must_fail git rev-parse refs/heads/t |
cddd127b MG |
387 | ' |
388 | ||
f3534c98 JT |
389 | test_expect_success 'deleting checked-out branch from repo that is a submodule' ' |
390 | test_when_finished "rm -rf repo1 repo2" && | |
391 | ||
392 | git init repo1 && | |
393 | git init repo1/sub && | |
394 | test_commit -C repo1/sub x && | |
f8d510ed | 395 | test_config_global protocol.file.allow always && |
f3534c98 JT |
396 | git -C repo1 submodule add ./sub && |
397 | git -C repo1 commit -m "adding sub" && | |
398 | ||
399 | git clone --recurse-submodules repo1 repo2 && | |
400 | git -C repo2/sub checkout -b work && | |
401 | test_must_fail git -C repo2/sub branch -D work | |
402 | ' | |
403 | ||
404 | test_expect_success 'bare main worktree has HEAD at branch deleted by secondary worktree' ' | |
405 | test_when_finished "rm -rf nonbare base secondary" && | |
406 | ||
ec9779bc | 407 | git init -b main nonbare && |
f3534c98 JT |
408 | test_commit -C nonbare x && |
409 | git clone --bare nonbare bare && | |
ec9779bc JS |
410 | git -C bare worktree add --detach ../secondary main && |
411 | git -C secondary branch -D main | |
f3534c98 JT |
412 | ' |
413 | ||
ac5bbc02 JH |
414 | test_expect_success 'git branch --list -v with --abbrev' ' |
415 | test_when_finished "git branch -D t" && | |
416 | git branch t && | |
417 | git branch -v --list t >actual.default && | |
418 | git branch -v --list --abbrev t >actual.abbrev && | |
419 | test_cmp actual.default actual.abbrev && | |
420 | ||
421 | git branch -v --list --no-abbrev t >actual.noabbrev && | |
422 | git branch -v --list --abbrev=0 t >actual.0abbrev && | |
a9ecaa06 | 423 | git -c core.abbrev=no branch -v --list t >actual.noabbrev-conf && |
ac5bbc02 | 424 | test_cmp actual.noabbrev actual.0abbrev && |
a9ecaa06 | 425 | test_cmp actual.noabbrev actual.noabbrev-conf && |
ac5bbc02 JH |
426 | |
427 | git branch -v --list --abbrev=36 t >actual.36abbrev && | |
428 | # how many hexdigits are used? | |
429 | read name objdefault rest <actual.abbrev && | |
430 | read name obj36 rest <actual.36abbrev && | |
431 | objfull=$(git rev-parse --verify t) && | |
432 | ||
433 | # are we really getting abbreviations? | |
434 | test "$obj36" != "$objdefault" && | |
435 | expr "$obj36" : "$objdefault" >/dev/null && | |
436 | test "$objfull" != "$obj36" && | |
437 | expr "$objfull" : "$obj36" >/dev/null | |
438 | ||
439 | ' | |
440 | ||
ec9779bc | 441 | test_expect_success 'git branch --column' ' |
ebe31ef2 | 442 | COLUMNS=81 git branch --column=column >actual && |
6d504d5b | 443 | cat >expect <<\EOF && |
66713e84 JS |
444 | a/b/c bam foo l * main n o/p r |
445 | abc bar j/k m/m mb o/o q topic | |
ebe31ef2 | 446 | EOF |
6d504d5b | 447 | test_cmp expect actual |
ebe31ef2 NTND |
448 | ' |
449 | ||
450 | test_expect_success 'git branch --column with an extremely long branch name' ' | |
451 | long=this/is/a/part/of/long/branch/name && | |
452 | long=z$long/$long/$long/$long && | |
453 | test_when_finished "git branch -d $long" && | |
454 | git branch $long && | |
455 | COLUMNS=80 git branch --column=column >actual && | |
6d504d5b | 456 | cat >expect <<EOF && |
ebe31ef2 NTND |
457 | a/b/c |
458 | abc | |
459 | bam | |
460 | bar | |
461 | foo | |
462 | j/k | |
463 | l | |
464 | m/m | |
ec9779bc | 465 | * main |
e3d6539d | 466 | mb |
ebe31ef2 NTND |
467 | n |
468 | o/o | |
469 | o/p | |
470 | q | |
471 | r | |
432f5e63 | 472 | topic |
ebe31ef2 NTND |
473 | $long |
474 | EOF | |
6d504d5b | 475 | test_cmp expect actual |
ebe31ef2 NTND |
476 | ' |
477 | ||
ec9779bc | 478 | test_expect_success 'git branch with column.*' ' |
ebe31ef2 NTND |
479 | git config column.ui column && |
480 | git config column.branch "dense" && | |
481 | COLUMNS=80 git branch >actual && | |
482 | git config --unset column.branch && | |
483 | git config --unset column.ui && | |
6d504d5b | 484 | cat >expect <<\EOF && |
66713e84 JS |
485 | a/b/c bam foo l * main n o/p r |
486 | abc bar j/k m/m mb o/o q topic | |
ebe31ef2 | 487 | EOF |
6d504d5b | 488 | test_cmp expect actual |
ebe31ef2 NTND |
489 | ' |
490 | ||
491 | test_expect_success 'git branch --column -v should fail' ' | |
492 | test_must_fail git branch --column -v | |
493 | ' | |
494 | ||
ec9779bc | 495 | test_expect_success 'git branch -v with column.ui ignored' ' |
ebe31ef2 | 496 | git config column.ui column && |
56300ff3 | 497 | COLUMNS=80 git branch -v | cut -c -8 | sed "s/ *$//" >actual && |
ebe31ef2 | 498 | git config --unset column.ui && |
6d504d5b | 499 | cat >expect <<\EOF && |
ebe31ef2 NTND |
500 | a/b/c |
501 | abc | |
502 | bam | |
503 | bar | |
504 | foo | |
505 | j/k | |
506 | l | |
507 | m/m | |
ec9779bc | 508 | * main |
e3d6539d | 509 | mb |
ebe31ef2 NTND |
510 | n |
511 | o/o | |
512 | o/p | |
513 | q | |
514 | r | |
432f5e63 | 515 | topic |
ebe31ef2 | 516 | EOF |
6d504d5b | 517 | test_cmp expect actual |
ebe31ef2 NTND |
518 | ' |
519 | ||
01ebb9dc GB |
520 | mv .git/config .git/config-saved |
521 | ||
bb0372c9 | 522 | test_expect_success DEFAULT_REPO_FORMAT 'git branch -m q q2 without config should succeed' ' |
5be60078 JH |
523 | git branch -m q q2 && |
524 | git branch -m q2 q | |
01ebb9dc GB |
525 | ' |
526 | ||
527 | mv .git/config-saved .git/config | |
528 | ||
5be60078 | 529 | git config branch.s/s.dummy Hello |
dc81c58c | 530 | |
0d158ebb | 531 | test_expect_success 'git branch -m s/s s should work when s/t is deleted' ' |
7687f19e | 532 | git branch --create-reflog s/s && |
d0ab0584 | 533 | git reflog exists refs/heads/s/s && |
7687f19e | 534 | git branch --create-reflog s/t && |
d0ab0584 | 535 | git reflog exists refs/heads/s/t && |
0d158ebb RR |
536 | git branch -d s/t && |
537 | git branch -m s/s s && | |
d0ab0584 | 538 | git reflog exists refs/heads/s |
0d158ebb | 539 | ' |
c976d415 | 540 | |
0d158ebb RR |
541 | test_expect_success 'config information was renamed, too' ' |
542 | test $(git config branch.s.dummy) = Hello && | |
b8f354f2 | 543 | test_must_fail git config branch.s/s.dummy |
0d158ebb | 544 | ' |
dc81c58c | 545 | |
c8b2cec0 | 546 | test_expect_success 'git branch -m correctly renames multiple config sections' ' |
ec9779bc JS |
547 | test_when_finished "git checkout main" && |
548 | git checkout -b source main && | |
c8b2cec0 ÆAB |
549 | |
550 | # Assert that a config file with multiple config sections has | |
551 | # those sections preserved... | |
552 | cat >expect <<-\EOF && | |
553 | branch.dest.key1=value1 | |
554 | some.gar.b=age | |
555 | branch.dest.key2=value2 | |
556 | EOF | |
557 | cat >config.branch <<\EOF && | |
558 | ;; Note the lack of -\EOF above & mixed indenting here. This is | |
559 | ;; intentional, we are also testing that the formatting of copied | |
560 | ;; sections is preserved. | |
561 | ||
562 | ;; Comment for source. Tabs | |
563 | [branch "source"] | |
564 | ;; Comment for the source value | |
565 | key1 = value1 | |
566 | ;; Comment for some.gar. Spaces | |
567 | [some "gar"] | |
568 | ;; Comment for the some.gar value | |
569 | b = age | |
570 | ;; Comment for source, again. Mixed tabs/spaces. | |
571 | [branch "source"] | |
572 | ;; Comment for the source value, again | |
573 | key2 = value2 | |
574 | EOF | |
575 | cat config.branch >>.git/config && | |
576 | git branch -m source dest && | |
577 | git config -f .git/config -l | grep -F -e source -e dest -e some.gar >actual && | |
578 | test_cmp expect actual && | |
579 | ||
580 | # ...and that the comments for those sections are also | |
581 | # preserved. | |
582 | cat config.branch | sed "s/\"source\"/\"dest\"/" >expect && | |
583 | sed -n -e "/Note the lack/,\$p" .git/config >actual && | |
584 | test_cmp expect actual | |
585 | ' | |
586 | ||
52d59cc6 SD |
587 | test_expect_success 'git branch -c dumps usage' ' |
588 | test_expect_code 128 git branch -c 2>err && | |
6789275d | 589 | test_grep "branch name required" err |
52d59cc6 SD |
590 | ' |
591 | ||
592 | test_expect_success 'git branch --copy dumps usage' ' | |
593 | test_expect_code 128 git branch --copy 2>err && | |
6789275d | 594 | test_grep "branch name required" err |
52d59cc6 SD |
595 | ' |
596 | ||
597 | test_expect_success 'git branch -c d e should work' ' | |
7687f19e | 598 | git branch --create-reflog d && |
52d59cc6 SD |
599 | git reflog exists refs/heads/d && |
600 | git config branch.d.dummy Hello && | |
601 | git branch -c d e && | |
602 | git reflog exists refs/heads/d && | |
603 | git reflog exists refs/heads/e && | |
604 | echo Hello >expect && | |
605 | git config branch.e.dummy >actual && | |
606 | test_cmp expect actual && | |
607 | echo Hello >expect && | |
608 | git config branch.d.dummy >actual && | |
609 | test_cmp expect actual | |
610 | ' | |
611 | ||
612 | test_expect_success 'git branch --copy is a synonym for -c' ' | |
7687f19e | 613 | git branch --create-reflog copy && |
52d59cc6 SD |
614 | git reflog exists refs/heads/copy && |
615 | git config branch.copy.dummy Hello && | |
616 | git branch --copy copy copy-to && | |
617 | git reflog exists refs/heads/copy && | |
618 | git reflog exists refs/heads/copy-to && | |
619 | echo Hello >expect && | |
620 | git config branch.copy.dummy >actual && | |
621 | test_cmp expect actual && | |
622 | echo Hello >expect && | |
623 | git config branch.copy-to.dummy >actual && | |
624 | test_cmp expect actual | |
625 | ' | |
626 | ||
e5435ff1 | 627 | test_expect_success 'git branch -c ee ef should copy ee to create branch ef' ' |
52d59cc6 SD |
628 | git checkout -b ee && |
629 | git reflog exists refs/heads/ee && | |
630 | git config branch.ee.dummy Hello && | |
631 | git branch -c ee ef && | |
632 | git reflog exists refs/heads/ee && | |
633 | git reflog exists refs/heads/ef && | |
634 | test $(git config branch.ee.dummy) = Hello && | |
635 | test $(git config branch.ef.dummy) = Hello && | |
e5435ff1 | 636 | test $(git rev-parse --abbrev-ref HEAD) = ee |
52d59cc6 SD |
637 | ' |
638 | ||
639 | test_expect_success 'git branch -c f/f g/g should work' ' | |
7687f19e | 640 | git branch --create-reflog f/f && |
52d59cc6 SD |
641 | git reflog exists refs/heads/f/f && |
642 | git config branch.f/f.dummy Hello && | |
643 | git branch -c f/f g/g && | |
644 | git reflog exists refs/heads/f/f && | |
645 | git reflog exists refs/heads/g/g && | |
646 | test $(git config branch.f/f.dummy) = Hello && | |
647 | test $(git config branch.g/g.dummy) = Hello | |
648 | ' | |
649 | ||
650 | test_expect_success 'git branch -c m2 m2 should work' ' | |
7687f19e | 651 | git branch --create-reflog m2 && |
52d59cc6 SD |
652 | git reflog exists refs/heads/m2 && |
653 | git config branch.m2.dummy Hello && | |
654 | git branch -c m2 m2 && | |
655 | git reflog exists refs/heads/m2 && | |
656 | test $(git config branch.m2.dummy) = Hello | |
657 | ' | |
658 | ||
659 | test_expect_success 'git branch -c zz zz/zz should fail' ' | |
7687f19e | 660 | git branch --create-reflog zz && |
52d59cc6 SD |
661 | git reflog exists refs/heads/zz && |
662 | test_must_fail git branch -c zz zz/zz | |
663 | ' | |
664 | ||
665 | test_expect_success 'git branch -c b/b b should fail' ' | |
7687f19e | 666 | git branch --create-reflog b/b && |
52d59cc6 SD |
667 | test_must_fail git branch -c b/b b |
668 | ' | |
669 | ||
670 | test_expect_success 'git branch -C o/q o/p should work when o/p exists' ' | |
7687f19e | 671 | git branch --create-reflog o/q && |
52d59cc6 SD |
672 | git reflog exists refs/heads/o/q && |
673 | git reflog exists refs/heads/o/p && | |
674 | git branch -C o/q o/p | |
675 | ' | |
676 | ||
677 | test_expect_success 'git branch -c -f o/q o/p should work when o/p exists' ' | |
678 | git reflog exists refs/heads/o/q && | |
679 | git reflog exists refs/heads/o/p && | |
680 | git branch -c -f o/q o/p | |
681 | ' | |
682 | ||
40c17eb1 | 683 | test_expect_success 'git branch -c qq rr/qq should fail when rr exists' ' |
52d59cc6 SD |
684 | git branch qq && |
685 | git branch rr && | |
686 | test_must_fail git branch -c qq rr/qq | |
687 | ' | |
688 | ||
689 | test_expect_success 'git branch -C b1 b2 should fail when b2 is checked out' ' | |
690 | git branch b1 && | |
691 | git checkout -b b2 && | |
692 | test_must_fail git branch -C b1 b2 | |
693 | ' | |
694 | ||
695 | test_expect_success 'git branch -C c1 c2 should succeed when c1 is checked out' ' | |
696 | git checkout -b c1 && | |
697 | git branch c2 && | |
698 | git branch -C c1 c2 && | |
e5435ff1 | 699 | test $(git rev-parse --abbrev-ref HEAD) = c1 |
52d59cc6 SD |
700 | ' |
701 | ||
e5435ff1 | 702 | test_expect_success 'git branch -C c1 c2 should never touch HEAD' ' |
52d59cc6 | 703 | msg="Branch: copied refs/heads/c1 to refs/heads/c2" && |
668e31c6 PS |
704 | git reflog HEAD >actual && |
705 | ! grep "$msg$" actual | |
52d59cc6 SD |
706 | ' |
707 | ||
ec9779bc JS |
708 | test_expect_success 'git branch -C main should work when main is checked out' ' |
709 | git checkout main && | |
710 | git branch -C main | |
52d59cc6 SD |
711 | ' |
712 | ||
ec9779bc JS |
713 | test_expect_success 'git branch -C main main should work when main is checked out' ' |
714 | git checkout main && | |
715 | git branch -C main main | |
52d59cc6 SD |
716 | ' |
717 | ||
ec9779bc JS |
718 | test_expect_success 'git branch -C main5 main5 should work when main is checked out' ' |
719 | git checkout main && | |
432f5e63 JS |
720 | git branch main5 && |
721 | git branch -C main5 main5 | |
52d59cc6 SD |
722 | ' |
723 | ||
724 | test_expect_success 'git branch -C ab cd should overwrite existing config for cd' ' | |
7687f19e | 725 | git branch --create-reflog cd && |
52d59cc6 SD |
726 | git reflog exists refs/heads/cd && |
727 | git config branch.cd.dummy CD && | |
7687f19e | 728 | git branch --create-reflog ab && |
52d59cc6 SD |
729 | git reflog exists refs/heads/ab && |
730 | git config branch.ab.dummy AB && | |
731 | git branch -C ab cd && | |
732 | git reflog exists refs/heads/ab && | |
733 | git reflog exists refs/heads/cd && | |
734 | test $(git config branch.ab.dummy) = AB && | |
735 | test $(git config branch.cd.dummy) = AB | |
736 | ' | |
737 | ||
738 | test_expect_success 'git branch -c correctly copies multiple config sections' ' | |
739 | FOO=1 && | |
740 | export FOO && | |
ec9779bc JS |
741 | test_when_finished "git checkout main" && |
742 | git checkout -b source2 main && | |
52d59cc6 SD |
743 | |
744 | # Assert that a config file with multiple config sections has | |
745 | # those sections preserved... | |
746 | cat >expect <<-\EOF && | |
747 | branch.source2.key1=value1 | |
748 | branch.dest2.key1=value1 | |
749 | more.gar.b=age | |
750 | branch.source2.key2=value2 | |
751 | branch.dest2.key2=value2 | |
752 | EOF | |
753 | cat >config.branch <<\EOF && | |
754 | ;; Note the lack of -\EOF above & mixed indenting here. This is | |
755 | ;; intentional, we are also testing that the formatting of copied | |
756 | ;; sections is preserved. | |
757 | ||
758 | ;; Comment for source2. Tabs | |
759 | [branch "source2"] | |
760 | ;; Comment for the source2 value | |
761 | key1 = value1 | |
762 | ;; Comment for more.gar. Spaces | |
763 | [more "gar"] | |
764 | ;; Comment for the more.gar value | |
765 | b = age | |
766 | ;; Comment for source2, again. Mixed tabs/spaces. | |
767 | [branch "source2"] | |
768 | ;; Comment for the source2 value, again | |
769 | key2 = value2 | |
770 | EOF | |
771 | cat config.branch >>.git/config && | |
772 | git branch -c source2 dest2 && | |
773 | git config -f .git/config -l | grep -F -e source2 -e dest2 -e more.gar >actual && | |
774 | test_cmp expect actual && | |
775 | ||
776 | # ...and that the comments and formatting for those sections | |
777 | # is also preserved. | |
778 | cat >expect <<\EOF && | |
779 | ;; Comment for source2. Tabs | |
780 | [branch "source2"] | |
781 | ;; Comment for the source2 value | |
782 | key1 = value1 | |
783 | ;; Comment for more.gar. Spaces | |
784 | [branch "dest2"] | |
785 | ;; Comment for the source2 value | |
786 | key1 = value1 | |
787 | ;; Comment for more.gar. Spaces | |
788 | [more "gar"] | |
789 | ;; Comment for the more.gar value | |
790 | b = age | |
791 | ;; Comment for source2, again. Mixed tabs/spaces. | |
792 | [branch "source2"] | |
793 | ;; Comment for the source2 value, again | |
794 | key2 = value2 | |
795 | [branch "dest2"] | |
796 | ;; Comment for the source2 value, again | |
797 | key2 = value2 | |
798 | EOF | |
799 | sed -n -e "/Comment for source2/,\$p" .git/config >actual && | |
800 | test_cmp expect actual | |
801 | ' | |
802 | ||
566c7707 RS |
803 | test_expect_success 'deleting a symref' ' |
804 | git branch target && | |
805 | git symbolic-ref refs/heads/symref refs/heads/target && | |
13baa9fe | 806 | echo "Deleted branch symref (was refs/heads/target)." >expect && |
566c7707 | 807 | git branch -d symref >actual && |
0497e6c6 PS |
808 | test_ref_exists refs/heads/target && |
809 | test_ref_missing refs/heads/symref && | |
1108cea7 | 810 | test_cmp expect actual |
566c7707 RS |
811 | ' |
812 | ||
0fe700e3 RS |
813 | test_expect_success 'deleting a dangling symref' ' |
814 | git symbolic-ref refs/heads/dangling-symref nowhere && | |
23937116 | 815 | git symbolic-ref --no-recurse refs/heads/dangling-symref && |
13baa9fe | 816 | echo "Deleted branch dangling-symref (was nowhere)." >expect && |
0fe700e3 | 817 | git branch -d dangling-symref >actual && |
0497e6c6 | 818 | test_ref_missing refs/heads/dangling-symref && |
1108cea7 | 819 | test_cmp expect actual |
0fe700e3 RS |
820 | ' |
821 | ||
62a2d525 JN |
822 | test_expect_success 'deleting a self-referential symref' ' |
823 | git symbolic-ref refs/heads/self-reference refs/heads/self-reference && | |
0497e6c6 | 824 | test_ref_exists refs/heads/self-reference && |
62a2d525 JN |
825 | echo "Deleted branch self-reference (was refs/heads/self-reference)." >expect && |
826 | git branch -d self-reference >actual && | |
0497e6c6 | 827 | test_ref_missing refs/heads/self-reference && |
1108cea7 | 828 | test_cmp expect actual |
62a2d525 JN |
829 | ' |
830 | ||
0d158ebb | 831 | test_expect_success 'renaming a symref is not allowed' ' |
ec9779bc | 832 | git symbolic-ref refs/heads/topic refs/heads/main && |
432f5e63 JS |
833 | test_must_fail git branch -m topic new-topic && |
834 | git symbolic-ref refs/heads/topic && | |
0497e6c6 PS |
835 | test_ref_exists refs/heads/main && |
836 | test_ref_missing refs/heads/new-topic | |
eca35a25 MV |
837 | ' |
838 | ||
0d158ebb RR |
839 | test_expect_success 'test tracking setup via --track' ' |
840 | git config remote.local.url . && | |
841 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc JS |
842 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
843 | git branch --track my1 local/main && | |
0d158ebb | 844 | test $(git config branch.my1.remote) = local && |
ec9779bc | 845 | test $(git config branch.my1.merge) = refs/heads/main |
0d158ebb RR |
846 | ' |
847 | ||
848 | test_expect_success 'test tracking setup (non-wildcard, matching)' ' | |
849 | git config remote.local.url . && | |
ec9779bc JS |
850 | git config remote.local.fetch refs/heads/main:refs/remotes/local/main && |
851 | (git show-ref -q refs/remotes/local/main || git fetch local) && | |
852 | git branch --track my4 local/main && | |
0d158ebb | 853 | test $(git config branch.my4.remote) = local && |
ec9779bc | 854 | test $(git config branch.my4.merge) = refs/heads/main |
0d158ebb RR |
855 | ' |
856 | ||
41c21f22 | 857 | test_expect_success 'tracking setup fails on non-matching refspec' ' |
0d158ebb | 858 | git config remote.local.url . && |
81f339dc | 859 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && |
ec9779bc | 860 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
81f339dc | 861 | git config remote.local.fetch refs/heads/s:refs/remotes/local/s && |
ec9779bc | 862 | test_must_fail git branch --track my5 local/main && |
9c9cd39a JH |
863 | test_must_fail git config branch.my5.remote && |
864 | test_must_fail git config branch.my5.merge | |
0d158ebb RR |
865 | ' |
866 | ||
867 | test_expect_success 'test tracking setup via config' ' | |
868 | git config branch.autosetupmerge true && | |
869 | git config remote.local.url . && | |
870 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc JS |
871 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
872 | git branch my3 local/main && | |
0d158ebb | 873 | test $(git config branch.my3.remote) = local && |
ec9779bc | 874 | test $(git config branch.my3.merge) = refs/heads/main |
0d158ebb RR |
875 | ' |
876 | ||
877 | test_expect_success 'test overriding tracking setup via --no-track' ' | |
878 | git config branch.autosetupmerge true && | |
879 | git config remote.local.url . && | |
880 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc JS |
881 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
882 | git branch --no-track my2 local/main && | |
0d158ebb RR |
883 | git config branch.autosetupmerge false && |
884 | ! test "$(git config branch.my2.remote)" = local && | |
ec9779bc | 885 | ! test "$(git config branch.my2.merge)" = refs/heads/main |
0d158ebb RR |
886 | ' |
887 | ||
888 | test_expect_success 'no tracking without .fetch entries' ' | |
889 | git config branch.autosetupmerge true && | |
890 | git branch my6 s && | |
002ba037 | 891 | git config branch.autosetupmerge false && |
0d158ebb RR |
892 | test -z "$(git config branch.my6.remote)" && |
893 | test -z "$(git config branch.my6.merge)" | |
894 | ' | |
895 | ||
896 | test_expect_success 'test tracking setup via --track but deeper' ' | |
897 | git config remote.local.url . && | |
898 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
899 | (git show-ref -q refs/remotes/local/o/o || git fetch local) && | |
900 | git branch --track my7 local/o/o && | |
901 | test "$(git config branch.my7.remote)" = local && | |
902 | test "$(git config branch.my7.merge)" = refs/heads/o/o | |
903 | ' | |
904 | ||
905 | test_expect_success 'test deleting branch deletes branch config' ' | |
906 | git branch -d my7 && | |
907 | test -z "$(git config branch.my7.remote)" && | |
908 | test -z "$(git config branch.my7.merge)" | |
909 | ' | |
910 | ||
911 | test_expect_success 'test deleting branch without config' ' | |
912 | git branch my7 s && | |
913 | sha1=$(git rev-parse my7 | cut -c 1-7) && | |
914 | echo "Deleted branch my7 (was $sha1)." >expect && | |
915 | git branch -d my7 >actual 2>&1 && | |
1108cea7 | 916 | test_cmp expect actual |
0d158ebb RR |
917 | ' |
918 | ||
f292244c KY |
919 | test_expect_success 'deleting currently checked out branch fails' ' |
920 | git worktree add -b my7 my7 && | |
921 | test_must_fail git -C my7 branch -d my7 && | |
92edf618 | 922 | test_must_fail git branch -d my7 2>actual && |
12b99928 | 923 | grep "^error: cannot delete branch .my7. used by worktree at " actual && |
92edf618 RJ |
924 | rm -r my7 && |
925 | git worktree prune | |
926 | ' | |
927 | ||
928 | test_expect_success 'deleting in-use branch fails' ' | |
929 | git worktree add my7 && | |
930 | test_commit -C my7 bt7 && | |
931 | git -C my7 bisect start HEAD HEAD~2 && | |
932 | test_must_fail git -C my7 branch -d my7 && | |
933 | test_must_fail git branch -d my7 2>actual && | |
12b99928 | 934 | grep "^error: cannot delete branch .my7. used by worktree at " actual && |
ab313814 NB |
935 | rm -r my7 && |
936 | git worktree prune | |
f292244c KY |
937 | ' |
938 | ||
0d158ebb RR |
939 | test_expect_success 'test --track without .fetch entries' ' |
940 | git branch --track my8 && | |
941 | test "$(git config branch.my8.remote)" && | |
942 | test "$(git config branch.my8.merge)" | |
943 | ' | |
944 | ||
945 | test_expect_success 'branch from non-branch HEAD w/autosetupmerge=always' ' | |
946 | git config branch.autosetupmerge always && | |
947 | git branch my9 HEAD^ && | |
948 | git config branch.autosetupmerge false | |
949 | ' | |
950 | ||
951 | test_expect_success 'branch from non-branch HEAD w/--track causes failure' ' | |
952 | test_must_fail git branch --track my10 HEAD^ | |
953 | ' | |
954 | ||
955 | test_expect_success 'branch from tag w/--track causes failure' ' | |
956 | git tag foobar && | |
957 | test_must_fail git branch --track my11 foobar | |
958 | ' | |
959 | ||
bdaf1dfa TK |
960 | test_expect_success 'simple tracking works when remote branch name matches' ' |
961 | test_when_finished "rm -rf otherserver" && | |
962 | git init otherserver && | |
963 | test_commit -C otherserver my_commit 1 && | |
964 | git -C otherserver branch feature && | |
965 | test_config branch.autosetupmerge simple && | |
966 | test_config remote.otherserver.url otherserver && | |
967 | test_config remote.otherserver.fetch refs/heads/*:refs/remotes/otherserver/* && | |
968 | git fetch otherserver && | |
969 | git branch feature otherserver/feature && | |
970 | test_cmp_config otherserver branch.feature.remote && | |
971 | test_cmp_config refs/heads/feature branch.feature.merge | |
972 | ' | |
973 | ||
974 | test_expect_success 'simple tracking skips when remote branch name does not match' ' | |
975 | test_config branch.autosetupmerge simple && | |
976 | test_config remote.local.url . && | |
977 | test_config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
978 | git fetch local && | |
979 | git branch my-other local/main && | |
980 | test_cmp_config "" --default "" branch.my-other.remote && | |
981 | test_cmp_config "" --default "" branch.my-other.merge | |
982 | ' | |
983 | ||
984 | test_expect_success 'simple tracking skips when remote ref is not a branch' ' | |
985 | test_config branch.autosetupmerge simple && | |
986 | test_config remote.localtags.url . && | |
987 | test_config remote.localtags.fetch refs/tags/*:refs/remotes/localtags/* && | |
988 | git tag mytag12 main && | |
989 | git fetch localtags && | |
990 | git branch mytag12 localtags/mytag12 && | |
991 | test_cmp_config "" --default "" branch.mytag12.remote && | |
992 | test_cmp_config "" --default "" branch.mytag12.merge | |
993 | ' | |
994 | ||
0cb24fe8 | 995 | test_expect_success '--set-upstream-to fails on multiple branches' ' |
6b709306 | 996 | echo "fatal: too many arguments to set new upstream" >expect && |
ec9779bc | 997 | test_must_fail git branch --set-upstream-to main a b c 2>err && |
1108cea7 | 998 | test_cmp expect err |
0cb24fe8 JH |
999 | ' |
1000 | ||
1001 | test_expect_success '--set-upstream-to fails on detached HEAD' ' | |
1002 | git checkout HEAD^{} && | |
6b709306 | 1003 | test_when_finished git checkout - && |
12b99928 | 1004 | echo "fatal: could not set upstream of HEAD to main when it does not point to any branch" >expect && |
ec9779bc | 1005 | test_must_fail git branch --set-upstream-to main 2>err && |
1108cea7 | 1006 | test_cmp expect err |
0cb24fe8 JH |
1007 | ' |
1008 | ||
8a3e5ecd | 1009 | test_expect_success '--set-upstream-to fails on a missing dst branch' ' |
6b709306 | 1010 | echo "fatal: branch '"'"'does-not-exist'"'"' does not exist" >expect && |
ec9779bc | 1011 | test_must_fail git branch --set-upstream-to main does-not-exist 2>err && |
1108cea7 | 1012 | test_cmp expect err |
8a3e5ecd JK |
1013 | ' |
1014 | ||
1015 | test_expect_success '--set-upstream-to fails on a missing src branch' ' | |
ec9779bc | 1016 | test_must_fail git branch --set-upstream-to does-not-exist main 2>err && |
6789275d | 1017 | test_grep "the requested upstream branch '"'"'does-not-exist'"'"' does not exist" err |
8a3e5ecd JK |
1018 | ' |
1019 | ||
1020 | test_expect_success '--set-upstream-to fails on a non-ref' ' | |
7435e7e2 | 1021 | echo "fatal: cannot set up tracking information; starting point '"'"'HEAD^{}'"'"' is not a branch" >expect && |
6b709306 | 1022 | test_must_fail git branch --set-upstream-to HEAD^{} 2>err && |
1108cea7 | 1023 | test_cmp expect err |
8a3e5ecd JK |
1024 | ' |
1025 | ||
27852b2c PS |
1026 | test_expect_success '--set-upstream-to fails on locked config' ' |
1027 | test_when_finished "rm -f .git/config.lock" && | |
1028 | >.git/config.lock && | |
1029 | git branch locked && | |
6b709306 | 1030 | test_must_fail git branch --set-upstream-to locked 2>err && |
6789275d | 1031 | test_grep "could not lock config file .git/config" err |
27852b2c PS |
1032 | ' |
1033 | ||
0d158ebb | 1034 | test_expect_success 'use --set-upstream-to modify HEAD' ' |
ec9779bc JS |
1035 | test_config branch.main.remote foo && |
1036 | test_config branch.main.merge foo && | |
d0423ddd | 1037 | git branch my12 && |
0d158ebb | 1038 | git branch --set-upstream-to my12 && |
ec9779bc JS |
1039 | test "$(git config branch.main.remote)" = "." && |
1040 | test "$(git config branch.main.merge)" = "refs/heads/my12" | |
0d158ebb RR |
1041 | ' |
1042 | ||
1043 | test_expect_success 'use --set-upstream-to modify a particular branch' ' | |
d0423ddd | 1044 | git branch my13 && |
ec9779bc | 1045 | git branch --set-upstream-to main my13 && |
93a6b3f2 | 1046 | test_when_finished "git branch --unset-upstream my13" && |
0d158ebb | 1047 | test "$(git config branch.my13.remote)" = "." && |
ec9779bc | 1048 | test "$(git config branch.my13.merge)" = "refs/heads/main" |
0d158ebb RR |
1049 | ' |
1050 | ||
1051 | test_expect_success '--unset-upstream should fail if given a non-existent branch' ' | |
12b99928 | 1052 | echo "fatal: branch '"'"'i-dont-exist'"'"' has no upstream information" >expect && |
6b709306 | 1053 | test_must_fail git branch --unset-upstream i-dont-exist 2>err && |
1108cea7 | 1054 | test_cmp expect err |
0d158ebb RR |
1055 | ' |
1056 | ||
b81842cb PS |
1057 | test_expect_success '--unset-upstream should fail if config is locked' ' |
1058 | test_when_finished "rm -f .git/config.lock" && | |
1059 | git branch --set-upstream-to locked && | |
1060 | >.git/config.lock && | |
6b709306 | 1061 | test_must_fail git branch --unset-upstream 2>err && |
6789275d | 1062 | test_grep "could not lock config file .git/config" err |
b81842cb PS |
1063 | ' |
1064 | ||
0d158ebb | 1065 | test_expect_success 'test --unset-upstream on HEAD' ' |
d0423ddd | 1066 | git branch my14 && |
ec9779bc JS |
1067 | test_config branch.main.remote foo && |
1068 | test_config branch.main.merge foo && | |
0d158ebb RR |
1069 | git branch --set-upstream-to my14 && |
1070 | git branch --unset-upstream && | |
ec9779bc JS |
1071 | test_must_fail git config branch.main.remote && |
1072 | test_must_fail git config branch.main.merge && | |
0d158ebb | 1073 | # fail for a branch without upstream set |
12b99928 | 1074 | echo "fatal: branch '"'"'main'"'"' has no upstream information" >expect && |
6b709306 | 1075 | test_must_fail git branch --unset-upstream 2>err && |
1108cea7 | 1076 | test_cmp expect err |
0d158ebb RR |
1077 | ' |
1078 | ||
0cb24fe8 | 1079 | test_expect_success '--unset-upstream should fail on multiple branches' ' |
6b709306 DL |
1080 | echo "fatal: too many arguments to unset upstream" >expect && |
1081 | test_must_fail git branch --unset-upstream a b c 2>err && | |
1108cea7 | 1082 | test_cmp expect err |
0cb24fe8 JH |
1083 | ' |
1084 | ||
1085 | test_expect_success '--unset-upstream should fail on detached HEAD' ' | |
1086 | git checkout HEAD^{} && | |
6b709306 | 1087 | test_when_finished git checkout - && |
12b99928 | 1088 | echo "fatal: could not unset upstream of HEAD when it does not point to any branch" >expect && |
6b709306 | 1089 | test_must_fail git branch --unset-upstream 2>err && |
1108cea7 | 1090 | test_cmp expect err |
0cb24fe8 JH |
1091 | ' |
1092 | ||
0d158ebb | 1093 | test_expect_success 'test --unset-upstream on a particular branch' ' |
d0423ddd | 1094 | git branch my15 && |
ec9779bc | 1095 | git branch --set-upstream-to main my14 && |
0d158ebb RR |
1096 | git branch --unset-upstream my14 && |
1097 | test_must_fail git config branch.my14.remote && | |
1098 | test_must_fail git config branch.my14.merge | |
1099 | ' | |
1100 | ||
cf317877 | 1101 | test_expect_success 'disabled option --set-upstream fails' ' |
ec9779bc | 1102 | test_must_fail git branch --set-upstream origin/main |
b347d06b CMN |
1103 | ' |
1104 | ||
a3f40ec4 | 1105 | test_expect_success '--set-upstream-to notices an error to set branch as own upstream' " |
95052d1f | 1106 | git branch --set-upstream-to refs/heads/my13 my13 2>actual && |
6d504d5b | 1107 | cat >expect <<-\EOF && |
0669bdf4 | 1108 | warning: not setting branch 'my13' as its own upstream |
95052d1f BG |
1109 | EOF |
1110 | test_expect_code 1 git config branch.my13.remote && | |
1111 | test_expect_code 1 git config branch.my13.merge && | |
1108cea7 | 1112 | test_cmp expect actual |
a3f40ec4 | 1113 | " |
95052d1f | 1114 | |
0746d19a PB |
1115 | # Keep this test last, as it changes the current branch |
1116 | cat >expect <<EOF | |
668e31c6 | 1117 | $HEAD refs/heads/g/h/i@{0}: branch: Created from main |
0746d19a | 1118 | EOF |
0d158ebb RR |
1119 | test_expect_success 'git checkout -b g/h/i -l should create a branch and a log' ' |
1120 | GIT_COMMITTER_DATE="2005-05-26 23:30" \ | |
ec9779bc | 1121 | git checkout -b g/h/i -l main && |
0497e6c6 | 1122 | test_ref_exists refs/heads/g/h/i && |
668e31c6 PS |
1123 | git reflog show --no-abbrev-commit refs/heads/g/h/i >actual && |
1124 | test_cmp expect actual | |
0d158ebb | 1125 | ' |
0746d19a | 1126 | |
b2099957 | 1127 | test_expect_success 'checkout -b makes reflog by default' ' |
ec9779bc | 1128 | git checkout main && |
b2099957 EM |
1129 | git config --unset core.logAllRefUpdates && |
1130 | git checkout -b alpha && | |
6e6842e3 | 1131 | git rev-parse --verify alpha@{0} |
b2099957 EM |
1132 | ' |
1133 | ||
1134 | test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' ' | |
ec9779bc | 1135 | git checkout main && |
b2099957 EM |
1136 | git config core.logAllRefUpdates false && |
1137 | git checkout -b beta && | |
6e6842e3 | 1138 | test_must_fail git rev-parse --verify beta@{0} |
b2099957 EM |
1139 | ' |
1140 | ||
1141 | test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' ' | |
ec9779bc | 1142 | git checkout main && |
b2099957 EM |
1143 | git checkout -lb gamma && |
1144 | git config --unset core.logAllRefUpdates && | |
6e6842e3 | 1145 | git rev-parse --verify gamma@{0} |
b2099957 EM |
1146 | ' |
1147 | ||
e4921d87 | 1148 | test_expect_success 'avoid ambiguous track and advise' ' |
1d0a694b DB |
1149 | git config branch.autosetupmerge true && |
1150 | git config remote.ambi1.url lalala && | |
ec9779bc | 1151 | git config remote.ambi1.fetch refs/heads/lalala:refs/heads/main && |
1d0a694b | 1152 | git config remote.ambi2.url lilili && |
ec9779bc | 1153 | git config remote.ambi2.fetch refs/heads/lilili:refs/heads/main && |
e4921d87 TK |
1154 | cat <<-EOF >expected && |
1155 | fatal: not tracking: ambiguous information for ref '\''refs/heads/main'\'' | |
1156 | hint: There are multiple remotes whose fetch refspecs map to the remote | |
1157 | hint: tracking ref '\''refs/heads/main'\'': | |
1158 | hint: ambi1 | |
1159 | hint: ambi2 | |
1160 | hint: '' | |
1161 | hint: This is typically a configuration error. | |
1162 | hint: '' | |
1163 | hint: To support setting up tracking branches, ensure that | |
1164 | hint: different remotes'\'' fetch refspecs map into different | |
1165 | hint: tracking namespaces. | |
1166 | EOF | |
1167 | test_must_fail git branch all1 main 2>actual && | |
1168 | test_cmp expected actual && | |
1d0a694b DB |
1169 | test -z "$(git config branch.all1.merge)" |
1170 | ' | |
1171 | ||
c998ae9b DS |
1172 | test_expect_success 'autosetuprebase local on a tracked local branch' ' |
1173 | git config remote.local.url . && | |
1174 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
1175 | git config branch.autosetuprebase local && | |
0cb0e143 | 1176 | (git show-ref -q refs/remotes/local/o || git fetch local) && |
c998ae9b DS |
1177 | git branch mybase && |
1178 | git branch --track myr1 mybase && | |
1179 | test "$(git config branch.myr1.remote)" = . && | |
1180 | test "$(git config branch.myr1.merge)" = refs/heads/mybase && | |
1181 | test "$(git config branch.myr1.rebase)" = true | |
1182 | ' | |
1183 | ||
1184 | test_expect_success 'autosetuprebase always on a tracked local branch' ' | |
1185 | git config remote.local.url . && | |
1186 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
1187 | git config branch.autosetuprebase always && | |
0cb0e143 | 1188 | (git show-ref -q refs/remotes/local/o || git fetch local) && |
c998ae9b DS |
1189 | git branch mybase2 && |
1190 | git branch --track myr2 mybase && | |
1191 | test "$(git config branch.myr2.remote)" = . && | |
1192 | test "$(git config branch.myr2.merge)" = refs/heads/mybase && | |
1193 | test "$(git config branch.myr2.rebase)" = true | |
1194 | ' | |
1195 | ||
1196 | test_expect_success 'autosetuprebase remote on a tracked local branch' ' | |
1197 | git config remote.local.url . && | |
1198 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
1199 | git config branch.autosetuprebase remote && | |
0cb0e143 | 1200 | (git show-ref -q refs/remotes/local/o || git fetch local) && |
c998ae9b DS |
1201 | git branch mybase3 && |
1202 | git branch --track myr3 mybase2 && | |
1203 | test "$(git config branch.myr3.remote)" = . && | |
1204 | test "$(git config branch.myr3.merge)" = refs/heads/mybase2 && | |
1205 | ! test "$(git config branch.myr3.rebase)" = true | |
1206 | ' | |
1207 | ||
1208 | test_expect_success 'autosetuprebase never on a tracked local branch' ' | |
1209 | git config remote.local.url . && | |
1210 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
1211 | git config branch.autosetuprebase never && | |
0cb0e143 | 1212 | (git show-ref -q refs/remotes/local/o || git fetch local) && |
c998ae9b DS |
1213 | git branch mybase4 && |
1214 | git branch --track myr4 mybase2 && | |
1215 | test "$(git config branch.myr4.remote)" = . && | |
1216 | test "$(git config branch.myr4.merge)" = refs/heads/mybase2 && | |
1217 | ! test "$(git config branch.myr4.rebase)" = true | |
1218 | ' | |
1219 | ||
1220 | test_expect_success 'autosetuprebase local on a tracked remote branch' ' | |
1221 | git config remote.local.url . && | |
1222 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
1223 | git config branch.autosetuprebase local && | |
ec9779bc JS |
1224 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
1225 | git branch --track myr5 local/main && | |
c998ae9b | 1226 | test "$(git config branch.myr5.remote)" = local && |
ec9779bc | 1227 | test "$(git config branch.myr5.merge)" = refs/heads/main && |
c998ae9b DS |
1228 | ! test "$(git config branch.myr5.rebase)" = true |
1229 | ' | |
1230 | ||
1231 | test_expect_success 'autosetuprebase never on a tracked remote branch' ' | |
1232 | git config remote.local.url . && | |
1233 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
1234 | git config branch.autosetuprebase never && | |
ec9779bc JS |
1235 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
1236 | git branch --track myr6 local/main && | |
c998ae9b | 1237 | test "$(git config branch.myr6.remote)" = local && |
ec9779bc | 1238 | test "$(git config branch.myr6.merge)" = refs/heads/main && |
c998ae9b DS |
1239 | ! test "$(git config branch.myr6.rebase)" = true |
1240 | ' | |
1241 | ||
1242 | test_expect_success 'autosetuprebase remote on a tracked remote branch' ' | |
1243 | git config remote.local.url . && | |
1244 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
1245 | git config branch.autosetuprebase remote && | |
ec9779bc JS |
1246 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
1247 | git branch --track myr7 local/main && | |
c998ae9b | 1248 | test "$(git config branch.myr7.remote)" = local && |
ec9779bc | 1249 | test "$(git config branch.myr7.merge)" = refs/heads/main && |
c998ae9b DS |
1250 | test "$(git config branch.myr7.rebase)" = true |
1251 | ' | |
1252 | ||
1253 | test_expect_success 'autosetuprebase always on a tracked remote branch' ' | |
1254 | git config remote.local.url . && | |
1255 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
1256 | git config branch.autosetuprebase remote && | |
ec9779bc JS |
1257 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
1258 | git branch --track myr8 local/main && | |
c998ae9b | 1259 | test "$(git config branch.myr8.remote)" = local && |
ec9779bc | 1260 | test "$(git config branch.myr8.merge)" = refs/heads/main && |
c998ae9b DS |
1261 | test "$(git config branch.myr8.rebase)" = true |
1262 | ' | |
1263 | ||
1264 | test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' ' | |
1265 | git config --unset branch.autosetuprebase && | |
1266 | git config remote.local.url . && | |
1267 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc JS |
1268 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
1269 | git branch --track myr9 local/main && | |
c998ae9b | 1270 | test "$(git config branch.myr9.remote)" = local && |
ec9779bc | 1271 | test "$(git config branch.myr9.merge)" = refs/heads/main && |
c998ae9b DS |
1272 | test "z$(git config branch.myr9.rebase)" = z |
1273 | ' | |
1274 | ||
1275 | test_expect_success 'autosetuprebase unconfigured on a tracked local branch' ' | |
1276 | git config remote.local.url . && | |
1277 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
0cb0e143 | 1278 | (git show-ref -q refs/remotes/local/o || git fetch local) && |
c998ae9b DS |
1279 | git branch mybase10 && |
1280 | git branch --track myr10 mybase2 && | |
1281 | test "$(git config branch.myr10.remote)" = . && | |
1282 | test "$(git config branch.myr10.merge)" = refs/heads/mybase2 && | |
1283 | test "z$(git config branch.myr10.rebase)" = z | |
1284 | ' | |
1285 | ||
1286 | test_expect_success 'autosetuprebase unconfigured on untracked local branch' ' | |
1287 | git config remote.local.url . && | |
1288 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc | 1289 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
c998ae9b DS |
1290 | git branch --no-track myr11 mybase2 && |
1291 | test "z$(git config branch.myr11.remote)" = z && | |
1292 | test "z$(git config branch.myr11.merge)" = z && | |
1293 | test "z$(git config branch.myr11.rebase)" = z | |
1294 | ' | |
1295 | ||
1296 | test_expect_success 'autosetuprebase unconfigured on untracked remote branch' ' | |
1297 | git config remote.local.url . && | |
1298 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc JS |
1299 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
1300 | git branch --no-track myr12 local/main && | |
c998ae9b DS |
1301 | test "z$(git config branch.myr12.remote)" = z && |
1302 | test "z$(git config branch.myr12.merge)" = z && | |
1303 | test "z$(git config branch.myr12.rebase)" = z | |
1304 | ' | |
1305 | ||
1306 | test_expect_success 'autosetuprebase never on an untracked local branch' ' | |
1307 | git config branch.autosetuprebase never && | |
1308 | git config remote.local.url . && | |
1309 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc | 1310 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
c998ae9b DS |
1311 | git branch --no-track myr13 mybase2 && |
1312 | test "z$(git config branch.myr13.remote)" = z && | |
1313 | test "z$(git config branch.myr13.merge)" = z && | |
1314 | test "z$(git config branch.myr13.rebase)" = z | |
1315 | ' | |
1316 | ||
1317 | test_expect_success 'autosetuprebase local on an untracked local branch' ' | |
1318 | git config branch.autosetuprebase local && | |
1319 | git config remote.local.url . && | |
1320 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc | 1321 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
c998ae9b DS |
1322 | git branch --no-track myr14 mybase2 && |
1323 | test "z$(git config branch.myr14.remote)" = z && | |
1324 | test "z$(git config branch.myr14.merge)" = z && | |
1325 | test "z$(git config branch.myr14.rebase)" = z | |
1326 | ' | |
1327 | ||
1328 | test_expect_success 'autosetuprebase remote on an untracked local branch' ' | |
1329 | git config branch.autosetuprebase remote && | |
1330 | git config remote.local.url . && | |
1331 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc | 1332 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
c998ae9b DS |
1333 | git branch --no-track myr15 mybase2 && |
1334 | test "z$(git config branch.myr15.remote)" = z && | |
1335 | test "z$(git config branch.myr15.merge)" = z && | |
1336 | test "z$(git config branch.myr15.rebase)" = z | |
1337 | ' | |
1338 | ||
1339 | test_expect_success 'autosetuprebase always on an untracked local branch' ' | |
1340 | git config branch.autosetuprebase always && | |
1341 | git config remote.local.url . && | |
1342 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc | 1343 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
c998ae9b DS |
1344 | git branch --no-track myr16 mybase2 && |
1345 | test "z$(git config branch.myr16.remote)" = z && | |
1346 | test "z$(git config branch.myr16.merge)" = z && | |
1347 | test "z$(git config branch.myr16.rebase)" = z | |
1348 | ' | |
1349 | ||
1350 | test_expect_success 'autosetuprebase never on an untracked remote branch' ' | |
1351 | git config branch.autosetuprebase never && | |
1352 | git config remote.local.url . && | |
1353 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc JS |
1354 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
1355 | git branch --no-track myr17 local/main && | |
c998ae9b DS |
1356 | test "z$(git config branch.myr17.remote)" = z && |
1357 | test "z$(git config branch.myr17.merge)" = z && | |
1358 | test "z$(git config branch.myr17.rebase)" = z | |
1359 | ' | |
1360 | ||
1361 | test_expect_success 'autosetuprebase local on an untracked remote branch' ' | |
1362 | git config branch.autosetuprebase local && | |
1363 | git config remote.local.url . && | |
1364 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc JS |
1365 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
1366 | git branch --no-track myr18 local/main && | |
c998ae9b DS |
1367 | test "z$(git config branch.myr18.remote)" = z && |
1368 | test "z$(git config branch.myr18.merge)" = z && | |
1369 | test "z$(git config branch.myr18.rebase)" = z | |
1370 | ' | |
1371 | ||
1372 | test_expect_success 'autosetuprebase remote on an untracked remote branch' ' | |
1373 | git config branch.autosetuprebase remote && | |
1374 | git config remote.local.url . && | |
1375 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc JS |
1376 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
1377 | git branch --no-track myr19 local/main && | |
c998ae9b DS |
1378 | test "z$(git config branch.myr19.remote)" = z && |
1379 | test "z$(git config branch.myr19.merge)" = z && | |
1380 | test "z$(git config branch.myr19.rebase)" = z | |
1381 | ' | |
1382 | ||
1383 | test_expect_success 'autosetuprebase always on an untracked remote branch' ' | |
1384 | git config branch.autosetuprebase always && | |
1385 | git config remote.local.url . && | |
1386 | git config remote.local.fetch refs/heads/*:refs/remotes/local/* && | |
ec9779bc JS |
1387 | (git show-ref -q refs/remotes/local/main || git fetch local) && |
1388 | git branch --no-track myr20 local/main && | |
c998ae9b DS |
1389 | test "z$(git config branch.myr20.remote)" = z && |
1390 | test "z$(git config branch.myr20.merge)" = z && | |
1391 | test "z$(git config branch.myr20.rebase)" = z | |
1392 | ' | |
1393 | ||
21b5b1e8 JH |
1394 | test_expect_success 'autosetuprebase always on detached HEAD' ' |
1395 | git config branch.autosetupmerge always && | |
ec9779bc | 1396 | test_when_finished git checkout main && |
21b5b1e8 JH |
1397 | git checkout HEAD^0 && |
1398 | git branch my11 && | |
1399 | test -z "$(git config branch.my11.remote)" && | |
1400 | test -z "$(git config branch.my11.merge)" | |
1401 | ' | |
1402 | ||
c998ae9b DS |
1403 | test_expect_success 'detect misconfigured autosetuprebase (bad value)' ' |
1404 | git config branch.autosetuprebase garbage && | |
1405 | test_must_fail git branch | |
1406 | ' | |
1407 | ||
1408 | test_expect_success 'detect misconfigured autosetuprebase (no value)' ' | |
1409 | git config --unset branch.autosetuprebase && | |
0d158ebb | 1410 | echo "[branch] autosetuprebase" >>.git/config && |
c998ae9b DS |
1411 | test_must_fail git branch && |
1412 | git config --unset branch.autosetuprebase | |
1413 | ' | |
1414 | ||
99c419c9 JH |
1415 | test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' ' |
1416 | git checkout my9 && | |
1417 | git config --unset branch.my8.merge && | |
1418 | test_must_fail git branch -d my8 | |
1419 | ' | |
1420 | ||
1421 | test_expect_success 'attempt to delete a branch merged to its base' ' | |
1422 | # we are on my9 which is the initial commit; traditionally | |
1423 | # we would not have allowed deleting my8 that is not merged | |
ec9779bc JS |
1424 | # to my9, but it is set to track main that already has my8 |
1425 | git config branch.my8.merge refs/heads/main && | |
99c419c9 JH |
1426 | git branch -d my8 |
1427 | ' | |
1428 | ||
1429 | test_expect_success 'attempt to delete a branch merged to its base' ' | |
ec9779bc | 1430 | git checkout main && |
99c419c9 JH |
1431 | echo Third >>A && |
1432 | git commit -m "Third commit" A && | |
1433 | git branch -t my10 my9 && | |
1434 | git branch -f my10 HEAD^ && | |
ec9779bc | 1435 | # we are on main which is at the third commit, and my10 |
99c419c9 JH |
1436 | # is behind us, so traditionally we would have allowed deleting |
1437 | # it; but my10 is set to track my9 that is further behind. | |
1438 | test_must_fail git branch -d my10 | |
1439 | ' | |
1440 | ||
597a9774 RS |
1441 | test_expect_success 'branch --delete --force removes dangling branch' ' |
1442 | git checkout main && | |
1443 | test_commit unstable && | |
1444 | hash=$(git rev-parse HEAD) && | |
1445 | objpath=$(echo $hash | sed -e "s|^..|.git/objects/&/|") && | |
1446 | git branch --no-track dangling && | |
1447 | mv $objpath $objpath.x && | |
1448 | test_when_finished "mv $objpath.x $objpath" && | |
1449 | git branch --delete --force dangling && | |
1450 | git for-each-ref refs/heads/dangling >actual && | |
1451 | test_must_be_empty actual | |
1452 | ' | |
1453 | ||
c2d17ba3 | 1454 | test_expect_success 'use --edit-description' ' |
e288b3de | 1455 | EDITOR=: git branch --edit-description && |
8f241151 | 1456 | test_expect_code 1 git config branch.main.description && |
e288b3de | 1457 | |
c2d17ba3 JH |
1458 | write_script editor <<-\EOF && |
1459 | echo "New contents" >"$1" | |
1460 | EOF | |
1461 | EDITOR=./editor git branch --edit-description && | |
1462 | write_script editor <<-\EOF && | |
1463 | git stripspace -s <"$1" >"EDITOR_OUTPUT" | |
1464 | EOF | |
1465 | EDITOR=./editor git branch --edit-description && | |
1466 | echo "New contents" >expect && | |
dcbaa0b3 | 1467 | test_cmp expect EDITOR_OUTPUT |
c2d17ba3 JH |
1468 | ' |
1469 | ||
1470 | test_expect_success 'detect typo in branch name when using --edit-description' ' | |
1471 | write_script editor <<-\EOF && | |
1472 | echo "New contents" >"$1" | |
1473 | EOF | |
512477b1 | 1474 | test_must_fail env EDITOR=./editor git branch --edit-description no-such-branch |
c2d17ba3 JH |
1475 | ' |
1476 | ||
1477 | test_expect_success 'refuse --edit-description on unborn branch for now' ' | |
ec9779bc | 1478 | test_when_finished "git checkout main" && |
c2d17ba3 JH |
1479 | write_script editor <<-\EOF && |
1480 | echo "New contents" >"$1" | |
1481 | EOF | |
1482 | git checkout --orphan unborn && | |
512477b1 | 1483 | test_must_fail env EDITOR=./editor git branch --edit-description |
c2d17ba3 JH |
1484 | ' |
1485 | ||
6c41e975 CMN |
1486 | test_expect_success '--merged catches invalid object names' ' |
1487 | test_must_fail git branch --merged 0000000000000000000000000000000000000000 | |
1488 | ' | |
1489 | ||
1f537be3 ES |
1490 | test_expect_success '--list during rebase' ' |
1491 | test_when_finished "reset_rebase" && | |
ec9779bc | 1492 | git checkout main && |
1f537be3 ES |
1493 | FAKE_LINES="1 edit 2" && |
1494 | export FAKE_LINES && | |
1495 | set_fake_editor && | |
1496 | git rebase -i HEAD~2 && | |
1497 | git branch --list >actual && | |
6789275d | 1498 | test_grep "rebasing main" actual |
1f537be3 ES |
1499 | ' |
1500 | ||
1501 | test_expect_success '--list during rebase from detached HEAD' ' | |
ec9779bc JS |
1502 | test_when_finished "reset_rebase && git checkout main" && |
1503 | git checkout main^0 && | |
1f537be3 ES |
1504 | oid=$(git rev-parse --short HEAD) && |
1505 | FAKE_LINES="1 edit 2" && | |
1506 | export FAKE_LINES && | |
1507 | set_fake_editor && | |
1508 | git rebase -i HEAD~2 && | |
1509 | git branch --list >actual && | |
6789275d | 1510 | test_grep "rebasing detached HEAD $oid" actual |
1f537be3 ES |
1511 | ' |
1512 | ||
1d7358c5 | 1513 | test_expect_success 'tracking with unexpected .fetch refspec' ' |
b0f49ff1 | 1514 | rm -rf a b c d && |
ec9779bc | 1515 | git init -b main a && |
62d94a3a JH |
1516 | ( |
1517 | cd a && | |
1518 | test_commit a | |
1519 | ) && | |
ec9779bc | 1520 | git init -b main b && |
62d94a3a JH |
1521 | ( |
1522 | cd b && | |
1523 | test_commit b | |
1524 | ) && | |
ec9779bc | 1525 | git init -b main c && |
62d94a3a JH |
1526 | ( |
1527 | cd c && | |
1528 | test_commit c && | |
1529 | git remote add a ../a && | |
1530 | git remote add b ../b && | |
1531 | git fetch --all | |
1532 | ) && | |
ec9779bc | 1533 | git init -b main d && |
62d94a3a JH |
1534 | ( |
1535 | cd d && | |
1536 | git remote add c ../c && | |
1537 | git config remote.c.fetch "+refs/remotes/*:refs/remotes/*" && | |
1538 | git fetch c && | |
ec9779bc JS |
1539 | git branch --track local/a/main remotes/a/main && |
1540 | test "$(git config branch.local/a/main.remote)" = "c" && | |
1541 | test "$(git config branch.local/a/main.merge)" = "refs/remotes/a/main" && | |
62d94a3a | 1542 | git rev-parse --verify a >expect && |
ec9779bc | 1543 | git rev-parse --verify local/a/main >actual && |
62d94a3a JH |
1544 | test_cmp expect actual |
1545 | ) | |
1546 | ' | |
1547 | ||
560ae1c1 | 1548 | test_expect_success 'configured committerdate sort' ' |
ec9779bc | 1549 | git init -b main sort && |
56d26ade VD |
1550 | test_config -C sort branch.sort "committerdate" && |
1551 | ||
560ae1c1 SM |
1552 | ( |
1553 | cd sort && | |
560ae1c1 SM |
1554 | test_commit initial && |
1555 | git checkout -b a && | |
1556 | test_commit a && | |
1557 | git checkout -b c && | |
1558 | test_commit c && | |
1559 | git checkout -b b && | |
1560 | test_commit b && | |
1561 | git branch >actual && | |
1562 | cat >expect <<-\EOF && | |
ec9779bc | 1563 | main |
560ae1c1 SM |
1564 | a |
1565 | c | |
1566 | * b | |
1567 | EOF | |
1568 | test_cmp expect actual | |
1569 | ) | |
1570 | ' | |
1571 | ||
1572 | test_expect_success 'option override configured sort' ' | |
56d26ade VD |
1573 | test_config -C sort branch.sort "committerdate" && |
1574 | ||
560ae1c1 SM |
1575 | ( |
1576 | cd sort && | |
560ae1c1 SM |
1577 | git branch --sort=refname >actual && |
1578 | cat >expect <<-\EOF && | |
1579 | a | |
1580 | * b | |
1581 | c | |
ec9779bc | 1582 | main |
560ae1c1 SM |
1583 | EOF |
1584 | test_cmp expect actual | |
1585 | ) | |
1586 | ' | |
1587 | ||
56d26ade VD |
1588 | test_expect_success '--no-sort cancels config sort keys' ' |
1589 | test_config -C sort branch.sort "-refname" && | |
1590 | ||
1591 | ( | |
1592 | cd sort && | |
1593 | ||
1594 | # objecttype is identical for all of them, so sort falls back on | |
1595 | # default (ascending refname) | |
1596 | git branch \ | |
1597 | --no-sort \ | |
1598 | --sort="objecttype" >actual && | |
1599 | cat >expect <<-\EOF && | |
1600 | a | |
1601 | * b | |
1602 | c | |
1603 | main | |
1604 | EOF | |
1605 | test_cmp expect actual | |
1606 | ) | |
1607 | ||
1608 | ' | |
1609 | ||
1610 | test_expect_success '--no-sort cancels command line sort keys' ' | |
1611 | ( | |
1612 | cd sort && | |
1613 | ||
1614 | # objecttype is identical for all of them, so sort falls back on | |
1615 | # default (ascending refname) | |
1616 | git branch \ | |
1617 | --sort="-refname" \ | |
1618 | --no-sort \ | |
1619 | --sort="objecttype" >actual && | |
1620 | cat >expect <<-\EOF && | |
1621 | a | |
1622 | * b | |
1623 | c | |
1624 | main | |
1625 | EOF | |
1626 | test_cmp expect actual | |
1627 | ) | |
1628 | ' | |
1629 | ||
1630 | test_expect_success '--no-sort without subsequent --sort prints expected branches' ' | |
1631 | ( | |
1632 | cd sort && | |
1633 | ||
1634 | # Sort the results with `sort` for a consistent comparison | |
1635 | # against expected | |
1636 | git branch --no-sort | sort >actual && | |
1637 | cat >expect <<-\EOF && | |
1638 | a | |
1639 | c | |
1640 | main | |
1641 | * b | |
1642 | EOF | |
1643 | test_cmp expect actual | |
1644 | ) | |
1645 | ' | |
1646 | ||
560ae1c1 | 1647 | test_expect_success 'invalid sort parameter in configuration' ' |
56d26ade VD |
1648 | test_config -C sort branch.sort "v:notvalid" && |
1649 | ||
560ae1c1 SM |
1650 | ( |
1651 | cd sort && | |
98e7ab6d JH |
1652 | |
1653 | # this works in the "listing" mode, so bad sort key | |
1654 | # is a dying offence. | |
1655 | test_must_fail git branch && | |
1656 | ||
1657 | # these do not need to use sorting, and should all | |
1658 | # succeed | |
1659 | git branch newone main && | |
1660 | git branch -c newone newerone && | |
1661 | git branch -m newone newestone && | |
1662 | git branch -d newerone newestone | |
560ae1c1 SM |
1663 | ) |
1664 | ' | |
1665 | ||
d3115660 JS |
1666 | test_expect_success 'tracking info copied with --track=inherit' ' |
1667 | git branch --track=inherit foo2 my1 && | |
1668 | test_cmp_config local branch.foo2.remote && | |
1669 | test_cmp_config refs/heads/main branch.foo2.merge | |
1670 | ' | |
1671 | ||
1672 | test_expect_success 'tracking info copied with autoSetupMerge=inherit' ' | |
1673 | test_unconfig branch.autoSetupMerge && | |
1674 | # default config does not copy tracking info | |
1675 | git branch foo-no-inherit my1 && | |
1676 | test_cmp_config "" --default "" branch.foo-no-inherit.remote && | |
1677 | test_cmp_config "" --default "" branch.foo-no-inherit.merge && | |
1678 | # with autoSetupMerge=inherit, we copy tracking info from my1 | |
1679 | test_config branch.autoSetupMerge inherit && | |
1680 | git branch foo3 my1 && | |
1681 | test_cmp_config local branch.foo3.remote && | |
1682 | test_cmp_config refs/heads/main branch.foo3.merge && | |
1683 | # no tracking info to inherit from main | |
1684 | git branch main2 main && | |
1685 | test_cmp_config "" --default "" branch.main2.remote && | |
1686 | test_cmp_config "" --default "" branch.main2.merge | |
1687 | ' | |
1688 | ||
1689 | test_expect_success '--track overrides branch.autoSetupMerge' ' | |
1690 | test_config branch.autoSetupMerge inherit && | |
1691 | git branch --track=direct foo4 my1 && | |
1692 | test_cmp_config . branch.foo4.remote && | |
1693 | test_cmp_config refs/heads/my1 branch.foo4.merge && | |
1694 | git branch --no-track foo5 my1 && | |
1695 | test_cmp_config "" --default "" branch.foo5.remote && | |
1696 | test_cmp_config "" --default "" branch.foo5.merge | |
1697 | ' | |
1698 | ||
a3b427b9 | 1699 | test_done |