1#!/bin/sh
2
3test_description='checkout into detached HEAD state'
4. ./test-lib.sh
5
6check_detached () {
7 test_must_fail git symbolic-ref -q HEAD >/dev/null
8}
9
10check_not_detached () {
11 git symbolic-ref -q HEAD >/dev/null
12}
13
14reset () {
15 git checkout master &&
16 check_not_detached
17}
18
19test_expect_success 'setup' '
20 test_commit one &&
21 test_commit two &&
22 git branch branch &&
23 git tag tag
24'
25
26test_expect_success 'checkout branch does not detach' '
27 reset &&
28 git checkout branch &&
29 check_not_detached
30'
31
32test_expect_success 'checkout tag detaches' '
33 reset &&
34 git checkout tag &&
35 check_detached
36'
37
38test_expect_success 'checkout branch by full name detaches' '
39 reset &&
40 git checkout refs/heads/branch &&
41 check_detached
42'
43
44test_expect_success 'checkout non-ref detaches' '
45 reset &&
46 git checkout branch^ &&
47 check_detached
48'
49
50test_expect_success 'checkout ref^0 detaches' '
51 reset &&
52 git checkout branch^0 &&
53 check_detached
54'
55
56test_expect_success 'checkout --detach detaches' '
57 reset &&
58 git checkout --detach branch &&
59 check_detached
60'
61
62test_expect_success 'checkout --detach without branch name' '
63 reset &&
64 git checkout --detach &&
65 check_detached
66'
67
68test_expect_success 'checkout --detach errors out for non-commit' '
69 reset &&
70 test_must_fail git checkout --detach one^{tree} &&
71 check_not_detached
72'
73
74test_expect_success 'checkout --detach errors out for extra argument' '
75 reset &&
76 git checkout master &&
77 test_must_fail git checkout --detach tag one.t &&
78 check_not_detached
79'
80
81test_expect_success 'checkout --detached and -b are incompatible' '
82 reset &&
83 test_must_fail git checkout --detach -b newbranch tag &&
84 check_not_detached
85'
86
87test_expect_success 'checkout --detach moves HEAD' '
88 reset &&
89 git checkout one &&
90 git checkout --detach two &&
91 git diff --exit-code HEAD &&
92 git diff --exit-code two
93'
94
95test_done