1 #!/bin/sh
3 test_description='checkout into detached HEAD state'
4 . ./test-lib.sh
6 check_detached () {
7 test_must_fail git symbolic-ref -q HEAD >/dev/null
8 }
10 check_not_detached () {
11 git symbolic-ref -q HEAD >/dev/null
12 }
14 reset () {
15 git checkout master &&
16 check_not_detached
17 }
19 test_expect_success 'setup' '
20 test_commit one &&
21 test_commit two &&
22 git branch branch &&
23 git tag tag
24 '
26 test_expect_success 'checkout branch does not detach' '
27 reset &&
28 git checkout branch &&
29 check_not_detached
30 '
32 test_expect_success 'checkout tag detaches' '
33 reset &&
34 git checkout tag &&
35 check_detached
36 '
38 test_expect_success 'checkout branch by full name detaches' '
39 reset &&
40 git checkout refs/heads/branch &&
41 check_detached
42 '
44 test_expect_success 'checkout non-ref detaches' '
45 reset &&
46 git checkout branch^ &&
47 check_detached
48 '
50 test_expect_success 'checkout ref^0 detaches' '
51 reset &&
52 git checkout branch^0 &&
53 check_detached
54 '
56 test_expect_success 'checkout --detach detaches' '
57 reset &&
58 git checkout --detach branch &&
59 check_detached
60 '
62 test_expect_success 'checkout --detach without branch name' '
63 reset &&
64 git checkout --detach &&
65 check_detached
66 '
68 test_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 '
74 test_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 '
81 test_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 '
87 test_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 '
95 test_done