Raw
1 #!/bin/sh
2
3 test_description='basic symbolic-ref tests'
4
5 . ./test-lib.sh
6
7 # If the tests munging HEAD fail, they can break detection of
8 # the git repo, meaning that further tests will operate on
9 # the surrounding git repo instead of the trash directory.
10 reset_to_sane() {
11 rm -rf .git &&
12 "$TAR" xf .git.tar
13 }
14
15 test_expect_success 'setup' '
16 git symbolic-ref HEAD refs/heads/foo &&
17 test_commit file &&
18 "$TAR" cf .git.tar .git
19 '
20
21 test_expect_success 'symbolic-ref read/write roundtrip' '
22 git symbolic-ref HEAD refs/heads/read-write-roundtrip &&
23 echo refs/heads/read-write-roundtrip >expect &&
24 git symbolic-ref HEAD >actual &&
25 test_cmp expect actual
26 '
27
28 test_expect_success 'symbolic-ref refuses non-ref for HEAD' '
29 test_must_fail git symbolic-ref HEAD foo
30 '
31
32 reset_to_sane
33
34 test_expect_success 'symbolic-ref refuses bare sha1' '
35 rev=$(git rev-parse HEAD) &&
36 test_must_fail git symbolic-ref HEAD "$rev"
37 '
38
39 reset_to_sane
40
41 test_expect_success 'HEAD cannot be removed' '
42 test_must_fail git symbolic-ref -d HEAD
43 '
44
45 reset_to_sane
46
47 test_expect_success 'symbolic-ref can be deleted' '
48 git symbolic-ref NOTHEAD refs/heads/foo &&
49 git symbolic-ref -d NOTHEAD &&
50 git rev-parse refs/heads/foo &&
51 test_must_fail git symbolic-ref NOTHEAD
52 '
53 reset_to_sane
54
55 test_expect_success 'symbolic-ref can delete dangling symref' '
56 git symbolic-ref NOTHEAD refs/heads/missing &&
57 git symbolic-ref -d NOTHEAD &&
58 test_must_fail git rev-parse refs/heads/missing &&
59 test_must_fail git symbolic-ref NOTHEAD
60 '
61 reset_to_sane
62
63 test_expect_success 'symbolic-ref fails to delete missing FOO' '
64 echo "fatal: Cannot delete FOO, not a symbolic ref" >expect &&
65 test_must_fail git symbolic-ref -d FOO >actual 2>&1 &&
66 test_cmp expect actual
67 '
68 reset_to_sane
69
70 test_expect_success 'symbolic-ref fails to delete real ref' '
71 echo "fatal: Cannot delete refs/heads/foo, not a symbolic ref" >expect &&
72 test_must_fail git symbolic-ref -d refs/heads/foo >actual 2>&1 &&
73 git rev-parse --verify refs/heads/foo &&
74 test_cmp expect actual
75 '
76 reset_to_sane
77
78 test_expect_success 'create large ref name' '
79 # make 256+ character ref; some systems may not handle that,
80 # so be gentle
81 long=0123456789abcdef &&
82 long=$long/$long/$long/$long &&
83 long=$long/$long/$long/$long &&
84 long_ref=refs/heads/$long &&
85 tree=$(git write-tree) &&
86 commit=$(echo foo | git commit-tree $tree) &&
87 if git update-ref $long_ref $commit; then
88 test_set_prereq LONG_REF
89 else
90 echo >&2 "long refs not supported"
91 fi
92 '
93
94 test_expect_success LONG_REF 'symbolic-ref can point to large ref name' '
95 git symbolic-ref HEAD $long_ref &&
96 echo $long_ref >expect &&
97 git symbolic-ref HEAD >actual &&
98 test_cmp expect actual
99 '
100
101 test_expect_success LONG_REF 'we can parse long symbolic ref' '
102 echo $commit >expect &&
103 git rev-parse --verify HEAD >actual &&
104 test_cmp expect actual
105 '
106
107 test_expect_success 'symbolic-ref reports failure in exit code' '
108 # Create d/f conflict to simulate failure.
109 test_must_fail git symbolic-ref refs/heads refs/heads/foo
110 '
111
112 test_expect_success 'symbolic-ref writes reflog entry' '
113 git checkout -b log1 &&
114 test_commit one &&
115 git checkout -b log2 &&
116 test_commit two &&
117 git checkout --orphan orphan &&
118 git symbolic-ref -m create HEAD refs/heads/log1 &&
119 git symbolic-ref -m update HEAD refs/heads/log2 &&
120 cat >expect <<-\EOF &&
121 update
122 create
123 EOF
124 git log --format=%gs -g -2 >actual &&
125 test_cmp expect actual
126 '
127
128 test_expect_success 'symbolic-ref does not create ref d/f conflicts' '
129 git checkout -b df &&
130 test_commit df &&
131 test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df &&
132 git pack-refs --all --prune &&
133 test_must_fail git symbolic-ref refs/heads/df/conflict refs/heads/df
134 '
135
136 test_expect_success 'symbolic-ref can overwrite pointer to invalid name' '
137 test_when_finished reset_to_sane &&
138 head=$(git rev-parse HEAD) &&
139 git symbolic-ref HEAD refs/heads/outer &&
140 test_when_finished "git update-ref -d refs/heads/outer/inner" &&
141 git update-ref refs/heads/outer/inner $head &&
142 git symbolic-ref HEAD refs/heads/unrelated
143 '
144
145 test_expect_success 'symbolic-ref can resolve d/f name (EISDIR)' '
146 test_when_finished reset_to_sane &&
147 head=$(git rev-parse HEAD) &&
148 git symbolic-ref HEAD refs/heads/outer/inner &&
149 test_when_finished "git update-ref -d refs/heads/outer" &&
150 git update-ref refs/heads/outer $head &&
151 echo refs/heads/outer/inner >expect &&
152 git symbolic-ref HEAD >actual &&
153 test_cmp expect actual
154 '
155
156 test_expect_success 'symbolic-ref can resolve d/f name (ENOTDIR)' '
157 test_when_finished reset_to_sane &&
158 head=$(git rev-parse HEAD) &&
159 git symbolic-ref HEAD refs/heads/outer &&
160 test_when_finished "git update-ref -d refs/heads/outer/inner" &&
161 git update-ref refs/heads/outer/inner $head &&
162 echo refs/heads/outer >expect &&
163 git symbolic-ref HEAD >actual &&
164 test_cmp expect actual
165 '
166
167 test_expect_success 'symbolic-ref refuses invalid target for non-HEAD' '
168 test_must_fail git symbolic-ref refs/heads/invalid foo..bar
169 '
170
171 test_expect_success 'symbolic-ref allows top-level target for non-HEAD' '
172 git symbolic-ref refs/heads/top-level ORIG_HEAD &&
173 git update-ref ORIG_HEAD HEAD &&
174 test_cmp_rev top-level HEAD
175 '
176
177 test_expect_success 'symbolic-ref pointing at another' '
178 git update-ref refs/heads/maint-2.37 HEAD &&
179 git symbolic-ref refs/heads/maint refs/heads/maint-2.37 &&
180 git checkout maint &&
181
182 git symbolic-ref HEAD >actual &&
183 echo refs/heads/maint-2.37 >expect &&
184 test_cmp expect actual &&
185
186 git symbolic-ref --no-recurse HEAD >actual &&
187 echo refs/heads/maint >expect &&
188 test_cmp expect actual
189 '
190
191 test_expect_success 'symbolic-ref --short handles complex utf8 case' '
192 name="测试-加-增加-加-增加" &&
193 git symbolic-ref TEST_SYMREF "refs/heads/$name" &&
194 # In the real world, we saw problems with this case only
195 # when the locale includes UTF-8. Set it here to try to make things as
196 # hard as possible for us to pass, but in practice we should do the
197 # right thing regardless (and of course some platforms may not even
198 # have this locale).
199 LC_ALL=en_US.UTF-8 git symbolic-ref --short TEST_SYMREF >actual &&
200 echo "$name" >expect &&
201 test_cmp expect actual
202 '
203
204 test_expect_success 'symbolic-ref --short handles name with suffix' '
205 git symbolic-ref TEST_SYMREF "refs/remotes/origin/HEAD" &&
206 git symbolic-ref --short TEST_SYMREF >actual &&
207 echo "origin" >expect &&
208 test_cmp expect actual
209 '
210
211 test_expect_success 'symbolic-ref --short handles almost-matching name' '
212 git symbolic-ref TEST_SYMREF "refs/headsXfoo" &&
213 git symbolic-ref --short TEST_SYMREF >actual &&
214 echo "headsXfoo" >expect &&
215 test_cmp expect actual
216 '
217
218 test_expect_success 'symbolic-ref --short handles name with percent' '
219 git symbolic-ref TEST_SYMREF "refs/heads/%foo" &&
220 git symbolic-ref --short TEST_SYMREF >actual &&
221 echo "%foo" >expect &&
222 test_cmp expect actual
223 '
224
225 test_done