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 "got: target commit is on a different branch" \
201 > $testroot/stderr.expected
202 cmp -s $testroot/stderr.expected $testroot/stderr
204 if [ "$ret" != "0" ]; then
205 diff -u $testroot/stderr.expected $testroot/stderr
206 test_done "$testroot" "$ret"
210 test_done "$testroot" "$ret"
213 function test_checkout_tag {
214 local testroot=`test_init checkout_tag`
217 (cd $testroot/repo && git tag -a -m "test" $tag)
219 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
220 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
221 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
222 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
223 echo "Now shut up and hack" >> $testroot/stdout.expected
225 got checkout -c $tag $testroot/repo $testroot/wt > $testroot/stdout
227 if [ "$ret" != "0" ]; then
228 test_done "$testroot" "$ret"
232 cmp -s $testroot/stdout.expected $testroot/stdout
234 if [ "$ret" != "0" ]; then
235 diff -u $testroot/stdout.expected $testroot/stdout
236 test_done "$testroot" "$ret"
240 echo "alpha" > $testroot/content.expected
241 echo "beta" >> $testroot/content.expected
242 echo "zeta" >> $testroot/content.expected
243 echo "delta" >> $testroot/content.expected
244 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
245 $testroot/wt/gamma/delta > $testroot/content
247 cmp -s $testroot/content.expected $testroot/content
249 if [ "$ret" != "0" ]; then
250 diff -u $testroot/content.expected $testroot/content
252 test_done "$testroot" "$ret"
255 function test_checkout_ignores_submodules {
256 local testroot=`test_init checkout_ignores_submodules`
258 (cd $testroot && git clone -q repo repo2 >/dev/null)
259 (cd $testroot/repo && git submodule -q add ../repo2)
260 (cd $testroot/repo && git commit -q -m 'adding submodule')
262 echo "A $testroot/wt/.gitmodules" > $testroot/stdout.expected
263 echo "A $testroot/wt/alpha" >> $testroot/stdout.expected
264 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
265 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
266 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
267 echo "Now shut up and hack" >> $testroot/stdout.expected
269 got checkout $testroot/repo $testroot/wt > $testroot/stdout
271 if [ "$ret" != "0" ]; then
272 test_done "$testroot" "$ret"
276 cmp -s $testroot/stdout.expected $testroot/stdout
278 if [ "$ret" != "0" ]; then
279 diff -u $testroot/stdout.expected $testroot/stdout
280 test_done "$testroot" "$ret"
284 echo "alpha" > $testroot/content.expected
285 echo "beta" >> $testroot/content.expected
286 echo "zeta" >> $testroot/content.expected
287 echo "delta" >> $testroot/content.expected
288 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
289 $testroot/wt/gamma/delta > $testroot/content
291 cmp -s $testroot/content.expected $testroot/content
293 if [ "$ret" != "0" ]; then
294 diff -u $testroot/content.expected $testroot/content
296 test_done "$testroot" "$ret"
299 function test_checkout_read_only {
300 local testroot=`test_init checkout_read_only`
302 # Make the repostiory read-only
303 chmod -R a-w $testroot/repo
305 echo "A $testroot/wt/alpha" > $testroot/stdout.expected
306 echo "A $testroot/wt/beta" >> $testroot/stdout.expected
307 echo "A $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
308 echo "A $testroot/wt/gamma/delta" >> $testroot/stdout.expected
309 echo "Now shut up and hack" >> $testroot/stdout.expected
311 got checkout $testroot/repo $testroot/wt \
312 > $testroot/stdout 2> $testroot/stderr
314 if [ "$ret" != "0" ]; then
315 test_done "$testroot" "$ret"
319 cmp -s $testroot/stdout.expected $testroot/stdout
321 if [ "$ret" != "0" ]; then
322 diff -u $testroot/stdout.expected $testroot/stdout
323 test_done "$testroot" "$ret"
327 echo -n "got: warning: could not create a reference " \
328 > $testroot/stderr.expected
329 echo -n "to the work tree's base commit; the commit could " \
330 >> $testroot/stderr.expected
331 echo -n "be garbage-collected by Git; making the repository " \
332 >> $testroot/stderr.expected
333 echo "writable and running 'got update' will prevent this" \
334 >> $testroot/stderr.expected
335 cmp -s $testroot/stderr.expected $testroot/stderr
337 if [ "$ret" != "0" ]; then
338 diff -u $testroot/stderr.expected $testroot/stderr
339 test_done "$testroot" "$ret"
343 echo "alpha" > $testroot/content.expected
344 echo "beta" >> $testroot/content.expected
345 echo "zeta" >> $testroot/content.expected
346 echo "delta" >> $testroot/content.expected
347 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
348 $testroot/wt/gamma/delta > $testroot/content
350 cmp -s $testroot/content.expected $testroot/content
352 if [ "$ret" != "0" ]; then
353 diff -u $testroot/content.expected $testroot/content
355 chmod -R u+w $testroot/repo # make repo cleanup work
356 test_done "$testroot" "$ret"
359 function test_checkout_into_nonempty_dir {
360 local testroot=`test_init checkout_into_nonempty_dir`
362 mkdir -p $testroot/wt
363 make_test_tree $testroot/wt
365 got checkout $testroot/repo $testroot/wt > $testroot/stdout \
368 if [ "$ret" == "0" ]; then
369 echo "checkout succeeded unexpectedly" >&2
370 test_done "$testroot" "1"
374 echo -n > $testroot/stdout.expected
375 cmp -s $testroot/stdout.expected $testroot/stdout
377 if [ "$ret" != "0" ]; then
378 diff -u $testroot/stdout.expected $testroot/stdout
379 test_done "$testroot" "$ret"
383 echo "got: $testroot/wt: directory exists and is not empty" \
384 > $testroot/stderr.expected
385 cmp -s $testroot/stderr.expected $testroot/stderr
387 if [ "$ret" != "0" ]; then
388 diff -u $testroot/stderr.expected $testroot/stderr
389 test_done "$testroot" "$ret"
393 echo "U $testroot/wt/alpha" > $testroot/stdout.expected
394 echo "U $testroot/wt/beta" >> $testroot/stdout.expected
395 echo "U $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
396 echo "U $testroot/wt/gamma/delta" >> $testroot/stdout.expected
397 echo "Now shut up and hack" >> $testroot/stdout.expected
399 got checkout -E $testroot/repo $testroot/wt > $testroot/stdout
401 if [ "$ret" != "0" ]; then
402 test_done "$testroot" "$ret"
406 cmp -s $testroot/stdout.expected $testroot/stdout
408 if [ "$ret" != "0" ]; then
409 diff -u $testroot/stdout.expected $testroot/stdout
410 test_done "$testroot" "$ret"
414 echo "E $testroot/wt/alpha" > $testroot/stdout.expected
415 echo "E $testroot/wt/beta" >> $testroot/stdout.expected
416 echo "E $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
417 echo "E $testroot/wt/gamma/delta" >> $testroot/stdout.expected
418 echo "Now shut up and hack" >> $testroot/stdout.expected
420 got checkout -E $testroot/repo $testroot/wt > $testroot/stdout
422 if [ "$ret" != "0" ]; then
423 test_done "$testroot" "$ret"
427 cmp -s $testroot/stdout.expected $testroot/stdout
429 if [ "$ret" != "0" ]; then
430 diff -u $testroot/stdout.expected $testroot/stdout
431 test_done "$testroot" "$ret"
435 echo "alpha" > $testroot/content.expected
436 echo "beta" >> $testroot/content.expected
437 echo "zeta" >> $testroot/content.expected
438 echo "delta" >> $testroot/content.expected
439 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
440 $testroot/wt/gamma/delta > $testroot/content
442 cmp -s $testroot/content.expected $testroot/content
444 if [ "$ret" != "0" ]; then
445 diff -u $testroot/content.expected $testroot/content
446 test_done "$testroot" "$ret"
450 echo "modified alpha" > $testroot/wt/alpha
452 echo "E $testroot/wt/alpha" > $testroot/stdout.expected
453 echo "E $testroot/wt/beta" >> $testroot/stdout.expected
454 echo "E $testroot/wt/epsilon/zeta" >> $testroot/stdout.expected
455 echo "E $testroot/wt/gamma/delta" >> $testroot/stdout.expected
456 echo "Now shut up and hack" >> $testroot/stdout.expected
458 got checkout -E $testroot/repo $testroot/wt > $testroot/stdout
460 if [ "$ret" != "0" ]; then
461 test_done "$testroot" "$ret"
465 cmp -s $testroot/stdout.expected $testroot/stdout
467 if [ "$ret" != "0" ]; then
468 diff -u $testroot/stdout.expected $testroot/stdout
469 test_done "$testroot" "$ret"
473 echo "modified alpha" > $testroot/content.expected
474 echo "beta" >> $testroot/content.expected
475 echo "zeta" >> $testroot/content.expected
476 echo "delta" >> $testroot/content.expected
477 cat $testroot/wt/alpha $testroot/wt/beta $testroot/wt/epsilon/zeta \
478 $testroot/wt/gamma/delta > $testroot/content
480 cmp -s $testroot/content.expected $testroot/content
482 if [ "$ret" != "0" ]; then
483 diff -u $testroot/content.expected $testroot/content
484 test_done "$testroot" "$ret"
488 echo 'M alpha' > $testroot/stdout.expected
489 (cd $testroot/wt && got status > $testroot/stdout)
491 cmp -s $testroot/stdout.expected $testroot/stdout
493 if [ "$ret" != "0" ]; then
494 diff -u $testroot/stdout.expected $testroot/stdout
496 test_done "$testroot" "$ret"
499 run_test test_checkout_basic
500 run_test test_checkout_dir_exists
501 run_test test_checkout_dir_not_empty
502 run_test test_checkout_sets_xbit
503 run_test test_checkout_commit_from_wrong_branch
504 run_test test_checkout_tag
505 run_test test_checkout_ignores_submodules
506 run_test test_checkout_read_only
507 run_test test_checkout_into_nonempty_dir