free_ref_entry(): new function
[git/mjg.git] / t / t2020-checkout-detach.sh
blob068fba4c8e8706aa615d0401da1da47901113156
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
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"
25 reset () {
26 git checkout master &&
27 check_not_detached
30 test_expect_success 'setup' '
31 test_commit one &&
32 test_commit two &&
33 test_commit three && git tag -d three &&
34 test_commit four && git tag -d four &&
35 git branch branch &&
36 git tag tag
39 test_expect_success 'checkout branch does not detach' '
40 reset &&
41 git checkout branch &&
42 check_not_detached
45 test_expect_success 'checkout tag detaches' '
46 reset &&
47 git checkout tag &&
48 check_detached
51 test_expect_success 'checkout branch by full name detaches' '
52 reset &&
53 git checkout refs/heads/branch &&
54 check_detached
57 test_expect_success 'checkout non-ref detaches' '
58 reset &&
59 git checkout branch^ &&
60 check_detached
63 test_expect_success 'checkout ref^0 detaches' '
64 reset &&
65 git checkout branch^0 &&
66 check_detached
69 test_expect_success 'checkout --detach detaches' '
70 reset &&
71 git checkout --detach branch &&
72 check_detached
75 test_expect_success 'checkout --detach without branch name' '
76 reset &&
77 git checkout --detach &&
78 check_detached
81 test_expect_success 'checkout --detach errors out for non-commit' '
82 reset &&
83 test_must_fail git checkout --detach one^{tree} &&
84 check_not_detached
87 test_expect_success 'checkout --detach errors out for extra argument' '
88 reset &&
89 git checkout master &&
90 test_must_fail git checkout --detach tag one.t &&
91 check_not_detached
94 test_expect_success 'checkout --detached and -b are incompatible' '
95 reset &&
96 test_must_fail git checkout --detach -b newbranch tag &&
97 check_not_detached
100 test_expect_success 'checkout --detach moves HEAD' '
101 reset &&
102 git checkout one &&
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' '
109 reset &&
110 git checkout --detach two &&
111 echo content >orphan &&
112 git add 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' '
122 reset &&
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' '
132 reset &&
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
141 cat >expect <<'EOF'
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' '
145 reset &&
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
154 test_done