]> git.ipfire.org Git - thirdparty/git.git/blob - t/t2020-checkout-detach.sh
Merge branch 'jk/clone-allow-bare-and-o-together'
[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_i18ngrep "you are leaving $2 behind" "$1" &&
21 test_i18ngrep ! "$PREV_HEAD_DESC" "$1"
22 }
23 check_no_orphan_warning() {
24 test_i18ngrep ! "you are leaving .* commit.*behind" "$1" &&
25 test_i18ngrep "$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 test_expect_success 'checkout tag detaches' '
49 reset &&
50 git checkout tag &&
51 check_detached
52 '
53
54 test_expect_success 'checkout branch by full name detaches' '
55 reset &&
56 git checkout refs/heads/branch &&
57 check_detached
58 '
59
60 test_expect_success 'checkout non-ref detaches' '
61 reset &&
62 git checkout branch^ &&
63 check_detached
64 '
65
66 test_expect_success 'checkout ref^0 detaches' '
67 reset &&
68 git checkout branch^0 &&
69 check_detached
70 '
71
72 test_expect_success 'checkout --detach detaches' '
73 reset &&
74 git checkout --detach branch &&
75 check_detached
76 '
77
78 test_expect_success 'checkout --detach without branch name' '
79 reset &&
80 git checkout --detach &&
81 check_detached
82 '
83
84 test_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
90 test_expect_success 'checkout --detach errors out for extra argument' '
91 reset &&
92 git checkout main &&
93 test_must_fail git checkout --detach tag one.t &&
94 check_not_detached
95 '
96
97 test_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
103 test_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
111 test_expect_success 'checkout warns on orphan commits' '
112 reset &&
113 git checkout --detach two &&
114 echo content >orphan &&
115 git add orphan &&
116 git commit -a -m orphan1 &&
117 echo new content >orphan &&
118 git commit -a -m orphan2 &&
119 orphan2=$(git rev-parse HEAD) &&
120 git checkout main 2>stderr
121 '
122
123 test_expect_success 'checkout warns on orphan commits: output' '
124 check_orphan_warning stderr "2 commits"
125 '
126
127 test_expect_success 'checkout warns orphaning 1 of 2 commits' '
128 git checkout "$orphan2" &&
129 git checkout HEAD^ 2>stderr
130 '
131
132 test_expect_success 'checkout warns orphaning 1 of 2 commits: output' '
133 check_orphan_warning stderr "1 commit"
134 '
135
136 test_expect_success 'checkout does not warn leaving ref tip' '
137 reset &&
138 git checkout --detach two &&
139 git checkout main 2>stderr
140 '
141
142 test_expect_success 'checkout does not warn leaving ref tip' '
143 check_no_orphan_warning stderr
144 '
145
146 test_expect_success 'checkout does not warn leaving reachable commit' '
147 reset &&
148 git checkout --detach HEAD^ &&
149 git checkout main 2>stderr
150 '
151
152 test_expect_success 'checkout does not warn leaving reachable commit' '
153 check_no_orphan_warning stderr
154 '
155
156 cat >expect <<'EOF'
157 Your branch is behind 'main' by 1 commit, and can be fast-forwarded.
158 (use "git pull" to update your local branch)
159 EOF
160 test_expect_success 'tracking count is accurate after orphan check' '
161 reset &&
162 git branch child main^ &&
163 git config branch.child.remote . &&
164 git config branch.child.merge refs/heads/main &&
165 git checkout child^ &&
166 git checkout child >stdout &&
167 test_cmp expect stdout
168 '
169
170 test_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
193 # Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (new format)
194 test_expect_success 'describe_detached_head prints no SHA-1 ellipsis when not asked to' "
195
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) &&
199
200 # The first detach operation is more chatty than the following ones.
201 cat >1st_detach <<-EOF &&
202 Note: switching to 'HEAD^'.
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
206 state without impacting any branches by switching back to a branch.
207
208 If you want to create a new branch to retain commits you create, you may
209 do so (now or later) by using -c with the switch command. Example:
210
211 git switch -c <new-branch-name>
212
213 Or undo this operation with:
214
215 git switch -
216
217 Turn off this advice by setting config variable advice.detachedHead to false
218
219 HEAD is now at \$commit three
220 EOF
221
222 # The remaining ones just show info about previous and current HEADs.
223 cat >2nd_detach <<-EOF &&
224 Previous HEAD position was \$commit three
225 HEAD is now at \$commit2 two
226 EOF
227
228 cat >3rd_detach <<-EOF &&
229 Previous HEAD position was \$commit2 two
230 HEAD is now at \$commit3 one
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 &&
241 test_cmp 1st_detach actual &&
242
243 GIT_PRINT_SHA1_ELLIPSIS="no" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
244 check_detached &&
245 test_cmp 2nd_detach actual &&
246
247 GIT_PRINT_SHA1_ELLIPSIS= git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
248 check_detached &&
249 test_cmp 3rd_detach actual &&
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 &&
260 test_cmp 1st_detach actual &&
261
262 GIT_PRINT_SHA1_ELLIPSIS='nope' &&
263 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
264 check_detached &&
265 test_cmp 2nd_detach actual &&
266
267 GIT_PRINT_SHA1_ELLIPSIS=nein &&
268 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
269 check_detached &&
270 test_cmp 3rd_detach actual &&
271
272 true
273 "
274
275 # Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (old format)
276 test_expect_success 'describe_detached_head does print SHA-1 ellipsis when asked to' "
277
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) &&
281
282 # The first detach operation is more chatty than the following ones.
283 cat >1st_detach <<-EOF &&
284 Note: switching to 'HEAD^'.
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
288 state without impacting any branches by switching back to a branch.
289
290 If you want to create a new branch to retain commits you create, you may
291 do so (now or later) by using -c with the switch command. Example:
292
293 git switch -c <new-branch-name>
294
295 Or undo this operation with:
296
297 git switch -
298
299 Turn off this advice by setting config variable advice.detachedHead to false
300
301 HEAD is now at \$commit... three
302 EOF
303
304 # The remaining ones just show info about previous and current HEADs.
305 cat >2nd_detach <<-EOF &&
306 Previous HEAD position was \$commit... three
307 HEAD is now at \$commit2... two
308 EOF
309
310 cat >3rd_detach <<-EOF &&
311 Previous HEAD position was \$commit2... two
312 HEAD is now at \$commit3... one
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
321 GIT_PRINT_SHA1_ELLIPSIS=yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
322 check_detached &&
323 test_cmp 1st_detach actual &&
324
325 GIT_PRINT_SHA1_ELLIPSIS=Yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
326 check_detached &&
327 test_cmp 2nd_detach actual &&
328
329 GIT_PRINT_SHA1_ELLIPSIS=YES git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
330 check_detached &&
331 test_cmp 3rd_detach actual &&
332
333 true
334 "
335
336 test_done