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 if [ "${GOT_TEST_ALGO}" = sha256
]; then
21 format_arg
="-A sha256"
25 local testname
=import_basic
26 local testroot
=`mktemp -d \
27 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
29 got init
$format_arg $testroot/repo
32 make_test_tree
$testroot/tree
34 got import
-m 'init' -r $testroot/repo
$testroot/tree \
37 if [ $ret -ne 0 ]; then
38 test_done
"$testroot" "$ret"
42 local head_commit
=`git_show_head $testroot/repo`
43 echo "A $testroot/tree/epsilon/zeta" > $testroot/stdout.expected
44 echo "A $testroot/tree/gamma/delta" >> $testroot/stdout.expected
45 echo "A $testroot/tree/alpha" >> $testroot/stdout.expected
46 echo "A $testroot/tree/beta" >> $testroot/stdout.expected
47 echo "Created branch refs/heads/main with commit $head_commit" \
48 >> $testroot/stdout.expected
50 cmp -s $testroot/stdout.expected
$testroot/stdout
52 if [ $ret -ne 0 ]; then
53 diff -u $testroot/stdout.expected
$testroot/stdout
54 test_done
"$testroot" "$ret"
58 (cd $testroot/repo
&& got log
-p |
grep -v ^
date: > $testroot/stdout
)
60 id_alpha
=`get_blob_id $testroot/repo "" alpha`
61 id_beta
=`get_blob_id $testroot/repo "" beta`
62 id_zeta
=`get_blob_id $testroot/repo epsilon zeta`
63 id_delta
=`get_blob_id $testroot/repo gamma delta`
64 tree_id
=`(cd $testroot/repo && got cat $head_commit | \
65 grep ^tree | cut -d ' ' -f 2)`
67 echo "-----------------------------------------------" \
68 > $testroot/stdout.expected
69 echo "commit $head_commit (main)" >> $testroot/stdout.expected
70 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
71 echo " " >> $testroot/stdout.expected
72 echo " init" >> $testroot/stdout.expected
73 echo " " >> $testroot/stdout.expected
74 echo "diff /dev/null $head_commit" >> $testroot/stdout.expected
75 echo "commit - /dev/null" >> $testroot/stdout.expected
76 echo "commit + $head_commit" >> $testroot/stdout.expected
77 echo "blob - /dev/null" >> $testroot/stdout.expected
78 echo "blob + $id_alpha (mode 644)" >> $testroot/stdout.expected
79 echo "--- /dev/null" >> $testroot/stdout.expected
80 echo "+++ alpha" >> $testroot/stdout.expected
81 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
82 echo "+alpha" >> $testroot/stdout.expected
83 echo "blob - /dev/null" >> $testroot/stdout.expected
84 echo "blob + $id_beta (mode 644)" >> $testroot/stdout.expected
85 echo "--- /dev/null" >> $testroot/stdout.expected
86 echo "+++ beta" >> $testroot/stdout.expected
87 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
88 echo "+beta" >> $testroot/stdout.expected
89 echo "blob - /dev/null" >> $testroot/stdout.expected
90 echo "blob + $id_zeta (mode 644)" >> $testroot/stdout.expected
91 echo "--- /dev/null" >> $testroot/stdout.expected
92 echo "+++ epsilon/zeta" >> $testroot/stdout.expected
93 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
94 echo "+zeta" >> $testroot/stdout.expected
95 echo "blob - /dev/null" >> $testroot/stdout.expected
96 echo "blob + $id_delta (mode 644)" >> $testroot/stdout.expected
97 echo "--- /dev/null" >> $testroot/stdout.expected
98 echo "+++ gamma/delta" >> $testroot/stdout.expected
99 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
100 echo "+delta" >> $testroot/stdout.expected
101 echo "" >> $testroot/stdout.expected
103 cmp -s $testroot/stdout.expected
$testroot/stdout
105 if [ $ret -ne 0 ]; then
106 diff -u $testroot/stdout.expected
$testroot/stdout
107 test_done
"$testroot" "$ret"
111 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
112 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
113 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
114 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
115 echo "Checked out refs/heads/main: $head_commit" \
116 >> $testroot/stdout.expected
117 echo "Now shut up and hack" >> $testroot/stdout.expected
119 got checkout
$testroot/repo
$testroot/wt
> $testroot/stdout
121 if [ $ret -ne 0 ]; then
122 test_done
"$testroot" "$ret"
126 cmp -s $testroot/stdout.expected
$testroot/stdout
128 if [ $ret -ne 0 ]; then
129 diff -u $testroot/stdout.expected
$testroot/stdout
130 test_done
"$testroot" "$ret"
134 echo "alpha" > $testroot/content.expected
135 echo "beta" >> $testroot/content.expected
136 echo "zeta" >> $testroot/content.expected
137 echo "delta" >> $testroot/content.expected
138 cat $testroot/wt
/alpha
$testroot/wt
/beta
$testroot/wt
/epsilon
/zeta \
139 $testroot/wt
/gamma
/delta
> $testroot/content
141 cmp -s $testroot/content.expected
$testroot/content
143 if [ $ret -ne 0 ]; then
144 diff -u $testroot/content.expected
$testroot/content
146 test_done
"$testroot" "$ret"
149 test_import_specified_head
() {
150 local testname
=import_specified_head
151 local testroot
=`mktemp -d \
152 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
155 gotadmin init
$format_arg -b $headref $testroot/repo
158 make_test_tree
$testroot/tree
160 got import
-m init
-r $testroot/repo
$testroot/tree
> $testroot/stdout
162 if [ $ret -ne 0 ]; then
163 test_done
"$testroot" "$ret"
167 local head_commit
=`git_show_head $testroot/repo`
168 echo "A $testroot/tree/epsilon/zeta" > $testroot/stdout.expected
169 echo "A $testroot/tree/gamma/delta" >> $testroot/stdout.expected
170 echo "A $testroot/tree/alpha" >> $testroot/stdout.expected
171 echo "A $testroot/tree/beta" >> $testroot/stdout.expected
172 echo "Created branch refs/heads/$headref with commit $head_commit" \
173 >> $testroot/stdout.expected
175 cmp -s $testroot/stdout.expected
$testroot/stdout
177 if [ $ret -ne 0 ]; then
179 diff -u $testroot/stdout.expected
$testroot/stdout
180 test_done
"$testroot" "$ret"
184 (cd $testroot/repo
&& got log
-p |
grep -v ^
date: > $testroot/stdout
)
186 id_alpha
=`get_blob_id $testroot/repo "" alpha`
187 id_beta
=`get_blob_id $testroot/repo "" beta`
188 id_zeta
=`get_blob_id $testroot/repo epsilon zeta`
189 id_delta
=`get_blob_id $testroot/repo gamma delta`
190 tree_id
=`(cd $testroot/repo && got cat $head_commit | \
191 grep ^tree | cut -d ' ' -f 2)`
193 echo "-----------------------------------------------" \
194 > $testroot/stdout.expected
195 echo "commit $head_commit ($headref)" >> $testroot/stdout.expected
196 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
197 echo " " >> $testroot/stdout.expected
198 echo " init" >> $testroot/stdout.expected
199 echo " " >> $testroot/stdout.expected
200 echo "diff /dev/null $head_commit" >> $testroot/stdout.expected
201 echo "commit - /dev/null" >> $testroot/stdout.expected
202 echo "commit + $head_commit" >> $testroot/stdout.expected
203 echo "blob - /dev/null" >> $testroot/stdout.expected
204 echo "blob + $id_alpha (mode 644)" >> $testroot/stdout.expected
205 echo "--- /dev/null" >> $testroot/stdout.expected
206 echo "+++ alpha" >> $testroot/stdout.expected
207 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
208 echo "+alpha" >> $testroot/stdout.expected
209 echo "blob - /dev/null" >> $testroot/stdout.expected
210 echo "blob + $id_beta (mode 644)" >> $testroot/stdout.expected
211 echo "--- /dev/null" >> $testroot/stdout.expected
212 echo "+++ beta" >> $testroot/stdout.expected
213 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
214 echo "+beta" >> $testroot/stdout.expected
215 echo "blob - /dev/null" >> $testroot/stdout.expected
216 echo "blob + $id_zeta (mode 644)" >> $testroot/stdout.expected
217 echo "--- /dev/null" >> $testroot/stdout.expected
218 echo "+++ epsilon/zeta" >> $testroot/stdout.expected
219 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
220 echo "+zeta" >> $testroot/stdout.expected
221 echo "blob - /dev/null" >> $testroot/stdout.expected
222 echo "blob + $id_delta (mode 644)" >> $testroot/stdout.expected
223 echo "--- /dev/null" >> $testroot/stdout.expected
224 echo "+++ gamma/delta" >> $testroot/stdout.expected
225 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
226 echo "+delta" >> $testroot/stdout.expected
227 echo "" >> $testroot/stdout.expected
229 cmp -s $testroot/stdout.expected
$testroot/stdout
231 if [ $ret -ne 0 ]; then
233 diff -u $testroot/stdout.expected
$testroot/stdout
234 test_done
"$testroot" "$ret"
238 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
239 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
240 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
241 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
242 echo "Checked out refs/heads/$headref: $head_commit" \
243 >> $testroot/stdout.expected
244 echo "Now shut up and hack" >> $testroot/stdout.expected
246 got checkout
$testroot/repo
$testroot/wt
> $testroot/stdout
247 cmp -s $testroot/stdout.expected
$testroot/stdout
249 if [ $ret -ne 0 ]; then
251 diff -u $testroot/stdout.expected
$testroot/stdout
252 test_done
"$testroot" "$ret"
256 cmp -s $testroot/stdout.expected
$testroot/stdout
258 if [ $ret -ne 0 ]; then
260 diff -u $testroot/stdout.expected
$testroot/stdout
261 test_done
"$testroot" "$ret"
265 echo "alpha" > $testroot/content.expected
266 echo "beta" >> $testroot/content.expected
267 echo "zeta" >> $testroot/content.expected
268 echo "delta" >> $testroot/content.expected
269 cat $testroot/wt
/alpha
$testroot/wt
/beta
$testroot/wt
/epsilon
/zeta \
270 $testroot/wt
/gamma
/delta
> $testroot/content
272 cmp -s $testroot/content.expected
$testroot/content
274 if [ $ret -ne 0 ]; then
276 diff -u $testroot/content.expected
$testroot/content
278 test_done
"$testroot" "$ret"
281 test_import_detached_head
() {
282 local testroot
=`test_init import_detached_head`
284 # mute verbose 'detached HEAD' warning
285 git
-C $testroot/repo config
--local advice.detachedHead false
287 if [ $ret -ne 0 ]; then
288 test_done
"$testroot" "$ret"
292 # enter detached HEAD state
293 local head_commit
=`git_show_head $testroot/repo | cut -c1-7`
294 git
-C $testroot/repo checkout
$head_commit \
295 > $testroot/stdout
2> $testroot/stderr
297 if [ $ret -ne 0 ]; then
298 test_done
"$testroot" "$ret"
302 echo "HEAD is now at $head_commit adding the test tree" >> \
303 $testroot/stderr.expected
305 cmp -s $testroot/stderr.expected
$testroot/stderr
307 if [ $ret -ne 0 ]; then
309 diff -u $testroot/stderr.expected
$testroot/stderr
310 test_done
"$testroot" "$ret"
314 mkdir
$testroot/import
315 make_test_tree
$testroot/import
317 # detached HEAD (i.e., not symbolic) so import should fallback to "main"
318 got import
-r $testroot/repo
-m init
$testroot/import
> $testroot/stdout
320 if [ $ret -ne 0 ]; then
321 test_done
"$testroot" "$ret"
325 local main_commit
=`git -C $testroot/repo show-ref main | \
327 echo "A $testroot/import/epsilon/zeta" > $testroot/stdout.expected
328 echo "A $testroot/import/gamma/delta" >> $testroot/stdout.expected
329 echo "A $testroot/import/alpha" >> $testroot/stdout.expected
330 echo "A $testroot/import/beta" >> $testroot/stdout.expected
331 echo "Created branch refs/heads/main with commit $main_commit" \
332 >> $testroot/stdout.expected
334 cmp -s $testroot/stdout.expected
$testroot/stdout
336 if [ $ret -ne 0 ]; then
338 diff -u $testroot/stdout.expected
$testroot/stdout
340 test_done
"$testroot" "$ret"
343 test_import_requires_new_branch
() {
344 local testroot
=`test_init import_requires_new_branch`
347 make_test_tree
$testroot/tree
349 got import
-b master
-m 'init' -r $testroot/repo
$testroot/tree \
350 > $testroot/stdout
2> $testroot/stderr
352 if [ $ret -eq 0 ]; then
353 echo "import command should have failed but did not"
354 test_done
"$testroot" "1"
358 echo "got: import target branch already exists" \
359 > $testroot/stderr.expected
360 cmp -s $testroot/stderr.expected
$testroot/stderr
362 if [ $ret -ne 0 ]; then
363 diff -u $testroot/stderr.expected
$testroot/stderr
364 test_done
"$testroot" "$ret"
368 got import
-b newbranch
-m 'init' -r $testroot/repo
$testroot/tree \
371 test_done
"$testroot" "$ret"
375 test_import_ignores
() {
376 local testname
=import_ignores
377 local testroot
=`mktemp -d \
378 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
380 got init
$format_arg $testroot/repo
383 make_test_tree
$testroot/tree
385 touch $testroot/tree
/upsilon
386 mkdir
$testroot/tree
/ysilon
387 got import
-I alpha
-I 'beta/' -I '*lta*' -I '*silon/' \
388 -m 'init' -r $testroot/repo
$testroot/tree
> $testroot/stdout
390 if [ $ret -ne 0 ]; then
391 test_done
"$testroot" "$ret"
395 local head_commit
=`git_show_head $testroot/repo`
396 echo "A $testroot/tree/beta" >> $testroot/stdout.expected
397 echo "A $testroot/tree/upsilon" >> $testroot/stdout.expected
398 echo "Created branch refs/heads/main with commit $head_commit" \
399 >> $testroot/stdout.expected
401 cmp -s $testroot/stdout.expected
$testroot/stdout
403 if [ $ret -ne 0 ]; then
404 diff -u $testroot/stdout.expected
$testroot/stdout
406 test_done
"$testroot" "$ret"
409 test_import_empty_dir
() {
410 local testname
=import_empty_dir
411 local testroot
=`mktemp -d \
412 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
414 gotadmin init
$format_arg $testroot/repo
417 mkdir
-p $testroot/tree
/empty
$testroot/tree
/notempty
418 echo "alpha" > $testroot/tree
/notempty
/alpha
420 got import
-m 'init' -r $testroot/repo
$testroot/tree
> $testroot/stdout
422 if [ $ret -ne 0 ]; then
423 test_done
"$testroot" "$ret"
427 local head_commit
=`git_show_head $testroot/repo`
428 echo "A $testroot/tree/notempty/alpha" >> $testroot/stdout.expected
429 echo "Created branch refs/heads/main with commit $head_commit" \
430 >> $testroot/stdout.expected
432 cmp -s $testroot/stdout.expected
$testroot/stdout
434 if [ $ret -ne 0 ]; then
435 diff -u $testroot/stdout.expected
$testroot/stdout
436 test_done
"$testroot" "$ret"
440 # Verify that Got did not import the empty directory
441 echo "notempty/" > $testroot/stdout.expected
442 echo "notempty/alpha" >> $testroot/stdout.expected
444 got tree
-r $testroot/repo
-R > $testroot/stdout
445 cmp -s $testroot/stdout.expected
$testroot/stdout
447 if [ $ret -ne 0 ]; then
448 diff -u $testroot/stdout.expected
$testroot/stdout
450 test_done
"$testroot" "$ret"
453 test_import_symlink
() {
454 local testname
=import_symlink
455 local testroot
=`mktemp -d \
456 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
458 gotadmin init
$format_arg $testroot/repo
461 echo 'this is file alpha' > $testroot/tree
/alpha
462 ln -s alpha
$testroot/tree
/alpha.link
464 got import
-m 'init' -r $testroot/repo
$testroot/tree \
467 if [ $ret -ne 0 ]; then
468 test_done
"$testroot" "$ret"
472 local head_commit
=`git_show_head $testroot/repo`
473 echo "A $testroot/tree/alpha" > $testroot/stdout.expected
474 echo "A $testroot/tree/alpha.link" >> $testroot/stdout.expected
475 echo "Created branch refs/heads/main with commit $head_commit" \
476 >> $testroot/stdout.expected
478 cmp -s $testroot/stdout.expected
$testroot/stdout
480 if [ $ret -ne 0 ]; then
481 diff -u $testroot/stdout.expected
$testroot/stdout
482 test_done
"$testroot" "$ret"
486 id_alpha
=`get_blob_id $testroot/repo "" alpha`
487 id_alpha_link
=$
(got tree
-r $testroot/repo
-i |
grep 'alpha.link@ -> alpha$' | cut
-d' ' -f 1)
488 tree_id
=`(cd $testroot/repo && got cat $head_commit | \
489 grep ^tree | cut -d ' ' -f 2)`
491 got tree
-i -r $testroot/repo
-c $head_commit > $testroot/stdout
493 echo "$id_alpha alpha" > $testroot/stdout.expected
494 echo "$id_alpha_link alpha.link@ -> alpha" >> $testroot/stdout.expected
496 cmp -s $testroot/stdout.expected
$testroot/stdout
498 if [ $ret -ne 0 ]; then
499 diff -u $testroot/stdout.expected
$testroot/stdout
501 test_done
"$testroot" "$ret"
505 run_test test_import_basic
506 run_test test_import_specified_head
507 run_test test_import_detached_head
508 run_test test_import_requires_new_branch
509 run_test test_import_ignores
510 run_test test_import_empty_dir
511 run_test test_import_symlink