]> asedeno.scripts.mit.edu Git - git.git/blob - t/t2017-checkout-orphan.sh
81cb393a95a3e3b0a172b6016d32cade8dbafdb4
[git.git] / t / t2017-checkout-orphan.sh
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
10 . ./test-lib.sh
11
12 TEST_FILE=foo
13
14 test_expect_success 'Setup' '
15         echo "Initial" >"$TEST_FILE" &&
16         git add "$TEST_FILE" &&
17         git commit -m "First Commit"
18         test_tick &&
19         echo "State 1" >>"$TEST_FILE" &&
20         git add "$TEST_FILE" &&
21         test_tick &&
22         git commit -m "Second Commit"
23 '
24
25 test_expect_success '--orphan creates a new orphan branch from HEAD' '
26         git checkout --orphan alpha &&
27         test_must_fail git rev-parse --verify HEAD &&
28         test "refs/heads/alpha" = "$(git symbolic-ref HEAD)" &&
29         test_tick &&
30         git commit -m "Third Commit" &&
31         test_must_fail git rev-parse --verify HEAD^ &&
32         git diff-tree --quiet master alpha
33 '
34
35 test_expect_success '--orphan creates a new orphan branch from <start_point>' '
36         git checkout master &&
37         git checkout --orphan beta master^ &&
38         test_must_fail git rev-parse --verify HEAD &&
39         test "refs/heads/beta" = "$(git symbolic-ref HEAD)" &&
40         test_tick &&
41         git commit -m "Fourth Commit" &&
42         test_must_fail git rev-parse --verify HEAD^ &&
43         git diff-tree --quiet master^ beta
44 '
45
46 test_expect_success '--orphan must be rejected with -b' '
47         git checkout master &&
48         test_must_fail git checkout --orphan new -b newer &&
49         test refs/heads/master = "$(git symbolic-ref HEAD)"
50 '
51
52 test_expect_success '--orphan must be rejected with -t' '
53         git checkout master &&
54         test_must_fail git checkout --orphan new -t master &&
55         test refs/heads/master = "$(git symbolic-ref HEAD)"
56 '
57
58 test_expect_success '--orphan ignores branch.autosetupmerge' '
59         git checkout master &&
60         git config branch.autosetupmerge always &&
61         git checkout --orphan gamma &&
62         test -z "$(git config branch.gamma.merge)" &&
63         test refs/heads/gamma = "$(git symbolic-ref HEAD)" &&
64         test_must_fail git rev-parse --verify HEAD^
65 '
66
67 test_expect_success '--orphan makes reflog by default' '
68         git checkout master &&
69         git config --unset core.logAllRefUpdates &&
70         git checkout --orphan delta &&
71         ! test -f .git/logs/refs/heads/delta &&
72         (
73                 PAGER= &&
74                 export PAGER &&
75                 test_must_fail git reflog show delta
76         ) &&
77         git commit -m Delta &&
78         test -f .git/logs/refs/heads/delta &&
79         PAGER= git reflog show delta
80 '
81
82 test_expect_success '--orphan does not make reflog when core.logAllRefUpdates = false' '
83         git checkout master &&
84         git config core.logAllRefUpdates false &&
85         git checkout --orphan epsilon &&
86         ! test -f .git/logs/refs/heads/epsilon &&
87         (
88                 PAGER= &&
89                 export PAGER &&
90                 test_must_fail git reflog show epsilon
91         ) &&
92         git commit -m Epsilon &&
93         ! test -f .git/logs/refs/heads/epsilon &&
94         (
95                 PAGER= &&
96                 export PAGER &&
97                 test_must_fail git reflog show epsilon
98         )
99 '
100
101 test_expect_success '--orphan with -l makes reflog when core.logAllRefUpdates = false' '
102         git checkout master &&
103         git checkout -l --orphan zeta &&
104         test -f .git/logs/refs/heads/zeta &&
105         (
106                 PAGER= &&
107                 export PAGER &&
108                 test_must_fail git reflog show zeta
109         ) &&
110         git commit -m Zeta &&
111         PAGER= git reflog show zeta
112 '
113
114 test_expect_success 'giving up --orphan not committed when -l and core.logAllRefUpdates = false deletes reflog' '
115         git checkout master &&
116         git checkout -l --orphan eta &&
117         test -f .git/logs/refs/heads/eta &&
118         (
119                 PAGER= &&
120                 export PAGER &&
121                 test_must_fail git reflog show eta
122         ) &&
123         git checkout master &&
124         ! test -f .git/logs/refs/heads/eta &&
125         (
126                 PAGER= &&
127                 export PAGER &&
128                 test_must_fail git reflog show eta
129         )
130 '
131
132 test_expect_success '--orphan is rejected with an existing name' '
133         git checkout master &&
134         test_must_fail git checkout --orphan master &&
135         test refs/heads/master = "$(git symbolic-ref HEAD)"
136 '
137
138 test_expect_success '--orphan refuses to switch if a merge is needed' '
139         git checkout master &&
140         git reset --hard &&
141         echo local >>"$TEST_FILE" &&
142         cat "$TEST_FILE" >"$TEST_FILE.saved" &&
143         test_must_fail git checkout --orphan new master^ &&
144         test refs/heads/master = "$(git symbolic-ref HEAD)" &&
145         test_cmp "$TEST_FILE" "$TEST_FILE.saved" &&
146         git diff-index --quiet --cached HEAD &&
147         git reset --hard
148 '
149
150 test_done