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 ORPHAN_WARNING
='you are leaving .* commit.*behind'
15 PREV_HEAD_DESC
='Previous HEAD position was'
16 check_orphan_warning
() {
17 test_i18ngrep
"$ORPHAN_WARNING" "$1" &&
18 test_i18ngrep
! "$PREV_HEAD_DESC" "$1"
20 check_no_orphan_warning
() {
21 test_i18ngrep
! "$ORPHAN_WARNING" "$1" &&
22 test_i18ngrep
"$PREV_HEAD_DESC" "$1"
26 git checkout master
&&
30 test_expect_success
'setup' '
33 test_commit three && git tag -d three &&
34 test_commit four && git tag -d four &&
39 test_expect_success
'checkout branch does not detach' '
41 git checkout branch &&
45 test_expect_success
'checkout tag detaches' '
51 test_expect_success
'checkout branch by full name detaches' '
53 git checkout refs/heads/branch &&
57 test_expect_success
'checkout non-ref detaches' '
59 git checkout branch^ &&
63 test_expect_success
'checkout ref^0 detaches' '
65 git checkout branch^0 &&
69 test_expect_success
'checkout --detach detaches' '
71 git checkout --detach branch &&
75 test_expect_success
'checkout --detach without branch name' '
77 git checkout --detach &&
81 test_expect_success
'checkout --detach errors out for non-commit' '
83 test_must_fail git checkout --detach one^{tree} &&
87 test_expect_success
'checkout --detach errors out for extra argument' '
89 git checkout master &&
90 test_must_fail git checkout --detach tag one.t &&
94 test_expect_success
'checkout --detached and -b are incompatible' '
96 test_must_fail git checkout --detach -b newbranch tag &&
100 test_expect_success
'checkout --detach moves HEAD' '
103 git checkout --detach two &&
104 git diff --exit-code HEAD &&
105 git diff --exit-code two
108 test_expect_success
'checkout warns on orphan commits' '
110 git checkout --detach two &&
111 echo content >orphan &&
113 git commit -a -m orphan &&
114 git checkout master 2>stderr
117 test_expect_success
'checkout warns on orphan commits: output' '
118 check_orphan_warning stderr
121 test_expect_success
'checkout does not warn leaving ref tip' '
123 git checkout --detach two &&
124 git checkout master 2>stderr
127 test_expect_success
'checkout does not warn leaving ref tip' '
128 check_no_orphan_warning stderr
131 test_expect_success
'checkout does not warn leaving reachable commit' '
133 git checkout --detach HEAD^ &&
134 git checkout master 2>stderr
137 test_expect_success
'checkout does not warn leaving reachable commit' '
138 check_no_orphan_warning stderr
142 Your branch is behind 'master' by 1 commit, and can be fast-forwarded.
144 test_expect_success
'tracking count is accurate after orphan check' '
146 git branch child master^ &&
147 git config branch.child.remote . &&
148 git config branch.child.merge refs/heads/master &&
149 git checkout child^ &&
150 git checkout child >stdout &&
151 test_cmp expect stdout