portable: release 0.93
[got-portable.git] / regress / cmdline / ref.sh
blob0dcbb993cdc019b50ced407fa75909e73e435526
1 #!/bin/sh
3 # Copyright (c) 2019 Stefan Sperling <stsp@openbsd.org>
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
436 done
438 test_done "$testroot" "$ret"
441 test_ref_commit_keywords() {
442 local testroot=$(test_init ref_commit_keywords)
443 local repo="$testroot/repo"
444 local wt="$testroot/wt"
446 got checkout "$repo" "$wt" > /dev/null
447 ret=$?
448 if [ $ret -ne 0 ]; then
449 echo "checkout failed unexpectedly" >&2
450 test_done "$testroot" "$ret"
451 return 1
454 for i in $(seq 8); do
455 echo "alpha change $i" > "$wt/alpha"
457 (cd "$wt" && got ci -m "commit number $i" > /dev/null)
458 ret=$?
459 if [ $ret -ne 0 ]; then
460 echo "commit failed unexpectedly" >&2
461 test_done "$testroot" "$ret"
462 return 1
465 set -- "$@" "$(git_show_head $repo)"
466 done
468 (cd "$wt" && got ref -c:head:-4 refs/heads/head-4)
469 ret=$?
470 if [ $ret -ne 0 ]; then
471 echo "got ref command failed unexpectedly"
472 test_done "$testroot" "$ret"
473 return 1
476 (cd "$wt" && got up -c head-4 > /dev/null)
477 ret=$?
478 if [ $ret -ne 0 ]; then
479 echo "got checkout command failed unexpectedly"
480 test_done "$testroot" "$ret"
481 return 1
484 (cd "$wt" && got ref -c:base:+2 refs/heads/base+2)
485 ret=$?
486 if [ $ret -ne 0 ]; then
487 echo "got ref command failed unexpectedly"
488 test_done "$testroot" "$ret"
489 return 1
492 (cd "$wt" && got ref -cmaster:- refs/heads/master-)
493 ret=$?
494 if [ $ret -ne 0 ]; then
495 echo "got ref command failed unexpectedly"
496 test_done "$testroot" "$ret"
497 return 1
500 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
501 echo -n "refs/got/worktree/base-" >> $testroot/stdout.expected
502 cat "$wt/.got/uuid" | tr -d '\n' >> $testroot/stdout.expected
503 echo ": $(pop_idx 4 $@)" >> $testroot/stdout.expected
504 echo "refs/heads/base+2: $(pop_idx 6 $@)" >> $testroot/stdout.expected
505 echo "refs/heads/head-4: $(pop_idx 4 $@)" >> $testroot/stdout.expected
506 echo "refs/heads/master: $(pop_idx 8 $@)" >> $testroot/stdout.expected
507 echo "refs/heads/master-: $(pop_idx 7 $@)" >> $testroot/stdout.expected
509 got ref -r "$repo" -l > $testroot/stdout
510 cmp -s $testroot/stdout $testroot/stdout.expected
511 ret=$?
512 if [ $ret -ne 0 ]; then
513 diff -u $testroot/stdout.expected $testroot/stdout
515 test_done "$testroot" "$ret"
518 test_parseargs "$@"
519 run_test test_ref_create
520 run_test test_ref_delete
521 run_test test_ref_list
522 run_test test_ref_commit_keywords