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.
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
26 if [ "$ret" != "0" ]; then
27 echo "got ref command failed unexpectedly"
28 test_done "$testroot" "$ret"
32 # Create a ref based on repository's HEAD reference
33 got ref -r $testroot/repo -c HEAD refs/heads/newref
35 if [ "$ret" != "0" ]; then
36 echo "got ref command failed unexpectedly"
37 test_done "$testroot" "$ret"
41 # Ensure that Git recognizes the ref Got has created
42 (cd $testroot/repo && git checkout -q newref)
44 if [ "$ret" != "0" ]; then
45 echo "git checkout command failed unexpectedly"
46 test_done "$testroot" "$ret"
50 # Ensure Got recognizes the new ref
51 got checkout -b newref $testroot/repo $testroot/wt >/dev/null
53 if [ "$ret" != "0" ]; then
54 echo "got checkout command failed unexpectedly"
55 test_done "$testroot" "$ret"
59 # Create a head ref based on another specific ref
60 (cd $testroot/wt && got ref -c refs/heads/master refs/heads/anotherref)
62 if [ "$ret" != "0" ]; then
63 test_done "$testroot" "$ret"
67 (cd $testroot/repo && git checkout -q anotherref)
69 if [ "$ret" != "0" ]; then
70 echo "git checkout command failed unexpectedly"
71 test_done "$testroot" "$ret"
74 # Create a symbolic ref
75 (cd $testroot/wt && got ref -s refs/heads/master refs/heads/symbolicref)
77 if [ "$ret" != "0" ]; then
78 test_done "$testroot" "$ret"
82 (cd $testroot/repo && git checkout -q symbolicref)
84 if [ "$ret" != "0" ]; then
85 echo "git checkout command failed unexpectedly"
86 test_done "$testroot" "$ret"
90 # Attempt to create a symbolic ref pointing at a non-reference
91 (cd $testroot/wt && got ref -s $commit_id refs/heads/symbolicref \
94 if [ "$ret" = "0" ]; then
95 echo "git ref command succeeded unexpectedly"
96 test_done "$testroot" "1"
100 echo "got: reference $commit_id not found" > $testroot/stderr.expected
101 cmp -s $testroot/stderr $testroot/stderr.expected
103 if [ "$ret" != "0" ]; then
104 diff -u $testroot/stderr.expected $testroot/stderr
105 test_done "$testroot" "$ret"
109 # Attempt to create a reference without specifying a name
110 (cd $testroot/wt && got ref -c $commit_id 2> $testroot/stderr)
112 if [ "$ret" = "0" ]; then
113 echo "git ref command succeeded unexpectedly"
114 test_done "$testroot" "1"
118 grep -q '^usage: got ref' $testroot/stderr
120 if [ "$ret" != "0" ]; then
121 echo "unexpected usage error message: " >&2
122 cat $testroot/stderr >&2
123 test_done "$testroot" "$ret"
127 # Attempt to create a symbolic reference without specifying a name
128 (cd $testroot/wt && got ref -s refs/heads/symbolicref \
131 if [ "$ret" = "0" ]; then
132 echo "git ref command succeeded unexpectedly"
133 test_done "$testroot" "1"
137 grep -q '^usage: got ref' $testroot/stderr
139 if [ "$ret" != "0" ]; then
140 echo "unexpected usage error message: " >&2
141 cat $testroot/stderr >&2
142 test_done "$testroot" "$ret"
147 got ref -r $testroot/repo -s refs/heads/newref HEAD
149 if [ "$ret" != "0" ]; then
150 echo "got ref command failed unexpectedly"
151 test_done "$testroot" "$ret"
155 # Ensure that Git recognizes the ref Got has created
156 (cd $testroot/repo && git checkout -q HEAD)
158 if [ "$ret" != "0" ]; then
159 echo "git checkout command failed unexpectedly"
160 test_done "$testroot" "$ret"
164 # Ensure Got recognizes the new ref
165 (cd $testroot/wt && got update -b HEAD >/dev/null)
167 if [ "$ret" != "0" ]; then
168 echo "got checkout command failed unexpectedly"
169 test_done "$testroot" "$ret"
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
185 if [ "$ret" != "0" ]; then
186 diff -u $testroot/stdout.expected $testroot/stdout
188 test_done "$testroot" "$ret"
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
198 if [ "$ret" != "0" ]; then
199 echo "got ref command failed unexpectedly"
200 test_done "$testroot" "$ret"
205 got ref -d -r $testroot/repo refs/heads/ref2 > $testroot/stdout
207 if [ "$ret" != "0" ]; then
208 echo "got ref command failed unexpectedly"
209 test_done "$testroot" "$ret"
213 got ref -l -r $testroot/repo > $testroot/stdout
214 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
215 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
216 echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
217 echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
218 cmp -s $testroot/stdout $testroot/stdout.expected
220 if [ "$ret" != "0" ]; then
221 diff -u $testroot/stdout.expected $testroot/stdout
222 test_done "$testroot" "$ret"
226 got ref -r $testroot/repo -d refs/heads/bogus_ref_name \
227 > $testroot/stdout 2> $testroot/stderr
229 if [ "$ret" = "0" ]; then
230 echo "got ref succeeded unexpectedly"
231 test_done "$testroot" "$ret"
235 echo "got: reference refs/heads/bogus_ref_name not found" \
236 > $testroot/stderr.expected
237 cmp -s $testroot/stderr $testroot/stderr.expected
239 if [ "$ret" != "0" ]; then
240 diff -u $testroot/stderr.expected $testroot/stderr
241 test_done "$testroot" "$ret"
245 (cd $testroot/repo && git pack-refs --all)
247 echo "modified alpha" > $testroot/repo/alpha
248 git_commit $testroot/repo -m "modified alpha"
249 local commit_id2=`git_show_head $testroot/repo`
251 # ref 'master' now exists in both packed and loose forms
253 got ref -l -r $testroot/repo > $testroot/stdout
254 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
255 echo "refs/heads/master: $commit_id2" >> $testroot/stdout.expected
256 echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
257 echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
258 cmp -s $testroot/stdout $testroot/stdout.expected
260 if [ "$ret" != "0" ]; then
261 diff -u $testroot/stdout.expected $testroot/stdout
262 test_done "$testroot" "$ret"
266 got ref -r $testroot/repo -d master
268 got ref -l -r $testroot/repo > $testroot/stdout
269 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
270 echo "refs/heads/ref1: $commit_id" >> $testroot/stdout.expected
271 echo "refs/heads/ref3: $commit_id" >> $testroot/stdout.expected
272 cmp -s $testroot/stdout $testroot/stdout.expected
274 if [ "$ret" != "0" ]; then
275 diff -u $testroot/stdout.expected $testroot/stdout
277 test_done "$testroot" "$ret"
281 local testroot=`test_init ref_list`
282 local commit_id=`git_show_head $testroot/repo`
284 # Create a tag pointing at a commit ID
285 got tag -r $testroot/repo -c $commit_id -m "1.0" "1.0" >/dev/null
287 if [ "$ret" != "0" ]; then
288 echo "got tag command failed unexpectedly"
289 test_done "$testroot" "$ret"
292 local tag_id=`got ref -r $testroot/repo -l \
293 | grep "^refs/tags/$tag" | tr -d ' ' | cut -d: -f2`
295 # Create a ref based on repository's HEAD reference
296 got ref -r $testroot/repo -c HEAD refs/foo/zoo
298 if [ "$ret" != "0" ]; then
299 echo "got ref command failed unexpectedly"
300 test_done "$testroot" "$ret"
304 # Create a head ref based on another specific ref
305 (cd $testroot/repo && got ref -c refs/heads/master refs/foo/bar/baz)
307 if [ "$ret" != "0" ]; then
308 test_done "$testroot" "$ret"
312 # Create a HEAD ref in the namespace of a remote repository
313 (cd $testroot/repo && got ref -s refs/heads/master \
314 refs/remotes/origin/HEAD)
316 if [ "$ret" != "0" ]; then
317 test_done "$testroot" "$ret"
321 got ref -r $testroot/repo -l > $testroot/stdout
323 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
324 echo "refs/foo/bar/baz: $commit_id" >> $testroot/stdout.expected
325 echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
326 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
327 echo "refs/remotes/origin/HEAD: refs/heads/master" \
328 >> $testroot/stdout.expected
329 echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
330 cmp -s $testroot/stdout $testroot/stdout.expected
332 if [ "$ret" != "0" ]; then
333 diff -u $testroot/stdout.expected $testroot/stdout
334 test_done "$testroot" "$ret"
338 got ref -r $testroot/repo -l refs > $testroot/stdout
340 echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
341 echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
342 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
343 echo "refs/remotes/origin/HEAD: refs/heads/master" \
344 >> $testroot/stdout.expected
345 echo "refs/tags/1.0: $tag_id" >> $testroot/stdout.expected
346 cmp -s $testroot/stdout $testroot/stdout.expected
348 if [ "$ret" != "0" ]; then
349 diff -u $testroot/stdout.expected $testroot/stdout
350 test_done "$testroot" "$ret"
354 got ref -r $testroot/repo -l refs/tags > $testroot/stdout
356 echo "refs/tags/1.0: $tag_id" > $testroot/stdout.expected
357 cmp -s $testroot/stdout $testroot/stdout.expected
359 if [ "$ret" != "0" ]; then
360 diff -u $testroot/stdout.expected $testroot/stdout
361 test_done "$testroot" "$ret"
365 for r in refs/foo/bar/baz refs/foo/bar/baz foo/bar/baz foo/bar; do
366 got ref -r $testroot/repo -l $r > $testroot/stdout
368 echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
369 cmp -s $testroot/stdout $testroot/stdout.expected
371 if [ "$ret" != "0" ]; then
372 diff -u $testroot/stdout.expected $testroot/stdout
373 test_done "$testroot" "$ret"
378 for r in refs/foo foo; do
379 got ref -r $testroot/repo -l $r > $testroot/stdout
381 echo "refs/foo/bar/baz: $commit_id" > $testroot/stdout.expected
382 echo "refs/foo/zoo: $commit_id" >> $testroot/stdout.expected
383 cmp -s $testroot/stdout $testroot/stdout.expected
385 if [ "$ret" != "0" ]; then
386 diff -u $testroot/stdout.expected $testroot/stdout
387 test_done "$testroot" "$ret"
392 for r in /refs/abc refs//foo/bar refs//foo//bar refs////////foo//bar; do
393 got ref -r $testroot/repo -l $r > $testroot/stdout \
396 echo -n > $testroot/stdout.expected
397 cmp -s $testroot/stdout $testroot/stdout.expected
399 if [ "$ret" != "0" ]; then
400 diff -u $testroot/stdout.expected $testroot/stdout
401 test_done "$testroot" "$ret"
405 echo "got: $r: bad reference name" > $testroot/stderr.expected
406 cmp -s $testroot/stderr $testroot/stderr.expected
408 if [ "$ret" != "0" ]; then
409 diff -u $testroot/stderr.expected $testroot/stderr
410 test_done "$testroot" "$ret"
415 # attempt to list non-existing references
416 for r in refs/fo bar baz moo riffs refs/abc refs/foo/bar/baz/moo; do
417 got ref -r $testroot/repo -l $r > $testroot/stdout
419 echo -n > $testroot/stdout.expected
420 cmp -s $testroot/stdout $testroot/stdout.expected
422 if [ "$ret" != "0" ]; then
423 diff -u $testroot/stdout.expected $testroot/stdout
424 test_done "$testroot" "$ret"
429 test_done "$testroot" "$ret"
433 run_test test_ref_create
434 run_test test_ref_delete
435 run_test test_ref_list