3 test_description
='checkout into detached HEAD state'
7 test_must_fail git symbolic-ref
-q HEAD
>/dev
/null
10 check_not_detached
() {
11 git symbolic-ref
-q HEAD
>/dev
/null
14 PREV_HEAD_DESC
='Previous HEAD position was'
15 check_orphan_warning
() {
16 test_i18ngrep
"you are leaving $2 behind" "$1" &&
17 test_i18ngrep
! "$PREV_HEAD_DESC" "$1"
19 check_no_orphan_warning
() {
20 test_i18ngrep
! "you are leaving .* commit.*behind" "$1" &&
21 test_i18ngrep
"$PREV_HEAD_DESC" "$1"
25 git checkout master
&&
29 test_expect_success
'setup' '
32 test_commit three && git tag -d three &&
33 test_commit four && git tag -d four &&
38 test_expect_success
'checkout branch does not detach' '
40 git checkout branch &&
44 test_expect_success
'checkout tag detaches' '
50 test_expect_success
'checkout branch by full name detaches' '
52 git checkout refs/heads/branch &&
56 test_expect_success
'checkout non-ref detaches' '
58 git checkout branch^ &&
62 test_expect_success
'checkout ref^0 detaches' '
64 git checkout branch^0 &&
68 test_expect_success
'checkout --detach detaches' '
70 git checkout --detach branch &&
74 test_expect_success
'checkout --detach without branch name' '
76 git checkout --detach &&
80 test_expect_success
'checkout --detach errors out for non-commit' '
82 test_must_fail git checkout --detach one^{tree} &&
86 test_expect_success
'checkout --detach errors out for extra argument' '
88 git checkout master &&
89 test_must_fail git checkout --detach tag one.t &&
93 test_expect_success
'checkout --detached and -b are incompatible' '
95 test_must_fail git checkout --detach -b newbranch tag &&
99 test_expect_success
'checkout --detach moves HEAD' '
102 git checkout --detach two &&
103 git diff --exit-code HEAD &&
104 git diff --exit-code two
107 test_expect_success
'checkout warns on orphan commits' '
109 git checkout --detach two &&
110 echo content >orphan &&
112 git commit -a -m orphan1 &&
113 echo new content >orphan &&
114 git commit -a -m orphan2 &&
115 orphan2=$(git rev-parse HEAD) &&
116 git checkout master 2>stderr
119 test_expect_success
'checkout warns on orphan commits: output' '
120 check_orphan_warning stderr "2 commits"
123 test_expect_success
'checkout warns orphaning 1 of 2 commits' '
124 git checkout "$orphan2" &&
125 git checkout HEAD^ 2>stderr
128 test_expect_success
'checkout warns orphaning 1 of 2 commits: output' '
129 check_orphan_warning stderr "1 commit"
132 test_expect_success
'checkout does not warn leaving ref tip' '
134 git checkout --detach two &&
135 git checkout master 2>stderr
138 test_expect_success
'checkout does not warn leaving ref tip' '
139 check_no_orphan_warning stderr
142 test_expect_success
'checkout does not warn leaving reachable commit' '
144 git checkout --detach HEAD^ &&
145 git checkout master 2>stderr
148 test_expect_success
'checkout does not warn leaving reachable commit' '
149 check_no_orphan_warning stderr
153 Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
154 (use "git pull" to update your local branch)
156 test_expect_success
'tracking count is accurate after orphan check' '
158 git branch child master^ &&
159 git config branch.child.remote . &&
160 git config branch.child.merge refs/heads/master &&
161 git checkout child^ &&
162 git checkout child >stdout &&
163 test_i18ncmp expect stdout
166 test_expect_success
'no advice given for explicit detached head state' '
168 test_config advice.detachedHead true &&
169 git checkout child && git checkout HEAD^0 >expect.advice 2>&1 &&
170 test_config advice.detachedHead false &&
171 git checkout child && git checkout HEAD^0 >expect.no-advice 2>&1 &&
172 test_unconfig advice.detachedHead &&
173 # without configuration, the advice.* variables default to true
174 git checkout child && git checkout HEAD^0 >actual 2>&1 &&
175 test_cmp expect.advice actual &&
177 # with explicit --detach
179 test_unconfig advice.detachedHead &&
180 git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
181 test_cmp expect.no-advice actual &&
183 # explicitly decline advice
184 test_config advice.detachedHead false &&
185 git checkout child && git checkout --detach HEAD^0 >actual 2>&1 &&
186 test_cmp expect.no-advice actual
189 # Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (new format)
190 test_expect_success
'describe_detached_head prints no SHA-1 ellipsis when not asked to' "
192 # The first detach operation is more chatty than the following ones.
193 cat >1st_detach <<-'EOF' &&
194 Note: checking out 'HEAD^'.
196 You are in 'detached HEAD' state. You can look around, make experimental
197 changes and commit them, and you can discard any commits you make in this
198 state without impacting any branches by performing another checkout.
200 If you want to create a new branch to retain commits you create, you may
201 do so (now or later) by using -b with the checkout command again. Example:
203 git checkout -b <new-branch-name>
205 HEAD is now at 7c7cd714e262 three
208 # The remaining ones just show info about previous and current HEADs.
209 cat >2nd_detach <<-'EOF' &&
210 Previous HEAD position was 7c7cd714e262 three
211 HEAD is now at 139b20d8e6c5 two
214 cat >3rd_detach <<-'EOF' &&
215 Previous HEAD position was 139b20d8e6c5 two
216 HEAD is now at d79ce1670bdc one
220 check_not_detached &&
222 # Various ways of *not* asking for ellipses
224 sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
225 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
227 test_i18ncmp 1st_detach actual &&
229 GIT_PRINT_SHA1_ELLIPSIS="no
" git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
231 test_i18ncmp 2nd_detach actual &&
233 GIT_PRINT_SHA1_ELLIPSIS= git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
235 test_i18ncmp 3rd_detach actual &&
237 sane_unset GIT_PRINT_SHA1_ELLIPSIS &&
239 # We only have four commits, but we can re-use them
241 check_not_detached &&
243 # Make no mention of the env var at all
244 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
246 test_i18ncmp 1st_detach actual &&
248 GIT_PRINT_SHA1_ELLIPSIS='nope' &&
249 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
251 test_i18ncmp 2nd_detach actual &&
253 GIT_PRINT_SHA1_ELLIPSIS=nein &&
254 git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
256 test_i18ncmp 3rd_detach actual &&
261 # Detached HEAD tests for GIT_PRINT_SHA1_ELLIPSIS (old format)
262 test_expect_success
'describe_detached_head does print SHA-1 ellipsis when asked to' "
264 # The first detach operation is more chatty than the following ones.
265 cat >1st_detach <<-'EOF' &&
266 Note: checking out 'HEAD^'.
268 You are in 'detached HEAD' state. You can look around, make experimental
269 changes and commit them, and you can discard any commits you make in this
270 state without impacting any branches by performing another checkout.
272 If you want to create a new branch to retain commits you create, you may
273 do so (now or later) by using -b with the checkout command again. Example:
275 git checkout -b <new-branch-name>
277 HEAD is now at 7c7cd714e262... three
280 # The remaining ones just show info about previous and current HEADs.
281 cat >2nd_detach <<-'EOF' &&
282 Previous HEAD position was 7c7cd714e262... three
283 HEAD is now at 139b20d8e6c5... two
286 cat >3rd_detach <<-'EOF' &&
287 Previous HEAD position was 139b20d8e6c5... two
288 HEAD is now at d79ce1670bdc... one
292 check_not_detached &&
294 # Various ways of asking for ellipses...
295 # The user can just use any kind of quoting (including none).
297 GIT_PRINT_SHA1_ELLIPSIS=yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
299 test_i18ncmp 1st_detach actual &&
301 GIT_PRINT_SHA1_ELLIPSIS=Yes git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
303 test_i18ncmp 2nd_detach actual &&
305 GIT_PRINT_SHA1_ELLIPSIS=YES git -c 'core.abbrev=12' checkout HEAD^ >actual 2>&1 &&
307 test_i18ncmp 3rd_detach actual &&