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 testname=import_basic
21 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
23 gotadmin init $testroot/repo
26 make_test_tree $testroot/tree
28 got import -m 'init' -r $testroot/repo $testroot/tree \
31 if [ $ret -ne 0 ]; then
32 test_done "$testroot" "$ret"
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
46 if [ $ret -ne 0 ]; then
47 diff -u $testroot/stdout.expected $testroot/stdout
48 test_done "$testroot" "$ret"
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
99 if [ $ret -ne 0 ]; then
100 diff -u $testroot/stdout.expected $testroot/stdout
101 test_done "$testroot" "$ret"
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
115 if [ $ret -ne 0 ]; then
116 test_done "$testroot" "$ret"
120 cmp -s $testroot/stdout.expected $testroot/stdout
122 if [ $ret -ne 0 ]; then
123 diff -u $testroot/stdout.expected $testroot/stdout
124 test_done "$testroot" "$ret"
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
137 if [ $ret -ne 0 ]; then
138 diff -u $testroot/content.expected $testroot/content
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"`
148 gotadmin init -b $headref $testroot/repo
151 make_test_tree $testroot/tree
153 got import -m init -r $testroot/repo $testroot/tree > $testroot/stdout
155 if [ $ret -ne 0 ]; then
156 test_done "$testroot" "$ret"
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
170 if [ $ret -ne 0 ]; then
172 diff -u $testroot/stdout.expected $testroot/stdout
173 test_done "$testroot" "$ret"
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
224 if [ $ret -ne 0 ]; then
226 diff -u $testroot/stdout.expected $testroot/stdout
227 test_done "$testroot" "$ret"
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
242 if [ $ret -ne 0 ]; then
244 diff -u $testroot/stdout.expected $testroot/stdout
245 test_done "$testroot" "$ret"
249 cmp -s $testroot/stdout.expected $testroot/stdout
251 if [ $ret -ne 0 ]; then
253 diff -u $testroot/stdout.expected $testroot/stdout
254 test_done "$testroot" "$ret"
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
267 if [ $ret -ne 0 ]; then
269 diff -u $testroot/content.expected $testroot/content
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)
280 if [ $ret -ne 0 ]; then
281 test_done "$testroot" "$ret"
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)
290 if [ $ret -ne 0 ]; then
291 test_done "$testroot" "$ret"
295 echo "HEAD is now at $head_commit adding the test tree" >> \
296 $testroot/stderr.expected
298 cmp -s $testroot/stderr.expected $testroot/stderr
300 if [ $ret -ne 0 ]; then
302 diff -u $testroot/stderr.expected $testroot/stderr
303 test_done "$testroot" "$ret"
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
313 if [ $ret -ne 0 ]; then
314 test_done "$testroot" "$ret"
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
329 if [ $ret -ne 0 ]; then
331 diff -u $testroot/stdout.expected $testroot/stdout
333 test_done "$testroot" "$ret"
336 test_import_requires_new_branch() {
337 local testroot=`test_init import_requires_new_branch`
340 make_test_tree $testroot/tree
342 got import -b master -m 'init' -r $testroot/repo $testroot/tree \
343 > $testroot/stdout 2> $testroot/stderr
345 if [ $ret -eq 0 ]; then
346 echo "import command should have failed but did not"
347 test_done "$testroot" "1"
351 echo "got: import target branch already exists" \
352 > $testroot/stderr.expected
353 cmp -s $testroot/stderr.expected $testroot/stderr
355 if [ $ret -ne 0 ]; then
356 diff -u $testroot/stderr.expected $testroot/stderr
357 test_done "$testroot" "$ret"
361 got import -b newbranch -m 'init' -r $testroot/repo $testroot/tree \
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
375 make_test_tree $testroot/tree
377 got import -I alpha -I '*lta*' -I '*silon' \
378 -m 'init' -r $testroot/repo $testroot/tree > $testroot/stdout
380 if [ $ret -ne 0 ]; then
381 test_done "$testroot" "$ret"
385 local head_commit=`git_show_head $testroot/repo`
386 echo "A $testroot/tree/beta" >> $testroot/stdout.expected
387 echo "Created branch refs/heads/main with commit $head_commit" \
388 >> $testroot/stdout.expected
390 cmp -s $testroot/stdout.expected $testroot/stdout
392 if [ $ret -ne 0 ]; then
393 diff -u $testroot/stdout.expected $testroot/stdout
395 test_done "$testroot" "$ret"
398 test_import_empty_dir() {
399 local testname=import_empty_dir
400 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
402 gotadmin init $testroot/repo
405 mkdir -p $testroot/tree/empty $testroot/tree/notempty
406 echo "alpha" > $testroot/tree/notempty/alpha
408 got import -m 'init' -r $testroot/repo $testroot/tree > $testroot/stdout
410 if [ $ret -ne 0 ]; then
411 test_done "$testroot" "$ret"
415 local head_commit=`git_show_head $testroot/repo`
416 echo "A $testroot/tree/notempty/alpha" >> $testroot/stdout.expected
417 echo "Created branch refs/heads/main with commit $head_commit" \
418 >> $testroot/stdout.expected
420 cmp -s $testroot/stdout.expected $testroot/stdout
422 if [ $ret -ne 0 ]; then
423 diff -u $testroot/stdout.expected $testroot/stdout
424 test_done "$testroot" "$ret"
428 # Verify that Got did not import the empty directory
429 echo "notempty/" > $testroot/stdout.expected
430 echo "notempty/alpha" >> $testroot/stdout.expected
432 got tree -r $testroot/repo -R > $testroot/stdout
433 cmp -s $testroot/stdout.expected $testroot/stdout
435 if [ $ret -ne 0 ]; then
436 diff -u $testroot/stdout.expected $testroot/stdout
438 test_done "$testroot" "$ret"
441 test_import_symlink() {
442 local testname=import_symlink
443 local testroot=`mktemp -d "$GOT_TEST_ROOT/got-test-$testname-XXXXXXXX"`
445 gotadmin init $testroot/repo
448 echo 'this is file alpha' > $testroot/tree/alpha
449 ln -s alpha $testroot/tree/alpha.link
451 got import -m 'init' -r $testroot/repo $testroot/tree \
454 if [ $ret -ne 0 ]; then
455 test_done "$testroot" "$ret"
459 local head_commit=`git_show_head $testroot/repo`
460 echo "A $testroot/tree/alpha" > $testroot/stdout.expected
461 echo "A $testroot/tree/alpha.link" >> $testroot/stdout.expected
462 echo "Created branch refs/heads/main with commit $head_commit" \
463 >> $testroot/stdout.expected
465 cmp -s $testroot/stdout.expected $testroot/stdout
467 if [ $ret -ne 0 ]; then
468 diff -u $testroot/stdout.expected $testroot/stdout
469 test_done "$testroot" "$ret"
473 id_alpha=`get_blob_id $testroot/repo "" alpha`
474 id_alpha_link=$(got tree -r $testroot/repo -i | grep 'alpha.link@ -> alpha$' | cut -d' ' -f 1)
475 tree_id=`(cd $testroot/repo && got cat $head_commit | \
476 grep ^tree | cut -d ' ' -f 2)`
478 got tree -i -r $testroot/repo -c $head_commit > $testroot/stdout
480 echo "$id_alpha alpha" > $testroot/stdout.expected
481 echo "$id_alpha_link alpha.link@ -> alpha" >> $testroot/stdout.expected
483 cmp -s $testroot/stdout.expected $testroot/stdout
485 if [ $ret -ne 0 ]; then
486 diff -u $testroot/stdout.expected $testroot/stdout
488 test_done "$testroot" "$ret"
492 run_test test_import_basic
493 run_test test_import_specified_head
494 run_test test_import_detached_head
495 run_test test_import_requires_new_branch
496 run_test test_import_ignores
497 run_test test_import_empty_dir
498 run_test test_import_symlink