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