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_import_basic() {
20 local testname=import_basic
21 local testroot=`mktemp -d \
22 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
24 gotadmin init $testroot/repo
26 mkdir $testroot/tree
27 make_test_tree $testroot/tree
29 got import -m 'init' -r $testroot/repo $testroot/tree \
30 > $testroot/stdout
31 ret=$?
32 if [ $ret -ne 0 ]; then
33 test_done "$testroot" "$ret"
34 return 1
35 fi
37 local head_commit=`git_show_head $testroot/repo`
38 echo "A $testroot/tree/gamma/delta" > $testroot/stdout.expected
39 echo "A $testroot/tree/epsilon/zeta" >> $testroot/stdout.expected
40 echo "A $testroot/tree/alpha" >> $testroot/stdout.expected
41 echo "A $testroot/tree/beta" >> $testroot/stdout.expected
42 echo "Created branch refs/heads/main with commit $head_commit" \
43 >> $testroot/stdout.expected
45 cmp -s $testroot/stdout.expected $testroot/stdout
46 ret=$?
47 if [ $ret -ne 0 ]; then
48 diff -u $testroot/stdout.expected $testroot/stdout
49 test_done "$testroot" "$ret"
50 return 1
51 fi
53 (cd $testroot/repo && got log -p | grep -v ^date: > $testroot/stdout)
55 id_alpha=`get_blob_id $testroot/repo "" alpha`
56 id_beta=`get_blob_id $testroot/repo "" beta`
57 id_zeta=`get_blob_id $testroot/repo epsilon zeta`
58 id_delta=`get_blob_id $testroot/repo gamma delta`
59 tree_id=`(cd $testroot/repo && got cat $head_commit | \
60 grep ^tree | cut -d ' ' -f 2)`
62 echo "-----------------------------------------------" \
63 > $testroot/stdout.expected
64 echo "commit $head_commit (main)" >> $testroot/stdout.expected
65 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
66 echo " " >> $testroot/stdout.expected
67 echo " init" >> $testroot/stdout.expected
68 echo " " >> $testroot/stdout.expected
69 echo "diff /dev/null $head_commit" >> $testroot/stdout.expected
70 echo "commit - /dev/null" >> $testroot/stdout.expected
71 echo "commit + $head_commit" >> $testroot/stdout.expected
72 echo "blob - /dev/null" >> $testroot/stdout.expected
73 echo "blob + $id_alpha (mode 644)" >> $testroot/stdout.expected
74 echo "--- /dev/null" >> $testroot/stdout.expected
75 echo "+++ alpha" >> $testroot/stdout.expected
76 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
77 echo "+alpha" >> $testroot/stdout.expected
78 echo "blob - /dev/null" >> $testroot/stdout.expected
79 echo "blob + $id_beta (mode 644)" >> $testroot/stdout.expected
80 echo "--- /dev/null" >> $testroot/stdout.expected
81 echo "+++ beta" >> $testroot/stdout.expected
82 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
83 echo "+beta" >> $testroot/stdout.expected
84 echo "blob - /dev/null" >> $testroot/stdout.expected
85 echo "blob + $id_zeta (mode 644)" >> $testroot/stdout.expected
86 echo "--- /dev/null" >> $testroot/stdout.expected
87 echo "+++ epsilon/zeta" >> $testroot/stdout.expected
88 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
89 echo "+zeta" >> $testroot/stdout.expected
90 echo "blob - /dev/null" >> $testroot/stdout.expected
91 echo "blob + $id_delta (mode 644)" >> $testroot/stdout.expected
92 echo "--- /dev/null" >> $testroot/stdout.expected
93 echo "+++ gamma/delta" >> $testroot/stdout.expected
94 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
95 echo "+delta" >> $testroot/stdout.expected
96 echo "" >> $testroot/stdout.expected
98 cmp -s $testroot/stdout.expected $testroot/stdout
99 ret=$?
100 if [ $ret -ne 0 ]; then
101 diff -u $testroot/stdout.expected $testroot/stdout
102 test_done "$testroot" "$ret"
103 return 1
104 fi
106 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
107 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
108 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
109 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
110 echo "Checked out refs/heads/main: $head_commit" \
111 >> $testroot/stdout.expected
112 echo "Now shut up and hack" >> $testroot/stdout.expected
114 got checkout $testroot/repo $testroot/wt > $testroot/stdout
115 ret=$?
116 if [ $ret -ne 0 ]; then
117 test_done "$testroot" "$ret"
118 return 1
119 fi
121 cmp -s $testroot/stdout.expected $testroot/stdout
122 ret=$?
123 if [ $ret -ne 0 ]; then
124 diff -u $testroot/stdout.expected $testroot/stdout
125 test_done "$testroot" "$ret"
126 return 1
127 fi
129 echo "alpha" > $testroot/content.expected
130 echo "beta" >> $testroot/content.expected
131 echo "zeta" >> $testroot/content.expected
132 echo "delta" >> $testroot/content.expected
133 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
134 $testroot/wt/gamma/delta > $testroot/content
136 cmp -s $testroot/content.expected $testroot/content
137 ret=$?
138 if [ $ret -ne 0 ]; then
139 diff -u $testroot/content.expected $testroot/content
140 fi
141 test_done "$testroot" "$ret"
144 test_import_specified_head() {
145 local testname=import_specified_head
146 local testroot=`mktemp -d \
147 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
148 local headref=trunk
150 gotadmin init -b $headref $testroot/repo
152 mkdir $testroot/tree
153 make_test_tree $testroot/tree
155 got import -m init -r $testroot/repo $testroot/tree > $testroot/stdout
156 ret=$?
157 if [ $ret -ne 0 ]; then
158 test_done "$testroot" "$ret"
159 return 1
160 fi
162 local head_commit=`git_show_head $testroot/repo`
163 echo "A $testroot/tree/gamma/delta" > $testroot/stdout.expected
164 echo "A $testroot/tree/epsilon/zeta" >> $testroot/stdout.expected
165 echo "A $testroot/tree/alpha" >> $testroot/stdout.expected
166 echo "A $testroot/tree/beta" >> $testroot/stdout.expected
167 echo "Created branch refs/heads/$headref with commit $head_commit" \
168 >> $testroot/stdout.expected
170 cmp -s $testroot/stdout.expected $testroot/stdout
171 ret=$?
172 if [ $ret -ne 0 ]; then
173 echo "fail"
174 diff -u $testroot/stdout.expected $testroot/stdout
175 test_done "$testroot" "$ret"
176 return 1
177 fi
179 (cd $testroot/repo && got log -p | grep -v ^date: > $testroot/stdout)
181 id_alpha=`get_blob_id $testroot/repo "" alpha`
182 id_beta=`get_blob_id $testroot/repo "" beta`
183 id_zeta=`get_blob_id $testroot/repo epsilon zeta`
184 id_delta=`get_blob_id $testroot/repo gamma delta`
185 tree_id=`(cd $testroot/repo && got cat $head_commit | \
186 grep ^tree | cut -d ' ' -f 2)`
188 echo "-----------------------------------------------" \
189 > $testroot/stdout.expected
190 echo "commit $head_commit ($headref)" >> $testroot/stdout.expected
191 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
192 echo " " >> $testroot/stdout.expected
193 echo " init" >> $testroot/stdout.expected
194 echo " " >> $testroot/stdout.expected
195 echo "diff /dev/null $head_commit" >> $testroot/stdout.expected
196 echo "commit - /dev/null" >> $testroot/stdout.expected
197 echo "commit + $head_commit" >> $testroot/stdout.expected
198 echo "blob - /dev/null" >> $testroot/stdout.expected
199 echo "blob + $id_alpha (mode 644)" >> $testroot/stdout.expected
200 echo "--- /dev/null" >> $testroot/stdout.expected
201 echo "+++ alpha" >> $testroot/stdout.expected
202 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
203 echo "+alpha" >> $testroot/stdout.expected
204 echo "blob - /dev/null" >> $testroot/stdout.expected
205 echo "blob + $id_beta (mode 644)" >> $testroot/stdout.expected
206 echo "--- /dev/null" >> $testroot/stdout.expected
207 echo "+++ beta" >> $testroot/stdout.expected
208 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
209 echo "+beta" >> $testroot/stdout.expected
210 echo "blob - /dev/null" >> $testroot/stdout.expected
211 echo "blob + $id_zeta (mode 644)" >> $testroot/stdout.expected
212 echo "--- /dev/null" >> $testroot/stdout.expected
213 echo "+++ epsilon/zeta" >> $testroot/stdout.expected
214 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
215 echo "+zeta" >> $testroot/stdout.expected
216 echo "blob - /dev/null" >> $testroot/stdout.expected
217 echo "blob + $id_delta (mode 644)" >> $testroot/stdout.expected
218 echo "--- /dev/null" >> $testroot/stdout.expected
219 echo "+++ gamma/delta" >> $testroot/stdout.expected
220 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
221 echo "+delta" >> $testroot/stdout.expected
222 echo "" >> $testroot/stdout.expected
224 cmp -s $testroot/stdout.expected $testroot/stdout
225 ret=$?
226 if [ $ret -ne 0 ]; then
227 echo "fail"
228 diff -u $testroot/stdout.expected $testroot/stdout
229 test_done "$testroot" "$ret"
230 return 1
231 fi
233 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
234 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
235 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
236 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
237 echo "Checked out refs/heads/$headref: $head_commit" \
238 >> $testroot/stdout.expected
239 echo "Now shut up and hack" >> $testroot/stdout.expected
241 got checkout $testroot/repo $testroot/wt > $testroot/stdout
242 cmp -s $testroot/stdout.expected $testroot/stdout
243 ret=$?
244 if [ $ret -ne 0 ]; then
245 echo "fail"
246 diff -u $testroot/stdout.expected $testroot/stdout
247 test_done "$testroot" "$ret"
248 return 1
249 fi
251 cmp -s $testroot/stdout.expected $testroot/stdout
252 ret=$?
253 if [ $ret -ne 0 ]; then
254 echo "fail"
255 diff -u $testroot/stdout.expected $testroot/stdout
256 test_done "$testroot" "$ret"
257 return 1
258 fi
260 echo "alpha" > $testroot/content.expected
261 echo "beta" >> $testroot/content.expected
262 echo "zeta" >> $testroot/content.expected
263 echo "delta" >> $testroot/content.expected
264 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
265 $testroot/wt/gamma/delta > $testroot/content
267 cmp -s $testroot/content.expected $testroot/content
268 ret=$?
269 if [ $ret -ne 0 ]; then
270 echo "fail"
271 diff -u $testroot/content.expected $testroot/content
272 fi
273 test_done "$testroot" "$ret"
276 test_import_detached_head() {
277 local testroot=`test_init import_detached_head`
279 # mute verbose 'detached HEAD' warning
280 (cd $testroot/repo && git config --local advice.detachedHead false)
281 ret=$?
282 if [ $ret -ne 0 ]; then
283 test_done "$testroot" "$ret"
284 return 1
285 fi
287 # enter detached HEAD state
288 local head_commit=`git_show_head $testroot/repo | cut -c1-7`
289 (cd $testroot/repo && \
290 git checkout $head_commit > $testroot/stdout 2> $testroot/stderr)
291 ret=$?
292 if [ $ret -ne 0 ]; then
293 test_done "$testroot" "$ret"
294 return 1
295 fi
297 echo "HEAD is now at $head_commit adding the test tree" >> \
298 $testroot/stderr.expected
300 cmp -s $testroot/stderr.expected $testroot/stderr
301 ret=$?
302 if [ $ret -ne 0 ]; then
303 echo "fail"
304 diff -u $testroot/stderr.expected $testroot/stderr
305 test_done "$testroot" "$ret"
306 return 1
307 fi
309 mkdir $testroot/import
310 make_test_tree $testroot/import
312 # detached HEAD (i.e., not symbolic) so import should fallback to "main"
313 got import -r $testroot/repo -m init $testroot/import > $testroot/stdout
314 ret=$?
315 if [ $ret -ne 0 ]; then
316 test_done "$testroot" "$ret"
317 return 1
318 fi
320 local main_commit=`(cd $testroot/repo && \
321 git show-ref main | cut -d ' ' -f 1)`
322 echo "A $testroot/import/gamma/delta" > $testroot/stdout.expected
323 echo "A $testroot/import/epsilon/zeta" >> $testroot/stdout.expected
324 echo "A $testroot/import/alpha" >> $testroot/stdout.expected
325 echo "A $testroot/import/beta" >> $testroot/stdout.expected
326 echo "Created branch refs/heads/main with commit $main_commit" \
327 >> $testroot/stdout.expected
329 cmp -s $testroot/stdout.expected $testroot/stdout
330 ret=$?
331 if [ $ret -ne 0 ]; then
332 echo "fail"
333 diff -u $testroot/stdout.expected $testroot/stdout
334 fi
335 test_done "$testroot" "$ret"
338 test_import_requires_new_branch() {
339 local testroot=`test_init import_requires_new_branch`
341 mkdir $testroot/tree
342 make_test_tree $testroot/tree
344 got import -b master -m 'init' -r $testroot/repo $testroot/tree \
345 > $testroot/stdout 2> $testroot/stderr
346 ret=$?
347 if [ $ret -eq 0 ]; then
348 echo "import command should have failed but did not"
349 test_done "$testroot" "1"
350 return 1
351 fi
353 echo "got: import target branch already exists" \
354 > $testroot/stderr.expected
355 cmp -s $testroot/stderr.expected $testroot/stderr
356 ret=$?
357 if [ $ret -ne 0 ]; then
358 diff -u $testroot/stderr.expected $testroot/stderr
359 test_done "$testroot" "$ret"
360 return 1
361 fi
363 got import -b newbranch -m 'init' -r $testroot/repo $testroot/tree \
364 > $testroot/stdout
365 ret=$?
366 test_done "$testroot" "$ret"
370 test_import_ignores() {
371 local testname=import_ignores
372 local testroot=`mktemp -d \
373 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
375 gotadmin init $testroot/repo
377 mkdir $testroot/tree
378 make_test_tree $testroot/tree
380 touch $testroot/tree/upsilon
381 mkdir $testroot/tree/ysilon
382 got import -I alpha -I 'beta/' -I '*lta*' -I '*silon/' \
383 -m 'init' -r $testroot/repo $testroot/tree > $testroot/stdout
384 ret=$?
385 if [ $ret -ne 0 ]; then
386 test_done "$testroot" "$ret"
387 return 1
388 fi
390 local head_commit=`git_show_head $testroot/repo`
391 echo "A $testroot/tree/beta" >> $testroot/stdout.expected
392 echo "A $testroot/tree/upsilon" >> $testroot/stdout.expected
393 echo "Created branch refs/heads/main with commit $head_commit" \
394 >> $testroot/stdout.expected
396 cmp -s $testroot/stdout.expected $testroot/stdout
397 ret=$?
398 if [ $ret -ne 0 ]; then
399 diff -u $testroot/stdout.expected $testroot/stdout
400 fi
401 test_done "$testroot" "$ret"
404 test_import_empty_dir() {
405 local testname=import_empty_dir
406 local testroot=`mktemp -d \
407 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
409 gotadmin init $testroot/repo
411 mkdir $testroot/tree
412 mkdir -p $testroot/tree/empty $testroot/tree/notempty
413 echo "alpha" > $testroot/tree/notempty/alpha
415 got import -m 'init' -r $testroot/repo $testroot/tree > $testroot/stdout
416 ret=$?
417 if [ $ret -ne 0 ]; then
418 test_done "$testroot" "$ret"
419 return 1
420 fi
422 local head_commit=`git_show_head $testroot/repo`
423 echo "A $testroot/tree/notempty/alpha" >> $testroot/stdout.expected
424 echo "Created branch refs/heads/main with commit $head_commit" \
425 >> $testroot/stdout.expected
427 cmp -s $testroot/stdout.expected $testroot/stdout
428 ret=$?
429 if [ $ret -ne 0 ]; then
430 diff -u $testroot/stdout.expected $testroot/stdout
431 test_done "$testroot" "$ret"
432 return 1
433 fi
435 # Verify that Got did not import the empty directory
436 echo "notempty/" > $testroot/stdout.expected
437 echo "notempty/alpha" >> $testroot/stdout.expected
439 got tree -r $testroot/repo -R > $testroot/stdout
440 cmp -s $testroot/stdout.expected $testroot/stdout
441 ret=$?
442 if [ $ret -ne 0 ]; then
443 diff -u $testroot/stdout.expected $testroot/stdout
444 fi
445 test_done "$testroot" "$ret"
448 test_import_symlink() {
449 local testname=import_symlink
450 local testroot=`mktemp -d \
451 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXXXX"`
453 gotadmin init $testroot/repo
455 mkdir $testroot/tree
456 echo 'this is file alpha' > $testroot/tree/alpha
457 ln -s alpha $testroot/tree/alpha.link
459 got import -m 'init' -r $testroot/repo $testroot/tree \
460 > $testroot/stdout
461 ret=$?
462 if [ $ret -ne 0 ]; then
463 test_done "$testroot" "$ret"
464 return 1
465 fi
467 local head_commit=`git_show_head $testroot/repo`
468 echo "A $testroot/tree/alpha" > $testroot/stdout.expected
469 echo "A $testroot/tree/alpha.link" >> $testroot/stdout.expected
470 echo "Created branch refs/heads/main with commit $head_commit" \
471 >> $testroot/stdout.expected
473 cmp -s $testroot/stdout.expected $testroot/stdout
474 ret=$?
475 if [ $ret -ne 0 ]; then
476 diff -u $testroot/stdout.expected $testroot/stdout
477 test_done "$testroot" "$ret"
478 return 1
479 fi
481 id_alpha=`get_blob_id $testroot/repo "" alpha`
482 id_alpha_link=$(got tree -r $testroot/repo -i | grep 'alpha.link@ -> alpha$' | cut -d' ' -f 1)
483 tree_id=`(cd $testroot/repo && got cat $head_commit | \
484 grep ^tree | cut -d ' ' -f 2)`
486 got tree -i -r $testroot/repo -c $head_commit > $testroot/stdout
488 echo "$id_alpha alpha" > $testroot/stdout.expected
489 echo "$id_alpha_link alpha.link@ -> alpha" >> $testroot/stdout.expected
491 cmp -s $testroot/stdout.expected $testroot/stdout
492 ret=$?
493 if [ $ret -ne 0 ]; then
494 diff -u $testroot/stdout.expected $testroot/stdout
495 fi
496 test_done "$testroot" "$ret"
499 test_parseargs "$@"
500 run_test test_import_basic
501 run_test test_import_specified_head
502 run_test test_import_detached_head
503 run_test test_import_requires_new_branch
504 run_test test_import_ignores
505 run_test test_import_empty_dir
506 run_test test_import_symlink