]> git.ipfire.org Git - thirdparty/git.git/blame - t/t2020-checkout-detach.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[thirdparty/git.git] / t / t2020-checkout-detach.sh
CommitLineData
32669671
JH
1#!/bin/sh
2
3test_description='checkout into detached HEAD state'
883b98ef 4GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
334afbc7
JS
5export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
6
f92dbdbc 7TEST_PASSES_SANITIZE_LEAK=true
32669671
JH
8. ./test-lib.sh
9
10check_detached () {
11 test_must_fail git symbolic-ref -q HEAD >/dev/null
12}
13
14check_not_detached () {
15 git symbolic-ref -q HEAD >/dev/null
16}
17
83933c98 18PREV_HEAD_DESC='Previous HEAD position was'
493dd6ed 19check_orphan_warning() {
f9492099 20 test_i18ngrep "you are leaving $2 behind" "$1" &&
83933c98 21 test_i18ngrep ! "$PREV_HEAD_DESC" "$1"
493dd6ed
JK
22}
23check_no_orphan_warning() {
f9492099 24 test_i18ngrep ! "you are leaving .* commit.*behind" "$1" &&
83933c98 25 test_i18ngrep "$PREV_HEAD_DESC" "$1"
493dd6ed
JK
26}
27
32669671 28reset () {
883b98ef 29 git checkout main &&
32669671
JH
30 check_not_detached
31}
32
33test_expect_success 'setup' '
34 test_commit one &&
35 test_commit two &&
493dd6ed
JK
36 test_commit three && git tag -d three &&
37 test_commit four && git tag -d four &&
32669671
JH
38 git branch branch &&
39 git tag tag
40'
41
42test_expect_success 'checkout branch does not detach' '
43 reset &&
44 git checkout branch &&
45 check_not_detached
46'
47
48test_expect_success 'checkout tag detaches' '
49 reset &&
50 git checkout tag &&
51 check_detached
52'
53
54test_expect_success 'checkout branch by full name detaches' '
55 reset &&
56 git checkout refs/heads/branch &&
57 check_detached
58'
59
60test_expect_success 'checkout non-ref detaches' '
61 reset &&
62 git checkout branch^ &&
63 check_detached
64'
65
66test_expect_success 'checkout ref^0 detaches' '
67 reset &&
68 git checkout branch^0 &&
69 check_detached
70'
71
72test_expect_success 'checkout --detach detaches' '
73 reset &&
74 git checkout --detach branch &&
75 check_detached
76'
77
78test_expect_success 'checkout --detach without branch name' '
79 reset &&
80 git checkout --detach &&
81 check_detached
82'
83
84test_expect_success 'checkout --detach errors out for non-commit' '
85 reset &&
86 test_must_fail git checkout --detach one^{tree} &&
87 check_not_detached
88'
89
90test_expect_success 'checkout --detach errors out for extra argument' '
91 reset &&
883b98ef 92 git checkout main &&
32669671
JH
93 test_must_fail git checkout --detach tag one.t &&
94 check_not_detached
95'
96
97test_expect_success 'checkout --detached and -b are incompatible' '
98 reset &&
99 test_must_fail git checkout --detach -b newbranch tag &&
100 check_not_detached
101'
102
103test_expect_success 'checkout --detach moves HEAD' '
104 reset &&
105 git checkout one &&
106 git checkout --detach two &&
107 git diff --exit-code HEAD &&
108 git diff --exit-code two
109'
110
493dd6ed
JK
111test_expect_success 'checkout warns on orphan commits' '
112 reset &&
113 git checkout --detach two &&
114 echo content >orphan &&
115 git add orphan &&
f9492099
JS
116 git commit -a -m orphan1 &&
117 echo new content >orphan &&
118 git commit -a -m orphan2 &&
119 orphan2=$(git rev-parse HEAD) &&
883b98ef 120 git checkout main 2>stderr
f06f08b7
ÆAB
121'
122
f2c8c800 123test_expect_success 'checkout warns on orphan commits: output' '
f9492099
JS
124 check_orphan_warning stderr "2 commits"
125'
126
127test_expect_success 'checkout warns orphaning 1 of 2 commits' '
128 git checkout "$orphan2" &&
129 git checkout HEAD^ 2>stderr
130'
131
132test_expect_success 'checkout warns orphaning 1 of 2 commits: output' '
5d886395 133 check_orphan_warning stderr "1 commit"
493dd6ed
JK
134'
135
136test_expect_success 'checkout does not warn leaving ref tip' '
137 reset &&
138 git checkout --detach two &&
883b98ef 139 git checkout main 2>stderr
f06f08b7
ÆAB
140'
141
f2c8c800 142test_expect_success 'checkout does not warn leaving ref tip' '
493dd6ed
JK
143 check_no_orphan_warning stderr
144'
145
146test_expect_success 'checkout does not warn leaving reachable commit' '
147 reset &&
148 git checkout --detach HEAD^ &&
883b98ef 149 git checkout main 2>stderr
f06f08b7
ÆAB
150'
151
f2c8c800 152test_expect_success 'checkout does not warn leaving reachable commit' '
493dd6ed
JK
153 check_no_orphan_warning stderr
154'
155
5c08dc48 156cat >expect <<'EOF'
883b98ef 157Your branch is behind 'main' by 1 commit, and can be fast-forwarded.
c190ced6 158 (use "git pull" to update your local branch)
5c08dc48
JK
159EOF
160test_expect_success 'tracking count is accurate after orphan check' '
161 reset &&
883b98ef 162 git branch child main^ &&
5c08dc48 163 git config branch.child.remote . &&
883b98ef 164 git config branch.child.merge refs/heads/main &&
5c08dc48
JK
165 git checkout child^ &&
166 git checkout child >stdout &&
1108cea7 167 test_cmp expect stdout
5c08dc48
JK
168'
169
779b88a9
SB
170test_expect_success 'no advice given for explicit detached head state' '
171 # baseline
172 test_config advice.detachedHead true &&
173 git checkout child && git checkout HEAD^0 >expect.advice 2>&1 &&
174 test_config advice.detachedHead false &&
175 git checkout child && git checkout HEAD^0 >expect.no-advice 2>&1 &&
176 test_unconfig advice.detachedHead &&
177 # without configuration, the advice.* variables default to true
178 git checkout child && git checkout HEAD^0 >actual 2>&1 &&
179 test_cmp expect.advice actual &&
180
181 # with explicit --detach
182 # no configuration
183 test_unconfig advice.detachedHead &&
184 git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
185 test_cmp expect.no-advice actual &&
186
187 # explicitly decline advice
188 test_config advice.detachedHead false &&
189 git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
190 test_cmp expect.no-advice actual
191'
192
ca69d4d5
AR
193# Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (new format)
194test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not asked to' "
195
883b98ef
JS
196 commit=$(git rev-parse --short=12 main^) &&
197 commit2=$(git rev-parse --short=12 main~2) &&
198 commit3=$(git rev-parse --short=12 main~3) &&
ae2f8d5b 199
ca69d4d5 200 # The first detach operation is more chatty than the following ones.
ae2f8d5b 201 cat >1st_detach <<-EOF &&
328c6cb8 202 Note: switching to 'HEAD^'.
ca69d4d5
AR
203
204 You are in 'detached HEAD' state. You can look around, make experimental
205 changes and commit them, and you can discard any commits you make in this
328c6cb8 206 state without impacting any branches by switching back to a branch.
ca69d4d5
AR
207
208 If you want to create a new branch to retain commits you create, you may
328c6cb8 209 do so (now or later) by using -c with the switch command. Example:
ca69d4d5 210
328c6cb8 211 git switch -c <new-branch-name>
ca69d4d5 212
328c6cb8 213 Or undo this operation with:
af9ded5b 214
328c6cb8 215 git switch -
af9ded5b
NTND
216
217 Turn off this advice by setting config variable advice.detachedHead to false
218
ae2f8d5b 219 HEAD is now at \$commit three
ca69d4d5
AR
220 EOF
221
222 # The remaining ones just show info about previous and current HEADs.
ae2f8d5b 223 cat >2nd_detach <<-EOF &&
224 Previous HEAD position was \$commit three
225 HEAD is now at \$commit2 two
ca69d4d5
AR
226 EOF
227
ae2f8d5b 228 cat >3rd_detach <<-EOF &&
229 Previous HEAD position was \$commit2 two
230 HEAD is now at \$commit3 one
ca69d4d5
AR
231 EOF
232
233 reset &&
234 check_not_detached &&
235
236 # Various ways of *not* asking for ellipses
237
238 sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
239 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
240 check_detached &&
1108cea7 241 test_cmp 1st_detach actual &&
ca69d4d5
AR
242
243 GIT_PRINT_SHA1_ELLIPSIS="no" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
244 check_detached &&
1108cea7 245 test_cmp 2nd_detach actual &&
ca69d4d5
AR
246
247 GIT_PRINT_SHA1_ELLIPSIS= git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
248 check_detached &&
1108cea7 249 test_cmp 3rd_detach actual &&
ca69d4d5
AR
250
251 sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
252
253 # We only have four commits, but we can re-use them
254 reset &&
255 check_not_detached &&
256
257 # Make no mention of the env var at all
258 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
259 check_detached &&
1108cea7 260 test_cmp 1st_detach actual &&
ca69d4d5
AR
261
262 GIT_PRINT_SHA1_ELLIPSIS='nope' &&
263 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
264 check_detached &&
1108cea7 265 test_cmp 2nd_detach actual &&
ca69d4d5
AR
266
267 GIT_PRINT_SHA1_ELLIPSIS=nein &&
268 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
269 check_detached &&
1108cea7 270 test_cmp 3rd_detach actual &&
ca69d4d5
AR
271
272 true
273"
274
275# Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (old format)
276test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked to' "
277
883b98ef
JS
278 commit=$(git rev-parse --short=12 main^) &&
279 commit2=$(git rev-parse --short=12 main~2) &&
280 commit3=$(git rev-parse --short=12 main~3) &&
ae2f8d5b 281
ca69d4d5 282 # The first detach operation is more chatty than the following ones.
ae2f8d5b 283 cat >1st_detach <<-EOF &&
328c6cb8 284 Note: switching to 'HEAD^'.
ca69d4d5
AR
285
286 You are in 'detached HEAD' state. You can look around, make experimental
287 changes and commit them, and you can discard any commits you make in this
328c6cb8 288 state without impacting any branches by switching back to a branch.
ca69d4d5
AR
289
290 If you want to create a new branch to retain commits you create, you may
328c6cb8 291 do so (now or later) by using -c with the switch command. Example:
ca69d4d5 292
328c6cb8 293 git switch -c <new-branch-name>
ca69d4d5 294
328c6cb8 295 Or undo this operation with:
af9ded5b 296
328c6cb8 297 git switch -
af9ded5b
NTND
298
299 Turn off this advice by setting config variable advice.detachedHead to false
300
ae2f8d5b 301 HEAD is now at \$commit... three
ca69d4d5
AR
302 EOF
303
304 # The remaining ones just show info about previous and current HEADs.
ae2f8d5b 305 cat >2nd_detach <<-EOF &&
306 Previous HEAD position was \$commit... three
307 HEAD is now at \$commit2... two
ca69d4d5
AR
308 EOF
309
ae2f8d5b 310 cat >3rd_detach <<-EOF &&
311 Previous HEAD position was \$commit2... two
312 HEAD is now at \$commit3... one
ca69d4d5
AR
313 EOF
314
315 reset &&
316 check_not_detached &&
317
318 # Various ways of asking for ellipses...
319 # The user can just use any kind of quoting (including none).
320
64a5e980 321 GIT_PRINT_SHA1_ELLIPSIS=yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
ca69d4d5 322 check_detached &&
1108cea7 323 test_cmp 1st_detach actual &&
ca69d4d5 324
64a5e980 325 GIT_PRINT_SHA1_ELLIPSIS=Yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
ca69d4d5 326 check_detached &&
1108cea7 327 test_cmp 2nd_detach actual &&
ca69d4d5 328
64a5e980 329 GIT_PRINT_SHA1_ELLIPSIS=YES git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
ca69d4d5 330 check_detached &&
1108cea7 331 test_cmp 3rd_detach actual &&
ca69d4d5
AR
332
333 true
334"
335
32669671 336test_done