3 test_description
='basic symbolic-ref tests'
5 TEST_PASSES_SANITIZE_LEAK
=true
8 # If the tests munging HEAD fail, they can break detection of
9 # the git repo, meaning that further tests will operate on
10 # the surrounding git repo instead of the trash directory.
16 test_expect_success
'setup' '
17 git symbolic-ref HEAD refs/heads/foo &&
19 "$TAR" cf .git.tar .git/
22 test_expect_success
'symbolic-ref read/write roundtrip' '
23 git symbolic-ref HEAD refs/heads/read-write-roundtrip &&
24 echo refs/heads/read-write-roundtrip >expect &&
25 git symbolic-ref HEAD >actual &&
26 test_cmp expect actual
29 test_expect_success
'symbolic-ref refuses non-ref for HEAD' '
30 test_must_fail git symbolic-ref HEAD foo
35 test_expect_success
'symbolic-ref refuses bare sha1' '
36 rev=$(git rev-parse HEAD) &&
37 test_must_fail git symbolic-ref HEAD "$rev"
42 test_expect_success
'HEAD cannot be removed' '
43 test_must_fail git symbolic-ref -d HEAD
48 test_expect_success
'symbolic-ref can be deleted' '
49 git symbolic-ref NOTHEAD refs/heads/foo &&
50 git symbolic-ref -d NOTHEAD &&
51 git rev-parse refs/heads/foo &&
52 test_must_fail git symbolic-ref NOTHEAD
56 test_expect_success
'symbolic-ref can delete dangling symref' '
57 git symbolic-ref NOTHEAD refs/heads/missing &&
58 git symbolic-ref -d NOTHEAD &&
59 test_must_fail git rev-parse refs/heads/missing &&
60 test_must_fail git symbolic-ref NOTHEAD
64 test_expect_success
'symbolic-ref fails to delete missing FOO' '
65 echo "fatal: Cannot delete FOO, not a symbolic ref" >expect &&
66 test_must_fail git symbolic-ref -d FOO >actual 2>&1 &&
67 test_cmp expect actual
71 test_expect_success
'symbolic-ref fails to delete real ref' '
72 echo "fatal: Cannot delete refs/heads/foo, not a symbolic ref" >expect &&
73 test_must_fail git symbolic-ref -d refs/heads/foo >actual 2>&1 &&
74 git rev-parse --verify refs/heads/foo &&
75 test_cmp expect actual
79 test_expect_success
'create large ref name' '
80 # make 256+ character ref; some systems may not handle that,
82 long=0123456789abcdef &&
83 long=$long/$long/$long/$long &&
84 long=$long/$long/$long/$long &&
85 long_ref=refs/heads/$long &&
86 tree=$(git write-tree) &&
87 commit=$(echo foo | git commit-tree $tree) &&
88 if git update-ref $long_ref $commit; then
89 test_set_prereq LONG_REF
91 echo >&2 "long refs not supported"
95 test_expect_success LONG_REF
'symbolic-ref can point to large ref name' '
96 git symbolic-ref HEAD $long_ref &&
97 echo $long_ref >expect &&
98 git symbolic-ref HEAD >actual &&
99 test_cmp expect actual
102 test_expect_success LONG_REF
'we can parse long symbolic ref' '
103 echo $commit >expect &&
104 git rev-parse --verify HEAD >actual &&
105 test_cmp expect actual
108 test_expect_success
'symbolic-ref reports failure in exit code' '
109 # Create d/f conflict to simulate failure.
110 test_must_fail git symbolic-ref refs/heads refs/heads/foo
113 test_expect_success
'symbolic-ref writes reflog entry' '
114 git checkout -b log1 &&
116 git checkout -b log2 &&
118 git checkout --orphan orphan &&
119 git symbolic-ref -m create HEAD refs/heads/log1 &&
120 git symbolic-ref -m update HEAD refs/heads/log2 &&
121 cat >expect <<-\EOF &&
125 git log --format=%gs -g -2 >actual &&
126 test_cmp expect actual
129 test_expect_success
'symbolic-ref does not create ref d/f conflicts' '
130 git checkout -b df &&
132 test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df &&
133 git pack-refs --all --prune &&
134 test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df
137 test_expect_success
'symbolic-ref can overwrite pointer to invalid name' '
138 test_when_finished reset_to_sane &&
139 head=$(git rev-parse HEAD) &&
140 git symbolic-ref HEAD refs/heads/outer &&
141 test_when_finished "git update-ref -d refs/heads/outer/inner" &&
142 git update-ref refs/heads/outer/inner $head &&
143 git symbolic-ref HEAD refs/heads/unrelated
146 test_expect_success
'symbolic-ref can resolve d/f name (EISDIR)' '
147 test_when_finished reset_to_sane &&
148 head=$(git rev-parse HEAD) &&
149 git symbolic-ref HEAD refs/heads/outer/inner &&
150 test_when_finished "git update-ref -d refs/heads/outer" &&
151 git update-ref refs/heads/outer $head &&
152 echo refs/heads/outer/inner >expect &&
153 git symbolic-ref HEAD >actual &&
154 test_cmp expect actual
157 test_expect_success
'symbolic-ref can resolve d/f name (ENOTDIR)' '
158 test_when_finished reset_to_sane &&
159 head=$(git rev-parse HEAD) &&
160 git symbolic-ref HEAD refs/heads/outer &&
161 test_when_finished "git update-ref -d refs/heads/outer/inner" &&
162 git update-ref refs/heads/outer/inner $head &&
163 echo refs/heads/outer >expect &&
164 git symbolic-ref HEAD >actual &&
165 test_cmp expect actual
168 test_expect_success
'symbolic-ref refuses invalid target for non-HEAD' '
169 test_must_fail git symbolic-ref refs/heads/invalid foo..bar
172 test_expect_success
'symbolic-ref allows top-level target for non-HEAD' '
173 git symbolic-ref refs/heads/top-level ORIG_HEAD &&
174 git update-ref ORIG_HEAD HEAD &&
175 test_cmp_rev top-level HEAD
178 test_expect_success
'symbolic-ref pointing at another' '
179 git update-ref refs/heads/maint-2.37 HEAD &&
180 git symbolic-ref refs/heads/maint refs/heads/maint-2.37 &&
181 git checkout maint &&
183 git symbolic-ref HEAD >actual &&
184 echo refs/heads/maint-2.37 >expect &&
185 test_cmp expect actual &&
187 git symbolic-ref --no-recurse HEAD >actual &&
188 echo refs/heads/maint >expect &&
189 test_cmp expect actual
192 test_expect_success
'symbolic-ref --short handles complex utf8 case' '
193 name="测试-加-增加-加-增加" &&
194 git symbolic-ref TEST_SYMREF "refs/heads/$name" &&
195 # In the real world, we saw problems with this case only
196 # when the locale includes UTF-8. Set it here to try to make things as
197 # hard as possible for us to pass, but in practice we should do the
198 # right thing regardless (and of course some platforms may not even
200 LC_ALL=en_US.UTF-8 git symbolic-ref --short TEST_SYMREF >actual &&
201 echo "$name" >expect &&
202 test_cmp expect actual
205 test_expect_success
'symbolic-ref --short handles name with suffix' '
206 git symbolic-ref TEST_SYMREF "refs/remotes/origin/HEAD" &&
207 git symbolic-ref --short TEST_SYMREF >actual &&
208 echo "origin" >expect &&
209 test_cmp expect actual
212 test_expect_success
'symbolic-ref --short handles almost-matching name' '
213 git symbolic-ref TEST_SYMREF "refs/headsXfoo" &&
214 git symbolic-ref --short TEST_SYMREF >actual &&
215 echo "headsXfoo" >expect &&
216 test_cmp expect actual
219 test_expect_success
'symbolic-ref --short handles name with percent' '
220 git symbolic-ref TEST_SYMREF "refs/heads/%foo" &&
221 git symbolic-ref --short TEST_SYMREF >actual &&
222 echo "%foo" >expect &&
223 test_cmp expect actual