Blob


1 #!/bin/sh
2 #
3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
4 #
5 # Permission to use, copy, modify, and distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8 #
9 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 . ./common.sh
19 test_ref_create() {
20 local testroot=`test_init ref_create`
21 local commit_id=`git_show_head $testroot/repo`
23 # Create a ref pointing at a commit ID
24 got ref -r $testroot/repo -c $commit_id refs/heads/commitref
25 ret=$?
26 if [ $ret -ne 0 ]; then
27 echo "got ref command failed unexpectedly"
28 test_done "$testroot" "$ret"
29 return 1
30 fi
32 # Create a ref based on repository's HEAD reference
33 got ref -r $testroot/repo -c HEAD refs/heads/newref
34 ret=$?
35 if [ $ret -ne 0 ]; then
36 echo "got ref command failed unexpectedly"
37 test_done "$testroot" "$ret"
38 return 1
39 fi
41 # Ensure that Git recognizes the ref Got has created
42 (cd $testroot/repo && git checkout -q newref)
43 ret=$?
44 if [ $ret -ne 0 ]; then
45 echo "git checkout command failed unexpectedly"
46 test_done "$testroot" "$ret"
47 return 1
48 fi
50 # Ensure Got recognizes the new ref
51 got checkout -b newref $testroot/repo $testroot/wt >/dev/null
52 ret=$?
53 if [ $ret -ne 0 ]; then
54 echo "got checkout command failed unexpectedly"
55 test_done "$testroot" "$ret"
56 return 1
57 fi
59 # Create a head ref based on another specific ref
60 (cd $testroot/wt && got ref -c refs/heads/master refs/heads/anotherref)
61 ret=$?
62 if [ $ret -ne 0 ]; then
63 test_done "$testroot" "$ret"
64 return 1
65 fi
67 (cd $testroot/repo && git checkout -q anotherref)
68 ret=$?
69 if [ $ret -ne 0 ]; then
70 echo "git checkout command failed unexpectedly"
71 test_done "$testroot" "$ret"
72 return 1
73 fi
75 # Create a symbolic ref
76 (cd $testroot/wt && got ref -s refs/heads/master refs/heads/symbolicref)
77 ret=$?
78 if [ $ret -ne 0 ]; then
79 test_done "$testroot" "$ret"
80 return 1
81 fi
83 (cd $testroot/repo && git checkout -q symbolicref)
84 ret=$?
85 if [ $ret -ne 0 ]; then
86 echo "git checkout command failed unexpectedly"
87 test_done "$testroot" "$ret"
88 return 1
89 fi
91 # Attempt to create a symbolic ref pointing at a non-reference
92 (cd $testroot/wt && got ref -s $commit_id refs/heads/symbolicref \
93 2> $testroot/stderr)
94 ret=$?
95 if [ $ret -eq 0 ]; then
96 echo "got ref command succeeded unexpectedly"
97 test_done "$testroot" "1"
98 return 1
99 fi
101 echo "got: reference $commit_id not found" > $testroot/stderr.expected
102 cmp -s $testroot/stderr $testroot/stderr.expected
103 ret=$?
104 if [ $ret -ne 0 ]; then
105 diff -u $testroot/stderr.expected $testroot/stderr
106 test_done "$testroot" "$ret"
107 return 1
108 fi
110 # Attempt to create a reference without specifying a name
111 (cd $testroot/wt && got ref -c $commit_id 2> $testroot/stderr)
112 ret=$?
113 if [ $ret -eq 0 ]; then
114 echo "got ref command succeeded unexpectedly"
115 test_done "$testroot" "1"
116 return 1
117 fi
119 grep -q '^usage: got ref' $testroot/stderr
120 ret=$?
121 if [ $ret -ne 0 ]; then
122 echo "unexpected usage error message: " >&2
123 cat $testroot/stderr >&2
124 test_done "$testroot" "$ret"
125 return 1
126 fi
128 # Attempt to create a symbolic reference without specifying a name
129 (cd $testroot/wt && got ref -s refs/heads/symbolicref \
130 2> $testroot/stderr)
131 ret=$?
132 if [ $ret -eq 0 ]; then
133 echo "got ref command succeeded unexpectedly"
134 test_done "$testroot" "1"
135 return 1
136 fi
138 grep -q '^usage: got ref' $testroot/stderr
139 ret=$?
140 if [ $ret -ne 0 ]; then
141 echo "unexpected usage error message: " >&2
142 cat $testroot/stderr >&2
143 test_done "$testroot" "$ret"
144 return 1
145 fi
147 # Change HEAD
148 got ref -r $testroot/repo -s refs/heads/newref HEAD
149 ret=$?
150 if [ $ret -ne 0 ]; then
151 echo "got ref command failed unexpectedly"
152 test_done "$testroot" "$ret"
153 return 1
154 fi
156 # Ensure that Git recognizes the ref Got has created
157 (cd $testroot/repo && git checkout -q HEAD)
158 ret=$?
159 if [ $ret -ne 0 ]; then
160 echo "git checkout command failed unexpectedly"
161 test_done "$testroot" "$ret"
162 return 1
163 fi
165 # Ensure Got recognizes the new ref
166 (cd $testroot/wt && got update -b HEAD >/dev/null)
167 ret=$?
168 if [ $ret -ne 0 ]; then
169 echo "got update command failed unexpectedly"
170 test_done "$testroot" "$ret"
171 return 1
172 fi
173 got ref -r $testroot/repo -l > $testroot/stdout
174 echo "HEAD: refs/heads/newref" > $testroot/stdout.expected
175 echo -n "refs/got/worktree/base-" >> $testroot/stdout.expected
176 cat $testroot/wt/.got/uuid | tr -d '\n' >> $testroot/stdout.expected
177 echo ": $commit_id" >> $testroot/stdout.expected
178 echo "refs/heads/anotherref: $commit_id" >> $testroot/stdout.expected
179 echo "refs/heads/commitref: $commit_id" >> $testroot/stdout.expected
180 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
181 echo "refs/heads/newref: $commit_id" >> $testroot/stdout.expected
182 echo "refs/heads/symbolicref: refs/heads/master" \
183 >> $testroot/stdout.expected
184 cmp -s $testroot/stdout $testroot/stdout.expected
185 ret=$?
186 if [ $ret -ne 0 ]; then
187 diff -u $testroot/stdout.expected $testroot/stdout
188 fi
189 test_done "$testroot" "$ret"
192 test_ref_delete() {
193 local testroot=`test_init ref_delete`
194 local commit_id=`git_show_head $testroot/repo`
196 for b in ref1 ref2 ref3; do
197 got ref -r $testroot/repo -c refs/heads/master refs/heads/$b
198 ret=$?
199 if [ $ret -ne 0 ]; then
200 echo "got ref command failed unexpectedly"
201 test_done "$testroot" "$ret"
202 return 1
203 fi
204 done
206 got ref -d -r $testroot/repo refs/heads/ref2 > $testroot/stdout
207 ret=$?
208 if [ $ret -ne 0 ]; then
209 echo "got ref command failed unexpectedly"
210 test_done "$testroot" "$ret"
211 return 1
212 fi
213 echo "Deleted refs/heads/ref2: $commit_id" > $testroot/stdout.expected
214 cmp -s $testroot/stdout $testroot/stdout.expected
215 ret=$?
216 if [ $ret -ne 0 ]; then
217 diff -u $testroot/stdout.expected $testroot/stdout
218 test_done "$testroot" "$ret"
219 return 1
220 fi
222 got ref -l -r $testroot/repo > $testroot/stdout
223 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
224 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
225 echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
226 echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
227 cmp -s $testroot/stdout $testroot/stdout.expected
228 ret=$?
229 if [ $ret -ne 0 ]; then
230 diff -u $testroot/stdout.expected $testroot/stdout
231 test_done "$testroot" "$ret"
232 return 1
233 fi
235 got ref -r $testroot/repo -d refs/heads/bogus_ref_name \
236 > $testroot/stdout 2> $testroot/stderr
237 ret=$?
238 if [ $ret -eq 0 ]; then
239 echo "got ref succeeded unexpectedly"
240 test_done "$testroot" "1"
241 return 1
242 fi
244 echo "got: reference refs/heads/bogus_ref_name not found" \
245 > $testroot/stderr.expected
246 cmp -s $testroot/stderr $testroot/stderr.expected
247 ret=$?
248 if [ $ret -ne 0 ]; then
249 diff -u $testroot/stderr.expected $testroot/stderr
250 test_done "$testroot" "$ret"
251 return 1
252 fi
254 (cd $testroot/repo && git pack-refs --all)
256 echo "modified alpha" > $testroot/repo/alpha
257 git_commit $testroot/repo -m "modified alpha"
258 local commit_id2=`git_show_head $testroot/repo`
260 # ref 'master' now exists in both packed and loose forms
262 got ref -l -r $testroot/repo > $testroot/stdout
263 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
264 echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
265 echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
266 echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
267 cmp -s $testroot/stdout $testroot/stdout.expected
268 ret=$?
269 if [ $ret -ne 0 ]; then
270 diff -u $testroot/stdout.expected $testroot/stdout
271 test_done "$testroot" "$ret"
272 return 1
273 fi
275 got ref -r $testroot/repo -d master >/dev/null
277 got ref -l -r $testroot/repo > $testroot/stdout
278 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
279 echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
280 echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
281 cmp -s $testroot/stdout $testroot/stdout.expected
282 ret=$?
283 if [ $ret -ne 0 ]; then
284 diff -u $testroot/stdout.expected $testroot/stdout
285 fi
286 test_done "$testroot" "$ret"
289 test_ref_list() {
290 local testroot=`test_init ref_list`
291 local commit_id=`git_show_head $testroot/repo`
293 # Create a tag pointing at a commit ID
294 got tag -r $testroot/repo -c $commit_id -m "1.0" "1.0" >/dev/null
295 ret=$?
296 if [ $ret -ne 0 ]; then
297 echo "got tag command failed unexpectedly"
298 test_done "$testroot" "$ret"
299 return 1
300 fi
301 local tag_id=`got ref -r $testroot/repo -l \
302 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
304 # Create a ref based on repository's HEAD reference
305 got ref -r $testroot/repo -c HEAD refs/foo/zoo
306 ret=$?
307 if [ $ret -ne 0 ]; then
308 echo "got ref command failed unexpectedly"
309 test_done "$testroot" "$ret"
310 return 1
311 fi
313 # Create a head ref based on another specific ref
314 (cd $testroot/repo && got ref -c refs/heads/master refs/foo/bar/baz)
315 ret=$?
316 if [ $ret -ne 0 ]; then
317 test_done "$testroot" "$ret"
318 return 1
319 fi
321 # Create a HEAD ref in the namespace of a remote repository
322 (cd $testroot/repo && got ref -s refs/heads/master \
323 refs/remotes/origin/HEAD)
324 ret=$?
325 if [ $ret -ne 0 ]; then
326 test_done "$testroot" "$ret"
327 return 1
328 fi
330 got ref -r $testroot/repo -l > $testroot/stdout
332 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
333 echo "refs/foo/bar/baz: $commit_id" >> $testroot/stdout.expected
334 echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
335 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
336 echo "refs/remotes/origin/HEAD: refs/heads/master" \
337 >> $testroot/stdout.expected
338 echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
339 cmp -s $testroot/stdout $testroot/stdout.expected
340 ret=$?
341 if [ $ret -ne 0 ]; then
342 diff -u $testroot/stdout.expected $testroot/stdout
343 test_done "$testroot" "$ret"
344 return 1
345 fi
347 got ref -r $testroot/repo -l refs > $testroot/stdout
349 echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
350 echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
351 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
352 echo "refs/remotes/origin/HEAD: refs/heads/master" \
353 >> $testroot/stdout.expected
354 echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
355 cmp -s $testroot/stdout $testroot/stdout.expected
356 ret=$?
357 if [ $ret -ne 0 ]; then
358 diff -u $testroot/stdout.expected $testroot/stdout
359 test_done "$testroot" "$ret"
360 return 1
361 fi
363 got ref -r $testroot/repo -l refs/tags > $testroot/stdout
365 echo "refs/tags/1.0: $tag_id" > $testroot/stdout.expected
366 cmp -s $testroot/stdout $testroot/stdout.expected
367 ret=$?
368 if [ $ret -ne 0 ]; then
369 diff -u $testroot/stdout.expected $testroot/stdout
370 test_done "$testroot" "$ret"
371 return 1
372 fi
374 for r in refs/foo/bar/baz refs/foo/bar/baz foo/bar/baz foo/bar; do
375 got ref -r $testroot/repo -l $r > $testroot/stdout
377 echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
378 cmp -s $testroot/stdout $testroot/stdout.expected
379 ret=$?
380 if [ $ret -ne 0 ]; then
381 diff -u $testroot/stdout.expected $testroot/stdout
382 test_done "$testroot" "$ret"
383 return 1
384 fi
385 done
387 for r in refs/foo foo; do
388 got ref -r $testroot/repo -l $r > $testroot/stdout
390 echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
391 echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
392 cmp -s $testroot/stdout $testroot/stdout.expected
393 ret=$?
394 if [ $ret -ne 0 ]; then
395 diff -u $testroot/stdout.expected $testroot/stdout
396 test_done "$testroot" "$ret"
397 return 1
398 fi
399 done
401 for r in /refs/abc refs//foo/bar refs//foo//bar refs////////foo//bar; do
402 got ref -r $testroot/repo -l $r > $testroot/stdout \
403 2> $testroot/stderr
405 echo -n > $testroot/stdout.expected
406 cmp -s $testroot/stdout $testroot/stdout.expected
407 ret=$?
408 if [ $ret -ne 0 ]; then
409 diff -u $testroot/stdout.expected $testroot/stdout
410 test_done "$testroot" "$ret"
411 return 1
412 fi
414 echo "got: $r: bad reference name" > $testroot/stderr.expected
415 cmp -s $testroot/stderr $testroot/stderr.expected
416 ret=$?
417 if [ $ret -ne 0 ]; then
418 diff -u $testroot/stderr.expected $testroot/stderr
419 test_done "$testroot" "$ret"
420 return 1
421 fi
422 done
424 # attempt to list non-existing references
425 for r in refs/fo bar baz moo riffs refs/abc refs/foo/bar/baz/moo; do
426 got ref -r $testroot/repo -l $r > $testroot/stdout
428 echo -n > $testroot/stdout.expected
429 cmp -s $testroot/stdout $testroot/stdout.expected
430 ret=$?
431 if [ $ret -ne 0 ]; then
432 diff -u $testroot/stdout.expected $testroot/stdout
433 test_done "$testroot" "$ret"
434 return 1
435 fi
436 done
438 test_done "$testroot" "$ret"
441 test_parseargs "$@"
442 run_test test_ref_create
443 run_test test_ref_delete
444 run_test test_ref_list