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