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