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 "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
23 gotadmin init $testroot/repo
25 mkdir $testroot/tree
26 make_test_tree $testroot/tree
28 got import -m 'init' -r $testroot/repo $testroot/tree \
29 > $testroot/stdout
30 ret=$?
31 if [ $ret -ne 0 ]; then
32 test_done "$testroot" "$ret"
33 return 1
34 fi
36 local head_commit=`git_show_head $testroot/repo`
37 echo "A $testroot/tree/gamma/delta" > $testroot/stdout.expected
38 echo "A $testroot/tree/epsilon/zeta" >> $testroot/stdout.expected
39 echo "A $testroot/tree/alpha" >> $testroot/stdout.expected
40 echo "A $testroot/tree/beta" >> $testroot/stdout.expected
41 echo "Created branch refs/heads/main with commit $head_commit" \
42 >> $testroot/stdout.expected
44 cmp -s $testroot/stdout.expected $testroot/stdout
45 ret=$?
46 if [ $ret -ne 0 ]; then
47 diff -u $testroot/stdout.expected $testroot/stdout
48 test_done "$testroot" "$ret"
49 return 1
50 fi
52 (cd $testroot/repo && got log -p | grep -v ^date: > $testroot/stdout)
54 id_alpha=`get_blob_id $testroot/repo "" alpha`
55 id_beta=`get_blob_id $testroot/repo "" beta`
56 id_zeta=`get_blob_id $testroot/repo epsilon zeta`
57 id_delta=`get_blob_id $testroot/repo gamma delta`
58 tree_id=`(cd $testroot/repo && got cat $head_commit | \
59 grep ^tree | cut -d ' ' -f 2)`
61 echo "-----------------------------------------------" \
62 > $testroot/stdout.expected
63 echo "commit $head_commit (main)" >> $testroot/stdout.expected
64 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
65 echo " " >> $testroot/stdout.expected
66 echo " init" >> $testroot/stdout.expected
67 echo " " >> $testroot/stdout.expected
68 echo "diff /dev/null $head_commit" >> $testroot/stdout.expected
69 echo "commit - /dev/null" >> $testroot/stdout.expected
70 echo "commit + $head_commit" >> $testroot/stdout.expected
71 echo "blob - /dev/null" >> $testroot/stdout.expected
72 echo "blob + $id_alpha (mode 644)" >> $testroot/stdout.expected
73 echo "--- /dev/null" >> $testroot/stdout.expected
74 echo "+++ alpha" >> $testroot/stdout.expected
75 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
76 echo "+alpha" >> $testroot/stdout.expected
77 echo "blob - /dev/null" >> $testroot/stdout.expected
78 echo "blob + $id_beta (mode 644)" >> $testroot/stdout.expected
79 echo "--- /dev/null" >> $testroot/stdout.expected
80 echo "+++ beta" >> $testroot/stdout.expected
81 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
82 echo "+beta" >> $testroot/stdout.expected
83 echo "blob - /dev/null" >> $testroot/stdout.expected
84 echo "blob + $id_zeta (mode 644)" >> $testroot/stdout.expected
85 echo "--- /dev/null" >> $testroot/stdout.expected
86 echo "+++ epsilon/zeta" >> $testroot/stdout.expected
87 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
88 echo "+zeta" >> $testroot/stdout.expected
89 echo "blob - /dev/null" >> $testroot/stdout.expected
90 echo "blob + $id_delta (mode 644)" >> $testroot/stdout.expected
91 echo "--- /dev/null" >> $testroot/stdout.expected
92 echo "+++ gamma/delta" >> $testroot/stdout.expected
93 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
94 echo "+delta" >> $testroot/stdout.expected
95 echo "" >> $testroot/stdout.expected
97 cmp -s $testroot/stdout.expected $testroot/stdout
98 ret=$?
99 if [ $ret -ne 0 ]; then
100 diff -u $testroot/stdout.expected $testroot/stdout
101 test_done "$testroot" "$ret"
102 return 1
103 fi
105 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
106 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
107 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
108 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
109 echo "Checked out refs/heads/main: $head_commit" \
110 >> $testroot/stdout.expected
111 echo "Now shut up and hack" >> $testroot/stdout.expected
113 got checkout $testroot/repo $testroot/wt > $testroot/stdout
114 ret=$?
115 if [ $ret -ne 0 ]; then
116 test_done "$testroot" "$ret"
117 return 1
118 fi
120 cmp -s $testroot/stdout.expected $testroot/stdout
121 ret=$?
122 if [ $ret -ne 0 ]; then
123 diff -u $testroot/stdout.expected $testroot/stdout
124 test_done "$testroot" "$ret"
125 return 1
126 fi
128 echo "alpha" > $testroot/content.expected
129 echo "beta" >> $testroot/content.expected
130 echo "zeta" >> $testroot/content.expected
131 echo "delta" >> $testroot/content.expected
132 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
133 $testroot/wt/gamma/delta > $testroot/content
135 cmp -s $testroot/content.expected $testroot/content
136 ret=$?
137 if [ $ret -ne 0 ]; then
138 diff -u $testroot/content.expected $testroot/content
139 fi
140 test_done "$testroot" "$ret"
143 test_import_specified_head() {
144 local testname=import_specified_head
145 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
146 local headref=trunk
148 gotadmin init -b $headref $testroot/repo
150 mkdir $testroot/tree
151 make_test_tree $testroot/tree
153 got import -m init -r $testroot/repo $testroot/tree > $testroot/stdout
154 ret=$?
155 if [ $ret -ne 0 ]; then
156 test_done "$testroot" "$ret"
157 return 1
158 fi
160 local head_commit=`git_show_head $testroot/repo`
161 echo "A $testroot/tree/gamma/delta" > $testroot/stdout.expected
162 echo "A $testroot/tree/epsilon/zeta" >> $testroot/stdout.expected
163 echo "A $testroot/tree/alpha" >> $testroot/stdout.expected
164 echo "A $testroot/tree/beta" >> $testroot/stdout.expected
165 echo "Created branch refs/heads/$headref with commit $head_commit" \
166 >> $testroot/stdout.expected
168 cmp -s $testroot/stdout.expected $testroot/stdout
169 ret=$?
170 if [ $ret -ne 0 ]; then
171 echo "fail"
172 diff -u $testroot/stdout.expected $testroot/stdout
173 test_done "$testroot" "$ret"
174 return 1
175 fi
177 (cd $testroot/repo && got log -p | grep -v ^date: > $testroot/stdout)
179 id_alpha=`get_blob_id $testroot/repo "" alpha`
180 id_beta=`get_blob_id $testroot/repo "" beta`
181 id_zeta=`get_blob_id $testroot/repo epsilon zeta`
182 id_delta=`get_blob_id $testroot/repo gamma delta`
183 tree_id=`(cd $testroot/repo && got cat $head_commit | \
184 grep ^tree | cut -d ' ' -f 2)`
186 echo "-----------------------------------------------" \
187 > $testroot/stdout.expected
188 echo "commit $head_commit ($headref)" >> $testroot/stdout.expected
189 echo "from: $GOT_AUTHOR" >> $testroot/stdout.expected
190 echo " " >> $testroot/stdout.expected
191 echo " init" >> $testroot/stdout.expected
192 echo " " >> $testroot/stdout.expected
193 echo "diff /dev/null $head_commit" >> $testroot/stdout.expected
194 echo "commit - /dev/null" >> $testroot/stdout.expected
195 echo "commit + $head_commit" >> $testroot/stdout.expected
196 echo "blob - /dev/null" >> $testroot/stdout.expected
197 echo "blob + $id_alpha (mode 644)" >> $testroot/stdout.expected
198 echo "--- /dev/null" >> $testroot/stdout.expected
199 echo "+++ alpha" >> $testroot/stdout.expected
200 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
201 echo "+alpha" >> $testroot/stdout.expected
202 echo "blob - /dev/null" >> $testroot/stdout.expected
203 echo "blob + $id_beta (mode 644)" >> $testroot/stdout.expected
204 echo "--- /dev/null" >> $testroot/stdout.expected
205 echo "+++ beta" >> $testroot/stdout.expected
206 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
207 echo "+beta" >> $testroot/stdout.expected
208 echo "blob - /dev/null" >> $testroot/stdout.expected
209 echo "blob + $id_zeta (mode 644)" >> $testroot/stdout.expected
210 echo "--- /dev/null" >> $testroot/stdout.expected
211 echo "+++ epsilon/zeta" >> $testroot/stdout.expected
212 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
213 echo "+zeta" >> $testroot/stdout.expected
214 echo "blob - /dev/null" >> $testroot/stdout.expected
215 echo "blob + $id_delta (mode 644)" >> $testroot/stdout.expected
216 echo "--- /dev/null" >> $testroot/stdout.expected
217 echo "+++ gamma/delta" >> $testroot/stdout.expected
218 echo "@@ -0,0 +1 @@" >> $testroot/stdout.expected
219 echo "+delta" >> $testroot/stdout.expected
220 echo "" >> $testroot/stdout.expected
222 cmp -s $testroot/stdout.expected $testroot/stdout
223 ret=$?
224 if [ $ret -ne 0 ]; then
225 echo "fail"
226 diff -u $testroot/stdout.expected $testroot/stdout
227 test_done "$testroot" "$ret"
228 return 1
229 fi
231 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
232 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
233 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
234 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
235 echo "Checked out refs/heads/$headref: $head_commit" \
236 >> $testroot/stdout.expected
237 echo "Now shut up and hack" >> $testroot/stdout.expected
239 got checkout $testroot/repo $testroot/wt > $testroot/stdout
240 cmp -s $testroot/stdout.expected $testroot/stdout
241 ret=$?
242 if [ $ret -ne 0 ]; then
243 echo "fail"
244 diff -u $testroot/stdout.expected $testroot/stdout
245 test_done "$testroot" "$ret"
246 return 1
247 fi
249 cmp -s $testroot/stdout.expected $testroot/stdout
250 ret=$?
251 if [ $ret -ne 0 ]; then
252 echo "fail"
253 diff -u $testroot/stdout.expected $testroot/stdout
254 test_done "$testroot" "$ret"
255 return 1
256 fi
258 echo "alpha" > $testroot/content.expected
259 echo "beta" >> $testroot/content.expected
260 echo "zeta" >> $testroot/content.expected
261 echo "delta" >> $testroot/content.expected
262 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
263 $testroot/wt/gamma/delta > $testroot/content
265 cmp -s $testroot/content.expected $testroot/content
266 ret=$?
267 if [ $ret -ne 0 ]; then
268 echo "fail"
269 diff -u $testroot/content.expected $testroot/content
270 fi
271 test_done "$testroot" "$ret"
274 test_import_detached_head() {
275 local testroot=`test_init import_detached_head`
277 # mute verbose 'detached HEAD' warning
278 (cd $testroot/repo && git config --local advice.detachedHead false)
279 ret=$?
280 if [ $ret -ne 0 ]; then
281 test_done "$testroot" "$ret"
282 return 1
283 fi
285 # enter detached HEAD state
286 local head_commit=`git_show_head $testroot/repo | cut -c1-7`
287 (cd $testroot/repo && \
288 git checkout $head_commit > $testroot/stdout 2> $testroot/stderr)
289 ret=$?
290 if [ $ret -ne 0 ]; then
291 test_done "$testroot" "$ret"
292 return 1
293 fi
295 echo "HEAD is now at $head_commit adding the test tree" >> \
296 $testroot/stderr.expected
298 cmp -s $testroot/stderr.expected $testroot/stderr
299 ret=$?
300 if [ $ret -ne 0 ]; then
301 echo "fail"
302 diff -u $testroot/stderr.expected $testroot/stderr
303 test_done "$testroot" "$ret"
304 return 1
305 fi
307 mkdir $testroot/import
308 make_test_tree $testroot/import
310 # detached HEAD (i.e., not symbolic) so import should fallback to "main"
311 got import -r $testroot/repo -m init $testroot/import > $testroot/stdout
312 ret=$?
313 if [ $ret -ne 0 ]; then
314 test_done "$testroot" "$ret"
315 return 1
316 fi
318 local main_commit=`(cd $testroot/repo && \
319 git show-ref main | cut -d ' ' -f 1)`
320 echo "A $testroot/import/gamma/delta" > $testroot/stdout.expected
321 echo "A $testroot/import/epsilon/zeta" >> $testroot/stdout.expected
322 echo "A $testroot/import/alpha" >> $testroot/stdout.expected
323 echo "A $testroot/import/beta" >> $testroot/stdout.expected
324 echo "Created branch refs/heads/main with commit $main_commit" \
325 >> $testroot/stdout.expected
327 cmp -s $testroot/stdout.expected $testroot/stdout
328 ret=$?
329 if [ $ret -ne 0 ]; then
330 echo "fail"
331 diff -u $testroot/stdout.expected $testroot/stdout
332 fi
333 test_done "$testroot" "$ret"
336 test_import_requires_new_branch() {
337 local testroot=`test_init import_requires_new_branch`
339 mkdir $testroot/tree
340 make_test_tree $testroot/tree
342 got import -b master -m 'init' -r $testroot/repo $testroot/tree \
343 > $testroot/stdout 2> $testroot/stderr
344 ret=$?
345 if [ $ret -eq 0 ]; then
346 echo "import command should have failed but did not"
347 test_done "$testroot" "1"
348 return 1
349 fi
351 echo "got: import target branch already exists" \
352 > $testroot/stderr.expected
353 cmp -s $testroot/stderr.expected $testroot/stderr
354 ret=$?
355 if [ $ret -ne 0 ]; then
356 diff -u $testroot/stderr.expected $testroot/stderr
357 test_done "$testroot" "$ret"
358 return 1
359 fi
361 got import -b newbranch -m 'init' -r $testroot/repo $testroot/tree \
362 > $testroot/stdout
363 ret=$?
364 test_done "$testroot" "$ret"
368 test_import_ignores() {
369 local testname=import_ignores
370 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
372 gotadmin init $testroot/repo
374 mkdir $testroot/tree
375 make_test_tree $testroot/tree
377 touch $testroot/tree/upsilon
378 mkdir $testroot/tree/ysilon
379 got import -I alpha -I 'beta/' -I '*lta*' -I '*silon/' \
380 -m 'init' -r $testroot/repo $testroot/tree > $testroot/stdout
381 ret=$?
382 if [ $ret -ne 0 ]; then
383 test_done "$testroot" "$ret"
384 return 1
385 fi
387 local head_commit=`git_show_head $testroot/repo`
388 echo "A $testroot/tree/beta" >> $testroot/stdout.expected
389 echo "A $testroot/tree/upsilon" >> $testroot/stdout.expected
390 echo "Created branch refs/heads/main with commit $head_commit" \
391 >> $testroot/stdout.expected
393 cmp -s $testroot/stdout.expected $testroot/stdout
394 ret=$?
395 if [ $ret -ne 0 ]; then
396 diff -u $testroot/stdout.expected $testroot/stdout
397 fi
398 test_done "$testroot" "$ret"
401 test_import_empty_dir() {
402 local testname=import_empty_dir
403 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
405 gotadmin init $testroot/repo
407 mkdir $testroot/tree
408 mkdir -p $testroot/tree/empty $testroot/tree/notempty
409 echo "alpha" > $testroot/tree/notempty/alpha
411 got import -m 'init' -r $testroot/repo $testroot/tree > $testroot/stdout
412 ret=$?
413 if [ $ret -ne 0 ]; then
414 test_done "$testroot" "$ret"
415 return 1
416 fi
418 local head_commit=`git_show_head $testroot/repo`
419 echo "A $testroot/tree/notempty/alpha" >> $testroot/stdout.expected
420 echo "Created branch refs/heads/main with commit $head_commit" \
421 >> $testroot/stdout.expected
423 cmp -s $testroot/stdout.expected $testroot/stdout
424 ret=$?
425 if [ $ret -ne 0 ]; then
426 diff -u $testroot/stdout.expected $testroot/stdout
427 test_done "$testroot" "$ret"
428 return 1
429 fi
431 # Verify that Got did not import the empty directory
432 echo "notempty/" > $testroot/stdout.expected
433 echo "notempty/alpha" >> $testroot/stdout.expected
435 got tree -r $testroot/repo -R > $testroot/stdout
436 cmp -s $testroot/stdout.expected $testroot/stdout
437 ret=$?
438 if [ $ret -ne 0 ]; then
439 diff -u $testroot/stdout.expected $testroot/stdout
440 fi
441 test_done "$testroot" "$ret"
444 test_import_symlink() {
445 local testname=import_symlink
446 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
448 gotadmin init $testroot/repo
450 mkdir $testroot/tree
451 echo 'this is file alpha' > $testroot/tree/alpha
452 ln -s alpha $testroot/tree/alpha.link
454 got import -m 'init' -r $testroot/repo $testroot/tree \
455 > $testroot/stdout
456 ret=$?
457 if [ $ret -ne 0 ]; then
458 test_done "$testroot" "$ret"
459 return 1
460 fi
462 local head_commit=`git_show_head $testroot/repo`
463 echo "A $testroot/tree/alpha" > $testroot/stdout.expected
464 echo "A $testroot/tree/alpha.link" >> $testroot/stdout.expected
465 echo "Created branch refs/heads/main with commit $head_commit" \
466 >> $testroot/stdout.expected
468 cmp -s $testroot/stdout.expected $testroot/stdout
469 ret=$?
470 if [ $ret -ne 0 ]; then
471 diff -u $testroot/stdout.expected $testroot/stdout
472 test_done "$testroot" "$ret"
473 return 1
474 fi
476 id_alpha=`get_blob_id $testroot/repo "" alpha`
477 id_alpha_link=$(got tree -r $testroot/repo -i | grep 'alpha.link@ -> alpha$' | cut -d' ' -f 1)
478 tree_id=`(cd $testroot/repo && got cat $head_commit | \
479 grep ^tree | cut -d ' ' -f 2)`
481 got tree -i -r $testroot/repo -c $head_commit > $testroot/stdout
483 echo "$id_alpha alpha" > $testroot/stdout.expected
484 echo "$id_alpha_link alpha.link@ -> alpha" >> $testroot/stdout.expected
486 cmp -s $testroot/stdout.expected $testroot/stdout
487 ret=$?
488 if [ $ret -ne 0 ]; then
489 diff -u $testroot/stdout.expected $testroot/stdout
490 fi
491 test_done "$testroot" "$ret"
494 test_parseargs "$@"
495 run_test test_import_basic
496 run_test test_import_specified_head
497 run_test test_import_detached_head
498 run_test test_import_requires_new_branch
499 run_test test_import_ignores
500 run_test test_import_empty_dir
501 run_test test_import_symlink