3 # Copyright (c) 2010 Erick Mattos
6 test_description
='git checkout --orphan
8 Main Tests for --orphan functionality.'
10 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
=main
11 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
13 TEST_PASSES_SANITIZE_LEAK
=true
18 test_expect_success
'Setup' '
19 echo "Initial" >"$TEST_FILE" &&
20 git add "$TEST_FILE" &&
21 git commit -m "First Commit" &&
23 echo "State 1" >>"$TEST_FILE" &&
24 git add "$TEST_FILE" &&
26 git commit -m "Second Commit"
29 test_expect_success
'--orphan creates a new orphan branch from HEAD' '
30 git checkout --orphan alpha &&
31 test_must_fail git rev-parse --verify HEAD &&
32 test "refs/heads/alpha" = "$(git symbolic-ref HEAD)" &&
34 git commit -m "Third Commit" &&
35 test_must_fail git rev-parse --verify HEAD^ &&
36 git diff-tree --quiet main alpha
39 test_expect_success
'--orphan creates a new orphan branch from <start_point>' '
41 git checkout --orphan beta main^ &&
42 test_must_fail git rev-parse --verify HEAD &&
43 test "refs/heads/beta" = "$(git symbolic-ref HEAD)" &&
45 git commit -m "Fourth Commit" &&
46 test_must_fail git rev-parse --verify HEAD^ &&
47 git diff-tree --quiet main^ beta
50 test_expect_success
'--orphan must be rejected with -b' '
52 test_must_fail git checkout --orphan new -b newer &&
53 test refs/heads/main = "$(git symbolic-ref HEAD)"
56 test_expect_success
'--orphan must be rejected with -t' '
58 test_must_fail git checkout --orphan new -t main &&
59 test refs/heads/main = "$(git symbolic-ref HEAD)"
62 test_expect_success
'--orphan ignores branch.autosetupmerge' '
64 git config branch.autosetupmerge always &&
65 git checkout --orphan gamma &&
66 test_cmp_config "" --default "" branch.gamma.merge &&
67 test refs/heads/gamma = "$(git symbolic-ref HEAD)" &&
68 test_must_fail git rev-parse --verify HEAD^ &&
70 git config branch.autosetupmerge inherit &&
71 git checkout --orphan eta &&
72 test_cmp_config "" --default "" branch.eta.merge &&
73 test_cmp_config "" --default "" branch.eta.remote &&
74 echo refs/heads/eta >expected &&
75 git symbolic-ref HEAD >actual &&
76 test_cmp expected actual &&
77 test_must_fail git rev-parse --verify HEAD^
80 test_expect_success
'--orphan makes reflog by default' '
82 git config --unset core.logAllRefUpdates &&
83 git checkout --orphan delta &&
84 test_must_fail git rev-parse --verify delta@{0} &&
85 git commit -m Delta &&
86 git rev-parse --verify delta@{0}
89 test_expect_success REFFILES
'--orphan does not make reflog when core.logAllRefUpdates = false' '
91 git config core.logAllRefUpdates false &&
92 git checkout --orphan epsilon &&
93 test_must_fail git rev-parse --verify epsilon@{0} &&
94 git commit -m Epsilon &&
95 test_must_fail git rev-parse --verify epsilon@{0}
98 test_expect_success
'--orphan with -l makes reflog when core.logAllRefUpdates = false' '
100 git checkout -l --orphan zeta &&
101 test_must_fail git rev-parse --verify zeta@{0} &&
102 git commit -m Zeta &&
103 git rev-parse --verify zeta@{0}
106 test_expect_success
'giving up --orphan not committed when -l and core.logAllRefUpdates = false deletes reflog' '
108 git checkout -l --orphan eta &&
109 test_must_fail git rev-parse --verify eta@{0} &&
111 test_must_fail git rev-parse --verify eta@{0}
114 test_expect_success
'--orphan is rejected with an existing name' '
116 test_must_fail git checkout --orphan main &&
117 test refs/heads/main = "$(git symbolic-ref HEAD)"
120 test_expect_success
'--orphan refuses to switch if a merge is needed' '
123 echo local >>"$TEST_FILE" &&
124 cat "$TEST_FILE" >"$TEST_FILE.saved" &&
125 test_must_fail git checkout --orphan new main^ &&
126 test refs/heads/main = "$(git symbolic-ref HEAD)" &&
127 test_cmp "$TEST_FILE" "$TEST_FILE.saved" &&
128 git diff-index --quiet --cached HEAD &&
132 test_expect_success
'cannot --detach on an unborn branch' '
134 git checkout --orphan new &&
135 test_must_fail git checkout --detach