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 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
29 ret="$?"
30 if [ "$ret" != "0" ]; then
31 test_done "$testroot" "$ret"
32 return 1
33 fi
35 cmp -s $testroot/stdout.expected $testroot/stdout
36 ret="$?"
37 if [ "$ret" != "0" ]; then
38 diff -u $testroot/stdout.expected $testroot/stdout
39 test_done "$testroot" "$ret"
40 return 1
41 fi
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
51 ret="$?"
52 if [ "$ret" != "0" ]; then
53 diff -u $testroot/content.expected $testroot/content
54 fi
55 test_done "$testroot" "$ret"
56 }
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
67 mkdir $testroot/wt
69 got checkout $testroot/repo $testroot/wt > $testroot/stdout
70 ret="$?"
71 if [ "$ret" != "0" ]; then
72 test_done "$testroot" "$ret"
73 return 1
74 fi
76 cmp -s $testroot/stdout.expected $testroot/stdout
77 ret="$?"
78 if [ "$ret" != "0" ]; then
79 diff -u $testroot/stdout.expected $testroot/stdout
80 test_done "$testroot" "$ret"
81 return 1
82 fi
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
92 ret="$?"
93 if [ "$ret" != "0" ]; then
94 diff -u $testroot/content.expected $testroot/content
95 fi
96 test_done "$testroot" "$ret"
97 }
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
108 mkdir $testroot/wt
109 touch $testroot/wt/foo
111 got checkout $testroot/repo $testroot/wt > $testroot/stdout \
112 2> $testroot/stderr
113 ret="$?"
114 if [ "$ret" == "0" ]; then
115 echo "checkout succeeded unexpectedly" >&2
116 test_done "$testroot" "1"
117 return 1
118 fi
120 echo "got: $testroot/wt: directory exists and is not empty" \
121 > $testroot/stderr.expected
122 cmp -s $testroot/stderr.expected $testroot/stderr
123 ret="$?"
124 if [ "$ret" != "0" ]; then
125 diff -u $testroot/stderr.expected $testroot/stderr
126 test_done "$testroot" "$ret"
127 return 1
128 fi
130 echo -n > $testroot/stdout.expected
131 cmp -s $testroot/stdout.expected $testroot/stdout
132 ret="$?"
133 if [ "$ret" != "0" ]; then
134 diff -u $testroot/stdout.expected $testroot/stdout
135 fi
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
152 ret="$?"
153 if [ "$ret" != "0" ]; then
154 test_done "$testroot" "$ret"
155 return 1
156 fi
158 cmp -s $testroot/stdout.expected $testroot/stdout
159 ret="$?"
160 if [ "$ret" != "0" ]; then
161 diff -u $testroot/stdout.expected $testroot/stdout
162 test_done "$testroot" "$ret"
163 return 1
164 fi
166 ls -l $testroot/wt/xfile | grep -q '^-rwx'
167 ret="$?"
168 if [ "$ret" != "0" ]; then
169 echo "file is not executable" >&2
170 ls -l $testroot/wt/xfile >&2
171 fi
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
185 ret="$?"
186 if [ "$ret" == "0" ]; then
187 test_done "$testroot" "1"
188 return 1
189 fi
191 echo -n "" > $testroot/stdout.expected
192 cmp -s $testroot/stdout.expected $testroot/stdout
193 ret="$?"
194 if [ "$ret" != "0" ]; then
195 diff -u $testroot/stdout.expected $testroot/stdout
196 test_done "$testroot" "$ret"
197 return 1
198 fi
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
209 ret="$?"
210 if [ "$ret" != "0" ]; then
211 diff -u $testroot/stderr.expected $testroot/stderr
212 test_done "$testroot" "$ret"
213 return 1
214 fi
216 test_done "$testroot" "$ret"
219 function test_checkout_tag {
220 local testroot=`test_init checkout_tag`
221 local tag="1.0.0"
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
232 ret="$?"
233 if [ "$ret" != "0" ]; then
234 test_done "$testroot" "$ret"
235 return 1
236 fi
238 cmp -s $testroot/stdout.expected $testroot/stdout
239 ret="$?"
240 if [ "$ret" != "0" ]; then
241 diff -u $testroot/stdout.expected $testroot/stdout
242 test_done "$testroot" "$ret"
243 return 1
244 fi
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
254 ret="$?"
255 if [ "$ret" != "0" ]; then
256 diff -u $testroot/content.expected $testroot/content
257 fi
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
276 ret="$?"
277 if [ "$ret" != "0" ]; then
278 test_done "$testroot" "$ret"
279 return 1
280 fi
282 cmp -s $testroot/stdout.expected $testroot/stdout
283 ret="$?"
284 if [ "$ret" != "0" ]; then
285 diff -u $testroot/stdout.expected $testroot/stdout
286 test_done "$testroot" "$ret"
287 return 1
288 fi
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
298 ret="$?"
299 if [ "$ret" != "0" ]; then
300 diff -u $testroot/content.expected $testroot/content
301 fi
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
319 ret="$?"
320 if [ "$ret" != "0" ]; then
321 test_done "$testroot" "$ret"
322 return 1
323 fi
325 cmp -s $testroot/stdout.expected $testroot/stdout
326 ret="$?"
327 if [ "$ret" != "0" ]; then
328 diff -u $testroot/stdout.expected $testroot/stdout
329 test_done "$testroot" "$ret"
330 return 1
331 fi
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
342 ret="$?"
343 if [ "$ret" != "0" ]; then
344 diff -u $testroot/stderr.expected $testroot/stderr
345 test_done "$testroot" "$ret"
346 return 1
347 fi
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
357 ret="$?"
358 if [ "$ret" != "0" ]; then
359 diff -u $testroot/content.expected $testroot/content
360 fi
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 \
372 2> $testroot/stderr
373 ret="$?"
374 if [ "$ret" == "0" ]; then
375 echo "checkout succeeded unexpectedly" >&2
376 test_done "$testroot" "1"
377 return 1
378 fi
380 echo -n > $testroot/stdout.expected
381 cmp -s $testroot/stdout.expected $testroot/stdout
382 ret="$?"
383 if [ "$ret" != "0" ]; then
384 diff -u $testroot/stdout.expected $testroot/stdout
385 test_done "$testroot" "$ret"
386 return 1
387 fi
389 echo "got: $testroot/wt: directory exists and is not empty" \
390 > $testroot/stderr.expected
391 cmp -s $testroot/stderr.expected $testroot/stderr
392 ret="$?"
393 if [ "$ret" != "0" ]; then
394 diff -u $testroot/stderr.expected $testroot/stderr
395 test_done "$testroot" "$ret"
396 return 1
397 fi
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
406 ret="$?"
407 if [ "$ret" != "0" ]; then
408 test_done "$testroot" "$ret"
409 return 1
410 fi
412 cmp -s $testroot/stdout.expected $testroot/stdout
413 ret="$?"
414 if [ "$ret" != "0" ]; then
415 diff -u $testroot/stdout.expected $testroot/stdout
416 test_done "$testroot" "$ret"
417 return 1
418 fi
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
427 ret="$?"
428 if [ "$ret" != "0" ]; then
429 test_done "$testroot" "$ret"
430 return 1
431 fi
433 cmp -s $testroot/stdout.expected $testroot/stdout
434 ret="$?"
435 if [ "$ret" != "0" ]; then
436 diff -u $testroot/stdout.expected $testroot/stdout
437 test_done "$testroot" "$ret"
438 return 1
439 fi
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
449 ret="$?"
450 if [ "$ret" != "0" ]; then
451 diff -u $testroot/content.expected $testroot/content
452 test_done "$testroot" "$ret"
453 return 1
454 fi
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
465 ret="$?"
466 if [ "$ret" != "0" ]; then
467 test_done "$testroot" "$ret"
468 return 1
469 fi
471 cmp -s $testroot/stdout.expected $testroot/stdout
472 ret="$?"
473 if [ "$ret" != "0" ]; then
474 diff -u $testroot/stdout.expected $testroot/stdout
475 test_done "$testroot" "$ret"
476 return 1
477 fi
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
487 ret="$?"
488 if [ "$ret" != "0" ]; then
489 diff -u $testroot/content.expected $testroot/content
490 test_done "$testroot" "$ret"
491 return
492 fi
494 echo 'M alpha' > $testroot/stdout.expected
495 (cd $testroot/wt && got status > $testroot/stdout)
497 cmp -s $testroot/stdout.expected $testroot/stdout
498 ret="$?"
499 if [ "$ret" != "0" ]; then
500 diff -u $testroot/stdout.expected $testroot/stdout
501 fi
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