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_branch_create() {
20 local testroot=`test_init branch_create`
21 local commit_id0=`git_show_head $testroot/repo`
23 # Create a branch based on repository's HEAD reference
24 got branch -r $testroot/repo newbranch
25 ret=$?
26 if [ $ret -ne 0 ]; then
27 echo "got branch command failed unexpectedly"
28 test_done "$testroot" "$ret"
29 return 1
30 fi
32 # Ensure that Git recognizes the branch Got has created
33 (cd $testroot/repo && git checkout -q newbranch)
34 ret=$?
35 if [ $ret -ne 0 ]; then
36 echo "git checkout command failed unexpectedly"
37 test_done "$testroot" "$ret"
38 return 1
39 fi
40 echo "modified delta on branch" > $testroot/repo/gamma/delta
41 git_commit $testroot/repo -m "committing to delta on newbranch"
43 got checkout -b newbranch $testroot/repo $testroot/wt >/dev/null
44 ret=$?
45 if [ $ret -ne 0 ]; then
46 echo "got checkout command failed unexpectedly"
47 test_done "$testroot" "$ret"
48 return 1
49 fi
51 echo "modified delta on branch" > $testroot/content.expected
52 cat $testroot/wt/gamma/delta > $testroot/content
53 cmp -s $testroot/content.expected $testroot/content
54 ret=$?
55 if [ $ret -ne 0 ]; then
56 diff -u $testroot/content.expected $testroot/content
57 test_done "$testroot" "$ret"
58 return 1
59 fi
61 # Create a branch based on the work tree's branch
62 (cd $testroot/wt && got branch -n refs/heads/anotherbranch)
63 ret=$?
64 if [ $ret -ne 0 ]; then
65 test_done "$testroot" "$ret"
66 return 1
67 fi
69 (cd $testroot/repo && git checkout -q anotherbranch)
70 ret=$?
71 if [ $ret -ne 0 ]; then
72 echo "git checkout command failed unexpectedly"
73 test_done "$testroot" "$ret"
74 return 1
75 fi
77 # Create a branch based on another specific branch
78 (cd $testroot/wt && got branch -n -c master yetanotherbranch)
79 ret=$?
80 if [ $ret -ne 0 ]; then
81 test_done "$testroot" "$ret"
82 return 1
83 fi
85 (cd $testroot/repo && git checkout -q yetanotherbranch)
86 ret=$?
87 if [ $ret -ne 0 ]; then
88 echo "git checkout command failed unexpectedly"
89 test_done "$testroot" "$ret"
90 return 1
91 fi
93 # Create a branch based on a specific commit
94 local commit_id=`git_show_head $testroot/repo`
95 got branch -r $testroot/repo -c $commit_id commitbranch
96 ret=$?
97 if [ $ret -ne 0 ]; then
98 echo "got branch command failed unexpectedly"
99 test_done "$testroot" "$ret"
100 return 1
101 fi
103 (cd $testroot/repo && git checkout -q commitbranch)
104 ret=$?
105 if [ $ret -ne 0 ]; then
106 echo "git checkout command failed unexpectedly"
107 test_done "$testroot" "$ret"
108 return 1
109 fi
111 # Create a branch and let the work tree be updated to it
112 (cd $testroot/wt && got branch -c $commit_id0 updatebranch \
113 > $testroot/stdout)
115 echo -n "Switching work tree from refs/heads/newbranch to " \
116 > $testroot/stdout.expected
117 echo "refs/heads/updatebranch" >> $testroot/stdout.expected
118 echo "U gamma/delta" >> $testroot/stdout.expected
119 echo "Updated to refs/heads/updatebranch: $commit_id0" \
120 >> $testroot/stdout.expected
122 cmp -s $testroot/stdout.expected $testroot/stdout
123 ret=$?
124 if [ $ret -ne 0 ]; then
125 diff -u $testroot/stdout.expected $testroot/stdout
126 fi
127 test_done "$testroot" "$ret"
130 test_branch_list() {
131 local testroot=`test_init branch_list`
132 local commit_id=`git_show_head $testroot/repo`
134 for b in branch1 branch2 branch3; do
135 got branch -r $testroot/repo $b
136 ret=$?
137 if [ $ret -ne 0 ]; then
138 echo "got branch command failed unexpectedly"
139 test_done "$testroot" "$ret"
140 return 1
141 fi
142 done
144 got branch -l -r $testroot/repo > $testroot/stdout
145 echo " branch1: $commit_id" > $testroot/stdout.expected
146 echo " branch2: $commit_id" >> $testroot/stdout.expected
147 echo " branch3: $commit_id" >> $testroot/stdout.expected
148 echo " master: $commit_id" >> $testroot/stdout.expected
149 cmp -s $testroot/stdout $testroot/stdout.expected
150 ret=$?
151 if [ $ret -ne 0 ]; then
152 diff -u $testroot/stdout.expected $testroot/stdout
153 test_done "$testroot" "$ret"
154 return 1
155 fi
157 got checkout $testroot/repo $testroot/wt >/dev/null
158 ret=$?
159 if [ $ret -ne 0 ]; then
160 echo "got checkout command failed unexpectedly"
161 test_done "$testroot" "$ret"
162 return 1
163 fi
165 (cd $testroot/wt && got branch -l > $testroot/stdout)
166 echo " branch1: $commit_id" > $testroot/stdout.expected
167 echo " branch2: $commit_id" >> $testroot/stdout.expected
168 echo " branch3: $commit_id" >> $testroot/stdout.expected
169 echo "* master: $commit_id" >> $testroot/stdout.expected
170 cmp -s $testroot/stdout $testroot/stdout.expected
171 ret=$?
172 if [ $ret -ne 0 ]; then
173 diff -u $testroot/stdout.expected $testroot/stdout
174 test_done "$testroot" "$ret"
175 return 1
176 fi
178 echo "modified delta" > $testroot/repo/gamma/delta
179 git_commit $testroot/repo -m "committing to delta"
180 local commit_id2=`git_show_head $testroot/repo`
182 (cd $testroot/wt && got branch -l > $testroot/stdout)
183 echo " branch1: $commit_id" > $testroot/stdout.expected
184 echo " branch2: $commit_id" >> $testroot/stdout.expected
185 echo " branch3: $commit_id" >> $testroot/stdout.expected
186 echo "~ master: $commit_id2" >> $testroot/stdout.expected
187 cmp -s $testroot/stdout $testroot/stdout.expected
188 ret=$?
189 if [ $ret -ne 0 ]; then
190 diff -u $testroot/stdout.expected $testroot/stdout
191 test_done "$testroot" "$ret"
192 return 1
193 fi
195 (cd $testroot/wt && got update > /dev/null)
196 ret=$?
197 if [ $ret -ne 0 ]; then
198 echo "got update command failed unexpectedly"
199 test_done "$testroot" "$ret"
200 return 1
201 fi
203 (cd $testroot/wt && got branch -l > $testroot/stdout)
204 echo " branch1: $commit_id" > $testroot/stdout.expected
205 echo " branch2: $commit_id" >> $testroot/stdout.expected
206 echo " branch3: $commit_id" >> $testroot/stdout.expected
207 echo "* master: $commit_id2" >> $testroot/stdout.expected
208 cmp -s $testroot/stdout $testroot/stdout.expected
209 ret=$?
210 if [ $ret -ne 0 ]; then
211 diff -u $testroot/stdout.expected $testroot/stdout
212 test_done "$testroot" "$ret"
213 return 1
214 fi
216 (cd $testroot/wt && got update -b branch1 > /dev/null)
217 ret=$?
218 if [ $ret -ne 0 ]; then
219 echo "got update command failed unexpectedly"
220 test_done "$testroot" "$ret"
221 return 1
222 fi
224 (cd $testroot/wt && got branch -l > $testroot/stdout)
225 echo "* branch1: $commit_id" > $testroot/stdout.expected
226 echo " branch2: $commit_id" >> $testroot/stdout.expected
227 echo " branch3: $commit_id" >> $testroot/stdout.expected
228 echo " master: $commit_id2" >> $testroot/stdout.expected
229 cmp -s $testroot/stdout $testroot/stdout.expected
230 ret=$?
231 if [ $ret -ne 0 ]; then
232 diff -u $testroot/stdout.expected $testroot/stdout
233 fi
234 test_done "$testroot" "$ret"
237 test_branch_delete() {
238 local testroot=`test_init branch_delete`
239 local commit_id=`git_show_head $testroot/repo`
241 for b in branch1 branch2 branch3; do
242 got branch -r $testroot/repo $b
243 ret=$?
244 if [ $ret -ne 0 ]; then
245 echo "got branch command failed unexpectedly"
246 test_done "$testroot" "$ret"
247 return 1
248 fi
249 done
251 got branch -d branch2 -r $testroot/repo > $testroot/stdout
252 ret=$?
253 if [ $ret -ne 0 ]; then
254 echo "got branch command failed unexpectedly"
255 test_done "$testroot" "$ret"
256 return 1
257 fi
259 got branch -l -r $testroot/repo > $testroot/stdout
260 echo " branch1: $commit_id" > $testroot/stdout.expected
261 echo " branch3: $commit_id" >> $testroot/stdout.expected
262 echo " master: $commit_id" >> $testroot/stdout.expected
263 cmp -s $testroot/stdout $testroot/stdout.expected
264 ret=$?
265 if [ $ret -ne 0 ]; then
266 diff -u $testroot/stdout.expected $testroot/stdout
267 test_done "$testroot" "$ret"
268 return 1
269 fi
271 got ref -l -r $testroot/repo > $testroot/stdout
272 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
273 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
274 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
275 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
276 cmp -s $testroot/stdout $testroot/stdout.expected
277 ret=$?
278 if [ $ret -ne 0 ]; then
279 diff -u $testroot/stdout.expected $testroot/stdout
280 test_done "$testroot" "$ret"
281 return 1
282 fi
284 got branch -d bogus_branch_name -r $testroot/repo \
285 > $testroot/stdout 2> $testroot/stderr
286 ret=$?
287 if [ $ret -eq 0 ]; then
288 echo "got branch succeeded unexpectedly"
289 test_done "$testroot" "1"
290 return 1
291 fi
293 echo "got: reference refs/heads/bogus_branch_name not found" \
294 > $testroot/stderr.expected
295 cmp -s $testroot/stderr $testroot/stderr.expected
296 ret=$?
297 if [ $ret -ne 0 ]; then
298 diff -u $testroot/stderr.expected $testroot/stderr
299 test_done "$testroot" "$ret"
300 return 1
301 fi
303 got ref -r $testroot/repo -c master refs/remotes/origin/master
304 got ref -r $testroot/repo -c branch1 refs/remotes/origin/branch1
305 got ref -r $testroot/repo -c branch3 refs/remotes/origin/branch3
307 got ref -l -r $testroot/repo > $testroot/stdout
308 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
309 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
310 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
311 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
312 echo "refs/remotes/origin/branch1: $commit_id" \
313 >> $testroot/stdout.expected
314 echo "refs/remotes/origin/branch3: $commit_id" \
315 >> $testroot/stdout.expected
316 echo "refs/remotes/origin/master: $commit_id" \
317 >> $testroot/stdout.expected
318 cmp -s $testroot/stdout $testroot/stdout.expected
319 ret=$?
320 if [ $ret -ne 0 ]; then
321 diff -u $testroot/stdout.expected $testroot/stdout
322 test_done "$testroot" "$ret"
323 return 1
324 fi
326 got branch -d origin/branch1 -r $testroot/repo > $testroot/stdout
327 ret=$?
328 if [ $ret -ne 0 ]; then
329 echo "got branch command failed unexpectedly"
330 test_done "$testroot" "$ret"
331 return 1
332 fi
334 got branch -d refs/remotes/origin/branch3 -r $testroot/repo \
335 > $testroot/stdout
336 ret=$?
337 if [ $ret -ne 0 ]; then
338 echo "got branch command failed unexpectedly"
339 test_done "$testroot" "$ret"
340 return 1
341 fi
343 got ref -l -r $testroot/repo > $testroot/stdout
344 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
345 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
346 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
347 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
348 echo "refs/remotes/origin/master: $commit_id" \
349 >> $testroot/stdout.expected
350 cmp -s $testroot/stdout $testroot/stdout.expected
351 ret=$?
352 if [ $ret -ne 0 ]; then
353 diff -u $testroot/stdout.expected $testroot/stdout
354 fi
355 test_done "$testroot" "$ret"
358 test_branch_delete_current_branch() {
359 local testroot=`test_init branch_delete_current_branch`
360 local commit_id=`git_show_head $testroot/repo`
362 got checkout $testroot/repo $testroot/wt >/dev/null
363 ret=$?
364 if [ $ret -ne 0 ]; then
365 echo "got checkout command failed unexpectedly"
366 test_done "$testroot" "$ret"
367 return 1
368 fi
370 (cd $testroot/wt && got branch -d master > $testroot/stdout \
371 2> $testroot/stderr)
373 echo "got: will not delete this work tree's current branch" \
374 > $testroot/stderr.expected
375 cmp -s $testroot/stderr $testroot/stderr.expected
376 ret=$?
377 if [ $ret -ne 0 ]; then
378 diff -u $testroot/stderr.expected $testroot/stderr
379 fi
380 test_done "$testroot" "$ret"
383 test_branch_delete_packed() {
384 local testroot=`test_init branch_delete_packed`
385 local commit_id=`git_show_head $testroot/repo`
387 for b in branch1 branch2 branch3; do
388 got branch -r $testroot/repo $b
389 ret=$?
390 if [ $ret -ne 0 ]; then
391 echo "got branch command failed unexpectedly"
392 test_done "$testroot" "$ret"
393 return 1
394 fi
395 done
397 (cd $testroot/repo && git pack-refs --all)
399 got branch -d refs/heads/branch2 -r $testroot/repo > $testroot/stdout
400 ret=$?
401 if [ $ret -ne 0 ]; then
402 echo "got update command failed unexpectedly"
403 test_done "$testroot" "$ret"
404 return 1
405 fi
407 got branch -l -r $testroot/repo > $testroot/stdout
408 echo " branch1: $commit_id" > $testroot/stdout.expected
409 echo " branch3: $commit_id" >> $testroot/stdout.expected
410 echo " master: $commit_id" >> $testroot/stdout.expected
411 cmp -s $testroot/stdout $testroot/stdout.expected
412 ret=$?
413 if [ $ret -ne 0 ]; then
414 diff -u $testroot/stdout.expected $testroot/stdout
415 test_done "$testroot" "$ret"
416 return 1
417 fi
419 got ref -l -r $testroot/repo > $testroot/stdout
420 echo "HEAD: refs/heads/master" > $testroot/stdout.expected
421 echo "refs/heads/branch1: $commit_id" >> $testroot/stdout.expected
422 echo "refs/heads/branch3: $commit_id" >> $testroot/stdout.expected
423 echo "refs/heads/master: $commit_id" >> $testroot/stdout.expected
424 cmp -s $testroot/stdout $testroot/stdout.expected
425 ret=$?
426 if [ $ret -ne 0 ]; then
427 diff -u $testroot/stdout.expected $testroot/stdout
428 test_done "$testroot" "$ret"
429 return 1
430 fi
432 got branch -d bogus_branch_name -r $testroot/repo \
433 > $testroot/stdout 2> $testroot/stderr
434 ret=$?
435 if [ $ret -eq 0 ]; then
436 echo "got update succeeded unexpectedly"
437 test_done "$testroot" "1"
438 return 1
439 fi
441 echo "got: reference refs/heads/bogus_branch_name not found" \
442 > $testroot/stderr.expected
443 cmp -s $testroot/stderr $testroot/stderr.expected
444 ret=$?
445 if [ $ret -ne 0 ]; then
446 diff -u $testroot/stderr.expected $testroot/stderr
447 fi
448 test_done "$testroot" "$ret"
451 test_branch_show() {
452 local testroot=`test_init branch_show`
453 local commit_id=`git_show_head $testroot/repo`
455 for b in branch1 branch2 branch3; do
456 got branch -r $testroot/repo $b
457 ret=$?
458 if [ $ret -ne 0 ]; then
459 echo "got branch command failed unexpectedly"
460 test_done "$testroot" "$ret"
461 return 1
462 fi
463 done
465 got checkout $testroot/repo $testroot/wt >/dev/null
466 ret=$?
467 if [ $ret -ne 0 ]; then
468 echo "got checkout command failed unexpectedly"
469 test_done "$testroot" "$ret"
470 return 1
471 fi
473 (cd $testroot/wt && got branch > $testroot/stdout)
474 echo "master" > $testroot/stdout.expected
475 cmp -s $testroot/stdout $testroot/stdout.expected
476 ret=$?
477 if [ $ret -ne 0 ]; then
478 diff -u $testroot/stdout.expected $testroot/stdout
479 test_done "$testroot" "$ret"
480 return 1
481 fi
483 (cd $testroot/wt && got update -b branch1 > /dev/null)
484 ret=$?
485 if [ $ret -ne 0 ]; then
486 echo "got update command failed unexpectedly"
487 test_done "$testroot" "$ret"
488 return 1
489 fi
491 (cd $testroot/wt && got branch > $testroot/stdout)
492 echo "branch1" > $testroot/stdout.expected
493 cmp -s $testroot/stdout $testroot/stdout.expected
494 ret=$?
495 if [ $ret -ne 0 ]; then
496 diff -u $testroot/stdout.expected $testroot/stdout
497 fi
498 test_done "$testroot" "$ret"
502 test_branch_packed_ref_collision() {
503 local testroot=`test_init branch_packed_ref_collision`
504 local commit_id=`git_show_head $testroot/repo`
506 got br -r $testroot/repo zoo > $testroot/stdout
507 got co -b zoo $testroot/repo $testroot/wt > /dev/null
508 echo "modified alpha" > $testroot/wt/alpha
510 # sleep in order to ensure that a significant fraction of time
511 # passes between commits; required for got branch -t option below
512 sleep 1
514 (cd $testroot/wt && got commit -m "modified alpha" >/dev/null)
515 local commit_id2=`git_show_branch_head $testroot/repo zoo`
517 # Fabricate a packed reference which points to an older commit
518 # and collides with the existing on-disk reference
519 echo '# pack-refs with: peeled fully-peeled sorted' > \
520 $testroot/repo/.git/packed-refs
521 echo "$commit_id refs/heads/zoo" >> $testroot/repo/.git/packed-refs
523 # Bug: This command used to show both packed and on-disk
524 # variants of ref/heads/zoo:
525 (cd $testroot/wt && got br -lt > $testroot/stdout)
527 echo "* zoo: $commit_id2" > $testroot/stdout.expected
528 echo " master: $commit_id" >> $testroot/stdout.expected
529 cmp -s $testroot/stdout $testroot/stdout.expected
530 ret=$?
531 if [ $ret -ne 0 ]; then
532 diff -u $testroot/stdout.expected $testroot/stdout
533 test_done "$testroot" "$ret"
534 return 1
535 fi
537 test_done "$testroot" "$ret"
540 test_parseargs "$@"
541 run_test test_branch_create
542 run_test test_branch_list
543 run_test test_branch_delete
544 run_test test_branch_delete_current_branch
545 run_test test_branch_delete_packed
546 run_test test_branch_show
547 run_test test_branch_packed_ref_collision