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