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.
19 function test_checkout_basic {
20 local testroot=`test_init checkout_basic`
22 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
23 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
24 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
25 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
26 echo "Now shut up and hack" >> $testroot/stdout.expected
28 got checkout $testroot/repo $testroot/wt > $testroot/stdout
30 if [ "$ret" != "0" ]; then
31 test_done "$testroot" "$ret"
35 cmp -s $testroot/stdout.expected $testroot/stdout
37 if [ "$ret" != "0" ]; then
38 diff -u $testroot/stdout.expected $testroot/stdout
39 test_done "$testroot" "$ret"
43 echo "alpha" > $testroot/content.expected
44 echo "beta" >> $testroot/content.expected
45 echo "zeta" >> $testroot/content.expected
46 echo "delta" >> $testroot/content.expected
47 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
48 $testroot/wt/gamma/delta > $testroot/content
50 cmp -s $testroot/content.expected $testroot/content
52 if [ "$ret" != "0" ]; then
53 diff -u $testroot/content.expected $testroot/content
55 test_done "$testroot" "$ret"
58 function test_checkout_dir_exists {
59 local testroot=`test_init checkout_dir_exists`
61 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
62 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
63 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
64 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
65 echo "Now shut up and hack" >> $testroot/stdout.expected
69 got checkout $testroot/repo $testroot/wt > $testroot/stdout
71 if [ "$ret" != "0" ]; then
72 test_done "$testroot" "$ret"
76 cmp -s $testroot/stdout.expected $testroot/stdout
78 if [ "$ret" != "0" ]; then
79 diff -u $testroot/stdout.expected $testroot/stdout
80 test_done "$testroot" "$ret"
84 echo "alpha" > $testroot/content.expected
85 echo "beta" >> $testroot/content.expected
86 echo "zeta" >> $testroot/content.expected
87 echo "delta" >> $testroot/content.expected
88 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
89 $testroot/wt/gamma/delta > $testroot/content
91 cmp -s $testroot/content.expected $testroot/content
93 if [ "$ret" != "0" ]; then
94 diff -u $testroot/content.expected $testroot/content
96 test_done "$testroot" "$ret"
99 function test_checkout_dir_not_empty {
100 local testroot=`test_init checkout_dir_not_empty`
102 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
103 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
104 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
105 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
106 echo "Now shut up and hack" >> $testroot/stdout.expected
109 touch $testroot/wt/foo
111 got checkout $testroot/repo $testroot/wt > $testroot/stdout \
114 if [ "$ret" == "0" ]; then
115 echo "checkout succeeded unexpectedly" >&2
116 test_done "$testroot" "1"
120 echo "got: $testroot/wt: directory exists and is not empty" \
121 > $testroot/stderr.expected
122 cmp -s $testroot/stderr.expected $testroot/stderr
124 if [ "$ret" != "0" ]; then
125 diff -u $testroot/stderr.expected $testroot/stderr
126 test_done "$testroot" "$ret"
130 echo -n > $testroot/stdout.expected
131 cmp -s $testroot/stdout.expected $testroot/stdout
133 if [ "$ret" != "0" ]; then
134 diff -u $testroot/stdout.expected $testroot/stdout
136 test_done "$testroot" "$ret"
140 function test_checkout_sets_xbit {
141 local testroot=`test_init checkout_sets_xbit 1`
143 touch $testroot/repo/xfile
144 chmod +x $testroot/repo/xfile
145 (cd $testroot/repo && git add .)
146 git_commit $testroot/repo -m "adding executable file"
148 echo "A $testroot/wt/xfile" > $testroot/stdout.expected
149 echo "Now shut up and hack" >> $testroot/stdout.expected
151 got checkout $testroot/repo $testroot/wt > $testroot/stdout
153 if [ "$ret" != "0" ]; then
154 test_done "$testroot" "$ret"
158 cmp -s $testroot/stdout.expected $testroot/stdout
160 if [ "$ret" != "0" ]; then
161 diff -u $testroot/stdout.expected $testroot/stdout
162 test_done "$testroot" "$ret"
166 ls -l $testroot/wt/xfile | grep -q '^-rwx'
168 if [ "$ret" != "0" ]; then
169 echo "file is not executable" >&2
170 ls -l $testroot/wt/xfile >&2
172 test_done "$testroot" "$ret"
175 function test_checkout_commit_from_wrong_branch {
176 local testroot=`test_init checkout_commit_from_wrong_branch`
178 (cd $testroot/repo && git checkout -q -b newbranch)
179 echo "modified alpha on new branch" > $testroot/repo/alpha
180 git_commit $testroot/repo -m "modified alpha on new branch"
182 local head_rev=`git_show_head $testroot/repo`
183 got checkout -b master -c $head_rev $testroot/repo $testroot/wt \
184 > $testroot/stdout 2> $testroot/stderr
186 if [ "$ret" == "0" ]; then
187 test_done "$testroot" "1"
191 echo -n "" > $testroot/stdout.expected
192 cmp -s $testroot/stdout.expected $testroot/stdout
194 if [ "$ret" != "0" ]; then
195 diff -u $testroot/stdout.expected $testroot/stdout
196 test_done "$testroot" "$ret"
200 echo -n "got: target commit is not contained in branch 'master'; " \
201 > $testroot/stderr.expected
202 echo -n "the branch to use must be specified with -b; if necessary " \
203 >> $testroot/stderr.expected
204 echo -n "a new branch can be created for this commit with "\
205 >> $testroot/stderr.expected
206 echo "'got branch -c $head_rev BRANCH_NAME'" \
207 >> $testroot/stderr.expected
208 cmp -s $testroot/stderr.expected $testroot/stderr
210 if [ "$ret" != "0" ]; then
211 diff -u $testroot/stderr.expected $testroot/stderr
212 test_done "$testroot" "$ret"
216 test_done "$testroot" "$ret"
219 function test_checkout_tag {
220 local testroot=`test_init checkout_tag`
223 (cd $testroot/repo && git tag -a -m "test" $tag)
225 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
226 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
227 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
228 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
229 echo "Now shut up and hack" >> $testroot/stdout.expected
231 got checkout -c $tag $testroot/repo $testroot/wt > $testroot/stdout
233 if [ "$ret" != "0" ]; then
234 test_done "$testroot" "$ret"
238 cmp -s $testroot/stdout.expected $testroot/stdout
240 if [ "$ret" != "0" ]; then
241 diff -u $testroot/stdout.expected $testroot/stdout
242 test_done "$testroot" "$ret"
246 echo "alpha" > $testroot/content.expected
247 echo "beta" >> $testroot/content.expected
248 echo "zeta" >> $testroot/content.expected
249 echo "delta" >> $testroot/content.expected
250 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
251 $testroot/wt/gamma/delta > $testroot/content
253 cmp -s $testroot/content.expected $testroot/content
255 if [ "$ret" != "0" ]; then
256 diff -u $testroot/content.expected $testroot/content
258 test_done "$testroot" "$ret"
261 function test_checkout_ignores_submodules {
262 local testroot=`test_init checkout_ignores_submodules`
264 (cd $testroot && git clone -q repo repo2 >/dev/null)
265 (cd $testroot/repo && git submodule -q add ../repo2)
266 (cd $testroot/repo && git commit -q -m 'adding submodule')
268 echo "A $testroot/wt/.gitmodules" > $testroot/stdout.expected
269 echo "A $testroot/wt/alpha" >> $testroot/stdout.expected
270 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
271 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
272 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
273 echo "Now shut up and hack" >> $testroot/stdout.expected
275 got checkout $testroot/repo $testroot/wt > $testroot/stdout
277 if [ "$ret" != "0" ]; then
278 test_done "$testroot" "$ret"
282 cmp -s $testroot/stdout.expected $testroot/stdout
284 if [ "$ret" != "0" ]; then
285 diff -u $testroot/stdout.expected $testroot/stdout
286 test_done "$testroot" "$ret"
290 echo "alpha" > $testroot/content.expected
291 echo "beta" >> $testroot/content.expected
292 echo "zeta" >> $testroot/content.expected
293 echo "delta" >> $testroot/content.expected
294 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
295 $testroot/wt/gamma/delta > $testroot/content
297 cmp -s $testroot/content.expected $testroot/content
299 if [ "$ret" != "0" ]; then
300 diff -u $testroot/content.expected $testroot/content
302 test_done "$testroot" "$ret"
305 function test_checkout_read_only {
306 local testroot=`test_init checkout_read_only`
308 # Make the repostiory read-only
309 chmod -R a-w $testroot/repo
311 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
312 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
313 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
314 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
315 echo "Now shut up and hack" >> $testroot/stdout.expected
317 got checkout $testroot/repo $testroot/wt \
318 > $testroot/stdout 2> $testroot/stderr
320 if [ "$ret" != "0" ]; then
321 test_done "$testroot" "$ret"
325 cmp -s $testroot/stdout.expected $testroot/stdout
327 if [ "$ret" != "0" ]; then
328 diff -u $testroot/stdout.expected $testroot/stdout
329 test_done "$testroot" "$ret"
333 echo -n "got: warning: could not create a reference " \
334 > $testroot/stderr.expected
335 echo -n "to the work tree's base commit; the commit could " \
336 >> $testroot/stderr.expected
337 echo -n "be garbage-collected by Git; making the repository " \
338 >> $testroot/stderr.expected
339 echo "writable and running 'got update' will prevent this" \
340 >> $testroot/stderr.expected
341 cmp -s $testroot/stderr.expected $testroot/stderr
343 if [ "$ret" != "0" ]; then
344 diff -u $testroot/stderr.expected $testroot/stderr
345 test_done "$testroot" "$ret"
349 echo "alpha" > $testroot/content.expected
350 echo "beta" >> $testroot/content.expected
351 echo "zeta" >> $testroot/content.expected
352 echo "delta" >> $testroot/content.expected
353 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
354 $testroot/wt/gamma/delta > $testroot/content
356 cmp -s $testroot/content.expected $testroot/content
358 if [ "$ret" != "0" ]; then
359 diff -u $testroot/content.expected $testroot/content
361 chmod -R u+w $testroot/repo # make repo cleanup work
362 test_done "$testroot" "$ret"
365 function test_checkout_into_nonempty_dir {
366 local testroot=`test_init checkout_into_nonempty_dir`
368 mkdir -p $testroot/wt
369 make_test_tree $testroot/wt
371 got checkout $testroot/repo $testroot/wt > $testroot/stdout \
374 if [ "$ret" == "0" ]; then
375 echo "checkout succeeded unexpectedly" >&2
376 test_done "$testroot" "1"
380 echo -n > $testroot/stdout.expected
381 cmp -s $testroot/stdout.expected $testroot/stdout
383 if [ "$ret" != "0" ]; then
384 diff -u $testroot/stdout.expected $testroot/stdout
385 test_done "$testroot" "$ret"
389 echo "got: $testroot/wt: directory exists and is not empty" \
390 > $testroot/stderr.expected
391 cmp -s $testroot/stderr.expected $testroot/stderr
393 if [ "$ret" != "0" ]; then
394 diff -u $testroot/stderr.expected $testroot/stderr
395 test_done "$testroot" "$ret"
399 echo "U $testroot/wt/alpha" > $testroot/stdout.expected
400 echo "U $testroot/wt/beta" >> $testroot/stdout.expected
401 echo "U $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
402 echo "U $testroot/wt/gamma/delta" >> $testroot/stdout.expected
403 echo "Now shut up and hack" >> $testroot/stdout.expected
405 got checkout -E $testroot/repo $testroot/wt > $testroot/stdout
407 if [ "$ret" != "0" ]; then
408 test_done "$testroot" "$ret"
412 cmp -s $testroot/stdout.expected $testroot/stdout
414 if [ "$ret" != "0" ]; then
415 diff -u $testroot/stdout.expected $testroot/stdout
416 test_done "$testroot" "$ret"
420 echo "E $testroot/wt/alpha" > $testroot/stdout.expected
421 echo "E $testroot/wt/beta" >> $testroot/stdout.expected
422 echo "E $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
423 echo "E $testroot/wt/gamma/delta" >> $testroot/stdout.expected
424 echo "Now shut up and hack" >> $testroot/stdout.expected
426 got checkout -E $testroot/repo $testroot/wt > $testroot/stdout
428 if [ "$ret" != "0" ]; then
429 test_done "$testroot" "$ret"
433 cmp -s $testroot/stdout.expected $testroot/stdout
435 if [ "$ret" != "0" ]; then
436 diff -u $testroot/stdout.expected $testroot/stdout
437 test_done "$testroot" "$ret"
441 echo "alpha" > $testroot/content.expected
442 echo "beta" >> $testroot/content.expected
443 echo "zeta" >> $testroot/content.expected
444 echo "delta" >> $testroot/content.expected
445 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
446 $testroot/wt/gamma/delta > $testroot/content
448 cmp -s $testroot/content.expected $testroot/content
450 if [ "$ret" != "0" ]; then
451 diff -u $testroot/content.expected $testroot/content
452 test_done "$testroot" "$ret"
456 echo "modified alpha" > $testroot/wt/alpha
458 echo "E $testroot/wt/alpha" > $testroot/stdout.expected
459 echo "E $testroot/wt/beta" >> $testroot/stdout.expected
460 echo "E $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
461 echo "E $testroot/wt/gamma/delta" >> $testroot/stdout.expected
462 echo "Now shut up and hack" >> $testroot/stdout.expected
464 got checkout -E $testroot/repo $testroot/wt > $testroot/stdout
466 if [ "$ret" != "0" ]; then
467 test_done "$testroot" "$ret"
471 cmp -s $testroot/stdout.expected $testroot/stdout
473 if [ "$ret" != "0" ]; then
474 diff -u $testroot/stdout.expected $testroot/stdout
475 test_done "$testroot" "$ret"
479 echo "modified alpha" > $testroot/content.expected
480 echo "beta" >> $testroot/content.expected
481 echo "zeta" >> $testroot/content.expected
482 echo "delta" >> $testroot/content.expected
483 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
484 $testroot/wt/gamma/delta > $testroot/content
486 cmp -s $testroot/content.expected $testroot/content
488 if [ "$ret" != "0" ]; then
489 diff -u $testroot/content.expected $testroot/content
490 test_done "$testroot" "$ret"
494 echo 'M alpha' > $testroot/stdout.expected
495 (cd $testroot/wt && got status > $testroot/stdout)
497 cmp -s $testroot/stdout.expected $testroot/stdout
499 if [ "$ret" != "0" ]; then
500 diff -u $testroot/stdout.expected $testroot/stdout
502 test_done "$testroot" "$ret"
505 run_test test_checkout_basic
506 run_test test_checkout_dir_exists
507 run_test test_checkout_dir_not_empty
508 run_test test_checkout_sets_xbit
509 run_test test_checkout_commit_from_wrong_branch
510 run_test test_checkout_tag
511 run_test test_checkout_ignores_submodules
512 run_test test_checkout_read_only
513 run_test test_checkout_into_nonempty_dir