object-file: convert 'switch' back to 'if'
[git/debian.git] / t / t2017-checkout-orphan.sh
blob947d1587ac8be9a2ab333049a2196c4e211185e6
1 #!/bin/sh
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
14 . ./test-lib.sh
16 TEST_FILE=foo
18 test_expect_success 'Setup' '
19 echo "Initial" >"$TEST_FILE" &&
20 git add "$TEST_FILE" &&
21 git commit -m "First Commit" &&
22 test_tick &&
23 echo "State 1" >>"$TEST_FILE" &&
24 git add "$TEST_FILE" &&
25 test_tick &&
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)" &&
33 test_tick &&
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>' '
40 git checkout main &&
41 git checkout --orphan beta main^ &&
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^ &&
47 git diff-tree --quiet main^ beta
50 test_expect_success '--orphan must be rejected with -b' '
51 git checkout main &&
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' '
57 git checkout main &&
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' '
63 git checkout main &&
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^ &&
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 &&
77 test_must_fail git rev-parse --verify HEAD^
80 test_expect_success '--orphan makes reflog by default' '
81 git checkout main &&
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' '
90 git checkout main &&
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' '
99 git checkout main &&
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' '
107 git checkout main &&
108 git checkout -l --orphan eta &&
109 test_must_fail git rev-parse --verify eta@{0} &&
110 git checkout main &&
111 test_must_fail git rev-parse --verify eta@{0}
114 test_expect_success '--orphan is rejected with an existing name' '
115 git checkout main &&
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' '
121 git checkout main &&
122 git reset --hard &&
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 &&
129 git reset --hard
132 test_expect_success 'cannot --detach on an unborn branch' '
133 git checkout main &&
134 git checkout --orphan new &&
135 test_must_fail git checkout --detach
138 test_done