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" != "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" != "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" != "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" != "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" != "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" != "0" ]; then
70 echo "git checkout command failed unexpectedly"
71 test_done "$testroot" "$ret"
72 fi
74 # Create a symbolic ref
75 (cd $testroot/wt && got ref -s refs/heads/master refs/heads/symbolicref)
76 ret="$?"
77 if [ "$ret" != "0" ]; then
78 test_done "$testroot" "$ret"
79 return 1
80 fi
82 (cd $testroot/repo && git checkout -q symbolicref)
83 ret="$?"
84 if [ "$ret" != "0" ]; then
85 echo "git checkout command failed unexpectedly"
86 test_done "$testroot" "$ret"
87 return 1
88 fi
90 # Attempt to create a symbolic ref pointing at a non-reference
91 (cd $testroot/wt && got ref -s $commit_id refs/heads/symbolicref \
92 2> $testroot/stderr)
93 ret="$?"
94 if [ "$ret" = "0" ]; then
95 echo "git ref command succeeded unexpectedly"
96 test_done "$testroot" "1"
97 return 1
98 fi
100 echo "got: reference $commit_id not found" > $testroot/stderr.expected
101 cmp -s $testroot/stderr $testroot/stderr.expected
102 ret="$?"
103 if [ "$ret" != "0" ]; then
104 diff -u $testroot/stderr.expected $testroot/stderr
105 test_done "$testroot" "$ret"
106 return 1
107 fi
109 # Attempt to create a reference without specifying a name
110 (cd $testroot/wt && got ref -c $commit_id 2> $testroot/stderr)
111 ret="$?"
112 if [ "$ret" = "0" ]; then
113 echo "git ref command succeeded unexpectedly"
114 test_done "$testroot" "1"
115 return 1
116 fi
118 grep -q '^usage: got ref' $testroot/stderr
119 ret="$?"
120 if [ "$ret" != "0" ]; then
121 echo "unexpected usage error message: " >&2
122 cat $testroot/stderr >&2
123 test_done "$testroot" "$ret"
124 return 1
125 fi
127 # Attempt to create a symbolic reference without specifying a name
128 (cd $testroot/wt && got ref -s refs/heads/symbolicref \
129 2> $testroot/stderr)
130 ret="$?"
131 if [ "$ret" = "0" ]; then
132 echo "git ref command succeeded unexpectedly"
133 test_done "$testroot" "1"
134 return 1
135 fi
137 grep -q '^usage: got ref' $testroot/stderr
138 ret="$?"
139 if [ "$ret" != "0" ]; then
140 echo "unexpected usage error message: " >&2
141 cat $testroot/stderr >&2
142 test_done "$testroot" "$ret"
143 return 1
144 fi
146 # Change HEAD
147 got ref -r $testroot/repo -s refs/heads/newref HEAD
148 ret="$?"
149 if [ "$ret" != "0" ]; then
150 echo "got ref command failed unexpectedly"
151 test_done "$testroot" "$ret"
152 return 1
153 fi
155 # Ensure that Git recognizes the ref Got has created
156 (cd $testroot/repo && git checkout -q HEAD)
157 ret="$?"
158 if [ "$ret" != "0" ]; then
159 echo "git checkout command failed unexpectedly"
160 test_done "$testroot" "$ret"
161 return 1
162 fi
164 # Ensure Got recognizes the new ref
165 (cd $testroot/wt && got update -b HEAD >/dev/null)
166 ret="$?"
167 if [ "$ret" != "0" ]; then
168 echo "got checkout command failed unexpectedly"
169 test_done "$testroot" "$ret"
170 return 1
171 fi
172 got ref -r $testroot/repo -l > $testroot/stdout
173 echo "HEAD: refs/heads/newref" > $testroot/stdout.expected
174 echo -n "refs/got/worktree/base-" >> $testroot/stdout.expected
175 cat $testroot/wt/.got/uuid | tr -d '\n' >> $testroot/stdout.expected
176 echo ": $commit_id" >> $testroot/stdout.expected
177 echo "refs/heads/anotherref: $commit_id" >> $testroot/stdout.expected
178 echo "refs/heads/commitref: $commit_id" >> $testroot/stdout.expected
179 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
180 echo "refs/heads/newref: $commit_id" >> $testroot/stdout.expected
181 echo "refs/heads/symbolicref: refs/heads/master" \
182 >> $testroot/stdout.expected
183 cmp -s $testroot/stdout $testroot/stdout.expected
184 ret="$?"
185 if [ "$ret" != "0" ]; then
186 diff -u $testroot/stdout.expected $testroot/stdout
187 fi
188 test_done "$testroot" "$ret"
191 test_ref_delete() {
192 local testroot=`test_init ref_delete`
193 local commit_id=`git_show_head $testroot/repo`
195 for b in ref1 ref2 ref3; do
196 got ref -r $testroot/repo -c refs/heads/master refs/heads/$b
197 ret="$?"
198 if [ "$ret" != "0" ]; then
199 echo "got ref command failed unexpectedly"
200 test_done "$testroot" "$ret"
201 return 1
202 fi
203 done
205 got ref -d -r $testroot/repo refs/heads/ref2 > $testroot/stdout
206 ret="$?"
207 if [ "$ret" != "0" ]; then
208 echo "got ref command failed unexpectedly"
209 test_done "$testroot" "$ret"
210 return 1
211 fi
212 echo "Deleted refs/heads/ref2: $commit_id" > $testroot/stdout.expected
213 cmp -s $testroot/stdout $testroot/stdout.expected
214 ret="$?"
215 if [ "$ret" != "0" ]; then
216 diff -u $testroot/stdout.expected $testroot/stdout
217 test_done "$testroot" "$ret"
218 return 1
219 fi
221 got ref -l -r $testroot/repo > $testroot/stdout
222 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
223 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
224 echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
225 echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
226 cmp -s $testroot/stdout $testroot/stdout.expected
227 ret="$?"
228 if [ "$ret" != "0" ]; then
229 diff -u $testroot/stdout.expected $testroot/stdout
230 test_done "$testroot" "$ret"
231 return 1
232 fi
234 got ref -r $testroot/repo -d refs/heads/bogus_ref_name \
235 > $testroot/stdout 2> $testroot/stderr
236 ret="$?"
237 if [ "$ret" = "0" ]; then
238 echo "got ref succeeded unexpectedly"
239 test_done "$testroot" "$ret"
240 return 1
241 fi
243 echo "got: reference refs/heads/bogus_ref_name not found" \
244 > $testroot/stderr.expected
245 cmp -s $testroot/stderr $testroot/stderr.expected
246 ret="$?"
247 if [ "$ret" != "0" ]; then
248 diff -u $testroot/stderr.expected $testroot/stderr
249 test_done "$testroot" "$ret"
250 return 1
251 fi
253 (cd $testroot/repo && git pack-refs --all)
255 echo "modified alpha" > $testroot/repo/alpha
256 git_commit $testroot/repo -m "modified alpha"
257 local commit_id2=`git_show_head $testroot/repo`
259 # ref 'master' now exists in both packed and loose forms
261 got ref -l -r $testroot/repo > $testroot/stdout
262 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
263 echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
264 echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
265 echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
266 cmp -s $testroot/stdout $testroot/stdout.expected
267 ret="$?"
268 if [ "$ret" != "0" ]; then
269 diff -u $testroot/stdout.expected $testroot/stdout
270 test_done "$testroot" "$ret"
271 return 1
272 fi
274 got ref -r $testroot/repo -d master >/dev/null
276 got ref -l -r $testroot/repo > $testroot/stdout
277 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
278 echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
279 echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
280 cmp -s $testroot/stdout $testroot/stdout.expected
281 ret="$?"
282 if [ "$ret" != "0" ]; then
283 diff -u $testroot/stdout.expected $testroot/stdout
284 fi
285 test_done "$testroot" "$ret"
288 test_ref_list() {
289 local testroot=`test_init ref_list`
290 local commit_id=`git_show_head $testroot/repo`
292 # Create a tag pointing at a commit ID
293 got tag -r $testroot/repo -c $commit_id -m "1.0" "1.0" >/dev/null
294 ret="$?"
295 if [ "$ret" != "0" ]; then
296 echo "got tag command failed unexpectedly"
297 test_done "$testroot" "$ret"
298 return 1
299 fi
300 local tag_id=`got ref -r $testroot/repo -l \
301 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
303 # Create a ref based on repository's HEAD reference
304 got ref -r $testroot/repo -c HEAD refs/foo/zoo
305 ret="$?"
306 if [ "$ret" != "0" ]; then
307 echo "got ref command failed unexpectedly"
308 test_done "$testroot" "$ret"
309 return 1
310 fi
312 # Create a head ref based on another specific ref
313 (cd $testroot/repo && got ref -c refs/heads/master refs/foo/bar/baz)
314 ret="$?"
315 if [ "$ret" != "0" ]; then
316 test_done "$testroot" "$ret"
317 return 1
318 fi
320 # Create a HEAD ref in the namespace of a remote repository
321 (cd $testroot/repo && got ref -s refs/heads/master \
322 refs/remotes/origin/HEAD)
323 ret="$?"
324 if [ "$ret" != "0" ]; then
325 test_done "$testroot" "$ret"
326 return 1
327 fi
329 got ref -r $testroot/repo -l > $testroot/stdout
331 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
332 echo "refs/foo/bar/baz: $commit_id" >> $testroot/stdout.expected
333 echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
334 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
335 echo "refs/remotes/origin/HEAD: refs/heads/master" \
336 >> $testroot/stdout.expected
337 echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
338 cmp -s $testroot/stdout $testroot/stdout.expected
339 ret="$?"
340 if [ "$ret" != "0" ]; then
341 diff -u $testroot/stdout.expected $testroot/stdout
342 test_done "$testroot" "$ret"
343 return 1
344 fi
346 got ref -r $testroot/repo -l refs > $testroot/stdout
348 echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
349 echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
350 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
351 echo "refs/remotes/origin/HEAD: refs/heads/master" \
352 >> $testroot/stdout.expected
353 echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
354 cmp -s $testroot/stdout $testroot/stdout.expected
355 ret="$?"
356 if [ "$ret" != "0" ]; then
357 diff -u $testroot/stdout.expected $testroot/stdout
358 test_done "$testroot" "$ret"
359 return 1
360 fi
362 got ref -r $testroot/repo -l refs/tags > $testroot/stdout
364 echo "refs/tags/1.0: $tag_id" > $testroot/stdout.expected
365 cmp -s $testroot/stdout $testroot/stdout.expected
366 ret="$?"
367 if [ "$ret" != "0" ]; then
368 diff -u $testroot/stdout.expected $testroot/stdout
369 test_done "$testroot" "$ret"
370 return 1
371 fi
373 for r in refs/foo/bar/baz refs/foo/bar/baz foo/bar/baz foo/bar; do
374 got ref -r $testroot/repo -l $r > $testroot/stdout
376 echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
377 cmp -s $testroot/stdout $testroot/stdout.expected
378 ret="$?"
379 if [ "$ret" != "0" ]; then
380 diff -u $testroot/stdout.expected $testroot/stdout
381 test_done "$testroot" "$ret"
382 return 1
383 fi
384 done
386 for r in refs/foo foo; do
387 got ref -r $testroot/repo -l $r > $testroot/stdout
389 echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
390 echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
391 cmp -s $testroot/stdout $testroot/stdout.expected
392 ret="$?"
393 if [ "$ret" != "0" ]; then
394 diff -u $testroot/stdout.expected $testroot/stdout
395 test_done "$testroot" "$ret"
396 return 1
397 fi
398 done
400 for r in /refs/abc refs//foo/bar refs//foo//bar refs////////foo//bar; do
401 got ref -r $testroot/repo -l $r > $testroot/stdout \
402 2> $testroot/stderr
404 echo -n > $testroot/stdout.expected
405 cmp -s $testroot/stdout $testroot/stdout.expected
406 ret="$?"
407 if [ "$ret" != "0" ]; then
408 diff -u $testroot/stdout.expected $testroot/stdout
409 test_done "$testroot" "$ret"
410 return 1
411 fi
413 echo "got: $r: bad reference name" > $testroot/stderr.expected
414 cmp -s $testroot/stderr $testroot/stderr.expected
415 ret="$?"
416 if [ "$ret" != "0" ]; then
417 diff -u $testroot/stderr.expected $testroot/stderr
418 test_done "$testroot" "$ret"
419 return 1
420 fi
421 done
423 # attempt to list non-existing references
424 for r in refs/fo bar baz moo riffs refs/abc refs/foo/bar/baz/moo; do
425 got ref -r $testroot/repo -l $r > $testroot/stdout
427 echo -n > $testroot/stdout.expected
428 cmp -s $testroot/stdout $testroot/stdout.expected
429 ret="$?"
430 if [ "$ret" != "0" ]; then
431 diff -u $testroot/stdout.expected $testroot/stdout
432 test_done "$testroot" "$ret"
433 return 1
434 fi
435 done
437 test_done "$testroot" "$ret"
440 test_parseargs "$@"
441 run_test test_ref_create
442 run_test test_ref_delete
443 run_test test_ref_list