Merge branch 'jc/calloc-fix'
[git] / t / t2011-checkout-invalid-head.sh
1 #!/bin/sh
2
3 test_description='checkout switching away from an invalid branch'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 test_expect_success 'setup' '
11         echo hello >world &&
12         git add world &&
13         git commit -m initial
14 '
15
16 test_expect_success 'checkout should not start branch from a tree' '
17         test_must_fail git checkout -b newbranch main^{tree}
18 '
19
20 test_expect_success 'checkout main from invalid HEAD' '
21         echo $ZERO_OID >.git/HEAD &&
22         git checkout main --
23 '
24
25 test_expect_success 'checkout notices failure to lock HEAD' '
26         test_when_finished "rm -f .git/HEAD.lock" &&
27         >.git/HEAD.lock &&
28         test_must_fail git checkout -b other
29 '
30
31 test_expect_success 'create ref directory/file conflict scenario' '
32         git update-ref refs/heads/outer/inner main &&
33
34         # do not rely on symbolic-ref to get a known state,
35         # as it may use the same code we are testing
36         reset_to_df () {
37                 echo "ref: refs/heads/outer" >.git/HEAD
38         }
39 '
40
41 test_expect_success 'checkout away from d/f HEAD (unpacked, to branch)' '
42         reset_to_df &&
43         git checkout main
44 '
45
46 test_expect_success 'checkout away from d/f HEAD (unpacked, to detached)' '
47         reset_to_df &&
48         git checkout --detach main
49 '
50
51 test_expect_success 'pack refs' '
52         git pack-refs --all --prune
53 '
54
55 test_expect_success 'checkout away from d/f HEAD (packed, to branch)' '
56         reset_to_df &&
57         git checkout main
58 '
59
60 test_expect_success 'checkout away from d/f HEAD (packed, to detached)' '
61         reset_to_df &&
62         git checkout --detach main
63 '
64 test_done