]>
Commit | Line | Data |
---|---|---|
9db5ebf4 EM |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2010 Erick Mattos | |
4 | # | |
5 | ||
6 | test_description='git checkout --orphan | |
7 | ||
8 | Main Tests for --orphan functionality.' | |
9 | ||
883b98ef | 10 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
334afbc7 JS |
11 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
12 | ||
9081a421 | 13 | TEST_PASSES_SANITIZE_LEAK=true |
9db5ebf4 EM |
14 | . ./test-lib.sh |
15 | ||
16 | TEST_FILE=foo | |
17 | ||
18 | test_expect_success 'Setup' ' | |
19 | echo "Initial" >"$TEST_FILE" && | |
20 | git add "$TEST_FILE" && | |
a48fcd83 | 21 | git commit -m "First Commit" && |
9db5ebf4 EM |
22 | test_tick && |
23 | echo "State 1" >>"$TEST_FILE" && | |
24 | git add "$TEST_FILE" && | |
25 | test_tick && | |
26 | git commit -m "Second Commit" | |
27 | ' | |
28 | ||
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)" && | |
33 | test_tick && | |
34 | git commit -m "Third Commit" && | |
35 | test_must_fail git rev-parse --verify HEAD^ && | |
883b98ef | 36 | git diff-tree --quiet main alpha |
9db5ebf4 EM |
37 | ' |
38 | ||
39 | test_expect_success '--orphan creates a new orphan branch from <start_point>' ' | |
883b98ef JS |
40 | git checkout main && |
41 | git checkout --orphan beta main^ && | |
9db5ebf4 EM |
42 | test_must_fail git rev-parse --verify HEAD && |
43 | test "refs/heads/beta" = "$(git symbolic-ref HEAD)" && | |
44 | test_tick && | |
45 | git commit -m "Fourth Commit" && | |
46 | test_must_fail git rev-parse --verify HEAD^ && | |
883b98ef | 47 | git diff-tree --quiet main^ beta |
9db5ebf4 EM |
48 | ' |
49 | ||
50 | test_expect_success '--orphan must be rejected with -b' ' | |
883b98ef | 51 | git checkout main && |
9db5ebf4 | 52 | test_must_fail git checkout --orphan new -b newer && |
883b98ef | 53 | test refs/heads/main = "$(git symbolic-ref HEAD)" |
9db5ebf4 EM |
54 | ' |
55 | ||
3631bf77 | 56 | test_expect_success '--orphan must be rejected with -t' ' |
883b98ef JS |
57 | git checkout main && |
58 | test_must_fail git checkout --orphan new -t main && | |
59 | test refs/heads/main = "$(git symbolic-ref HEAD)" | |
3631bf77 EM |
60 | ' |
61 | ||
62 | test_expect_success '--orphan ignores branch.autosetupmerge' ' | |
883b98ef | 63 | git checkout main && |
3631bf77 EM |
64 | git config branch.autosetupmerge always && |
65 | git checkout --orphan gamma && | |
d3115660 | 66 | test_cmp_config "" --default "" branch.gamma.merge && |
3631bf77 | 67 | test refs/heads/gamma = "$(git symbolic-ref HEAD)" && |
d3115660 JS |
68 | test_must_fail git rev-parse --verify HEAD^ && |
69 | git checkout main && | |
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 && | |
3631bf77 EM |
77 | test_must_fail git rev-parse --verify HEAD^ |
78 | ' | |
79 | ||
80 | test_expect_success '--orphan makes reflog by default' ' | |
883b98ef | 81 | git checkout main && |
3631bf77 EM |
82 | git config --unset core.logAllRefUpdates && |
83 | git checkout --orphan delta && | |
6e6842e3 | 84 | test_must_fail git rev-parse --verify delta@{0} && |
3631bf77 | 85 | git commit -m Delta && |
6e6842e3 | 86 | git rev-parse --verify delta@{0} |
3631bf77 EM |
87 | ' |
88 | ||
4e8df1a3 | 89 | test_expect_success '--orphan does not make reflog when core.logAllRefUpdates = false' ' |
883b98ef | 90 | git checkout main && |
3631bf77 EM |
91 | git config core.logAllRefUpdates false && |
92 | git checkout --orphan epsilon && | |
6e6842e3 | 93 | test_must_fail git rev-parse --verify epsilon@{0} && |
3631bf77 | 94 | git commit -m Epsilon && |
6e6842e3 | 95 | test_must_fail git rev-parse --verify epsilon@{0} |
3631bf77 EM |
96 | ' |
97 | ||
98 | test_expect_success '--orphan with -l makes reflog when core.logAllRefUpdates = false' ' | |
883b98ef | 99 | git checkout main && |
3631bf77 | 100 | git checkout -l --orphan zeta && |
6e6842e3 | 101 | test_must_fail git rev-parse --verify zeta@{0} && |
3631bf77 | 102 | git commit -m Zeta && |
6e6842e3 | 103 | git rev-parse --verify zeta@{0} |
3631bf77 EM |
104 | ' |
105 | ||
106 | test_expect_success 'giving up --orphan not committed when -l and core.logAllRefUpdates = false deletes reflog' ' | |
883b98ef | 107 | git checkout main && |
3631bf77 | 108 | git checkout -l --orphan eta && |
6e6842e3 | 109 | test_must_fail git rev-parse --verify eta@{0} && |
883b98ef | 110 | git checkout main && |
6e6842e3 | 111 | test_must_fail git rev-parse --verify eta@{0} |
3631bf77 EM |
112 | ' |
113 | ||
9db5ebf4 | 114 | test_expect_success '--orphan is rejected with an existing name' ' |
883b98ef JS |
115 | git checkout main && |
116 | test_must_fail git checkout --orphan main && | |
117 | test refs/heads/main = "$(git symbolic-ref HEAD)" | |
9db5ebf4 EM |
118 | ' |
119 | ||
120 | test_expect_success '--orphan refuses to switch if a merge is needed' ' | |
883b98ef | 121 | git checkout main && |
9db5ebf4 EM |
122 | git reset --hard && |
123 | echo local >>"$TEST_FILE" && | |
124 | cat "$TEST_FILE" >"$TEST_FILE.saved" && | |
883b98ef JS |
125 | test_must_fail git checkout --orphan new main^ && |
126 | test refs/heads/main = "$(git symbolic-ref HEAD)" && | |
9db5ebf4 EM |
127 | test_cmp "$TEST_FILE" "$TEST_FILE.saved" && |
128 | git diff-index --quiet --cached HEAD && | |
129 | git reset --hard | |
130 | ' | |
131 | ||
8ced1aa0 | 132 | test_expect_success 'cannot --detach on an unborn branch' ' |
883b98ef | 133 | git checkout main && |
8ced1aa0 CW |
134 | git checkout --orphan new && |
135 | test_must_fail git checkout --detach | |
136 | ' | |
137 | ||
9db5ebf4 | 138 | test_done |