3 # Copyright (c) 2005 Amos Waterland
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.
14 'prepare a trivial repository' \
16 git update-index --add A &&
17 git commit -m "Initial commit." &&
19 git update-index --add A &&
20 git commit -m "Second commit." &&
21 HEAD=$(git rev-parse --verify HEAD)'
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
29 test_expect_success
'branch -h in broken repository' '
34 >.git/refs/heads/master &&
35 test_expect_code 129 git branch -h >usage 2>&1
37 grep "[Uu]sage" broken/usage
41 'git branch abc should create a branch' \
42 'git branch abc && test -f .git/refs/heads/abc'
45 'git branch a/b/c should create a branch' \
46 'git branch a/b/c && test -f .git/refs/heads/a/b/c'
49 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
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'
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'
66 'git branch j/k should work after branch j has been deleted' \
72 'git branch l should work after branch l/m has been deleted' \
78 'git branch -m m m/m should work' \
80 git branch -m m m/m &&
81 test -f .git/logs/refs/heads/m/m'
84 'git branch -m n/n n should work' \
87 test -f .git/logs/refs/heads/n'
89 test_expect_success
'git branch -m o/o o should fail when o/p exists' '
92 test_must_fail git branch -m o/o o
95 test_expect_success
'git branch -m q r/q should fail when r exists' '
98 test_must_fail git branch -m q r/q
101 mv .git
/config .git
/config-saved
103 test_expect_success
'git branch -m q q2 without config should succeed' '
104 git branch -m q q2 &&
108 mv .git
/config-saved .git
/config
110 git config branch.s
/s.dummy Hello
112 test_expect_success \
113 'git branch -m s/s s should work when s/t is deleted' \
114 'git branch -l s/s &&
115 test -f .git/logs/refs/heads/s/s &&
117 test -f .git/logs/refs/heads/s/t &&
119 git branch -m s/s s &&
120 test -f .git/logs/refs/heads/s'
122 test_expect_success
'config information was renamed, too' \
123 "test $(git config branch.s.dummy) = Hello &&
124 test_must_fail git config branch.s/s/dummy"
126 test_expect_success
'renaming a symref is not allowed' \
128 git symbolic-ref refs/heads/master2 refs/heads/master &&
129 test_must_fail git branch -m master2 master3 &&
130 git symbolic-ref refs/heads/master2 &&
131 test -f .git/refs/heads/master &&
132 ! test -f .git/refs/heads/master3
135 test_expect_success SYMLINKS \
136 'git branch -m u v should fail when the reflog for u is a symlink' '
138 mv .git/logs/refs/heads/u real-u &&
139 ln -s real-u .git/logs/refs/heads/u &&
140 test_must_fail git branch -m u v
143 test_expect_success
'test tracking setup via --track' \
144 'git config remote.local.url . &&
145 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
146 (git show-ref -q refs/remotes/local/master || git fetch local) &&
147 git branch --track my1 local/master &&
148 test $(git config branch.my1.remote) = local &&
149 test $(git config branch.my1.merge) = refs/heads/master'
151 test_expect_success
'test tracking setup (non-wildcard, matching)' \
152 'git config remote.local.url . &&
153 git config remote.local.fetch refs/heads/master:refs/remotes/local/master &&
154 (git show-ref -q refs/remotes/local/master || git fetch local) &&
155 git branch --track my4 local/master &&
156 test $(git config branch.my4.remote) = local &&
157 test $(git config branch.my4.merge) = refs/heads/master'
159 test_expect_success
'test tracking setup (non-wildcard, not matching)' \
160 'git config remote.local.url . &&
161 git config remote.local.fetch refs/heads/s:refs/remotes/local/s &&
162 (git show-ref -q refs/remotes/local/master || git fetch local) &&
163 git branch --track my5 local/master &&
164 ! test "$(git config branch.my5.remote)" = local &&
165 ! test "$(git config branch.my5.merge)" = refs/heads/master'
167 test_expect_success
'test tracking setup via config' \
168 'git config branch.autosetupmerge true &&
169 git config remote.local.url . &&
170 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
171 (git show-ref -q refs/remotes/local/master || git fetch local) &&
172 git branch my3 local/master &&
173 test $(git config branch.my3.remote) = local &&
174 test $(git config branch.my3.merge) = refs/heads/master'
176 test_expect_success
'test overriding tracking setup via --no-track' \
177 'git config branch.autosetupmerge true &&
178 git config remote.local.url . &&
179 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
180 (git show-ref -q refs/remotes/local/master || git fetch local) &&
181 git branch --no-track my2 local/master &&
182 git config branch.autosetupmerge false &&
183 ! test "$(git config branch.my2.remote)" = local &&
184 ! test "$(git config branch.my2.merge)" = refs/heads/master'
186 test_expect_success
'no tracking without .fetch entries' \
187 'git config branch.autosetupmerge true &&
189 git config branch.automsetupmerge false &&
190 test -z "$(git config branch.my6.remote)" &&
191 test -z "$(git config branch.my6.merge)"'
193 test_expect_success
'test tracking setup via --track but deeper' \
194 'git config remote.local.url . &&
195 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
196 (git show-ref -q refs/remotes/local/o/o || git fetch local) &&
197 git branch --track my7 local/o/o &&
198 test "$(git config branch.my7.remote)" = local &&
199 test "$(git config branch.my7.merge)" = refs/heads/o/o'
201 test_expect_success
'test deleting branch deletes branch config' \
202 'git branch -d my7 &&
203 test -z "$(git config branch.my7.remote)" &&
204 test -z "$(git config branch.my7.merge)"'
206 test_expect_success
'test deleting branch without config' \
208 sha1=$(git rev-parse my7 | cut -c 1-7) &&
209 echo "Deleted branch my7 (was $sha1)." >expect &&
210 git branch -d my7 >actual 2>&1 &&
211 test_i18ncmp expect actual'
213 test_expect_success
'test --track without .fetch entries' \
214 'git branch --track my8 &&
215 test "$(git config branch.my8.remote)" &&
216 test "$(git config branch.my8.merge)"'
218 test_expect_success \
219 'branch from non-branch HEAD w/autosetupmerge=always' \
220 'git config branch.autosetupmerge always &&
221 git branch my9 HEAD^ &&
222 git config branch.autosetupmerge false'
224 test_expect_success \
225 'branch from non-branch HEAD w/--track causes failure' \
226 'test_must_fail git branch --track my10 HEAD^'
228 test_expect_success \
229 'branch from tag w/--track causes failure' \
231 test_must_fail git branch --track my11 foobar'
233 # Keep this test last, as it changes the current branch
235 $_z40 $HEAD $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 1117150200 +0000 branch: Created from master
237 test_expect_success \
238 'git checkout -b g/h/i -l should create a branch and a log' \
239 'GIT_COMMITTER_DATE="2005-05-26 23:30" \
240 git checkout -b g/h/i -l master &&
241 test -f .git/refs/heads/g/h/i &&
242 test -f .git/logs/refs/heads/g/h/i &&
243 test_cmp expect .git/logs/refs/heads/g/h/i'
245 test_expect_success
'checkout -b makes reflog by default' '
246 git checkout master &&
247 git config --unset core.logAllRefUpdates &&
248 git checkout -b alpha &&
249 git rev-parse --verify alpha@{0}
252 test_expect_success
'checkout -b does not make reflog when core.logAllRefUpdates = false' '
253 git checkout master &&
254 git config core.logAllRefUpdates false &&
255 git checkout -b beta &&
256 test_must_fail git rev-parse --verify beta@{0}
259 test_expect_success
'checkout -b with -l makes reflog when core.logAllRefUpdates = false' '
260 git checkout master &&
261 git checkout -lb gamma &&
262 git config --unset core.logAllRefUpdates &&
263 git rev-parse --verify gamma@{0}
266 test_expect_success
'avoid ambiguous track' '
267 git config branch.autosetupmerge true &&
268 git config remote.ambi1.url lalala &&
269 git config remote.ambi1.fetch refs/heads/lalala:refs/heads/master &&
270 git config remote.ambi2.url lilili &&
271 git config remote.ambi2.fetch refs/heads/lilili:refs/heads/master &&
272 git branch all1 master &&
273 test -z "$(git config branch.all1.merge)"
276 test_expect_success
'autosetuprebase local on a tracked local branch' '
277 git config remote.local.url . &&
278 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
279 git config branch.autosetuprebase local &&
280 (git show-ref -q refs/remotes/local/o || git fetch local) &&
282 git branch --track myr1 mybase &&
283 test "$(git config branch.myr1.remote)" = . &&
284 test "$(git config branch.myr1.merge)" = refs/heads/mybase &&
285 test "$(git config branch.myr1.rebase)" = true
288 test_expect_success
'autosetuprebase always 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 always &&
292 (git show-ref -q refs/remotes/local/o || git fetch local) &&
293 git branch mybase2 &&
294 git branch --track myr2 mybase &&
295 test "$(git config branch.myr2.remote)" = . &&
296 test "$(git config branch.myr2.merge)" = refs/heads/mybase &&
297 test "$(git config branch.myr2.rebase)" = true
300 test_expect_success
'autosetuprebase remote 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 remote &&
304 (git show-ref -q refs/remotes/local/o || git fetch local) &&
305 git branch mybase3 &&
306 git branch --track myr3 mybase2 &&
307 test "$(git config branch.myr3.remote)" = . &&
308 test "$(git config branch.myr3.merge)" = refs/heads/mybase2 &&
309 ! test "$(git config branch.myr3.rebase)" = true
312 test_expect_success
'autosetuprebase never 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 never &&
316 (git show-ref -q refs/remotes/local/o || git fetch local) &&
317 git branch mybase4 &&
318 git branch --track myr4 mybase2 &&
319 test "$(git config branch.myr4.remote)" = . &&
320 test "$(git config branch.myr4.merge)" = refs/heads/mybase2 &&
321 ! test "$(git config branch.myr4.rebase)" = true
324 test_expect_success
'autosetuprebase local on a tracked remote branch' '
325 git config remote.local.url . &&
326 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
327 git config branch.autosetuprebase local &&
328 (git show-ref -q refs/remotes/local/master || git fetch local) &&
329 git branch --track myr5 local/master &&
330 test "$(git config branch.myr5.remote)" = local &&
331 test "$(git config branch.myr5.merge)" = refs/heads/master &&
332 ! test "$(git config branch.myr5.rebase)" = true
335 test_expect_success
'autosetuprebase never on a tracked remote branch' '
336 git config remote.local.url . &&
337 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
338 git config branch.autosetuprebase never &&
339 (git show-ref -q refs/remotes/local/master || git fetch local) &&
340 git branch --track myr6 local/master &&
341 test "$(git config branch.myr6.remote)" = local &&
342 test "$(git config branch.myr6.merge)" = refs/heads/master &&
343 ! test "$(git config branch.myr6.rebase)" = true
346 test_expect_success
'autosetuprebase remote on a tracked remote branch' '
347 git config remote.local.url . &&
348 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
349 git config branch.autosetuprebase remote &&
350 (git show-ref -q refs/remotes/local/master || git fetch local) &&
351 git branch --track myr7 local/master &&
352 test "$(git config branch.myr7.remote)" = local &&
353 test "$(git config branch.myr7.merge)" = refs/heads/master &&
354 test "$(git config branch.myr7.rebase)" = true
357 test_expect_success
'autosetuprebase always on a tracked remote branch' '
358 git config remote.local.url . &&
359 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
360 git config branch.autosetuprebase remote &&
361 (git show-ref -q refs/remotes/local/master || git fetch local) &&
362 git branch --track myr8 local/master &&
363 test "$(git config branch.myr8.remote)" = local &&
364 test "$(git config branch.myr8.merge)" = refs/heads/master &&
365 test "$(git config branch.myr8.rebase)" = true
368 test_expect_success
'autosetuprebase unconfigured on a tracked remote branch' '
369 git config --unset branch.autosetuprebase &&
370 git config remote.local.url . &&
371 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
372 (git show-ref -q refs/remotes/local/master || git fetch local) &&
373 git branch --track myr9 local/master &&
374 test "$(git config branch.myr9.remote)" = local &&
375 test "$(git config branch.myr9.merge)" = refs/heads/master &&
376 test "z$(git config branch.myr9.rebase)" = z
379 test_expect_success
'autosetuprebase unconfigured on a tracked local branch' '
380 git config remote.local.url . &&
381 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
382 (git show-ref -q refs/remotes/local/o || git fetch local) &&
383 git branch mybase10 &&
384 git branch --track myr10 mybase2 &&
385 test "$(git config branch.myr10.remote)" = . &&
386 test "$(git config branch.myr10.merge)" = refs/heads/mybase2 &&
387 test "z$(git config branch.myr10.rebase)" = z
390 test_expect_success
'autosetuprebase unconfigured on untracked local branch' '
391 git config remote.local.url . &&
392 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
393 (git show-ref -q refs/remotes/local/master || git fetch local) &&
394 git branch --no-track myr11 mybase2 &&
395 test "z$(git config branch.myr11.remote)" = z &&
396 test "z$(git config branch.myr11.merge)" = z &&
397 test "z$(git config branch.myr11.rebase)" = z
400 test_expect_success
'autosetuprebase unconfigured on untracked remote branch' '
401 git config remote.local.url . &&
402 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
403 (git show-ref -q refs/remotes/local/master || git fetch local) &&
404 git branch --no-track myr12 local/master &&
405 test "z$(git config branch.myr12.remote)" = z &&
406 test "z$(git config branch.myr12.merge)" = z &&
407 test "z$(git config branch.myr12.rebase)" = z
410 test_expect_success
'autosetuprebase never on an untracked local branch' '
411 git config branch.autosetuprebase never &&
412 git config remote.local.url . &&
413 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
414 (git show-ref -q refs/remotes/local/master || git fetch local) &&
415 git branch --no-track myr13 mybase2 &&
416 test "z$(git config branch.myr13.remote)" = z &&
417 test "z$(git config branch.myr13.merge)" = z &&
418 test "z$(git config branch.myr13.rebase)" = z
421 test_expect_success
'autosetuprebase local on an untracked local branch' '
422 git config branch.autosetuprebase local &&
423 git config remote.local.url . &&
424 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
425 (git show-ref -q refs/remotes/local/master || git fetch local) &&
426 git branch --no-track myr14 mybase2 &&
427 test "z$(git config branch.myr14.remote)" = z &&
428 test "z$(git config branch.myr14.merge)" = z &&
429 test "z$(git config branch.myr14.rebase)" = z
432 test_expect_success
'autosetuprebase remote on an untracked local branch' '
433 git config branch.autosetuprebase remote &&
434 git config remote.local.url . &&
435 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
436 (git show-ref -q refs/remotes/local/master || git fetch local) &&
437 git branch --no-track myr15 mybase2 &&
438 test "z$(git config branch.myr15.remote)" = z &&
439 test "z$(git config branch.myr15.merge)" = z &&
440 test "z$(git config branch.myr15.rebase)" = z
443 test_expect_success
'autosetuprebase always on an untracked local branch' '
444 git config branch.autosetuprebase always &&
445 git config remote.local.url . &&
446 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
447 (git show-ref -q refs/remotes/local/master || git fetch local) &&
448 git branch --no-track myr16 mybase2 &&
449 test "z$(git config branch.myr16.remote)" = z &&
450 test "z$(git config branch.myr16.merge)" = z &&
451 test "z$(git config branch.myr16.rebase)" = z
454 test_expect_success
'autosetuprebase never on an untracked remote branch' '
455 git config branch.autosetuprebase never &&
456 git config remote.local.url . &&
457 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
458 (git show-ref -q refs/remotes/local/master || git fetch local) &&
459 git branch --no-track myr17 local/master &&
460 test "z$(git config branch.myr17.remote)" = z &&
461 test "z$(git config branch.myr17.merge)" = z &&
462 test "z$(git config branch.myr17.rebase)" = z
465 test_expect_success
'autosetuprebase local on an untracked remote branch' '
466 git config branch.autosetuprebase local &&
467 git config remote.local.url . &&
468 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
469 (git show-ref -q refs/remotes/local/master || git fetch local) &&
470 git branch --no-track myr18 local/master &&
471 test "z$(git config branch.myr18.remote)" = z &&
472 test "z$(git config branch.myr18.merge)" = z &&
473 test "z$(git config branch.myr18.rebase)" = z
476 test_expect_success
'autosetuprebase remote on an untracked remote branch' '
477 git config branch.autosetuprebase remote &&
478 git config remote.local.url . &&
479 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
480 (git show-ref -q refs/remotes/local/master || git fetch local) &&
481 git branch --no-track myr19 local/master &&
482 test "z$(git config branch.myr19.remote)" = z &&
483 test "z$(git config branch.myr19.merge)" = z &&
484 test "z$(git config branch.myr19.rebase)" = z
487 test_expect_success
'autosetuprebase always on an untracked remote branch' '
488 git config branch.autosetuprebase always &&
489 git config remote.local.url . &&
490 git config remote.local.fetch refs/heads/*:refs/remotes/local/* &&
491 (git show-ref -q refs/remotes/local/master || git fetch local) &&
492 git branch --no-track myr20 local/master &&
493 test "z$(git config branch.myr20.remote)" = z &&
494 test "z$(git config branch.myr20.merge)" = z &&
495 test "z$(git config branch.myr20.rebase)" = z
498 test_expect_success
'autosetuprebase always on detached HEAD' '
499 git config branch.autosetupmerge always &&
500 test_when_finished git checkout master &&
501 git checkout HEAD^0 &&
503 test -z "$(git config branch.my11.remote)" &&
504 test -z "$(git config branch.my11.merge)"
507 test_expect_success
'detect misconfigured autosetuprebase (bad value)' '
508 git config branch.autosetuprebase garbage &&
509 test_must_fail git branch
512 test_expect_success
'detect misconfigured autosetuprebase (no value)' '
513 git config --unset branch.autosetuprebase &&
514 echo "[branch] autosetuprebase" >> .git/config &&
515 test_must_fail git branch &&
516 git config --unset branch.autosetuprebase
519 test_expect_success
'attempt to delete a branch without base and unmerged to HEAD' '
521 git config --unset branch.my8.merge &&
522 test_must_fail git branch -d my8
525 test_expect_success
'attempt to delete a branch merged to its base' '
526 # we are on my9 which is the initial commit; traditionally
527 # we would not have allowed deleting my8 that is not merged
528 # to my9, but it is set to track master that already has my8
529 git config branch.my8.merge refs/heads/master &&
533 test_expect_success
'attempt to delete a branch merged to its base' '
534 git checkout master &&
536 git commit -m "Third commit" A &&
537 git branch -t my10 my9 &&
538 git branch -f my10 HEAD^ &&
539 # we are on master which is at the third commit, and my10
540 # is behind us, so traditionally we would have allowed deleting
541 # it; but my10 is set to track my9 that is further behind.
542 test_must_fail git branch -d my10