1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Amos Waterland
4 #
6 test_description='git branch --foo should not create bogus branch
8 This test runs git branch --help and checks that the argument is properly
9 handled. Specifically, that a bogus branch is not created.
10 '
11 . ./test-lib.sh
13 test_expect_success \
14 'prepare a trivial repository' \
15 'echo Hello > A &&
16 git update-index --add A &&
17 git commit -m "Initial commit." &&
18 echo World >> A &&
19 git update-index --add A &&
20 git commit -m "Second commit." &&
21 HEAD=$(git rev-parse --verify HEAD)'
23 test_expect_success \
24 'git branch --help should not have created a bogus branch' '
25 git branch --help </dev/null >/dev/null 2>/dev/null;
26 ! test -f .git/refs/heads/--help
27 '
29 test_expect_success 'branch -h in broken repository' '
30 mkdir broken &&
31 (
32 cd broken &&
33 git init &&
34 >.git/refs/heads/master &&
35 test_expect_code 129 git branch -h >usage 2>&1
36 ) &&
37 grep "[Uu]sage" broken/usage
38 '
40 test_expect_success \
41 'git branch abc should create a branch' \
42 'git branch abc && test -f .git/refs/heads/abc'
44 test_expect_success \
45 'git branch a/b/c should create a branch' \
46 'git branch a/b/c && test -f .git/refs/heads/a/b/c'
48 cat >expect <<EOF
49 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
50 EOF
51 test_expect_success \
52 'git branch -l d/e/f should create a branch and a log' \
53 'GIT_COMMITTER_DATE="2005-05-26 23:30" \
54 git branch -l d/e/f &&
55 test -f .git/refs/heads/d/e/f &&
56 test -f .git/logs/refs/heads/d/e/f &&
57 test_cmp expect .git/logs/refs/heads/d/e/f'
59 test_expect_success \
60 'git branch -d d/e/f should delete a branch and a log' \
61 'git branch -d d/e/f &&
62 test ! -f .git/refs/heads/d/e/f &&
63 test ! -f .git/logs/refs/heads/d/e/f'
65 test_expect_success \
66 'git branch j/k should work after branch j has been deleted' \
67 'git branch j &&
68 git branch -d j &&
69 git branch j/k'
71 test_expect_success \
72 'git branch l should work after branch l/m has been deleted' \
73 'git branch l/m &&
74 git branch -d l/m &&
75 git branch l'
77 test_expect_success \
78 'git branch -m m m/m should work' \
79 'git branch -l m &&
80 git branch -m m m/m &&
81 test -f .git/logs/refs/heads/m/m'
83 test_expect_success \
84 'git branch -m n/n n should work' \
85 'git branch -l n/n &&
86 git branch -m n/n n
87 test -f .git/logs/refs/heads/n'
89 test_expect_success 'git branch -m o/o o should fail when o/p exists' '
90 git branch o/o &&
91 git branch o/p &&
92 test_must_fail git branch -m o/o o
93 '
95 test_expect_success 'git branch -m q r/q should fail when r exists' '
96 git branch q &&
97 git branch r &&
98 test_must_fail git branch -m q r/q
99 '
101 test_expect_success 'git branch -M foo bar should fail when bar is checked out' '
102 git branch bar &&
103 git checkout -b foo &&
104 test_must_fail git branch -M bar foo
105 '
107 test_expect_success 'git branch -M baz bam should succeed when baz is checked out' '
108 git checkout -b baz &&
109 git branch bam &&
110 git branch -M baz bam
111 '
113 mv .git/config .git/config-saved
115 test_expect_success 'git branch -m q q2 without config should succeed' '
116 git branch -m q q2 &&
117 git branch -m q2 q
118 '
120 mv .git/config-saved .git/config
122 git config branch.s/s.dummy Hello
124 test_expect_success \
125 'git branch -m s/s s should work when s/t is deleted' \
126 'git branch -l s/s &&
127 test -f .git/logs/refs/heads/s/s &&
128 git branch -l s/t &&
129 test -f .git/logs/refs/heads/s/t &&
130 git branch -d s/t &&
131 git branch -m s/s s &&
132 test -f .git/logs/refs/heads/s'
134 test_expect_success 'config information was renamed, too' \
135 "test $(git config branch.s.dummy) = Hello &&
136 test_must_fail git config branch.s/s/dummy"
138 test_expect_success 'renaming a symref is not allowed' \
139 '
140 git symbolic-ref refs/heads/master2 refs/heads/master &&
141 test_must_fail git branch -m master2 master3 &&
142 git symbolic-ref refs/heads/master2 &&
143 test -f .git/refs/heads/master &&
144 ! test -f .git/refs/heads/master3
145 '
147 test_expect_success SYMLINKS \
148 'git branch -m u v should fail when the reflog for u is a symlink' '
149 git branch -l u &&
150 mv .git/logs/refs/heads/u real-u &&
151 ln -s real-u .git/logs/refs/heads/u &&
152 test_must_fail git branch -m u v
153 '
155 test_expect_success 'test tracking setup via --track' \
156 'git config remote.local.url . &&
157 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
158 (git show-ref -q refs/remotes/local/master || git fetch local) &&
159 git branch --track my1 local/master &&
160 test $(git config branch.my1.remote) = local &&
161 test $(git config branch.my1.merge) = refs/heads/master'
163 test_expect_success 'test tracking setup (non-wildcard, matching)' \
164 'git config remote.local.url . &&
165 git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
166 (git show-ref -q refs/remotes/local/master || git fetch local) &&
167 git branch --track my4 local/master &&
168 test $(git config branch.my4.remote) = local &&
169 test $(git config branch.my4.merge) = refs/heads/master'
171 test_expect_success 'test tracking setup (non-wildcard, not matching)' \
172 'git config remote.local.url . &&
173 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
174 (git show-ref -q refs/remotes/local/master || git fetch local) &&
175 git branch --track my5 local/master &&
176 ! test "$(git config branch.my5.remote)" = local &&
177 ! test "$(git config branch.my5.merge)" = refs/heads/master'
179 test_expect_success 'test tracking setup via config' \
180 'git config branch.autosetupmerge true &&
181 git config remote.local.url . &&
182 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
183 (git show-ref -q refs/remotes/local/master || git fetch local) &&
184 git branch my3 local/master &&
185 test $(git config branch.my3.remote) = local &&
186 test $(git config branch.my3.merge) = refs/heads/master'
188 test_expect_success 'test overriding tracking setup via --no-track' \
189 'git config branch.autosetupmerge true &&
190 git config remote.local.url . &&
191 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
192 (git show-ref -q refs/remotes/local/master || git fetch local) &&
193 git branch --no-track my2 local/master &&
194 git config branch.autosetupmerge false &&
195 ! test "$(git config branch.my2.remote)" = local &&
196 ! test "$(git config branch.my2.merge)" = refs/heads/master'
198 test_expect_success 'no tracking without .fetch entries' \
199 'git config branch.autosetupmerge true &&
200 git branch my6 s &&
201 git config branch.automsetupmerge false &&
202 test -z "$(git config branch.my6.remote)" &&
203 test -z "$(git config branch.my6.merge)"'
205 test_expect_success 'test tracking setup via --track but deeper' \
206 'git config remote.local.url . &&
207 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
208 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
209 git branch --track my7 local/o/o &&
210 test "$(git config branch.my7.remote)" = local &&
211 test "$(git config branch.my7.merge)" = refs/heads/o/o'
213 test_expect_success 'test deleting branch deletes branch config' \
214 'git branch -d my7 &&
215 test -z "$(git config branch.my7.remote)" &&
216 test -z "$(git config branch.my7.merge)"'
218 test_expect_success 'test deleting branch without config' \
219 'git branch my7 s &&
220 sha1=$(git rev-parse my7 | cut -c 1-7) &&
221 echo "Deleted branch my7 (was $sha1)." >expect &&
222 git branch -d my7 >actual 2>&1 &&
223 test_i18ncmp expect actual'
225 test_expect_success 'test --track without .fetch entries' \
226 'git branch --track my8 &&
227 test "$(git config branch.my8.remote)" &&
228 test "$(git config branch.my8.merge)"'
230 test_expect_success \
231 'branch from non-branch HEAD w/autosetupmerge=always' \
232 'git config branch.autosetupmerge always &&
233 git branch my9 HEAD^ &&
234 git config branch.autosetupmerge false'
236 test_expect_success \
237 'branch from non-branch HEAD w/--track causes failure' \
238 'test_must_fail git branch --track my10 HEAD^'
240 test_expect_success \
241 'branch from tag w/--track causes failure' \
242 'git tag foobar &&
243 test_must_fail git branch --track my11 foobar'
245 # Keep this test last, as it changes the current branch
246 cat >expect <<EOF
247 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
248 EOF
249 test_expect_success \
250 'git checkout -b g/h/i -l should create a branch and a log' \
251 'GIT_COMMITTER_DATE="2005-05-26 23:30" \
252 git checkout -b g/h/i -l master &&
253 test -f .git/refs/heads/g/h/i &&
254 test -f .git/logs/refs/heads/g/h/i &&
255 test_cmp expect .git/logs/refs/heads/g/h/i'
257 test_expect_success 'checkout -b makes reflog by default' '
258 git checkout master &&
259 git config --unset core.logAllRefUpdates &&
260 git checkout -b alpha &&
261 git rev-parse --verify alpha@{0}
262 '
264 test_expect_success 'checkout -b does not make reflog when core.logAllRefUpdates = false' '
265 git checkout master &&
266 git config core.logAllRefUpdates false &&
267 git checkout -b beta &&
268 test_must_fail git rev-parse --verify beta@{0}
269 '
271 test_expect_success 'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
272 git checkout master &&
273 git checkout -lb gamma &&
274 git config --unset core.logAllRefUpdates &&
275 git rev-parse --verify gamma@{0}
276 '
278 test_expect_success 'avoid ambiguous track' '
279 git config branch.autosetupmerge true &&
280 git config remote.ambi1.url lalala &&
281 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
282 git config remote.ambi2.url lilili &&
283 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
284 git branch all1 master &&
285 test -z "$(git config branch.all1.merge)"
286 '
288 test_expect_success 'autosetuprebase local on a tracked local branch' '
289 git config remote.local.url . &&
290 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
291 git config branch.autosetuprebase local &&
292 (git show-ref -q refs/remotes/local/o || git fetch local) &&
293 git branch mybase &&
294 git branch --track myr1 mybase &&
295 test "$(git config branch.myr1.remote)" = . &&
296 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
297 test "$(git config branch.myr1.rebase)" = true
298 '
300 test_expect_success 'autosetuprebase always on a tracked local branch' '
301 git config remote.local.url . &&
302 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
303 git config branch.autosetuprebase always &&
304 (git show-ref -q refs/remotes/local/o || git fetch local) &&
305 git branch mybase2 &&
306 git branch --track myr2 mybase &&
307 test "$(git config branch.myr2.remote)" = . &&
308 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
309 test "$(git config branch.myr2.rebase)" = true
310 '
312 test_expect_success 'autosetuprebase remote on a tracked local branch' '
313 git config remote.local.url . &&
314 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
315 git config branch.autosetuprebase remote &&
316 (git show-ref -q refs/remotes/local/o || git fetch local) &&
317 git branch mybase3 &&
318 git branch --track myr3 mybase2 &&
319 test "$(git config branch.myr3.remote)" = . &&
320 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
321 ! test "$(git config branch.myr3.rebase)" = true
322 '
324 test_expect_success 'autosetuprebase never on a tracked local branch' '
325 git config remote.local.url . &&
326 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
327 git config branch.autosetuprebase never &&
328 (git show-ref -q refs/remotes/local/o || git fetch local) &&
329 git branch mybase4 &&
330 git branch --track myr4 mybase2 &&
331 test "$(git config branch.myr4.remote)" = . &&
332 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
333 ! test "$(git config branch.myr4.rebase)" = true
334 '
336 test_expect_success 'autosetuprebase local on a tracked remote branch' '
337 git config remote.local.url . &&
338 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
339 git config branch.autosetuprebase local &&
340 (git show-ref -q refs/remotes/local/master || git fetch local) &&
341 git branch --track myr5 local/master &&
342 test "$(git config branch.myr5.remote)" = local &&
343 test "$(git config branch.myr5.merge)" = refs/heads/master &&
344 ! test "$(git config branch.myr5.rebase)" = true
345 '
347 test_expect_success 'autosetuprebase never on a tracked remote branch' '
348 git config remote.local.url . &&
349 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
350 git config branch.autosetuprebase never &&
351 (git show-ref -q refs/remotes/local/master || git fetch local) &&
352 git branch --track myr6 local/master &&
353 test "$(git config branch.myr6.remote)" = local &&
354 test "$(git config branch.myr6.merge)" = refs/heads/master &&
355 ! test "$(git config branch.myr6.rebase)" = true
356 '
358 test_expect_success 'autosetuprebase remote on a tracked remote branch' '
359 git config remote.local.url . &&
360 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
361 git config branch.autosetuprebase remote &&
362 (git show-ref -q refs/remotes/local/master || git fetch local) &&
363 git branch --track myr7 local/master &&
364 test "$(git config branch.myr7.remote)" = local &&
365 test "$(git config branch.myr7.merge)" = refs/heads/master &&
366 test "$(git config branch.myr7.rebase)" = true
367 '
369 test_expect_success 'autosetuprebase always on a tracked remote branch' '
370 git config remote.local.url . &&
371 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
372 git config branch.autosetuprebase remote &&
373 (git show-ref -q refs/remotes/local/master || git fetch local) &&
374 git branch --track myr8 local/master &&
375 test "$(git config branch.myr8.remote)" = local &&
376 test "$(git config branch.myr8.merge)" = refs/heads/master &&
377 test "$(git config branch.myr8.rebase)" = true
378 '
380 test_expect_success 'autosetuprebase unconfigured on a tracked remote branch' '
381 git config --unset branch.autosetuprebase &&
382 git config remote.local.url . &&
383 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
384 (git show-ref -q refs/remotes/local/master || git fetch local) &&
385 git branch --track myr9 local/master &&
386 test "$(git config branch.myr9.remote)" = local &&
387 test "$(git config branch.myr9.merge)" = refs/heads/master &&
388 test "z$(git config branch.myr9.rebase)" = z
389 '
391 test_expect_success 'autosetuprebase unconfigured on a tracked local branch' '
392 git config remote.local.url . &&
393 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
394 (git show-ref -q refs/remotes/local/o || git fetch local) &&
395 git branch mybase10 &&
396 git branch --track myr10 mybase2 &&
397 test "$(git config branch.myr10.remote)" = . &&
398 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
399 test "z$(git config branch.myr10.rebase)" = z
400 '
402 test_expect_success 'autosetuprebase unconfigured on untracked local branch' '
403 git config remote.local.url . &&
404 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
405 (git show-ref -q refs/remotes/local/master || git fetch local) &&
406 git branch --no-track myr11 mybase2 &&
407 test "z$(git config branch.myr11.remote)" = z &&
408 test "z$(git config branch.myr11.merge)" = z &&
409 test "z$(git config branch.myr11.rebase)" = z
410 '
412 test_expect_success 'autosetuprebase unconfigured on untracked remote branch' '
413 git config remote.local.url . &&
414 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
415 (git show-ref -q refs/remotes/local/master || git fetch local) &&
416 git branch --no-track myr12 local/master &&
417 test "z$(git config branch.myr12.remote)" = z &&
418 test "z$(git config branch.myr12.merge)" = z &&
419 test "z$(git config branch.myr12.rebase)" = z
420 '
422 test_expect_success 'autosetuprebase never on an untracked local branch' '
423 git config branch.autosetuprebase never &&
424 git config remote.local.url . &&
425 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
426 (git show-ref -q refs/remotes/local/master || git fetch local) &&
427 git branch --no-track myr13 mybase2 &&
428 test "z$(git config branch.myr13.remote)" = z &&
429 test "z$(git config branch.myr13.merge)" = z &&
430 test "z$(git config branch.myr13.rebase)" = z
431 '
433 test_expect_success 'autosetuprebase local on an untracked local branch' '
434 git config branch.autosetuprebase local &&
435 git config remote.local.url . &&
436 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
437 (git show-ref -q refs/remotes/local/master || git fetch local) &&
438 git branch --no-track myr14 mybase2 &&
439 test "z$(git config branch.myr14.remote)" = z &&
440 test "z$(git config branch.myr14.merge)" = z &&
441 test "z$(git config branch.myr14.rebase)" = z
442 '
444 test_expect_success 'autosetuprebase remote on an untracked local branch' '
445 git config branch.autosetuprebase remote &&
446 git config remote.local.url . &&
447 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
448 (git show-ref -q refs/remotes/local/master || git fetch local) &&
449 git branch --no-track myr15 mybase2 &&
450 test "z$(git config branch.myr15.remote)" = z &&
451 test "z$(git config branch.myr15.merge)" = z &&
452 test "z$(git config branch.myr15.rebase)" = z
453 '
455 test_expect_success 'autosetuprebase always on an untracked local branch' '
456 git config branch.autosetuprebase always &&
457 git config remote.local.url . &&
458 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
459 (git show-ref -q refs/remotes/local/master || git fetch local) &&
460 git branch --no-track myr16 mybase2 &&
461 test "z$(git config branch.myr16.remote)" = z &&
462 test "z$(git config branch.myr16.merge)" = z &&
463 test "z$(git config branch.myr16.rebase)" = z
464 '
466 test_expect_success 'autosetuprebase never on an untracked remote branch' '
467 git config branch.autosetuprebase never &&
468 git config remote.local.url . &&
469 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
470 (git show-ref -q refs/remotes/local/master || git fetch local) &&
471 git branch --no-track myr17 local/master &&
472 test "z$(git config branch.myr17.remote)" = z &&
473 test "z$(git config branch.myr17.merge)" = z &&
474 test "z$(git config branch.myr17.rebase)" = z
475 '
477 test_expect_success 'autosetuprebase local on an untracked remote branch' '
478 git config branch.autosetuprebase local &&
479 git config remote.local.url . &&
480 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
481 (git show-ref -q refs/remotes/local/master || git fetch local) &&
482 git branch --no-track myr18 local/master &&
483 test "z$(git config branch.myr18.remote)" = z &&
484 test "z$(git config branch.myr18.merge)" = z &&
485 test "z$(git config branch.myr18.rebase)" = z
486 '
488 test_expect_success 'autosetuprebase remote on an untracked remote branch' '
489 git config branch.autosetuprebase remote &&
490 git config remote.local.url . &&
491 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
492 (git show-ref -q refs/remotes/local/master || git fetch local) &&
493 git branch --no-track myr19 local/master &&
494 test "z$(git config branch.myr19.remote)" = z &&
495 test "z$(git config branch.myr19.merge)" = z &&
496 test "z$(git config branch.myr19.rebase)" = z
497 '
499 test_expect_success 'autosetuprebase always on an untracked remote branch' '
500 git config branch.autosetuprebase always &&
501 git config remote.local.url . &&
502 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
503 (git show-ref -q refs/remotes/local/master || git fetch local) &&
504 git branch --no-track myr20 local/master &&
505 test "z$(git config branch.myr20.remote)" = z &&
506 test "z$(git config branch.myr20.merge)" = z &&
507 test "z$(git config branch.myr20.rebase)" = z
508 '
510 test_expect_success 'autosetuprebase always on detached HEAD' '
511 git config branch.autosetupmerge always &&
512 test_when_finished git checkout master &&
513 git checkout HEAD^0 &&
514 git branch my11 &&
515 test -z "$(git config branch.my11.remote)" &&
516 test -z "$(git config branch.my11.merge)"
517 '
519 test_expect_success 'detect misconfigured autosetuprebase (bad value)' '
520 git config branch.autosetuprebase garbage &&
521 test_must_fail git branch
522 '
524 test_expect_success 'detect misconfigured autosetuprebase (no value)' '
525 git config --unset branch.autosetuprebase &&
526 echo "[branch] autosetuprebase" >> .git/config &&
527 test_must_fail git branch &&
528 git config --unset branch.autosetuprebase
529 '
531 test_expect_success 'attempt to delete a branch without base and unmerged to HEAD' '
532 git checkout my9 &&
533 git config --unset branch.my8.merge &&
534 test_must_fail git branch -d my8
535 '
537 test_expect_success 'attempt to delete a branch merged to its base' '
538 # we are on my9 which is the initial commit; traditionally
539 # we would not have allowed deleting my8 that is not merged
540 # to my9, but it is set to track master that already has my8
541 git config branch.my8.merge refs/heads/master &&
542 git branch -d my8
543 '
545 test_expect_success 'attempt to delete a branch merged to its base' '
546 git checkout master &&
547 echo Third >>A &&
548 git commit -m "Third commit" A &&
549 git branch -t my10 my9 &&
550 git branch -f my10 HEAD^ &&
551 # we are on master which is at the third commit, and my10
552 # is behind us, so traditionally we would have allowed deleting
553 # it; but my10 is set to track my9 that is further behind.
554 test_must_fail git branch -d my10
555 '
557 test_expect_success 'use set-upstream on the current branch' '
558 git checkout master &&
559 git --bare init myupstream.git &&
560 git push myupstream.git master:refs/heads/frotz &&
561 git remote add origin myupstream.git &&
562 git fetch &&
563 git branch --set-upstream master origin/frotz &&
565 test "z$(git config branch.master.remote)" = "zorigin" &&
566 test "z$(git config branch.master.merge)" = "zrefs/heads/frotz"
568 '
570 test_done